RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
Resonance.h
Go to the documentation of this file.
1//
2// Copyright (C) 2015 Paolo Tosco
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10#include <RDGeneral/export.h>
11#ifndef _RESONANCE_H__
12#define _RESONANCE_H__
13#include <memory>
14#include <cstdint>
15
16#include <vector>
17#include <stack>
18#include <map>
19#include <unordered_map>
20
21namespace RDKit {
22class ROMol;
23class Atom;
24class Bond;
25class BondElectrons;
26class AtomElectrons;
27class ConjElectrons;
28class CEVect2;
29typedef std::map<unsigned int, BondElectrons *> ConjBondMap;
30typedef std::map<unsigned int, AtomElectrons *> ConjAtomMap;
31typedef std::vector<ConjElectrons *> CEVect;
32typedef std::vector<CEVect2 *> CEVect3;
33typedef std::vector<std::uint8_t> ConjFP;
34typedef std::unordered_map<std::size_t, ConjElectrons *> CEMap;
35
36/*!
37 * Create a derived class from this abstract base class
38 * and implement the operator()() method.
39 * The operator()() method is called at each iteration of the
40 * algorithm, and provides a mechanism to monitor or stop
41 * its progress.
42 * To have your callback called, pass an instance of your
43 * derived class to ResonanceMolSupplier::setProgressCallback().
44 */
47
48 public:
51 /*! Get the number of conjugated groups this molecule has. */
52 unsigned int getNumConjGrps() const { return d_progress.size(); }
53 /*! Get the maximum number of resonance structures the supplier
54 is allowed to generate.
55 \return The number of structures.
56 */
57 size_t getMaxStructures() const { return d_maxStructs; }
58 /*! Get the number of resonance structures generated so far
59 for a certain conjugated group.
60 \param conjGrpIdx the conjugated group index.
61 \return the number of resonance structures generated so far
62 for this group.
63 */
64 size_t getNumStructures(unsigned int conjGrpIdx) const;
65 /*! Get the number of resonance structures with a different score
66 (i.e., non-degenerate) generated so far for a certain conjugated
67 group.
68 \param conjGrpIdx the conjugated group index.
69 \return the number of non-degenrate resonance structures generated
70 so far for this group.
71 */
72 size_t getNumDiverseStructures(unsigned int conjGrpIdx) const;
73 /*! Pure virtual; this must be implemented in the derived class.
74 \return true if the resonance structure generation should continue;
75 false if the resonance structure generation should stop.
76 */
77 virtual bool operator()() = 0;
78
79 private:
80 struct ResonanceProgress {
81 size_t d_totalStructs;
82 size_t d_diverseStructs;
83 };
84 size_t d_maxStructs{0};
85 std::vector<ResonanceProgress> d_progress;
86};
87
89 public:
90 typedef enum {
91 /*! include resonance structures whose octets are less complete
92 * than the most octet-complete structure */
94 /*! include resonance structures featuring charge separation also
95 * when uncharged resonance structures exist */
97 /*! enumerate all possible degenerate Kekule resonance structures
98 * (the default is to include just one) */
99 KEKULE_ALL = (1 << 2),
100 /*! if the UNCONSTRAINED_CATIONS flag is not set, positively
101 * charged atoms left and right of N with an incomplete octet are
102 * acceptable only if the conjugated group has a positive total
103 * formal charge.
104 * UNCONSTRAINED_CATIONS implies ALLOW_INCOMPLETE_OCTETS
105 * and ALLOW_CHARGE_SEPARATION.
106 */
108 /*! if the UNCONSTRAINED_ANIONS flag is not set, negatively
109 * charged atoms left of N are acceptable only if the conjugated
110 * group has a negative total formal charge.
111 * UNCONSTRAINED_ANIONS implies ALLOW_CHARGE_SEPARATION.
112 */
114 } ResonanceFlags;
115 /*!
116 * \param mol - the starter molecule
117 * \param flags - flags which influence criteria to generate
118 * resonance structures
119 * \param maxStructs - maximum number of complete resonance
120 * structures generated
121 * \param numThreads - the number of threads used to carry out the
122 * resonance structure enumeration (defaults
123 * to 1; 0 selects the number of concurrent
124 * threads supported by the hardware; negative
125 * values are added to the number of
126 * concurrent threads supported by the
127 * hardware)
128 */
130 unsigned int maxStructs = 1000);
132 /*! Returns a reference to the Kekulized form of the ROMol the
133 * ResonanceMolSupplier was initialized with */
134 const ROMol &mol() const { return *d_mol; }
135 /*! Returns the flags the ResonanceMolSupplier was initialized with
136 */
137 unsigned int flags() const { return d_flags; }
138 /*! Returns the number of individual conjugated groups
139 in the molecule */
140 unsigned int getNumConjGrps() const { return d_nConjGrp; }
141 /*! Given a bond index, it returns the index of the conjugated
142 * group the bond belongs to, or -1 if it is not conjugated */
143 int getBondConjGrpIdx(unsigned int bi) const;
144 /*! Given an atom index, it returns the index of the conjugated
145 * group the atom belongs to, or -1 if it is not conjugated */
146 int getAtomConjGrpIdx(unsigned int ai) const;
147 /*! Sets the number of threads to be used to enumerate resonance
148 * structures (defaults to 1; 0 selects the number of concurrent
149 * threads supported by the hardware; negative values are added
150 * to the number of concurrent threads supported by the hardware)
151 */
152 void setNumThreads(int numThreads = 1);
153 /*! Pass a pointer to an instance of a class derived from
154 * ResonanceMolSupplierCallback, which must implement the callback()
155 * method. The ResonanceMolSupplier takes ownership of the pointer,
156 * which must not be deleted.
157 */
159 /*! Get the pointer to the ResonanceMolSupplierCallback subclass
160 * instance (do not delete it, has the ResonanceMolSupplier takes
161 * ownership of the pointer), or nullptr if none was set.
162 */
164 return d_callback.get();
165 }
166 /*! Returns true if the resonance structure generation was canceled
167 * before completion due to the ResMolSupplierProgressCallback
168 * having returned false.
169 */
170 bool wasCanceled() const { return d_wasCanceled; }
171 /*! Ask ResonanceMolSupplier to enumerate resonance structures
172 * (automatically done as soon as any attempt to access them is
173 * made) */
174 void enumerate();
175 /*! Returns true if resonance structure enumeration has already
176 * happened */
177 bool getIsEnumerated() { return d_isEnumerated; }
178 /*! Returns the number of resonance structures in the
179 * ResonanceMolSupplier */
180 unsigned int length();
181 /*! Resets the ResonanceMolSupplier index */
182 void reset();
183 /*! Returns true if there are no more resonance structures left */
184 bool atEnd();
185 /*! Returns a pointer to the next resonance structure as a ROMol,
186 * or NULL if there are no more resonance structures left.
187 * The caller is responsible for freeing memory associated to
188 * the pointer */
190 /*! Sets the ResonanceMolSupplier index to idx */
191 void moveTo(unsigned int idx);
192 /*! Returns a pointer to the resonance structure with index idx as
193 * a ROMol. The index generates complete resonance structures by
194 * combining ConjElectrons objects for the respective conjugated
195 * groups in a breadth-first fashion, in order to return the most
196 * stable complete resonance structures first.
197 * The caller is responsible for freeing memory associated to
198 * the pointer */
199 ROMol *operator[](unsigned int idx);
200
201 private:
202 typedef struct CEPerm {
203 unsigned int idx;
204 std::vector<unsigned int> v;
205 } CEPerm;
206 unsigned int d_nConjGrp;
207 unsigned int d_length;
208 unsigned int d_flags;
209 unsigned int d_maxStructs;
210 unsigned int d_idx;
211 unsigned int d_numThreads;
212 bool d_isEnumerated;
213 bool d_wasCanceled;
214 CEVect3 d_ceVect3;
215 const ROMol *d_mol;
216 std::vector<int> d_bondConjGrpIdx;
217 std::vector<int> d_atomConjGrpIdx;
218 std::vector<size_t> d_enumIdx;
219 std::unique_ptr<ResonanceMolSupplierCallback> d_callback;
220 // disable copy constructor and assignment operator
222 ResonanceMolSupplier &operator=(const ResonanceMolSupplier &);
223 void mainLoop(unsigned int ti, unsigned int nt);
224 void assignConjGrpIdx();
225 void resizeCeVect();
226 void trimCeVect2();
227 void prepEnumIdxVect();
228 void idxToCEPerm(size_t idx, std::vector<unsigned int> &c) const;
229 void setResonanceMolSupplierLength();
230 void buildCEMap(CEMap &ceMap, unsigned int conjGrpIdx);
231 void storeCEMap(const CEMap &ceMap, unsigned int conjGrpIdx);
232 void assignBondsFormalChargesHelper(ROMol &mol,
233 std::vector<unsigned int> &c) const;
234 ROMol *assignBondsFormalCharges(std::vector<unsigned int> &c) const;
235 static bool cePermCompare(const CEPerm *a, const CEPerm *b);
236};
237} // namespace RDKit
238#endif
The class for representing atoms.
Definition Atom.h:75
class for representing a bond
Definition Bond.h:48
size_t getNumDiverseStructures(unsigned int conjGrpIdx) const
size_t getNumStructures(unsigned int conjGrpIdx) const
unsigned int getNumConjGrps() const
Definition Resonance.h:52
ResonanceMolSupplierCallback * getProgressCallback() const
Definition Resonance.h:163
ResonanceMolSupplier(ROMol &mol, unsigned int flags=0, unsigned int maxStructs=1000)
const ROMol & mol() const
Definition Resonance.h:134
void setProgressCallback(ResonanceMolSupplierCallback *callback)
ROMol * operator[](unsigned int idx)
int getAtomConjGrpIdx(unsigned int ai) const
void setNumThreads(int numThreads=1)
unsigned int flags() const
Definition Resonance.h:137
unsigned int getNumConjGrps() const
Definition Resonance.h:140
int getBondConjGrpIdx(unsigned int bi) const
void moveTo(unsigned int idx)
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:321
Std stuff.
std::vector< CEVect2 * > CEVect3
Definition Resonance.h:32
std::unordered_map< std::size_t, ConjElectrons * > CEMap
Definition Resonance.h:34
std::vector< std::uint8_t > ConjFP
Definition Resonance.h:33
std::vector< ConjElectrons * > CEVect
Definition Resonance.h:31
std::map< unsigned int, AtomElectrons * > ConjAtomMap
Definition Resonance.h:30
std::map< unsigned int, BondElectrons * > ConjBondMap
Definition Resonance.h:29