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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * 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.
63 static void periodic_dist(matrix box, rvec x[], int n, atom_id index[],
64 real *rmin, real *rmax, int *min_ind)
67 int sx, sy, sz, i, j, s;
68 real sqr_box, r2min, r2max, r2;
69 rvec shift[NSHIFT], d0, d;
71 sqr_box = sqr(min(norm(box[XX]), min(norm(box[YY]), norm(box[ZZ]))));
74 for (sz = -1; sz <= 1; sz++)
76 for (sy = -1; sy <= 1; sy++)
78 for (sx = -1; sx <= 1; sx++)
80 if (sx != 0 || sy != 0 || sz != 0)
82 for (i = 0; i < DIM; i++)
84 shift[s][i] = sx*box[XX][i]+sy*box[YY][i]+sz*box[ZZ][i];
95 for (i = 0; i < n; i++)
97 for (j = i+1; j < n; j++)
99 rvec_sub(x[index[i]], x[index[j]], d0);
105 for (s = 0; s < NSHIFT; s++)
107 rvec_add(d0, shift[s], d);
123 static void periodic_mindist_plot(const char *trxfn, const char *outfn,
124 t_topology *top, int ePBC,
125 int n, atom_id index[], gmx_bool bSplit,
126 const output_env_t oenv)
129 const char *leg[5] = { "min per.", "max int.", "box1", "box2", "box3" };
134 int natoms, ind_min[2] = {0, 0}, ind_mini = 0, ind_minj = 0;
135 real r, rmin, rmax, rmint, tmint;
137 gmx_rmpbc_t gpbc = NULL;
139 natoms = read_first_x(oenv, &status, trxfn, &t, &x, box);
141 check_index(NULL, n, index, NULL, natoms);
143 out = xvgropen(outfn, "Minimum distance to periodic image",
144 output_env_get_time_label(oenv), "Distance (nm)", oenv);
145 if (output_env_get_print_xvgr_codes(oenv))
147 fprintf(out, "@ subtitle \"and maximum internal distance\"\n");
149 xvgr_legend(out, 5, leg, oenv);
156 gpbc = gmx_rmpbc_init(&top->idef, ePBC, natoms, box);
164 gmx_rmpbc(gpbc, natoms, box, x);
167 periodic_dist(box, x, n, index, &rmin, &rmax, ind_min);
172 ind_mini = ind_min[0];
173 ind_minj = ind_min[1];
175 if (bSplit && !bFirst && fabs(t/output_env_get_time_factor(oenv)) < 1e-5)
177 fprintf(out, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
179 fprintf(out, "\t%g\t%6.3f %6.3f %6.3f %6.3f %6.3f\n",
180 output_env_conv_time(oenv, t), rmin, rmax, norm(box[0]), norm(box[1]), norm(box[2]));
183 while (read_next_x(oenv, status, &t, natoms, x, box));
187 gmx_rmpbc_done(gpbc);
193 "\nThe shortest periodic distance is %g (nm) at time %g (%s),\n"
194 "between atoms %d and %d\n",
195 rmint, output_env_conv_time(oenv, tmint), output_env_get_time_unit(oenv),
196 index[ind_mini]+1, index[ind_minj]+1);
199 static void calc_dist(real rcut, gmx_bool bPBC, int ePBC, matrix box, rvec x[],
200 int nx1, int nx2, atom_id index1[], atom_id index2[],
202 real *rmin, real *rmax, int *nmin, int *nmax,
203 int *ixmin, int *jxmin, int *ixmax, int *jxmax)
205 int i, j, i0 = 0, j1;
209 real r2, rmin2, rmax2, rcut2;
222 /* Must init pbc every step because of pressure coupling */
225 set_pbc(&pbc, ePBC, box);
242 for (j = 0; (j < j1); j++)
251 for (i = i0; (i < nx1); i++)
258 pbc_dx(&pbc, x[ix], x[jx], dx);
262 rvec_sub(x[ix], x[jx], dx);
308 void dist_plot(const char *fn, const char *afile, const char *dfile,
309 const char *nfile, const char *rfile, const char *xfile,
310 real rcut, gmx_bool bMat, t_atoms *atoms,
311 int ng, atom_id *index[], int gnx[], char *grpn[], gmx_bool bSplit,
312 gmx_bool bMin, int nres, atom_id *residue, gmx_bool bPBC, int ePBC,
313 gmx_bool bGroup, gmx_bool bEachResEachTime, gmx_bool bPrintResName,
314 const output_env_t oenv)
316 FILE *atm, *dist, *num;
320 real t, dmin, dmax, **mindres = NULL, **maxdres = NULL;
323 int i = -1, j, k, natoms;
324 int min1, min2, max1, max2, min1r, min2r, max1r, max2r;
330 FILE *respertime = NULL;
332 if ((natoms = read_first_x(oenv, &status, fn, &t, &x0, box)) == 0)
334 gmx_fatal(FARGS, "Could not read coordinates from statusfile\n");
337 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
338 dist = xvgropen(dfile, buf, output_env_get_time_label(oenv), "Distance (nm)", oenv);
339 sprintf(buf, "Number of Contacts %s %g nm", bMin ? "<" : ">", rcut);
340 num = nfile ? xvgropen(nfile, buf, output_env_get_time_label(oenv), "Number", oenv) : NULL;
341 atm = afile ? ffopen(afile, "w") : NULL;
342 trxout = xfile ? open_trx(xfile, "w") : NULL;
349 sprintf(buf, "Internal in %s", grpn[0]);
350 leg[0] = strdup(buf);
351 xvgr_legend(dist, 0, (const char**)leg, oenv);
354 xvgr_legend(num, 0, (const char**)leg, oenv);
359 snew(leg, (ng*(ng-1))/2);
360 for (i = j = 0; (i < ng-1); i++)
362 for (k = i+1; (k < ng); k++, j++)
364 sprintf(buf, "%s-%s", grpn[i], grpn[k]);
365 leg[j] = strdup(buf);
368 xvgr_legend(dist, j, (const char**)leg, oenv);
371 xvgr_legend(num, j, (const char**)leg, oenv);
378 for (i = 0; (i < ng-1); i++)
380 sprintf(buf, "%s-%s", grpn[0], grpn[i+1]);
381 leg[i] = strdup(buf);
383 xvgr_legend(dist, ng-1, (const char**)leg, oenv);
386 xvgr_legend(num, ng-1, (const char**)leg, oenv);
390 if (bEachResEachTime)
392 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
393 respertime = xvgropen(rfile, buf, output_env_get_time_label(oenv), "Distance (nm)", oenv);
394 xvgr_legend(respertime, ng-1, (const char**)leg, oenv);
397 fprintf(respertime, "# ");
399 for (j = 0; j < nres; j++)
401 fprintf(respertime, "%s%d ", *(atoms->resinfo[atoms->atom[index[0][residue[j]]].resind].name), atoms->atom[index[0][residue[j]]].resind);
403 fprintf(respertime, "\n");
412 for (i = 1; i < ng; i++)
414 snew(mindres[i-1], nres);
415 snew(maxdres[i-1], nres);
416 for (j = 0; j < nres; j++)
418 mindres[i-1][j] = 1e6;
420 /* maxdres[*][*] is already 0 */
426 if (bSplit && !bFirst && fabs(t/output_env_get_time_factor(oenv)) < 1e-5)
428 fprintf(dist, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
431 fprintf(num, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
435 fprintf(atm, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
438 fprintf(dist, "%12e", output_env_conv_time(oenv, t));
441 fprintf(num, "%12e", output_env_conv_time(oenv, t));
448 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[0], gnx[0], index[0], index[0], bGroup,
449 &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
450 fprintf(dist, " %12e", bMin ? dmin : dmax);
453 fprintf(num, " %8d", bMin ? nmin : nmax);
458 for (i = 0; (i < ng-1); i++)
460 for (k = i+1; (k < ng); k++)
462 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[i], gnx[k], index[i], index[k],
463 bGroup, &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
464 fprintf(dist, " %12e", bMin ? dmin : dmax);
467 fprintf(num, " %8d", bMin ? nmin : nmax);
475 for (i = 1; (i < ng); i++)
477 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[0], gnx[i], index[0], index[i], bGroup,
478 &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
479 fprintf(dist, " %12e", bMin ? dmin : dmax);
482 fprintf(num, " %8d", bMin ? nmin : nmax);
486 for (j = 0; j < nres; j++)
488 calc_dist(rcut, bPBC, ePBC, box, x0, residue[j+1]-residue[j], gnx[i],
489 &(index[0][residue[j]]), index[i], bGroup,
490 &dmin, &dmax, &nmin, &nmax, &min1r, &min2r, &max1r, &max2r);
491 mindres[i-1][j] = min(mindres[i-1][j], dmin);
492 maxdres[i-1][j] = max(maxdres[i-1][j], dmax);
502 if ( (bMin ? min1 : max1) != -1)
506 fprintf(atm, "%12e %12d %12d\n",
507 output_env_conv_time(oenv, t), 1+(bMin ? min1 : max1),
508 1+(bMin ? min2 : max2));
514 oindex[0] = bMin ? min1 : max1;
515 oindex[1] = bMin ? min2 : max2;
516 write_trx(trxout, 2, oindex, atoms, i, t, box, x0, NULL, NULL);
519 /*dmin should be minimum distance for residue and group*/
520 if (bEachResEachTime)
522 fprintf(respertime, "%12e", t);
523 for (i = 1; i < ng; i++)
525 for (j = 0; j < nres; j++)
527 fprintf(respertime, " %7g", bMin ? mindres[i-1][j] : maxdres[i-1][j]);
528 /*reset distances for next time point*/
529 mindres[i-1][j] = 1e6;
533 fprintf(respertime, "\n");
536 while (read_next_x(oenv, status, &t, natoms, x0, box));
553 if (nres && !bEachResEachTime)
557 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
558 res = xvgropen(rfile, buf, "Residue (#)", "Distance (nm)", oenv);
559 xvgr_legend(res, ng-1, (const char**)leg, oenv);
560 for (j = 0; j < nres; j++)
562 fprintf(res, "%4d", j+1);
563 for (i = 1; i < ng; i++)
565 fprintf(res, " %7g", bMin ? mindres[i-1][j] : maxdres[i-1][j]);
574 int find_residues(t_atoms *atoms, int n, atom_id index[], atom_id **resindex)
577 int nres = 0, resnr, presnr;
580 /* build index of first atom numbers for each residue */
582 snew(residx, atoms->nres+1);
583 for (i = 0; i < n; i++)
585 resnr = atoms->atom[index[i]].resind;
595 printf("Found %d residues out of %d (%d/%d atoms)\n",
596 nres, atoms->nres, atoms->nr, n);
598 srenew(residx, nres+1);
599 /* mark end of last residue */
605 void dump_res(FILE *out, int nres, atom_id *resindex, int n, atom_id index[])
609 for (i = 0; i < nres-1; i++)
611 fprintf(out, "Res %d (%d):", i, resindex[i+1]-resindex[i]);
612 for (j = resindex[i]; j < resindex[i+1]; j++)
614 fprintf(out, " %d(%d)", j, index[j]);
620 int gmx_mindist(int argc, char *argv[])
622 const char *desc[] = {
623 "[TT]g_mindist[tt] computes the distance between one group and a number of",
624 "other groups. Both the minimum distance",
625 "(between any pair of atoms from the respective groups)",
626 "and the number of contacts within a given",
627 "distance are written to two separate output files.",
628 "With the [TT]-group[tt] option a contact of an atom in another group",
629 "with multiple atoms in the first group is counted as one contact",
630 "instead of as multiple contacts.",
631 "With [TT]-or[tt], minimum distances to each residue in the first",
632 "group are determined and plotted as a function of residue number.[PAR]",
633 "With option [TT]-pi[tt] the minimum distance of a group to its",
634 "periodic image is plotted. This is useful for checking if a protein",
635 "has seen its periodic image during a simulation. Only one shift in",
636 "each direction is considered, giving a total of 26 shifts.",
637 "It also plots the maximum distance within the group and the lengths",
638 "of the three box vectors.[PAR]",
639 "Other programs that calculate distances are [TT]g_dist[tt]",
640 "and [TT]g_bond[tt]."
642 const char *bugs[] = {
643 "The [TT]-pi[tt] option is very slow."
646 static gmx_bool bMat = FALSE, bPI = FALSE, bSplit = FALSE, bMax = FALSE, bPBC = TRUE;
647 static gmx_bool bGroup = FALSE;
648 static real rcutoff = 0.6;
650 static gmx_bool bEachResEachTime = FALSE, bPrintResName = FALSE;
652 { "-matrix", FALSE, etBOOL, {&bMat},
653 "Calculate half a matrix of group-group distances" },
654 { "-max", FALSE, etBOOL, {&bMax},
655 "Calculate *maximum* distance instead of minimum" },
656 { "-d", FALSE, etREAL, {&rcutoff},
657 "Distance for contacts" },
658 { "-group", FALSE, etBOOL, {&bGroup},
659 "Count contacts with multiple atoms in the first group as one" },
660 { "-pi", FALSE, etBOOL, {&bPI},
661 "Calculate minimum distance with periodic images" },
662 { "-split", FALSE, etBOOL, {&bSplit},
663 "Split graph where time is zero" },
664 { "-ng", FALSE, etINT, {&ng},
665 "Number of secondary groups to compute distance to a central group" },
666 { "-pbc", FALSE, etBOOL, {&bPBC},
667 "Take periodic boundary conditions into account" },
668 { "-respertime", FALSE, etBOOL, {&bEachResEachTime},
669 "When writing per-residue distances, write distance for each time point" },
670 { "-printresname", FALSE, etBOOL, {&bPrintResName},
671 "Write residue names" }
674 t_topology *top = NULL;
680 gmx_bool bTop = FALSE;
684 const char *trxfnm, *tpsfnm, *ndxfnm, *distfnm, *numfnm, *atmfnm, *oxfnm, *resfnm;
687 atom_id **index, *residues = NULL;
689 { efTRX, "-f", NULL, ffREAD },
690 { efTPS, NULL, NULL, ffOPTRD },
691 { efNDX, NULL, NULL, ffOPTRD },
692 { efXVG, "-od", "mindist", ffWRITE },
693 { efXVG, "-on", "numcont", ffOPTWR },
694 { efOUT, "-o", "atm-pair", ffOPTWR },
695 { efTRO, "-ox", "mindist", ffOPTWR },
696 { efXVG, "-or", "mindistres", ffOPTWR }
698 #define NFILE asize(fnm)
700 CopyRight(stderr, argv[0]);
701 parse_common_args(&argc, argv,
702 PCA_CAN_VIEW | PCA_CAN_TIME | PCA_TIME_UNIT | PCA_BE_NICE,
703 NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv);
705 trxfnm = ftp2fn(efTRX, NFILE, fnm);
706 ndxfnm = ftp2fn_null(efNDX, NFILE, fnm);
707 distfnm = opt2fn("-od", NFILE, fnm);
708 numfnm = opt2fn_null("-on", NFILE, fnm);
709 atmfnm = ftp2fn_null(efOUT, NFILE, fnm);
710 oxfnm = opt2fn_null("-ox", NFILE, fnm);
711 resfnm = opt2fn_null("-or", NFILE, fnm);
712 if (bPI || resfnm != NULL)
714 /* We need a tps file */
715 tpsfnm = ftp2fn(efTPS, NFILE, fnm);
719 tpsfnm = ftp2fn_null(efTPS, NFILE, fnm);
722 if (!tpsfnm && !ndxfnm)
724 gmx_fatal(FARGS, "You have to specify either the index file or a tpr file");
730 fprintf(stderr, "Choose a group for distance calculation\n");
741 if (tpsfnm || resfnm || !ndxfnm)
744 bTop = read_tps_conf(tpsfnm, title, top, &ePBC, &x, NULL, box, FALSE);
747 printf("\nWARNING: Without a run input file a trajectory with broken molecules will not give the correct periodic image distance\n\n");
750 get_index(top ? &(top->atoms) : NULL, ndxfnm, ng, gnx, index, grpname);
752 if (bMat && (ng == 1))
755 printf("Special case: making distance matrix between all atoms in group %s\n",
760 for (i = 1; (i < ng); i++)
763 grpname[i] = grpname[0];
765 index[i][0] = index[0][i];
772 nres = find_residues(top ? &(top->atoms) : NULL,
773 gnx[0], index[0], &residues);
776 dump_res(debug, nres, residues, gnx[0], index[0]);
782 periodic_mindist_plot(trxfnm, distfnm, top, ePBC, gnx[0], index[0], bSplit, oenv);
786 dist_plot(trxfnm, atmfnm, distfnm, numfnm, resfnm, oxfnm,
787 rcutoff, bMat, top ? &(top->atoms) : NULL,
788 ng, index, gnx, grpname, bSplit, !bMax, nres, residues, bPBC, ePBC,
789 bGroup, bEachResEachTime, bPrintResName, oenv);
792 do_view(oenv, distfnm, "-nxy");
795 do_view(oenv, numfnm, "-nxy");