readability-implicit-bool-conversion 1/2
[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,2017,2018, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include "residuetypes.h"
38
39 #include <cassert>
40 #include <cstdio>
41
42 #include "gromacs/utility/cstringutil.h"
43 #include "gromacs/utility/fatalerror.h"
44 #include "gromacs/utility/futil.h"
45 #include "gromacs/utility/smalloc.h"
46 #include "gromacs/utility/strdb.h"
47
48 const char gmx_residuetype_undefined[] = "Other";
49
50 struct gmx_residuetype_t
51 {
52     int      n;
53     char **  resname;
54     char **  restype;
55 };
56
57 int
58 gmx_residuetype_init(gmx_residuetype_t **prt)
59 {
60     FILE                 *  db;
61     char                    line[STRLEN];
62     char                    resname[STRLEN], restype[STRLEN], dum[STRLEN];
63     gmx_residuetype_t      *rt;
64
65     snew(rt, 1);
66     *prt = rt;
67
68     rt->n        = 0;
69     rt->resname  = nullptr;
70     rt->restype  = nullptr;
71
72     db = libopen("residuetypes.dat");
73
74     while (get_a_line(db, line, STRLEN))
75     {
76         strip_comment(line);
77         trim(line);
78         if (line[0] != '\0')
79         {
80             if (sscanf(line, "%1000s %1000s %1000s", resname, restype, dum) != 2)
81             {
82                 gmx_fatal(FARGS, "Incorrect number of columns (2 expected) for line in residuetypes.dat");
83             }
84             gmx_residuetype_add(rt, resname, restype);
85         }
86     }
87
88     fclose(db);
89
90     return 0;
91 }
92
93 int
94 gmx_residuetype_destroy(gmx_residuetype_t *rt)
95 {
96     int i;
97
98     for (i = 0; i < rt->n; i++)
99     {
100         sfree(rt->resname[i]);
101         sfree(rt->restype[i]);
102     }
103     sfree(rt->resname);
104     sfree(rt->restype);
105     sfree(rt);
106
107     return 0;
108 }
109
110 /* Return 0 if the name was found, otherwise -1.
111  * p_restype is set to a pointer to the type name, or 'Other' if we did not find it.
112  */
113 int
114 gmx_residuetype_get_type(gmx_residuetype_t *rt, const char * resname, const char ** p_restype)
115 {
116     int    i, rc;
117
118     rc = -1;
119     for (i = 0; i < rt->n && rc; i++)
120     {
121         rc = gmx_strcasecmp(rt->resname[i], resname);
122     }
123
124     *p_restype = (rc == 0) ? rt->restype[i-1] : gmx_residuetype_undefined;
125
126     return rc;
127 }
128
129 int
130 gmx_residuetype_add(gmx_residuetype_t *rt, const char *newresname, const char *newrestype)
131 {
132     bool          found;
133     const char *  p_oldtype;
134
135     found = !gmx_residuetype_get_type(rt, newresname, &p_oldtype);
136
137     if (found && gmx_strcasecmp(p_oldtype, newrestype))
138     {
139         fprintf(stderr, "Warning: Residue '%s' already present with type '%s' in database, ignoring new type '%s'.",
140                 newresname, p_oldtype, newrestype);
141     }
142
143     if (!found)
144     {
145         srenew(rt->resname, rt->n+1);
146         srenew(rt->restype, rt->n+1);
147         rt->resname[rt->n] = gmx_strdup(newresname);
148         rt->restype[rt->n] = gmx_strdup(newrestype);
149         rt->n++;
150     }
151
152     return 0;
153 }
154
155 int
156 gmx_residuetype_get_alltypes(gmx_residuetype_t   *rt,
157                              const char ***       p_typenames,
158                              int *                ntypes)
159 {
160     int          n           = 0;
161     const char **my_typename = nullptr;
162
163     if (rt->n > 0)
164     {
165         int         i = 0;
166         const char *p = rt->restype[i];
167         snew(my_typename, n+1);
168         my_typename[n] = p;
169         n              = 1;
170
171         for (i = 1; i < rt->n; i++)
172         {
173             p = rt->restype[i];
174             bool bFound = false;
175             for (int j = 0; j < n && !bFound; j++)
176             {
177                 bFound = !gmx_strcasecmp(p, my_typename[j]);
178             }
179             if (!bFound)
180             {
181                 srenew(my_typename, n+1);
182                 my_typename[n] = p;
183                 n++;
184             }
185         }
186     }
187     *ntypes      = n;
188     *p_typenames = my_typename;
189
190     return 0;
191 }
192
193 gmx_bool
194 gmx_residuetype_is_protein(gmx_residuetype_t *rt, const char *resnm)
195 {
196     gmx_bool    rc;
197     const char *p_type;
198
199     rc = gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
200         gmx_strcasecmp(p_type, "Protein") == 0;
201     return rc;
202 }
203
204 gmx_bool
205 gmx_residuetype_is_dna(gmx_residuetype_t *rt, const char *resnm)
206 {
207     gmx_bool    rc;
208     const char *p_type;
209
210     rc = gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
211         gmx_strcasecmp(p_type, "DNA") == 0;
212     return rc;
213 }
214
215 gmx_bool
216 gmx_residuetype_is_rna(gmx_residuetype_t *rt, const char *resnm)
217 {
218     gmx_bool    rc;
219     const char *p_type;
220
221     rc = gmx_residuetype_get_type(rt, resnm, &p_type) == 0 &&
222         gmx_strcasecmp(p_type, "RNA") == 0;
223     return rc;
224 }
225
226 /* Return the size of the arrays */
227 int
228 gmx_residuetype_get_size(gmx_residuetype_t *rt)
229 {
230     return rt->n;
231 }
232
233 /* Search for a residuetype with name resnm within the
234  * gmx_residuetype database. Return the index if found,
235  * otherwise -1.
236  */
237 int
238 gmx_residuetype_get_index(gmx_residuetype_t *rt, const char *resnm)
239 {
240     int i, rc;
241
242     rc = -1;
243     for (i = 0; i < rt->n && rc; i++)
244     {
245         rc = gmx_strcasecmp(rt->resname[i], resnm);
246     }
247
248     return (0 == rc) ? i-1 : -1;
249 }
250
251 /* Return the name of the residuetype with the given index, or
252  * NULL if not found. */
253 const char *
254 gmx_residuetype_get_name(gmx_residuetype_t *rt, int index)
255 {
256     if (index >= 0 && index < rt->n)
257     {
258         return rt->resname[index];
259     }
260     else
261     {
262         return nullptr;
263     }
264 }