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 */
62 NOTFOUND = -4, WILDCARD, WILDPROT, PROTEIN
65 //! Basic entries in AtomProperty.
67 //! Default constructor.
68 BaseEntry(const std::string &aName, const std::string &rName)
69 : atomName(aName), residueName(rName), isAvailable(false), value(0.0)
74 std::string residueName;
75 //! Is property available.
77 //! Value set for property.
81 //! Conglomeration of atom property entries.
83 //! Has property been set.
85 //! Database the property is coming from.
87 //! Default value for property.
89 //! Basic entries for properties.
90 std::vector<BaseEntry> entry;
93 //! Implementation detail type for Atomproperties.
94 class AtomProperties::Impl
97 //! Should user be warned about error.
99 //! Should user be warned about vdW not found.
100 bool bWarnVDW = false;
101 //! The different atom properties.
102 AtomProperty prop[epropNR];
103 //! The residue types.
108 * Find number of matching characters in entry.
110 * If not all characters are matching, return NOTFOUND.
111 * If the length of the database entry is different from the search,
112 * also return NOTFOUND.
114 * \param[in] search Entry to compare to database.
115 * \param[in] database Name of the database entry to compare to.
116 * \returns Number of matching characters or NOTFOUND.
118 static int compareToDatabase(const std::string &search, const std::string &database)
120 if (database.length() > search.length())
125 for (size_t i = 0; i < database.length(); i++)
127 if (search[i] == database[i])
132 if (matches == database.length())
143 * Finds the index for the property being searched.
145 * \param[in] ap Property to search for.
146 * \param[in] restype Residuetypes in database.
147 * \param[in] residueName The name of the residue to look for.
148 * \param[in] atomName The name of the atom to look for.
149 * \param[in] bExact Do we have the correct match.
150 * \returns The index for the property.
152 static int findPropertyIndex(AtomProperty *ap, ResidueType *restype,
153 const std::string &residueName, const std::string &atomName,
158 bool bProtein = restype->namedResidueHasType(residueName, "Protein");
159 bool bProtWild = residueName == "AAA";
160 int malen = NOTFOUND;
161 int mrlen = NOTFOUND;
162 for (size_t i = 0; (i < ap->entry.size()); i++)
164 int rlen = compareToDatabase(residueName, ap->entry[i].residueName);
165 if (rlen == NOTFOUND)
167 if ( (ap->entry[i].residueName == "*") ||
168 (ap->entry[i].residueName == "???") )
172 else if (ap->entry[i].residueName == "AAA")
177 int alen = compareToDatabase(atomName, ap->entry[i].atomName);
178 if ( (alen > NOTFOUND) && (rlen > NOTFOUND))
180 if ( ( (alen > malen) && (rlen >= mrlen)) ||
181 ( (rlen > mrlen) && (alen >= malen) ) )
190 *bExact = ((malen == static_cast<long int>(atomName.length())) &&
191 ((mrlen == static_cast<long int>(residueName.length())) ||
192 ((mrlen == WILDPROT) && bProtWild) ||
193 ((mrlen == WILDCARD) && !bProtein && !bProtWild)));
197 fprintf(debug, "searching residue: %4s atom: %4s\n", residueName.c_str(),
201 fprintf(debug, " not successful\n");
205 fprintf(debug, " match: %4s %4s\n",
206 ap->entry[j].residueName.c_str(), ap->entry[j].atomName.c_str());
213 * Add new property to list.
215 * \param[in] ap Atomproperty to add.
216 * \param[in] restype Residue type database to use.
217 * \param[in] residueName Name of the residue.
218 * \param[in] atomName Name of the atom.
219 * \param[in] propValue Value of property.
220 * \param[in] line Where to add property.
222 static void addProperty(AtomProperty *ap, ResidueType *restype,
223 const std::string &residueName, const std::string &atomName,
224 real propValue, int line)
227 int j = findPropertyIndex(ap, restype, residueName, atomName, &bExact);
231 ap->entry.emplace_back(BaseEntry(atomName, residueName));
233 j = ap->entry.size() - 1;
235 if (ap->entry[j].isAvailable)
237 if (ap->entry[j].value == propValue)
239 fprintf(stderr, "Warning double identical entries for %s %s %g on line %d in file %s\n",
240 residueName.c_str(), atomName.c_str(), propValue, line, ap->db.c_str());
244 fprintf(stderr, "Warning double different entries %s %s %g and %g on line %d in file %s\n"
245 "Using last entry (%g)\n",
246 residueName.c_str(), atomName.c_str(),
247 propValue, ap->entry[j].value, line, ap->db.c_str(), propValue);
248 ap->entry[j].value = propValue;
253 ap->entry[j].isAvailable = TRUE;
254 ap->entry[j].value = propValue;
259 * Read property value into structure.
261 * \param[in] ap Atomproperty to be read in.
262 * \param[in] restype Library of residue types.
263 * \param[in] factor Scaling factor for property.
265 static void readProperty(AtomProperty *ap, ResidueType *restype, double factor)
267 char line[STRLEN], resnm[32], atomnm[32];
269 gmx::FilePtr fp = gmx::openLibraryFile(ap->db);
271 while (get_a_line(fp.get(), line, STRLEN))
275 if (sscanf(line, "%31s %31s %20lf", resnm, atomnm, &pp) == 3)
278 addProperty(ap, restype, resnm, atomnm, pp, line_no);
282 fprintf(stderr, "WARNING: Error in file %s at line %d ignored\n",
283 ap->db.c_str(), line_no);
290 * Set value for properties.
292 * \param[in] ap Atomproperty to set.
293 * \param[in] restype Library of residue types.
294 * \param[in] eprop Which property to set.
295 * \param[in] haveBeenWarned If we already set a warning before
296 * \returns True of warning should be printed.
298 static bool setProperties(AtomProperty *ap, ResidueType *restype, int eprop, bool haveBeenWarned)
300 const char *fns[epropNR] = { "atommass.dat", "vdwradii.dat", "dgsolv.dat", "electroneg.dat", "elements.dat" };
301 double fac[epropNR] = { 1.0, 1.0, 418.4, 1.0, 1.0 };
302 double def[epropNR] = { 12.011, 0.14, 0.0, 2.2, -1 };
304 bool printWarning = false;
308 ap->def = def[eprop];
309 readProperty(ap, restype, fac[eprop]);
313 fprintf(debug, "Entries in %s: %zu\n", ap->db.c_str(), ap->entry.size());
316 if ( (!haveBeenWarned && (eprop == epropMass) ) || (eprop == epropVDW))
325 AtomProperties::AtomProperties()
330 AtomProperties::~AtomProperties()
334 AtomProperty *AtomProperties::prop(int eprop)
336 return &impl_->prop[eprop];
339 ResidueType *AtomProperties::restype()
341 return &impl_->restype;
344 //! Print warning that vdW radii and masses are guessed.
345 static void printWarning()
348 "WARNING: Masses and atomic (Van der Waals) radii will be guessed\n"
349 " based on residue and atom names, since they could not be\n"
350 " definitively assigned from the information in your input\n"
351 " files. These guessed numbers might deviate from the mass\n"
352 " and radius of the atom type. Please check the output\n"
353 " files if necessary.\n\n");
356 static void printvdwWarning(FILE *fp)
360 fprintf(fp, "NOTE: From version 5.0 %s uses the Van der Waals radii\n",
361 gmx::getProgramContext().displayName());
362 fprintf(fp, "from the source below. This means the results may be different\n");
363 fprintf(fp, "compared to previous GROMACS versions.\n");
364 please_cite(fp, "Bondi1964a");
368 bool AtomProperties::setAtomProperty(int eprop,
369 const std::string &residueName,
370 const std::string &atomName,
374 std::string tmpAtomName, tmpResidueName;
377 if (setProperties(prop(eprop), restype(), eprop, impl_->bWarned))
380 impl_->bWarned = true;
382 if (isdigit(atomName[0]))
384 /* put digit after atomname */
385 tmpAtomName.append(atomName.substr(1));
386 tmpAtomName.append(1, atomName[0]);
390 tmpAtomName = atomName;
392 j = findPropertyIndex(&(impl_->prop[eprop]), &impl_->restype, residueName,
393 tmpAtomName, &bExact);
395 if (eprop == epropVDW && !impl_->bWarnVDW)
397 printvdwWarning(stdout);
398 impl_->bWarnVDW = true;
402 *value = impl_->prop[eprop].entry[j].value;
407 *value = impl_->prop[eprop].def;
413 const std::string AtomProperties::elementFromAtomNumber(int atomNumber)
415 if (setProperties(prop(epropElement), restype(), epropElement, impl_->bWarned))
418 impl_->bWarned = true;
420 for (const auto &e : prop(epropElement)->entry)
422 if (std::round(e.value) == atomNumber)
430 int AtomProperties::atomNumberFromElement(const char *element)
432 if (setProperties(prop(epropElement), restype(), epropElement, impl_->bWarned))
435 impl_->bWarned = true;
437 for (const auto &e : prop(epropElement)->entry)
439 if (gmx_strcasecmp(e.atomName.c_str(), element) == 0)
441 return gmx::roundToInt(e.value);