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.
45 #include "gromacs/fileio/espio.h"
46 #include "gromacs/fileio/filetypes.h"
47 #include "gromacs/fileio/g96io.h"
48 #include "gromacs/fileio/gmxfio.h"
49 #include "gromacs/fileio/groio.h"
50 #include "gromacs/fileio/pdbio.h"
51 #include "gromacs/fileio/tpxio.h"
52 #include "gromacs/fileio/trxio.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/topology/atoms.h"
55 #include "gromacs/topology/block.h"
56 #include "gromacs/topology/mtop_util.h"
57 #include "gromacs/topology/symtab.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/trajectory/trajectoryframe.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/smalloc.h"
65 void write_sto_conf_indexed(const char* outfile,
79 ftp = fn2ftp(outfile);
83 out = gmx_fio_fopen(outfile, "w");
84 write_hconf_indexed_p(out, title, atoms, nindex, index, x, v, box);
88 clear_trxframe(&fr, TRUE);
89 fr.natoms = atoms->nr;
91 fr.atoms = const_cast<t_atoms*>(atoms);
93 fr.x = const_cast<rvec*>(x);
97 fr.v = const_cast<rvec*>(v);
100 copy_mat(box, fr.box);
101 out = gmx_fio_fopen(outfile, "w");
102 write_g96_conf(out, title, &fr, nindex, index);
109 out = gmx_fio_fopen(outfile, "w");
110 write_pdbfile_indexed(out, title, atoms, x, ePBC, box, ' ', -1, nindex, index, nullptr,
115 out = gmx_fio_fopen(outfile, "w");
116 write_espresso_conf_indexed(out, title, atoms, nindex, index, x, v, box);
119 case efTPR: gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
120 default: gmx_incons("Not supported in write_sto_conf_indexed");
124 void write_sto_conf(const char* outfile,
126 const t_atoms* atoms,
136 ftp = fn2ftp(outfile);
139 case efGRO: write_conf_p(outfile, title, atoms, x, v, box); break;
141 clear_trxframe(&fr, TRUE);
142 fr.natoms = atoms->nr;
144 fr.atoms = const_cast<t_atoms*>(atoms); // TODO check
146 fr.x = const_cast<rvec*>(x);
150 fr.v = const_cast<rvec*>(v);
153 copy_mat(box, fr.box);
154 out = gmx_fio_fopen(outfile, "w");
155 write_g96_conf(out, title, &fr, -1, nullptr);
161 out = gmx_fio_fopen(outfile, "w");
162 write_pdbfile(out, title, atoms, x, ePBC, box, ' ', -1, nullptr);
166 out = gmx_fio_fopen(outfile, "w");
167 write_espresso_conf_indexed(out, title, atoms, atoms->nr, nullptr, x, v, box);
170 case efTPR: gmx_fatal(FARGS, "Sorry, can not write a topology to %s", outfile);
171 default: gmx_incons("Not supported in write_sto_conf");
175 void write_sto_conf_mtop(const char* outfile,
177 const gmx_mtop_t* mtop,
187 ftp = fn2ftp(outfile);
191 out = gmx_fio_fopen(outfile, "w");
192 write_hconf_mtop(out, title, mtop, x, v, box);
196 /* This is a brute force approach which requires a lot of memory.
197 * We should implement mtop versions of all writing routines.
199 atoms = gmx_mtop_global_atoms(mtop);
201 write_sto_conf(outfile, title, &atoms, x, v, ePBC, box);
208 static void get_stx_coordnum(const char* infile, int* natoms)
213 char g96_line[STRLEN + 1];
215 ftp = fn2ftp(infile);
216 range_check(ftp, 0, efNR);
219 case efGRO: get_coordnum(infile, natoms); break;
222 in = gmx_fio_fopen(infile, "r");
228 *natoms = read_g96_conf(in, infile, nullptr, &fr, nullptr, g96_line);
235 in = gmx_fio_fopen(infile, "r");
236 get_pdb_coordnum(in, natoms);
239 case efESP: *natoms = get_espresso_coordnum(infile); break;
240 default: gmx_fatal(FARGS, "File type %s not supported in get_stx_coordnum", ftp2ext(ftp));
244 //! Constructs plausible chain IDs for multi-molecule systems, e.g. when read from .tpr files
248 //! Fill in the chain ID for the indicated atom range, which might be a molecule.
249 void fill(t_atoms* atoms, int startAtomIndex, int endAtomIndex);
250 //! If only one chain was found, we don't add a chain ID.
251 void clearIfNeeded(t_atoms* atoms) const;
254 //! Minimum size for a chain worth giving an ID
255 static constexpr int s_chainMinAtoms = 15;
257 //! The number of the next chain that will be assigned.
258 int nextChainNumber_ = 0;
259 //! The chain ID of the next chain that will be assigned.
260 char nextChainId_ = 'A';
261 //! Whether the set of chain IDs (ie. upper- and lower-case letters and single digits) is exhausted.
262 bool outOfIds_ = false;
265 void ChainIdFiller::fill(t_atoms* atoms, const int startAtomIndex, const int endAtomIndex)
267 // TODO remove these some time, extra braces added for review convenience
269 // We always assign a new chain number, but only assign a chain id
270 // characters for larger molecules.
272 if (endAtomIndex - startAtomIndex >= s_chainMinAtoms && !outOfIds_)
274 /* Set the chain id for the output */
275 chainIdToAssign = nextChainId_;
276 /* Here we allow for the max possible 2*26+10=62 chain ids */
277 if (nextChainId_ == 'Z')
281 else if (nextChainId_ == 'z')
285 else if (nextChainId_ == '9')
296 chainIdToAssign = ' ';
298 for (int a = startAtomIndex; a < endAtomIndex; a++)
300 atoms->resinfo[atoms->atom[a].resind].chainnum = nextChainNumber_;
301 atoms->resinfo[atoms->atom[a].resind].chainid = chainIdToAssign;
307 void ChainIdFiller::clearIfNeeded(t_atoms* atoms) const
309 /* Blank out the chain id if there was only one chain */
310 if (nextChainId_ == 'B')
312 for (int r = 0; r < atoms->nres; r++)
314 atoms->resinfo[r].chainid = ' ';
319 //! Make chain IDs in the t_atoms for a gmx_mtop_t built from a .tpr file
320 static void makeChainIdentifiersAfterTprReading(t_atoms* atoms, const gmx::RangePartitioning& mols)
322 ChainIdFiller filler;
323 for (auto m = 0; m != mols.numBlocks(); ++m)
325 filler.fill(atoms, mols.block(m).begin(), mols.block(m).end());
327 filler.clearIfNeeded(atoms);
330 //! Make chain IDs in the t_atoms for a legacy t_topology built from a .tpr file
331 static void tpx_make_chain_identifiers(t_atoms* atoms, const t_block* mols)
333 ChainIdFiller filler;
334 for (int m = 0; m < mols->nr; m++)
336 filler.fill(atoms, mols->index[m], mols->index[m + 1]);
338 filler.clearIfNeeded(atoms);
341 static void read_stx_conf(const char* infile,
353 char g96_line[STRLEN + 1];
357 fprintf(stderr, "Warning: Number of atoms in %s is 0\n", infile);
359 else if (atoms->atom == nullptr)
361 gmx_mem("Uninitialized array atom");
369 ftp = fn2ftp(infile);
372 case efGRO: gmx_gro_read_conf(infile, symtab, name, atoms, x, v, box); break;
374 fr.natoms = atoms->nr;
379 in = gmx_fio_fopen(infile, "r");
380 read_g96_conf(in, infile, name, &fr, symtab, g96_line);
382 copy_mat(fr.box, box);
386 case efENT: gmx_pdb_read_conf(infile, symtab, name, atoms, x, ePBC, box); break;
387 case efESP: gmx_espresso_read_conf(infile, symtab, name, atoms, x, v, box); break;
388 default: gmx_incons("Not supported in read_stx_conf");
392 void readConfAndAtoms(const char* infile,
401 GMX_RELEASE_ASSERT(infile, "Need a valid file name string");
403 if (fn2ftp(infile) == efTPR)
407 readConfAndTopology(infile, &haveTopology, &mtop, ePBC, x, v, box);
408 *symtab = mtop.symtab;
409 *name = gmx_strdup(*mtop.name);
410 *atoms = gmx_mtop_global_atoms(&mtop);
411 gmx::RangePartitioning molecules = gmx_mtop_molecules(mtop);
412 makeChainIdentifiersAfterTprReading(atoms, molecules);
414 /* Inelegant solution to avoid all char pointers in atoms becoming
415 * invalid after destruction of mtop.
416 * This will be fixed soon by converting t_symtab to C++.
418 mtop.symtab.symbuf = nullptr;
425 get_stx_coordnum(infile, &natoms);
427 init_t_atoms(atoms, natoms, (fn2ftp(infile) == efPDB));
429 bool xIsNull = false;
440 read_stx_conf(infile, symtab, name, atoms, *x, (v == nullptr) ? nullptr : *v, ePBC, box);
448 void readConfAndTopology(const char* infile, bool* haveTopology, gmx_mtop_t* mtop, int* ePBC, rvec** x, rvec** v, matrix box)
450 GMX_RELEASE_ASSERT(mtop != nullptr, "readConfAndTopology requires mtop!=NULL");
457 *haveTopology = fn2bTPX(infile);
460 TpxFileHeader header = readTpxHeader(infile, true);
463 snew(*x, header.natoms);
467 snew(*v, header.natoms);
470 int ePBC_tmp = read_tpx(infile, nullptr, box, &natoms, (x == nullptr) ? nullptr : *x,
471 (v == nullptr) ? nullptr : *v, mtop);
483 open_symtab(&symtab);
485 readConfAndAtoms(infile, &symtab, &name, &atoms, ePBC, x, v, box);
487 convertAtomsToMtop(&symtab, put_symtab(&symtab, name), &atoms, mtop);
492 gmx_bool read_tps_conf(const char* infile, t_topology* top, int* ePBC, rvec** x, rvec** v, matrix box, gmx_bool requireMasses)
497 readConfAndTopology(infile, &haveTopology, &mtop, ePBC, x, v, box);
499 *top = gmx_mtop_t_to_t_topology(&mtop, true);
501 tpx_make_chain_identifiers(&top->atoms, &top->mols);
503 if (requireMasses && !top->atoms.haveMass)
505 atomsSetMassesBasedOnNames(&top->atoms, TRUE);
507 if (!top->atoms.haveMass)
510 "Masses were requested, but for some atom(s) masses could not be found in "
511 "the database. Use a tpr file as input, if possible, or add these atoms to "
512 "the mass database.");