Add AWH biasing module + tests
[gromacs.git] / src / gromacs / gmxpreprocess / hizzie.cpp
blob5dfbe4947beb5d61a45a85927ac0b3bd7e0fcf83
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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,2017, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 /* This file is completely threadsafe - keep it that way! */
38 #include "gmxpre.h"
40 #include "hizzie.h"
42 #include <stdio.h>
43 #include <string.h>
45 #include <cmath>
47 #include "gromacs/fileio/pdbio.h"
48 #include "gromacs/gmxpreprocess/pdb2top.h"
49 #include "gromacs/gmxpreprocess/toputil.h"
50 #include "gromacs/math/functions.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/topology/block.h"
54 #include "gromacs/utility/arraysize.h"
55 #include "gromacs/utility/cstringutil.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/smalloc.h"
59 static int in_strings(char *key, int nstr, const char **str)
61 int j;
63 for (j = 0; (j < nstr); j++)
65 if (strcmp(str[j], key) == 0)
67 return j;
71 return -1;
74 static gmx_bool hbond(rvec x[], int i, int j, real distance)
76 real tol = distance*distance;
77 rvec tmp;
79 rvec_sub(x[i], x[j], tmp);
81 return (iprod(tmp, tmp) < tol);
84 static void chk_allhb(t_atoms *pdba, rvec x[], t_blocka *hb,
85 gmx_bool donor[], gmx_bool accept[], real dist)
87 int i, j, k, ii, natom;
89 natom = pdba->nr;
90 snew(hb->index, natom+1);
91 snew(hb->a, 6*natom);
92 hb->nr = natom;
93 hb->nra = 6*natom;
95 k = ii = 0;
96 hb->index[ii++] = 0;
97 for (i = 0; (i < natom); i++)
99 if (donor[i])
101 for (j = i+1; (j < natom); j++)
103 if ((accept[j]) && (hbond(x, i, j, dist)))
105 hb->a[k++] = j;
109 else if (accept[i])
111 for (j = i+1; (j < natom); j++)
113 if ((donor[j]) && (hbond(x, i, j, dist)))
115 hb->a[k++] = j;
119 hb->index[ii++] = k;
121 hb->nra = k;
124 static void pr_hbonds(FILE *fp, t_blocka *hb, t_atoms *pdba)
126 int i, j, k, j0, j1;
128 fprintf(fp, "Dumping all hydrogen bonds!\n");
129 for (i = 0; (i < hb->nr); i++)
131 j0 = hb->index[i];
132 j1 = hb->index[i+1];
133 for (j = j0; (j < j1); j++)
135 k = hb->a[j];
136 fprintf(fp, "%5s%4d%5s - %5s%4d%5s\n",
137 *pdba->resinfo[pdba->atom[i].resind].name,
138 pdba->resinfo[pdba->atom[i].resind].nr, *pdba->atomname[i],
139 *pdba->resinfo[pdba->atom[k].resind].name,
140 pdba->resinfo[pdba->atom[k].resind].nr, *pdba->atomname[k]);
145 static gmx_bool chk_hbonds(int i, t_atoms *pdba, rvec x[],
146 gmx_bool ad[], gmx_bool hbond[], rvec xh,
147 real angle, real dist)
149 gmx_bool bHB;
150 int j, aj, ri, natom;
151 real d2, dist2, a;
152 rvec nh, oh;
154 natom = pdba->nr;
155 bHB = FALSE;
156 ri = pdba->atom[i].resind;
157 dist2 = gmx::square(dist);
158 for (j = 0; (j < natom); j++)
160 /* Check whether the other atom is a donor/acceptor and not i */
161 if ((ad[j]) && (j != i))
163 /* Check whether the other atom is on the same ring as well */
164 if ((pdba->atom[j].resind != ri) ||
165 ((strcmp(*pdba->atomname[j], "ND1") != 0) &&
166 (strcmp(*pdba->atomname[j], "NE2") != 0)))
168 aj = j;
169 d2 = distance2(x[i], x[j]);
170 rvec_sub(x[i], xh, nh);
171 rvec_sub(x[aj], xh, oh);
172 a = RAD2DEG * acos(cos_angle(nh, oh));
173 if ((d2 < dist2) && (a > angle))
175 if (debug)
177 fprintf(debug,
178 "HBOND between %s%d-%s and %s%d-%s is %g nm, %g deg\n",
179 *pdba->resinfo[pdba->atom[i].resind].name,
180 pdba->resinfo[pdba->atom[i].resind].nr, *pdba->atomname[i],
181 *pdba->resinfo[pdba->atom[aj].resind].name,
182 pdba->resinfo[pdba->atom[aj].resind].nr, *pdba->atomname[aj],
183 std::sqrt(d2), a);
185 hbond[i] = TRUE;
186 bHB = TRUE;
191 return bHB;
194 static void calc_ringh(rvec xattach, rvec xb, rvec xc, rvec xh)
196 rvec tab, tac;
197 real n;
199 /* Add a proton on a ring to atom attach at distance 0.1 nm */
200 rvec_sub(xattach, xb, tab);
201 rvec_sub(xattach, xc, tac);
202 rvec_add(tab, tac, xh);
203 n = 0.1/norm(xh);
204 svmul(n, xh, xh);
205 rvec_inc(xh, xattach);
208 void set_histp(t_atoms *pdba, rvec *x, real angle, real dist)
210 static const char *prot_acc[] = {
211 "O", "OD1", "OD2", "OE1", "OE2", "OG", "OG1", "OH", "OW"
213 #define NPA asize(prot_acc)
214 static const char *prot_don[] = {
215 "N", "NH1", "NH2", "NE", "ND1", "ND2", "NE2", "NZ", "OG", "OG1", "OH", "NE1", "OW"
217 #define NPD asize(prot_don)
219 gmx_bool *donor, *acceptor;
220 gmx_bool *hbond;
221 gmx_bool bHDd, bHEd;
222 rvec xh1, xh2;
223 int natom;
224 int i, j, nd, na, hisind, type = -1;
225 int nd1, ne2, cg, cd2, ce1;
226 t_blocka *hb;
227 char *atomnm;
229 natom = pdba->nr;
231 i = 0;
232 while (i < natom &&
233 gmx_strcasecmp(*pdba->resinfo[pdba->atom[i].resind].name, "HIS") != 0)
235 i++;
237 if (natom == i)
239 return;
242 /* A histidine residue exists that requires automated assignment, so
243 * doing the analysis of donors and acceptors is worthwhile. */
244 fprintf(stderr,
245 "Analysing hydrogen-bonding network for automated assignment of histidine\n"
246 " protonation.");
248 snew(donor, natom);
249 snew(acceptor, natom);
250 snew(hbond, natom);
251 snew(hb, 1);
253 nd = na = 0;
254 for (j = 0; (j < natom); j++)
256 if (in_strings(*pdba->atomname[j], NPA, prot_acc) != -1)
258 acceptor[j] = TRUE;
259 na++;
261 if (in_strings(*pdba->atomname[j], NPD, prot_don) != -1)
263 donor[j] = TRUE;
264 nd++;
267 fprintf(stderr, " %d donors and %d acceptors were found.\n", nd, na);
268 chk_allhb(pdba, x, hb, donor, acceptor, dist);
269 if (debug)
271 pr_hbonds(debug, hb, pdba);
273 fprintf(stderr, "There are %d hydrogen bonds\n", hb->nra);
275 /* Now do the HIS stuff */
276 hisind = -1;
277 while (i < natom)
279 if (gmx_strcasecmp(*pdba->resinfo[pdba->atom[i].resind].name, "HIS") != 0)
281 i++;
283 else
285 if (pdba->atom[i].resind != hisind)
287 hisind = pdba->atom[i].resind;
289 /* Find the atoms in the ring */
290 nd1 = ne2 = cg = cd2 = ce1 = -1;
291 while (i < natom && pdba->atom[i].resind == hisind)
293 atomnm = *pdba->atomname[i];
294 if (strcmp(atomnm, "CD2") == 0)
296 cd2 = i;
298 else if (strcmp(atomnm, "CG") == 0)
300 cg = i;
302 else if (strcmp(atomnm, "CE1") == 0)
304 ce1 = i;
306 else if (strcmp(atomnm, "ND1") == 0)
308 nd1 = i;
310 else if (strcmp(atomnm, "NE2") == 0)
312 ne2 = i;
315 i++;
318 if (!((cg == -1 ) || (cd2 == -1) || (ce1 == -1) ||
319 (nd1 == -1) || (ne2 == -1)))
321 calc_ringh(x[nd1], x[cg], x[ce1], xh1);
322 calc_ringh(x[ne2], x[ce1], x[cd2], xh2);
324 bHDd = chk_hbonds(nd1, pdba, x, acceptor, hbond, xh1, angle, dist);
325 chk_hbonds(nd1, pdba, x, donor, hbond, xh1, angle, dist);
326 bHEd = chk_hbonds(ne2, pdba, x, acceptor, hbond, xh2, angle, dist);
327 chk_hbonds(ne2, pdba, x, donor, hbond, xh2, angle, dist);
329 if (bHDd)
331 if (bHEd)
333 type = ehisH;
335 else
337 type = ehisA;
340 else
342 type = ehisB;
344 fprintf(stderr, "Will use %s for residue %d\n",
345 hh[type], pdba->resinfo[hisind].nr);
347 else
349 gmx_fatal(FARGS, "Incomplete ring in HIS%d",
350 pdba->resinfo[hisind].nr);
353 snew(pdba->resinfo[hisind].rtp, 1);
354 *pdba->resinfo[hisind].rtp = gmx_strdup(hh[type]);
358 done_blocka(hb);
359 sfree(hb);
360 sfree(donor);
361 sfree(acceptor);
362 sfree(hbond);