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,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/gmxpreprocess/fflibutil.h"
49 #include "gromacs/gmxpreprocess/gpp_atomtype.h"
50 #include "gromacs/gmxpreprocess/grompp-impl.h"
51 #include "gromacs/gmxpreprocess/h_db.h"
52 #include "gromacs/gmxpreprocess/hackblock.h"
53 #include "gromacs/gmxpreprocess/notset.h"
54 #include "gromacs/gmxpreprocess/resall.h"
55 #include "gromacs/gmxpreprocess/toputil.h"
56 #include "gromacs/utility/cstringutil.h"
57 #include "gromacs/utility/fatalerror.h"
58 #include "gromacs/utility/futil.h"
59 #include "gromacs/utility/smalloc.h"
60 #include "gromacs/utility/strdb.h"
62 /* use bonded types definitions in hackblock.h */
63 #define ekwRepl (ebtsNR+1)
64 #define ekwAdd (ebtsNR+2)
65 #define ekwDel (ebtsNR+3)
67 static const char *kw_names[ekwNR] = {
68 "replace", "add", "delete"
71 static int find_kw(char *keyw)
75 for (i = 0; i < ebtsNR; i++)
77 if (gmx_strcasecmp(btsNames[i], keyw) == 0)
82 for (i = 0; i < ekwNR; i++)
84 if (gmx_strcasecmp(kw_names[i], keyw) == 0)
86 return ebtsNR + 1 + i;
93 #define FATAL() gmx_fatal(FARGS, "Reading Termini Database: not enough items on line\n%s", line)
95 static void read_atom(char *line, bool bAdd,
96 char **nname, t_atom *a, gpp_atomtype *atype, int *cgnr)
102 /* This code is messy, because of support for different formats:
103 * for replace: [new name] <atom type> <m> <q> [cgnr (old format)]
104 * for add: <atom type> <m> <q> [cgnr]
106 nr = sscanf(line, "%s %s %s %s %s", buf[0], buf[1], buf[2], buf[3], buf[4]);
108 /* Here there an ambiguity due to the old replace format with cgnr,
109 * which was read for years, but ignored in the rest of the code.
110 * We have to assume that the atom type does not start with a digit
111 * to make a line with 4 entries uniquely interpretable.
113 if (!bAdd && nr == 4 && isdigit(buf[1][0]))
118 if (nr < 3 || nr > 4)
120 gmx_fatal(FARGS, "Reading Termini Database: expected %d or %d items of atom data in stead of %d on line\n%s", 3, 4, nr, line);
127 *nname = gmx_strdup(buf[i++]);
134 a->type = get_atomtype_type(buf[i++], atype);
135 sscanf(buf[i++], "%lf", &m);
137 sscanf(buf[i++], "%lf", &q);
141 sscanf(buf[i++], "%d", cgnr);
149 static void print_atom(FILE *out, t_atom *a, gpp_atomtype *atype)
151 fprintf(out, "\t%s\t%g\t%g\n",
152 get_atomtype_name(a->type, atype), a->m, a->q);
155 static void print_ter_db(const char *ff, char C, int nb, t_hackblock tb[],
159 int i, j, k, bt, nrepl, nadd, ndel;
162 sprintf(buf, "%s-%c.tdb", ff, C);
163 out = gmx_fio_fopen(buf, "w");
165 for (i = 0; (i < nb); i++)
167 fprintf(out, "[ %s ]\n", tb[i].name);
173 for (j = 0; j < tb[i].nhack; j++)
175 if (tb[i].hack[j].oname != nullptr && tb[i].hack[j].nname != nullptr)
179 else if (tb[i].hack[j].oname == nullptr && tb[i].hack[j].nname != nullptr)
183 else if (tb[i].hack[j].oname != nullptr && tb[i].hack[j].nname == nullptr)
187 else if (tb[i].hack[j].oname == nullptr && tb[i].hack[j].nname == nullptr)
189 gmx_fatal(FARGS, "invalid hack (%s) in termini database", tb[i].name);
194 fprintf(out, "[ %s ]\n", kw_names[ekwRepl-ebtsNR-1]);
195 for (j = 0; j < tb[i].nhack; j++)
197 if (tb[i].hack[j].oname != nullptr && tb[i].hack[j].nname != nullptr)
199 fprintf(out, "%s\t", tb[i].hack[j].oname);
200 print_atom(out, tb[i].hack[j].atom, atype);
206 fprintf(out, "[ %s ]\n", kw_names[ekwAdd-ebtsNR-1]);
207 for (j = 0; j < tb[i].nhack; j++)
209 if (tb[i].hack[j].oname == nullptr && tb[i].hack[j].nname != nullptr)
211 print_ab(out, &(tb[i].hack[j]), tb[i].hack[j].nname);
212 print_atom(out, tb[i].hack[j].atom, atype);
218 fprintf(out, "[ %s ]\n", kw_names[ekwDel-ebtsNR-1]);
219 for (j = 0; j < tb[i].nhack; j++)
221 if (tb[i].hack[j].oname != nullptr && tb[i].hack[j].nname == nullptr)
223 fprintf(out, "%s\n", tb[i].hack[j].oname);
227 for (bt = 0; bt < ebtsNR; bt++)
231 fprintf(out, "[ %s ]\n", btsNames[bt]);
232 for (j = 0; j < tb[i].rb[bt].nb; j++)
234 for (k = 0; k < btsNiatoms[bt]; k++)
236 fprintf(out, "%s%s", k ? "\t" : "", tb[i].rb[bt].b[j].a[k]);
238 if (tb[i].rb[bt].b[j].s)
240 fprintf(out, "\t%s", tb[i].rb[bt].b[j].s);
251 static void read_ter_db_file(const char *fn,
252 int *ntbptr, t_hackblock **tbptr,
255 char filebase[STRLEN], *ptr;
257 char header[STRLEN], buf[STRLEN], line[STRLEN];
259 int i, j, n, ni, kwnr, nb, maxnb, nh;
261 fflib_filename_base(fn, filebase, STRLEN);
262 /* Remove the C/N termini extension */
263 ptr = strrchr(filebase, '.');
275 get_a_line(in, line, STRLEN);
278 if (get_header(line, header))
280 /* this is a new block, or a new keyword */
281 kwnr = find_kw(header);
286 /* here starts a new block */
292 clear_t_hackblock(&tb[nb]);
293 tb[nb].name = gmx_strdup(header);
294 tb[nb].filebase = gmx_strdup(filebase);
301 gmx_fatal(FARGS, "reading termini database: "
302 "directive expected before line:\n%s\n"
303 "This might be a file in an old format.", line);
305 /* this is not a header, so it must be data */
308 /* this is a hack: add/rename/delete atoms */
309 /* make space for hacks */
310 if (tb[nb].nhack >= tb[nb].maxhack)
312 tb[nb].maxhack += 10;
313 srenew(tb[nb].hack, tb[nb].maxhack);
316 clear_t_hack(&(tb[nb].hack[nh]));
317 for (i = 0; i < 4; i++)
319 tb[nb].hack[nh].a[i] = nullptr;
325 if (kwnr == ekwRepl || kwnr == ekwDel)
327 if (sscanf(line, "%s%n", buf, &n) != 1)
329 gmx_fatal(FARGS, "Reading Termini Database '%s': "
330 "expected atom name on line\n%s", fn, line);
332 tb[nb].hack[nh].oname = gmx_strdup(buf);
333 /* we only replace or delete one atom at a time */
334 tb[nb].hack[nh].nr = 1;
336 else if (kwnr == ekwAdd)
338 read_ab(line, fn, &(tb[nb].hack[nh]));
339 get_a_line(in, line, STRLEN);
343 gmx_fatal(FARGS, "unimplemented keyword number %d (%s:%d)",
344 kwnr, __FILE__, __LINE__);
346 if (kwnr == ekwRepl || kwnr == ekwAdd)
348 snew(tb[nb].hack[nh].atom, 1);
349 read_atom(line+n, kwnr == ekwAdd,
350 &tb[nb].hack[nh].nname, tb[nb].hack[nh].atom, atype,
351 &tb[nb].hack[nh].cgnr);
352 if (tb[nb].hack[nh].nname == nullptr)
354 if (tb[nb].hack[nh].oname != nullptr)
356 tb[nb].hack[nh].nname = gmx_strdup(tb[nb].hack[nh].oname);
360 gmx_fatal(FARGS, "Reading Termini Database '%s': don't know which name the new atom should have on line\n%s", fn, line);
365 else if (kwnr >= 0 && kwnr < ebtsNR)
367 /* this is bonded data: bonds, angles, dihedrals or impropers */
368 srenew(tb[nb].rb[kwnr].b, tb[nb].rb[kwnr].nb+1);
370 for (j = 0; j < btsNiatoms[kwnr]; j++)
372 if (sscanf(line+n, "%s%n", buf, &ni) == 1)
374 tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = gmx_strdup(buf);
378 gmx_fatal(FARGS, "Reading Termini Database '%s': expected %d atom names (found %d) on line\n%s", fn, btsNiatoms[kwnr], j-1, line);
382 for (; j < MAXATOMLIST; j++)
384 tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = nullptr;
387 sscanf(line+n, "%s", buf);
388 tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].s = gmx_strdup(buf);
389 tb[nb].rb[kwnr].nb++;
393 gmx_fatal(FARGS, "Reading Termini Database: Expecting a header at line\n"
397 get_a_line(in, line, STRLEN);
408 int read_ter_db(const char *ffdir, char ter,
409 t_hackblock **tbptr, gpp_atomtype *atype)
414 sprintf(ext, ".%c.tdb", ter);
416 /* Search for termini database files.
417 * Do not generate an error when none are found.
419 std::vector<std::string> tdbf = fflib_search_file_end(ffdir, ext, FALSE);
422 for (const auto &filename : tdbf)
424 read_ter_db_file(filename.c_str(), &ntb, tbptr, atype);
429 print_ter_db("new", ter, ntb, *tbptr, atype);
435 t_hackblock **filter_ter(int nb, t_hackblock tb[],
439 // TODO Four years later, no force fields have ever used this, so decide status of this feature
440 /* Since some force fields (e.g. OPLS) needs different
441 * atomtypes for different residues there could be a lot
442 * of entries in the databases for specific residues
443 * (e.g. GLY-NH3+, SER-NH3+, ALA-NH3+).
445 * To reduce the database size, we assume that a terminus specifier liker
447 * [ GLY|SER|ALA-NH3+ ]
449 * would cover all of the three residue types above.
450 * Wildcards (*,?) are not OK. Don't worry about e.g. GLU vs. GLUH since
451 * pdb2gmx only uses the first 3 letters when calling this routine.
453 * To automate this, this routines scans a list of termini
454 * for the residue name "resname" and returns an allocated list of
455 * pointers to the termini that could be applied to the
456 * residue in question. The variable pointed to by nret will
457 * contain the number of valid pointers in the list.
458 * Remember to free the list when you are done with it...
461 int i, j, n, none_idx;
469 for (i = 0; i < nb; i++)
475 if (gmx_strncasecmp(resname, s, 3) == 0)
484 /* advance to next |-separated field */
492 while (!found && s != nullptr);
495 /* All residue-specific termini have been added. We might have to fall
496 * back on generic termini, which are characterized by not having
497 * '-' in the name prior to the last position (which indicates charge).
498 * The [ None ] alternative is special since we don't want that
499 * to be the default, so we put it last in the list we return.
502 for (i = 0; i < nb; i++)
505 if (!gmx_strcasecmp("None", s))
511 /* Time to see if there's a generic terminus that matches.
512 Is there a hyphen? */
513 char *c = strchr(s, '-');
515 /* A conjunction hyphen normally indicates a residue-specific
516 terminus, which is named like "GLY-COOH". A generic terminus
517 won't have a hyphen. */
518 bool bFoundAnyHyphen = (c != nullptr);
519 /* '-' as the last character indicates charge, so if that's
520 the only one found e.g. "COO-", then it was not a conjunction
521 hyphen, so this is a generic terminus */
522 bool bOnlyFoundChargeHyphen = (bFoundAnyHyphen &&
524 /* Thus, "GLY-COO-" is not recognized as a generic terminus. */
525 bool bFoundGenericTerminus = !bFoundAnyHyphen || bOnlyFoundChargeHyphen;
526 if (bFoundGenericTerminus)
528 /* Check that we haven't already added a residue-specific version
531 for (j = 0; j < n && strstr((*list[j]).name, s) == nullptr; j++)
547 list[n] = &(tb[none_idx]);
556 t_hackblock *choose_ter(int nb, t_hackblock **tb, const char *title)
560 printf("%s\n", title);
561 for (i = 0; (i < nb); i++)
563 bool bIsZwitterion = (0 == gmx_wcmatch("*ZWITTERION*", (*tb[i]).name));
564 printf("%2d: %s%s\n", i, (*tb[i]).name,
565 bIsZwitterion ? " (only use with zwitterions containing exactly one residue)" : "");
569 ret = fscanf(stdin, "%d", &sel);
571 while ((ret != 1) || (sel < 0) || (sel >= nb));