RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
Bond.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Greg Landrum and other RDKit contributors
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 RD_BOND_H
12#define RD_BOND_H
13
14// std stuff
15#include <iostream>
16#include <utility>
17
18// Ours
19#include <RDGeneral/Invariant.h>
20#include <Query/QueryObjects.h>
21#include <RDGeneral/types.h>
22#include <RDGeneral/RDProps.h>
23#include <GraphMol/details.h>
24
25namespace RDKit {
26class ROMol;
27class RWMol;
28class Atom;
29
30//! class for representing a bond
31/*!
32
33 <b>Notes:</b>
34 - many of the methods of Atom require that the Atom be associated
35 with a molecule (an ROMol).
36 - each Bond maintains a Dict of \c properties:
37 - Each \c property is keyed by name and can store an
38 arbitrary type.
39 - \c Properties can be marked as \c calculated, in which case
40 they will be cleared when the \c clearComputedProps() method
41 is called.
42 - Because they have no impact upon chemistry, all \c property
43 operations are \c const, this allows extra flexibility for
44 clients who need to store extra data on Bond objects.
45
46*/
48 friend class RWMol;
49 friend class ROMol;
50
51 public:
52 // FIX: grn...
54
55 //! the type of Bond
56 typedef enum {
57 UNSPECIFIED = 0,
73 DATIVEONE, //!< one-electron dative (e.g. from a C in a Cp ring to a metal)
74 DATIVE, //!< standard two-electron dative
75 DATIVEL, //!< standard two-electron dative
76 DATIVER, //!< standard two-electron dative
78 ZERO //!< Zero-order bond (from
79 // http://pubs.acs.org/doi/abs/10.1021/ci200488k)
80 } BondType;
81
82 //! the bond's direction (for chirality)
83 typedef enum {
84 NONE = 0, //!< no special style
85 BEGINWEDGE, //!< wedged: narrow at begin
86 BEGINDASH, //!< dashed: narrow at begin
87 // FIX: this may not really be adequate
88 ENDDOWNRIGHT, //!< for cis/trans
89 ENDUPRIGHT, //!< ditto
90 EITHERDOUBLE, //!< a "crossed" double bond
91 UNKNOWN, //!< intentionally unspecified stereochemistry
92 } BondDir;
93
94 //! the nature of the bond's stereochem (for cis/trans)
95 typedef enum { // stereochemistry of double bonds
96 STEREONONE = 0, // no special style
97 STEREOANY, // intentionally unspecified
98 // -- Put any true specifications about this point so
99 // that we can do comparisons like if(bond->getStereo()>Bond::STEREOANY)
100 STEREOZ, // Z double bond
101 STEREOE, // E double bond
102 STEREOCIS, // cis double bond
103 STEREOTRANS, // trans double bond
104 STEREOATROPCW, // atropisomer clockwise rotation
105 STEREOATROPCCW, // atropisomer counter clockwise rotation
106 } BondStereo;
107
109 //! construct with a particular BondType
110 explicit Bond(BondType bT);
111 Bond(const Bond &other);
112 virtual ~Bond();
113 Bond &operator=(const Bond &other);
114
115 Bond(Bond &&o) noexcept : RDProps(std::move(o)) {
116 df_isAromatic = o.df_isAromatic;
117 df_isConjugated = o.df_isConjugated;
118 d_bondType = o.d_bondType;
119 d_dirTag = o.d_dirTag;
120 d_stereo = o.d_stereo;
121 d_index = o.d_index;
122 d_beginAtomIdx = o.d_beginAtomIdx;
123 d_endAtomIdx = o.d_endAtomIdx;
124 // NOTE: this is somewhat fraught for bonds associated with molecules since
125 // the molecule will still be pointing to the original object
126 dp_mol = std::exchange(o.dp_mol, nullptr);
127 dp_stereoAtoms = std::exchange(o.dp_stereoAtoms, nullptr);
128 }
129 Bond &operator=(Bond &&o) noexcept {
130 if (this == &o) {
131 return *this;
132 }
133 RDProps::operator=(std::move(o));
134 df_isAromatic = o.df_isAromatic;
135 df_isConjugated = o.df_isConjugated;
136 d_bondType = o.d_bondType;
137 d_dirTag = o.d_dirTag;
138 d_stereo = o.d_stereo;
139 d_index = o.d_index;
140 d_beginAtomIdx = o.d_beginAtomIdx;
141 d_endAtomIdx = o.d_endAtomIdx;
142 // NOTE: this is somewhat fraught for bonds associated with molecules since
143 // the molecule will still be pointing to the original object
144 delete dp_stereoAtoms;
145 dp_mol = std::exchange(o.dp_mol, nullptr);
146 dp_stereoAtoms = std::exchange(o.dp_stereoAtoms, nullptr);
147 return *this;
148 }
149
150 //! returns a copy
151 /*!
152 <b>Note:</b> the caller is responsible for <tt>delete</tt>ing
153 the returned pointer.
154 */
155 virtual Bond *copy() const;
156
157 //! returns our \c bondType
158 BondType getBondType() const { return static_cast<BondType>(d_bondType); }
159 //! sets our \c bondType
160 void setBondType(BondType bT) { d_bondType = bT; }
161 //! \brief returns our \c bondType as a double
162 //! (e.g. SINGLE->1.0, AROMATIC->1.5, etc.)
163 double getBondTypeAsDouble() const;
164
165 //! returns our contribution to the explicit valence of an Atom
166 /*!
167 <b>Notes:</b>
168 - requires an owning molecule
169 */
170 virtual double getValenceContrib(const Atom *at) const;
171
172 //! sets our \c isAromatic flag
173 void setIsAromatic(bool what) { df_isAromatic = what; }
174 //! returns the status of our \c isAromatic flag
175 bool getIsAromatic() const { return df_isAromatic; }
176
177 //! sets our \c isConjugated flag
178 void setIsConjugated(bool what) { df_isConjugated = what; }
179 //! returns the status of our \c isConjugated flag
180 bool getIsConjugated() const { return df_isConjugated; }
181
182 //! returns whether or not this instance belongs to a molecule
183 bool hasOwningMol() const { return dp_mol != nullptr; }
184
185 //! returns a reference to the ROMol that owns this instance
187 PRECONDITION(dp_mol, "no owner");
188 return *dp_mol;
189 }
190 //! sets our owning molecule
191 void setOwningMol(ROMol *other);
192 //! sets our owning molecule
193 void setOwningMol(ROMol &other) { setOwningMol(&other); }
194
195 // inverts the chirality of an atropisomer
197
198 //! returns our index within the ROMol
199 /*!
200 <b>Notes:</b>
201 - this makes no sense if we do not have an owning molecule
202
203 */
204 unsigned int getIdx() const { return d_index; }
205 //! sets our index within the ROMol
206 /*!
207 <b>Notes:</b>
208 - this makes no sense if we do not have an owning molecule
209 - the index should be <tt>< this->getOwningMol()->getNumBonds()</tt>
210 */
211 void setIdx(unsigned int index) { d_index = index; }
212
213 //! returns the index of our begin Atom
214 /*!
215 <b>Notes:</b>
216 - this makes no sense if we do not have an owning molecule
217 */
218 unsigned int getBeginAtomIdx() const { return d_beginAtomIdx; }
219
220 //! returns the index of our end Atom
221 /*!
222 <b>Notes:</b>
223 - this makes no sense if we do not have an owning molecule
224 */
225 unsigned int getEndAtomIdx() const { return d_endAtomIdx; }
226
227 //! given the index of one Atom, returns the index of the other
228 /*!
229 <b>Notes:</b>
230 - this makes no sense if we do not have an owning molecule
231 */
232 unsigned int getOtherAtomIdx(unsigned int thisIdx) const;
233
234 //! sets the index of our begin Atom
235 /*!
236 <b>Notes:</b>
237 - requires an owning molecule
238 */
239 void setBeginAtomIdx(unsigned int what);
240 //! sets the index of our end Atom
241 /*!
242 <b>Notes:</b>
243 - requires an owning molecule
244 */
245 void setEndAtomIdx(unsigned int what);
246
247 //! sets our begin Atom
248 /*!
249 <b>Notes:</b>
250 - requires an owning molecule
251 */
253 //! sets our end Atom
254 /*!
255 <b>Notes:</b>
256 - requires an owning molecule
257 */
258 void setEndAtom(Atom *at);
259
260 //! returns a pointer to our begin Atom
261 /*!
262 <b>Notes:</b>
263 - requires an owning molecule
264 */
266 //! returns a pointer to our end Atom
267 /*!
268 <b>Notes:</b>
269 - requires an owning molecule
270 */
271 Atom *getEndAtom() const;
272 //! returns a pointer to the other Atom
273 /*!
274 <b>Notes:</b>
275 - requires an owning molecule
276 */
277 Atom *getOtherAtom(Atom const *what) const;
278
279 // ------------------------------------
280 // Please see the note in Atom.h for some explanation
281 // of these methods
282 // ------------------------------------
283
284 // This method can be used to distinguish query bonds from standard bonds
285 virtual bool hasQuery() const { return false; }
286
287 // FIX: the const crap here is all mucked up.
288 //! NOT CALLABLE
289 virtual void setQuery(QUERYBOND_QUERY *what);
290 //! NOT CALLABLE
291 virtual QUERYBOND_QUERY *getQuery() const;
292
293 //! NOT CALLABLE
294 virtual void expandQuery(
295 QUERYBOND_QUERY *what,
297 bool maintainOrder = true);
298
299 //! returns whether or not we match the argument
300 /*!
301 <b>Notes:</b>
302 - for Bond objects, "match" means that either one of the Bonds
303 has \c bondType Bond::UNSPECIFIED or both Bonds have the
304 same \c bondType.
305 */
306 virtual bool Match(Bond const *what) const;
307
308 //! sets our direction
309 void setBondDir(BondDir what) { d_dirTag = what; }
310 //! returns our direction
311 BondDir getBondDir() const { return static_cast<BondDir>(d_dirTag); }
312
313 //! sets our stereo code
314 /*!
315 STEREONONE, STEREOANY, STEREOE and STEREOZ can be set without
316 neighboring atoms specified in getStereoAtoms since they are
317 defined by the topology of the molecular graph. In order to set
318 STEREOCIS or STEREOTRANS the neighboring atoms must be set first
319 (using setStereoBonds()) to know what atoms are being considered.
320
321 <b>Notes:</b>
322 - MolOps::findPotentialStereoBonds can be used to set
323 getStereoAtoms before setting CIS/TRANS
324 */
326 PRECONDITION(((what != STEREOCIS && what != STEREOTRANS) ||
327 getStereoAtoms().size() == 2),
328 "Stereo atoms should be specified before specifying CIS/TRANS "
329 "bond stereochemistry")
330 d_stereo = what;
331 }
332 //! returns our stereo code
333 BondStereo getStereo() const { return static_cast<BondStereo>(d_stereo); }
334
335 //! sets the atoms to be considered as reference points for bond stereo
336 /*!
337 These do not necessarily need to be the highest 'ranking' atoms
338 like CIP stereo requires. They can be any arbitrary atoms
339 neighboring the begin and end atoms of this bond
340 respectively. STEREOCIS or STEREOTRANS is then set relative to
341 only these atoms.
342
343 If CIP rankings are desired, use
344 MolOps::findPotentialStereoBonds, but this is a more costly
345 function as it takes the whole molecule topology into account.
346 */
347 void setStereoAtoms(unsigned int bgnIdx, unsigned int endIdx);
348
349 //! returns the indices of our stereo atoms
350 const INT_VECT &getStereoAtoms() const {
351 if (!dp_stereoAtoms) {
352 const_cast<Bond *>(this)->dp_stereoAtoms = new INT_VECT();
353 }
354 return *dp_stereoAtoms;
355 }
356 //! \overload
358 if (!dp_stereoAtoms) {
359 dp_stereoAtoms = new INT_VECT();
360 }
361 return *dp_stereoAtoms;
362 }
363
364 //! calculates any of our lazy \c properties
365 /*!
366 <b>Notes:</b>
367 - requires an owning molecule
368 */
369 void updatePropertyCache(bool strict = true) { (void)strict; }
370
371 protected:
372 //! sets our owning molecule
373 /// void setOwningMol(ROMol *other);
374 //! sets our owning molecule
375 /// void setOwningMol(ROMol &other) { setOwningMol(&other); }
378 std::uint8_t d_bondType;
379 std::uint8_t d_dirTag;
380 std::uint8_t d_stereo;
385
386 void initBond();
387};
388
389inline bool isDative(const Bond &bond) {
390 auto bt = bond.getBondType();
393}
394
395inline bool canSetDoubleBondStereo(const Bond &bond) {
396 auto bondType = bond.getBondType();
397 return (bondType == Bond::SINGLE || bondType == Bond::AROMATIC ||
398 isDative(bond));
399}
400
401inline bool canHaveDirection(const Bond &bond) {
402 auto bondType = bond.getBondType();
403 return (bondType == Bond::SINGLE || bondType == Bond::AROMATIC);
404}
405
406//! returns twice the \c bondType
407//! (e.g. SINGLE->2, AROMATIC->3, etc.)
409
410}; // namespace RDKit
411
412//! allows Bond objects to be dumped to streams
413RDKIT_GRAPHMOL_EXPORT extern std::ostream &operator<<(std::ostream &target,
414 const RDKit::Bond &b);
415
416#endif
RDKIT_GRAPHMOL_EXPORT std::ostream & operator<<(std::ostream &target, const RDKit::Bond &b)
allows Bond objects to be dumped to streams
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
Pulls in all the query types.
Base class for all queries.
Definition Query.h:45
The class for representing atoms.
Definition Atom.h:75
class for representing a bond
Definition Bond.h:47
unsigned int getBeginAtomIdx() const
returns the index of our begin Atom
Definition Bond.h:218
atomindex_t d_index
Definition Bond.h:381
Atom * getEndAtom() const
returns a pointer to our end Atom
void updatePropertyCache(bool strict=true)
calculates any of our lazy properties
Definition Bond.h:369
double getBondTypeAsDouble() const
returns our bondType as a double (e.g. SINGLE->1.0, AROMATIC->1.5, etc.)
void setEndAtomIdx(unsigned int what)
sets the index of our end Atom
unsigned int getOtherAtomIdx(unsigned int thisIdx) const
given the index of one Atom, returns the index of the other
INT_VECT * dp_stereoAtoms
Definition Bond.h:384
virtual ~Bond()
BondType
the type of Bond
Definition Bond.h:56
@ DATIVER
standard two-electron dative
Definition Bond.h:76
@ QUINTUPLE
Definition Bond.h:62
@ TWOANDAHALF
Definition Bond.h:65
@ HYDROGEN
Definition Bond.h:71
@ THREECENTER
Definition Bond.h:72
@ FIVEANDAHALF
Definition Bond.h:68
@ QUADRUPLE
Definition Bond.h:61
@ FOURANDAHALF
Definition Bond.h:67
@ AROMATIC
Definition Bond.h:69
@ THREEANDAHALF
Definition Bond.h:66
@ IONIC
Definition Bond.h:70
@ DOUBLE
Definition Bond.h:59
@ HEXTUPLE
Definition Bond.h:63
@ TRIPLE
Definition Bond.h:60
@ ONEANDAHALF
Definition Bond.h:64
@ DATIVE
standard two-electron dative
Definition Bond.h:74
@ SINGLE
Definition Bond.h:58
@ OTHER
Definition Bond.h:77
@ DATIVEONE
one-electron dative (e.g. from a C in a Cp ring to a metal)
Definition Bond.h:73
@ DATIVEL
standard two-electron dative
Definition Bond.h:75
Bond(const Bond &other)
void initBond()
void setIsAromatic(bool what)
sets our isAromatic flag
Definition Bond.h:173
unsigned int getIdx() const
returns our index within the ROMol
Definition Bond.h:204
void setBeginAtom(Atom *at)
sets our begin Atom
bool getIsConjugated() const
returns the status of our isConjugated flag
Definition Bond.h:180
bool hasOwningMol() const
returns whether or not this instance belongs to a molecule
Definition Bond.h:183
virtual QUERYBOND_QUERY * getQuery() const
NOT CALLABLE.
virtual bool hasQuery() const
Definition Bond.h:285
void setStereoAtoms(unsigned int bgnIdx, unsigned int endIdx)
sets the atoms to be considered as reference points for bond stereo
void setBeginAtomIdx(unsigned int what)
sets the index of our begin Atom
void setIsConjugated(bool what)
sets our isConjugated flag
Definition Bond.h:178
Queries::Query< int, Bond const *, true > QUERYBOND_QUERY
Definition Bond.h:53
BondType getBondType() const
returns our bondType
Definition Bond.h:158
ROMol & getOwningMol() const
returns a reference to the ROMol that owns this instance
Definition Bond.h:186
virtual void setQuery(QUERYBOND_QUERY *what)
NOT CALLABLE.
Bond & operator=(const Bond &other)
void setOwningMol(ROMol *other)
sets our owning molecule
void setBondType(BondType bT)
sets our bondType
Definition Bond.h:160
bool df_isAromatic
Definition Bond.h:376
atomindex_t d_beginAtomIdx
Definition Bond.h:382
Atom * getOtherAtom(Atom const *what) const
returns a pointer to the other Atom
INT_VECT & getStereoAtoms()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Bond.h:357
BondDir
the bond's direction (for chirality)
Definition Bond.h:83
@ EITHERDOUBLE
a "crossed" double bond
Definition Bond.h:90
@ BEGINWEDGE
wedged: narrow at begin
Definition Bond.h:85
@ ENDUPRIGHT
ditto
Definition Bond.h:89
@ UNKNOWN
intentionally unspecified stereochemistry
Definition Bond.h:91
@ ENDDOWNRIGHT
for cis/trans
Definition Bond.h:88
@ BEGINDASH
dashed: narrow at begin
Definition Bond.h:86
Bond(Bond &&o) noexcept
Definition Bond.h:115
virtual void expandQuery(QUERYBOND_QUERY *what, Queries::CompositeQueryType how=Queries::COMPOSITE_AND, bool maintainOrder=true)
NOT CALLABLE.
Atom * getBeginAtom() const
returns a pointer to our begin Atom
void setOwningMol(ROMol &other)
sets our owning molecule
Definition Bond.h:193
const INT_VECT & getStereoAtoms() const
returns the indices of our stereo atoms
Definition Bond.h:350
std::uint8_t d_stereo
Definition Bond.h:380
BondStereo getStereo() const
returns our stereo code
Definition Bond.h:333
void setStereo(BondStereo what)
sets our stereo code
Definition Bond.h:325
void setIdx(unsigned int index)
sets our index within the ROMol
Definition Bond.h:211
unsigned int getEndAtomIdx() const
returns the index of our end Atom
Definition Bond.h:225
virtual Bond * copy() const
returns a copy
std::uint8_t d_dirTag
Definition Bond.h:379
std::uint8_t d_bondType
Definition Bond.h:378
Bond & operator=(Bond &&o) noexcept
Definition Bond.h:129
virtual double getValenceContrib(const Atom *at) const
returns our contribution to the explicit valence of an Atom
void setEndAtom(Atom *at)
sets our end Atom
ROMol * dp_mol
Definition Bond.h:383
void setBondDir(BondDir what)
sets our direction
Definition Bond.h:309
BondStereo
the nature of the bond's stereochem (for cis/trans)
Definition Bond.h:95
@ STEREOTRANS
Definition Bond.h:103
@ STEREOE
Definition Bond.h:101
@ STEREOZ
Definition Bond.h:100
@ STEREOATROPCCW
Definition Bond.h:105
@ STEREOCIS
Definition Bond.h:102
@ STEREOATROPCW
Definition Bond.h:104
@ STEREOANY
Definition Bond.h:97
bool getIsAromatic() const
returns the status of our isAromatic flag
Definition Bond.h:175
virtual bool Match(Bond const *what) const
returns whether or not we match the argument
BondDir getBondDir() const
returns our direction
Definition Bond.h:311
bool invertChirality()
Bond(BondType bT)
construct with a particular BondType
bool df_isConjugated
Definition Bond.h:377
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:233
@ COMPOSITE_AND
Std stuff.
std::vector< int > INT_VECT
Definition types.h:284
bool rdvalue_is(const RDValue_cast_t)
bool canHaveDirection(const Bond &bond)
Definition Bond.h:401
bool canSetDoubleBondStereo(const Bond &bond)
Definition Bond.h:395
bool isDative(const Bond &bond)
Definition Bond.h:389
std::uint32_t atomindex_t
Definition details.h:14
RDKIT_GRAPHMOL_EXPORT uint8_t getTwiceBondType(const RDKit::Bond &b)