Remove unused thole polarization rfac parameter
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / specbond.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "specbond.h"
41
42 #include <cctype>
43 #include <cmath>
44 #include <cstring>
45
46 #include <algorithm>
47
48 #include "gromacs/fileio/pdbio.h"
49 #include "gromacs/gmxpreprocess/pdb2top.h"
50 #include "gromacs/math/vec.h"
51 #include "gromacs/utility/arrayref.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/smalloc.h"
55 #include "gromacs/utility/strdb.h"
56
57 struct SpecialBond
58 {
59     std::string firstResidue, secondResidue;
60     std::string firstAtomName, secondAtomName;
61     std::string newFirstResidue, newSecondResidue;
62     int         firstBondNumber, secondBondNumber;
63     real        length;
64 };
65
66 static bool yesno()
67 {
68     char c;
69
70     do
71     {
72         c = toupper(fgetc(stdin));
73     } while ((c != 'Y') && (c != 'N'));
74
75     return (c == 'Y');
76 }
77
78 std::vector<SpecialBond> generateSpecialBonds()
79 {
80     const char* sbfile = "specbond.dat";
81
82     std::vector<SpecialBond> specialBonds;
83     char                     r1buf[32], r2buf[32], a1buf[32], a2buf[32], nr1buf[32], nr2buf[32];
84     double                   length;
85     int                      nb1, nb2;
86     char**                   lines;
87
88     int nlines = get_lines(sbfile, &lines);
89     for (int i = 0; (i < nlines); i++)
90     {
91         if (sscanf(lines[i], "%s%s%d%s%s%d%lf%s%s", r1buf, a1buf, &nb1, r2buf, a2buf, &nb2, &length, nr1buf, nr2buf)
92             != 9)
93         {
94             fprintf(stderr, "Invalid line '%s' in %s\n", lines[i], sbfile);
95         }
96         else
97         {
98             SpecialBond newBond;
99
100             newBond.firstResidue     = r1buf;
101             newBond.secondResidue    = r2buf;
102             newBond.newFirstResidue  = nr1buf;
103             newBond.newSecondResidue = nr2buf;
104             newBond.firstAtomName    = a1buf;
105             newBond.secondAtomName   = a2buf;
106             newBond.firstBondNumber  = nb1;
107             newBond.secondBondNumber = nb2;
108             newBond.length           = length;
109             specialBonds.push_back(newBond);
110         }
111         sfree(lines[i]);
112     }
113     if (nlines > 0)
114     {
115         sfree(lines);
116     }
117     fprintf(stderr, "%zu out of %d lines of %s converted successfully\n", specialBonds.size(), nlines, sbfile);
118
119     return specialBonds;
120 }
121
122 static bool is_special(gmx::ArrayRef<const SpecialBond> sb, const char* res, const char* atom)
123 {
124     return std::any_of(sb.begin(), sb.end(), [res, atom](const auto& bond) {
125         return (((strncmp(bond.firstResidue.c_str(), res, 3) == 0)
126                  && (gmx::equalCaseInsensitive(bond.firstAtomName, atom)))
127                 || ((strncmp(bond.secondResidue.c_str(), res, 3) == 0)
128                     && (gmx::equalCaseInsensitive(bond.secondAtomName, atom))));
129     });
130 }
131
132 static bool is_bond(gmx::ArrayRef<const SpecialBond> sb, t_atoms* pdba, int a1, int a2, real d, int* index_sb, bool* bSwap)
133 {
134     const char* at1  = *pdba->atomname[a1];
135     const char* at2  = *pdba->atomname[a2];
136     const char* res1 = *pdba->resinfo[pdba->atom[a1].resind].name;
137     const char* res2 = *pdba->resinfo[pdba->atom[a2].resind].name;
138
139     int i = 0;
140     for (const auto& bond : sb)
141     {
142         *index_sb = i;
143         if (((strncmp(bond.firstResidue.c_str(), res1, 3) == 0)
144              && (gmx::equalCaseInsensitive(bond.firstAtomName, at1))
145              && (strncmp(bond.secondResidue.c_str(), res2, 3) == 0)
146              && (gmx::equalCaseInsensitive(bond.secondAtomName, at2))))
147         {
148             *bSwap = FALSE;
149             if ((0.9 * bond.length < d) && (1.1 * bond.length > d))
150             {
151                 return TRUE;
152             }
153         }
154         if (((strncmp(bond.firstResidue.c_str(), res2, 3) == 0)
155              && (gmx::equalCaseInsensitive(bond.firstAtomName, at2))
156              && (strncmp(bond.secondResidue.c_str(), res1, 3) == 0)
157              && (gmx::equalCaseInsensitive(bond.secondAtomName, at1))))
158         {
159             *bSwap = TRUE;
160             if ((0.9 * bond.length < d) && (1.1 * bond.length > d))
161             {
162                 return TRUE;
163             }
164         }
165         i++;
166     }
167     return FALSE;
168 }
169
170 static void rename_1res(t_atoms* pdba, int resind, const char* newres, bool bVerbose)
171 {
172     if (bVerbose)
173     {
174         printf("Using rtp entry %s for %s %d\n",
175                newres,
176                *pdba->resinfo[resind].name,
177                pdba->resinfo[resind].nr);
178     }
179     /* this used to free *resname, which messes up the symtab! */
180     snew(pdba->resinfo[resind].rtp, 1);
181     *pdba->resinfo[resind].rtp = gmx_strdup(newres);
182 }
183
184 std::vector<DisulfideBond> makeDisulfideBonds(t_atoms* pdba, rvec x[], bool bInteractive, bool bVerbose)
185 {
186     std::vector<SpecialBond>   specialBonds = generateSpecialBonds();
187     std::vector<DisulfideBond> bonds;
188     bool                       bSwap;
189     int                        index_sb;
190     char                       buf[10];
191
192
193     if (!specialBonds.empty())
194     {
195         std::vector<int> specialBondResIdxs;
196         std::vector<int> specialBondAtomIdxs;
197
198         for (int i = 0; (i < pdba->nr); i++)
199         {
200             /* Check if this atom is special and if it is not a double atom
201              * in the input that still needs to be removed.
202              */
203             int prevAtom = -1;
204             if (!specialBondAtomIdxs.empty())
205             {
206                 prevAtom = specialBondAtomIdxs.back();
207             }
208             if (is_special(specialBonds, *pdba->resinfo[pdba->atom[i].resind].name, *pdba->atomname[i])
209                 && !(!specialBondAtomIdxs.empty() && pdba->atom[prevAtom].resind == pdba->atom[i].resind
210                      && gmx_strcasecmp(*pdba->atomname[prevAtom], *pdba->atomname[i]) == 0))
211             {
212                 specialBondResIdxs.push_back(pdba->atom[i].resind);
213                 specialBondAtomIdxs.push_back(i);
214             }
215         }
216         /* distance matrix d[nspec][nspec] */
217         int                            nspec = specialBondAtomIdxs.size();
218         std::vector<std::vector<real>> d(nspec);
219         for (int i = 0; (i < nspec); i++)
220         {
221             d[i].resize(nspec);
222             int ai = specialBondAtomIdxs[i];
223             for (int j = 0; (j < nspec); j++)
224             {
225                 int aj  = specialBondAtomIdxs[j];
226                 d[i][j] = std::sqrt(distance2(x[ai], x[aj]));
227             }
228         }
229         if (nspec > 1)
230         {
231 #define MAXCOL 7
232             fprintf(stderr, "Special Atom Distance matrix:\n");
233             for (int b = 0; (b < nspec); b += MAXCOL)
234             {
235                 /* print resname/number column headings */
236                 fprintf(stderr, "%8s%8s", "", "");
237                 int e = std::min(b + MAXCOL, nspec - 1);
238                 for (int i = b; (i < e); i++)
239                 {
240                     sprintf(buf,
241                             "%s%d",
242                             *pdba->resinfo[pdba->atom[specialBondAtomIdxs[i]].resind].name,
243                             pdba->resinfo[specialBondResIdxs[i]].nr);
244                     fprintf(stderr, "%8s", buf);
245                 }
246                 fprintf(stderr, "\n");
247                 /* print atomname/number column headings */
248                 fprintf(stderr, "%8s%8s", "", "");
249                 e = std::min(b + MAXCOL, nspec - 1);
250                 for (int i = b; (i < e); i++)
251                 {
252                     std::string buf = gmx::formatString(
253                             "%s%d", *pdba->atomname[specialBondAtomIdxs[i]], specialBondAtomIdxs[i] + 1);
254                     fprintf(stderr, "%8s", buf.c_str());
255                 }
256                 fprintf(stderr, "\n");
257                 /* print matrix */
258                 e = std::min(b + MAXCOL, nspec);
259                 for (int i = b + 1; (i < nspec); i++)
260                 {
261                     std::string buf = gmx::formatString(
262                             "%s%d",
263                             *pdba->resinfo[pdba->atom[specialBondAtomIdxs[i]].resind].name,
264                             pdba->resinfo[specialBondResIdxs[i]].nr);
265                     fprintf(stderr, "%8s", buf.c_str());
266                     buf = gmx::formatString(
267                             "%s%d", *pdba->atomname[specialBondAtomIdxs[i]], specialBondAtomIdxs[i] + 1);
268                     fprintf(stderr, "%8s", buf.c_str());
269                     int e2 = std::min(i, e);
270                     for (int j = b; (j < e2); j++)
271                     {
272                         fprintf(stderr, " %7.3f", d[i][j]);
273                     }
274                     fprintf(stderr, "\n");
275                 }
276             }
277         }
278
279         for (int i = 0; (i < nspec); i++)
280         {
281             int ai = specialBondAtomIdxs[i];
282             for (int j = i + 1; (j < nspec); j++)
283             {
284                 int aj = specialBondAtomIdxs[j];
285                 /* Ensure creation of at most nspec special bonds to avoid overflowing bonds[] */
286                 if (bonds.size() < specialBondAtomIdxs.size()
287                     && is_bond(specialBonds, pdba, ai, aj, d[i][j], &index_sb, &bSwap))
288                 {
289                     fprintf(stderr,
290                             "%s %s-%d %s-%d and %s-%d %s-%d%s",
291                             bInteractive ? "Link" : "Linking",
292                             *pdba->resinfo[pdba->atom[ai].resind].name,
293                             pdba->resinfo[specialBondResIdxs[i]].nr,
294                             *pdba->atomname[ai],
295                             ai + 1,
296                             *pdba->resinfo[pdba->atom[aj].resind].name,
297                             pdba->resinfo[specialBondResIdxs[j]].nr,
298                             *pdba->atomname[aj],
299                             aj + 1,
300                             bInteractive ? " (y/n) ?" : "...\n");
301                     bool bDoit = bInteractive ? yesno() : true;
302
303                     if (bDoit)
304                     {
305                         DisulfideBond newBond;
306                         /* Store the residue numbers in the bonds array */
307                         newBond.firstResidue  = specialBondResIdxs[i];
308                         newBond.secondResidue = specialBondResIdxs[j];
309                         newBond.firstAtom     = *pdba->atomname[ai];
310                         newBond.secondAtom    = *pdba->atomname[aj];
311                         bonds.push_back(newBond);
312                         /* rename residues */
313                         if (bSwap)
314                         {
315                             rename_1res(pdba,
316                                         specialBondResIdxs[i],
317                                         specialBonds[index_sb].newSecondResidue.c_str(),
318                                         bVerbose);
319                             rename_1res(pdba,
320                                         specialBondResIdxs[j],
321                                         specialBonds[index_sb].newFirstResidue.c_str(),
322                                         bVerbose);
323                         }
324                         else
325                         {
326                             rename_1res(pdba,
327                                         specialBondResIdxs[i],
328                                         specialBonds[index_sb].newFirstResidue.c_str(),
329                                         bVerbose);
330                             rename_1res(pdba,
331                                         specialBondResIdxs[j],
332                                         specialBonds[index_sb].newSecondResidue.c_str(),
333                                         bVerbose);
334                         }
335                     }
336                 }
337             }
338         }
339     }
340
341     return bonds;
342 }