Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / topology / residuetypes.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 #include "gmxpre.h"
37
38 #include "residuetypes.h"
39
40 #include <cassert>
41 #include <cstdio>
42
43 #include <algorithm>
44 #include <iterator>
45 #include <optional>
46 #include <string>
47
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"
54
55 //! Definition for residue type that is not known.
56 const std::string c_undefinedResidueType = "Other";
57
58 //! Single ResidueType entry object
59 struct ResidueTypeEntry
60 {
61     //! Default constructor creates complete object.
62     ResidueTypeEntry(const std::string& rName, const std::string& rType) :
63         residueName(rName), residueType(rType)
64     {
65     }
66     //! Name of the residue in the entry.
67     std::string residueName;
68     //! Type of the residue in the entry.
69     std::string residueType;
70 };
71
72 //! Implementation detail for ResidueTypes
73 class ResidueType::Impl
74 {
75 public:
76     //! Storage object for entries.
77     std::vector<ResidueTypeEntry> entry;
78 };
79
80 ResidueType::ResidueType() : impl_(new Impl)
81 {
82     char line[STRLEN];
83     char resname[STRLEN], restype[STRLEN], dum[STRLEN];
84
85     gmx::FilePtr db = gmx::openLibraryFile("residuetypes.dat");
86
87     while (get_a_line(db.get(), line, STRLEN))
88     {
89         strip_comment(line);
90         trim(line);
91         if (line[0] != '\0')
92         {
93             if (sscanf(line, "%1000s %1000s %1000s", resname, restype, dum) != 2)
94             {
95                 gmx_fatal(
96                         FARGS,
97                         "Incorrect number of columns (2 expected) for line in residuetypes.dat  ");
98             }
99             addResidue(resname, restype);
100         }
101     }
102 }
103
104 ResidueType::~ResidueType() {}
105
106 /*! \brief
107  * Return an optional const iterator to a residue entry that matches the given name.
108  *
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.
112  */
113 static std::optional<gmx::ArrayRef<const ResidueTypeEntry>::const_iterator>
114 findResidueEntryWithName(gmx::ArrayRef<const ResidueTypeEntry> entries, const std::string& residueName)
115 {
116     auto foundIt =
117             std::find_if(entries.begin(), entries.end(), [&residueName](const ResidueTypeEntry& old) {
118                 return gmx::equalCaseInsensitive(residueName, old.residueName);
119             });
120     return (foundIt != entries.end()) ? std::make_optional(foundIt) : std::nullopt;
121 }
122
123 bool ResidueType::nameIndexedInResidueTypes(const std::string& residueName)
124 {
125     return findResidueEntryWithName(impl_->entry, residueName).has_value();
126 }
127
128 void ResidueType::addResidue(const std::string& residueName, const std::string& residueType)
129 {
130     if (auto foundIt = findResidueEntryWithName(impl_->entry, residueName))
131     {
132         if (!gmx::equalCaseInsensitive((*foundIt)->residueType, residueType))
133         {
134             fprintf(stderr,
135                     "Warning: Residue '%s' already present with type '%s' in database, ignoring "
136                     "new type '%s'.\n",
137                     residueName.c_str(),
138                     (*foundIt)->residueType.c_str(),
139                     residueType.c_str());
140         }
141     }
142     else
143     {
144         impl_->entry.emplace_back(residueName, residueType);
145     }
146 }
147
148 bool ResidueType::namedResidueHasType(const std::string& residueName, const std::string& residueType)
149 {
150     auto foundIt = findResidueEntryWithName(impl_->entry, residueName);
151     return foundIt ? gmx::equalCaseInsensitive(residueType, (*foundIt)->residueType) : false;
152 }
153
154 int ResidueType::numberOfEntries() const
155 {
156     return impl_->entry.size();
157 }
158
159 int ResidueType::indexFromResidueName(const std::string& residueName) const
160 {
161     gmx::ArrayRef<const ResidueTypeEntry> temp(impl_->entry);
162     auto                                  foundIt = findResidueEntryWithName(temp, residueName);
163     return foundIt ? std::distance(temp.begin(), *foundIt) : -1;
164 }
165
166 std::string ResidueType::nameFromResidueIndex(int index) const
167 {
168     if (index >= 0 && index < gmx::ssize(impl_->entry))
169     {
170         return impl_->entry[index].residueName;
171     }
172     else
173     {
174         return "";
175     }
176 }
177
178 std::string ResidueType::typeOfNamedDatabaseResidue(const std::string& residueName)
179 {
180     auto foundIt = findResidueEntryWithName(impl_->entry, residueName);
181     return foundIt ? (*foundIt)->residueType : c_undefinedResidueType;
182 }
183
184 std::optional<std::string> ResidueType::optionalTypeOfNamedDatabaseResidue(const std::string& residueName)
185 {
186     auto foundIt = findResidueEntryWithName(impl_->entry, residueName);
187     return foundIt ? std::make_optional((*foundIt)->residueType) : std::nullopt;
188 }