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,2021, 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/utilities.h"
59 #include "gromacs/math/vec.h"
60 #include "gromacs/pbcutil/pbc.h"
61 #include "gromacs/pbcutil/rmpbc.h"
62 #include "gromacs/topology/atomprop.h"
63 #include "gromacs/topology/index.h"
64 #include "gromacs/topology/topology.h"
65 #include "gromacs/utility/arraysize.h"
66 #include "gromacs/utility/cstringutil.h"
67 #include "gromacs/utility/fatalerror.h"
68 #include "gromacs/utility/futil.h"
69 #include "gromacs/utility/gmxassert.h"
70 #include "gromacs/utility/smalloc.h"
71 #include "gromacs/utility/strdb.h"
73 static real calc_mass(t_atoms* atoms, gmx_bool bGetMass, AtomProperties* aps)
79 for (i = 0; (i < atoms->nr); i++)
83 aps->setAtomProperty(epropMass,
84 std::string(*atoms->resinfo[atoms->atom[i].resind].name),
85 std::string(*atoms->atomname[i]),
88 tmass += atoms->atom[i].m;
94 static real calc_geom(int isize, const int* index, rvec* x, rvec geom_center, rvec minval, rvec maxval, gmx_bool bDiam)
99 clear_rvec(geom_center);
116 for (j = 0; j < DIM; j++)
118 minval[j] = maxval[j] = x[ii][j];
120 for (i = 0; i < isize; i++)
130 rvec_inc(geom_center, x[ii]);
131 for (j = 0; j < DIM; j++)
133 if (x[ii][j] < minval[j])
135 minval[j] = x[ii][j];
137 if (x[ii][j] > maxval[j])
139 maxval[j] = x[ii][j];
146 for (j = i + 1; j < isize; j++)
148 d = distance2(x[ii], x[index[j]]);
149 diam2 = std::max(d, diam2);
154 for (j = i + 1; j < isize; j++)
156 d = distance2(x[i], x[j]);
157 diam2 = std::max(d, diam2);
162 svmul(1.0 / isize, geom_center, geom_center);
165 return std::sqrt(diam2);
168 static void center_conf(int natom, rvec* x, rvec center, rvec geom_cent)
173 rvec_sub(center, geom_cent, shift);
175 printf(" shift :%7.3f%7.3f%7.3f (nm)\n", shift[XX], shift[YY], shift[ZZ]);
177 for (i = 0; (i < natom); i++)
179 rvec_inc(x[i], shift);
183 static void scale_conf(int natom, rvec x[], matrix box, const rvec scale)
187 for (i = 0; i < natom; i++)
189 for (j = 0; j < DIM; j++)
194 for (i = 0; i < DIM; i++)
196 for (j = 0; j < DIM; j++)
198 box[i][j] *= scale[j];
203 static void read_bfac(const char* fn, int* n_bfac, double** bfac_val, int** bfac_nr)
208 *n_bfac = get_lines(fn, &bfac_lines);
209 snew(*bfac_val, *n_bfac);
210 snew(*bfac_nr, *n_bfac);
211 fprintf(stderr, "Reading %d B-factors from %s\n", *n_bfac, fn);
212 for (i = 0; (i < *n_bfac); i++)
214 sscanf(bfac_lines[i], "%d %lf", &(*bfac_nr)[i], &(*bfac_val)[i]);
218 static void set_pdb_conf_bfac(int natoms, int nres, t_atoms* atoms, int n_bfac, double* bfac, int* bfac_nr, gmx_bool peratom)
220 real bfac_min, bfac_max;
224 if (n_bfac > atoms->nres)
231 for (i = 0; (i < n_bfac); i++)
233 /* if ((bfac_nr[i]-1<0) || (bfac_nr[i]-1>=atoms->nr))
234 gmx_fatal(FARGS,"Index of B-Factor %d is out of range: %d (%g)",
235 i+1,bfac_nr[i],bfac[i]); */
236 if (bfac[i] > bfac_max)
240 if (bfac[i] < bfac_min)
245 while ((bfac_max > 99.99) || (bfac_min < -99.99))
248 "Range of values for B-factors too large (min %g, max %g) "
249 "will scale down a factor 10\n",
252 for (i = 0; (i < n_bfac); i++)
259 while ((std::abs(bfac_max) < 0.5) && (std::abs(bfac_min) < 0.5))
262 "Range of values for B-factors too small (min %g, max %g) "
263 "will scale up a factor 10\n",
266 for (i = 0; (i < n_bfac); i++)
274 for (i = 0; (i < natoms); i++)
276 atoms->pdbinfo[i].bfac = 0;
281 fprintf(stderr, "Will attach %d B-factors to %d residues\n", n_bfac, nres);
282 for (i = 0; (i < n_bfac); i++)
285 for (n = 0; (n < natoms); n++)
287 if (bfac_nr[i] == atoms->resinfo[atoms->atom[n].resind].nr)
289 atoms->pdbinfo[n].bfac = bfac[i];
295 gmx_warning("Residue nr %d not found\n", bfac_nr[i]);
301 fprintf(stderr, "Will attach %d B-factors to %d atoms\n", n_bfac, natoms);
302 for (i = 0; (i < n_bfac); i++)
304 atoms->pdbinfo[bfac_nr[i] - 1].bfac = bfac[i];
309 static void pdb_legend(FILE* out, int natoms, int nres, t_atoms* atoms, rvec x[])
311 real bfac_min, bfac_max, xmin, ymin, zmin;
320 for (i = 0; (i < natoms); i++)
322 xmin = std::min(xmin, x[i][XX]);
323 ymin = std::min(ymin, x[i][YY]);
324 zmin = std::min(zmin, x[i][ZZ]);
325 bfac_min = std::min(bfac_min, atoms->pdbinfo[i].bfac);
326 bfac_max = std::max(bfac_max, atoms->pdbinfo[i].bfac);
328 fprintf(stderr, "B-factors range from %g to %g\n", bfac_min, bfac_max);
329 for (i = 1; (i < 12); i++)
332 "%-6s%5d %-4.4s%3.3s %c%4d%c %8.3f%8.3f%8.3f%6.2f%6.2f\n",
340 (xmin + (i * 0.12)) * 10,
344 bfac_min + ((i - 1.0) * (bfac_max - bfac_min) / 10));
348 static void visualize_images(const char* fn, PbcType pbcType, matrix box)
356 init_t_atoms(&atoms, nat, FALSE);
359 /* FIXME: Constness should not be cast away */
360 c = const_cast<char*>("C");
361 ala = const_cast<char*>("ALA");
362 for (i = 0; i < nat; i++)
364 atoms.atomname[i] = &c;
365 atoms.atom[i].resind = i;
366 atoms.resinfo[i].name = &ala;
367 atoms.resinfo[i].nr = i + 1;
368 atoms.resinfo[i].chainid = 'A' + i / NCUCVERT;
370 calc_triclinic_images(box, img + 1);
372 write_sto_conf(fn, "Images", &atoms, img, nullptr, pbcType, box);
378 static void visualize_box(FILE* out, int a0, int r0, matrix box, const rvec gridsize)
382 int nx, ny, nz, nbox, nat;
384 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 };
389 nx = gmx::roundToInt(gridsize[XX]);
390 ny = gmx::roundToInt(gridsize[YY]);
391 nz = gmx::roundToInt(gridsize[ZZ]);
395 nat = nbox * NCUCVERT;
397 calc_compact_unitcell_vertices(ecenterDEF, box, vert);
399 for (z = 0; z < nz; z++)
401 for (y = 0; y < ny; y++)
403 for (x = 0; x < nx; x++)
405 for (i = 0; i < DIM; i++)
407 shift[i] = x * box[0][i] + y * box[1][i] + z * box[2][i];
409 for (i = 0; i < NCUCVERT; i++)
411 rvec_add(vert[i], shift, vert[j]);
418 for (i = 0; i < nat; i++)
420 gmx_fprintf_pdb_atomline(out,
437 edge = compact_unitcell_edges();
438 for (j = 0; j < nbox; j++)
440 for (i = 0; i < NCUCEDGE; i++)
444 a0 + j * NCUCVERT + edge[2 * i],
445 a0 + j * NCUCVERT + edge[2 * i + 1]);
454 for (z = 0; z <= 1; z++)
456 for (y = 0; y <= 1; y++)
458 for (x = 0; x <= 1; x++)
460 gmx_fprintf_pdb_atomline(out,
469 x * 10 * box[XX][XX],
470 y * 10 * box[YY][YY],
471 z * 10 * box[ZZ][ZZ],
479 for (i = 0; i < 24; i += 2)
481 fprintf(out, "CONECT%5d%5d\n", a0 + rectedge[i], a0 + rectedge[i + 1]);
486 static void calc_rotmatrix(rvec principal_axis, rvec targetvec, matrix rotmatrix)
489 real ux, uy, uz, costheta, sintheta;
491 costheta = cos_angle(principal_axis, targetvec);
492 sintheta = std::sqrt(1.0 - costheta * costheta); /* sign is always positive since 0<theta<pi */
494 /* Determine rotation from cross product with target vector */
495 cprod(principal_axis, targetvec, rotvec);
496 unitv(rotvec, rotvec);
497 printf("Aligning %g %g %g to %g %g %g : xprod %g %g %g\n",
511 rotmatrix[0][0] = ux * ux + (1.0 - ux * ux) * costheta;
512 rotmatrix[0][1] = ux * uy * (1 - costheta) - uz * sintheta;
513 rotmatrix[0][2] = ux * uz * (1 - costheta) + uy * sintheta;
514 rotmatrix[1][0] = ux * uy * (1 - costheta) + uz * sintheta;
515 rotmatrix[1][1] = uy * uy + (1.0 - uy * uy) * costheta;
516 rotmatrix[1][2] = uy * uz * (1 - costheta) - ux * sintheta;
517 rotmatrix[2][0] = ux * uz * (1 - costheta) - uy * sintheta;
518 rotmatrix[2][1] = uy * uz * (1 - costheta) + ux * sintheta;
519 rotmatrix[2][2] = uz * uz + (1.0 - uz * uz) * costheta;
521 printf("Rotation matrix: \n%g %g %g\n%g %g %g\n%g %g %g\n",
533 static void renum_resnr(t_atoms* atoms, int isize, const int* index, int resnr_start)
535 int i, resind_prev, resind;
538 for (i = 0; i < isize; i++)
540 resind = atoms->atom[index == nullptr ? i : index[i]].resind;
541 if (resind != resind_prev)
543 atoms->resinfo[resind].nr = resnr_start;
546 resind_prev = resind;
550 int gmx_editconf(int argc, char* argv[])
552 const char* desc[] = {
553 "[THISMODULE] converts generic structure format to [REF].gro[ref], [TT].g96[tt]",
554 "or [REF].pdb[ref].",
556 "The box can be modified with options [TT]-box[tt], [TT]-d[tt] and",
557 "[TT]-angles[tt]. Both [TT]-box[tt] and [TT]-d[tt]",
558 "will center the system in the box, unless [TT]-noc[tt] is used.",
559 "The [TT]-center[tt] option can be used to shift the geometric center",
560 "of the system from the default of (x/2, y/2, z/2) implied by [TT]-c[tt]",
561 "to some other value.",
563 "Option [TT]-bt[tt] determines the box type: [TT]triclinic[tt] is a",
564 "triclinic box, [TT]cubic[tt] is a rectangular box with all sides equal",
565 "[TT]dodecahedron[tt] represents a rhombic dodecahedron and",
566 "[TT]octahedron[tt] is a truncated octahedron.",
567 "The last two are special cases of a triclinic box.",
568 "The length of the three box vectors of the truncated octahedron is the",
569 "shortest distance between two opposite hexagons.",
570 "Relative to a cubic box with some periodic image distance, the volume of a ",
571 "dodecahedron with this same periodic distance is 0.71 times that of the cube, ",
572 "and that of a truncated octahedron is 0.77 times.",
574 "Option [TT]-box[tt] requires only",
575 "one value for a cubic, rhombic dodecahedral, or truncated octahedral box.",
577 "With [TT]-d[tt] and a [TT]triclinic[tt] box the size of the system in the [IT]x[it]-, ",
579 "and [IT]z[it]-directions is used. With [TT]-d[tt] and [TT]cubic[tt],",
580 "[TT]dodecahedron[tt] or [TT]octahedron[tt] boxes, the dimensions are set",
581 "to the diameter of the system (largest distance between atoms) plus twice",
582 "the specified distance.",
584 "Option [TT]-angles[tt] is only meaningful with option [TT]-box[tt] and",
585 "a triclinic box and cannot be used with option [TT]-d[tt].",
587 "When [TT]-n[tt] or [TT]-ndef[tt] is set, a group",
588 "can be selected for calculating the size and the geometric center,",
589 "otherwise the whole system is used.",
591 "[TT]-rotate[tt] rotates the coordinates and velocities.",
593 "[TT]-princ[tt] aligns the principal axes of the system along the",
594 "coordinate axes, with the longest axis aligned with the [IT]x[it]-axis. ",
595 "This may allow you to decrease the box volume,",
596 "but beware that molecules can rotate significantly in a nanosecond.",
598 "Scaling is applied before any of the other operations are",
599 "performed. Boxes and coordinates can be scaled to give a certain density (option",
600 "[TT]-density[tt]). Note that this may be inaccurate in case a [REF].gro[ref]",
601 "file is given as input. A special feature of the scaling option is that when the",
602 "factor -1 is given in one dimension, one obtains a mirror image,",
603 "mirrored in one of the planes. When one uses -1 in three dimensions, ",
604 "a point-mirror image is obtained.[PAR]",
605 "Groups are selected after all operations have been applied.[PAR]",
606 "Periodicity can be removed in a crude manner.",
607 "It is important that the box vectors at the bottom of your input file",
608 "are correct when the periodicity is to be removed.",
610 "When writing [REF].pdb[ref] files, B-factors can be",
611 "added with the [TT]-bf[tt] option. B-factors are read",
612 "from a file with with following format: first line states number of",
613 "entries in the file, next lines state an index",
614 "followed by a B-factor. The B-factors will be attached per residue",
615 "unless the number of B-factors is larger than the number of the residues or unless the",
616 "[TT]-atom[tt] option is set. Obviously, any type of numeric data can",
617 "be added instead of B-factors. [TT]-legend[tt] will produce",
618 "a row of CA atoms with B-factors ranging from the minimum to the",
619 "maximum value found, effectively making a legend for viewing.",
621 "With the option [TT]-mead[tt] a special [REF].pdb[ref] ([REF].pqr[ref])",
622 "file for the MEAD electrostatics",
623 "program (Poisson-Boltzmann solver) can be made. A further prerequisite",
624 "is that the input file is a run input file.",
625 "The B-factor field is then filled with the Van der Waals radius",
626 "of the atoms while the occupancy field will hold the charge.",
628 "The option [TT]-grasp[tt] is similar, but it puts the charges in the B-factor",
629 "and the radius in the occupancy.",
631 "Option [TT]-align[tt] allows alignment",
632 "of the principal axis of a specified group against the given vector, ",
633 "with an optional center of rotation specified by [TT]-aligncenter[tt].",
635 "Finally, with option [TT]-label[tt], [TT]editconf[tt] can add a chain identifier",
636 "to a [REF].pdb[ref] file, which can be useful for analysis with e.g. Rasmol.",
638 "To convert a truncated octrahedron file produced by a package which uses",
639 "a cubic box with the corners cut off (such as GROMOS), use::",
641 " gmx editconf -f in -rotate 0 45 35.264 -bt o -box veclen -o out",
643 "where [TT]veclen[tt] is the size of the cubic box times [SQRT]3[sqrt]/2."
645 const char* bugs[] = {
646 "For complex molecules, the periodicity removal routine may break down, ",
647 "in that case you can use [gmx-trjconv]."
649 static real dist = 0.0;
650 static gmx_bool bNDEF = FALSE, bRMPBC = FALSE, bCenter = FALSE, bReadVDW = FALSE, bCONECT = FALSE;
651 static gmx_bool peratom = FALSE, bLegend = FALSE, bOrient = FALSE, bMead = FALSE,
652 bGrasp = FALSE, bSig56 = FALSE;
653 static rvec scale = { 1, 1, 1 }, newbox = { 0, 0, 0 }, newang = { 90, 90, 90 };
654 static real rho = 1000.0, rvdw = 0.12;
655 static rvec center = { 0, 0, 0 }, translation = { 0, 0, 0 }, rotangles = { 0, 0, 0 },
656 aligncenter = { 0, 0, 0 }, targetvec = { 0, 0, 0 };
657 static const char *btype[] = { nullptr, "triclinic", "cubic",
658 "dodecahedron", "octahedron", nullptr },
660 static rvec visbox = { 0, 0, 0 };
661 static int resnr_start = -1;
663 { "-ndef", FALSE, etBOOL, { &bNDEF }, "Choose output from default index groups" },
668 "HIDDENVisualize a grid of boxes, -1 visualizes the 14 box images" },
669 { "-bt", FALSE, etENUM, { btype }, "Box type for [TT]-box[tt] and [TT]-d[tt]" },
670 { "-box", FALSE, etRVEC, { newbox }, "Box vector lengths (a,b,c)" },
671 { "-angles", FALSE, etRVEC, { newang }, "Angles between the box vectors (bc,ac,ab)" },
672 { "-d", FALSE, etREAL, { &dist }, "Distance between the solute and the box" },
677 "Center molecule in box (implied by [TT]-box[tt] and [TT]-d[tt])" },
678 { "-center", FALSE, etRVEC, { center }, "Shift the geometrical center to (x,y,z)" },
679 { "-aligncenter", FALSE, etRVEC, { aligncenter }, "Center of rotation for alignment" },
680 { "-align", FALSE, etRVEC, { targetvec }, "Align to target vector" },
681 { "-translate", FALSE, etRVEC, { translation }, "Translation" },
686 "Rotation around the X, Y and Z axes in degrees" },
687 { "-princ", FALSE, etBOOL, { &bOrient }, "Orient molecule(s) along their principal axes" },
688 { "-scale", FALSE, etRVEC, { scale }, "Scaling factor" },
693 "Density (g/L) of the output box achieved by scaling" },
694 { "-pbc", FALSE, etBOOL, { &bRMPBC }, "Remove the periodicity (make molecule whole again)" },
695 { "-resnr", FALSE, etINT, { &resnr_start }, " Renumber residues starting from resnr" },
700 "Store the charge of the atom in the B-factor field and the radius of the atom in the "
706 "Default Van der Waals radius (in nm) if one can not be found in the database or if no "
707 "parameters are present in the topology file" },
712 "Use rmin/2 (minimum in the Van der Waals potential) rather than [GRK]sigma[grk]/2 " },
717 "Read the Van der Waals radii from the file [TT]vdwradii.dat[tt] rather than computing "
718 "the radii based on the force field" },
719 { "-atom", FALSE, etBOOL, { &peratom }, "Force B-factor attachment per atom" },
720 { "-legend", FALSE, etBOOL, { &bLegend }, "Make B-factor legend" },
721 { "-label", FALSE, etSTR, { &label }, "Add chain label for all residues" },
726 "Add CONECT records to a [REF].pdb[ref] file when written. Can only be done when a "
727 "topology is present" }
729 #define NPA asize(pa)
732 const char * infile, *outfile;
733 int outftp, inftp, natom, i, j, n_bfac, itype, ntype;
734 double * bfac = nullptr, c6, c12;
735 int* bfac_nr = nullptr;
736 t_topology* top = nullptr;
737 char * grpname, *sgrpname, *agrpname;
738 int isize, ssize, numAlignmentAtoms;
739 int * index, *sindex, *aindex;
740 rvec * x, *v, gc, rmin, rmax, size;
742 matrix box, rotmatrix, trans;
744 gmx_bool bIndex, bSetSize, bSetAng, bDist, bSetCenter, bAlign;
745 gmx_bool bHaveV, bScale, bRho, bTranslate, bRotate, bCalcGeom, bCalcDiam;
746 real diam = 0, mass = 0, d, vdw;
748 gmx_output_env_t* oenv;
749 t_filenm fnm[] = { { efSTX, "-f", nullptr, ffREAD },
750 { efNDX, "-n", nullptr, ffOPTRD },
751 { efSTO, nullptr, nullptr, ffOPTWR },
752 { efPQR, "-mead", "mead", ffOPTWR },
753 { efDAT, "-bf", "bfact", ffOPTRD } };
754 #define NFILE asize(fnm)
756 if (!parse_common_args(
757 &argc, argv, PCA_CAN_VIEW, NFILE, fnm, NPA, pa, asize(desc), desc, asize(bugs), bugs, &oenv))
762 "Note that major changes are planned in future for "
763 "editconf, to improve usability and utility.\n");
765 bIndex = opt2bSet("-n", NFILE, fnm) || bNDEF;
766 bMead = opt2bSet("-mead", NFILE, fnm);
767 bSetSize = opt2parg_bSet("-box", NPA, pa);
768 bSetAng = opt2parg_bSet("-angles", NPA, pa);
769 bSetCenter = opt2parg_bSet("-center", NPA, pa);
770 bDist = opt2parg_bSet("-d", NPA, pa);
771 bAlign = opt2parg_bSet("-align", NPA, pa);
772 /* Only automatically turn on centering without -noc */
773 if ((bDist || bSetSize || bSetCenter) && !opt2parg_bSet("-c", NPA, pa))
777 bScale = opt2parg_bSet("-scale", NPA, pa);
778 bRho = opt2parg_bSet("-density", NPA, pa);
779 bTranslate = opt2parg_bSet("-translate", NPA, pa);
780 bRotate = opt2parg_bSet("-rotate", NPA, pa);
783 fprintf(stderr, "WARNING: setting -density overrides -scale\n");
785 bScale = bScale || bRho;
786 bCalcGeom = bCenter || bRotate || bOrient || bScale;
788 GMX_RELEASE_ASSERT(btype[0] != nullptr, "Option setting inconsistency; btype[0] is NULL");
790 bCalcDiam = (btype[0][0] == 'c' || btype[0][0] == 'd' || btype[0][0] == 'o');
792 infile = ftp2fn(efSTX, NFILE, fnm);
795 outfile = ftp2fn(efPQR, NFILE, fnm);
799 outfile = ftp2fn(efSTO, NFILE, fnm);
801 outftp = fn2ftp(outfile);
802 inftp = fn2ftp(infile);
808 printf("Incompatible options -mead and -grasp. Turning off -grasp\n");
811 if (bGrasp && (outftp != efPDB))
814 "Output file should be a .pdb file"
815 " when using the -grasp option\n");
817 if ((bMead || bGrasp) && (fn2ftp(infile) != efTPR))
820 "Input file should be a .tpr file"
821 " when using the -mead option\n");
827 open_symtab(&symtab);
828 readConfAndAtoms(infile, &symtab, &name, &atoms, &pbcType, &x, &v, box);
830 if (atoms.pdbinfo == nullptr)
832 snew(atoms.pdbinfo, atoms.nr);
834 atoms.havePdbInfo = TRUE;
836 if (fn2ftp(infile) == efPDB)
838 get_pdb_atomnumber(&atoms, &aps);
840 printf("Read %d atoms\n", atoms.nr);
842 /* Get the element numbers if available in a pdb file */
843 if (fn2ftp(infile) == efPDB)
845 get_pdb_atomnumber(&atoms, &aps);
848 if (pbcType != PbcType::No)
851 printf("Volume: %g nm^3, corresponds to roughly %d electrons\n",
853 100 * (static_cast<int>(vol * 4.5)));
856 if (bMead || bGrasp || bCONECT)
858 top = read_top(infile, nullptr);
863 if (atoms.nr != top->atoms.nr)
865 gmx_fatal(FARGS, "Atom numbers don't match (%d vs. %d)", atoms.nr, top->atoms.nr);
867 snew(atoms.pdbinfo, top->atoms.nr);
868 ntype = top->idef.atnr;
869 for (i = 0; (i < atoms.nr); i++)
871 /* Determine the Van der Waals radius from the force field */
874 if (!aps.setAtomProperty(epropVDW,
875 *top->atoms.resinfo[top->atoms.atom[i].resind].name,
876 *top->atoms.atomname[i],
884 itype = top->atoms.atom[i].type;
885 c12 = top->idef.iparams[itype * ntype + itype].lj.c12;
886 c6 = top->idef.iparams[itype * ntype + itype].lj.c6;
887 if ((c6 != 0) && (c12 != 0))
898 vdw = 0.5 * gmx::sixthroot(sig6);
905 /* Factor of 10 for nm -> Angstroms */
910 atoms.pdbinfo[i].occup = top->atoms.atom[i].q;
911 atoms.pdbinfo[i].bfac = vdw;
915 atoms.pdbinfo[i].occup = vdw;
916 atoms.pdbinfo[i].bfac = top->atoms.atom[i].q;
921 for (i = 0; (i < natom) && !bHaveV; i++)
923 for (j = 0; (j < DIM) && !bHaveV; j++)
925 bHaveV = bHaveV || (v[i][j] != 0);
928 printf("%selocities found\n", bHaveV ? "V" : "No v");
934 gmx_fatal(FARGS, "Sorry, can not visualize box with index groups");
938 gmx_fatal(FARGS, "Sorry, can only visualize box with a pdb file");
941 else if (visbox[0] == -1)
943 visualize_images("images.pdb", pbcType, box);
949 rm_gropbc(&atoms, x, box);
956 fprintf(stderr, "\nSelect a group for determining the system size:\n");
957 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &ssize, &sindex, &sgrpname);
964 diam = calc_geom(ssize, sindex, x, gc, rmin, rmax, bCalcDiam);
965 rvec_sub(rmax, rmin, size);
966 printf(" system size :%7.3f%7.3f%7.3f (nm)\n", size[XX], size[YY], size[ZZ]);
969 printf(" diameter :%7.3f (nm)\n", diam);
971 printf(" center :%7.3f%7.3f%7.3f (nm)\n", gc[XX], gc[YY], gc[ZZ]);
972 printf(" box vectors :%7.3f%7.3f%7.3f (nm)\n", norm(box[XX]), norm(box[YY]), norm(box[ZZ]));
973 printf(" box angles :%7.2f%7.2f%7.2f (degrees)\n",
974 norm2(box[ZZ]) == 0 ? 0 : gmx::c_rad2Deg * gmx_angle(box[YY], box[ZZ]),
975 norm2(box[ZZ]) == 0 ? 0 : gmx::c_rad2Deg * gmx_angle(box[XX], box[ZZ]),
976 norm2(box[YY]) == 0 ? 0 : gmx::c_rad2Deg * gmx_angle(box[XX], box[YY]));
977 printf(" box volume :%7.2f (nm^3)\n", det(box));
980 if (bRho || bOrient || bAlign)
982 mass = calc_mass(&atoms, !fn2bTPX(infile), &aps);
990 /* Get a group for principal component analysis */
991 fprintf(stderr, "\nSelect group for the determining the orientation\n");
992 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &isize, &index, &grpnames);
994 /* Orient the principal axes along the coordinate axes */
995 orient_princ(&atoms, isize, index, natom, x, bHaveV ? v : nullptr, nullptr);
1002 /* scale coordinates and box */
1005 /* Compute scaling constant */
1009 dens = (mass * gmx::c_amu) / (vol * gmx::c_nano * gmx::c_nano * gmx::c_nano);
1010 fprintf(stderr, "Volume of input %g (nm^3)\n", vol);
1011 fprintf(stderr, "Mass of input %g (a.m.u.)\n", mass);
1012 fprintf(stderr, "Density of input %g (g/l)\n", dens);
1013 if (vol == 0 || mass == 0)
1016 "Cannot scale density with "
1017 "zero mass (%g) or volume (%g)\n",
1022 scale[XX] = scale[YY] = scale[ZZ] = std::cbrt(dens / rho);
1023 fprintf(stderr, "Scaling all box vectors by %g\n", scale[XX]);
1025 scale_conf(atoms.nr, x, box, scale);
1032 fprintf(stderr, "\nSelect a group that you want to align:\n");
1033 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &numAlignmentAtoms, &aindex, &agrpname);
1037 numAlignmentAtoms = atoms.nr;
1038 snew(aindex, numAlignmentAtoms);
1039 for (i = 0; i < numAlignmentAtoms; i++)
1044 printf("Aligning %d atoms (out of %d) to %g %g %g, center of rotation %g %g %g\n",
1053 /*subtract out pivot point*/
1054 for (i = 0; i < numAlignmentAtoms; i++)
1056 rvec_dec(x[aindex[i]], aligncenter);
1058 /*now determine transform and rotate*/
1060 principal_comp(numAlignmentAtoms, aindex, atoms.atom, x, trans, princd);
1062 unitv(targetvec, targetvec);
1063 printf("Using %g %g %g as principal axis\n", trans[0][2], trans[1][2], trans[2][2]);
1064 tmpvec[XX] = trans[0][2];
1065 tmpvec[YY] = trans[1][2];
1066 tmpvec[ZZ] = trans[2][2];
1067 calc_rotmatrix(tmpvec, targetvec, rotmatrix);
1068 /* rotmatrix finished */
1070 for (i = 0; i < numAlignmentAtoms; ++i)
1072 mvmul(rotmatrix, x[aindex[i]], tmpvec);
1073 copy_rvec(tmpvec, x[aindex[i]]);
1076 /*add pivot point back*/
1077 for (i = 0; i < numAlignmentAtoms; i++)
1079 rvec_inc(x[aindex[i]], aligncenter);
1091 fprintf(stderr, "\nSelect a group that you want to translate:\n");
1092 get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &ssize, &sindex, &sgrpname);
1099 printf("Translating %d atoms (out of %d) by %g %g %g nm\n",
1107 for (i = 0; i < ssize; i++)
1109 rvec_inc(x[sindex[i]], translation);
1114 for (i = 0; i < natom; i++)
1116 rvec_inc(x[i], translation);
1123 printf("Rotating %g, %g, %g degrees around the X, Y and Z axis respectively\n",
1127 for (i = 0; i < DIM; i++)
1129 rotangles[i] *= gmx::c_deg2Rad;
1131 rotate_conf(natom, x, v, rotangles[XX], rotangles[YY], rotangles[ZZ]);
1136 /* recalc geometrical center and max and min coordinates and size */
1137 calc_geom(ssize, sindex, x, gc, rmin, rmax, FALSE);
1138 rvec_sub(rmax, rmin, size);
1139 if (bScale || bOrient || bRotate)
1141 printf("new system size : %6.3f %6.3f %6.3f\n", size[XX], size[YY], size[ZZ]);
1145 if ((btype[0] != nullptr) && (bSetSize || bDist || (btype[0][0] == 't' && bSetAng)))
1147 pbcType = PbcType::Xyz;
1148 if (!(bSetSize || bDist))
1150 for (i = 0; i < DIM; i++)
1152 newbox[i] = norm(box[i]);
1156 /* calculate new boxsize */
1157 switch (btype[0][0])
1162 for (i = 0; i < DIM; i++)
1164 newbox[i] = size[i] + 2 * dist;
1169 box[XX][XX] = newbox[XX];
1170 box[YY][YY] = newbox[YY];
1171 box[ZZ][ZZ] = newbox[ZZ];
1175 matrix_convert(box, newbox, newang);
1187 d = diam + 2 * dist;
1189 if (btype[0][0] == 'c')
1191 for (i = 0; i < DIM; i++)
1196 else if (btype[0][0] == 'd')
1200 box[ZZ][XX] = d / 2;
1201 box[ZZ][YY] = d / 2;
1202 box[ZZ][ZZ] = d * std::sqrt(2.0) / 2.0;
1207 box[YY][XX] = d / 3;
1208 box[YY][YY] = d * std::sqrt(2.0) * 2.0 / 3.0;
1209 box[ZZ][XX] = -d / 3;
1210 box[ZZ][YY] = d * std::sqrt(2.0) / 3.0;
1211 box[ZZ][ZZ] = d * std::sqrt(6.0) / 3.0;
1217 /* calculate new coords for geometrical center */
1220 calc_box_center(ecenterDEF, box, center);
1223 /* center molecule on 'center' */
1226 center_conf(natom, x, center, gc);
1232 calc_geom(ssize, sindex, x, gc, rmin, rmax, FALSE);
1233 printf("new center :%7.3f%7.3f%7.3f (nm)\n", gc[XX], gc[YY], gc[ZZ]);
1235 if (bOrient || bScale || bDist || bSetSize)
1237 printf("new box vectors :%7.3f%7.3f%7.3f (nm)\n", norm(box[XX]), norm(box[YY]), norm(box[ZZ]));
1238 printf("new box angles :%7.2f%7.2f%7.2f (degrees)\n",
1239 norm2(box[ZZ]) == 0 ? 0 : gmx::c_rad2Deg * gmx_angle(box[YY], box[ZZ]),
1240 norm2(box[ZZ]) == 0 ? 0 : gmx::c_rad2Deg * gmx_angle(box[XX], box[ZZ]),
1241 norm2(box[YY]) == 0 ? 0 : gmx::c_rad2Deg * gmx_angle(box[XX], box[YY]));
1242 printf("new box volume :%7.2f (nm^3)\n", det(box));
1245 if (check_box(PbcType::Xyz, box))
1247 printf("\nWARNING: %s\n"
1248 "See the GROMACS manual for a description of the requirements that\n"
1249 "must be satisfied by descriptions of simulation cells.\n",
1250 check_box(PbcType::Xyz, box));
1253 if (bDist && btype[0][0] == 't')
1257 printf("\nWARNING: Your box is triclinic with non-orthogonal axes. In this case, the\n"
1258 "distance from the solute to a box surface along the corresponding normal\n"
1259 "vector might be somewhat smaller than your specified value %f.\n"
1260 "You can check the actual value with g_mindist -pi\n",
1263 else if (!opt2parg_bSet("-bt", NPA, pa))
1265 printf("\nWARNING: No boxtype specified - distance condition applied in each "
1267 "If the molecule rotates the actual distance will be smaller. You might want\n"
1268 "to use a cubic box instead, or why not try a dodecahedron today?\n");
1271 if (bCONECT && (outftp == efPDB) && (inftp == efTPR))
1273 conect = gmx_conect_generate(top);
1282 fprintf(stderr, "\nSelect a group for output:\n");
1283 get_index(&atoms, opt2fn_null("-n", NFILE, fnm), 1, &isize, &index, &grpname);
1285 if (resnr_start >= 0)
1287 renum_resnr(&atoms, isize, index, resnr_start);
1290 if (opt2parg_bSet("-label", NPA, pa))
1292 for (i = 0; (i < atoms.nr); i++)
1294 atoms.resinfo[atoms.atom[i].resind].chainid = label[0];
1298 if (opt2bSet("-bf", NFILE, fnm) || bLegend)
1300 gmx_fatal(FARGS, "Sorry, cannot do bfactors with an index group.");
1303 if (outftp == efPDB)
1305 out = gmx_ffopen(outfile, "w");
1306 write_pdbfile_indexed(out, name, &atoms, x, pbcType, box, ' ', 1, isize, index, conect, FALSE);
1311 write_sto_conf_indexed(
1312 outfile, name, &atoms, x, bHaveV ? v : nullptr, pbcType, box, isize, index);
1319 if (resnr_start >= 0)
1321 renum_resnr(&atoms, atoms.nr, nullptr, resnr_start);
1324 if ((outftp == efPDB) || (outftp == efPQR))
1326 out = gmx_ffopen(outfile, "w");
1331 "The B-factors in this file hold atomic radii\n");
1334 "The occupancy in this file hold atomic charges\n");
1338 fprintf(out, "GRASP PDB FILE\nFORMAT NUMBER=1\n");
1341 "The B-factors in this file hold atomic charges\n");
1344 "The occupancy in this file hold atomic radii\n");
1346 else if (opt2bSet("-bf", NFILE, fnm))
1348 read_bfac(opt2fn("-bf", NFILE, fnm), &n_bfac, &bfac, &bfac_nr);
1349 set_pdb_conf_bfac(atoms.nr, atoms.nres, &atoms, n_bfac, bfac, bfac_nr, peratom);
1351 if (opt2parg_bSet("-label", NPA, pa))
1353 for (i = 0; (i < atoms.nr); i++)
1355 atoms.resinfo[atoms.atom[i].resind].chainid = label[0];
1358 /* Need to bypass the regular write_pdbfile because I don't want to change
1359 * all instances to include the boolean flag for writing out PQR files.
1362 snew(index, atoms.nr);
1363 for (int i = 0; i < atoms.nr; i++)
1367 write_pdbfile_indexed(
1368 out, name, &atoms, x, pbcType, box, ' ', -1, atoms.nr, index, conect, outftp == efPQR);
1372 pdb_legend(out, atoms.nr, atoms.nres, &atoms, x);
1377 bLegend ? atoms.nr + 12 : atoms.nr,
1378 bLegend ? atoms.nres = 12 : atoms.nres,
1386 write_sto_conf(outfile, name, &atoms, x, bHaveV ? v : nullptr, pbcType, box);
1390 done_symtab(&symtab);
1400 do_view(oenv, outfile, nullptr);
1401 output_env_done(oenv);