File: MolEnumerator.h

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 (283 lines) | stat: -rw-r--r-- 9,311 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
//
//  Copyright (C) 2020-2021 Greg Landrum and T5 Informatics GmbH
//
//   @@ 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.
//
#ifndef RDKIT_MOLENUMERATOR_H
#define RDKIT_MOLENUMERATOR_H

#include <RDGeneral/export.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolBundle.h>

#include <vector>
#include <map>
#include <string>
#include <memory>
#include <limits>

namespace RDKit {
class ChemicalReaction;
namespace MolEnumerator {

namespace detail {
extern const std::string idxPropName;
void preserveOrigIndices(ROMol &mol);
void removeOrigIndices(ROMol &mol);
}  // namespace detail

//! abstract base class for the a molecule enumeration operation
class RDKIT_MOLENUMERATOR_EXPORT MolEnumeratorOp {
 public:
  MolEnumeratorOp() {}
  virtual ~MolEnumeratorOp() {}
  //! returns a vector of the number of possible variations at variability point
  //! covered by this operation
  virtual std::vector<size_t> getVariationCounts() const = 0;
  //! returns a the molecule corresponding to a particular variation
  /*!  which.size() should be equal to the number of variation counts.
   */
  virtual std::unique_ptr<ROMol> operator()(
      const std::vector<size_t> &which) const = 0;
  //! initializes this operation to work on a particular molecule
  virtual void initFromMol(const ROMol &mol) = 0;
  //! polymorphic copy
  virtual std::unique_ptr<MolEnumeratorOp> copy() const = 0;
};

//! Molecule enumeration operation corresponding to position variation bonds
/*! This uses ATTACH and ENDPTS properties on bonds and requires that the bond
 * has one dummy atom (which will be discarded). The other atom of the bond will
 * be connected to the atoms listed in the ENDPTS property
 */
class RDKIT_MOLENUMERATOR_EXPORT PositionVariationOp : public MolEnumeratorOp {
 public:
  PositionVariationOp() {}
  PositionVariationOp(const std::shared_ptr<ROMol> mol) : dp_mol(mol) {
    PRECONDITION(mol, "bad molecule");
    initFromMol();
  }
  PositionVariationOp(const ROMol &mol) : dp_mol(new ROMol(mol)) {
    initFromMol();
  }
  PositionVariationOp(const PositionVariationOp &other)
      : dp_mol(other.dp_mol), d_variationPoints(other.d_variationPoints) {}
  PositionVariationOp &operator=(const PositionVariationOp &other) {
    if (&other == this) {
      return *this;
    }
    dp_mol = other.dp_mol;
    d_variationPoints = other.d_variationPoints;
    return *this;
  }
  //! \override
  std::vector<size_t> getVariationCounts() const override;

  //! \override
  std::unique_ptr<ROMol> operator()(
      const std::vector<size_t> &which) const override;

  //! \override
  void initFromMol(const ROMol &mol) override;

  //! \override
  std::unique_ptr<MolEnumeratorOp> copy() const override {
    return std::unique_ptr<MolEnumeratorOp>(new PositionVariationOp(*this));
  }

 private:
  std::shared_ptr<ROMol> dp_mol{nullptr};
  std::vector<std::pair<unsigned int, std::vector<unsigned int>>>
      d_variationPoints{};
  std::vector<size_t> d_dummiesAtEachPoint{};
  void initFromMol();
};

//! Molecule enumeration operation corresponding to LINKNODES
/*!
 */
class RDKIT_MOLENUMERATOR_EXPORT LinkNodeOp : public MolEnumeratorOp {
 public:
  LinkNodeOp() {}
  LinkNodeOp(const std::shared_ptr<ROMol> mol) : dp_mol(mol) {
    PRECONDITION(mol, "bad molecule");
    initFromMol();
  }
  LinkNodeOp(const ROMol &mol) : dp_mol(new ROMol(mol)) { initFromMol(); }
  LinkNodeOp(const LinkNodeOp &other)
      : dp_mol(other.dp_mol),
        dp_frame(other.dp_frame),
        d_countAtEachPoint(other.d_countAtEachPoint),
        d_variations(other.d_variations),
        d_pointRanges(other.d_pointRanges),
        d_isotopeMap(other.d_isotopeMap),
        d_atomMap(other.d_atomMap) {}
  LinkNodeOp &operator=(const LinkNodeOp &other) {
    if (&other == this) {
      return *this;
    }
    dp_mol = other.dp_mol;
    dp_frame = other.dp_frame;
    d_countAtEachPoint = other.d_countAtEachPoint;
    d_variations = other.d_variations;
    d_pointRanges = other.d_pointRanges;
    d_isotopeMap = other.d_isotopeMap;
    d_atomMap = other.d_atomMap;
    return *this;
  }
  //! \override
  std::vector<size_t> getVariationCounts() const override;

  //! \override
  std::unique_ptr<ROMol> operator()(
      const std::vector<size_t> &which) const override;

  //! \override
  void initFromMol(const ROMol &mol) override;

  //! \override
  std::unique_ptr<MolEnumeratorOp> copy() const override {
    return std::unique_ptr<MolEnumeratorOp>(new LinkNodeOp(*this));
  }

 private:
  std::shared_ptr<ROMol> dp_mol{nullptr};
  std::shared_ptr<RWMol> dp_frame{nullptr};
  std::vector<size_t> d_countAtEachPoint{};
  std::vector<std::tuple<unsigned, unsigned, unsigned>> d_variations;
  std::vector<std::pair<unsigned, unsigned>> d_pointRanges;
  std::map<unsigned, unsigned> d_isotopeMap;
  std::map<unsigned, Atom *> d_atomMap;

  void initFromMol();
};

//! Molecule enumeration operation corresponding to SRUs
/*!
  This should be considered a work-in-progress and to be somewhat fragile.

  NOTE: The SRU labels are parsed to infer the desired number of repetitions
  allowed.

  Known limitations:
  - Overlapping SRUs, i.e. where one monomer is contained within another, are
  not supported

 */
class RDKIT_MOLENUMERATOR_EXPORT RepeatUnitOp : public MolEnumeratorOp {
 public:
  RepeatUnitOp() {};
  RepeatUnitOp(const std::shared_ptr<ROMol> mol) : dp_mol(mol) {
    PRECONDITION(mol, "bad molecule");
    initFromMol();
  };
  RepeatUnitOp(const ROMol &mol) : dp_mol(new ROMol(mol)) { initFromMol(); };
  RepeatUnitOp(const RepeatUnitOp &other)
      : d_maxNumRounds(other.d_maxNumRounds),
        dp_mol(other.dp_mol),
        dp_frame(other.dp_frame),
        d_repeats(other.d_repeats),
        d_countAtEachPoint(other.d_countAtEachPoint),
        d_variations(other.d_variations),
        d_pointRanges(other.d_pointRanges),
        d_isotopeMap(other.d_isotopeMap),
        d_atomMap(other.d_atomMap),
        d_minRepeatCounts(other.d_minRepeatCounts) {};
  RepeatUnitOp &operator=(const RepeatUnitOp &other) {
    if (&other == this) {
      return *this;
    }
    dp_mol = other.dp_mol;
    dp_frame = other.dp_frame;
    d_repeats = other.d_repeats;
    d_countAtEachPoint = other.d_countAtEachPoint;
    d_variations = other.d_variations;
    d_pointRanges = other.d_pointRanges;
    d_isotopeMap = other.d_isotopeMap;
    d_atomMap = other.d_atomMap;
    d_maxNumRounds = other.d_maxNumRounds;
    d_minRepeatCounts = other.d_minRepeatCounts;
    return *this;
  };
  //! \override
  std::vector<size_t> getVariationCounts() const override;

  //! \override
  std::unique_ptr<ROMol> operator()(
      const std::vector<size_t> &which) const override;

  //! \override
  void initFromMol(const ROMol &mol) override;

  //! \override
  std::unique_ptr<MolEnumeratorOp> copy() const override {
    return std::unique_ptr<MolEnumeratorOp>(new RepeatUnitOp(*this));
  }

  static const size_t DEFAULT_REPEAT_COUNT;
  size_t d_maxNumRounds = std::numeric_limits<size_t>::max();

 private:
  std::shared_ptr<ROMol> dp_mol{nullptr};
  std::shared_ptr<RWMol> dp_frame{nullptr};
  std::vector<std::shared_ptr<RWMol>> d_repeats;
  std::vector<RWMol> dp_repeatUnits{};
  std::vector<size_t> d_countAtEachPoint{};
  std::vector<unsigned> d_sruOrder{};
  std::vector<std::tuple<unsigned, unsigned, unsigned>> d_variations;
  std::vector<std::pair<unsigned, unsigned>> d_pointRanges;
  std::map<unsigned, unsigned> d_isotopeMap;
  std::map<unsigned, Atom *> d_atomMap;
  std::vector<size_t> d_minRepeatCounts;

  void initFromMol();
};

//! Parameters used to control the molecule enumeration
struct RDKIT_MOLENUMERATOR_EXPORT MolEnumeratorParams {
  bool sanitize = false;
  size_t maxToEnumerate = 1000;
  bool doRandom = false;  //< not yet implemented
  int randomSeed = -1;    //< not yet implemented
  std::shared_ptr<MolEnumeratorOp> dp_operation;
};

//! Returns a MolBundle containing the molecules resulting from applying the
//! operators contained in \c paramsLists to \c mol.
//! the operators are applied in order
/*!
NOTE: the current implementation does not support molecules which include
both LINKNODE and SRU features.

*/
RDKIT_MOLENUMERATOR_EXPORT MolBundle
enumerate(const ROMol &mol, const std::vector<MolEnumeratorParams> &paramsList);

//! Returns a MolBundle containing the molecules resulting from applying the
//! enumerable operators contained in \c mol.
/*!
\param maxPerOperation: the maximum number of molecules which an individual
operation is allowed to generate

NOTE: the current implementation does not support molecules which include
both LINKNODE and SRU features.

*/
RDKIT_MOLENUMERATOR_EXPORT MolBundle enumerate(const ROMol &mol,
                                               size_t maxPerOperation = 0);

//! Returns a MolBundle containing the molecules resulting from applying the
//! operator contained in \c params to \c mol.
inline MolBundle enumerate(const ROMol &mol,
                           const MolEnumeratorParams &params) {
  std::vector<MolEnumeratorParams> v = {params};
  return enumerate(mol, v);
};
}  // namespace MolEnumerator
}  // namespace RDKit

#endif