2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 #include "residuetypes.h"
48 #include "gromacs/utility/arrayref.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/smalloc.h"
53 #include "gromacs/utility/strdb.h"
55 //! Definition for residue type that is not known.
56 const std::string c_undefinedResidueType = "Other";
58 //! Single ResidueType entry object
59 struct ResidueTypeEntry
61 //! Default constructor creates complete object.
62 ResidueTypeEntry(const std::string& rName, const std::string& rType) :
63 residueName(rName), residueType(rType)
66 //! Name of the residue in the entry.
67 std::string residueName;
68 //! Type of the residue in the entry.
69 std::string residueType;
72 //! Implementation detail for ResidueTypes
73 class ResidueType::Impl
76 //! Storage object for entries.
77 std::vector<ResidueTypeEntry> entry;
80 ResidueType::ResidueType() : impl_(new Impl)
83 char resname[STRLEN], restype[STRLEN], dum[STRLEN];
85 gmx::FilePtr db = gmx::openLibraryFile("residuetypes.dat");
87 while (get_a_line(db.get(), line, STRLEN))
93 if (sscanf(line, "%1000s %1000s %1000s", resname, restype, dum) != 2)
97 "Incorrect number of columns (2 expected) for line in residuetypes.dat ");
99 addResidue(resname, restype);
104 ResidueType::~ResidueType() {}
107 * Return an optional const iterator to a residue entry that matches the given name.
109 * \param[in] entries Currently registered residue entries in the database.
110 * \param[in] residueName Name of a residue to compare to database.
111 * \returns An optional iterator to the residue entry that was found.
113 static std::optional<gmx::ArrayRef<const ResidueTypeEntry>::const_iterator>
114 findResidueEntryWithName(gmx::ArrayRef<const ResidueTypeEntry> entries, const std::string& residueName)
117 std::find_if(entries.begin(), entries.end(), [&residueName](const ResidueTypeEntry& old) {
118 return gmx::equalCaseInsensitive(residueName, old.residueName);
120 return (foundIt != entries.end()) ? std::make_optional(foundIt) : std::nullopt;
123 bool ResidueType::nameIndexedInResidueTypes(const std::string& residueName)
125 return findResidueEntryWithName(impl_->entry, residueName).has_value();
128 void ResidueType::addResidue(const std::string& residueName, const std::string& residueType)
130 if (auto foundIt = findResidueEntryWithName(impl_->entry, residueName))
132 if (!gmx::equalCaseInsensitive((*foundIt)->residueType, residueType))
135 "Warning: Residue '%s' already present with type '%s' in database, ignoring "
138 (*foundIt)->residueType.c_str(),
139 residueType.c_str());
144 impl_->entry.emplace_back(residueName, residueType);
148 bool ResidueType::namedResidueHasType(const std::string& residueName, const std::string& residueType)
150 auto foundIt = findResidueEntryWithName(impl_->entry, residueName);
151 return foundIt ? gmx::equalCaseInsensitive(residueType, (*foundIt)->residueType) : false;
154 int ResidueType::numberOfEntries() const
156 return impl_->entry.size();
159 int ResidueType::indexFromResidueName(const std::string& residueName) const
161 gmx::ArrayRef<const ResidueTypeEntry> temp(impl_->entry);
162 auto foundIt = findResidueEntryWithName(temp, residueName);
163 return foundIt ? std::distance(temp.begin(), *foundIt) : -1;
166 std::string ResidueType::nameFromResidueIndex(int index) const
168 if (index >= 0 && index < gmx::ssize(impl_->entry))
170 return impl_->entry[index].residueName;
178 std::string ResidueType::typeOfNamedDatabaseResidue(const std::string& residueName)
180 auto foundIt = findResidueEntryWithName(impl_->entry, residueName);
181 return foundIt ? (*foundIt)->residueType : c_undefinedResidueType;
184 std::optional<std::string> ResidueType::optionalTypeOfNamedDatabaseResidue(const std::string& residueName)
186 auto foundIt = findResidueEntryWithName(impl_->entry, residueName);
187 return foundIt ? std::make_optional((*foundIt)->residueType) : std::nullopt;