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,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source 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.
46 #include "gromacs/commandline/pargs.h"
47 #include "gromacs/commandline/viewit.h"
48 #include "gromacs/fileio/confio.h"
49 #include "gromacs/fileio/trxio.h"
50 #include "gromacs/fileio/xvgr.h"
51 #include "gromacs/gmxana/gmx_ana.h"
52 #include "gromacs/math/functions.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/mdtypes/md_enums.h"
55 #include "gromacs/pbcutil/pbc.h"
56 #include "gromacs/pbcutil/rmpbc.h"
57 #include "gromacs/topology/index.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/utility/arraysize.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/futil.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/smalloc.h"
67 static void periodic_dist(int ePBC, matrix box, rvec x[], int n, const int index[], real* rmin, real* rmax, int* min_ind)
70 int nsz, nshift, sx, sy, sz, i, j, s;
71 real sqr_box, r2min, r2max, r2;
72 rvec shift[NSHIFT_MAX], d0, d;
74 sqr_box = std::min(norm2(box[XX]), norm2(box[YY]));
77 sqr_box = std::min(sqr_box, norm2(box[ZZ]));
80 else if (ePBC == epbcXY)
86 gmx_fatal(FARGS, "pbc = %s is not supported by g_mindist", epbc_names[ePBC]);
90 for (sz = -nsz; sz <= nsz; sz++)
92 for (sy = -1; sy <= 1; sy++)
94 for (sx = -1; sx <= 1; sx++)
96 if (sx != 0 || sy != 0 || sz != 0)
98 for (i = 0; i < DIM; i++)
100 shift[nshift][i] = sx * box[XX][i] + sy * box[YY][i] + sz * box[ZZ][i];
111 for (i = 0; i < n; i++)
113 for (j = i + 1; j < n; j++)
115 rvec_sub(x[index[i]], x[index[j]], d0);
121 for (s = 0; s < nshift; s++)
123 rvec_add(d0, shift[s], d);
135 *rmin = std::sqrt(r2min);
136 *rmax = std::sqrt(r2max);
139 static void periodic_mindist_plot(const char* trxfn,
141 const t_topology* top,
146 const gmx_output_env_t* oenv)
149 const char* leg[5] = { "min per.", "max int.", "box1", "box2", "box3" };
154 int natoms, ind_min[2] = { 0, 0 }, ind_mini = 0, ind_minj = 0;
155 real rmin, rmax, rmint, tmint;
157 gmx_rmpbc_t gpbc = nullptr;
159 natoms = read_first_x(oenv, &status, trxfn, &t, &x, box);
161 check_index(nullptr, n, index, nullptr, natoms);
163 out = xvgropen(outfn, "Minimum distance to periodic image", output_env_get_time_label(oenv),
164 "Distance (nm)", oenv);
165 if (output_env_get_print_xvgr_codes(oenv))
167 fprintf(out, "@ subtitle \"and maximum internal distance\"\n");
169 xvgr_legend(out, 5, leg, oenv);
176 gpbc = gmx_rmpbc_init(&top->idef, ePBC, natoms);
184 gmx_rmpbc(gpbc, natoms, box, x);
187 periodic_dist(ePBC, box, x, n, index, &rmin, &rmax, ind_min);
192 ind_mini = ind_min[0];
193 ind_minj = ind_min[1];
195 if (bSplit && !bFirst && std::abs(t / output_env_get_time_factor(oenv)) < 1e-5)
197 fprintf(out, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
199 fprintf(out, "\t%g\t%6.3f %6.3f %6.3f %6.3f %6.3f\n", output_env_conv_time(oenv, t), rmin,
200 rmax, norm(box[0]), norm(box[1]), norm(box[2]));
202 } while (read_next_x(oenv, status, &t, x, box));
206 gmx_rmpbc_done(gpbc);
212 "\nThe shortest periodic distance is %g (nm) at time %g (%s),\n"
213 "between atoms %d and %d\n",
214 rmint, output_env_conv_time(oenv, tmint), output_env_get_time_unit(oenv).c_str(),
215 index[ind_mini] + 1, index[ind_minj] + 1);
218 static void calc_dist(real rcut,
237 int i, j, i0 = 0, j1;
241 real r2, rmin2, rmax2, rcut2;
252 rcut2 = gmx::square(rcut);
254 /* Must init pbc every step because of pressure coupling */
257 set_pbc(&pbc, ePBC, box);
270 GMX_RELEASE_ASSERT(index1 != nullptr, "Need a valid index for plotting distances");
275 for (j = 0; (j < j1); j++)
278 if (index2 == nullptr)
284 for (i = i0; (i < nx1); i++)
291 pbc_dx(&pbc, x[ix], x[jx], dx);
295 rvec_sub(x[ix], x[jx], dx);
337 *rmin = std::sqrt(rmin2);
338 *rmax = std::sqrt(rmax2);
341 static void dist_plot(const char* fn,
349 const t_atoms* atoms,
361 gmx_bool bEachResEachTime,
362 gmx_bool bPrintResName,
363 const gmx_output_env_t* oenv)
365 FILE * atm, *dist, *num;
369 real t, dmin, dmax, **mindres = nullptr, **maxdres = nullptr;
373 int min2, max2, min1r, min2r, max1r, max2r;
380 FILE* respertime = nullptr;
382 if (read_first_x(oenv, &status, fn, &t, &x0, box) == 0)
384 gmx_fatal(FARGS, "Could not read coordinates from statusfile\n");
387 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
388 dist = xvgropen(dfile, buf, output_env_get_time_label(oenv), "Distance (nm)", oenv);
389 sprintf(buf, "Number of Contacts %s %g nm", bMin ? "<" : ">", rcut);
390 num = nfile ? xvgropen(nfile, buf, output_env_get_time_label(oenv), "Number", oenv) : nullptr;
391 atm = afile ? gmx_ffopen(afile, "w") : nullptr;
392 trxout = xfile ? open_trx(xfile, "w") : nullptr;
399 sprintf(buf, "Internal in %s", grpn[0]);
400 leg[0] = gmx_strdup(buf);
401 xvgr_legend(dist, 0, leg, oenv);
404 xvgr_legend(num, 0, leg, oenv);
409 GMX_RELEASE_ASSERT(ng > 1, "Must have more than one group with bMat");
410 snew(leg, (ng * (ng - 1)) / 2);
411 for (i = j = 0; (i < ng - 1); i++)
413 for (k = i + 1; (k < ng); k++, j++)
415 sprintf(buf, "%s-%s", grpn[i], grpn[k]);
416 leg[j] = gmx_strdup(buf);
419 xvgr_legend(dist, j, leg, oenv);
422 xvgr_legend(num, j, leg, oenv);
429 for (i = 0; (i < ng - 1); i++)
431 sprintf(buf, "%s-%s", grpn[0], grpn[i + 1]);
432 leg[i] = gmx_strdup(buf);
434 xvgr_legend(dist, ng - 1, leg, oenv);
437 xvgr_legend(num, ng - 1, leg, oenv);
441 if (bEachResEachTime)
443 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
444 respertime = xvgropen(rfile, buf, output_env_get_time_label(oenv), "Distance (nm)", oenv);
445 xvgr_legend(respertime, ng - 1, leg, oenv);
446 if (bPrintResName && output_env_get_print_xvgr_codes(oenv))
448 fprintf(respertime, "# ");
450 for (j = 0; j < nres; j++)
452 fprintf(respertime, "%s%d ",
453 *(atoms->resinfo[atoms->atom[index[0][residue[j]]].resind].name),
454 atoms->atom[index[0][residue[j]]].resind);
456 fprintf(respertime, "\n");
462 snew(mindres, ng - 1);
463 snew(maxdres, ng - 1);
464 for (i = 1; i < ng; i++)
466 snew(mindres[i - 1], nres);
467 snew(maxdres[i - 1], nres);
468 for (j = 0; j < nres; j++)
470 mindres[i - 1][j] = 1e6;
472 /* maxdres[*][*] is already 0 */
478 if (bSplit && !bFirst && std::abs(t / output_env_get_time_factor(oenv)) < 1e-5)
480 fprintf(dist, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
483 fprintf(num, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
487 fprintf(atm, "%s\n", output_env_get_print_xvgr_codes(oenv) ? "&" : "");
490 fprintf(dist, "%12e", output_env_conv_time(oenv, t));
493 fprintf(num, "%12e", output_env_conv_time(oenv, t));
500 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[0], gnx[0], index[0], index[0], bGroup,
501 &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
502 fprintf(dist, " %12e", bMin ? dmin : dmax);
505 fprintf(num, " %8d", bMin ? nmin : nmax);
510 for (i = 0; (i < ng - 1); i++)
512 for (k = i + 1; (k < ng); k++)
514 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[i], gnx[k], index[i], index[k],
515 bGroup, &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
516 fprintf(dist, " %12e", bMin ? dmin : dmax);
519 fprintf(num, " %8d", bMin ? nmin : nmax);
527 GMX_RELEASE_ASSERT(ng > 1, "Must have more than one group when not using -matrix");
528 for (i = 1; (i < ng); i++)
530 calc_dist(rcut, bPBC, ePBC, box, x0, gnx[0], gnx[i], index[0], index[i], bGroup,
531 &dmin, &dmax, &nmin, &nmax, &min1, &min2, &max1, &max2);
532 fprintf(dist, " %12e", bMin ? dmin : dmax);
535 fprintf(num, " %8d", bMin ? nmin : nmax);
539 for (j = 0; j < nres; j++)
541 calc_dist(rcut, bPBC, ePBC, box, x0, residue[j + 1] - residue[j], gnx[i],
542 &(index[0][residue[j]]), index[i], bGroup, &dmin, &dmax, &nmin,
543 &nmax, &min1r, &min2r, &max1r, &max2r);
544 mindres[i - 1][j] = std::min(mindres[i - 1][j], dmin);
545 maxdres[i - 1][j] = std::max(maxdres[i - 1][j], dmax);
555 if ((bMin ? min1 : max1) != -1)
559 fprintf(atm, "%12e %12d %12d\n", output_env_conv_time(oenv, t),
560 1 + (bMin ? min1 : max1), 1 + (bMin ? min2 : max2));
566 oindex[0] = bMin ? min1 : max1;
567 oindex[1] = bMin ? min2 : max2;
568 write_trx(trxout, 2, oindex, atoms, i, t, box, x0, nullptr, nullptr);
571 /*dmin should be minimum distance for residue and group*/
572 if (bEachResEachTime)
574 fprintf(respertime, "%12e", t);
575 for (i = 1; i < ng; i++)
577 for (j = 0; j < nres; j++)
579 fprintf(respertime, " %7g", bMin ? mindres[i - 1][j] : maxdres[i - 1][j]);
580 /*reset distances for next time point*/
581 mindres[i - 1][j] = 1e6;
582 maxdres[i - 1][j] = 0;
585 fprintf(respertime, "\n");
587 } while (read_next_x(oenv, status, &t, x0, box));
605 xvgrclose(respertime);
608 if (nres && !bEachResEachTime)
612 sprintf(buf, "%simum Distance", bMin ? "Min" : "Max");
613 res = xvgropen(rfile, buf, "Residue (#)", "Distance (nm)", oenv);
614 xvgr_legend(res, ng - 1, leg, oenv);
615 for (j = 0; j < nres; j++)
617 fprintf(res, "%4d", j + 1);
618 for (i = 1; i < ng; i++)
620 fprintf(res, " %7g", bMin ? mindres[i - 1][j] : maxdres[i - 1][j]);
632 int freeLeg = bMat ? (ng == 1 ? 1 : (ng * (ng - 1)) / 2) : ng - 1;
633 for (int i = 0; i < freeLeg; i++)
640 static int find_residues(const t_atoms* atoms, int n, const int index[], int** resindex)
643 int nres = 0, resnr, presnr = 0;
644 bool presFound = false;
647 /* build index of first atom numbers for each residue */
648 snew(residx, atoms->nres + 1);
649 for (i = 0; i < n; i++)
651 resnr = atoms->atom[index[i]].resind;
652 if (!presFound || resnr != presnr)
662 printf("Found %d residues out of %d (%d/%d atoms)\n", nres, atoms->nres, atoms->nr, n);
664 srenew(residx, nres + 1);
665 /* mark end of last residue */
671 static void dump_res(FILE* out, int nres, int* resindex, int index[])
675 for (i = 0; i < nres - 1; i++)
677 fprintf(out, "Res %d (%d):", i, resindex[i + 1] - resindex[i]);
678 for (j = resindex[i]; j < resindex[i + 1]; j++)
680 fprintf(out, " %d(%d)", j, index[j]);
686 int gmx_mindist(int argc, char* argv[])
688 const char* desc[] = {
689 "[THISMODULE] computes the distance between one group and a number of",
690 "other groups. Both the minimum distance",
691 "(between any pair of atoms from the respective groups)",
692 "and the number of contacts within a given",
693 "distance are written to two separate output files.",
694 "With the [TT]-group[tt] option a contact of an atom in another group",
695 "with multiple atoms in the first group is counted as one contact",
696 "instead of as multiple contacts.",
697 "With [TT]-or[tt], minimum distances to each residue in the first",
698 "group are determined and plotted as a function of residue number.[PAR]",
699 "With option [TT]-pi[tt] the minimum distance of a group to its",
700 "periodic image is plotted. This is useful for checking if a protein",
701 "has seen its periodic image during a simulation. Only one shift in",
702 "each direction is considered, giving a total of 26 shifts. Note",
703 "that periodicity information is required from the file supplied with",
704 "with [TT]-s[tt], either as a .tpr file or a .pdb file with CRYST1 fields.",
705 "It also plots the maximum distance within the group and the lengths",
706 "of the three box vectors.[PAR]",
707 "Also [gmx-distance] and [gmx-pairdist] calculate distances."
710 gmx_bool bMat = FALSE, bPI = FALSE, bSplit = FALSE, bMax = FALSE, bPBC = TRUE;
711 gmx_bool bGroup = FALSE;
714 gmx_bool bEachResEachTime = FALSE, bPrintResName = FALSE;
716 { "-matrix", FALSE, etBOOL, { &bMat }, "Calculate half a matrix of group-group distances" },
717 { "-max", FALSE, etBOOL, { &bMax }, "Calculate *maximum* distance instead of minimum" },
718 { "-d", FALSE, etREAL, { &rcutoff }, "Distance for contacts" },
723 "Count contacts with multiple atoms in the first group as one" },
724 { "-pi", FALSE, etBOOL, { &bPI }, "Calculate minimum distance with periodic images" },
725 { "-split", FALSE, etBOOL, { &bSplit }, "Split graph where time is zero" },
730 "Number of secondary groups to compute distance to a central group" },
731 { "-pbc", FALSE, etBOOL, { &bPBC }, "Take periodic boundary conditions into account" },
735 { &bEachResEachTime },
736 "When writing per-residue distances, write distance for each time point" },
737 { "-printresname", FALSE, etBOOL, { &bPrintResName }, "Write residue names" }
739 gmx_output_env_t* oenv;
740 t_topology* top = nullptr;
744 gmx_bool bTop = FALSE;
747 const char *trxfnm, *tpsfnm, *ndxfnm, *distfnm, *numfnm, *atmfnm, *oxfnm, *resfnm;
750 int ** index, *residues = nullptr;
751 t_filenm fnm[] = { { efTRX, "-f", nullptr, ffREAD }, { efTPS, nullptr, nullptr, ffOPTRD },
752 { efNDX, nullptr, nullptr, ffOPTRD }, { efXVG, "-od", "mindist", ffWRITE },
753 { efXVG, "-on", "numcont", ffOPTWR }, { efOUT, "-o", "atm-pair", ffOPTWR },
754 { efTRO, "-ox", "mindist", ffOPTWR }, { efXVG, "-or", "mindistres", ffOPTWR } };
755 #define NFILE asize(fnm)
757 if (!parse_common_args(&argc, argv, PCA_CAN_VIEW | PCA_CAN_TIME | PCA_TIME_UNIT, NFILE, fnm,
758 asize(pa), pa, asize(desc), desc, 0, nullptr, &oenv))
763 trxfnm = ftp2fn(efTRX, NFILE, fnm);
764 ndxfnm = ftp2fn_null(efNDX, NFILE, fnm);
765 distfnm = opt2fn("-od", NFILE, fnm);
766 numfnm = opt2fn_null("-on", NFILE, fnm);
767 atmfnm = ftp2fn_null(efOUT, NFILE, fnm);
768 oxfnm = opt2fn_null("-ox", NFILE, fnm);
769 resfnm = opt2fn_null("-or", NFILE, fnm);
770 if (bPI || resfnm != nullptr)
772 /* We need a tps file */
773 tpsfnm = ftp2fn(efTPS, NFILE, fnm);
777 tpsfnm = ftp2fn_null(efTPS, NFILE, fnm);
780 if (!tpsfnm && !ndxfnm)
782 gmx_fatal(FARGS, "You have to specify either the index file or a tpr file");
788 fprintf(stderr, "Choose a group for distance calculation\n");
799 if (tpsfnm || resfnm || !ndxfnm)
802 bTop = read_tps_conf(tpsfnm, top, &ePBC, &x, nullptr, box, FALSE);
805 printf("\nWARNING: Without a run input file a trajectory with broken molecules will "
806 "not give the correct periodic image distance\n\n");
809 get_index(top ? &(top->atoms) : nullptr, ndxfnm, ng, gnx, index, grpname);
811 if (bMat && (ng == 1))
814 printf("Special case: making distance matrix between all atoms in group %s\n", grpname[0]);
818 for (i = 1; (i < ng); i++)
821 grpname[i] = grpname[0];
823 index[i][0] = index[0][i];
827 GMX_RELEASE_ASSERT(!bMat || ng > 1, "Must have more than one group with bMat");
831 GMX_RELEASE_ASSERT(top != nullptr, "top pointer cannot be NULL when finding residues");
832 nres = find_residues(&(top->atoms), gnx[0], index[0], &residues);
836 dump_res(debug, nres, residues, index[0]);
839 else if (bEachResEachTime || bPrintResName)
841 gmx_fatal(FARGS, "Option -or needs to be set to print residues");
846 periodic_mindist_plot(trxfnm, distfnm, top, ePBC, gnx[0], index[0], bSplit, oenv);
850 dist_plot(trxfnm, atmfnm, distfnm, numfnm, resfnm, oxfnm, rcutoff, bMat,
851 top ? &(top->atoms) : nullptr, ng, index, gnx, grpname, bSplit, !bMax, nres,
852 residues, bPBC, ePBC, bGroup, bEachResEachTime, bPrintResName, oenv);
855 do_view(oenv, distfnm, "-nxy");
858 do_view(oenv, numfnm, "-nxy");
861 output_env_done(oenv);
863 for (int i = 0; i < ng; i++)