File: SmilesParse.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (580 lines) | stat: -rw-r--r-- 18,922 bytes parent folder | download | duplicates (2)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//
//  Copyright (C) 2001-2021 Greg Landrum and other RDKit contributors
//
//   @@ 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.
//

// ----------------------------------------------------------------------------------
//  Despite the name of this file, both SMILES and SMARTS parsers are exposed
//  here
//
//  General comments about the parsers:
//   - Atom numbering will be preserved, so input order of atoms==internal order
//
//   - Bond ordering is not, in general, preserved.  Specifically, ring closure
//     bonds will occur at the end of the bond list in general.  Basically ring
//     closure bonds are not constructed until fragments are closed.  This
//     forces
//     some form of reordering.
//
//
//
#include "SmilesParse.h"
#include <RDGeneral/BoostStartInclude.h>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <RDGeneral/BoostEndInclude.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/QueryOps.h>
#include <GraphMol/Chirality.h>
#include <GraphMol/Atropisomers.h>
#include <GraphMol/FileParsers/MolFileStereochem.h>
#include <GraphMol/CIPLabeler/CIPLabeler.h>
#include <GraphMol/Chirality.h>

#include "SmilesParseOps.h"
#include <RDGeneral/RDLog.h>
#include <RDGeneral/Invariant.h>
#include "smiles.tab.hpp"
// NOTE: this is a bit fragile since a lot of the #defines in smiles.tab.hpp
// could prevent the same #defines in smarts.tab.hpp from being read.
// Fortunately if there are actually any problems here, they will inevitably
// show up very quickly in the tests.
#include "smarts.tab.hpp"
#include <list>
#include <utility>
#include <vector>

int yysmiles_lex_init(void **);
int yysmiles_lex_destroy(void *);
size_t setup_smiles_string(const std::string &text, void *);
extern int yysmiles_debug;

int yysmarts_lex_init(void **);
int yysmarts_lex_destroy(void *);
size_t setup_smarts_string(const std::string &text, void *);
extern int yysmarts_debug;
namespace RDKit {
namespace v2 {
namespace SmilesParse {
namespace {

int smarts_parse_helper(const std::string &inp,
                        std::vector<RDKit::RWMol *> &molVect, Atom *&atom,
                        Bond *&bond, int start_tok) {
  std::list<unsigned int> branchPoints;
  void *scanner;
  int res = 1;  // initialize with fail code

  TEST_ASSERT(!yysmarts_lex_init(&scanner));
  try {
    size_t ltrim = setup_smarts_string(inp, scanner);
    unsigned numAtomsParsed = 0;
    unsigned numBondsParsed = 0;
    res = yysmarts_parse(inp.c_str() + ltrim, &molVect, atom, bond,
                         numAtomsParsed, numBondsParsed, &branchPoints, scanner,
                         start_tok);
  } catch (...) {
    yysmarts_lex_destroy(scanner);
    throw;
  }
  yysmarts_lex_destroy(scanner);

  if (res == 1) {
    std::stringstream errout;
    errout << "Failed parsing SMARTS '" << inp << "'";
    throw SmilesParseException(errout.str());
  }
  if (!branchPoints.empty()) {
    throw SmilesParseException("extra open parentheses");
  }

  return res;
}
int smarts_bond_parse(const std::string &inp, Bond *&bond) {
  auto start_tok = static_cast<int>(START_BOND);
  std::vector<RWMol *> molVect;
  Atom *atom = nullptr;
  return smarts_parse_helper(inp, molVect, atom, bond, start_tok);
}

int smarts_atom_parse(const std::string &inp, Atom *&atom) {
  auto start_tok = static_cast<int>(START_ATOM);
  std::vector<RWMol *> molVect;
  Bond *bond = nullptr;
  return smarts_parse_helper(inp, molVect, atom, bond, start_tok);
}

int smarts_parse(const std::string &inp, std::vector<RDKit::RWMol *> &molVect) {
  auto start_tok = static_cast<int>(START_MOL);
  Atom *atom = nullptr;
  Bond *bond = nullptr;
  return smarts_parse_helper(inp, molVect, atom, bond, start_tok);
}

int smiles_parse_helper(const std::string &inp,
                        std::vector<RDKit::RWMol *> &molVect, Atom *&atom,
                        Bond *&bond, int start_tok) {
  std::vector<std::pair<unsigned int, unsigned int>> branchPoints;
  void *scanner;
  int res = 1;  // initialize with fail code
  unsigned numAtomsParsed = 0;
  unsigned numBondsParsed = 0;
  TEST_ASSERT(!yysmiles_lex_init(&scanner));
  size_t ltrim = 0;
  try {
    ltrim = setup_smiles_string(inp, scanner);
    // NOTE: This variable will be used to point to the location of the
    // offending token if we encounter a syntax error
    unsigned int current_token_position = 0;
    res = yysmiles_parse(inp.c_str() + ltrim, &molVect, atom, bond,
                         numAtomsParsed, numBondsParsed, branchPoints, scanner,
                         start_tok, current_token_position);
  } catch (...) {
    yysmiles_lex_destroy(scanner);
    throw;
  }
  yysmiles_lex_destroy(scanner);

  if (res == 1) {
    std::stringstream errout;
    errout << "Failed parsing SMILES '" << inp << "'";
    throw SmilesParseException(errout.str());
  }

  if (!branchPoints.empty()) {
    auto input_smiles = inp.c_str() + ltrim;
    // If there are multiple unclosed brackets, we want to report them all at
    // once. e.g. CC(CC(CC
    for (auto [_, open_bracket_position] : branchPoints) {
      SmilesParseOps::detail::printSyntaxErrorMessage(
          input_smiles, "extra open parentheses", open_bracket_position);
    }

    std::stringstream errout;
    errout << "Failed parsing SMILES '" << inp << "'";
    throw SmilesParseException(errout.str());
  }
  return res;
}

int smiles_bond_parse(const std::string &inp, Bond *&bond) {
  auto start_tok = static_cast<int>(START_BOND);
  std::vector<RWMol *> molVect;
  Atom *atom = nullptr;
  return smiles_parse_helper(inp, molVect, atom, bond, start_tok);
}
int smiles_atom_parse(const std::string &inp, Atom *&atom) {
  auto start_tok = static_cast<int>(START_ATOM);
  std::vector<RWMol *> molVect;
  Bond *bond = nullptr;
  return smiles_parse_helper(inp, molVect, atom, bond, start_tok);
}

int smiles_parse(const std::string &inp, std::vector<RDKit::RWMol *> &molVect) {
  auto start_tok = static_cast<int>(START_MOL);
  Atom *atom = nullptr;
  Bond *bond = nullptr;
  return smiles_parse_helper(inp, molVect, atom, bond, start_tok);
}

typedef enum { BASE = 0, BRANCH, RECURSE } SmaState;

std::string labelRecursivePatterns(const std::string &sma) {
#ifndef NO_AUTOMATIC_SMARTS_RELABELLING
  std::list<SmaState> state;
  std::list<unsigned int> startRecurse;
  std::map<std::string, std::string> patterns;
  std::string res;

  state.push_back(BASE);

  unsigned int pos = 0;
  while (pos < sma.size()) {
    res += sma[pos];
    if (sma[pos] == '$' && pos + 1 < sma.size() && sma[pos + 1] == '(') {
      state.push_back(RECURSE);
      startRecurse.push_back(pos);
      ++pos;
      res += sma[pos];
    } else if (sma[pos] == '(') {
      state.push_back(BRANCH);
    } else if (sma[pos] == ')') {
      if (state.empty() || state.back() == BASE) {
        // seriously bogus input. Just return the input
        // and let the SMARTS parser itself report the error
        return sma;
      }
      SmaState currState = state.back();
      state.pop_back();
      if (currState == RECURSE) {
        unsigned int dollarPos = startRecurse.back();
        startRecurse.pop_back();
        if (pos + 1 >= sma.size() || sma[pos + 1] != '_') {
          std::string recurs = sma.substr(dollarPos, pos - dollarPos + 1);
          std::string label;
          if (patterns.find(recurs) != patterns.end()) {
            // seen this one before, add the label
            label = patterns[recurs];
          } else {
            label = std::to_string(patterns.size() + 100);
            patterns[recurs] = label;
          }
          res += "_" + label;
        }
      } else if (currState == BRANCH) {
        // no need to do anything here.
      }
    }
    ++pos;
  }
  // std::cerr<< " >"<<sma<<"->"<<res<<std::endl;
  return res;
#else
  return sma;
#endif
}
}  // namespace

std::unique_ptr<RWMol> toMol(const std::string &inp,
                             int func(const std::string &,
                                      std::vector<RDKit::RWMol *> &),
                             const std::string &origInp) {
  // empty strings produce empty molecules:
  if (inp.empty()) {
    return std::make_unique<RWMol>();
  }
  std::unique_ptr<RWMol> res;
  std::vector<RDKit::RWMol *> molVect;
  try {
    func(inp, molVect);
    if (!molVect.empty()) {
      res.reset(molVect[0]);
      SmilesParseOps::CloseMolRings(res.get(), false);
      SmilesParseOps::CheckChiralitySpecifications(res.get(), true);
      SmilesParseOps::SetUnspecifiedBondTypes(res.get());
      SmilesParseOps::AdjustAtomChiralityFlags(res.get());
      // No sense leaving this bookmark intact:
      if (res->hasAtomBookmark(ci_RIGHTMOST_ATOM)) {
        res->clearAtomBookmark(ci_RIGHTMOST_ATOM);
      }
      molVect[0] = nullptr;  // NOTE: to avoid leaks on failures, this should
                             // occur last in this if.
    }
  } catch (SmilesParseException &e) {
    std::string nm = "SMILES";
    if (func == smarts_parse) {
      nm = "SMARTS";
    }
    BOOST_LOG(rdErrorLog) << nm << " Parse Error: " << e.what()
                          << " for input: '" << origInp << "'" << std::endl;

    // reset res so that we return a nullptr. We don't want to reset(),
    // because that would delete the mol and leak any unmatched
    // ring closure bonds. These will be cleaned up in the loop below.
    res.release();
  }
  for (auto *molPtr : molVect) {
    if (molPtr) {
      // Clean-up the bond bookmarks when not calling CloseMolRings
      SmilesParseOps::CleanupAfterParseError(molPtr);
      delete molPtr;
    }
  }

  return res;
}

std::unique_ptr<Atom> toAtom(const std::string &inp,
                             int func(const std::string &, Atom *&)) {
  // empty strings produce nullptrs:
  if (inp.empty()) {
    return nullptr;
  }
  Atom *res = nullptr;
  try {
    func(inp, res);
  } catch (SmilesParseException &e) {
    std::string nm = "SMILES";
    if (func != smiles_atom_parse) {
      nm = "SMARTS";
    }
    BOOST_LOG(rdErrorLog) << nm << " Parse Error: " << e.what()
                          << " for input: '" << inp << "'" << std::endl;
    res = nullptr;
  }
  return std::unique_ptr<Atom>(res);
}

std::unique_ptr<Bond> toBond(const std::string &inp,
                             int func(const std::string &, Bond *&)) {
  // empty strings produce nullptrs:
  if (inp.empty()) {
    return nullptr;
  }
  Bond *res = nullptr;
  try {
    func(inp, res);
  } catch (SmilesParseException &e) {
    std::string nm = "SMILES";
    if (func != smiles_bond_parse) {
      nm = "SMARTS";
    }
    BOOST_LOG(rdErrorLog) << nm << " Parse Error: " << e.what()
                          << " for input: '" << inp << "'" << std::endl;
    res = nullptr;
  }
  return std::unique_ptr<Bond>(res);
}

namespace {
// despite the name: works for both SMILES and SMARTS
template <typename T>
void preprocessSmiles(const std::string &smiles, const T &params,
                      std::string &lsmiles, std::string &name,
                      std::string &cxPart) {
  cxPart = "";
  name = "";
  if (params.parseName && !params.allowCXSMILES) {
    size_t sidx = smiles.find_first_of(" \t");
    if (sidx != std::string::npos && sidx != 0) {
      lsmiles = smiles.substr(0, sidx);
      name = boost::trim_copy(smiles.substr(sidx, smiles.size() - sidx));
    }
  } else if (params.allowCXSMILES) {
    size_t sidx = smiles.find_first_of(" \t");
    if (sidx != std::string::npos && sidx != 0) {
      lsmiles = smiles.substr(0, sidx);
      cxPart = boost::trim_copy(smiles.substr(sidx, smiles.size() - sidx));
    }
  }

  if (lsmiles.empty()) {
    lsmiles = smiles;
  }

  if (!params.replacements.empty()) {
    std::string smi = lsmiles;
    for (auto loopAgain = true; loopAgain;) {
      loopAgain = false;
      for (const auto &pr : params.replacements) {
        if (smi.find(pr.first) != std::string::npos) {
          loopAgain = true;
          boost::replace_all(smi, pr.first, pr.second);
        }
      }
    }
    lsmiles = smi;
  }
}
}  // namespace

std::unique_ptr<Atom> AtomFromSmiles(const std::string &smiles) {
  yysmiles_debug = false;

  return toAtom(smiles, smiles_atom_parse);
}

std::unique_ptr<Bond> BondFromSmiles(const std::string &smiles) {
  yysmiles_debug = false;

  return toBond(smiles, smiles_bond_parse);
}

namespace {
template <typename T>
void handleCXPartAndName(RWMol *res, const T &params, const std::string &cxPart,
                         std::string &name) {
  if (!res || cxPart.empty()) {
    return;
  }
  std::string::const_iterator pos = cxPart.cbegin();
  bool cxfailed = false;
  if (params.allowCXSMILES) {
    if (*pos == '|') {
      try {
        SmilesParseOps::parseCXExtensions(*res, cxPart, pos);
      } catch (...) {
        cxfailed = true;
        if (params.strictCXSMILES) {
          throw;
        }
      }
      res->setProp("_CXSMILES_Data", std::string(cxPart.cbegin(), pos));
    } else if (params.strictCXSMILES && !params.parseName &&
               pos != cxPart.cend()) {
      throw RDKit::SmilesParseException(
          "CXSMILES extension does not start with | and parseName=false");
    }
  }
  if (!cxfailed && params.parseName && pos != cxPart.end()) {
    std::string nmpart(pos, cxPart.cend());
    name = boost::trim_copy(nmpart);
  }
}
}  // namespace

std::unique_ptr<RWMol> MolFromSmiles(const std::string &smiles,
                                     const SmilesParserParams &params) {
  // Calling MolFromSmiles in a multithreaded context is generally safe *unless*
  // the value of debugParse is different for different threads. The if
  // statement below avoids a TSAN warning in the case where multiple threads
  // all use the same value for debugParse.
  if (yysmiles_debug != params.debugParse) {
    yysmiles_debug = params.debugParse;
  }

  std::string lsmiles, name, cxPart;
  preprocessSmiles(smiles, params, lsmiles, name, cxPart);
  // strip any leading/trailing whitespace:
  // boost::trim_if(smi,boost::is_any_of(" \t\r\n"));
  auto res = toMol(lsmiles, smiles_parse, lsmiles);
  if (!res) {
    return res;
  }
  handleCXPartAndName(res.get(), params, cxPart, name);

  // get a conformer
  const Conformer *conf = nullptr, *conf3d = nullptr;
  if (res && res->getNumConformers() > 0) {
    for (unsigned int confId = 0; confId < res->getNumConformers(); ++confId) {
      auto *testConf = &res->getConformer(confId);
      if (!testConf->is3D()) {
        if (conf == nullptr) {  // only take the first 2d conf
          conf = testConf;
        }
      } else {
        if (conf3d == nullptr) {  // only take the first 3d conf
          conf3d = testConf;
        }
      }
      if (conf != nullptr && conf3d != nullptr) {
        break;
      }
    }
  }

  if (res->hasProp(SmilesParseOps::detail::_needsDetectAtomStereo)) {
    // we encountered a wedged bond in the CXSMILES,
    // these need to be handled the same way they were in mol files
    res->clearProp(SmilesParseOps::detail::_needsDetectAtomStereo);

    if (conf) {
      MolOps::assignChiralTypesFromBondDirs(*res, conf->getId());
    }
  }

  // if we read a 3D conformer, set the stereo:
  // if (res->getNumConformers() && res->getConformer().is3D()) {
  if (!conf && conf3d) {
    res->updatePropertyCache(false);
    MolOps::assignChiralTypesFrom3D(*res, conf3d->getId(), true);
  }

  if (conf) {
    Atropisomers::detectAtropisomerChirality(*res, conf);
  } else if (conf3d) {
    Atropisomers::detectAtropisomerChirality(*res, conf3d);
  } else {
    Atropisomers::detectAtropisomerChirality(*res, nullptr);
  }

  if (res && (params.sanitize || params.removeHs)) {
    if (params.removeHs) {
      MolOps::RemoveHsParameters rhp;
      rhp.updateExplicitCount = true;
      MolOps::removeHs(*res, rhp, params.sanitize);
    } else if (params.sanitize) {
      MolOps::sanitizeMol(*res);
    }

    if (res->hasProp(SmilesParseOps::detail::_needsDetectBondStereo)) {
      // we encountered either wiggly bond in the CXSMILES,
      // these need to be handled the same way they were in mol files
      if (conf || conf3d) {
        MolOps::clearSingleBondDirFlags(*res);
      }
      MolOps::setDoubleBondNeighborDirections(*res, conf ? conf : conf3d);
    }
    res->clearProp(SmilesParseOps::detail::_needsDetectBondStereo);
    // figure out stereochemistry:
    bool cleanIt = true, force = true, flagPossible = true;
    MolOps::assignStereochemistry(*res, cleanIt, force, flagPossible);
  } else {
    //  we still need to do something about double bond stereochemistry
    //  (was github issue 337)
    //  now that atom stereochem has been perceived, the wedging
    //  information is no longer needed, so we clear
    //  single bond dir flags:
    MolOps::clearSingleBondDirFlags(*res, true);
  }

  if (res && res->hasProp(common_properties::_NeedsQueryScan)) {
    res->clearProp(common_properties::_NeedsQueryScan);
    if (!params.sanitize) {
      // we know that this can be the ring bond query, do ring perception if we
      // need to:
      MolOps::fastFindRings(*res);
    }
    QueryOps::completeMolQueries(res.get(), 0xDEADBEEF);
  }

  if (res) {
    if (!params.skipCleanup) {
      SmilesParseOps::CleanupAfterParsing(res.get());
    }
    if (!name.empty()) {
      res->setProp(common_properties::_Name, name);
    }
  }
  return res;
};

std::unique_ptr<Atom> AtomFromSmarts(const std::string &smiles) {
  yysmarts_debug = false;

  return toAtom(smiles, smarts_atom_parse);
};

std::unique_ptr<Bond> BondFromSmarts(const std::string &smiles) {
  yysmarts_debug = false;

  return toBond(smiles, smarts_bond_parse);
};

std::unique_ptr<RWMol> MolFromSmarts(const std::string &smarts,
                                     const SmartsParserParams &params) {
  // Calling MolFromSmarts in a multithreaded context is generally safe *unless*
  // the value of debugParse is different for different threads. The if
  // statement below avoids a TSAN warning in the case where multiple threads
  // all use the same value for debugParse.
  if (yysmarts_debug != params.debugParse) {
    yysmarts_debug = params.debugParse;
  }

  std::string lsmarts, name, cxPart;
  preprocessSmiles(smarts, params, lsmarts, name, cxPart);

  auto res = toMol(labelRecursivePatterns(lsmarts), smarts_parse, lsmarts);
  handleCXPartAndName(res.get(), params, cxPart, name);
  if (res) {
    if (params.mergeHs) {
      MolOps::mergeQueryHs(*res);
    }
    MolOps::setBondStereoFromDirections(*res);
    if (!params.skipCleanup) {
      SmilesParseOps::CleanupAfterParsing(res.get());
    }
    if (!name.empty()) {
      res->setProp(common_properties::_Name, name);
    }
  }
  return res;
};
}  // namespace SmilesParse
}  // namespace v2
}  // namespace RDKit