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,2016,2017,2018,2019, 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.
45 #include "gromacs/fileio/confio.h"
46 #include "gromacs/fileio/trxio.h"
47 #include "gromacs/fileio/xvgr.h"
48 #include "gromacs/gmxana/gstat.h"
49 #include "gromacs/listed_forces/bonded.h"
50 #include "gromacs/math/functions.h"
51 #include "gromacs/math/units.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/math/vecdump.h"
54 #include "gromacs/pbcutil/pbc.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/smalloc.h"
58 void print_one(const gmx_output_env_t* oenv,
68 char buf[256], t2[256];
71 sprintf(buf, "%s%s.xvg", base, name);
72 fprintf(stderr, "\rPrinting %s ", buf);
74 sprintf(t2, "%s %s", title, name);
75 fp = xvgropen(buf, t2, "Time (ps)", ylabel, oenv);
76 for (k = 0; (k < nf); k++)
78 fprintf(fp, "%10g %10g\n", time[k], data[k]);
83 static int calc_RBbin(real phi, int gmx_unused multiplicity, real gmx_unused core_frac)
85 /* multiplicity and core_frac NOT used,
86 * just given to enable use of pt-to-fn in caller low_ana_dih_trans*/
87 static const real r30 = M_PI / 6.0;
88 static const real r90 = M_PI / 2.0;
89 static const real r150 = M_PI * 5.0 / 6.0;
91 if ((phi < r30) && (phi > -r30))
95 else if ((phi > -r150) && (phi < -r90))
99 else if ((phi < r150) && (phi > r90))
106 static int calc_Nbin(real phi, int multiplicity, real core_frac)
108 static const real r360 = 360 * DEG2RAD;
109 real rot_width, core_width, core_offset, low, hi;
111 /* with multiplicity 3 and core_frac 0.5
112 * 0<g(-)<120, 120<t<240, 240<g(+)<360
113 * 0< bin0 < 30, 30<bin1<90, 90<bin0<150, 150<bin2<210, 210<bin0<270, 270<bin3<330, 330<bin0<360
114 * so with multiplicity 3, bin1 is core g(-), bin2 is core t, bin3 is
115 core g(+), bin0 is between rotamers */
121 rot_width = 360. / multiplicity;
122 core_width = core_frac * rot_width;
123 core_offset = (rot_width - core_width) / 2.0;
124 for (bin = 1; bin <= multiplicity; bin++)
126 low = ((bin - 1) * rot_width) + core_offset;
127 hi = ((bin - 1) * rot_width) + core_offset + core_width;
130 if ((phi > low) && (phi < hi))
138 void ana_dih_trans(const char* fn_trans,
139 const char* fn_histo,
146 const gmx_output_env_t* oenv)
148 /* just a wrapper; declare extra args, then chuck away at end. */
156 snew(multiplicity, nangles);
157 for (k = 0; (k < nangles); k++)
162 low_ana_dih_trans(TRUE, fn_trans, TRUE, fn_histo, maxchi, dih, nlist, dlist, nframes, nangles,
163 grpname, multiplicity, time, bRb, 0.5, oenv);
168 void low_ana_dih_trans(gmx_bool bTrans,
169 const char* fn_trans,
171 const char* fn_histo,
183 const gmx_output_env_t* oenv)
188 int i, j, k, Dih, ntrans;
189 int cur_bin, new_bin;
192 int (*calc_bin)(real, int, real);
199 /* Assumes the frames are equally spaced in time */
200 dt = (time[nframes - 1] - time[0]) / (nframes - 1);
202 /* Analysis of dihedral transitions */
203 fprintf(stderr, "Now calculating transitions...\n");
207 calc_bin = calc_RBbin;
211 calc_bin = calc_Nbin;
214 for (k = 0; k < NROT; k++)
216 snew(rot_occ[k], nangles);
217 for (i = 0; (i < nangles); i++)
225 /* dih[i][j] is the dihedral angle i in frame j */
227 for (i = 0; (i < nangles); i++)
232 mind = maxd = prev = dih[i][0];
234 cur_bin = calc_bin(dih[i][0], multiplicity[i], core_frac);
235 rot_occ[cur_bin][i]++;
237 for (j = 1; (j < nframes); j++)
239 new_bin = calc_bin(dih[i][j], multiplicity[i], core_frac);
240 rot_occ[new_bin][i]++;
246 else if ((new_bin != 0) && (cur_bin != new_bin))
254 /* why is all this md rubbish periodic? Remove 360 degree periodicity */
255 if ((dih[i][j] - prev) > M_PI)
257 dih[i][j] -= 2 * M_PI;
259 else if ((dih[i][j] - prev) < -M_PI)
261 dih[i][j] += 2 * M_PI;
266 mind = std::min(mind, dih[i][j]);
267 maxd = std::max(maxd, dih[i][j]);
268 if ((maxd - mind) > 2 * M_PI / 3) /* or 120 degrees, assuming */
269 { /* multiplicity 3. Not so general.*/
272 maxd = mind = dih[i][j]; /* get ready for next transition */
277 for (k = 0; k < NROT; k++)
279 rot_occ[k][i] /= nframes;
282 fprintf(stderr, "Total number of transitions: %10d\n", ntrans);
285 ttime = (dt * nframes * nangles) / ntrans;
286 fprintf(stderr, "Time between transitions: %10.3f ps\n", ttime);
289 /* new by grs - copy transitions from tr_h[] to dlist->ntr[]
290 * and rotamer populations from rot_occ to dlist->rot_occ[]
291 * based on fn histogramming in g_chi. diff roles for i and j here */
294 for (Dih = 0; (Dih < NONCHI + maxchi); Dih++)
296 for (i = 0; (i < nlist); i++)
298 if (((Dih < edOmega)) || ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i]))))
299 || ((Dih > edOmega) && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1)))
301 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
302 dlist[i].ntr[Dih] = tr_h[j];
303 for (k = 0; k < NROT; k++)
305 dlist[i].rot_occ[Dih][k] = rot_occ[k][j];
312 /* end addition by grs */
316 sprintf(title, "Number of transitions: %s", grpname);
317 fp = xvgropen(fn_trans, title, "Time (ps)", "# transitions/timeframe", oenv);
318 for (j = 0; (j < nframes); j++)
320 fprintf(fp, "%10.3f %10d\n", time[j], tr_f[j]);
325 /* Compute histogram from # transitions per dihedral */
327 for (j = 0; (j < nframes); j++)
331 for (i = 0; (i < nangles); i++)
335 for (j = nframes; ((tr_f[j - 1] == 0) && (j > 0)); j--) {}
337 ttime = dt * nframes;
340 sprintf(title, "Transition time: %s", grpname);
341 fp = xvgropen(fn_histo, title, "Time (ps)", "#", oenv);
342 for (i = j - 1; (i > 0); i--)
346 fprintf(fp, "%10.3f %10d\n", ttime / i, tr_f[i]);
354 for (k = 0; k < NROT; k++)
360 void mk_multiplicity_lookup(int* multiplicity, int maxchi, int nlist, t_dlist dlist[], int nangles)
362 /* new by grs - for dihedral j (as in dih[j]) get multiplicity from dlist
363 * and store in multiplicity[j]
370 for (Dih = 0; (Dih < NONCHI + maxchi); Dih++)
372 for (i = 0; (i < nlist); i++)
374 std::strncpy(name, dlist[i].name, 3);
376 if (((Dih < edOmega)) || ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i]))))
377 || ((Dih > edOmega) && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1)))
379 /* default - we will correct the rest below */
382 /* make omegas 2fold, though doesn't make much more sense than 3 */
383 if (Dih == edOmega && (has_dihedral(edOmega, &(dlist[i]))))
388 /* dihedrals to aromatic rings, COO, CONH2 or guanidinium are 2fold*/
389 if (Dih > edOmega && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1))
391 if (((std::strstr(name, "PHE") != nullptr) && (Dih == edChi2))
392 || ((std::strstr(name, "TYR") != nullptr) && (Dih == edChi2))
393 || ((std::strstr(name, "PTR") != nullptr) && (Dih == edChi2))
394 || ((std::strstr(name, "TRP") != nullptr) && (Dih == edChi2))
395 || ((std::strstr(name, "HIS") != nullptr) && (Dih == edChi2))
396 || ((std::strstr(name, "GLU") != nullptr) && (Dih == edChi3))
397 || ((std::strstr(name, "ASP") != nullptr) && (Dih == edChi2))
398 || ((std::strstr(name, "GLN") != nullptr) && (Dih == edChi3))
399 || ((std::strstr(name, "ASN") != nullptr) && (Dih == edChi2))
400 || ((std::strstr(name, "ARG") != nullptr) && (Dih == edChi4)))
411 fprintf(stderr, "WARNING: not all dihedrals found in topology (only %d out of %d)!\n", j, nangles);
413 /* Check for remaining dihedrals */
414 for (; (j < nangles); j++)
420 void mk_chi_lookup(int** lookup, int maxchi, int nlist, t_dlist dlist[])
423 /* by grs. should rewrite everything to use this. (but haven't,
424 * and at mmt only used in get_chi_product_traj
425 * returns the dihed number given the residue number (from-0)
426 * and chi (from-0) nr. -1 for chi undefined for that res (eg gly, ala..)*/
431 /* NONCHI points to chi1, therefore we have to start counting there. */
432 for (Dih = NONCHI; (Dih < NONCHI + maxchi); Dih++)
434 for (i = 0; (i < nlist); i++)
437 if (((Dih < edOmega)) || ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i]))))
438 || ((Dih > edOmega) && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1)))
440 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
456 void get_chi_product_traj(real** dih,
469 const gmx_output_env_t* oenv)
472 gmx_bool bRotZero, bHaveChi = FALSE;
473 int accum = 0, index, i, j, k, Xi, n, b;
478 char hisfile[256], histitle[256], *namept;
480 int (*calc_bin)(real, int, real);
482 /* Analysis of dihedral transitions */
483 fprintf(stderr, "Now calculating Chi product trajectories...\n");
487 calc_bin = calc_RBbin;
491 calc_bin = calc_Nbin;
494 snew(chi_prtrj, nframes);
496 /* file for info on all residues */
499 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "Probability", oenv);
503 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "# Counts", oenv);
506 for (i = 0; (i < nlist); i++)
509 /* get nbin, the nr. of cumulative rotamers that need to be considered */
511 for (Xi = 0; Xi < maxchi; Xi++)
513 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
516 n = multiplicity[index];
520 nbin += 1; /* for the "zero rotamer", outside the core region */
522 for (j = 0; (j < nframes); j++)
527 index = lookup[i][0]; /* index into dih of chi1 of res i */
535 b = calc_bin(dih[index][j], multiplicity[index], core_frac);
541 for (Xi = 1; Xi < maxchi; Xi++)
543 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
546 n = multiplicity[index];
547 b = calc_bin(dih[index][j], n, core_frac);
548 accum = n * accum + b - 1;
563 chi_prtrj[j] = accum;
564 if (accum + 1 > nbin)
575 /* print cuml rotamer vs time */
576 print_one(oenv, "chiproduct", dlist[i].name, "chi product for",
577 "cumulative rotamer", nframes, time, chi_prtrj);
580 /* make a histogram pf culm. rotamer occupancy too */
581 snew(chi_prhist, nbin);
582 make_histo(nullptr, nframes, chi_prtrj, nbin, chi_prhist, 0, nbin);
585 sprintf(hisfile, "histo-chiprod%s.xvg", dlist[i].name);
586 sprintf(histitle, "cumulative rotamer distribution for %s", dlist[i].name);
587 fprintf(stderr, " and %s ", hisfile);
588 fp = xvgropen(hisfile, histitle, "number", "", oenv);
589 if (output_env_get_print_xvgr_codes(oenv))
591 fprintf(fp, "@ xaxis tick on\n");
592 fprintf(fp, "@ xaxis tick major 1\n");
593 fprintf(fp, "@ type xy\n");
595 for (k = 0; (k < nbin); k++)
599 fprintf(fp, "%5d %10g\n", k, (1.0 * chi_prhist[k]) / nframes);
603 fprintf(fp, "%5d %10d\n", k, chi_prhist[k]);
606 fprintf(fp, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
610 /* and finally print out occupancies to a single file */
611 /* get the gmx from-1 res nr by setting a ptr to the number part
612 * of dlist[i].name - potential bug for 4-letter res names... */
613 namept = dlist[i].name + 3;
614 fprintf(fpall, "%5s ", namept);
615 for (k = 0; (k < nbin); k++)
619 fprintf(fpall, " %10g", (1.0 * chi_prhist[k]) / nframes);
623 fprintf(fpall, " %10d", chi_prhist[k]);
626 fprintf(fpall, "\n");
635 fprintf(stderr, "\n");
638 void calc_distribution_props(int nh, const int histo[], real start, int nkkk, t_karplus kkk[], real* S2)
640 real d, dc, ds, c1, c2, tdc, tds;
641 real fac, ang, invth, Jc;
646 gmx_fatal(FARGS, "No points in histogram (%s, %d)", __FILE__, __LINE__);
650 /* Compute normalisation factor */
652 for (j = 0; (j < nh); j++)
658 for (i = 0; (i < nkkk); i++)
665 for (j = 0; (j < nh); j++)
667 d = invth * histo[j];
668 ang = j * fac - start;
671 ds = d * std::sin(ang);
674 for (i = 0; (i < nkkk); i++)
676 c1 = std::cos(ang + kkk[i].offset);
678 Jc = (kkk[i].A * c2 + kkk[i].B * c1 + kkk[i].C);
679 kkk[i].Jc += histo[j] * Jc;
680 kkk[i].Jcsig += histo[j] * gmx::square(Jc);
683 for (i = 0; (i < nkkk); i++)
686 kkk[i].Jcsig = std::sqrt(kkk[i].Jcsig / th - gmx::square(kkk[i].Jc));
688 *S2 = tdc * tdc + tds * tds;
691 static void calc_angles(struct t_pbc* pbc, int n3, int index[], real ang[], rvec x_s[])
697 for (i = ix = 0; (ix < n3); i++, ix += 3)
699 ang[i] = bond_angle(x_s[index[ix]], x_s[index[ix + 1]], x_s[index[ix + 2]], pbc, r_ij, r_kj,
704 fprintf(debug, "Angle[0]=%g, costh=%g, index0 = %d, %d, %d\n", ang[0], costh, index[0],
706 pr_rvec(debug, 0, "rij", r_ij, DIM, TRUE);
707 pr_rvec(debug, 0, "rkj", r_kj, DIM, TRUE);
711 static real calc_fraction(const real angles[], int nangles)
714 real trans = 0, gauche = 0;
717 for (i = 0; i < nangles; i++)
719 angle = angles[i] * RAD2DEG;
721 if (angle > 135 && angle < 225)
725 else if (angle > 270 && angle < 330)
729 else if (angle < 90 && angle > 30)
734 if (trans + gauche > 0)
736 return trans / (trans + gauche);
744 static void calc_dihs(struct t_pbc* pbc, int n4, const int index[], real ang[], rvec x_s[])
746 int i, ix, t1, t2, t3;
747 rvec r_ij, r_kj, r_kl, m, n;
750 for (i = ix = 0; (ix < n4); i++, ix += 4)
752 aaa = dih_angle(x_s[index[ix]], x_s[index[ix + 1]], x_s[index[ix + 2]], x_s[index[ix + 3]],
753 pbc, r_ij, r_kj, r_kl, m, n, &t1, &t2, &t3);
755 ang[i] = aaa; /* not taking into account ryckaert bellemans yet */
759 void make_histo(FILE* log, int ndata, real data[], int npoints, int histo[], real minx, real maxx)
766 minx = maxx = data[0];
767 for (i = 1; (i < ndata); i++)
769 minx = std::min(minx, data[i]);
770 maxx = std::max(maxx, data[i]);
772 fprintf(log, "Min data: %10g Max data: %10g\n", minx, maxx);
774 dx = npoints / (maxx - minx);
777 fprintf(debug, "Histogramming: ndata=%d, nhisto=%d, minx=%g,maxx=%g,dx=%g\n", ndata,
778 npoints, minx, maxx, dx);
780 for (i = 0; (i < ndata); i++)
782 ind = static_cast<int>((data[i] - minx) * dx);
783 if ((ind >= 0) && (ind < npoints))
789 fprintf(log, "index = %d, data[%d] = %g\n", ind, i, data[i]);
794 void normalize_histo(int npoints, const int histo[], real dx, real normhisto[])
800 for (i = 0; (i < npoints); i++)
806 fprintf(stderr, "Empty histogram!\n");
810 for (i = 0; (i < npoints); i++)
812 normhisto[i] = fac * histo[i];
816 void read_ang_dih(const char* trj_fn,
830 const gmx_output_env_t* oenv)
834 int i, angind, total, teller;
835 int nangles, n_alloc;
836 real t, fraction, pifac, aa, angle;
841 #define prev (1 - cur)
844 read_first_x(oenv, &status, trj_fn, &t, &x, box);
856 snew(angles[cur], nangles);
857 snew(angles[prev], nangles);
859 /* Start the loop over frames */
864 *trans_frac = nullptr;
865 *aver_angle = nullptr;
869 if (teller >= n_alloc)
874 for (i = 0; (i < nangles); i++)
876 srenew(dih[i], n_alloc);
879 srenew(*time, n_alloc);
880 srenew(*trans_frac, n_alloc);
881 srenew(*aver_angle, n_alloc);
888 set_pbc(pbc, -1, box);
893 calc_angles(pbc, isize, index, angles[cur], x);
897 calc_dihs(pbc, isize, index, angles[cur], x);
900 fraction = calc_fraction(angles[cur], nangles);
901 (*trans_frac)[teller] = fraction;
903 /* Change Ryckaert-Bellemans dihedrals to polymer convention
904 * Modified 990913 by Erik:
905 * We actually shouldn't change the convention, since it's
906 * calculated from polymer above, but we change the intervall
907 * from [-180,180] to [0,360].
911 for (i = 0; (i < nangles); i++)
913 if (angles[cur][i] <= 0.0)
915 angles[cur][i] += 2 * M_PI;
920 /* Periodicity in dihedral space... */
923 for (i = 0; (i < nangles); i++)
925 real dd = angles[cur][i];
926 angles[cur][i] = std::atan2(std::sin(dd), std::cos(dd));
933 for (i = 0; (i < nangles); i++)
935 while (angles[cur][i] <= angles[prev][i] - M_PI)
937 angles[cur][i] += 2 * M_PI;
939 while (angles[cur][i] > angles[prev][i] + M_PI)
941 angles[cur][i] -= 2 * M_PI;
950 for (i = 0; (i < nangles); i++)
952 aa = aa + angles[cur][i];
954 /* angle in rad / 2Pi * max determines bin. bins go from 0 to maxangstat,
955 even though scale goes from -pi to pi (dihedral) or -pi/2 to pi/2
956 (angle) Basically: translate the x-axis by Pi. Translate it back by
960 angle = angles[cur][i];
963 while (angle < -M_PI)
967 while (angle >= M_PI)
975 /* Update the distribution histogram */
976 angind = gmx::roundToInt((angle * maxangstat) / pifac);
977 if (angind == maxangstat)
981 if ((angind < 0) || (angind >= maxangstat))
983 /* this will never happen */
984 gmx_fatal(FARGS, "angle (%f) index out of range (0..%d) : %d\n", angle, maxangstat, angind);
988 if (angind == maxangstat)
990 fprintf(stderr, "angle %d fr %d = %g\n", i, cur, angle);
996 /* average over all angles */
997 (*aver_angle)[teller] = (aa / nangles);
999 /* this copies all current dih. angles to dih[i], teller is frame */
1002 for (i = 0; i < nangles; i++)
1004 dih[i][teller] = angles[cur][i];
1011 /* Increment loop counter */
1013 } while (read_next_x(oenv, status, &t, x, box));
1018 sfree(angles[prev]);