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 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, 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.
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.
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.
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.
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.
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.
46 #include "gromacs/fileio/confio.h"
47 #include "gromacs/fileio/trxio.h"
48 #include "gromacs/fileio/xvgr.h"
49 #include "gromacs/gmxana/angle_correction.h"
50 #include "gromacs/gmxana/gstat.h"
51 #include "gromacs/listed_forces/bonded.h"
52 #include "gromacs/math/functions.h"
53 #include "gromacs/math/units.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/math/vecdump.h"
56 #include "gromacs/pbcutil/pbc.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "gromacs/utility/smalloc.h"
60 void print_one(const gmx_output_env_t* oenv,
70 char buf[256], t2[256];
73 sprintf(buf, "%s%s.xvg", base, name);
74 fprintf(stderr, "\rPrinting %s ", buf);
76 sprintf(t2, "%s %s", title, name);
77 fp = xvgropen(buf, t2, "Time (ps)", ylabel, oenv);
78 for (k = 0; (k < nf); k++)
80 fprintf(fp, "%10g %10g\n", time[k], data[k]);
85 static int calc_RBbin(real phi, int gmx_unused multiplicity, real gmx_unused core_frac)
87 /* multiplicity and core_frac NOT used,
88 * just given to enable use of pt-to-fn in caller low_ana_dih_trans*/
89 static const real r30 = M_PI / 6.0;
90 static const real r90 = M_PI / 2.0;
91 static const real r150 = M_PI * 5.0 / 6.0;
93 if ((phi < r30) && (phi > -r30))
97 else if ((phi > -r150) && (phi < -r90))
101 else if ((phi < r150) && (phi > r90))
108 static int calc_Nbin(real phi, int multiplicity, real core_frac)
110 static const real r360 = 360 * DEG2RAD;
111 real rot_width, core_width, core_offset, low, hi;
113 /* with multiplicity 3 and core_frac 0.5
114 * 0<g(-)<120, 120<t<240, 240<g(+)<360
115 * 0< bin0 < 30, 30<bin1<90, 90<bin0<150, 150<bin2<210, 210<bin0<270, 270<bin3<330, 330<bin0<360
116 * so with multiplicity 3, bin1 is core g(-), bin2 is core t, bin3 is
117 core g(+), bin0 is between rotamers */
123 rot_width = 360. / multiplicity;
124 core_width = core_frac * rot_width;
125 core_offset = (rot_width - core_width) / 2.0;
126 for (bin = 1; bin <= multiplicity; bin++)
128 low = ((bin - 1) * rot_width) + core_offset;
129 hi = ((bin - 1) * rot_width) + core_offset + core_width;
132 if ((phi > low) && (phi < hi))
140 void ana_dih_trans(const char* fn_trans,
141 const char* fn_histo,
148 const gmx_output_env_t* oenv)
150 /* just a wrapper; declare extra args, then chuck away at end. */
158 snew(multiplicity, nangles);
159 for (k = 0; (k < nangles); k++)
164 low_ana_dih_trans(TRUE, fn_trans, TRUE, fn_histo, maxchi, dih, nlist, dlist, nframes, nangles,
165 grpname, multiplicity, time, bRb, 0.5, oenv);
170 void low_ana_dih_trans(gmx_bool bTrans,
171 const char* fn_trans,
173 const char* fn_histo,
185 const gmx_output_env_t* oenv)
190 int i, j, k, Dih, ntrans;
191 int cur_bin, new_bin;
194 int (*calc_bin)(real, int, real);
201 /* Assumes the frames are equally spaced in time */
202 dt = (time[nframes - 1] - time[0]) / (nframes - 1);
204 /* Analysis of dihedral transitions */
205 fprintf(stderr, "Now calculating transitions...\n");
209 calc_bin = calc_RBbin;
213 calc_bin = calc_Nbin;
216 for (k = 0; k < NROT; k++)
218 snew(rot_occ[k], nangles);
219 for (i = 0; (i < nangles); i++)
227 /* dih[i][j] is the dihedral angle i in frame j */
229 for (i = 0; (i < nangles); i++)
234 mind = maxd = prev = dih[i][0];
236 cur_bin = calc_bin(dih[i][0], multiplicity[i], core_frac);
237 rot_occ[cur_bin][i]++;
239 for (j = 1; (j < nframes); j++)
241 new_bin = calc_bin(dih[i][j], multiplicity[i], core_frac);
242 rot_occ[new_bin][i]++;
248 else if ((new_bin != 0) && (cur_bin != new_bin))
256 /* why is all this md rubbish periodic? Remove 360 degree periodicity */
257 if ((dih[i][j] - prev) > M_PI)
259 dih[i][j] -= 2 * M_PI;
261 else if ((dih[i][j] - prev) < -M_PI)
263 dih[i][j] += 2 * M_PI;
268 mind = std::min(mind, dih[i][j]);
269 maxd = std::max(maxd, dih[i][j]);
270 if ((maxd - mind) > 2 * M_PI / 3) /* or 120 degrees, assuming */
271 { /* multiplicity 3. Not so general.*/
274 maxd = mind = dih[i][j]; /* get ready for next transition */
279 for (k = 0; k < NROT; k++)
281 rot_occ[k][i] /= nframes;
284 fprintf(stderr, "Total number of transitions: %10d\n", ntrans);
287 ttime = (dt * nframes * nangles) / ntrans;
288 fprintf(stderr, "Time between transitions: %10.3f ps\n", ttime);
291 /* new by grs - copy transitions from tr_h[] to dlist->ntr[]
292 * and rotamer populations from rot_occ to dlist->rot_occ[]
293 * based on fn histogramming in g_chi. diff roles for i and j here */
296 for (Dih = 0; (Dih < NONCHI + maxchi); Dih++)
298 for (i = 0; (i < nlist); i++)
300 if (((Dih < edOmega)) || ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i]))))
301 || ((Dih > edOmega) && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1)))
303 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
304 dlist[i].ntr[Dih] = tr_h[j];
305 for (k = 0; k < NROT; k++)
307 dlist[i].rot_occ[Dih][k] = rot_occ[k][j];
314 /* end addition by grs */
318 sprintf(title, "Number of transitions: %s", grpname);
319 fp = xvgropen(fn_trans, title, "Time (ps)", "# transitions/timeframe", oenv);
320 for (j = 0; (j < nframes); j++)
322 fprintf(fp, "%10.3f %10d\n", time[j], tr_f[j]);
327 /* Compute histogram from # transitions per dihedral */
329 for (j = 0; (j < nframes); j++)
333 for (i = 0; (i < nangles); i++)
337 for (j = nframes; ((tr_f[j - 1] == 0) && (j > 0)); j--) {}
339 ttime = dt * nframes;
342 sprintf(title, "Transition time: %s", grpname);
343 fp = xvgropen(fn_histo, title, "Time (ps)", "#", oenv);
344 for (i = j - 1; (i > 0); i--)
348 fprintf(fp, "%10.3f %10d\n", ttime / i, tr_f[i]);
356 for (k = 0; k < NROT; k++)
362 void mk_multiplicity_lookup(int* multiplicity, int maxchi, int nlist, t_dlist dlist[], int nangles)
364 /* new by grs - for dihedral j (as in dih[j]) get multiplicity from dlist
365 * and store in multiplicity[j]
372 for (Dih = 0; (Dih < NONCHI + maxchi); Dih++)
374 for (i = 0; (i < nlist); i++)
376 std::strncpy(name, dlist[i].name, 3);
378 if (((Dih < edOmega)) || ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i]))))
379 || ((Dih > edOmega) && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1)))
381 /* default - we will correct the rest below */
384 /* make omegas 2fold, though doesn't make much more sense than 3 */
385 if (Dih == edOmega && (has_dihedral(edOmega, &(dlist[i]))))
390 /* dihedrals to aromatic rings, COO, CONH2 or guanidinium are 2fold*/
391 if (Dih > edOmega && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1))
393 if (((std::strstr(name, "PHE") != nullptr) && (Dih == edChi2))
394 || ((std::strstr(name, "TYR") != nullptr) && (Dih == edChi2))
395 || ((std::strstr(name, "PTR") != nullptr) && (Dih == edChi2))
396 || ((std::strstr(name, "TRP") != nullptr) && (Dih == edChi2))
397 || ((std::strstr(name, "HIS") != nullptr) && (Dih == edChi2))
398 || ((std::strstr(name, "GLU") != nullptr) && (Dih == edChi3))
399 || ((std::strstr(name, "ASP") != nullptr) && (Dih == edChi2))
400 || ((std::strstr(name, "GLN") != nullptr) && (Dih == edChi3))
401 || ((std::strstr(name, "ASN") != nullptr) && (Dih == edChi2))
402 || ((std::strstr(name, "ARG") != nullptr) && (Dih == edChi4)))
413 fprintf(stderr, "WARNING: not all dihedrals found in topology (only %d out of %d)!\n", j, nangles);
415 /* Check for remaining dihedrals */
416 for (; (j < nangles); j++)
422 void mk_chi_lookup(int** lookup, int maxchi, int nlist, t_dlist dlist[])
425 /* by grs. should rewrite everything to use this. (but haven't,
426 * and at mmt only used in get_chi_product_traj
427 * returns the dihed number given the residue number (from-0)
428 * and chi (from-0) nr. -1 for chi undefined for that res (eg gly, ala..)*/
433 /* NONCHI points to chi1, therefore we have to start counting there. */
434 for (Dih = NONCHI; (Dih < NONCHI + maxchi); Dih++)
436 for (i = 0; (i < nlist); i++)
439 if (((Dih < edOmega)) || ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i]))))
440 || ((Dih > edOmega) && (dlist[i].atm.Cn[Dih - NONCHI + 3] != -1)))
442 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
458 void get_chi_product_traj(real** dih,
471 const gmx_output_env_t* oenv)
474 gmx_bool bRotZero, bHaveChi = FALSE;
475 int accum = 0, index, i, j, k, Xi, n, b;
480 char hisfile[256], histitle[256], *namept;
482 int (*calc_bin)(real, int, real);
484 /* Analysis of dihedral transitions */
485 fprintf(stderr, "Now calculating Chi product trajectories...\n");
489 calc_bin = calc_RBbin;
493 calc_bin = calc_Nbin;
496 snew(chi_prtrj, nframes);
498 /* file for info on all residues */
501 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "Probability", oenv);
505 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "# Counts", oenv);
508 for (i = 0; (i < nlist); i++)
511 /* get nbin, the nr. of cumulative rotamers that need to be considered */
513 for (Xi = 0; Xi < maxchi; Xi++)
515 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
518 n = multiplicity[index];
522 nbin += 1; /* for the "zero rotamer", outside the core region */
524 for (j = 0; (j < nframes); j++)
529 index = lookup[i][0]; /* index into dih of chi1 of res i */
537 b = calc_bin(dih[index][j], multiplicity[index], core_frac);
543 for (Xi = 1; Xi < maxchi; Xi++)
545 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
548 n = multiplicity[index];
549 b = calc_bin(dih[index][j], n, core_frac);
550 accum = n * accum + b - 1;
565 chi_prtrj[j] = accum;
566 if (accum + 1 > nbin)
577 /* print cuml rotamer vs time */
578 print_one(oenv, "chiproduct", dlist[i].name, "chi product for",
579 "cumulative rotamer", nframes, time, chi_prtrj);
582 /* make a histogram pf culm. rotamer occupancy too */
583 snew(chi_prhist, nbin);
584 make_histo(nullptr, nframes, chi_prtrj, nbin, chi_prhist, 0, nbin);
587 sprintf(hisfile, "histo-chiprod%s.xvg", dlist[i].name);
588 sprintf(histitle, "cumulative rotamer distribution for %s", dlist[i].name);
589 fprintf(stderr, " and %s ", hisfile);
590 fp = xvgropen(hisfile, histitle, "number", "", oenv);
591 if (output_env_get_print_xvgr_codes(oenv))
593 fprintf(fp, "@ xaxis tick on\n");
594 fprintf(fp, "@ xaxis tick major 1\n");
595 fprintf(fp, "@ type xy\n");
597 for (k = 0; (k < nbin); k++)
601 fprintf(fp, "%5d %10g\n", k, (1.0 * chi_prhist[k]) / nframes);
605 fprintf(fp, "%5d %10d\n", k, chi_prhist[k]);
608 fprintf(fp, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
612 /* and finally print out occupancies to a single file */
613 /* get the gmx from-1 res nr by setting a ptr to the number part
614 * of dlist[i].name - potential bug for 4-letter res names... */
615 namept = dlist[i].name + 3;
616 fprintf(fpall, "%5s ", namept);
617 for (k = 0; (k < nbin); k++)
621 fprintf(fpall, " %10g", (1.0 * chi_prhist[k]) / nframes);
625 fprintf(fpall, " %10d", chi_prhist[k]);
628 fprintf(fpall, "\n");
637 fprintf(stderr, "\n");
640 void calc_distribution_props(int nh, const int histo[], real start, int nkkk, t_karplus kkk[], real* S2)
642 real d, dc, ds, c1, c2, tdc, tds;
643 real fac, ang, invth, Jc;
648 gmx_fatal(FARGS, "No points in histogram (%s, %d)", __FILE__, __LINE__);
652 /* Compute normalisation factor */
654 for (j = 0; (j < nh); j++)
660 for (i = 0; (i < nkkk); i++)
667 for (j = 0; (j < nh); j++)
669 d = invth * histo[j];
670 ang = j * fac - start;
673 ds = d * std::sin(ang);
676 for (i = 0; (i < nkkk); i++)
678 c1 = std::cos(ang + kkk[i].offset);
680 Jc = (kkk[i].A * c2 + kkk[i].B * c1 + kkk[i].C);
681 kkk[i].Jc += histo[j] * Jc;
682 kkk[i].Jcsig += histo[j] * gmx::square(Jc);
685 for (i = 0; (i < nkkk); i++)
688 kkk[i].Jcsig = std::sqrt(kkk[i].Jcsig / th - gmx::square(kkk[i].Jc));
690 *S2 = tdc * tdc + tds * tds;
693 static void calc_angles(struct t_pbc* pbc, int n3, int index[], real ang[], rvec x_s[])
699 for (i = ix = 0; (ix < n3); i++, ix += 3)
701 ang[i] = bond_angle(x_s[index[ix]], x_s[index[ix + 1]], x_s[index[ix + 2]], pbc, r_ij, r_kj,
706 fprintf(debug, "Angle[0]=%g, costh=%g, index0 = %d, %d, %d\n", ang[0], costh, index[0],
708 pr_rvec(debug, 0, "rij", r_ij, DIM, TRUE);
709 pr_rvec(debug, 0, "rkj", r_kj, DIM, TRUE);
713 static real calc_fraction(const real angles[], int nangles)
716 real trans = 0, gauche = 0;
719 for (i = 0; i < nangles; i++)
721 angle = angles[i] * RAD2DEG;
723 if (angle > 135 && angle < 225)
727 else if ((angle > 270 && angle < 330) || (angle < 90 && angle > 30))
732 if (trans + gauche > 0)
734 return trans / (trans + gauche);
742 static void calc_dihs(struct t_pbc* pbc, int n4, const int index[], real ang[], rvec x_s[])
744 int i, ix, t1, t2, t3;
745 rvec r_ij, r_kj, r_kl, m, n;
748 for (i = ix = 0; (ix < n4); i++, ix += 4)
750 aaa = dih_angle(x_s[index[ix]], x_s[index[ix + 1]], x_s[index[ix + 2]], x_s[index[ix + 3]],
751 pbc, r_ij, r_kj, r_kl, m, n, &t1, &t2, &t3);
753 ang[i] = aaa; /* not taking into account ryckaert bellemans yet */
757 void make_histo(FILE* log, int ndata, real data[], int npoints, int histo[], real minx, real maxx)
764 minx = maxx = data[0];
765 for (i = 1; (i < ndata); i++)
767 minx = std::min(minx, data[i]);
768 maxx = std::max(maxx, data[i]);
770 fprintf(log, "Min data: %10g Max data: %10g\n", minx, maxx);
772 dx = npoints / (maxx - minx);
775 fprintf(debug, "Histogramming: ndata=%d, nhisto=%d, minx=%g,maxx=%g,dx=%g\n", ndata,
776 npoints, minx, maxx, dx);
778 for (i = 0; (i < ndata); i++)
780 ind = static_cast<int>((data[i] - minx) * dx);
781 if ((ind >= 0) && (ind < npoints))
787 fprintf(log, "index = %d, data[%d] = %g\n", ind, i, data[i]);
792 void normalize_histo(int npoints, const int histo[], real dx, real normhisto[])
798 for (i = 0; (i < npoints); i++)
804 fprintf(stderr, "Empty histogram!\n");
808 for (i = 0; (i < npoints); i++)
810 normhisto[i] = fac * histo[i];
814 void read_ang_dih(const char* trj_fn,
828 const gmx_output_env_t* oenv)
832 int i, angind, total, teller;
833 int nangles, n_alloc;
834 real t, fraction, pifac, angle;
839 #define prev (1 - cur)
842 read_first_x(oenv, &status, trj_fn, &t, &x, box);
854 snew(angles[cur], nangles);
855 snew(angles[prev], nangles);
857 /* Start the loop over frames */
862 *trans_frac = nullptr;
863 *aver_angle = nullptr;
867 if (teller >= n_alloc)
872 for (i = 0; (i < nangles); i++)
874 srenew(dih[i], n_alloc);
877 srenew(*time, n_alloc);
878 srenew(*trans_frac, n_alloc);
879 srenew(*aver_angle, n_alloc);
886 set_pbc(pbc, PbcType::Unset, box);
891 calc_angles(pbc, isize, index, angles[cur], x);
895 calc_dihs(pbc, isize, index, angles[cur], x);
898 fraction = calc_fraction(angles[cur], nangles);
899 (*trans_frac)[teller] = fraction;
901 /* Change Ryckaert-Bellemans dihedrals to polymer convention
902 * Modified 990913 by Erik:
903 * We actually shouldn't change the convention, since it's
904 * calculated from polymer above, but we change the intervall
905 * from [-180,180] to [0,360].
909 for (i = 0; (i < nangles); i++)
911 if (angles[cur][i] <= 0.0)
913 angles[cur][i] += 2 * M_PI;
918 /* Periodicity in dihedral space... */
921 for (i = 0; (i < nangles); i++)
923 real dd = angles[cur][i];
924 angles[cur][i] = std::atan2(std::sin(dd), std::cos(dd));
931 for (i = 0; (i < nangles); i++)
933 while (angles[cur][i] <= angles[prev][i] - M_PI)
935 angles[cur][i] += 2 * M_PI;
937 while (angles[cur][i] > angles[prev][i] + M_PI)
939 angles[cur][i] -= 2 * M_PI;
948 for (i = 0; (i < nangles); i++)
950 if (!bAngles && i > 0)
952 real diffa = angles[cur][i] - angles[cur][i - 1];
953 diffa = correctRadianAngleRange(diffa);
954 angles[cur][i] = angles[cur][i - 1] + diffa;
957 aa = aa + angles[cur][i];
959 /* angle in rad / 2Pi * max determines bin. bins go from 0 to maxangstat,
960 even though scale goes from -pi to pi (dihedral) or -pi/2 to pi/2
961 (angle) Basically: translate the x-axis by Pi. Translate it back by
965 angle = angles[cur][i];
968 angle = correctRadianAngleRange(angle);
972 /* Update the distribution histogram */
973 angind = gmx::roundToInt((angle * maxangstat) / pifac);
974 if (angind == maxangstat)
978 if ((angind < 0) || (angind >= maxangstat))
980 /* this will never happen */
981 gmx_fatal(FARGS, "angle (%f) index out of range (0..%d) : %d\n", angle, maxangstat, angind);
985 if (angind == maxangstat)
987 fprintf(stderr, "angle %d fr %d = %g\n", i, cur, angle);
993 /* average over all angles */
994 aa = correctRadianAngleRange(aa / nangles);
995 (*aver_angle)[teller] = (aa);
997 /* this copies all current dih. angles to dih[i], teller is frame */
1000 for (i = 0; i < nangles; i++)
1004 dih[i][teller] = correctRadianAngleRange(angles[cur][i]);
1008 dih[i][teller] = angles[cur][i];
1016 /* Increment loop counter */
1018 } while (read_next_x(oenv, status, &t, x, box));
1023 sfree(angles[prev]);