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.
49 #include "gromacs/math/functions.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/topology/residuetypes.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/futil.h"
55 #include "gromacs/utility/pleasecite.h"
56 #include "gromacs/utility/programcontext.h"
57 #include "gromacs/utility/smalloc.h"
58 #include "gromacs/utility/strdb.h"
60 /* NOTFOUND should be smallest, others larger in increasing priority */
69 //! Basic entries in AtomProperty.
72 //! Default constructor.
73 BaseEntry(const std::string& aName, const std::string& rName) :
83 std::string residueName;
84 //! Is property available.
86 //! Value set for property.
90 //! Conglomeration of atom property entries.
93 //! Has property been set.
95 //! Database the property is coming from.
97 //! Default value for property.
99 //! Basic entries for properties.
100 std::vector<BaseEntry> entry;
103 //! Implementation detail type for Atomproperties.
104 class AtomProperties::Impl
107 //! Should user be warned about error.
108 bool bWarned = false;
109 //! Should user be warned about vdW not found.
110 bool bWarnVDW = false;
111 //! The different atom properties.
112 AtomProperty prop[epropNR];
113 //! The residue types.
118 * Find number of matching characters in entry.
120 * If not all characters are matching, return NOTFOUND.
121 * If the length of the database entry is different from the search,
122 * also return NOTFOUND.
124 * \param[in] search Entry to compare to database.
125 * \param[in] database Name of the database entry to compare to.
126 * \returns Number of matching characters or NOTFOUND.
128 static int compareToDatabase(const std::string& search, const std::string& database)
130 if (database.length() > search.length())
135 for (size_t i = 0; i < database.length(); i++)
137 if (search[i] == database[i])
142 if (matches == database.length())
153 * Finds the index for the property being searched.
155 * \param[in] ap Property to search for.
156 * \param[in] restype Residuetypes in database.
157 * \param[in] residueName The name of the residue to look for.
158 * \param[in] atomName The name of the atom to look for.
159 * \param[in] bExact Do we have the correct match.
160 * \returns The index for the property.
162 static int findPropertyIndex(AtomProperty* ap,
163 ResidueType* restype,
164 const std::string& residueName,
165 const std::string& atomName,
170 bool bProtein = restype->namedResidueHasType(residueName, "Protein");
171 bool bProtWild = residueName == "AAA";
172 int malen = NOTFOUND;
173 int mrlen = NOTFOUND;
174 for (size_t i = 0; (i < ap->entry.size()); i++)
176 int rlen = compareToDatabase(residueName, ap->entry[i].residueName);
177 if (rlen == NOTFOUND)
179 if ((ap->entry[i].residueName == "*") || (ap->entry[i].residueName == "???"))
183 else if (ap->entry[i].residueName == "AAA")
188 int alen = compareToDatabase(atomName, ap->entry[i].atomName);
189 if ((alen > NOTFOUND) && (rlen > NOTFOUND))
191 if (((alen > malen) && (rlen >= mrlen)) || ((rlen > mrlen) && (alen >= malen)))
200 *bExact = ((malen == static_cast<long int>(atomName.length()))
201 && ((mrlen == static_cast<long int>(residueName.length())) || ((mrlen == WILDPROT) && bProtWild)
202 || ((mrlen == WILDCARD) && !bProtein && !bProtWild)));
206 fprintf(debug, "searching residue: %4s atom: %4s\n", residueName.c_str(), atomName.c_str());
209 fprintf(debug, " not successful\n");
213 fprintf(debug, " match: %4s %4s\n", ap->entry[j].residueName.c_str(),
214 ap->entry[j].atomName.c_str());
221 * Add new property to list.
223 * \param[in] ap Atomproperty to add.
224 * \param[in] restype Residue type database to use.
225 * \param[in] residueName Name of the residue.
226 * \param[in] atomName Name of the atom.
227 * \param[in] propValue Value of property.
228 * \param[in] line Where to add property.
230 static void addProperty(AtomProperty* ap,
231 ResidueType* restype,
232 const std::string& residueName,
233 const std::string& atomName,
238 int j = findPropertyIndex(ap, restype, residueName, atomName, &bExact);
242 ap->entry.emplace_back(BaseEntry(atomName, residueName));
244 j = ap->entry.size() - 1;
246 if (ap->entry[j].isAvailable)
248 if (ap->entry[j].value == propValue)
250 fprintf(stderr, "Warning double identical entries for %s %s %g on line %d in file %s\n",
251 residueName.c_str(), atomName.c_str(), propValue, line, ap->db.c_str());
256 "Warning double different entries %s %s %g and %g on line %d in file %s\n"
257 "Using last entry (%g)\n",
258 residueName.c_str(), atomName.c_str(), propValue, ap->entry[j].value, line,
259 ap->db.c_str(), propValue);
260 ap->entry[j].value = propValue;
265 ap->entry[j].isAvailable = TRUE;
266 ap->entry[j].value = propValue;
271 * Read property value into structure.
273 * \param[in] ap Atomproperty to be read in.
274 * \param[in] restype Library of residue types.
275 * \param[in] factor Scaling factor for property.
277 static void readProperty(AtomProperty* ap, ResidueType* restype, double factor)
279 char line[STRLEN], resnm[32], atomnm[32];
281 gmx::FilePtr fp = gmx::openLibraryFile(ap->db);
283 while (get_a_line(fp.get(), line, STRLEN))
287 if (sscanf(line, "%31s %31s %20lf", resnm, atomnm, &pp) == 3)
290 addProperty(ap, restype, resnm, atomnm, pp, line_no);
294 fprintf(stderr, "WARNING: Error in file %s at line %d ignored\n", ap->db.c_str(), line_no);
301 * Set value for properties.
303 * \param[in] ap Atomproperty to set.
304 * \param[in] restype Library of residue types.
305 * \param[in] eprop Which property to set.
306 * \param[in] haveBeenWarned If we already set a warning before
307 * \returns True of warning should be printed.
309 static bool setProperties(AtomProperty* ap, ResidueType* restype, int eprop, bool haveBeenWarned)
311 const char* fns[epropNR] = { "atommass.dat", "vdwradii.dat", "dgsolv.dat", "electroneg.dat",
313 double fac[epropNR] = { 1.0, 1.0, 418.4, 1.0, 1.0 };
314 double def[epropNR] = { 12.011, 0.14, 0.0, 2.2, -1 };
316 bool printWarning = false;
320 ap->def = def[eprop];
321 readProperty(ap, restype, fac[eprop]);
325 fprintf(debug, "Entries in %s: %zu\n", ap->db.c_str(), ap->entry.size());
328 if ((!haveBeenWarned && (eprop == epropMass)) || (eprop == epropVDW))
336 AtomProperties::AtomProperties() : impl_(new Impl) {}
338 AtomProperties::~AtomProperties() {}
340 AtomProperty* AtomProperties::prop(int eprop)
342 return &impl_->prop[eprop];
345 ResidueType* AtomProperties::restype()
347 return &impl_->restype;
350 //! Print warning that vdW radii and masses are guessed.
351 static void printWarning()
354 "WARNING: Masses and atomic (Van der Waals) radii will be guessed\n"
355 " based on residue and atom names, since they could not be\n"
356 " definitively assigned from the information in your input\n"
357 " files. These guessed numbers might deviate from the mass\n"
358 " and radius of the atom type. Please check the output\n"
359 " files if necessary.\n\n");
362 static void printvdwWarning(FILE* fp)
366 fprintf(fp, "NOTE: From version 5.0 %s uses the Van der Waals radii\n",
367 gmx::getProgramContext().displayName());
368 fprintf(fp, "from the source below. This means the results may be different\n");
369 fprintf(fp, "compared to previous GROMACS versions.\n");
370 please_cite(fp, "Bondi1964a");
374 bool AtomProperties::setAtomProperty(int eprop,
375 const std::string& residueName,
376 const std::string& atomName,
380 std::string tmpAtomName, tmpResidueName;
383 if (setProperties(prop(eprop), restype(), eprop, impl_->bWarned))
386 impl_->bWarned = true;
388 if (isdigit(atomName[0]))
390 /* put digit after atomname */
391 tmpAtomName.append(atomName.substr(1));
392 tmpAtomName.append(1, atomName[0]);
396 tmpAtomName = atomName;
398 j = findPropertyIndex(&(impl_->prop[eprop]), &impl_->restype, residueName, tmpAtomName, &bExact);
400 if (eprop == epropVDW && !impl_->bWarnVDW)
402 printvdwWarning(stdout);
403 impl_->bWarnVDW = true;
407 *value = impl_->prop[eprop].entry[j].value;
412 *value = impl_->prop[eprop].def;
418 std::string AtomProperties::elementFromAtomNumber(int atomNumber)
420 if (setProperties(prop(epropElement), restype(), epropElement, impl_->bWarned))
423 impl_->bWarned = true;
425 for (const auto& e : prop(epropElement)->entry)
427 if (std::round(e.value) == atomNumber)
435 int AtomProperties::atomNumberFromElement(const char* element)
437 if (setProperties(prop(epropElement), restype(), epropElement, impl_->bWarned))
440 impl_->bWarned = true;
442 for (const auto& e : prop(epropElement)->entry)
444 if (gmx_strcasecmp(e.atomName.c_str(), element) == 0)
446 return gmx::roundToInt(e.value);