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,2017,2018 by the GROMACS development team.
7 * Copyright (c) 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.
51 #include "gromacs/fileio/gmxfio.h"
52 #include "gromacs/gmxpreprocess/fflibutil.h"
53 #include "gromacs/gmxpreprocess/gpp_atomtype.h"
54 #include "gromacs/gmxpreprocess/grompp_impl.h"
55 #include "gromacs/gmxpreprocess/h_db.h"
56 #include "gromacs/gmxpreprocess/notset.h"
57 #include "gromacs/gmxpreprocess/toputil.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/enumerationhelpers.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/futil.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/strdb.h"
64 #include "gromacs/utility/stringtoenumvalueconverter.h"
66 #include "hackblock.h"
69 /* use bonded types definitions in hackblock.h */
70 enum class ReplaceType : int
78 static const char* enumValueToString(ReplaceType enumValue)
80 constexpr gmx::EnumerationArray<ReplaceType, const char*> replaceTypeNames = { "replace",
83 return replaceTypeNames[enumValue];
86 template<typename EnumType>
87 static std::optional<EnumType> findTypeFromKeyword(char* keyw)
89 gmx::StringToEnumValueConverter<EnumType, enumValueToString, gmx::StringCompareType::CaseInsensitive, gmx::StripStrings::Yes> converter;
90 return converter.valueFrom(keyw);
93 static void read_atom(char* line, bool bAdd, std::string* nname, t_atom* a, PreprocessingAtomTypes* atype, int* cgnr)
99 /* This code is messy, because of support for different formats:
100 * for replace: [new name] <atom type> <m> <q> [cgnr (old format)]
101 * for add: <atom type> <m> <q> [cgnr]
103 nr = sscanf(line, "%s %s %s %s %s", buf[0], buf[1], buf[2], buf[3], buf[4]);
105 /* Here there an ambiguity due to the old replace format with cgnr,
106 * which was read for years, but ignored in the rest of the code.
107 * We have to assume that the atom type does not start with a digit
108 * to make a line with 4 entries uniquely interpretable.
110 if (!bAdd && nr == 4 && isdigit(buf[1][0]))
115 if (nr < 3 || nr > 4)
118 "Reading Termini Database: expected %d or %d items of atom data in stead of %d "
137 a->type = *atype->atomTypeFromName(buf[i++]);
138 sscanf(buf[i++], "%lf", &m);
140 sscanf(buf[i++], "%lf", &q);
144 sscanf(buf[i++], "%d", cgnr);
152 static void print_atom(FILE* out, const t_atom& a, PreprocessingAtomTypes* atype)
154 fprintf(out, "\t%s\t%g\t%g\n", *atype->atomNameFromAtomType(a.type), a.m, a.q);
157 static void print_ter_db(const char* ff,
159 gmx::ArrayRef<const MoleculePatchDatabase> tb,
160 PreprocessingAtomTypes* atype)
162 std::string buf = gmx::formatString("%s-%c.tdb", ff, C);
163 FILE* out = gmx_fio_fopen(buf.c_str(), "w");
165 for (const auto& modification : tb)
167 fprintf(out, "[ %s ]\n", modification.name.c_str());
169 if (std::any_of(modification.hack.begin(), modification.hack.end(), [](const auto& mod) {
170 return mod.type() == MoleculePatchType::Replace;
173 fprintf(out, "[ %s ]\n", enumValueToString(ReplaceType::Repl));
174 for (const auto& hack : modification.hack)
176 if (hack.type() == MoleculePatchType::Replace)
178 fprintf(out, "%s\t", hack.oname.c_str());
179 print_atom(out, hack.atom.back(), atype);
183 if (std::any_of(modification.hack.begin(), modification.hack.end(), [](const auto& mod) {
184 return mod.type() == MoleculePatchType::Add;
187 fprintf(out, "[ %s ]\n", enumValueToString(ReplaceType::Add));
188 for (const auto& hack : modification.hack)
190 if (hack.type() == MoleculePatchType::Add)
192 print_ab(out, hack, hack.nname.c_str());
193 print_atom(out, hack.atom.back(), atype);
197 if (std::any_of(modification.hack.begin(), modification.hack.end(), [](const auto& mod) {
198 return mod.type() == MoleculePatchType::Delete;
201 fprintf(out, "[ %s ]\n", enumValueToString(ReplaceType::Del));
202 for (const auto& hack : modification.hack)
204 if (hack.type() == MoleculePatchType::Delete)
206 fprintf(out, "%s\n", hack.oname.c_str());
210 for (auto bt : gmx::EnumerationWrapper<BondedTypes>{})
212 if (!modification.rb[bt].b.empty())
214 fprintf(out, "[ %s ]\n", enumValueToString(bt));
215 for (const auto& b : modification.rb[bt].b)
217 for (int k = 0; k < enumValueToNumIAtoms(bt); k++)
219 fprintf(out, "%s%s", k ? "\t" : "", b.a[k].c_str());
223 fprintf(out, "\t%s", b.s.c_str());
234 static void read_ter_db_file(const char* fn,
235 std::vector<MoleculePatchDatabase>* tbptr,
236 PreprocessingAtomTypes* atype)
238 char filebase[STRLEN];
239 char header[STRLEN], buf[STRLEN], line[STRLEN];
241 fflib_filename_base(fn, filebase, STRLEN);
242 /* Remove the C/N termini extension */
243 char* ptr = strrchr(filebase, '.');
249 FILE* in = fflib_open(fn);
251 std::optional<BondedTypes> btkw;
252 std::optional<ReplaceType> rtkw;
253 get_a_line(in, line, STRLEN);
254 MoleculePatchDatabase* block = nullptr;
257 if (get_header(line, header))
259 /* this is a new block, or a new keyword */
260 btkw = findTypeFromKeyword<BondedTypes>(header);
261 rtkw = findTypeFromKeyword<ReplaceType>(header);
263 if (!btkw.has_value() && !rtkw.has_value())
265 tbptr->emplace_back(MoleculePatchDatabase());
266 block = &tbptr->back();
267 clearModificationBlock(block);
268 block->name = header;
269 block->filebase = filebase;
274 if (block == nullptr)
277 "reading termini database: "
278 "directive expected before line:\n%s\n"
279 "This might be a file in an old format.",
282 /* this is not a header, so it must be data */
283 if (!btkw.has_value())
285 /* this is a hack: add/rename/delete atoms */
286 /* make space for hacks */
287 block->hack.emplace_back(MoleculePatch());
288 MoleculePatch* hack = &block->hack.back();
292 if (*rtkw == ReplaceType::Repl || *rtkw == ReplaceType::Del)
294 if (sscanf(line, "%s%n", buf, &n) != 1)
297 "Reading Termini Database '%s': "
298 "expected atom name on line\n%s",
303 /* we only replace or delete one atom at a time */
306 else if (*rtkw == ReplaceType::Add)
308 read_ab(line, fn, hack);
309 get_a_line(in, line, STRLEN);
314 "unimplemented keyword number %d (%s:%d)",
315 static_cast<int>(*rtkw),
319 if (*rtkw == ReplaceType::Repl || *rtkw == ReplaceType::Add)
321 hack->atom.emplace_back();
322 read_atom(line + n, *rtkw == ReplaceType::Add, &hack->nname, &hack->atom.back(), atype, &hack->cgnr);
323 if (hack->nname.empty())
325 if (!hack->oname.empty())
327 hack->nname = hack->oname;
332 "Reading Termini Database '%s': don't know which name the "
333 "new atom should have on line\n%s",
340 else if (*btkw >= BondedTypes::Bonds && *btkw < BondedTypes::Count)
342 /* this is bonded data: bonds, angles, dihedrals or impropers */
344 block->rb[*btkw].b.emplace_back();
345 BondedInteraction* newBond = &block->rb[*btkw].b.back();
346 for (int j = 0; j < enumValueToNumIAtoms(*btkw); j++)
349 if (sscanf(line + n, "%s%n", buf, &ni) == 1)
356 "Reading Termini Database '%s': expected %d atom names (found "
359 enumValueToNumIAtoms(*btkw),
366 sscanf(line + n, "%s", buf);
372 "Reading Termini Database: Expecting a header at line\n"
377 get_a_line(in, line, STRLEN);
383 int read_ter_db(const char* ffdir, char ter, std::vector<MoleculePatchDatabase>* tbptr, PreprocessingAtomTypes* atype)
385 std::string ext = gmx::formatString(".%c.tdb", ter);
387 /* Search for termini database files.
388 * Do not generate an error when none are found.
390 std::vector<std::string> tdbf = fflib_search_file_end(ffdir, ext.c_str(), FALSE);
392 for (const auto& filename : tdbf)
394 read_ter_db_file(filename.c_str(), tbptr, atype);
399 print_ter_db("new", ter, *tbptr, atype);
402 return tbptr->size();
405 std::vector<MoleculePatchDatabase*> filter_ter(gmx::ArrayRef<MoleculePatchDatabase> tb, const char* resname)
407 // TODO Four years later, no force fields have ever used this, so decide status of this feature
408 /* Since some force fields (e.g. OPLS) needs different
409 * atomtypes for different residues there could be a lot
410 * of entries in the databases for specific residues
411 * (e.g. GLY-NH3+, SER-NH3+, ALA-NH3+).
413 * To reduce the database size, we assume that a terminus specifier liker
415 * [ GLY|SER|ALA-NH3+ ]
417 * would cover all of the three residue types above.
418 * Wildcards (*,?) are not OK. Don't worry about e.g. GLU vs. GLUH since
419 * pdb2gmx only uses the first 3 letters when calling this routine.
421 * To automate this, this routines scans a list of termini
422 * for the residue name "resname" and returns an allocated list of
423 * pointers to the termini that could be applied to the
424 * residue in question. The variable pointed to by nret will
425 * contain the number of valid pointers in the list.
426 * Remember to free the list when you are done with it...
429 auto none_idx = tb.end();
430 std::vector<MoleculePatchDatabase*> list;
432 for (auto it = tb.begin(); it != tb.end(); it++)
434 const char* s = it->name.c_str();
438 if (gmx::equalCaseInsensitive(resname, s, 3))
441 list.push_back(&*it);
445 /* advance to next |-separated field */
452 } while (!found && s != nullptr);
455 /* All residue-specific termini have been added. We might have to fall
456 * back on generic termini, which are characterized by not having
457 * '-' in the name prior to the last position (which indicates charge).
458 * The [ None ] alternative is special since we don't want that
459 * to be the default, so we put it last in the list we return.
461 for (auto it = tb.begin(); it != tb.end(); it++)
463 const char* s = it->name.c_str();
464 if (gmx::equalCaseInsensitive("None", it->name))
470 /* Time to see if there's a generic terminus that matches.
471 Is there a hyphen? */
472 const char* c = strchr(s, '-');
474 /* A conjunction hyphen normally indicates a residue-specific
475 terminus, which is named like "GLY-COOH". A generic terminus
476 won't have a hyphen. */
477 bool bFoundAnyHyphen = (c != nullptr);
478 /* '-' as the last character indicates charge, so if that's
479 the only one found e.g. "COO-", then it was not a conjunction
480 hyphen, so this is a generic terminus */
481 bool bOnlyFoundChargeHyphen = (bFoundAnyHyphen && *(c + 1) == '\0');
482 /* Thus, "GLY-COO-" is not recognized as a generic terminus. */
483 bool bFoundGenericTerminus = !bFoundAnyHyphen || bOnlyFoundChargeHyphen;
484 if (bFoundGenericTerminus)
486 /* Check that we haven't already added a residue-specific version
489 auto found = std::find_if(list.begin(), list.end(), [&s](const MoleculePatchDatabase* b) {
490 return strstr(b->name.c_str(), s) != nullptr;
492 if (found == list.end())
494 list.push_back(&*it);
499 if (none_idx != tb.end())
501 list.push_back(&*none_idx);
508 MoleculePatchDatabase* choose_ter(gmx::ArrayRef<MoleculePatchDatabase*> tb, const char* title)
512 printf("%s\n", title);
514 for (const auto& modification : tb)
516 bool bIsZwitterion = (0 == gmx_wcmatch("*ZWITTERION*", modification->name.c_str()));
517 printf("%2d: %s%s\n",
519 modification->name.c_str(),
520 bIsZwitterion ? " (only use with zwitterions containing exactly one residue)" : "");
525 ret = fscanf(stdin, "%d", &sel);
526 } while ((ret != 1) || (sel < 0) || (sel >= tb.ssize()));