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.
45 #include "gromacs/utility/smalloc.h"
49 #include "gromacs/fileio/xvgr.h"
53 #include "gromacs/fileio/confio.h"
54 #include "gromacs/fileio/trxio.h"
56 #include "gromacs/utility/fatalerror.h"
58 void print_one(const output_env_t oenv, const char *base, const char *name,
59 const char *title, const char *ylabel, int nf, real time[],
63 char buf[256], t2[256];
66 sprintf(buf, "%s%s.xvg", base, name);
67 fprintf(stderr, "\rPrinting %s ", buf);
68 sprintf(t2, "%s %s", title, name);
69 fp = xvgropen(buf, t2, "Time (ps)", ylabel, oenv);
70 for (k = 0; (k < nf); k++)
72 fprintf(fp, "%10g %10g\n", time[k], data[k]);
77 static int calc_RBbin(real phi, int gmx_unused multiplicity, real gmx_unused core_frac)
79 /* multiplicity and core_frac NOT used,
80 * just given to enable use of pt-to-fn in caller low_ana_dih_trans*/
81 static const real r30 = M_PI/6.0;
82 static const real r90 = M_PI/2.0;
83 static const real r150 = M_PI*5.0/6.0;
85 if ((phi < r30) && (phi > -r30))
89 else if ((phi > -r150) && (phi < -r90))
93 else if ((phi < r150) && (phi > r90))
100 static int calc_Nbin(real phi, int multiplicity, real core_frac)
102 static const real r360 = 360*DEG2RAD;
103 real rot_width, core_width, core_offset, low, hi;
105 /* with multiplicity 3 and core_frac 0.5
106 * 0<g(-)<120, 120<t<240, 240<g(+)<360
107 * 0< bin0 < 30, 30<bin1<90, 90<bin0<150, 150<bin2<210, 210<bin0<270, 270<bin3<330, 330<bin0<360
108 * so with multiplicity 3, bin1 is core g(-), bin2 is core t, bin3 is
109 core g(+), bin0 is between rotamers */
115 rot_width = 360/multiplicity;
116 core_width = core_frac * rot_width;
117 core_offset = (rot_width - core_width)/2.0;
118 for (bin = 1; bin <= multiplicity; bin++)
120 low = ((bin - 1) * rot_width ) + core_offset;
121 hi = ((bin - 1) * rot_width ) + core_offset + core_width;
124 if ((phi > low) && (phi < hi))
132 void ana_dih_trans(const char *fn_trans, const char *fn_histo,
133 real **dih, int nframes, int nangles,
134 const char *grpname, real *time, gmx_bool bRb,
135 const output_env_t oenv)
137 /* just a wrapper; declare extra args, then chuck away at end. */
145 snew(multiplicity, nangles);
146 for (k = 0; (k < nangles); k++)
151 low_ana_dih_trans(TRUE, fn_trans, TRUE, fn_histo, maxchi,
152 dih, nlist, dlist, nframes,
153 nangles, grpname, multiplicity, time, bRb, 0.5, oenv);
159 void low_ana_dih_trans(gmx_bool bTrans, const char *fn_trans,
160 gmx_bool bHisto, const char *fn_histo, int maxchi,
161 real **dih, int nlist, t_dlist dlist[], int nframes,
162 int nangles, const char *grpname, int multiplicity[],
163 real *time, gmx_bool bRb, real core_frac,
164 const output_env_t oenv)
169 int i, j, k, Dih, ntrans;
170 int cur_bin, new_bin;
173 int (*calc_bin)(real, int, real);
180 /* Assumes the frames are equally spaced in time */
181 dt = (time[nframes-1]-time[0])/(nframes-1);
183 /* Analysis of dihedral transitions */
184 fprintf(stderr, "Now calculating transitions...\n");
188 calc_bin = calc_RBbin;
192 calc_bin = calc_Nbin;
195 for (k = 0; k < NROT; k++)
197 snew(rot_occ[k], nangles);
198 for (i = 0; (i < nangles); i++)
206 /* dih[i][j] is the dihedral angle i in frame j */
208 for (i = 0; (i < nangles); i++)
213 mind = maxd = prev = dih[i][0];
215 cur_bin = calc_bin(dih[i][0], multiplicity[i], core_frac);
216 rot_occ[cur_bin][i]++;
218 for (j = 1; (j < nframes); j++)
220 new_bin = calc_bin(dih[i][j], multiplicity[i], core_frac);
221 rot_occ[new_bin][i]++;
227 else if ((new_bin != 0) && (cur_bin != new_bin))
235 /* why is all this md rubbish periodic? Remove 360 degree periodicity */
236 if ( (dih[i][j] - prev) > M_PI)
240 else if ( (dih[i][j] - prev) < -M_PI)
247 mind = min(mind, dih[i][j]);
248 maxd = max(maxd, dih[i][j]);
249 if ( (maxd - mind) > 2*M_PI/3) /* or 120 degrees, assuming */
250 { /* multiplicity 3. Not so general.*/
253 maxd = mind = dih[i][j]; /* get ready for next transition */
258 for (k = 0; k < NROT; k++)
260 rot_occ[k][i] /= nframes;
263 fprintf(stderr, "Total number of transitions: %10d\n", ntrans);
266 ttime = (dt*nframes*nangles)/ntrans;
267 fprintf(stderr, "Time between transitions: %10.3f ps\n", ttime);
270 /* new by grs - copy transitions from tr_h[] to dlist->ntr[]
271 * and rotamer populations from rot_occ to dlist->rot_occ[]
272 * based on fn histogramming in g_chi. diff roles for i and j here */
275 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
277 for (i = 0; (i < nlist); i++)
279 if (((Dih < edOmega) ) ||
280 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
281 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
283 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
284 dlist[i].ntr[Dih] = tr_h[j];
285 for (k = 0; k < NROT; k++)
287 dlist[i].rot_occ[Dih][k] = rot_occ[k][j];
294 /* end addition by grs */
298 sprintf(title, "Number of transitions: %s", grpname);
299 fp = xvgropen(fn_trans, title, "Time (ps)", "# transitions/timeframe", oenv);
300 for (j = 0; (j < nframes); j++)
302 fprintf(fp, "%10.3f %10d\n", time[j], tr_f[j]);
307 /* Compute histogram from # transitions per dihedral */
309 for (j = 0; (j < nframes); j++)
313 for (i = 0; (i < nangles); i++)
317 for (j = nframes; ((tr_f[j-1] == 0) && (j > 0)); j--)
325 sprintf(title, "Transition time: %s", grpname);
326 fp = xvgropen(fn_histo, title, "Time (ps)", "#", oenv);
327 for (i = j-1; (i > 0); i--)
331 fprintf(fp, "%10.3f %10d\n", ttime/i, tr_f[i]);
339 for (k = 0; k < NROT; k++)
346 void mk_multiplicity_lookup (int *multiplicity, int maxchi,
347 int nlist, t_dlist dlist[], int nangles)
349 /* new by grs - for dihedral j (as in dih[j]) get multiplicity from dlist
350 * and store in multiplicity[j]
357 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
359 for (i = 0; (i < nlist); i++)
361 strncpy(name, dlist[i].name, 3);
363 if (((Dih < edOmega) ) ||
364 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
365 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
367 /* default - we will correct the rest below */
370 /* make omegas 2fold, though doesn't make much more sense than 3 */
371 if (Dih == edOmega && (has_dihedral(edOmega, &(dlist[i]))))
376 /* dihedrals to aromatic rings, COO, CONH2 or guanidinium are 2fold*/
377 if (Dih > edOmega && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1))
379 if ( ((strstr(name, "PHE") != NULL) && (Dih == edChi2)) ||
380 ((strstr(name, "TYR") != NULL) && (Dih == edChi2)) ||
381 ((strstr(name, "PTR") != NULL) && (Dih == edChi2)) ||
382 ((strstr(name, "TRP") != NULL) && (Dih == edChi2)) ||
383 ((strstr(name, "HIS") != NULL) && (Dih == edChi2)) ||
384 ((strstr(name, "GLU") != NULL) && (Dih == edChi3)) ||
385 ((strstr(name, "ASP") != NULL) && (Dih == edChi2)) ||
386 ((strstr(name, "GLN") != NULL) && (Dih == edChi3)) ||
387 ((strstr(name, "ASN") != NULL) && (Dih == edChi2)) ||
388 ((strstr(name, "ARG") != NULL) && (Dih == edChi4)) )
399 fprintf(stderr, "WARNING: not all dihedrals found in topology (only %d out of %d)!\n",
402 /* Check for remaining dihedrals */
403 for (; (j < nangles); j++)
410 void mk_chi_lookup (int **lookup, int maxchi,
411 int nlist, t_dlist dlist[])
414 /* by grs. should rewrite everything to use this. (but haven't,
415 * and at mmt only used in get_chi_product_traj
416 * returns the dihed number given the residue number (from-0)
417 * and chi (from-0) nr. -1 for chi undefined for that res (eg gly, ala..)*/
422 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
424 for (i = 0; (i < nlist); i++)
427 if (((Dih < edOmega) ) ||
428 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
429 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
431 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
448 void get_chi_product_traj (real **dih, int nframes, int nlist,
449 int maxchi, t_dlist dlist[], real time[],
450 int **lookup, int *multiplicity, gmx_bool bRb, gmx_bool bNormalize,
451 real core_frac, gmx_bool bAll, const char *fnall,
452 const output_env_t oenv)
455 gmx_bool bRotZero, bHaveChi = FALSE;
456 int accum = 0, index, i, j, k, Xi, n, b;
461 char hisfile[256], histitle[256], *namept;
463 int (*calc_bin)(real, int, real);
465 /* Analysis of dihedral transitions */
466 fprintf(stderr, "Now calculating Chi product trajectories...\n");
470 calc_bin = calc_RBbin;
474 calc_bin = calc_Nbin;
477 snew(chi_prtrj, nframes);
479 /* file for info on all residues */
482 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "Probability", oenv);
486 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "# Counts", oenv);
489 for (i = 0; (i < nlist); i++)
492 /* get nbin, the nr. of cumulative rotamers that need to be considered */
494 for (Xi = 0; Xi < maxchi; Xi++)
496 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
499 n = multiplicity[index];
503 nbin += 1; /* for the "zero rotamer", outside the core region */
505 for (j = 0; (j < nframes); j++)
510 index = lookup[i][0]; /* index into dih of chi1 of res i */
519 b = calc_bin(dih[index][j], multiplicity[index], core_frac);
525 for (Xi = 1; Xi < maxchi; Xi++)
527 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
530 n = multiplicity[index];
531 b = calc_bin(dih[index][j], n, core_frac);
532 accum = n * accum + b - 1;
547 chi_prtrj[j] = accum;
559 /* print cuml rotamer vs time */
560 print_one(oenv, "chiproduct", dlist[i].name, "chi product for",
561 "cumulative rotamer", nframes, time, chi_prtrj);
564 /* make a histogram pf culm. rotamer occupancy too */
565 snew(chi_prhist, nbin);
566 make_histo(NULL, nframes, chi_prtrj, nbin, chi_prhist, 0, nbin);
569 sprintf(hisfile, "histo-chiprod%s.xvg", dlist[i].name);
570 sprintf(histitle, "cumulative rotamer distribution for %s", dlist[i].name);
571 fprintf(stderr, " and %s ", hisfile);
572 fp = xvgropen(hisfile, histitle, "number", "", oenv);
573 fprintf(fp, "@ xaxis tick on\n");
574 fprintf(fp, "@ xaxis tick major 1\n");
575 fprintf(fp, "@ type xy\n");
576 for (k = 0; (k < nbin); k++)
580 fprintf(fp, "%5d %10g\n", k, (1.0*chi_prhist[k])/nframes);
584 fprintf(fp, "%5d %10d\n", k, chi_prhist[k]);
591 /* and finally print out occupancies to a single file */
592 /* get the gmx from-1 res nr by setting a ptr to the number part
593 * of dlist[i].name - potential bug for 4-letter res names... */
594 namept = dlist[i].name + 3;
595 fprintf(fpall, "%5s ", namept);
596 for (k = 0; (k < nbin); k++)
600 fprintf(fpall, " %10g", (1.0*chi_prhist[k])/nframes);
604 fprintf(fpall, " %10d", chi_prhist[k]);
607 fprintf(fpall, "\n");
616 fprintf(stderr, "\n");
620 void calc_distribution_props(int nh, int histo[], real start,
621 int nkkk, t_karplus kkk[],
624 real d, dc, ds, c1, c2, tdc, tds;
625 real fac, ang, invth, Jc;
630 gmx_fatal(FARGS, "No points in histogram (%s, %d)", __FILE__, __LINE__);
634 /* Compute normalisation factor */
636 for (j = 0; (j < nh); j++)
642 for (i = 0; (i < nkkk); i++)
648 for (j = 0; (j < nh); j++)
658 for (i = 0; (i < nkkk); i++)
660 c1 = cos(ang+kkk[i].offset);
662 Jc = (kkk[i].A*c2 + kkk[i].B*c1 + kkk[i].C);
663 kkk[i].Jc += histo[j]*Jc;
664 kkk[i].Jcsig += histo[j]*sqr(Jc);
667 for (i = 0; (i < nkkk); i++)
670 kkk[i].Jcsig = sqrt(kkk[i].Jcsig/th-sqr(kkk[i].Jc));
672 *S2 = tdc*tdc+tds*tds;
675 static void calc_angles(t_pbc *pbc,
676 int n3, atom_id index[], real ang[], rvec x_s[])
682 for (i = ix = 0; (ix < n3); i++, ix += 3)
684 ang[i] = bond_angle(x_s[index[ix]], x_s[index[ix+1]], x_s[index[ix+2]],
685 pbc, r_ij, r_kj, &costh, &t1, &t2);
689 fprintf(debug, "Angle[0]=%g, costh=%g, index0 = %d, %d, %d\n",
690 ang[0], costh, index[0], index[1], index[2]);
691 pr_rvec(debug, 0, "rij", r_ij, DIM, TRUE);
692 pr_rvec(debug, 0, "rkj", r_kj, DIM, TRUE);
696 static real calc_fraction(real angles[], int nangles)
699 real trans = 0, gauche = 0;
702 for (i = 0; i < nangles; i++)
704 angle = angles[i] * RAD2DEG;
706 if (angle > 135 && angle < 225)
710 else if (angle > 270 && angle < 330)
714 else if (angle < 90 && angle > 30)
719 if (trans+gauche > 0)
721 return trans/(trans+gauche);
729 static void calc_dihs(t_pbc *pbc,
730 int n4, atom_id index[], real ang[], rvec x_s[])
732 int i, ix, t1, t2, t3;
733 rvec r_ij, r_kj, r_kl, m, n;
736 for (i = ix = 0; (ix < n4); i++, ix += 4)
738 aaa = dih_angle(x_s[index[ix]], x_s[index[ix+1]], x_s[index[ix+2]],
739 x_s[index[ix+3]], pbc,
740 r_ij, r_kj, r_kl, m, n,
741 &sign, &t1, &t2, &t3);
743 ang[i] = aaa; /* not taking into account ryckaert bellemans yet */
747 void make_histo(FILE *log,
748 int ndata, real data[], int npoints, int histo[],
749 real minx, real maxx)
756 minx = maxx = data[0];
757 for (i = 1; (i < ndata); i++)
759 minx = min(minx, data[i]);
760 maxx = max(maxx, data[i]);
762 fprintf(log, "Min data: %10g Max data: %10g\n", minx, maxx);
764 dx = (double)npoints/(maxx-minx);
768 "Histogramming: ndata=%d, nhisto=%d, minx=%g,maxx=%g,dx=%g\n",
769 ndata, npoints, minx, maxx, dx);
771 for (i = 0; (i < ndata); i++)
773 ind = (data[i]-minx)*dx;
774 if ((ind >= 0) && (ind < npoints))
780 fprintf(log, "index = %d, data[%d] = %g\n", ind, i, data[i]);
785 void normalize_histo(int npoints, int histo[], real dx, real normhisto[])
791 for (i = 0; (i < npoints); i++)
797 fprintf(stderr, "Empty histogram!\n");
801 for (i = 0; (i < npoints); i++)
803 normhisto[i] = fac*histo[i];
807 void read_ang_dih(const char *trj_fn,
808 gmx_bool bAngles, gmx_bool bSaveAll, gmx_bool bRb, gmx_bool bPBC,
809 int maxangstat, int angstat[],
810 int *nframes, real **time,
811 int isize, atom_id index[],
815 const output_env_t oenv)
819 int i, angind, natoms, total, teller;
820 int nangles, n_alloc;
821 real t, fraction, pifac, aa, angle;
829 natoms = read_first_x(oenv, &status, trj_fn, &t, &x, box);
841 snew(angles[cur], nangles);
842 snew(angles[prev], nangles);
844 /* Start the loop over frames */
854 if (teller >= n_alloc)
859 for (i = 0; (i < nangles); i++)
861 srenew(dih[i], n_alloc);
864 srenew(*time, n_alloc);
865 srenew(*trans_frac, n_alloc);
866 srenew(*aver_angle, n_alloc);
873 set_pbc(pbc, -1, box);
878 calc_angles(pbc, isize, index, angles[cur], x);
882 calc_dihs(pbc, isize, index, angles[cur], x);
885 fraction = calc_fraction(angles[cur], nangles);
886 (*trans_frac)[teller] = fraction;
888 /* Change Ryckaert-Bellemans dihedrals to polymer convention
889 * Modified 990913 by Erik:
890 * We actually shouldn't change the convention, since it's
891 * calculated from polymer above, but we change the intervall
892 * from [-180,180] to [0,360].
896 for (i = 0; (i < nangles); i++)
898 if (angles[cur][i] <= 0.0)
900 angles[cur][i] += 2*M_PI;
905 /* Periodicity in dihedral space... */
908 for (i = 0; (i < nangles); i++)
910 real dd = angles[cur][i];
911 angles[cur][i] = atan2(sin(dd), cos(dd));
918 for (i = 0; (i < nangles); i++)
920 while (angles[cur][i] <= angles[prev][i] - M_PI)
922 angles[cur][i] += 2*M_PI;
924 while (angles[cur][i] > angles[prev][i] + M_PI)
926 angles[cur][i] -= 2*M_PI;
935 for (i = 0; (i < nangles); i++)
937 aa = aa+angles[cur][i];
939 /* angle in rad / 2Pi * max determines bin. bins go from 0 to maxangstat,
940 even though scale goes from -pi to pi (dihedral) or -pi/2 to pi/2
941 (angle) Basically: translate the x-axis by Pi. Translate it back by
945 angle = angles[cur][i];
948 while (angle < -M_PI)
952 while (angle >= M_PI)
960 /* Update the distribution histogram */
961 angind = (int) ((angle*maxangstat)/pifac + 0.5);
962 if (angind == maxangstat)
966 if ( (angind < 0) || (angind >= maxangstat) )
968 /* this will never happen */
969 gmx_fatal(FARGS, "angle (%f) index out of range (0..%d) : %d\n",
970 angle, maxangstat, angind);
974 if (angind == maxangstat)
976 fprintf(stderr, "angle %d fr %d = %g\n", i, cur, angle);
982 /* average over all angles */
983 (*aver_angle)[teller] = (aa/nangles);
985 /* this copies all current dih. angles to dih[i], teller is frame */
988 for (i = 0; i < nangles; i++)
990 dih[i][teller] = angles[cur][i];
997 /* Increment loop counter */
1000 while (read_next_x(oenv, status, &t, x, box));
1005 sfree(angles[prev]);