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, 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.
44 #include "gromacs/math/units.h"
45 #include "gromacs/utility/smalloc.h"
46 #include "gromacs/legacyheaders/macros.h"
47 #include "gromacs/legacyheaders/txtdump.h"
48 #include "gromacs/fileio/xvgr.h"
49 #include "gromacs/legacyheaders/typedefs.h"
50 #include "gromacs/math/vec.h"
52 #include "gromacs/fileio/confio.h"
53 #include "gromacs/fileio/trxio.h"
55 #include "gromacs/bonded/bonded.h"
56 #include "gromacs/pbcutil/pbc.h"
57 #include "gromacs/utility/fatalerror.h"
59 void print_one(const output_env_t oenv, const char *base, const char *name,
60 const char *title, const char *ylabel, int nf, real time[],
64 char buf[256], t2[256];
67 sprintf(buf, "%s%s.xvg", base, name);
68 fprintf(stderr, "\rPrinting %s ", buf);
69 sprintf(t2, "%s %s", title, name);
70 fp = xvgropen(buf, t2, "Time (ps)", ylabel, oenv);
71 for (k = 0; (k < nf); k++)
73 fprintf(fp, "%10g %10g\n", time[k], data[k]);
78 static int calc_RBbin(real phi, int gmx_unused multiplicity, real gmx_unused core_frac)
80 /* multiplicity and core_frac NOT used,
81 * just given to enable use of pt-to-fn in caller low_ana_dih_trans*/
82 static const real r30 = M_PI/6.0;
83 static const real r90 = M_PI/2.0;
84 static const real r150 = M_PI*5.0/6.0;
86 if ((phi < r30) && (phi > -r30))
90 else if ((phi > -r150) && (phi < -r90))
94 else if ((phi < r150) && (phi > r90))
101 static int calc_Nbin(real phi, int multiplicity, real core_frac)
103 static const real r360 = 360*DEG2RAD;
104 real rot_width, core_width, core_offset, low, hi;
106 /* with multiplicity 3 and core_frac 0.5
107 * 0<g(-)<120, 120<t<240, 240<g(+)<360
108 * 0< bin0 < 30, 30<bin1<90, 90<bin0<150, 150<bin2<210, 210<bin0<270, 270<bin3<330, 330<bin0<360
109 * so with multiplicity 3, bin1 is core g(-), bin2 is core t, bin3 is
110 core g(+), bin0 is between rotamers */
116 rot_width = 360/multiplicity;
117 core_width = core_frac * rot_width;
118 core_offset = (rot_width - core_width)/2.0;
119 for (bin = 1; bin <= multiplicity; bin++)
121 low = ((bin - 1) * rot_width ) + core_offset;
122 hi = ((bin - 1) * rot_width ) + core_offset + core_width;
125 if ((phi > low) && (phi < hi))
133 void ana_dih_trans(const char *fn_trans, const char *fn_histo,
134 real **dih, int nframes, int nangles,
135 const char *grpname, real *time, gmx_bool bRb,
136 const output_env_t oenv)
138 /* just a wrapper; declare extra args, then chuck away at end. */
146 snew(multiplicity, nangles);
147 for (k = 0; (k < nangles); k++)
152 low_ana_dih_trans(TRUE, fn_trans, TRUE, fn_histo, maxchi,
153 dih, nlist, dlist, nframes,
154 nangles, grpname, multiplicity, time, bRb, 0.5, oenv);
160 void low_ana_dih_trans(gmx_bool bTrans, const char *fn_trans,
161 gmx_bool bHisto, const char *fn_histo, int maxchi,
162 real **dih, int nlist, t_dlist dlist[], int nframes,
163 int nangles, const char *grpname, int multiplicity[],
164 real *time, gmx_bool bRb, real core_frac,
165 const output_env_t oenv)
170 int i, j, k, Dih, ntrans;
171 int cur_bin, new_bin;
174 int (*calc_bin)(real, int, real);
181 /* Assumes the frames are equally spaced in time */
182 dt = (time[nframes-1]-time[0])/(nframes-1);
184 /* Analysis of dihedral transitions */
185 fprintf(stderr, "Now calculating transitions...\n");
189 calc_bin = calc_RBbin;
193 calc_bin = calc_Nbin;
196 for (k = 0; k < NROT; k++)
198 snew(rot_occ[k], nangles);
199 for (i = 0; (i < nangles); i++)
207 /* dih[i][j] is the dihedral angle i in frame j */
209 for (i = 0; (i < nangles); i++)
214 mind = maxd = prev = dih[i][0];
216 cur_bin = calc_bin(dih[i][0], multiplicity[i], core_frac);
217 rot_occ[cur_bin][i]++;
219 for (j = 1; (j < nframes); j++)
221 new_bin = calc_bin(dih[i][j], multiplicity[i], core_frac);
222 rot_occ[new_bin][i]++;
228 else if ((new_bin != 0) && (cur_bin != new_bin))
236 /* why is all this md rubbish periodic? Remove 360 degree periodicity */
237 if ( (dih[i][j] - prev) > M_PI)
241 else if ( (dih[i][j] - prev) < -M_PI)
248 mind = min(mind, dih[i][j]);
249 maxd = max(maxd, dih[i][j]);
250 if ( (maxd - mind) > 2*M_PI/3) /* or 120 degrees, assuming */
251 { /* multiplicity 3. Not so general.*/
254 maxd = mind = dih[i][j]; /* get ready for next transition */
259 for (k = 0; k < NROT; k++)
261 rot_occ[k][i] /= nframes;
264 fprintf(stderr, "Total number of transitions: %10d\n", ntrans);
267 ttime = (dt*nframes*nangles)/ntrans;
268 fprintf(stderr, "Time between transitions: %10.3f ps\n", ttime);
271 /* new by grs - copy transitions from tr_h[] to dlist->ntr[]
272 * and rotamer populations from rot_occ to dlist->rot_occ[]
273 * based on fn histogramming in g_chi. diff roles for i and j here */
276 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
278 for (i = 0; (i < nlist); i++)
280 if (((Dih < edOmega) ) ||
281 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
282 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
284 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
285 dlist[i].ntr[Dih] = tr_h[j];
286 for (k = 0; k < NROT; k++)
288 dlist[i].rot_occ[Dih][k] = rot_occ[k][j];
295 /* end addition by grs */
299 sprintf(title, "Number of transitions: %s", grpname);
300 fp = xvgropen(fn_trans, title, "Time (ps)", "# transitions/timeframe", oenv);
301 for (j = 0; (j < nframes); j++)
303 fprintf(fp, "%10.3f %10d\n", time[j], tr_f[j]);
308 /* Compute histogram from # transitions per dihedral */
310 for (j = 0; (j < nframes); j++)
314 for (i = 0; (i < nangles); i++)
318 for (j = nframes; ((tr_f[j-1] == 0) && (j > 0)); j--)
326 sprintf(title, "Transition time: %s", grpname);
327 fp = xvgropen(fn_histo, title, "Time (ps)", "#", oenv);
328 for (i = j-1; (i > 0); i--)
332 fprintf(fp, "%10.3f %10d\n", ttime/i, tr_f[i]);
340 for (k = 0; k < NROT; k++)
347 void mk_multiplicity_lookup (int *multiplicity, int maxchi,
348 int nlist, t_dlist dlist[], int nangles)
350 /* new by grs - for dihedral j (as in dih[j]) get multiplicity from dlist
351 * and store in multiplicity[j]
358 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
360 for (i = 0; (i < nlist); i++)
362 strncpy(name, dlist[i].name, 3);
364 if (((Dih < edOmega) ) ||
365 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
366 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
368 /* default - we will correct the rest below */
371 /* make omegas 2fold, though doesn't make much more sense than 3 */
372 if (Dih == edOmega && (has_dihedral(edOmega, &(dlist[i]))))
377 /* dihedrals to aromatic rings, COO, CONH2 or guanidinium are 2fold*/
378 if (Dih > edOmega && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1))
380 if ( ((strstr(name, "PHE") != NULL) && (Dih == edChi2)) ||
381 ((strstr(name, "TYR") != NULL) && (Dih == edChi2)) ||
382 ((strstr(name, "PTR") != NULL) && (Dih == edChi2)) ||
383 ((strstr(name, "TRP") != NULL) && (Dih == edChi2)) ||
384 ((strstr(name, "HIS") != NULL) && (Dih == edChi2)) ||
385 ((strstr(name, "GLU") != NULL) && (Dih == edChi3)) ||
386 ((strstr(name, "ASP") != NULL) && (Dih == edChi2)) ||
387 ((strstr(name, "GLN") != NULL) && (Dih == edChi3)) ||
388 ((strstr(name, "ASN") != NULL) && (Dih == edChi2)) ||
389 ((strstr(name, "ARG") != NULL) && (Dih == edChi4)) )
400 fprintf(stderr, "WARNING: not all dihedrals found in topology (only %d out of %d)!\n",
403 /* Check for remaining dihedrals */
404 for (; (j < nangles); j++)
411 void mk_chi_lookup (int **lookup, int maxchi,
412 int nlist, t_dlist dlist[])
415 /* by grs. should rewrite everything to use this. (but haven't,
416 * and at mmt only used in get_chi_product_traj
417 * returns the dihed number given the residue number (from-0)
418 * and chi (from-0) nr. -1 for chi undefined for that res (eg gly, ala..)*/
423 /* NONCHI points to chi1, therefore we have to start counting there. */
424 for (Dih = NONCHI; (Dih < NONCHI+maxchi); Dih++)
426 for (i = 0; (i < nlist); i++)
429 if (((Dih < edOmega) ) ||
430 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
431 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
433 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
450 void get_chi_product_traj (real **dih, int nframes, int nlist,
451 int maxchi, t_dlist dlist[], real time[],
452 int **lookup, int *multiplicity, gmx_bool bRb, gmx_bool bNormalize,
453 real core_frac, gmx_bool bAll, const char *fnall,
454 const output_env_t oenv)
457 gmx_bool bRotZero, bHaveChi = FALSE;
458 int accum = 0, index, i, j, k, Xi, n, b;
463 char hisfile[256], histitle[256], *namept;
465 int (*calc_bin)(real, int, real);
467 /* Analysis of dihedral transitions */
468 fprintf(stderr, "Now calculating Chi product trajectories...\n");
472 calc_bin = calc_RBbin;
476 calc_bin = calc_Nbin;
479 snew(chi_prtrj, nframes);
481 /* file for info on all residues */
484 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "Probability", oenv);
488 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "# Counts", oenv);
491 for (i = 0; (i < nlist); i++)
494 /* get nbin, the nr. of cumulative rotamers that need to be considered */
496 for (Xi = 0; Xi < maxchi; Xi++)
498 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
501 n = multiplicity[index];
505 nbin += 1; /* for the "zero rotamer", outside the core region */
507 for (j = 0; (j < nframes); j++)
512 index = lookup[i][0]; /* index into dih of chi1 of res i */
521 b = calc_bin(dih[index][j], multiplicity[index], core_frac);
527 for (Xi = 1; Xi < maxchi; Xi++)
529 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
532 n = multiplicity[index];
533 b = calc_bin(dih[index][j], n, core_frac);
534 accum = n * accum + b - 1;
549 chi_prtrj[j] = accum;
561 /* print cuml rotamer vs time */
562 print_one(oenv, "chiproduct", dlist[i].name, "chi product for",
563 "cumulative rotamer", nframes, time, chi_prtrj);
566 /* make a histogram pf culm. rotamer occupancy too */
567 snew(chi_prhist, nbin);
568 make_histo(NULL, nframes, chi_prtrj, nbin, chi_prhist, 0, nbin);
571 sprintf(hisfile, "histo-chiprod%s.xvg", dlist[i].name);
572 sprintf(histitle, "cumulative rotamer distribution for %s", dlist[i].name);
573 fprintf(stderr, " and %s ", hisfile);
574 fp = xvgropen(hisfile, histitle, "number", "", oenv);
575 if (output_env_get_print_xvgr_codes(oenv))
577 fprintf(fp, "@ xaxis tick on\n");
578 fprintf(fp, "@ xaxis tick major 1\n");
579 fprintf(fp, "@ type xy\n");
581 for (k = 0; (k < nbin); k++)
585 fprintf(fp, "%5d %10g\n", k, (1.0*chi_prhist[k])/nframes);
589 fprintf(fp, "%5d %10d\n", k, chi_prhist[k]);
592 fprintf(fp, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
596 /* and finally print out occupancies to a single file */
597 /* get the gmx from-1 res nr by setting a ptr to the number part
598 * of dlist[i].name - potential bug for 4-letter res names... */
599 namept = dlist[i].name + 3;
600 fprintf(fpall, "%5s ", namept);
601 for (k = 0; (k < nbin); k++)
605 fprintf(fpall, " %10g", (1.0*chi_prhist[k])/nframes);
609 fprintf(fpall, " %10d", chi_prhist[k]);
612 fprintf(fpall, "\n");
621 fprintf(stderr, "\n");
625 void calc_distribution_props(int nh, int histo[], real start,
626 int nkkk, t_karplus kkk[],
629 real d, dc, ds, c1, c2, tdc, tds;
630 real fac, ang, invth, Jc;
635 gmx_fatal(FARGS, "No points in histogram (%s, %d)", __FILE__, __LINE__);
639 /* Compute normalisation factor */
641 for (j = 0; (j < nh); j++)
647 for (i = 0; (i < nkkk); i++)
653 for (j = 0; (j < nh); j++)
663 for (i = 0; (i < nkkk); i++)
665 c1 = cos(ang+kkk[i].offset);
667 Jc = (kkk[i].A*c2 + kkk[i].B*c1 + kkk[i].C);
668 kkk[i].Jc += histo[j]*Jc;
669 kkk[i].Jcsig += histo[j]*sqr(Jc);
672 for (i = 0; (i < nkkk); i++)
675 kkk[i].Jcsig = sqrt(kkk[i].Jcsig/th-sqr(kkk[i].Jc));
677 *S2 = tdc*tdc+tds*tds;
680 static void calc_angles(struct t_pbc *pbc,
681 int n3, atom_id index[], real ang[], rvec x_s[])
687 for (i = ix = 0; (ix < n3); i++, ix += 3)
689 ang[i] = bond_angle(x_s[index[ix]], x_s[index[ix+1]], x_s[index[ix+2]],
690 pbc, r_ij, r_kj, &costh, &t1, &t2);
694 fprintf(debug, "Angle[0]=%g, costh=%g, index0 = %d, %d, %d\n",
695 ang[0], costh, index[0], index[1], index[2]);
696 pr_rvec(debug, 0, "rij", r_ij, DIM, TRUE);
697 pr_rvec(debug, 0, "rkj", r_kj, DIM, TRUE);
701 static real calc_fraction(real angles[], int nangles)
704 real trans = 0, gauche = 0;
707 for (i = 0; i < nangles; i++)
709 angle = angles[i] * RAD2DEG;
711 if (angle > 135 && angle < 225)
715 else if (angle > 270 && angle < 330)
719 else if (angle < 90 && angle > 30)
724 if (trans+gauche > 0)
726 return trans/(trans+gauche);
734 static void calc_dihs(struct t_pbc *pbc,
735 int n4, atom_id index[], real ang[], rvec x_s[])
737 int i, ix, t1, t2, t3;
738 rvec r_ij, r_kj, r_kl, m, n;
741 for (i = ix = 0; (ix < n4); i++, ix += 4)
743 aaa = dih_angle(x_s[index[ix]], x_s[index[ix+1]], x_s[index[ix+2]],
744 x_s[index[ix+3]], pbc,
745 r_ij, r_kj, r_kl, m, n,
746 &sign, &t1, &t2, &t3);
748 ang[i] = aaa; /* not taking into account ryckaert bellemans yet */
752 void make_histo(FILE *log,
753 int ndata, real data[], int npoints, int histo[],
754 real minx, real maxx)
761 minx = maxx = data[0];
762 for (i = 1; (i < ndata); i++)
764 minx = min(minx, data[i]);
765 maxx = max(maxx, data[i]);
767 fprintf(log, "Min data: %10g Max data: %10g\n", minx, maxx);
769 dx = (double)npoints/(maxx-minx);
773 "Histogramming: ndata=%d, nhisto=%d, minx=%g,maxx=%g,dx=%g\n",
774 ndata, npoints, minx, maxx, dx);
776 for (i = 0; (i < ndata); i++)
778 ind = (data[i]-minx)*dx;
779 if ((ind >= 0) && (ind < npoints))
785 fprintf(log, "index = %d, data[%d] = %g\n", ind, i, data[i]);
790 void normalize_histo(int npoints, int histo[], real dx, real normhisto[])
796 for (i = 0; (i < npoints); i++)
802 fprintf(stderr, "Empty histogram!\n");
806 for (i = 0; (i < npoints); i++)
808 normhisto[i] = fac*histo[i];
812 void read_ang_dih(const char *trj_fn,
813 gmx_bool bAngles, gmx_bool bSaveAll, gmx_bool bRb, gmx_bool bPBC,
814 int maxangstat, int angstat[],
815 int *nframes, real **time,
816 int isize, atom_id index[],
820 const output_env_t oenv)
824 int i, angind, natoms, total, teller;
825 int nangles, n_alloc;
826 real t, fraction, pifac, aa, angle;
834 natoms = read_first_x(oenv, &status, trj_fn, &t, &x, box);
846 snew(angles[cur], nangles);
847 snew(angles[prev], nangles);
849 /* Start the loop over frames */
859 if (teller >= n_alloc)
864 for (i = 0; (i < nangles); i++)
866 srenew(dih[i], n_alloc);
869 srenew(*time, n_alloc);
870 srenew(*trans_frac, n_alloc);
871 srenew(*aver_angle, n_alloc);
878 set_pbc(pbc, -1, box);
883 calc_angles(pbc, isize, index, angles[cur], x);
887 calc_dihs(pbc, isize, index, angles[cur], x);
890 fraction = calc_fraction(angles[cur], nangles);
891 (*trans_frac)[teller] = fraction;
893 /* Change Ryckaert-Bellemans dihedrals to polymer convention
894 * Modified 990913 by Erik:
895 * We actually shouldn't change the convention, since it's
896 * calculated from polymer above, but we change the intervall
897 * from [-180,180] to [0,360].
901 for (i = 0; (i < nangles); i++)
903 if (angles[cur][i] <= 0.0)
905 angles[cur][i] += 2*M_PI;
910 /* Periodicity in dihedral space... */
913 for (i = 0; (i < nangles); i++)
915 real dd = angles[cur][i];
916 angles[cur][i] = atan2(sin(dd), cos(dd));
923 for (i = 0; (i < nangles); i++)
925 while (angles[cur][i] <= angles[prev][i] - M_PI)
927 angles[cur][i] += 2*M_PI;
929 while (angles[cur][i] > angles[prev][i] + M_PI)
931 angles[cur][i] -= 2*M_PI;
940 for (i = 0; (i < nangles); i++)
942 aa = aa+angles[cur][i];
944 /* angle in rad / 2Pi * max determines bin. bins go from 0 to maxangstat,
945 even though scale goes from -pi to pi (dihedral) or -pi/2 to pi/2
946 (angle) Basically: translate the x-axis by Pi. Translate it back by
950 angle = angles[cur][i];
953 while (angle < -M_PI)
957 while (angle >= M_PI)
965 /* Update the distribution histogram */
966 angind = (int) ((angle*maxangstat)/pifac + 0.5);
967 if (angind == maxangstat)
971 if ( (angind < 0) || (angind >= maxangstat) )
973 /* this will never happen */
974 gmx_fatal(FARGS, "angle (%f) index out of range (0..%d) : %d\n",
975 angle, maxangstat, angind);
979 if (angind == maxangstat)
981 fprintf(stderr, "angle %d fr %d = %g\n", i, cur, angle);
987 /* average over all angles */
988 (*aver_angle)[teller] = (aa/nangles);
990 /* this copies all current dih. angles to dih[i], teller is frame */
993 for (i = 0; i < nangles; i++)
995 dih[i][teller] = angles[cur][i];
1002 /* Increment loop counter */
1005 while (read_next_x(oenv, status, &t, x, box));
1010 sfree(angles[prev]);