RDKit
Open-source cheminformatics and machine learning.
Toggle main menu visibility
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
21
namespace
RDKit
{
22
class
ROMol
;
23
class
Atom
;
24
class
Bond
;
25
class
BondElectrons;
26
class
AtomElectrons;
27
class
ConjElectrons;
28
class
CEVect2;
29
typedef
std::map<unsigned int, BondElectrons *>
ConjBondMap
;
30
typedef
std::map<unsigned int, AtomElectrons *>
ConjAtomMap
;
31
typedef
std::vector<ConjElectrons *>
CEVect
;
32
typedef
std::vector<CEVect2 *>
CEVect3
;
33
typedef
std::vector<std::uint8_t>
ConjFP
;
34
typedef
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
*/
45
class
RDKIT_GRAPHMOL_EXPORT
ResonanceMolSupplierCallback
{
46
friend
class
ResonanceMolSupplier
;
47
48
public
:
49
ResonanceMolSupplierCallback
() {}
50
virtual
~ResonanceMolSupplierCallback
() {}
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
88
class
RDKIT_GRAPHMOL_EXPORT
ResonanceMolSupplier
{
89
public
:
90
typedef
enum
{
91
/*! include resonance structures whose octets are less complete
92
* than the most octet-complete structure */
93
ALLOW_INCOMPLETE_OCTETS
= (1 << 0),
94
/*! include resonance structures featuring charge separation also
95
* when uncharged resonance structures exist */
96
ALLOW_CHARGE_SEPARATION
= (1 << 1),
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
*/
107
UNCONSTRAINED_CATIONS
= (1 << 3),
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
*/
113
UNCONSTRAINED_ANIONS
= (1 << 4)
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
*/
129
ResonanceMolSupplier
(
ROMol
&
mol
,
unsigned
int
flags
= 0,
130
unsigned
int
maxStructs = 1000);
131
~ResonanceMolSupplier
();
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
*/
158
void
setProgressCallback
(
ResonanceMolSupplierCallback
*callback);
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
*/
163
ResonanceMolSupplierCallback
*
getProgressCallback
()
const
{
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 */
189
ROMol
*
next
();
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
221
ResonanceMolSupplier
(
const
ResonanceMolSupplier
&);
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
RDKit::Atom
The class for representing atoms.
Definition
Atom.h:75
RDKit::Bond
class for representing a bond
Definition
Bond.h:48
RDKit::ROMol
Definition
ROMol.h:344
RDKit::ResonanceMolSupplierCallback
Definition
Resonance.h:45
RDKit::ResonanceMolSupplierCallback::ResonanceMolSupplierCallback
ResonanceMolSupplierCallback()
Definition
Resonance.h:49
RDKit::ResonanceMolSupplierCallback::getMaxStructures
size_t getMaxStructures() const
Definition
Resonance.h:57
RDKit::ResonanceMolSupplierCallback::ResonanceMolSupplier
friend class ResonanceMolSupplier
Definition
Resonance.h:46
RDKit::ResonanceMolSupplierCallback::getNumDiverseStructures
size_t getNumDiverseStructures(unsigned int conjGrpIdx) const
RDKit::ResonanceMolSupplierCallback::operator()
virtual bool operator()()=0
RDKit::ResonanceMolSupplierCallback::~ResonanceMolSupplierCallback
virtual ~ResonanceMolSupplierCallback()
Definition
Resonance.h:50
RDKit::ResonanceMolSupplierCallback::getNumStructures
size_t getNumStructures(unsigned int conjGrpIdx) const
RDKit::ResonanceMolSupplierCallback::getNumConjGrps
unsigned int getNumConjGrps() const
Definition
Resonance.h:52
RDKit::ResonanceMolSupplier
Definition
Resonance.h:88
RDKit::ResonanceMolSupplier::getProgressCallback
ResonanceMolSupplierCallback * getProgressCallback() const
Definition
Resonance.h:163
RDKit::ResonanceMolSupplier::atEnd
bool atEnd()
RDKit::ResonanceMolSupplier::ResonanceMolSupplier
ResonanceMolSupplier(ROMol &mol, unsigned int flags=0, unsigned int maxStructs=1000)
RDKit::ResonanceMolSupplier::mol
const ROMol & mol() const
Definition
Resonance.h:134
RDKit::ResonanceMolSupplier::setProgressCallback
void setProgressCallback(ResonanceMolSupplierCallback *callback)
RDKit::ResonanceMolSupplier::enumerate
void enumerate()
RDKit::ResonanceMolSupplier::wasCanceled
bool wasCanceled() const
Definition
Resonance.h:170
RDKit::ResonanceMolSupplier::reset
void reset()
RDKit::ResonanceMolSupplier::operator[]
ROMol * operator[](unsigned int idx)
RDKit::ResonanceMolSupplier::getAtomConjGrpIdx
int getAtomConjGrpIdx(unsigned int ai) const
RDKit::ResonanceMolSupplier::getIsEnumerated
bool getIsEnumerated()
Definition
Resonance.h:177
RDKit::ResonanceMolSupplier::setNumThreads
void setNumThreads(int numThreads=1)
RDKit::ResonanceMolSupplier::flags
unsigned int flags() const
Definition
Resonance.h:137
RDKit::ResonanceMolSupplier::getNumConjGrps
unsigned int getNumConjGrps() const
Definition
Resonance.h:140
RDKit::ResonanceMolSupplier::length
unsigned int length()
RDKit::ResonanceMolSupplier::UNCONSTRAINED_ANIONS
@ UNCONSTRAINED_ANIONS
Definition
Resonance.h:113
RDKit::ResonanceMolSupplier::ALLOW_CHARGE_SEPARATION
@ ALLOW_CHARGE_SEPARATION
Definition
Resonance.h:96
RDKit::ResonanceMolSupplier::UNCONSTRAINED_CATIONS
@ UNCONSTRAINED_CATIONS
Definition
Resonance.h:107
RDKit::ResonanceMolSupplier::KEKULE_ALL
@ KEKULE_ALL
Definition
Resonance.h:99
RDKit::ResonanceMolSupplier::ALLOW_INCOMPLETE_OCTETS
@ ALLOW_INCOMPLETE_OCTETS
Definition
Resonance.h:93
RDKit::ResonanceMolSupplier::getBondConjGrpIdx
int getBondConjGrpIdx(unsigned int bi) const
RDKit::ResonanceMolSupplier::next
ROMol * next()
RDKit::ResonanceMolSupplier::~ResonanceMolSupplier
~ResonanceMolSupplier()
RDKit::ResonanceMolSupplier::moveTo
void moveTo(unsigned int idx)
export.h
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition
export.h:321
RDKit
Std stuff.
Definition
Abbreviations.h:19
RDKit::CEVect3
std::vector< CEVect2 * > CEVect3
Definition
Resonance.h:32
RDKit::CEMap
std::unordered_map< std::size_t, ConjElectrons * > CEMap
Definition
Resonance.h:34
RDKit::ConjFP
std::vector< std::uint8_t > ConjFP
Definition
Resonance.h:33
RDKit::CEVect
std::vector< ConjElectrons * > CEVect
Definition
Resonance.h:31
RDKit::ConjAtomMap
std::map< unsigned int, AtomElectrons * > ConjAtomMap
Definition
Resonance.h:30
RDKit::ConjBondMap
std::map< unsigned int, BondElectrons * > ConjBondMap
Definition
Resonance.h:29
GraphMol
Resonance.h
Generated on
for RDKit by
1.17.0