3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Green Red Orange Magenta Azure Cyan Skyblue
53 void print_one(const output_env_t oenv, const char *base, const char *name,
54 const char *title, const char *ylabel, int nf, real time[],
58 char buf[256], t2[256];
61 sprintf(buf, "%s%s.xvg", base, name);
62 fprintf(stderr, "\rPrinting %s ", buf);
63 sprintf(t2, "%s %s", title, name);
64 fp = xvgropen(buf, t2, "Time (ps)", ylabel, oenv);
65 for (k = 0; (k < nf); k++)
67 fprintf(fp, "%10g %10g\n", time[k], data[k]);
72 static int calc_RBbin(real phi, int gmx_unused multiplicity, real gmx_unused core_frac)
74 /* multiplicity and core_frac NOT used,
75 * just given to enable use of pt-to-fn in caller low_ana_dih_trans*/
76 static const real r30 = M_PI/6.0;
77 static const real r90 = M_PI/2.0;
78 static const real r150 = M_PI*5.0/6.0;
80 if ((phi < r30) && (phi > -r30))
84 else if ((phi > -r150) && (phi < -r90))
88 else if ((phi < r150) && (phi > r90))
95 static int calc_Nbin(real phi, int multiplicity, real core_frac)
97 static const real r360 = 360*DEG2RAD;
98 real rot_width, core_width, core_offset, low, hi;
100 /* with multiplicity 3 and core_frac 0.5
101 * 0<g(-)<120, 120<t<240, 240<g(+)<360
102 * 0< bin0 < 30, 30<bin1<90, 90<bin0<150, 150<bin2<210, 210<bin0<270, 270<bin3<330, 330<bin0<360
103 * so with multiplicity 3, bin1 is core g(-), bin2 is core t, bin3 is
104 core g(+), bin0 is between rotamers */
110 rot_width = 360/multiplicity;
111 core_width = core_frac * rot_width;
112 core_offset = (rot_width - core_width)/2.0;
113 for (bin = 1; bin <= multiplicity; bin++)
115 low = ((bin - 1) * rot_width ) + core_offset;
116 hi = ((bin - 1) * rot_width ) + core_offset + core_width;
119 if ((phi > low) && (phi < hi))
127 void ana_dih_trans(const char *fn_trans, const char *fn_histo,
128 real **dih, int nframes, int nangles,
129 const char *grpname, real *time, gmx_bool bRb,
130 const output_env_t oenv)
132 /* just a wrapper; declare extra args, then chuck away at end. */
140 snew(multiplicity, nangles);
141 for (k = 0; (k < nangles); k++)
146 low_ana_dih_trans(TRUE, fn_trans, TRUE, fn_histo, maxchi,
147 dih, nlist, dlist, nframes,
148 nangles, grpname, multiplicity, time, bRb, 0.5, oenv);
154 void low_ana_dih_trans(gmx_bool bTrans, const char *fn_trans,
155 gmx_bool bHisto, const char *fn_histo, int maxchi,
156 real **dih, int nlist, t_dlist dlist[], int nframes,
157 int nangles, const char *grpname, int multiplicity[],
158 real *time, gmx_bool bRb, real core_frac,
159 const output_env_t oenv)
164 int i, j, k, Dih, ntrans;
165 int cur_bin, new_bin;
168 int (*calc_bin)(real, int, real);
175 /* Assumes the frames are equally spaced in time */
176 dt = (time[nframes-1]-time[0])/(nframes-1);
178 /* Analysis of dihedral transitions */
179 fprintf(stderr, "Now calculating transitions...\n");
183 calc_bin = calc_RBbin;
187 calc_bin = calc_Nbin;
190 for (k = 0; k < NROT; k++)
192 snew(rot_occ[k], nangles);
193 for (i = 0; (i < nangles); i++)
201 /* dih[i][j] is the dihedral angle i in frame j */
203 for (i = 0; (i < nangles); i++)
208 mind = maxd = prev = dih[i][0];
210 cur_bin = calc_bin(dih[i][0], multiplicity[i], core_frac);
211 rot_occ[cur_bin][i]++;
213 for (j = 1; (j < nframes); j++)
215 new_bin = calc_bin(dih[i][j], multiplicity[i], core_frac);
216 rot_occ[new_bin][i]++;
222 else if ((new_bin != 0) && (cur_bin != new_bin))
230 /* why is all this md rubbish periodic? Remove 360 degree periodicity */
231 if ( (dih[i][j] - prev) > M_PI)
235 else if ( (dih[i][j] - prev) < -M_PI)
242 mind = min(mind, dih[i][j]);
243 maxd = max(maxd, dih[i][j]);
244 if ( (maxd - mind) > 2*M_PI/3) /* or 120 degrees, assuming */
245 { /* multiplicity 3. Not so general.*/
248 maxd = mind = dih[i][j]; /* get ready for next transition */
253 for (k = 0; k < NROT; k++)
255 rot_occ[k][i] /= nframes;
258 fprintf(stderr, "Total number of transitions: %10d\n", ntrans);
261 ttime = (dt*nframes*nangles)/ntrans;
262 fprintf(stderr, "Time between transitions: %10.3f ps\n", ttime);
265 /* new by grs - copy transitions from tr_h[] to dlist->ntr[]
266 * and rotamer populations from rot_occ to dlist->rot_occ[]
267 * based on fn histogramming in g_chi. diff roles for i and j here */
270 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
272 for (i = 0; (i < nlist); i++)
274 if (((Dih < edOmega) ) ||
275 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
276 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
278 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
279 dlist[i].ntr[Dih] = tr_h[j];
280 for (k = 0; k < NROT; k++)
282 dlist[i].rot_occ[Dih][k] = rot_occ[k][j];
289 /* end addition by grs */
293 sprintf(title, "Number of transitions: %s", grpname);
294 fp = xvgropen(fn_trans, title, "Time (ps)", "# transitions/timeframe", oenv);
295 for (j = 0; (j < nframes); j++)
297 fprintf(fp, "%10.3f %10d\n", time[j], tr_f[j]);
302 /* Compute histogram from # transitions per dihedral */
304 for (j = 0; (j < nframes); j++)
308 for (i = 0; (i < nangles); i++)
312 for (j = nframes; ((tr_f[j-1] == 0) && (j > 0)); j--)
320 sprintf(title, "Transition time: %s", grpname);
321 fp = xvgropen(fn_histo, title, "Time (ps)", "#", oenv);
322 for (i = j-1; (i > 0); i--)
326 fprintf(fp, "%10.3f %10d\n", ttime/i, tr_f[i]);
334 for (k = 0; k < NROT; k++)
341 void mk_multiplicity_lookup (int *multiplicity, int maxchi,
342 int nlist, t_dlist dlist[], int nangles)
344 /* new by grs - for dihedral j (as in dih[j]) get multiplicity from dlist
345 * and store in multiplicity[j]
352 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
354 for (i = 0; (i < nlist); i++)
356 strncpy(name, dlist[i].name, 3);
358 if (((Dih < edOmega) ) ||
359 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
360 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
362 /* default - we will correct the rest below */
365 /* make omegas 2fold, though doesn't make much more sense than 3 */
366 if (Dih == edOmega && (has_dihedral(edOmega, &(dlist[i]))))
371 /* dihedrals to aromatic rings, COO, CONH2 or guanidinium are 2fold*/
372 if (Dih > edOmega && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1))
374 if ( ((strstr(name, "PHE") != NULL) && (Dih == edChi2)) ||
375 ((strstr(name, "TYR") != NULL) && (Dih == edChi2)) ||
376 ((strstr(name, "PTR") != NULL) && (Dih == edChi2)) ||
377 ((strstr(name, "TRP") != NULL) && (Dih == edChi2)) ||
378 ((strstr(name, "HIS") != NULL) && (Dih == edChi2)) ||
379 ((strstr(name, "GLU") != NULL) && (Dih == edChi3)) ||
380 ((strstr(name, "ASP") != NULL) && (Dih == edChi2)) ||
381 ((strstr(name, "GLN") != NULL) && (Dih == edChi3)) ||
382 ((strstr(name, "ASN") != NULL) && (Dih == edChi2)) ||
383 ((strstr(name, "ARG") != NULL) && (Dih == edChi4)) )
394 fprintf(stderr, "WARNING: not all dihedrals found in topology (only %d out of %d)!\n",
397 /* Check for remaining dihedrals */
398 for (; (j < nangles); j++)
405 void mk_chi_lookup (int **lookup, int maxchi,
406 int nlist, t_dlist dlist[])
409 /* by grs. should rewrite everything to use this. (but haven't,
410 * and at mmt only used in get_chi_product_traj
411 * returns the dihed number given the residue number (from-0)
412 * and chi (from-0) nr. -1 for chi undefined for that res (eg gly, ala..)*/
417 for (Dih = 0; (Dih < NONCHI+maxchi); Dih++)
419 for (i = 0; (i < nlist); i++)
422 if (((Dih < edOmega) ) ||
423 ((Dih == edOmega) && (has_dihedral(edOmega, &(dlist[i])))) ||
424 ((Dih > edOmega) && (dlist[i].atm.Cn[Dih-NONCHI+3] != -1)))
426 /* grs debug printf("Not OK? i %d j %d Dih %d \n", i, j, Dih) ; */
443 void get_chi_product_traj (real **dih, int nframes, int nlist,
444 int maxchi, t_dlist dlist[], real time[],
445 int **lookup, int *multiplicity, gmx_bool bRb, gmx_bool bNormalize,
446 real core_frac, gmx_bool bAll, const char *fnall,
447 const output_env_t oenv)
450 gmx_bool bRotZero, bHaveChi = FALSE;
451 int accum = 0, index, i, j, k, Xi, n, b;
456 char hisfile[256], histitle[256], *namept;
458 int (*calc_bin)(real, int, real);
460 /* Analysis of dihedral transitions */
461 fprintf(stderr, "Now calculating Chi product trajectories...\n");
465 calc_bin = calc_RBbin;
469 calc_bin = calc_Nbin;
472 snew(chi_prtrj, nframes);
474 /* file for info on all residues */
477 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "Probability", oenv);
481 fpall = xvgropen(fnall, "Cumulative Rotamers", "Residue", "# Counts", oenv);
484 for (i = 0; (i < nlist); i++)
487 /* get nbin, the nr. of cumulative rotamers that need to be considered */
489 for (Xi = 0; Xi < maxchi; Xi++)
491 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
494 n = multiplicity[index];
498 nbin += 1; /* for the "zero rotamer", outside the core region */
500 for (j = 0; (j < nframes); j++)
505 index = lookup[i][0]; /* index into dih of chi1 of res i */
514 b = calc_bin(dih[index][j], multiplicity[index], core_frac);
520 for (Xi = 1; Xi < maxchi; Xi++)
522 index = lookup[i][Xi]; /* chi_(Xi+1) of res i (-1 if off end) */
525 n = multiplicity[index];
526 b = calc_bin(dih[index][j], n, core_frac);
527 accum = n * accum + b - 1;
542 chi_prtrj[j] = accum;
554 /* print cuml rotamer vs time */
555 print_one(oenv, "chiproduct", dlist[i].name, "chi product for",
556 "cumulative rotamer", nframes, time, chi_prtrj);
559 /* make a histogram pf culm. rotamer occupancy too */
560 snew(chi_prhist, nbin);
561 make_histo(NULL, nframes, chi_prtrj, nbin, chi_prhist, 0, nbin);
564 sprintf(hisfile, "histo-chiprod%s.xvg", dlist[i].name);
565 sprintf(histitle, "cumulative rotamer distribution for %s", dlist[i].name);
566 fprintf(stderr, " and %s ", hisfile);
567 fp = xvgropen(hisfile, histitle, "number", "", oenv);
568 fprintf(fp, "@ xaxis tick on\n");
569 fprintf(fp, "@ xaxis tick major 1\n");
570 fprintf(fp, "@ type xy\n");
571 for (k = 0; (k < nbin); k++)
575 fprintf(fp, "%5d %10g\n", k, (1.0*chi_prhist[k])/nframes);
579 fprintf(fp, "%5d %10d\n", k, chi_prhist[k]);
586 /* and finally print out occupancies to a single file */
587 /* get the gmx from-1 res nr by setting a ptr to the number part
588 * of dlist[i].name - potential bug for 4-letter res names... */
589 namept = dlist[i].name + 3;
590 fprintf(fpall, "%5s ", namept);
591 for (k = 0; (k < nbin); k++)
595 fprintf(fpall, " %10g", (1.0*chi_prhist[k])/nframes);
599 fprintf(fpall, " %10d", chi_prhist[k]);
602 fprintf(fpall, "\n");
611 fprintf(stderr, "\n");
615 void calc_distribution_props(int nh, int histo[], real start,
616 int nkkk, t_karplus kkk[],
619 real d, dc, ds, c1, c2, tdc, tds;
620 real fac, ang, invth, Jc;
625 gmx_fatal(FARGS, "No points in histogram (%s, %d)", __FILE__, __LINE__);
629 /* Compute normalisation factor */
631 for (j = 0; (j < nh); j++)
637 for (i = 0; (i < nkkk); i++)
643 for (j = 0; (j < nh); j++)
653 for (i = 0; (i < nkkk); i++)
655 c1 = cos(ang+kkk[i].offset);
657 Jc = (kkk[i].A*c2 + kkk[i].B*c1 + kkk[i].C);
658 kkk[i].Jc += histo[j]*Jc;
659 kkk[i].Jcsig += histo[j]*sqr(Jc);
662 for (i = 0; (i < nkkk); i++)
665 kkk[i].Jcsig = sqrt(kkk[i].Jcsig/th-sqr(kkk[i].Jc));
667 *S2 = tdc*tdc+tds*tds;
670 static void calc_angles(t_pbc *pbc,
671 int n3, atom_id index[], real ang[], rvec x_s[])
677 for (i = ix = 0; (ix < n3); i++, ix += 3)
679 ang[i] = bond_angle(x_s[index[ix]], x_s[index[ix+1]], x_s[index[ix+2]],
680 pbc, r_ij, r_kj, &costh, &t1, &t2);
684 fprintf(debug, "Angle[0]=%g, costh=%g, index0 = %d, %d, %d\n",
685 ang[0], costh, index[0], index[1], index[2]);
686 pr_rvec(debug, 0, "rij", r_ij, DIM, TRUE);
687 pr_rvec(debug, 0, "rkj", r_kj, DIM, TRUE);
691 static real calc_fraction(real angles[], int nangles)
694 real trans = 0, gauche = 0;
697 for (i = 0; i < nangles; i++)
699 angle = angles[i] * RAD2DEG;
701 if (angle > 135 && angle < 225)
705 else if (angle > 270 && angle < 330)
709 else if (angle < 90 && angle > 30)
714 if (trans+gauche > 0)
716 return trans/(trans+gauche);
724 static void calc_dihs(t_pbc *pbc,
725 int n4, atom_id index[], real ang[], rvec x_s[])
727 int i, ix, t1, t2, t3;
728 rvec r_ij, r_kj, r_kl, m, n;
731 for (i = ix = 0; (ix < n4); i++, ix += 4)
733 aaa = dih_angle(x_s[index[ix]], x_s[index[ix+1]], x_s[index[ix+2]],
734 x_s[index[ix+3]], pbc,
735 r_ij, r_kj, r_kl, m, n,
736 &sign, &t1, &t2, &t3);
738 ang[i] = aaa; /* not taking into account ryckaert bellemans yet */
742 void make_histo(FILE *log,
743 int ndata, real data[], int npoints, int histo[],
744 real minx, real maxx)
751 minx = maxx = data[0];
752 for (i = 1; (i < ndata); i++)
754 minx = min(minx, data[i]);
755 maxx = max(maxx, data[i]);
757 fprintf(log, "Min data: %10g Max data: %10g\n", minx, maxx);
759 dx = (double)npoints/(maxx-minx);
763 "Histogramming: ndata=%d, nhisto=%d, minx=%g,maxx=%g,dx=%g\n",
764 ndata, npoints, minx, maxx, dx);
766 for (i = 0; (i < ndata); i++)
768 ind = (data[i]-minx)*dx;
769 if ((ind >= 0) && (ind < npoints))
775 fprintf(log, "index = %d, data[%d] = %g\n", ind, i, data[i]);
780 void normalize_histo(int npoints, int histo[], real dx, real normhisto[])
786 for (i = 0; (i < npoints); i++)
792 fprintf(stderr, "Empty histogram!\n");
796 for (i = 0; (i < npoints); i++)
798 normhisto[i] = fac*histo[i];
802 void read_ang_dih(const char *trj_fn,
803 gmx_bool bAngles, gmx_bool bSaveAll, gmx_bool bRb, gmx_bool bPBC,
804 int maxangstat, int angstat[],
805 int *nframes, real **time,
806 int isize, atom_id index[],
810 const output_env_t oenv)
814 int i, angind, natoms, total, teller;
815 int nangles, n_alloc;
816 real t, fraction, pifac, aa, angle;
824 natoms = read_first_x(oenv, &status, trj_fn, &t, &x, box);
836 snew(angles[cur], nangles);
837 snew(angles[prev], nangles);
839 /* Start the loop over frames */
849 if (teller >= n_alloc)
854 for (i = 0; (i < nangles); i++)
856 srenew(dih[i], n_alloc);
859 srenew(*time, n_alloc);
860 srenew(*trans_frac, n_alloc);
861 srenew(*aver_angle, n_alloc);
868 set_pbc(pbc, -1, box);
873 calc_angles(pbc, isize, index, angles[cur], x);
877 calc_dihs(pbc, isize, index, angles[cur], x);
880 fraction = calc_fraction(angles[cur], nangles);
881 (*trans_frac)[teller] = fraction;
883 /* Change Ryckaert-Bellemans dihedrals to polymer convention
884 * Modified 990913 by Erik:
885 * We actually shouldn't change the convention, since it's
886 * calculated from polymer above, but we change the intervall
887 * from [-180,180] to [0,360].
891 for (i = 0; (i < nangles); i++)
893 if (angles[cur][i] <= 0.0)
895 angles[cur][i] += 2*M_PI;
900 /* Periodicity in dihedral space... */
903 for (i = 0; (i < nangles); i++)
905 real dd = angles[cur][i];
906 angles[cur][i] = atan2(sin(dd), cos(dd));
913 for (i = 0; (i < nangles); i++)
915 while (angles[cur][i] <= angles[prev][i] - M_PI)
917 angles[cur][i] += 2*M_PI;
919 while (angles[cur][i] > angles[prev][i] + M_PI)
921 angles[cur][i] -= 2*M_PI;
930 for (i = 0; (i < nangles); i++)
932 aa = aa+angles[cur][i];
934 /* angle in rad / 2Pi * max determines bin. bins go from 0 to maxangstat,
935 even though scale goes from -pi to pi (dihedral) or -pi/2 to pi/2
936 (angle) Basically: translate the x-axis by Pi. Translate it back by
940 angle = angles[cur][i];
943 while (angle < -M_PI)
947 while (angle >= M_PI)
955 /* Update the distribution histogram */
956 angind = (int) ((angle*maxangstat)/pifac + 0.5);
957 if (angind == maxangstat)
961 if ( (angind < 0) || (angind >= maxangstat) )
963 /* this will never happen */
964 gmx_fatal(FARGS, "angle (%f) index out of range (0..%d) : %d\n",
965 angle, maxangstat, angind);
969 if (angind == maxangstat)
971 fprintf(stderr, "angle %d fr %d = %g\n", i, cur, angle);
977 /* average over all angles */
978 (*aver_angle)[teller] = (aa/nangles);
980 /* this copies all current dih. angles to dih[i], teller is frame */
983 for (i = 0; i < nangles; i++)
985 dih[i][teller] = angles[cur][i];
992 /* Increment loop counter */
995 while (read_next_x(oenv, status, &t, x, box));
1000 sfree(angles[prev]);