Split lines with many copyright years
[gromacs.git] / src / gromacs / gmxpreprocess / xlate.cpp
blob4166698511a6bad72f85c17784e1d551a060e665
1 /*
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, 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.
38 #include "gmxpre.h"
40 #include "xlate.h"
42 #include <cctype>
43 #include <cstring>
45 #include <string>
46 #include <vector>
48 #include "gromacs/gmxpreprocess/fflibutil.h"
49 #include "gromacs/gmxpreprocess/grompp_impl.h"
50 #include "gromacs/topology/residuetypes.h"
51 #include "gromacs/topology/symtab.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/futil.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/strdb.h"
58 #include "hackblock.h"
60 typedef struct
62 char* filebase;
63 char* res;
64 char* atom;
65 char* replace;
66 } t_xlate_atom;
68 static void get_xlatoms(const std::string& filename, FILE* fp, int* nptr, t_xlate_atom** xlptr)
70 char filebase[STRLEN];
71 char line[STRLEN];
72 char abuf[1024], rbuf[1024], repbuf[1024], dumbuf[1024];
73 char* _ptr;
74 int n, na, idum;
75 t_xlate_atom* xl;
77 fflib_filename_base(filename.c_str(), filebase, STRLEN);
79 n = *nptr;
80 xl = *xlptr;
82 while (get_a_line(fp, line, STRLEN))
84 na = sscanf(line, "%s%s%s%s", rbuf, abuf, repbuf, dumbuf);
85 /* Check if we are reading an old format file with the number of items
86 * on the first line.
88 if (na == 1 && n == *nptr && sscanf(rbuf, "%d", &idum) == 1)
90 continue;
92 if (na != 3)
94 gmx_fatal(FARGS, "Expected a residue name and two atom names in file '%s', not '%s'",
95 filename.c_str(), line);
98 srenew(xl, n + 1);
99 xl[n].filebase = gmx_strdup(filebase);
101 /* Use wildcards... */
102 if (strcmp(rbuf, "*") != 0)
104 xl[n].res = gmx_strdup(rbuf);
106 else
108 xl[n].res = nullptr;
111 /* Replace underscores in the string by spaces */
112 while ((_ptr = strchr(abuf, '_')) != nullptr)
114 *_ptr = ' ';
117 xl[n].atom = gmx_strdup(abuf);
118 xl[n].replace = gmx_strdup(repbuf);
119 n++;
122 *nptr = n;
123 *xlptr = xl;
126 static void done_xlatom(int nxlate, t_xlate_atom* xlatom)
128 int i;
130 for (i = 0; (i < nxlate); i++)
132 sfree(xlatom[i].filebase);
133 if (xlatom[i].res != nullptr)
135 sfree(xlatom[i].res);
137 sfree(xlatom[i].atom);
138 sfree(xlatom[i].replace);
140 sfree(xlatom);
143 void rename_atoms(const char* xlfile,
144 const char* ffdir,
145 t_atoms* atoms,
146 t_symtab* symtab,
147 gmx::ArrayRef<const PreprocessResidue> localPpResidue,
148 bool bResname,
149 ResidueType* rt,
150 bool bReorderNum,
151 bool bVerbose)
153 int nxlate, a, i, resind;
154 t_xlate_atom* xlatom;
155 char c, *rnm, atombuf[32];
156 bool bReorderedNum, bRenamed, bMatch;
157 bool bStartTerm, bEndTerm;
159 nxlate = 0;
160 xlatom = nullptr;
161 if (xlfile != nullptr)
163 gmx::FilePtr fp = gmx::openLibraryFile(xlfile);
164 get_xlatoms(xlfile, fp.get(), &nxlate, &xlatom);
166 else
168 std::vector<std::string> fns = fflib_search_file_end(ffdir, ".arn", FALSE);
169 for (const auto& filename : fns)
171 FILE* fp = fflib_open(filename);
172 get_xlatoms(filename, fp, &nxlate, &xlatom);
173 gmx_ffclose(fp);
177 for (a = 0; (a < atoms->nr); a++)
179 resind = atoms->atom[a].resind;
181 bStartTerm = (resind == 0) || atoms->resinfo[resind].chainnum != atoms->resinfo[resind - 1].chainnum;
182 bEndTerm = (resind >= atoms->nres - 1)
183 || atoms->resinfo[resind].chainnum != atoms->resinfo[resind + 1].chainnum;
185 if (bResname)
187 rnm = *(atoms->resinfo[resind].name);
189 else
191 rnm = *(atoms->resinfo[resind].rtp);
194 strcpy(atombuf, *(atoms->atomname[a]));
195 bReorderedNum = FALSE;
196 if (bReorderNum)
198 if (isdigit(atombuf[0]))
200 c = atombuf[0];
201 for (i = 0; (static_cast<size_t>(i) < strlen(atombuf) - 1); i++)
203 atombuf[i] = atombuf[i + 1];
205 atombuf[i] = c;
206 bReorderedNum = TRUE;
209 bRenamed = FALSE;
210 for (i = 0; (i < nxlate) && !bRenamed; i++)
212 /* Check if the base file name of the rtp and arn entry match */
213 if (localPpResidue.empty()
214 || gmx::equalCaseInsensitive(localPpResidue[resind].filebase, xlatom[i].filebase))
216 /* Match the residue name */
217 bMatch = (xlatom[i].res == nullptr
218 || (gmx_strcasecmp("protein-nterm", xlatom[i].res) == 0
219 && rt->namedResidueHasType(rnm, "Protein") && bStartTerm)
220 || (gmx_strcasecmp("protein-cterm", xlatom[i].res) == 0
221 && rt->namedResidueHasType(rnm, "Protein") && bEndTerm)
222 || (gmx_strcasecmp("protein", xlatom[i].res) == 0
223 && rt->namedResidueHasType(rnm, "Protein"))
224 || (gmx_strcasecmp("DNA", xlatom[i].res) == 0
225 && rt->namedResidueHasType(rnm, "DNA"))
226 || (gmx_strcasecmp("RNA", xlatom[i].res) == 0
227 && rt->namedResidueHasType(rnm, "RNA")));
228 if (!bMatch)
230 const char* ptr0 = rnm;
231 const char* ptr1 = xlatom[i].res;
232 while (ptr0[0] != '\0' && ptr1[0] != '\0' && (ptr0[0] == ptr1[0] || ptr1[0] == '?'))
234 ptr0++;
235 ptr1++;
237 bMatch = (ptr0[0] == '\0' && ptr1[0] == '\0');
239 if (bMatch && strcmp(atombuf, xlatom[i].atom) == 0)
241 /* We have a match. */
242 /* Don't free the old atomname,
243 * since it might be in the symtab.
245 const char* ptr0 = xlatom[i].replace;
246 if (bVerbose)
248 printf("Renaming atom '%s' in residue %d %s to '%s'\n", *atoms->atomname[a],
249 atoms->resinfo[resind].nr, *atoms->resinfo[resind].name, ptr0);
251 atoms->atomname[a] = put_symtab(symtab, ptr0);
252 bRenamed = TRUE;
256 if (bReorderedNum && !bRenamed)
258 atoms->atomname[a] = put_symtab(symtab, atombuf);
262 done_xlatom(nxlate, xlatom);