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.
48 #include "gromacs/commandline/pargs.h"
49 #include "gromacs/commandline/viewit.h"
50 #include "gromacs/fileio/confio.h"
51 #include "gromacs/fileio/pdbio.h"
52 #include "gromacs/fileio/tpxio.h"
53 #include "gromacs/fileio/trxio.h"
54 #include "gromacs/gmxana/princ.h"
55 #include "gromacs/gmxlib/conformation_utilities.h"
56 #include "gromacs/math/functions.h"
57 #include "gromacs/math/units.h"
58 #include "gromacs/math/vec.h"
59 #include "gromacs/pbcutil/pbc.h"
60 #include "gromacs/pbcutil/rmpbc.h"
61 #include "gromacs/topology/atomprop.h"
62 #include "gromacs/topology/index.h"
63 #include "gromacs/topology/topology.h"
64 #include "gromacs/utility/arraysize.h"
65 #include "gromacs/utility/cstringutil.h"
66 #include "gromacs/utility/fatalerror.h"
67 #include "gromacs/utility/futil.h"
68 #include "gromacs/utility/gmxassert.h"
69 #include "gromacs/utility/smalloc.h"
70 #include "gromacs/utility/strdb.h"
72 static real calc_mass(t_atoms* atoms, gmx_bool bGetMass, AtomProperties* aps)
78 for (i = 0; (i < atoms->nr); i++)
82 aps->setAtomProperty(epropMass,
83 std::string(*atoms->resinfo[atoms->atom[i].resind].name),
84 std::string(*atoms->atomname[i]),
87 tmass += atoms->atom[i].m;
93 static real calc_geom(int isize, const int* index, rvec* x, rvec geom_center, rvec minval, rvec maxval, gmx_bool bDiam)
98 clear_rvec(geom_center);
115 for (j = 0; j < DIM; j++)
117 minval[j] = maxval[j] = x[ii][j];
119 for (i = 0; i < isize; i++)
129 rvec_inc(geom_center, x[ii]);
130 for (j = 0; j < DIM; j++)
132 if (x[ii][j] < minval[j])
134 minval[j] = x[ii][j];
136 if (x[ii][j] > maxval[j])
138 maxval[j] = x[ii][j];
145 for (j = i + 1; j < isize; j++)
147 d = distance2(x[ii], x[index[j]]);
148 diam2 = std::max(d, diam2);
153 for (j = i + 1; j < isize; j++)
155 d = distance2(x[i], x[j]);
156 diam2 = std::max(d, diam2);
161 svmul(1.0 / isize, geom_center, geom_center);
164 return std::sqrt(diam2);
167 static void center_conf(int natom, rvec* x, rvec center, rvec geom_cent)
172 rvec_sub(center, geom_cent, shift);
174 printf(" shift :%7.3f%7.3f%7.3f (nm)\n", shift[XX], shift[YY], shift[ZZ]);
176 for (i = 0; (i < natom); i++)
178 rvec_inc(x[i], shift);
182 static void scale_conf(int natom, rvec x[], matrix box, const rvec scale)
186 for (i = 0; i < natom; i++)
188 for (j = 0; j < DIM; j++)
193 for (i = 0; i < DIM; i++)
195 for (j = 0; j < DIM; j++)
197 box[i][j] *= scale[j];
202 static void read_bfac(const char* fn, int* n_bfac, double** bfac_val, int** bfac_nr)
207 *n_bfac = get_lines(fn, &bfac_lines);
208 snew(*bfac_val, *n_bfac);
209 snew(*bfac_nr, *n_bfac);
210 fprintf(stderr, "Reading %d B-factors from %s\n", *n_bfac, fn);
211 for (i = 0; (i < *n_bfac); i++)
213 sscanf(bfac_lines[i], "%d %lf", &(*bfac_nr)[i], &(*bfac_val)[i]);
217 static void set_pdb_conf_bfac(int natoms, int nres, t_atoms* atoms, int n_bfac, double* bfac, int* bfac_nr, gmx_bool peratom)
219 real bfac_min, bfac_max;
223 if (n_bfac > atoms->nres)
230 for (i = 0; (i < n_bfac); i++)
232 /* if ((bfac_nr[i]-1<0) || (bfac_nr[i]-1>=atoms->nr))
233 gmx_fatal(FARGS,"Index of B-Factor %d is out of range: %d (%g)",
234 i+1,bfac_nr[i],bfac[i]); */
235 if (bfac[i] > bfac_max)
239 if (bfac[i] < bfac_min)
244 while ((bfac_max > 99.99) || (bfac_min < -99.99))
247 "Range of values for B-factors too large (min %g, max %g) "
248 "will scale down a factor 10\n",
251 for (i = 0; (i < n_bfac); i++)
258 while ((std::abs(bfac_max) < 0.5) && (std::abs(bfac_min) < 0.5))
261 "Range of values for B-factors too small (min %g, max %g) "
262 "will scale up a factor 10\n",
265 for (i = 0; (i < n_bfac); i++)
273 for (i = 0; (i < natoms); i++)
275 atoms->pdbinfo[i].bfac = 0;
280 fprintf(stderr, "Will attach %d B-factors to %d residues\n", n_bfac, nres);
281 for (i = 0; (i < n_bfac); i++)
284 for (n = 0; (n < natoms); n++)
286 if (bfac_nr[i] == atoms->resinfo[atoms->atom[n].resind].nr)
288 atoms->pdbinfo[n].bfac = bfac[i];
294 gmx_warning("Residue nr %d not found\n", bfac_nr[i]);
300 fprintf(stderr, "Will attach %d B-factors to %d atoms\n", n_bfac, natoms);
301 for (i = 0; (i < n_bfac); i++)
303 atoms->pdbinfo[bfac_nr[i] - 1].bfac = bfac[i];
308 static void pdb_legend(FILE* out, int natoms, int nres, t_atoms* atoms, rvec x[])
310 real bfac_min, bfac_max, xmin, ymin, zmin;
319 for (i = 0; (i < natoms); i++)
321 xmin = std::min(xmin, x[i][XX]);
322 ymin = std::min(ymin, x[i][YY]);
323 zmin = std::min(zmin, x[i][ZZ]);
324 bfac_min = std::min(bfac_min, atoms->pdbinfo[i].bfac);
325 bfac_max = std::max(bfac_max, atoms->pdbinfo[i].bfac);
327 fprintf(stderr, "B-factors range from %g to %g\n", bfac_min, bfac_max);
328 for (i = 1; (i < 12); i++)
331 "%-6s%5d %-4.4s%3.3s %c%4d%c %8.3f%8.3f%8.3f%6.2f%6.2f\n",
339 (xmin + (i * 0.12)) * 10,
343 bfac_min + ((i - 1.0) * (bfac_max - bfac_min) / 10));
347 static void visualize_images(const char* fn, PbcType pbcType, matrix box)
355 init_t_atoms(&atoms, nat, FALSE);
358 /* FIXME: Constness should not be cast away */
359 c = const_cast<char*>("C");
360 ala = const_cast<char*>("ALA");
361 for (i = 0; i < nat; i++)
363 atoms.atomname[i] = &c;
364 atoms.atom[i].resind = i;
365 atoms.resinfo[i].name = &ala;
366 atoms.resinfo[i].nr = i + 1;
367 atoms.resinfo[i].chainid = 'A' + i / NCUCVERT;
369 calc_triclinic_images(box, img + 1);
371 write_sto_conf(fn, "Images", &atoms, img, nullptr, pbcType, box);
377 static void visualize_box(FILE* out, int a0, int r0, matrix box, const rvec gridsize)
381 int nx, ny, nz, nbox, nat;
383 int rectedge[24] = { 0, 1, 1, 3, 3, 2, 0, 2, 0, 4, 1, 5, 3, 7, 2, 6, 4, 5, 5, 7, 7, 6, 6, 4 };
388 nx = gmx::roundToInt(gridsize[XX]);
389 ny = gmx::roundToInt(gridsize[YY]);
390 nz = gmx::roundToInt(gridsize[ZZ]);
394 nat = nbox * NCUCVERT;
396 calc_compact_unitcell_vertices(ecenterDEF, box, vert);
398 for (z = 0; z < nz; z++)
400 for (y = 0; y < ny; y++)
402 for (x = 0; x < nx; x++)
404 for (i = 0; i < DIM; i++)
406 shift[i] = x * box[0][i] + y * box[1][i] + z * box[2][i];
408 for (i = 0; i < NCUCVERT; i++)
410 rvec_add(vert[i], shift, vert[j]);
417 for (i = 0; i < nat; i++)
419 gmx_fprintf_pdb_atomline(out,
436 edge = compact_unitcell_edges();
437 for (j = 0; j < nbox; j++)
439 for (i = 0; i < NCUCEDGE; i++)
443 a0 + j * NCUCVERT + edge[2 * i],
444 a0 + j * NCUCVERT + edge[2 * i + 1]);
453 for (z = 0; z <= 1; z++)
455 for (y = 0; y <= 1; y++)
457 for (x = 0; x <= 1; x++)
459 gmx_fprintf_pdb_atomline(out,
468 x * 10 * box[XX][XX],
469 y * 10 * box[YY][YY],
470 z * 10 * box[ZZ][ZZ],
478 for (i = 0; i < 24; i += 2)
480 fprintf(out, "CONECT%5d%5d\n", a0 + rectedge[i], a0 + rectedge[i + 1]);
485 static void calc_rotmatrix(rvec principal_axis, rvec targetvec, matrix rotmatrix)
488 real ux, uy, uz, costheta, sintheta;
490 costheta = cos_angle(principal_axis, targetvec);
491 sintheta = std::sqrt(1.0 - costheta * costheta); /* sign is always positive since 0<theta<pi */
493 /* Determine rotation from cross product with target vector */
494 cprod(principal_axis, targetvec, rotvec);
495 unitv(rotvec, rotvec);
496 printf("Aligning %g %g %g to %g %g %g : xprod %g %g %g\n",
510 rotmatrix[0][0] = ux * ux + (1.0 - ux * ux) * costheta;
511 rotmatrix[0][1] = ux * uy * (1 - costheta) - uz * sintheta;
512 rotmatrix[0][2] = ux * uz * (1 - costheta) + uy * sintheta;
513 rotmatrix[1][0] = ux * uy * (1 - costheta) + uz * sintheta;
514 rotmatrix[1][1] = uy * uy + (1.0 - uy * uy) * costheta;
515 rotmatrix[1][2] = uy * uz * (1 - costheta) - ux * sintheta;
516 rotmatrix[2][0] = ux * uz * (1 - costheta) - uy * sintheta;
517 rotmatrix[2][1] = uy * uz * (1 - costheta) + ux * sintheta;
518 rotmatrix[2][2] = uz * uz + (1.0 - uz * uz) * costheta;
520 printf("Rotation matrix: \n%g %g %g\n%g %g %g\n%g %g %g\n",
532 static void renum_resnr(t_atoms* atoms, int isize, const int* index, int resnr_start)
534 int i, resind_prev, resind;
537 for (i = 0; i < isize; i++)
539 resind = atoms->atom[index == nullptr ? i : index[i]].resind;
540 if (resind != resind_prev)
542 atoms->resinfo[resind].nr = resnr_start;
545 resind_prev = resind;
549 int gmx_editconf(int argc, char* argv[])
551 const char* desc[] = {
552 "[THISMODULE] converts generic structure format to [REF].gro[ref], [TT].g96[tt]",
553 "or [REF].pdb[ref].",
555 "The box can be modified with options [TT]-box[tt], [TT]-d[tt] and",
556 "[TT]-angles[tt]. Both [TT]-box[tt] and [TT]-d[tt]",
557 "will center the system in the box, unless [TT]-noc[tt] is used.",
558 "The [TT]-center[tt] option can be used to shift the geometric center",
559 "of the system from the default of (x/2, y/2, z/2) implied by [TT]-c[tt]",
560 "to some other value.",
562 "Option [TT]-bt[tt] determines the box type: [TT]triclinic[tt] is a",
563 "triclinic box, [TT]cubic[tt] is a rectangular box with all sides equal",
564 "[TT]dodecahedron[tt] represents a rhombic dodecahedron and",
565 "[TT]octahedron[tt] is a truncated octahedron.",
566 "The last two are special cases of a triclinic box.",
567 "The length of the three box vectors of the truncated octahedron is the",
568 "shortest distance between two opposite hexagons.",
569 "Relative to a cubic box with some periodic image distance, the volume of a ",
570 "dodecahedron with this same periodic distance is 0.71 times that of the cube, ",
571 "and that of a truncated octahedron is 0.77 times.",
573 "Option [TT]-box[tt] requires only",
574 "one value for a cubic, rhombic dodecahedral, or truncated octahedral box.",
576 "With [TT]-d[tt] and a [TT]triclinic[tt] box the size of the system in the [IT]x[it]-, ",
578 "and [IT]z[it]-directions is used. With [TT]-d[tt] and [TT]cubic[tt],",
579 "[TT]dodecahedron[tt] or [TT]octahedron[tt] boxes, the dimensions are set",
580 "to the diameter of the system (largest distance between atoms) plus twice",
581 "the specified distance.",
583 "Option [TT]-angles[tt] is only meaningful with option [TT]-box[tt] and",
584 "a triclinic box and cannot be used with option [TT]-d[tt].",
586 "When [TT]-n[tt] or [TT]-ndef[tt] is set, a group",
587 "can be selected for calculating the size and the geometric center,",
588 "otherwise the whole system is used.",
590 "[TT]-rotate[tt] rotates the coordinates and velocities.",
592 "[TT]-princ[tt] aligns the principal axes of the system along the",
593 "coordinate axes, with the longest axis aligned with the [IT]x[it]-axis. ",
594 "This may allow you to decrease the box volume,",
595 "but beware that molecules can rotate significantly in a nanosecond.",
597 "Scaling is applied before any of the other operations are",
598 "performed. Boxes and coordinates can be scaled to give a certain density (option",
599 "[TT]-density[tt]). Note that this may be inaccurate in case a [REF].gro[ref]",
600 "file is given as input. A special feature of the scaling option is that when the",
601 "factor -1 is given in one dimension, one obtains a mirror image,",
602 "mirrored in one of the planes. When one uses -1 in three dimensions, ",
603 "a point-mirror image is obtained.[PAR]",
604 "Groups are selected after all operations have been applied.[PAR]",
605 "Periodicity can be removed in a crude manner.",
606 "It is important that the box vectors at the bottom of your input file",
607 "are correct when the periodicity is to be removed.",
609 "When writing [REF].pdb[ref] files, B-factors can be",
610 "added with the [TT]-bf[tt] option. B-factors are read",
611 "from a file with with following format: first line states number of",
612 "entries in the file, next lines state an index",
613 "followed by a B-factor. The B-factors will be attached per residue",
614 "unless the number of B-factors is larger than the number of the residues or unless the",
615 "[TT]-atom[tt] option is set. Obviously, any type of numeric data can",
616 "be added instead of B-factors. [TT]-legend[tt] will produce",
617 "a row of CA atoms with B-factors ranging from the minimum to the",
618 "maximum value found, effectively making a legend for viewing.",
620 "With the option [TT]-mead[tt] a special [REF].pdb[ref] ([REF].pqr[ref])",
621 "file for the MEAD electrostatics",
622 "program (Poisson-Boltzmann solver) can be made. A further prerequisite",
623 "is that the input file is a run input file.",
624 "The B-factor field is then filled with the Van der Waals radius",
625 "of the atoms while the occupancy field will hold the charge.",
627 "The option [TT]-grasp[tt] is similar, but it puts the charges in the B-factor",
628 "and the radius in the occupancy.",
630 "Option [TT]-align[tt] allows alignment",
631 "of the principal axis of a specified group against the given vector, ",
632 "with an optional center of rotation specified by [TT]-aligncenter[tt].",
634 "Finally, with option [TT]-label[tt], [TT]editconf[tt] can add a chain identifier",
635 "to a [REF].pdb[ref] file, which can be useful for analysis with e.g. Rasmol.",
637 "To convert a truncated octrahedron file produced by a package which uses",
638 "a cubic box with the corners cut off (such as GROMOS), use::",
640 " gmx editconf -f in -rotate 0 45 35.264 -bt o -box veclen -o out",
642 "where [TT]veclen[tt] is the size of the cubic box times [SQRT]3[sqrt]/2."
644 const char* bugs[] = {
645 "For complex molecules, the periodicity removal routine may break down, ",
646 "in that case you can use [gmx-trjconv]."
648 static real dist = 0.0;
649 static gmx_bool bNDEF = FALSE, bRMPBC = FALSE, bCenter = FALSE, bReadVDW = FALSE, bCONECT = FALSE;
650 static gmx_bool peratom = FALSE, bLegend = FALSE, bOrient = FALSE, bMead = FALSE,
651 bGrasp = FALSE, bSig56 = FALSE;
652 static rvec scale = { 1, 1, 1 }, newbox = { 0, 0, 0 }, newang = { 90, 90, 90 };
653 static real rho = 1000.0, rvdw = 0.12;
654 static rvec center = { 0, 0, 0 }, translation = { 0, 0, 0 }, rotangles = { 0, 0, 0 },
655 aligncenter = { 0, 0, 0 }, targetvec = { 0, 0, 0 };
656 static const char *btype[] = { nullptr, "triclinic", "cubic",
657 "dodecahedron", "octahedron", nullptr },
659 static rvec visbox = { 0, 0, 0 };
660 static int resnr_start = -1;
662 { "-ndef", FALSE, etBOOL, { &bNDEF }, "Choose output from default index groups" },
667 "HIDDENVisualize a grid of boxes, -1 visualizes the 14 box images" },
668 { "-bt", FALSE, etENUM, { btype }, "Box type for [TT]-box[tt] and [TT]-d[tt]" },
669 { "-box", FALSE, etRVEC, { newbox }, "Box vector lengths (a,b,c)" },
670 { "-angles", FALSE, etRVEC, { newang }, "Angles between the box vectors (bc,ac,ab)" },
671 { "-d", FALSE, etREAL, { &dist }, "Distance between the solute and the box" },
676 "Center molecule in box (implied by [TT]-box[tt] and [TT]-d[tt])" },
677 { "-center", FALSE, etRVEC, { center }, "Shift the geometrical center to (x,y,z)" },
678 { "-aligncenter", FALSE, etRVEC, { aligncenter }, "Center of rotation for alignment" },
679 { "-align", FALSE, etRVEC, { targetvec }, "Align to target vector" },
680 { "-translate", FALSE, etRVEC, { translation }, "Translation" },
685 "Rotation around the X, Y and Z axes in degrees" },
686 { "-princ", FALSE, etBOOL, { &bOrient }, "Orient molecule(s) along their principal axes" },
687 { "-scale", FALSE, etRVEC, { scale }, "Scaling factor" },
692 "Density (g/L) of the output box achieved by scaling" },
693 { "-pbc", FALSE, etBOOL, { &bRMPBC }, "Remove the periodicity (make molecule whole again)" },
694 { "-resnr", FALSE, etINT, { &resnr_start }, " Renumber residues starting from resnr" },
699 "Store the charge of the atom in the B-factor field and the radius of the atom in the "
705 "Default Van der Waals radius (in nm) if one can not be found in the database or if no "
706 "parameters are present in the topology file" },
711 "Use rmin/2 (minimum in the Van der Waals potential) rather than [GRK]sigma[grk]/2 " },
716 "Read the Van der Waals radii from the file [TT]vdwradii.dat[tt] rather than computing "
717 "the radii based on the force field" },
718 { "-atom", FALSE, etBOOL, { &peratom }, "Force B-factor attachment per atom" },
719 { "-legend", FALSE, etBOOL, { &bLegend }, "Make B-factor legend" },
720 { "-label", FALSE, etSTR, { &label }, "Add chain label for all residues" },
725 "Add CONECT records to a [REF].pdb[ref] file when written. Can only be done when a "
726 "topology is present" }
728 #define NPA asize(pa)
731 const char * infile, *outfile;
732 int outftp, inftp, natom, i, j, n_bfac, itype, ntype;
733 double * bfac = nullptr, c6, c12;
734 int* bfac_nr = nullptr;
735 t_topology* top = nullptr;
736 char * grpname, *sgrpname, *agrpname;
737 int isize, ssize, numAlignmentAtoms;
738 int * index, *sindex, *aindex;
739 rvec * x, *v, gc, rmin, rmax, size;
741 matrix box, rotmatrix, trans;
743 gmx_bool bIndex, bSetSize, bSetAng, bDist, bSetCenter, bAlign;
744 gmx_bool bHaveV, bScale, bRho, bTranslate, bRotate, bCalcGeom, bCalcDiam;
745 real diam = 0, mass = 0, d, vdw;
747 gmx_output_env_t* oenv;
748 t_filenm fnm[] = { { efSTX, "-f", nullptr, ffREAD },
749 { efNDX, "-n", nullptr, ffOPTRD },
750 { efSTO, nullptr, nullptr, ffOPTWR },
751 { efPQR, "-mead", "mead", ffOPTWR },
752 { efDAT, "-bf", "bfact", ffOPTRD } };
753 #define NFILE asize(fnm)
755 if (!parse_common_args(
756 &argc, argv, PCA_CAN_VIEW, NFILE, fnm, NPA, pa, asize(desc), desc, asize(bugs), bugs, &oenv))
761 "Note that major changes are planned in future for "
762 "editconf, to improve usability and utility.\n");
764 bIndex = opt2bSet("-n", NFILE, fnm) || bNDEF;
765 bMead = opt2bSet("-mead", NFILE, fnm);
766 bSetSize = opt2parg_bSet("-box", NPA, pa);
767 bSetAng = opt2parg_bSet("-angles", NPA, pa);
768 bSetCenter = opt2parg_bSet("-center", NPA, pa);
769 bDist = opt2parg_bSet("-d", NPA, pa);
770 bAlign = opt2parg_bSet("-align", NPA, pa);
771 /* Only automatically turn on centering without -noc */
772 if ((bDist || bSetSize || bSetCenter) && !opt2parg_bSet("-c", NPA, pa))
776 bScale = opt2parg_bSet("-scale", NPA, pa);
777 bRho = opt2parg_bSet("-density", NPA, pa);
778 bTranslate = opt2parg_bSet("-translate", NPA, pa);
779 bRotate = opt2parg_bSet("-rotate", NPA, pa);
782 fprintf(stderr, "WARNING: setting -density overrides -scale\n");
784 bScale = bScale || bRho;
785 bCalcGeom = bCenter || bRotate || bOrient || bScale;
787 GMX_RELEASE_ASSERT(btype[0] != nullptr, "Option setting inconsistency; btype[0] is NULL");
789 bCalcDiam = (btype[0][0] == 'c' || btype[0][0] == 'd' || btype[0][0] == 'o');
791 infile = ftp2fn(efSTX, NFILE, fnm);
794 outfile = ftp2fn(efPQR, NFILE, fnm);
798 outfile = ftp2fn(efSTO, NFILE, fnm);
800 outftp = fn2ftp(outfile);
801 inftp = fn2ftp(infile);
807 printf("Incompatible options -mead and -grasp. Turning off -grasp\n");
810 if (bGrasp && (outftp != efPDB))
813 "Output file should be a .pdb file"
814 " when using the -grasp option\n");
816 if ((bMead || bGrasp) && (fn2ftp(infile) != efTPR))
819 "Input file should be a .tpr file"
820 " when using the -mead option\n");
826 open_symtab(&symtab);
827 readConfAndAtoms(infile, &symtab, &name, &atoms, &pbcType, &x, &v, box);
829 if (atoms.pdbinfo == nullptr)
831 snew(atoms.pdbinfo, atoms.nr);
833 atoms.havePdbInfo = TRUE;
835 if (fn2ftp(infile) == efPDB)
837 get_pdb_atomnumber(&atoms, &aps);
839 printf("Read %d atoms\n", atoms.nr);
841 /* Get the element numbers if available in a pdb file */
842 if (fn2ftp(infile) == efPDB)
844 get_pdb_atomnumber(&atoms, &aps);
847 if (pbcType != PbcType::No)
850 printf("Volume: %g nm^3, corresponds to roughly %d electrons\n",
852 100 * (static_cast<int>(vol * 4.5)));
855 if (bMead || bGrasp || bCONECT)
857 top = read_top(infile, nullptr);
862 if (atoms.nr != top->atoms.nr)
864 gmx_fatal(FARGS, "Atom numbers don't match (%d vs. %d)", atoms.nr, top->atoms.nr);
866 snew(atoms.pdbinfo, top->atoms.nr);
867 ntype = top->idef.atnr;
868 for (i = 0; (i < atoms.nr); i++)
870 /* Determine the Van der Waals radius from the force field */
873 if (!aps.setAtomProperty(epropVDW,
874 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
875 *top->atoms.atomname[i],
883 itype = top->atoms.atom[i].type;
884 c12 = top->idef.iparams[itype * ntype + itype].lj.c12;
885 c6 = top->idef.iparams[itype * ntype + itype].lj.c6;
886 if ((c6 != 0) && (c12 != 0))
897 vdw = 0.5 * gmx::sixthroot(sig6);
904 /* Factor of 10 for nm -> Angstroms */
909 atoms.pdbinfo[i].occup = top->atoms.atom[i].q;
910 atoms.pdbinfo[i].bfac = vdw;
914 atoms.pdbinfo[i].occup = vdw;
915 atoms.pdbinfo[i].bfac = top->atoms.atom[i].q;
920 for (i = 0; (i < natom) && !bHaveV; i++)
922 for (j = 0; (j < DIM) && !bHaveV; j++)
924 bHaveV = bHaveV || (v[i][j] != 0);
927 printf("%selocities found\n", bHaveV ? "V" : "No v");
933 gmx_fatal(FARGS, "Sorry, can not visualize box with index groups");
937 gmx_fatal(FARGS, "Sorry, can only visualize box with a pdb file");
940 else if (visbox[0] == -1)
942 visualize_images("images.pdb", pbcType, box);
948 rm_gropbc(&atoms, x, box);
955 fprintf(stderr, "\nSelect a group for determining the system size:\n");
956 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &ssize, &sindex, &sgrpname);
963 diam = calc_geom(ssize, sindex, x, gc, rmin, rmax, bCalcDiam);
964 rvec_sub(rmax, rmin, size);
965 printf(" system size :%7.3f%7.3f%7.3f (nm)\n", size[XX], size[YY], size[ZZ]);
968 printf(" diameter :%7.3f (nm)\n", diam);
970 printf(" center :%7.3f%7.3f%7.3f (nm)\n", gc[XX], gc[YY], gc[ZZ]);
971 printf(" box vectors :%7.3f%7.3f%7.3f (nm)\n", norm(box[XX]), norm(box[YY]), norm(box[ZZ]));
972 printf(" box angles :%7.2f%7.2f%7.2f (degrees)\n",
973 norm2(box[ZZ]) == 0 ? 0 : RAD2DEG * gmx_angle(box[YY], box[ZZ]),
974 norm2(box[ZZ]) == 0 ? 0 : RAD2DEG * gmx_angle(box[XX], box[ZZ]),
975 norm2(box[YY]) == 0 ? 0 : RAD2DEG * gmx_angle(box[XX], box[YY]));
976 printf(" box volume :%7.2f (nm^3)\n", det(box));
979 if (bRho || bOrient || bAlign)
981 mass = calc_mass(&atoms, !fn2bTPX(infile), &aps);
989 /* Get a group for principal component analysis */
990 fprintf(stderr, "\nSelect group for the determining the orientation\n");
991 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &isize, &index, &grpnames);
993 /* Orient the principal axes along the coordinate axes */
994 orient_princ(&atoms, isize, index, natom, x, bHaveV ? v : nullptr, nullptr);
1001 /* scale coordinates and box */
1004 /* Compute scaling constant */
1008 dens = (mass * AMU) / (vol * NANO * NANO * NANO);
1009 fprintf(stderr, "Volume of input %g (nm^3)\n", vol);
1010 fprintf(stderr, "Mass of input %g (a.m.u.)\n", mass);
1011 fprintf(stderr, "Density of input %g (g/l)\n", dens);
1012 if (vol == 0 || mass == 0)
1015 "Cannot scale density with "
1016 "zero mass (%g) or volume (%g)\n",
1021 scale[XX] = scale[YY] = scale[ZZ] = std::cbrt(dens / rho);
1022 fprintf(stderr, "Scaling all box vectors by %g\n", scale[XX]);
1024 scale_conf(atoms.nr, x, box, scale);
1031 fprintf(stderr, "\nSelect a group that you want to align:\n");
1032 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &numAlignmentAtoms, &aindex, &agrpname);
1036 numAlignmentAtoms = atoms.nr;
1037 snew(aindex, numAlignmentAtoms);
1038 for (i = 0; i < numAlignmentAtoms; i++)
1043 printf("Aligning %d atoms (out of %d) to %g %g %g, center of rotation %g %g %g\n",
1052 /*subtract out pivot point*/
1053 for (i = 0; i < numAlignmentAtoms; i++)
1055 rvec_dec(x[aindex[i]], aligncenter);
1057 /*now determine transform and rotate*/
1059 principal_comp(numAlignmentAtoms, aindex, atoms.atom, x, trans, princd);
1061 unitv(targetvec, targetvec);
1062 printf("Using %g %g %g as principal axis\n", trans[0][2], trans[1][2], trans[2][2]);
1063 tmpvec[XX] = trans[0][2];
1064 tmpvec[YY] = trans[1][2];
1065 tmpvec[ZZ] = trans[2][2];
1066 calc_rotmatrix(tmpvec, targetvec, rotmatrix);
1067 /* rotmatrix finished */
1069 for (i = 0; i < numAlignmentAtoms; ++i)
1071 mvmul(rotmatrix, x[aindex[i]], tmpvec);
1072 copy_rvec(tmpvec, x[aindex[i]]);
1075 /*add pivot point back*/
1076 for (i = 0; i < numAlignmentAtoms; i++)
1078 rvec_inc(x[aindex[i]], aligncenter);
1090 fprintf(stderr, "\nSelect a group that you want to translate:\n");
1091 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &ssize, &sindex, &sgrpname);
1098 printf("Translating %d atoms (out of %d) by %g %g %g nm\n",
1106 for (i = 0; i < ssize; i++)
1108 rvec_inc(x[sindex[i]], translation);
1113 for (i = 0; i < natom; i++)
1115 rvec_inc(x[i], translation);
1122 printf("Rotating %g, %g, %g degrees around the X, Y and Z axis respectively\n",
1126 for (i = 0; i < DIM; i++)
1128 rotangles[i] *= DEG2RAD;
1130 rotate_conf(natom, x, v, rotangles[XX], rotangles[YY], rotangles[ZZ]);
1135 /* recalc geometrical center and max and min coordinates and size */
1136 calc_geom(ssize, sindex, x, gc, rmin, rmax, FALSE);
1137 rvec_sub(rmax, rmin, size);
1138 if (bScale || bOrient || bRotate)
1140 printf("new system size : %6.3f %6.3f %6.3f\n", size[XX], size[YY], size[ZZ]);
1144 if ((btype[0] != nullptr) && (bSetSize || bDist || (btype[0][0] == 't' && bSetAng)))
1146 pbcType = PbcType::Xyz;
1147 if (!(bSetSize || bDist))
1149 for (i = 0; i < DIM; i++)
1151 newbox[i] = norm(box[i]);
1155 /* calculate new boxsize */
1156 switch (btype[0][0])
1161 for (i = 0; i < DIM; i++)
1163 newbox[i] = size[i] + 2 * dist;
1168 box[XX][XX] = newbox[XX];
1169 box[YY][YY] = newbox[YY];
1170 box[ZZ][ZZ] = newbox[ZZ];
1174 matrix_convert(box, newbox, newang);
1186 d = diam + 2 * dist;
1188 if (btype[0][0] == 'c')
1190 for (i = 0; i < DIM; i++)
1195 else if (btype[0][0] == 'd')
1199 box[ZZ][XX] = d / 2;
1200 box[ZZ][YY] = d / 2;
1201 box[ZZ][ZZ] = d * std::sqrt(2.0) / 2.0;
1206 box[YY][XX] = d / 3;
1207 box[YY][YY] = d * std::sqrt(2.0) * 2.0 / 3.0;
1208 box[ZZ][XX] = -d / 3;
1209 box[ZZ][YY] = d * std::sqrt(2.0) / 3.0;
1210 box[ZZ][ZZ] = d * std::sqrt(6.0) / 3.0;
1216 /* calculate new coords for geometrical center */
1219 calc_box_center(ecenterDEF, box, center);
1222 /* center molecule on 'center' */
1225 center_conf(natom, x, center, gc);
1231 calc_geom(ssize, sindex, x, gc, rmin, rmax, FALSE);
1232 printf("new center :%7.3f%7.3f%7.3f (nm)\n", gc[XX], gc[YY], gc[ZZ]);
1234 if (bOrient || bScale || bDist || bSetSize)
1236 printf("new box vectors :%7.3f%7.3f%7.3f (nm)\n", norm(box[XX]), norm(box[YY]), norm(box[ZZ]));
1237 printf("new box angles :%7.2f%7.2f%7.2f (degrees)\n",
1238 norm2(box[ZZ]) == 0 ? 0 : RAD2DEG * gmx_angle(box[YY], box[ZZ]),
1239 norm2(box[ZZ]) == 0 ? 0 : RAD2DEG * gmx_angle(box[XX], box[ZZ]),
1240 norm2(box[YY]) == 0 ? 0 : RAD2DEG * gmx_angle(box[XX], box[YY]));
1241 printf("new box volume :%7.2f (nm^3)\n", det(box));
1244 if (check_box(PbcType::Xyz, box))
1246 printf("\nWARNING: %s\n"
1247 "See the GROMACS manual for a description of the requirements that\n"
1248 "must be satisfied by descriptions of simulation cells.\n",
1249 check_box(PbcType::Xyz, box));
1252 if (bDist && btype[0][0] == 't')
1256 printf("\nWARNING: Your box is triclinic with non-orthogonal axes. In this case, the\n"
1257 "distance from the solute to a box surface along the corresponding normal\n"
1258 "vector might be somewhat smaller than your specified value %f.\n"
1259 "You can check the actual value with g_mindist -pi\n",
1262 else if (!opt2parg_bSet("-bt", NPA, pa))
1264 printf("\nWARNING: No boxtype specified - distance condition applied in each "
1266 "If the molecule rotates the actual distance will be smaller. You might want\n"
1267 "to use a cubic box instead, or why not try a dodecahedron today?\n");
1270 if (bCONECT && (outftp == efPDB) && (inftp == efTPR))
1272 conect = gmx_conect_generate(top);
1281 fprintf(stderr, "\nSelect a group for output:\n");
1282 get_index(&atoms, opt2fn_null("-n", NFILE, fnm), 1, &isize, &index, &grpname);
1284 if (resnr_start >= 0)
1286 renum_resnr(&atoms, isize, index, resnr_start);
1289 if (opt2parg_bSet("-label", NPA, pa))
1291 for (i = 0; (i < atoms.nr); i++)
1293 atoms.resinfo[atoms.atom[i].resind].chainid = label[0];
1297 if (opt2bSet("-bf", NFILE, fnm) || bLegend)
1299 gmx_fatal(FARGS, "Sorry, cannot do bfactors with an index group.");
1302 if (outftp == efPDB)
1304 out = gmx_ffopen(outfile, "w");
1305 write_pdbfile_indexed(out, name, &atoms, x, pbcType, box, ' ', 1, isize, index, conect, FALSE);
1310 write_sto_conf_indexed(
1311 outfile, name, &atoms, x, bHaveV ? v : nullptr, pbcType, box, isize, index);
1318 if (resnr_start >= 0)
1320 renum_resnr(&atoms, atoms.nr, nullptr, resnr_start);
1323 if ((outftp == efPDB) || (outftp == efPQR))
1325 out = gmx_ffopen(outfile, "w");
1330 "The B-factors in this file hold atomic radii\n");
1333 "The occupancy in this file hold atomic charges\n");
1337 fprintf(out, "GRASP PDB FILE\nFORMAT NUMBER=1\n");
1340 "The B-factors in this file hold atomic charges\n");
1343 "The occupancy in this file hold atomic radii\n");
1345 else if (opt2bSet("-bf", NFILE, fnm))
1347 read_bfac(opt2fn("-bf", NFILE, fnm), &n_bfac, &bfac, &bfac_nr);
1348 set_pdb_conf_bfac(atoms.nr, atoms.nres, &atoms, n_bfac, bfac, bfac_nr, peratom);
1350 if (opt2parg_bSet("-label", NPA, pa))
1352 for (i = 0; (i < atoms.nr); i++)
1354 atoms.resinfo[atoms.atom[i].resind].chainid = label[0];
1357 /* Need to bypass the regular write_pdbfile because I don't want to change
1358 * all instances to include the boolean flag for writing out PQR files.
1361 snew(index, atoms.nr);
1362 for (int i = 0; i < atoms.nr; i++)
1366 write_pdbfile_indexed(
1367 out, name, &atoms, x, pbcType, box, ' ', -1, atoms.nr, index, conect, outftp == efPQR);
1371 pdb_legend(out, atoms.nr, atoms.nres, &atoms, x);
1376 bLegend ? atoms.nr + 12 : atoms.nr,
1377 bLegend ? atoms.nres = 12 : atoms.nres,
1385 write_sto_conf(outfile, name, &atoms, x, bHaveV ? v : nullptr, pbcType, box);
1389 done_symtab(&symtab);
1399 do_view(oenv, outfile, nullptr);
1400 output_env_done(oenv);