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, 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.
52 #include "gromacs/fileio/futil.h"
53 #include "gromacs/commandline/pargs.h"
55 #include "gromacs/fileio/tpxio.h"
56 #include "gromacs/fileio/trxio.h"
58 #include "gromacs/fileio/xtcio.h"
62 static void periodic_dist(matrix box, rvec x[], int n, atom_id index[],
63 real *rmin, real *rmax, int *min_ind)
66 int sx, sy, sz, i, j, s;
67 real sqr_box, r2min, r2max, r2;
68 rvec shift[NSHIFT], d0, d;
70 sqr_box = sqr(min(norm(box[XX]), min(norm(box[YY]), norm(box[ZZ]))));
73 for (sz = -1; sz <= 1; sz++)
75 for (sy = -1; sy <= 1; sy++)
77 for (sx = -1; sx <= 1; sx++)
79 if (sx != 0 || sy != 0 || sz != 0)
81 for (i = 0; i < DIM; i++)
83 shift[s][i] = sx*box[XX][i]+sy*box[YY][i]+sz*box[ZZ][i];
94 for (i = 0; i < n; i++)
96 for (j = i+1; j < n; j++)
98 rvec_sub(x[index[i]], x[index[j]], d0);
104 for (s = 0; s < NSHIFT; s++)
106 rvec_add(d0, shift[s], d);
122 static void periodic_mindist_plot(const char *trxfn, const char *outfn,
123 t_topology *top, int ePBC,
124 int n, atom_id index[], gmx_bool bSplit,
125 const output_env_t oenv)
128 const char *leg[5] = { "min per.", "max int.", "box1", "box2", "box3" };
133 int natoms, ind_min[2] = {0, 0}, ind_mini = 0, ind_minj = 0;
134 real r, rmin, rmax, rmint, tmint;
136 gmx_rmpbc_t gpbc = NULL;
138 natoms = read_first_x(oenv, &status, trxfn, &t, &x, box);
140 check_index(NULL, n, index, NULL, natoms);
142 out = xvgropen(outfn, "Minimum distance to periodic image",
143 output_env_get_time_label(oenv), "Distance (nm)", oenv);
144 if (output_env_get_print_xvgr_codes(oenv))
146 fprintf(out, "@ subtitle \"and maximum internal distance\"\n");
148 xvgr_legend(out, 5, leg, oenv);
155 gpbc = gmx_rmpbc_init(&top->idef, ePBC, natoms);
163 gmx_rmpbc(gpbc, natoms, box, x);
166 periodic_dist(box, x, n, index, &rmin, &rmax, ind_min);
171 ind_mini = ind_min[0];
172 ind_minj = ind_min[1];
174 if (bSplit && !bFirst && abs(t/output_env_get_time_factor(oenv)) < 1e-5)
178 fprintf(out, "\t%g\t%6.3f %6.3f %6.3f %6.3f %6.3f\n",
179 output_env_conv_time(oenv, t), rmin, rmax, norm(box[0]), norm(box[1]), norm(box[2]));
182 while (read_next_x(oenv, status, &t, x, box));
186 gmx_rmpbc_done(gpbc);
192 "\nThe shortest periodic distance is %g (nm) at time %g (%s),\n"
193 "between atoms %d and %d\n",
194 rmint, output_env_conv_time(oenv, tmint), output_env_get_time_unit(oenv),
195 index[ind_mini]+1, index[ind_minj]+1);
198 static void calc_dist(real rcut, gmx_bool bPBC, int ePBC, matrix box, rvec x[],
199 int nx1, int nx2, atom_id index1[], atom_id index2[],
201 real *rmin, real *rmax, int *nmin, int *nmax,
202 int *ixmin, int *jxmin, int *ixmax, int *jxmax)
204 int i, j, i0 = 0, j1;
208 real r2, rmin2, rmax2, rcut2;
221 /* Must init pbc every step because of pressure coupling */
224 set_pbc(&pbc, ePBC, box);
241 for (j = 0; (j < j1); j++)
250 for (i = i0; (i < nx1); i++)
257 pbc_dx(&pbc, x[ix], x[jx], dx);
261 rvec_sub(x[ix], x[jx], dx);
307 void dist_plot(const char *fn, const char *afile, const char *dfile,
308 const char *nfile, const char *rfile, const char *xfile,
309 real rcut, gmx_bool bMat, t_atoms *atoms,
310 int ng, atom_id *index[], int gnx[], char *grpn[], gmx_bool bSplit,
311 gmx_bool bMin, int nres, atom_id *residue, gmx_bool bPBC, int ePBC,
312 gmx_bool bGroup, gmx_bool bEachResEachTime, gmx_bool bPrintResName,
313 const output_env_t oenv)
315 FILE *atm, *dist, *num;
319 real t, dmin, dmax, **mindres = NULL, **maxdres = NULL;
322 int i = -1, j, k, natoms;
323 int min1, min2, max1, max2, min1r, min2r, max1r, max2r;
329 FILE *respertime = NULL;
331 if ((natoms = read_first_x(oenv, &status, fn, &t, &x0, box)) == 0)
333 gmx_fatal(FARGS, "Could not read coordinates from statusfile\n");
336 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
337 dist = xvgropen(dfile, buf, output_env_get_time_label(oenv), "Distance (nm)", oenv);
338 sprintf(buf, "Number of Contacts %s %g nm", bMin ? "<" : ">", rcut);
339 num = nfile ? xvgropen(nfile, buf, output_env_get_time_label(oenv), "Number", oenv) : NULL;
340 atm = afile ? ffopen(afile, "w") : NULL;
341 trxout = xfile ? open_trx(xfile, "w") : NULL;
348 sprintf(buf, "Internal in %s", grpn[0]);
349 leg[0] = strdup(buf);
350 xvgr_legend(dist, 0, (const char**)leg, oenv);
353 xvgr_legend(num, 0, (const char**)leg, oenv);
358 snew(leg, (ng*(ng-1))/2);
359 for (i = j = 0; (i < ng-1); i++)
361 for (k = i+1; (k < ng); k++, j++)
363 sprintf(buf, "%s-%s", grpn[i], grpn[k]);
364 leg[j] = strdup(buf);
367 xvgr_legend(dist, j, (const char**)leg, oenv);
370 xvgr_legend(num, j, (const char**)leg, oenv);
377 for (i = 0; (i < ng-1); i++)
379 sprintf(buf, "%s-%s", grpn[0], grpn[i+1]);
380 leg[i] = strdup(buf);
382 xvgr_legend(dist, ng-1, (const char**)leg, oenv);
385 xvgr_legend(num, ng-1, (const char**)leg, oenv);
389 if (bEachResEachTime)
391 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
392 respertime = xvgropen(rfile, buf, output_env_get_time_label(oenv), "Distance (nm)", oenv);
393 xvgr_legend(respertime, ng-1, (const char**)leg, oenv);
396 fprintf(respertime, "# ");
398 for (j = 0; j < nres; j++)
400 fprintf(respertime, "%s%d ", *(atoms->resinfo[atoms->atom[index[0][residue[j]]].resind].name), atoms->atom[index[0][residue[j]]].resind);
402 fprintf(respertime, "\n");
411 for (i = 1; i < ng; i++)
413 snew(mindres[i-1], nres);
414 snew(maxdres[i-1], nres);
415 for (j = 0; j < nres; j++)
417 mindres[i-1][j] = 1e6;
419 /* maxdres[*][*] is already 0 */
425 if (bSplit && !bFirst && abs(t/output_env_get_time_factor(oenv)) < 1e-5)
427 fprintf(dist, "&\n");
437 fprintf(dist, "%12e", output_env_conv_time(oenv, t));
440 fprintf(num, "%12e", output_env_conv_time(oenv, t));
447 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[0], gnx[0], index[0], index[0], bGroup,
448 &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
449 fprintf(dist, " %12e", bMin ? dmin : dmax);
452 fprintf(num, " %8d", bMin ? nmin : nmax);
457 for (i = 0; (i < ng-1); i++)
459 for (k = i+1; (k < ng); k++)
461 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[i], gnx[k], index[i], index[k],
462 bGroup, &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
463 fprintf(dist, " %12e", bMin ? dmin : dmax);
466 fprintf(num, " %8d", bMin ? nmin : nmax);
474 for (i = 1; (i < ng); i++)
476 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[0], gnx[i], index[0], index[i], bGroup,
477 &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
478 fprintf(dist, " %12e", bMin ? dmin : dmax);
481 fprintf(num, " %8d", bMin ? nmin : nmax);
485 for (j = 0; j < nres; j++)
487 calc_dist(rcut, bPBC, ePBC, box, x0, residue[j+1]-residue[j], gnx[i],
488 &(index[0][residue[j]]), index[i], bGroup,
489 &dmin, &dmax, &nmin, &nmax, &min1r, &min2r, &max1r, &max2r);
490 mindres[i-1][j] = min(mindres[i-1][j], dmin);
491 maxdres[i-1][j] = max(maxdres[i-1][j], dmax);
501 if ( (bMin ? min1 : max1) != -1)
505 fprintf(atm, "%12e %12d %12d\n",
506 output_env_conv_time(oenv, t), 1+(bMin ? min1 : max1),
507 1+(bMin ? min2 : max2));
513 oindex[0] = bMin ? min1 : max1;
514 oindex[1] = bMin ? min2 : max2;
515 write_trx(trxout, 2, oindex, atoms, i, t, box, x0, NULL, NULL);
518 /*dmin should be minimum distance for residue and group*/
519 if (bEachResEachTime)
521 fprintf(respertime, "%12e", t);
522 for (i = 1; i < ng; i++)
524 for (j = 0; j < nres; j++)
526 fprintf(respertime, " %7g", bMin ? mindres[i-1][j] : maxdres[i-1][j]);
527 /*reset distances for next time point*/
528 mindres[i-1][j] = 1e6;
532 fprintf(respertime, "\n");
535 while (read_next_x(oenv, status, &t, x0, box));
552 if (nres && !bEachResEachTime)
556 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
557 res = xvgropen(rfile, buf, "Residue (#)", "Distance (nm)", oenv);
558 xvgr_legend(res, ng-1, (const char**)leg, oenv);
559 for (j = 0; j < nres; j++)
561 fprintf(res, "%4d", j+1);
562 for (i = 1; i < ng; i++)
564 fprintf(res, " %7g", bMin ? mindres[i-1][j] : maxdres[i-1][j]);
573 int find_residues(t_atoms *atoms, int n, atom_id index[], atom_id **resindex)
576 int nres = 0, resnr, presnr;
579 /* build index of first atom numbers for each residue */
581 snew(residx, atoms->nres+1);
582 for (i = 0; i < n; i++)
584 resnr = atoms->atom[index[i]].resind;
594 printf("Found %d residues out of %d (%d/%d atoms)\n",
595 nres, atoms->nres, atoms->nr, n);
597 srenew(residx, nres+1);
598 /* mark end of last residue */
604 void dump_res(FILE *out, int nres, atom_id *resindex, atom_id index[])
608 for (i = 0; i < nres-1; i++)
610 fprintf(out, "Res %d (%d):", i, resindex[i+1]-resindex[i]);
611 for (j = resindex[i]; j < resindex[i+1]; j++)
613 fprintf(out, " %d(%d)", j, index[j]);
619 int gmx_mindist(int argc, char *argv[])
621 const char *desc[] = {
622 "[THISMODULE] computes the distance between one group and a number of",
623 "other groups. Both the minimum distance",
624 "(between any pair of atoms from the respective groups)",
625 "and the number of contacts within a given",
626 "distance are written to two separate output files.",
627 "With the [TT]-group[tt] option a contact of an atom in another group",
628 "with multiple atoms in the first group is counted as one contact",
629 "instead of as multiple contacts.",
630 "With [TT]-or[tt], minimum distances to each residue in the first",
631 "group are determined and plotted as a function of residue number.[PAR]",
632 "With option [TT]-pi[tt] the minimum distance of a group to its",
633 "periodic image is plotted. This is useful for checking if a protein",
634 "has seen its periodic image during a simulation. Only one shift in",
635 "each direction is considered, giving a total of 26 shifts.",
636 "It also plots the maximum distance within the group and the lengths",
637 "of the three box vectors.[PAR]",
638 "Also [gmx-distance] calculates distances."
640 const char *bugs[] = {
641 "The [TT]-pi[tt] option is very slow."
644 static gmx_bool bMat = FALSE, bPI = FALSE, bSplit = FALSE, bMax = FALSE, bPBC = TRUE;
645 static gmx_bool bGroup = FALSE;
646 static real rcutoff = 0.6;
648 static gmx_bool bEachResEachTime = FALSE, bPrintResName = FALSE;
650 { "-matrix", FALSE, etBOOL, {&bMat},
651 "Calculate half a matrix of group-group distances" },
652 { "-max", FALSE, etBOOL, {&bMax},
653 "Calculate *maximum* distance instead of minimum" },
654 { "-d", FALSE, etREAL, {&rcutoff},
655 "Distance for contacts" },
656 { "-group", FALSE, etBOOL, {&bGroup},
657 "Count contacts with multiple atoms in the first group as one" },
658 { "-pi", FALSE, etBOOL, {&bPI},
659 "Calculate minimum distance with periodic images" },
660 { "-split", FALSE, etBOOL, {&bSplit},
661 "Split graph where time is zero" },
662 { "-ng", FALSE, etINT, {&ng},
663 "Number of secondary groups to compute distance to a central group" },
664 { "-pbc", FALSE, etBOOL, {&bPBC},
665 "Take periodic boundary conditions into account" },
666 { "-respertime", FALSE, etBOOL, {&bEachResEachTime},
667 "When writing per-residue distances, write distance for each time point" },
668 { "-printresname", FALSE, etBOOL, {&bPrintResName},
669 "Write residue names" }
672 t_topology *top = NULL;
678 gmx_bool bTop = FALSE;
682 const char *trxfnm, *tpsfnm, *ndxfnm, *distfnm, *numfnm, *atmfnm, *oxfnm, *resfnm;
685 atom_id **index, *residues = NULL;
687 { efTRX, "-f", NULL, ffREAD },
688 { efTPS, NULL, NULL, ffOPTRD },
689 { efNDX, NULL, NULL, ffOPTRD },
690 { efXVG, "-od", "mindist", ffWRITE },
691 { efXVG, "-on", "numcont", ffOPTWR },
692 { efOUT, "-o", "atm-pair", ffOPTWR },
693 { efTRO, "-ox", "mindist", ffOPTWR },
694 { efXVG, "-or", "mindistres", ffOPTWR }
696 #define NFILE asize(fnm)
698 if (!parse_common_args(&argc, argv,
699 PCA_CAN_VIEW | PCA_CAN_TIME | PCA_TIME_UNIT | PCA_BE_NICE,
700 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, 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");