File: AtomSymbolMatch.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (311 lines) | stat: -rw-r--r-- 10,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//
//  Copyright (C) 2016 Novartis Institutes for BioMedical Research
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#if defined(__CYGWIN__) && !defined(_GNU_SOURCE)
// -std=c++11 doesn't declare strtok_r
#define _GNU_SOURCE
#endif

#include <string.h>
#include <ctype.h>
#include "Pattern.h"

namespace RDKit {
namespace StructureCheck {

static const char *AtomSymbol[] = {
    // Periodic Table
    "*",   // ANY_ELEMENT = 0,
    "H",   // 1
    "He",  // 2
    "Li", "Be", "B",
    "C",  // 6
    "N",  "O",  "F",
    "Ne",  // 10
    "Na", "Mg", "Al", "Si", "P",  "S",  "Cl",
    "Ar",  // 18
    "K",  "Ca", "Sc", "Ti", "V",  "Cr", "Mn", "Fe", "Co",
    "Ni",  // 28
    "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br",
    "Kr",  // 36
    "Rb", "Sr", "Y",  "Zr", "Nb", "Mo", "Tc", "Ru", "Rh",
    "Pd",  // 46
    "Ag", "Cd", "In", "Sn", "Sb", "Te", "I",
    "Xe",  // 54
    "Cs", "Ba",

    "La",  // 57
    "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy",
    "Ho", "Er", "Tm", "Yb",
    "Lu",  // 71

    "Hf", "Ta", "W",  "Re", "Os", "Ir",
    "Pt",  // 78
    "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At",
    "Rn",  // 86
    "Fr", "Ra",

    "Ac",  // 89
    "Th", "Pa", "U",  "Np", "Pu", "Am", "Cm", "Bk", "Cf",
    "Es", "Fm", "Md", "No",
    "Lr",  // 103

    "Rf", "Db", "Sg", "Bh", "Hn",
    "Mt"  // 109
};

class AtomSymbolMapper {
  std::map<std::string, unsigned> SymbolMap;

 public:
  AtomSymbolMapper() {
    for (unsigned n = 0; n < 110; n++) SymbolMap[AtomSymbol[n]] = n;
  }
  inline unsigned getAtomicNumber(const std::string symbol) const {
    return SymbolMap.find(symbol)->second;
  }
};
static const AtomSymbolMapper smap;

unsigned getAtomicNumber(const std::string symbol) {
  return smap.getAtomicNumber(symbol);
}

// predefined generic atom type sets for use in STRUCHK
static const char *HC_table[] = /* pseudosymbol "G" */
    {"H", "C", nullptr};

static const char *non_metal_hetero_elements[] = /* pseudosymbol "Q" */
    {
        "He",    "B",  "N",  "O",  "F",  "Ne", "Si", "P",  "S",  "Cl", "Ar",
        "As",    "Se", "Br", "Kr", "Sb", "Te", "I",  "Xe", "At", /* "Rn", This
                                                                    element must
                                                                    be
                                                                    removed */
        nullptr, /* because of a trick in utils.c */
};

static const char *metals[] = /* pseudosymbol "M" */
    {
        "Li", "Be", "Na", "Mg", "Al",    "K",  "Ca", "Sc", "Ti", "V",  "Cr",
        "Mn", "Fe", "Co", "Ni", "Cu",    "Zn", "Ga", "Rb", "Sr", "Y",  "Zr",
        "Nb", "Mo", "Tc", "Ru", "Rh",    "Pd", "Ag", "Cd", "In", "Sn", "Cs",
        "Ba", "La", "Ce", "Pr", "Nd",    "Pm", "Sm", "Eu", "Gd", "Tb", "Dy",
        "Ho", "Er", "Tm", "Yb", "Lu",    "Hf", "Ta", "W",  "Re", "Os", "Ir",
        "Pt", "Au", "Hg", "Tl", "Pb",    "Bi", "Po", "Fr", "Ra", "Ac", "Th",
        "Pa", "U",  "Np", "Pu", nullptr,
};

static const char *non_metal_small_solution[] = /* pseudosymbol "Qs" */
    {
        "H", "B", "C",  "N",  "O",  "F", "Si",
        "P", "S", "Cl", "Se", "Br", "I", nullptr,
};

static const char *alkali_metals[] = /* pseudosymbol "alk" */
    {
        "Li", "Na", "K", "Rb", "Cs", "Fr", nullptr,
};

static const char *gr2[] = /* pseudosymbol "gr2" */
    {
        "Be", "Mg", "Ca", "Sr", "Ba", "Ra", nullptr,
};

static const char *gr3[] = /* pseudosymbol "gr3" */
    {
        "B", "Al", "Ga", "In", "Tl", nullptr,
};

static const char *gr4[] = /* pseudosymbol "gr4" */
    {
        "C", "Si", "Ge", "Sn", "Pb", nullptr,
};

static const char *ONS_table[] = /* pseudosymbol "ONS" or "ons" */
    {"O", "N", "S", nullptr};

static const char *on2[] = /* pseudosymbol "on2" */
    {
        "O", "N", "S", "P", "Se", "Te", "Po", nullptr,
};

static const char *halogenes[] = /* pseudosymbol "X" or "hal" */
    {"F", "Cl", "Br", "I", "At", nullptr};

static const char *ha2[] = /* pseudosymbol "ha2" */
    {"Cl", "Br", "I", "At", nullptr};

static const char *transition_metals[] = /* pseudosymbol "trn" */
    {
        "Sc", "Ti", "V",  "Cr", "Mn", "Fe", "Co", "Ni", "Cu",    "Zn", "Y",
        "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd",    "La", "Hf",
        "Ta", "W",  "Re", "Os", "Ir", "Pt", "Au", "Hg", nullptr,
};

static const char *tra[] = /* pseudosymbol "tra" */
    {
        "Ti", "V",  "Cr", "Mn", "Fe", "Co", "Ni", "Zr", "Nb", "Mo", "Tc",
        "Ru", "Rh", "Pd", "Hf", "Ta", "W",  "Re", "Os", "Ir", "Pt", nullptr,
};

static const char *trb[] = /* pseudosymbol "trb" */
    {
        "Cu", "Zn", "Ag", "Cd", "Au", "Hg", nullptr,
};

static const char *tm1[] = /* pseudosymbol "tm1" */
    {
        "Cu", "Ag", "Au", nullptr,
};

static const char *tm2[] = /* pseudosymbol "tm2" */
    {
        "Zn", "Cd", "Hg", nullptr,
};

static const char *tm3[] = /* pseudosymbol "tm3" */
    {
        "Sc", "Y", "La", nullptr,
};

static const char *tm4[] = /* pseudosymbol "tm4" */
    {
        "Ti", "Zr", "Hf", nullptr,
};

static const char *tm5[] = /* pseudosymbol "tm5" */
    {
        "V", "Nb", "Ta", nullptr,
};

static const char *tm6[] = /* pseudosymbol "tm6" */
    {
        "Cr", "Mo", "W", nullptr,
};

static const char *tm7[] = /* pseudosymbol "tm7" */
    {
        "Mn", "Tc", "Re", nullptr,
};

static const char *tm8[] = /* pseudosymbol "tm8" */
    {
        "Fe", "Co", "Ni", "Ru", "Rh", "Pd", "Os", "Ir", "Pt", nullptr,
};

static const char *lanthanoids[] = /* pseudosymbol "lan" */
    {
        "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd",    "Tb",
        "Dy", "Ho", "Er", "Tm", "Yb", "Lu", nullptr,
};

static const char *amino_acids[] = /* pseudosymbol "Ami" or "ami"*/
    {
        "Ala", "Arg", "Asn", "Asp", "Cys", "Gln", "Glu",
        "Gly", "His", "Ile", "Leu", "Lys", "Met", "Phe",
        "Pro", "Ser", "Thr", "Trp", "Tyr", "Val", nullptr,
};

static bool IsInStringTable(const char *symbol, const char *table[]) {
  // Checks if the string symbol is listed in table[]
  const char **stringp;
  for (stringp = table; *stringp; stringp++)
    if (0 == strcmp(*stringp, symbol)) return true;
  return false;
}

bool AtomSymbolMatch(const std::string symbol, const std::string pattern) {
  /*
   * Returns TRUE if symbol is in the comma separated list of atom symbols
   * stored in pattern and FALSE otherwise.
   * There are also a number of standard atom type lists like "alk" for alkali
   * metals or
   * "Q" for non-C/non-H defined above as arrays of strings.
   */
  char *context;
#ifdef WIN32
#define strtok_r strtok_s  // thread safe strtok()
#endif
  const char *atsym = symbol.c_str();
  char pat_buf[512];
  char *tokp;

  strcpy(pat_buf, pattern.c_str());
  for (tokp = strtok_r(pat_buf, ",", &context); tokp;
       tokp = strtok_r((char *)nullptr, ",", &context)) {
    if (islower(*tokp)) {
      if (0 == strcmp("alk", tokp)) {
        if (IsInStringTable(atsym, alkali_metals)) return true;
      } else if (0 == strcmp("gr2", tokp)) {
        if (IsInStringTable(atsym, gr2)) return true;
      } else if (0 == strcmp("gr3", tokp)) {
        if (IsInStringTable(atsym, gr3)) return true;
      } else if (0 == strcmp("gr4", tokp)) {
        if (IsInStringTable(atsym, gr4)) return true;
      } else if (0 == strcmp("ons", tokp)) {
        if (IsInStringTable(atsym, ONS_table)) return true;
      } else if (0 == strcmp("on2", tokp)) {
        if (IsInStringTable(atsym, on2)) return true;
      } else if (0 == strcmp("hal", tokp)) {
        if (IsInStringTable(atsym, halogenes)) return true;
      } else if (0 == strcmp("ha2", tokp)) {
        if (IsInStringTable(atsym, ha2)) return true;
      } else if (0 == strcmp("trn", tokp)) {
        if (IsInStringTable(atsym, transition_metals)) return true;
      } else if (0 == strcmp("tra", tokp)) {
        if (IsInStringTable(atsym, tra)) return true;
      } else if (0 == strcmp("trb", tokp)) {
        if (IsInStringTable(atsym, trb)) return true;
      } else if (0 == strcmp("tm1", tokp)) {
        if (IsInStringTable(atsym, tm1)) return true;
      } else if (0 == strcmp("tm2", tokp)) {
        if (IsInStringTable(atsym, tm2)) return true;
      } else if (0 == strcmp("tm3", tokp)) {
        if (IsInStringTable(atsym, tm3)) return true;
      } else if (0 == strcmp("tm4", tokp)) {
        if (IsInStringTable(atsym, tm4)) return true;
      } else if (0 == strcmp("tm5", tokp)) {
        if (IsInStringTable(atsym, tm5)) return true;
      } else if (0 == strcmp("tm6", tokp)) {
        if (IsInStringTable(atsym, tm6)) return true;
      } else if (0 == strcmp("tm7", tokp)) {
        if (IsInStringTable(atsym, tm7)) return true;
      } else if (0 == strcmp("tm8", tokp)) {
        if (IsInStringTable(atsym, tm8)) return true;
      } else if (0 == strcmp("lan", tokp)) {
        if (IsInStringTable(atsym, lanthanoids)) return true;
      } else if (0 == strcmp("ami", tokp)) {
        if (IsInStringTable(atsym, amino_acids)) return true;
      }
    }
    if (0 == strcmp(atsym, tokp)) return true;
  }

  if (0 == strcmp("A", pattern.c_str()))
    return (0 != strcmp("H", atsym));
  else if (0 == strcmp("Qs", pattern.c_str()))
    return (IsInStringTable(atsym, non_metal_small_solution));
  else if (0 == strcmp("G", pattern.c_str()))
    return (IsInStringTable(atsym, HC_table));
  else if (0 == strcmp("ONS", pattern.c_str()))
    return (IsInStringTable(atsym, ONS_table));
  else if (0 == strcmp("X", pattern.c_str()))
    return (IsInStringTable(atsym, halogenes));
  else if (0 == strcmp("Q", pattern.c_str()))
    return (IsInStringTable(atsym, non_metal_hetero_elements));
  else if (0 == strcmp("M", pattern.c_str()))
    return (IsInStringTable(atsym, metals));
  else if (0 == strcmp("Ami", pattern.c_str()))
    return (IsInStringTable(atsym, amino_acids));
  return false;
}

}  // namespace StructureCheck
}  // namespace RDKit