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
61 static void calc_dist(int nind, atom_id index[], rvec x[], int ePBC, matrix box,
70 set_pbc(&pbc, ePBC, box);
71 for (i = 0; (i < nind-1); i++)
74 for (j = i+1; (j < nind); j++)
76 pbc_dx(&pbc, xi, x[index[j]], dx);
78 d[i][j] = sqrt(temp2);
83 static void calc_dist_tot(int nind, atom_id index[], rvec x[],
85 real **d, real **dtot, real **dtot2,
86 gmx_bool bNMR, real **dtot1_3, real **dtot1_6)
90 real temp, temp2, temp1_3;
94 set_pbc(&pbc, ePBC, box);
95 for (i = 0; (i < nind-1); i++)
98 for (j = i+1; (j < nind); j++)
100 pbc_dx(&pbc, xi, x[index[j]], dx);
105 dtot2[i][j] += temp2;
108 temp1_3 = 1.0/(temp*temp2);
109 dtot1_3[i][j] += temp1_3;
110 dtot1_6[i][j] += temp1_3*temp1_3;
116 static void calc_nmr(int nind, int nframes, real **dtot1_3, real **dtot1_6,
117 real *max1_3, real *max1_6)
120 real temp1_3, temp1_6;
122 for (i = 0; (i < nind-1); i++)
124 for (j = i+1; (j < nind); j++)
126 temp1_3 = pow(dtot1_3[i][j]/nframes, -1.0/3.0);
127 temp1_6 = pow(dtot1_6[i][j]/nframes, -1.0/6.0);
128 if (temp1_3 > *max1_3)
132 if (temp1_6 > *max1_6)
136 dtot1_3[i][j] = temp1_3;
137 dtot1_6[i][j] = temp1_6;
138 dtot1_3[j][i] = temp1_3;
139 dtot1_6[j][i] = temp1_6;
144 static char Hnum[] = "123";
169 static int read_equiv(const char *eq_fn, t_equiv ***equivptr)
172 char line[STRLEN], resname[10], atomname[10], *lp;
173 int neq, na, n, resnr;
176 fp = ffopen(eq_fn, "r");
179 while (get_a_line(fp, line, STRLEN))
182 /* this is not efficient, but I'm lazy */
183 srenew(equiv, neq+1);
186 if (sscanf(lp, "%s %n", atomname, &n) == 1)
190 equiv[neq][0].nname = strdup(atomname);
191 while (sscanf(lp, "%d %s %s %n", &resnr, resname, atomname, &n) == 3)
193 /* this is not efficient, but I'm lazy (again) */
194 srenew(equiv[neq], na+1);
195 equiv[neq][na].rnr = resnr-1;
196 equiv[neq][na].rname = strdup(resname);
197 equiv[neq][na].aname = strdup(atomname);
200 equiv[neq][na].nname = NULL;
206 /* make empty element as flag for end of array */
207 srenew(equiv[neq], na+1);
208 equiv[neq][na].rnr = NOTSET;
209 equiv[neq][na].rname = NULL;
210 equiv[neq][na].aname = NULL;
222 static void dump_equiv(FILE *out, int neq, t_equiv **equiv)
226 fprintf(out, "Dumping equivalent list\n");
227 for (i = 0; i < neq; i++)
229 fprintf(out, "%s", equiv[i][0].nname);
230 for (j = 0; equiv[i][j].rnr != NOTSET; j++)
232 fprintf(out, " %d %s %s",
233 equiv[i][j].rnr, equiv[i][j].rname, equiv[i][j].aname);
239 static gmx_bool is_equiv(int neq, t_equiv **equiv, char **nname,
240 int rnr1, char *rname1, char *aname1,
241 int rnr2, char *rname2, char *aname2)
247 /* we can terminate each loop when bFound is true! */
248 for (i = 0; i < neq && !bFound; i++)
250 /* find first atom */
251 for (j = 0; equiv[i][j].rnr != NOTSET && !bFound; j++)
253 bFound = ( equiv[i][j].rnr == rnr1 &&
254 strcmp(equiv[i][j].rname, rname1) == 0 &&
255 strcmp(equiv[i][j].aname, aname1) == 0 );
259 /* find second atom */
261 for (j = 0; equiv[i][j].rnr != NOTSET && !bFound; j++)
263 bFound = ( equiv[i][j].rnr == rnr2 &&
264 strcmp(equiv[i][j].rname, rname2) == 0 &&
265 strcmp(equiv[i][j].aname, aname2) == 0 );
271 *nname = strdup(equiv[i-1][0].nname);
277 static int analyze_noe_equivalent(const char *eq_fn,
278 t_atoms *atoms, int isize, atom_id *index,
280 atom_id *noe_index, t_noe_gr *noe_gr)
283 int i, j, anmil, anmjl, rnri, rnrj, gi, groupnr, neq;
284 char *anmi, *anmj, **nnm;
285 gmx_bool bMatch, bEquiv;
293 neq = read_equiv(eq_fn, &equiv);
296 dump_equiv(debug, neq, equiv);
306 for (i = 0; i < isize; i++)
308 if (equiv && i < isize-1)
310 /* check explicit list of equivalent atoms */
314 rnri = atoms->atom[index[i]].resind;
315 rnrj = atoms->atom[index[j]].resind;
317 is_equiv(neq, equiv, &nnm[i],
318 rnri, *atoms->resinfo[rnri].name, *atoms->atomname[index[i]],
319 rnrj, *atoms->resinfo[rnrj].name, *atoms->atomname[index[j]]);
320 if (nnm[i] && bEquiv)
322 nnm[j] = strdup(nnm[i]);
326 /* set index for matching atom */
327 noe_index[j] = groupnr;
328 /* skip matching atom */
332 while (bEquiv && i < isize-1);
340 /* look for triplets of consecutive atoms with name XX?,
341 X are any number of letters or digits and ? goes from 1 to 3
342 This is supposed to cover all CH3 groups and the like */
343 anmi = *atoms->atomname[index[i]];
344 anmil = strlen(anmi);
345 bMatch = i < isize-3 && anmi[anmil-1] == '1';
348 for (j = 1; j < 3; j++)
350 anmj = *atoms->atomname[index[i+j]];
351 anmjl = strlen(anmj);
352 bMatch = bMatch && ( anmil == anmjl && anmj[anmjl-1] == Hnum[j] &&
353 strncmp(anmi, anmj, anmil-1) == 0 );
356 /* set index for this atom */
357 noe_index[i] = groupnr;
360 /* set index for next two matching atoms */
361 for (j = 1; j < 3; j++)
363 noe_index[i+j] = groupnr;
365 /* skip matching atoms */
374 /* make index without looking for equivalent atoms */
375 for (i = 0; i < isize; i++)
381 noe_index[isize] = groupnr;
386 for (i = 0; i < isize; i++)
388 rnri = atoms->atom[index[i]].resind;
389 fprintf(debug, "%s %s %d -> %s\n", *atoms->atomname[index[i]],
390 *atoms->resinfo[rnri].name, rnri, nnm[i] ? nnm[i] : "");
394 for (i = 0; i < isize; i++)
397 if (!noe_gr[gi].aname)
400 noe_gr[gi].anr = index[i];
403 noe_gr[gi].aname = strdup(nnm[i]);
407 noe_gr[gi].aname = strdup(*atoms->atomname[index[i]]);
408 if (noe_index[i] == noe_index[i+1])
410 noe_gr[gi].aname[strlen(noe_gr[gi].aname)-1] = '*';
413 noe_gr[gi].rnr = atoms->atom[index[i]].resind;
414 noe_gr[gi].rname = strdup(*atoms->resinfo[noe_gr[gi].rnr].name);
415 /* dump group definitions */
418 fprintf(debug, "%d %d %d %d %s %s %d\n", i, gi,
419 noe_gr[gi].ianr, noe_gr[gi].anr, noe_gr[gi].aname,
420 noe_gr[gi].rname, noe_gr[gi].rnr);
424 for (i = 0; i < isize; i++)
433 /* #define NSCALE 3 */
434 /* static char *noe_scale[NSCALE+1] = { */
435 /* "strong", "medium", "weak", "none" */
439 static char *noe2scale(real r3, real r6, real rmax)
442 static char buf[NSCALE+1];
444 /* r goes from 0 to rmax
445 NSCALE*r/rmax goes from 0 to NSCALE
446 NSCALE - NSCALE*r/rmax goes from NSCALE to 0 */
447 s3 = NSCALE - min(NSCALE, (int)(NSCALE*r3/rmax));
448 s6 = NSCALE - min(NSCALE, (int)(NSCALE*r6/rmax));
450 for (i = 0; i < s3; i++)
463 static void calc_noe(int isize, atom_id *noe_index,
464 real **dtot1_3, real **dtot1_6, int gnr, t_noe **noe)
468 /* make half matrix of noe-group distances from atom distances */
469 for (i = 0; i < isize; i++)
472 for (j = i; j < isize; j++)
476 noe[gi][gj].i_3 += pow(dtot1_3[i][j], -3);
477 noe[gi][gj].i_6 += pow(dtot1_6[i][j], -6);
482 for (i = 0; i < gnr; i++)
484 for (j = i+1; j < gnr; j++)
486 noe[i][j].r_3 = pow(noe[i][j].i_3/noe[i][j].nr, -1.0/3.0);
487 noe[i][j].r_6 = pow(noe[i][j].i_6/noe[i][j].nr, -1.0/6.0);
488 noe[j][i] = noe[i][j];
493 static void write_noe(FILE *fp, int gnr, t_noe **noe, t_noe_gr *noe_gr, real rmax)
496 real r3, r6, min3, min6;
497 char buf[10], b3[10], b6[10];
502 ";%4s %3s %4s %4s%3s %4s %4s %4s %4s%3s %5s %5s %8s %2s %2s %s\n",
503 "ianr", "anr", "anm", "rnm", "rnr", "ianr", "anr", "anm", "rnm", "rnr",
504 "1/r^3", "1/r^6", "intnsty", "Dr", "Da", "scale");
505 for (i = 0; i < gnr; i++)
508 for (j = i+1; j < gnr; j++)
513 min3 = min(r3, min3);
514 min6 = min(r6, min6);
515 if (r3 < rmax || r6 < rmax)
517 if (grj.rnr == gri.rnr)
519 sprintf(buf, "%2d", grj.anr-gri.anr);
527 sprintf(b3, "%-5.3f", r3);
535 sprintf(b6, "%-5.3f", r6);
542 "%4d %4d %4s %4s%3d %4d %4d %4s %4s%3d %5s %5s %8d %2d %2s %s\n",
543 gri.ianr+1, gri.anr+1, gri.aname, gri.rname, gri.rnr+1,
544 grj.ianr+1, grj.anr+1, grj.aname, grj.rname, grj.rnr+1,
545 b3, b6, (int)(noe[i][j].i_6+0.5), grj.rnr-gri.rnr, buf,
546 noe2scale(r3, r6, rmax));
550 #define MINI ((i == 3) ? min3 : min6)
551 for (i = 3; i <= 6; i += 3)
555 fprintf(stdout, "NOTE: no 1/r^%d averaged distances found below %g, "
556 "smallest was %g\n", i, rmax, MINI );
560 fprintf(stdout, "Smallest 1/r^%d averaged distance was %g\n", i, MINI );
566 static void calc_rms(int nind, int nframes,
567 real **dtot, real **dtot2,
568 real **rmsmat, real *rmsmax,
569 real **rmscmat, real *rmscmax,
570 real **meanmat, real *meanmax)
573 real mean, mean2, rms, rmsc;
574 /* N.B. dtot and dtot2 contain the total distance and the total squared
575 * distance respectively, BUT they return RMS and the scaled RMS resp.
582 for (i = 0; (i < nind-1); i++)
584 for (j = i+1; (j < nind); j++)
586 mean = dtot[i][j]/nframes;
587 mean2 = dtot2[i][j]/nframes;
588 rms = sqrt(max(0, mean2-mean*mean));
602 meanmat[i][j] = meanmat[j][i] = mean;
603 rmsmat[i][j] = rmsmat[j][i] = rms;
604 rmscmat[i][j] = rmscmat[j][i] = rmsc;
609 real rms_diff(int natom, real **d, real **d_r)
615 for (i = 0; (i < natom-1); i++)
617 for (j = i+1; (j < natom); j++)
619 r = d[i][j]-d_r[i][j];
623 r2 /= (natom*(natom-1))/2;
628 int gmx_rmsdist (int argc, char *argv[])
630 const char *desc[] = {
631 "[TT]g_rmsdist[tt] computes the root mean square deviation of atom distances,",
632 "which has the advantage that no fit is needed like in standard RMS",
633 "deviation as computed by [TT]g_rms[tt].",
634 "The reference structure is taken from the structure file.",
635 "The RMSD at time t is calculated as the RMS",
636 "of the differences in distance between atom-pairs in the reference",
637 "structure and the structure at time t.[PAR]",
638 "[TT]g_rmsdist[tt] can also produce matrices of the rms distances, rms distances",
639 "scaled with the mean distance and the mean distances and matrices with",
640 "NMR averaged distances (1/r^3 and 1/r^6 averaging). Finally, lists",
641 "of atom pairs with 1/r^3 and 1/r^6 averaged distance below the",
642 "maximum distance ([TT]-max[tt], which will default to 0.6 in this case)",
643 "can be generated, by default averaging over equivalent hydrogens",
644 "(all triplets of hydrogens named *[123]). Additionally a list of",
645 "equivalent atoms can be supplied ([TT]-equiv[tt]), each line containing",
646 "a set of equivalent atoms specified as residue number and name and",
647 "atom name; e.g.:[PAR]",
648 "[TT]3 SER HB1 3 SER HB2[tt][PAR]",
649 "Residue and atom names must exactly match those in the structure",
650 "file, including case. Specifying non-sequential atoms is undefined."
654 int natom, i, j, teller, gi, gj;
666 atom_id *index, *noe_index;
668 real **d_r, **d, **dtot, **dtot2, **mean, **rms, **rmsc, *resnr;
669 real **dtot1_3 = NULL, **dtot1_6 = NULL;
670 real rmsnow, meanmax, rmsmax, rmscmax;
672 t_noe_gr *noe_gr = NULL;
676 gmx_bool bRMS, bScale, bMean, bNOE, bNMR3, bNMR6, bNMR;
678 static int nlevels = 40;
679 static real scalemax = -1.0;
680 static gmx_bool bSumH = TRUE;
681 static gmx_bool bPBC = TRUE;
685 { "-nlevels", FALSE, etINT, {&nlevels},
686 "Discretize RMS in this number of levels" },
687 { "-max", FALSE, etREAL, {&scalemax},
688 "Maximum level in matrices" },
689 { "-sumh", FALSE, etBOOL, {&bSumH},
690 "Average distance over equivalent hydrogens" },
691 { "-pbc", FALSE, etBOOL, {&bPBC},
692 "Use periodic boundary conditions when computing distances" }
695 { efTRX, "-f", NULL, ffREAD },
696 { efTPS, NULL, NULL, ffREAD },
697 { efNDX, NULL, NULL, ffOPTRD },
698 { efDAT, "-equiv", "equiv", ffOPTRD },
699 { efXVG, NULL, "distrmsd", ffWRITE },
700 { efXPM, "-rms", "rmsdist", ffOPTWR },
701 { efXPM, "-scl", "rmsscale", ffOPTWR },
702 { efXPM, "-mean", "rmsmean", ffOPTWR },
703 { efXPM, "-nmr3", "nmr3", ffOPTWR },
704 { efXPM, "-nmr6", "nmr6", ffOPTWR },
705 { efDAT, "-noe", "noe", ffOPTWR },
707 #define NFILE asize(fnm)
709 parse_common_args(&argc, argv, PCA_CAN_VIEW | PCA_CAN_TIME | PCA_BE_NICE,
710 NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv);
712 bRMS = opt2bSet("-rms", NFILE, fnm);
713 bScale = opt2bSet("-scl", NFILE, fnm);
714 bMean = opt2bSet("-mean", NFILE, fnm);
715 bNOE = opt2bSet("-noe", NFILE, fnm);
716 bNMR3 = opt2bSet("-nmr3", NFILE, fnm);
717 bNMR6 = opt2bSet("-nmr6", NFILE, fnm);
718 bNMR = bNMR3 || bNMR6 || bNOE;
724 if (bNOE && scalemax < 0)
727 fprintf(stderr, "WARNING: using -noe without -max "
728 "makes no sense, setting -max to %g\n\n", scalemax);
731 /* get topology and index */
732 read_tps_conf(ftp2fn(efTPS, NFILE, fnm), buf, &top, &ePBC, &x, NULL, box, FALSE);
738 atoms = &(top.atoms);
740 get_index(atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &isize, &index, &grpname);
742 /* initialize arrays */
748 snew(dtot1_3, isize);
749 snew(dtot1_6, isize);
756 for (i = 0; (i < isize); i++)
759 snew(dtot[i], isize);
760 snew(dtot2[i], isize);
763 snew(dtot1_3[i], isize);
764 snew(dtot1_6[i], isize);
766 snew(mean[i], isize);
768 snew(rmsc[i], isize);
774 calc_dist(isize, index, x, ePBC, box, d_r);
777 /*open output files*/
778 fp = xvgropen(ftp2fn(efXVG, NFILE, fnm), "RMS Deviation", "Time (ps)", "RMSD (nm)",
780 if (output_env_get_print_xvgr_codes(oenv))
782 fprintf(fp, "@ subtitle \"of distances between %s atoms\"\n", grpname);
786 natom = read_first_x(oenv, &status, ftp2fn(efTRX, NFILE, fnm), &t, &x, box);
790 calc_dist_tot(isize, index, x, ePBC, box, d, dtot, dtot2, bNMR, dtot1_3, dtot1_6);
792 rmsnow = rms_diff(isize, d, d_r);
793 fprintf(fp, "%g %g\n", t, rmsnow);
795 while (read_next_x(oenv, status, &t, natom, x, box));
796 fprintf(stderr, "\n");
800 teller = nframes_read(status);
804 calc_rms(isize, teller, dtot, dtot2, mean, &meanmax, rms, &rmsmax, rmsc, &rmscmax);
805 fprintf(stderr, "rmsmax = %g, rmscmax = %g\n", rmsmax, rmscmax);
809 calc_nmr(isize, teller, dtot1_3, dtot1_6, &max1_3, &max1_6);
823 /* make list of noe atom groups */
824 snew(noe_index, isize+1);
826 gnr = analyze_noe_equivalent(opt2fn_null("-equiv", NFILE, fnm),
827 atoms, isize, index, bSumH, noe_index, noe_gr);
828 fprintf(stdout, "Found %d non-equivalent atom-groups in %d atoms\n",
830 /* make half matrix of of noe-group distances from atom distances */
832 for (i = 0; i < gnr; i++)
836 calc_noe(isize, noe_index, dtot1_3, dtot1_6, gnr, noe);
839 rlo.r = 1.0, rlo.g = 1.0, rlo.b = 1.0;
840 rhi.r = 0.0, rhi.g = 0.0, rhi.b = 0.0;
844 write_xpm(opt2FILE("-rms", NFILE, fnm, "w"), 0,
845 "RMS of distance", "RMS (nm)", "Atom Index", "Atom Index",
846 isize, isize, resnr, resnr, rms, 0.0, rmsmax, rlo, rhi, &nlevels);
851 write_xpm(opt2FILE("-scl", NFILE, fnm, "w"), 0,
852 "Relative RMS", "RMS", "Atom Index", "Atom Index",
853 isize, isize, resnr, resnr, rmsc, 0.0, rmscmax, rlo, rhi, &nlevels);
858 write_xpm(opt2FILE("-mean", NFILE, fnm, "w"), 0,
859 "Mean Distance", "Distance (nm)", "Atom Index", "Atom Index",
860 isize, isize, resnr, resnr, mean, 0.0, meanmax, rlo, rhi, &nlevels);
865 write_xpm(opt2FILE("-nmr3", NFILE, fnm, "w"), 0, "1/r^3 averaged distances",
866 "Distance (nm)", "Atom Index", "Atom Index",
867 isize, isize, resnr, resnr, dtot1_3, 0.0, max1_3, rlo, rhi, &nlevels);
871 write_xpm(opt2FILE("-nmr6", NFILE, fnm, "w"), 0, "1/r^6 averaged distances",
872 "Distance (nm)", "Atom Index", "Atom Index",
873 isize, isize, resnr, resnr, dtot1_6, 0.0, max1_6, rlo, rhi, &nlevels);
878 write_noe(opt2FILE("-noe", NFILE, fnm, "w"), gnr, noe, noe_gr, scalemax);
881 do_view(oenv, ftp2fn(efXVG, NFILE, fnm), NULL);