RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
ROMol.h
Go to the documentation of this file.
1//
2// Copyright (C) 2003-2022 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/*! \file ROMol.h
11
12 \brief Defines the primary molecule class \c ROMol as well as associated
13 typedefs
14
15*/
16
17#include <RDGeneral/export.h>
18#ifndef RD_ROMOL_H
19#define RD_ROMOL_H
20
21/// Std stuff
22#include <cstddef>
23#include <iterator>
24#include <utility>
25#include <map>
26#include <ranges>
27#include <limits>
28
29// boost stuff
31#include <boost/graph/adjacency_list.hpp>
32#include <boost/smart_ptr.hpp>
33#include <boost/dynamic_bitset.hpp>
34
35#ifdef RDK_USE_BOOST_SERIALIZATION
36#include <boost/serialization/split_member.hpp>
37#endif
39
40// our stuff
41#include <RDGeneral/types.h>
42#include <RDGeneral/RDProps.h>
43#include "Atom.h"
44#include "Bond.h"
45#include "Conformer.h"
46#include "SubstanceGroup.h"
47#include "StereoGroup.h"
48#include "RingInfo.h"
49
50namespace RDKit {
51class SubstanceGroup;
52class Atom;
53class Bond;
54//! This is the BGL type used to store the topology:
55typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
56 Atom *, Bond *>
58class MolPickler;
59class RWMol;
60class QueryAtom;
61class QueryBond;
62class RingInfo;
63
64template <class T1, class T2>
65class AtomIterator_;
66class BondIterator_;
68
69template <class T1, class T2>
71template <class T1, class T2>
73template <class T1, class T2>
75template <class T1, class T2>
77
81
82//! ROMol is a molecule class that is intended to have a fixed topology
83/*!
84 This is the primary class for most molecule operations.
85
86 If you need to be manipulating the molecule (e.g. adding or deleting
87 atoms or bonds, use an RWMol instead.
88
89 <b>Notes:</b>
90 - each ROMol maintains a Dict of \c properties:
91 - Each \c property is keyed by name and can store an
92 arbitrary type.
93 - \c Properties can be marked as \c calculated, in which case
94 they will be cleared when the \c clearComputedProps() method
95 is called.
96 - Because they have no impact upon chemistry, all \c property
97 operations are \c const, this allows extra flexibility for
98 clients who need to store extra data on ROMol objects.
99
100 - each ROMol has collections of \c bookmarks for Atoms and Bonds:
101 - the Atom bookmarks and Bond bookmarks are stored separately
102 from each other
103 - each \c bookmark, an integer, can map to more than one
104 Atom or Bond
105 - these are currently used in molecule construction, but
106 could also be useful for reaction mapping and the like
107
108 - information about rings (SSSR and the like) is stored in the
109 molecule's RingInfo pointer.
110
111 */
112
113//! \name C++11 Iterators
114
115template <class Graph, class Vertex,
116 class Iterator = typename Graph::vertex_iterator>
118 Graph *graph;
119 Iterator vstart, vend;
120
121 struct CXXAtomIter {
122 using iterator_category = std::random_access_iterator_tag;
123 using difference_type = std::ptrdiff_t;
124 using value_type = Vertex;
125 using pointer = Vertex *;
126 using reference = Vertex &;
127 using const_reference = Vertex const &;
128
129 Graph *graph = nullptr;
130 Iterator pos;
131
133
134 CXXAtomIter(Graph *graph, Iterator pos) : graph(graph), pos(pos) {}
135
136 // we only return const references since we don't want clients modifying the
137 // graph itself through these iterators
138 const_reference operator*() const { return (*graph)[*pos]; }
139 // we only return const references since we don't want clients modifying the
140 // graph itself through these iterators
142 return (*graph)[*(pos + n)];
143 }
144
146 ++pos;
147 return *this;
148 }
150 CXXAtomIter tmp = *this;
151 ++(*this);
152 return tmp;
153 }
155 --pos;
156 return *this;
157 }
159 return CXXAtomIter(graph, pos + n);
160 }
162 return CXXAtomIter(graph, pos - n);
163 }
164
166 CXXAtomIter tmp = *this;
167 --(*this);
168 return tmp;
169 }
171 pos += n;
172 return *this;
173 }
175 pos -= n;
176 return *this;
177 }
179 return pos - other.pos;
180 }
182 return CXXAtomIter(it.graph, it.pos + n);
183 }
184
185 bool operator==(const CXXAtomIter &other) const {
186 return graph == other.graph && pos == other.pos;
187 }
188 bool operator!=(const CXXAtomIter &other) const {
189 return !(*this == other);
190 }
191 bool operator<(const CXXAtomIter &other) const { return pos < other.pos; }
192 bool operator<=(const CXXAtomIter &other) const { return pos <= other.pos; }
193 bool operator>(const CXXAtomIter &other) const { return pos > other.pos; }
194 bool operator>=(const CXXAtomIter &other) const { return pos >= other.pos; }
195 };
196
198 std::tie(vstart, vend) = boost::vertices(*graph);
199 }
200 CXXAtomIterator(Graph *graph, Iterator start, Iterator end)
201 : graph(graph), vstart(start), vend(end) {};
202 CXXAtomIter begin() { return {graph, vstart}; }
203 CXXAtomIter end() { return {graph, vend}; }
204 size_t size() const { return vend - vstart; }
205};
206// clang-format off
207static_assert(
208 std::ranges::random_access_range<CXXAtomIterator<MolGraph, Atom *>>
209 and std::ranges::sized_range<CXXAtomIterator<MolGraph, Atom *>>
210 );
211// clang-format on
212
213template <class Graph, class Edge,
214 class Iterator = typename Graph::edge_iterator>
216 Graph *graph;
217 Iterator vstart, vend;
218
219 struct CXXBondIter {
220 using iterator_category = std::bidirectional_iterator_tag;
221 using difference_type = std::ptrdiff_t;
222 using value_type = Edge;
223 using pointer = Edge *;
224 using reference = Edge &;
225 using const_reference = Edge const &;
226
227 Graph *graph = nullptr;
228 Iterator pos;
229
231
232 CXXBondIter(Graph *graph, Iterator pos) : graph(graph), pos(pos) {}
233 // we only return const references since we don't want clients modifying the
234 // graph itself through these iterators
235 const_reference operator*() const { return (*graph)[*pos]; }
237 ++pos;
238 return *this;
239 }
241 CXXBondIter tmp = *this;
242 ++(*this);
243 return tmp;
244 }
246 --pos;
247 return *this;
248 }
250 CXXBondIter tmp = *this;
251 --(*this);
252 return tmp;
253 }
254 bool operator==(const CXXBondIter &other) const {
255 return graph == other.graph && pos == other.pos;
256 }
257 bool operator!=(const CXXBondIter &other) const {
258 return !(*this == other);
259 }
260 };
261
263 auto vs = boost::edges(*graph);
264 vstart = vs.first;
265 vend = vs.second;
266 }
267 CXXBondIterator(Graph *graph, Iterator start, Iterator end)
268 : graph(graph), vstart(start), vend(end) {};
269 CXXBondIter begin() { return {graph, vstart}; }
270 CXXBondIter end() { return {graph, vend}; }
271 size_t size() const {
272 // bond iterators aren't random access, so we can't just do vend - vstart
273 // here. Instead we have to iterate through;
274 size_t count = 0;
275 for (auto it = vstart; it != vend; ++it) {
276 ++count;
277 }
278 return count;
279 }
280};
281// we don't model sized_range because size() is O(N)
282static_assert(
283 std::ranges::bidirectional_range<CXXBondIterator<MolGraph, Bond *>>);
284
286 public:
287 friend class MolPickler;
288 friend class RWMol;
289
290 //! \cond TYPEDEFS
291
292 //! \name typedefs
293 //! @{
294 typedef MolGraph::vertex_descriptor vertex_descriptor;
295 typedef MolGraph::edge_descriptor edge_descriptor;
296
297 typedef MolGraph::edge_iterator EDGE_ITER;
298 typedef MolGraph::out_edge_iterator OEDGE_ITER;
299 typedef MolGraph::vertex_iterator VERTEX_ITER;
300 typedef MolGraph::adjacency_iterator ADJ_ITER;
301 typedef std::pair<EDGE_ITER, EDGE_ITER> BOND_ITER_PAIR;
302 typedef std::pair<OEDGE_ITER, OEDGE_ITER> OBOND_ITER_PAIR;
303 typedef std::pair<VERTEX_ITER, VERTEX_ITER> ATOM_ITER_PAIR;
304 typedef std::pair<ADJ_ITER, ADJ_ITER> ADJ_ITER_PAIR;
305
306 typedef std::vector<Atom *> ATOM_PTR_VECT;
307 typedef ATOM_PTR_VECT::iterator ATOM_PTR_VECT_I;
308 typedef ATOM_PTR_VECT::const_iterator ATOM_PTR_VECT_CI;
309 typedef std::vector<Bond *> BOND_PTR_VECT;
310 typedef BOND_PTR_VECT::iterator BOND_PTR_VECT_I;
311 typedef BOND_PTR_VECT::const_iterator BOND_PTR_VECT_CI;
312
313 typedef std::list<Atom *> ATOM_PTR_LIST;
314 typedef ATOM_PTR_LIST::iterator ATOM_PTR_LIST_I;
315 typedef ATOM_PTR_LIST::const_iterator ATOM_PTR_LIST_CI;
316 typedef std::list<Bond *> BOND_PTR_LIST;
317 typedef BOND_PTR_LIST::iterator BOND_PTR_LIST_I;
318 typedef BOND_PTR_LIST::const_iterator BOND_PTR_LIST_CI;
319
320 // list of conformations
321 typedef std::list<CONFORMER_SPTR> CONF_SPTR_LIST;
322 typedef CONF_SPTR_LIST::iterator CONF_SPTR_LIST_I;
323 typedef CONF_SPTR_LIST::const_iterator CONF_SPTR_LIST_CI;
324 typedef std::pair<CONF_SPTR_LIST_I, CONF_SPTR_LIST_I> CONFS_I_PAIR;
325
326 // ROFIX: these will need to be readonly somehow?
327 typedef std::map<int, ATOM_PTR_LIST> ATOM_BOOKMARK_MAP;
328 typedef std::map<int, BOND_PTR_LIST> BOND_BOOKMARK_MAP;
329
330 typedef class AtomIterator_<Atom, ROMol> AtomIterator;
331 typedef class AtomIterator_<const Atom, const ROMol> ConstAtomIterator;
332 typedef class BondIterator_ BondIterator;
333 typedef class ConstBondIterator_ ConstBondIterator;
334 typedef class AromaticAtomIterator_<Atom, ROMol> AromaticAtomIterator;
335 typedef class AromaticAtomIterator_<const Atom, const ROMol>
336 ConstAromaticAtomIterator;
337 typedef class HeteroatomIterator_<Atom, ROMol> HeteroatomIterator;
338 typedef class HeteroatomIterator_<const Atom, const ROMol>
339 ConstHeteroatomIterator;
340 typedef class QueryAtomIterator_<Atom, ROMol> QueryAtomIterator;
341 typedef class QueryAtomIterator_<const Atom, const ROMol>
342 ConstQueryAtomIterator;
343 typedef class MatchingAtomIterator_<Atom, ROMol> MatchingAtomIterator;
344 typedef class MatchingAtomIterator_<const Atom, const ROMol>
345 ConstMatchingAtomIterator;
346
347 typedef CONF_SPTR_LIST_I ConformerIterator;
348 typedef CONF_SPTR_LIST_CI ConstConformerIterator;
349
350 //! @}
351 //! \endcond
352
353 //! C++11 Range iterator
354 /*!
355 <b>Usage</b>
356 \code
357 for(auto atom : mol.atoms()) {
358 atom->getIdx();
359 };
360 \endcode
361 */
362
364
366 return {&d_graph};
367 }
368
370 atomNeighbors(Atom const *at) const {
371 auto pr = getAtomNeighbors(at);
372 return {&d_graph, pr.first, pr.second};
373 }
374
376 Atom const *at) {
377 auto pr = getAtomNeighbors(at);
378 return {&d_graph, pr.first, pr.second};
379 }
380
382 atomBonds(Atom const *at) const {
383 auto pr = getAtomBonds(at);
384 return {&d_graph, pr.first, pr.second};
385 }
386
388 Atom const *at) {
389 auto pr = getAtomBonds(at);
390 return {&d_graph, pr.first, pr.second};
391 }
392
393 /*!
394 <b>Usage</b>
395 \code
396 for(auto bond : mol.bonds()) {
397 bond->getIdx();
398 };
399 \endcode
400 */
401
403
405 return {&d_graph};
406 }
407
408 ROMol() : RDProps() { initMol(); }
409
410 //! copy constructor with a twist
411 /*!
412 \param other the molecule to be copied
413 \param quickCopy (optional) if this is true, the resulting ROMol will not
414 copy any of the properties or bookmarks and conformers from \c other.
415 This can
416 make the copy substantially faster (thus the name).
417 \param confId (optional) if this is >=0, the resulting ROMol will contain
418 only
419 the specified conformer from \c other.
420 */
421 ROMol(const ROMol &other, bool quickCopy = false, int confId = -1)
422 : RDProps() {
423 dp_ringInfo = nullptr;
424 initFromOther(other, quickCopy, confId);
425 numBonds = rdcast<unsigned int>(boost::num_edges(d_graph));
426 }
427 //! construct a molecule from a pickle string
428 ROMol(const std::string &binStr);
429 //! construct a molecule from a pickle string
430 ROMol(const std::string &binStr, unsigned int propertyFlags);
431
432 ROMol(ROMol &&o) noexcept
433 : RDProps(std::move(o)),
434 d_graph(std::move(o.d_graph)),
435 d_atomBookmarks(std::move(o.d_atomBookmarks)),
436 d_bondBookmarks(std::move(o.d_bondBookmarks)),
437 d_confs(std::move(o.d_confs)),
438 d_sgroups(std::move(o.d_sgroups)),
439 d_stereo_groups(std::move(o.d_stereo_groups)),
440 numBonds(o.numBonds) {
441 for (auto atom : atoms()) {
442 atom->setOwningMol(this);
443 }
444 for (auto bond : bonds()) {
445 bond->setOwningMol(this);
446 }
447 for (auto conf : d_confs) {
448 conf->setOwningMol(this);
449 }
450 for (auto &sg : d_sgroups) {
451 sg.setOwningMol(this);
452 }
453 o.d_graph.clear();
454 o.numBonds = 0;
455 dp_ringInfo = std::exchange(o.dp_ringInfo, nullptr);
456 dp_delAtoms = std::exchange(o.dp_delAtoms, nullptr);
457 dp_delBonds = std::exchange(o.dp_delBonds, nullptr);
458 }
459 ROMol &operator=(ROMol &&o) noexcept {
460 if (this == &o) {
461 return *this;
462 }
463 RDProps::operator=(std::move(o));
464 d_graph = std::move(o.d_graph);
465 d_atomBookmarks = std::move(o.d_atomBookmarks);
466 d_bondBookmarks = std::move(o.d_bondBookmarks);
467 if (dp_ringInfo) {
468 delete dp_ringInfo;
469 }
470 dp_ringInfo = std::exchange(o.dp_ringInfo, nullptr);
471
472 d_confs = std::move(o.d_confs);
473 d_sgroups = std::move(o.d_sgroups);
474 d_stereo_groups = std::move(o.d_stereo_groups);
475 dp_delAtoms = std::exchange(o.dp_delAtoms, nullptr);
476 dp_delBonds = std::exchange(o.dp_delBonds, nullptr);
477 numBonds = o.numBonds;
478 o.numBonds = 0;
479
480 for (auto atom : atoms()) {
481 atom->setOwningMol(this);
482 }
483 for (auto bond : bonds()) {
484 bond->setOwningMol(this);
485 }
486 for (auto conf : d_confs) {
487 conf->setOwningMol(this);
488 }
489 for (auto &sg : d_sgroups) {
490 sg.setOwningMol(this);
491 }
492
493 o.d_graph.clear();
494 return *this;
495 }
496
498 delete; // disable assignment, RWMol's support assignment
499
500 virtual ~ROMol() { destroy(); }
501
502 //! @}
503 //! \name Atoms
504 //! @{
505
506 //! returns our number of atoms
507 inline unsigned int getNumAtoms() const {
508 return rdcast<unsigned int>(boost::num_vertices(d_graph));
509 }
510 unsigned int getNumAtoms(bool onlyExplicit) const;
511 //! returns our number of heavy atoms (atomic number > 1)
512 unsigned int getNumHeavyAtoms() const;
513 //! returns a pointer to a particular Atom
514 Atom *getAtomWithIdx(unsigned int idx);
515 //! \overload
516 const Atom *getAtomWithIdx(unsigned int idx) const;
517 //! \overload
518 template <class U>
519 Atom *getAtomWithIdx(const U idx) {
521 }
522 //! \overload
523 template <class U>
524 const Atom *getAtomWithIdx(const U idx) const {
526 }
527 //! returns the degree (number of neighbors) of an Atom in the graph
528 unsigned int getAtomDegree(const Atom *at) const;
529 //! @}
530
531 //! \name Bonds
532 //! @{
533
534 //! returns our number of Bonds
535 unsigned int getNumBonds(bool onlyHeavy = 1) const;
536 //! returns a pointer to a particular Bond
537 Bond *getBondWithIdx(unsigned int idx);
538 //! \overload
539 const Bond *getBondWithIdx(unsigned int idx) const;
540 //! \overload
541 template <class U>
542 Bond *getBondWithIdx(const U idx) {
544 }
545 //! \overload
546 template <class U>
547 const Bond *getBondWithIdx(const U idx) const {
549 }
550 //! returns a pointer to the bond between two atoms, Null on failure
551 Bond *getBondBetweenAtoms(unsigned int idx1, unsigned int idx2);
552 //! \overload
553 const Bond *getBondBetweenAtoms(unsigned int idx1, unsigned int idx2) const;
554 //! \overload
555 template <class U, class V>
556 Bond *getBondBetweenAtoms(const U idx1, const V idx2) {
559 }
560 //! \overload
561 template <class U, class V>
562 const Bond *getBondBetweenAtoms(const U idx1, const V idx2) const {
565 }
566
567 //! @}
568
569 //! \name Bookmarks
570 //! @{
571
572 //! associates an Atom pointer with a bookmark
573 void setAtomBookmark(Atom *at, int mark) {
574 d_atomBookmarks[mark].push_back(at);
575 }
576 //! associates an Atom pointer with a bookmark
577 void replaceAtomBookmark(Atom *at, int mark) {
578 d_atomBookmarks[mark].clear();
579 d_atomBookmarks[mark].push_back(at);
580 }
581 //! returns the first Atom associated with the \c bookmark provided
583 //! returns the Atom associated with the \c bookmark provided
584 //! a check is made to ensure it is the only atom with that bookmark
586 //! returns all Atoms associated with the \c bookmark provided
587 ATOM_PTR_LIST &getAllAtomsWithBookmark(int mark);
588 //! removes a \c bookmark from our collection
589 void clearAtomBookmark(int mark);
590 //! removes a particular Atom from the list associated with the \c bookmark
591 void clearAtomBookmark(int mark, const Atom *atom);
592
593 //! blows out all atomic \c bookmarks
594 void clearAllAtomBookmarks() { d_atomBookmarks.clear(); }
595 //! queries whether or not any atoms are associated with a \c bookmark
596 bool hasAtomBookmark(int mark) const { return d_atomBookmarks.count(mark); }
597 //! returns a pointer to all of our atom \c bookmarks
598 ATOM_BOOKMARK_MAP *getAtomBookmarks() { return &d_atomBookmarks; }
599
600 //! associates a Bond pointer with a bookmark
601 void setBondBookmark(Bond *bond, int mark) {
602 d_bondBookmarks[mark].push_back(bond);
603 }
604 //! returns the first Bond associated with the \c bookmark provided
606 //! returns the Bond associated with the \c bookmark provided
607 //! a check is made to ensure it is the only bond with that bookmark
609 //! returns all bonds associated with the \c bookmark provided
610 BOND_PTR_LIST &getAllBondsWithBookmark(int mark);
611 //! removes a \c bookmark from our collection
612 void clearBondBookmark(int mark);
613 //! removes a particular Bond from the list associated with the \c bookmark
614 void clearBondBookmark(int mark, const Bond *bond);
615
616 //! blows out all bond \c bookmarks
617 void clearAllBondBookmarks() { d_bondBookmarks.clear(); }
618 //! queries whether or not any bonds are associated with a \c bookmark
619 bool hasBondBookmark(int mark) const { return d_bondBookmarks.count(mark); }
620 //! returns a pointer to all of our bond \c bookmarks
621 BOND_BOOKMARK_MAP *getBondBookmarks() { return &d_bondBookmarks; }
622
623 //! @}
624
625 //! \name Conformers
626 //! @{
627
628 //! return the conformer with a specified ID
629 //! if the ID is negative the first conformation will be returned
630 const Conformer &getConformer(int id = -1) const;
631
632 //! return the conformer with a specified ID
633 //! if the ID is negative the first conformation will be returned
634 Conformer &getConformer(int id = -1);
635
636 //! Delete the conformation with the specified ID
637 void removeConformer(unsigned int id);
638
639 //! Clear all the conformations on the molecule
640 void clearConformers() { d_confs.clear(); }
641
642 //! Add a new conformation to the molecule
643 /*!
644 \param conf - conformation to be added to the molecule, this molecule takes
645 ownership
646 of the conformer
647 \param assignId - a unique ID will be assigned to the conformation if
648 true
649 otherwise it is assumed that the conformation already has
650 an (unique) ID set
651 */
652 unsigned int addConformer(Conformer *conf, bool assignId = false);
653
654 inline unsigned int getNumConformers() const {
655 return rdcast<unsigned int>(d_confs.size());
656 }
657
658 //! \name Topology
659 //! @{
660
661 //! returns a pointer to our RingInfo structure
662 //! <b>Note:</b> the client should not delete this.
663 RingInfo *getRingInfo() const { return dp_ringInfo; }
664
665 //! provides access to all neighbors around an Atom
666 /*!
667 \param at the atom whose neighbors we are looking for
668
669 <b>Usage</b>
670 \code
671 ... mol is a const ROMol & ...
672 ... atomPtr is a const Atom * ...
673 ... requires #include <boost/range/iterator_range.hpp>
674 for (const auto &nbri :
675 boost::make_iterator_range(m.getAtomNeighbors(atomPtr))) {
676 const auto &nbr = (*m)[nbri];
677 // nbr is an atom pointer
678 }
679
680 \endcode
681
682 */
683 ADJ_ITER_PAIR getAtomNeighbors(Atom const *at) const;
684
685 //! provides access to all Bond objects connected to an Atom
686 /*!
687 \param at the atom whose neighbors we are looking for
688
689 <b>Usage</b>
690 \code
691 ... mol is a const ROMol & ...
692 ... atomPtr is a const Atom * ...
693 ... requires #include <boost/range/iterator_range.hpp>
694 for (const auto &nbri :
695 boost::make_iterator_range(m.getAtomBonds(atomPtr))) {
696 const auto &nbr = (*m)[nbri];
697 // nbr is a bond pointer
698 }
699 \endcode
700 or, if you need a non-const Bond *:
701 \code
702 ... mol is a const ROMol & ...
703 ... atomPtr is a const Atom * ...
704 ... requires #include <boost/range/iterator_range.hpp>
705 for (const auto &nbri :
706 boost::make_iterator_range(m.getAtomBonds(atomPtr))) {
707 auto nbr = (*m)[nbri];
708 // nbr is a bond pointer
709 }
710 \endcode
711
712
713 */
714 OBOND_ITER_PAIR getAtomBonds(Atom const *at) const;
715
716 //! returns an iterator pair for looping over all Atoms
717 /*!
718
719 <b>Usage</b>
720 \code
721
722 ROMol::VERTEX_ITER atBegin,atEnd;
723 boost::tie(atBegin,atEnd) = mol.getVertices();
724 while(atBegin!=atEnd){
725 ATOM_SPTR at2=mol[*atBegin];
726 ... do something with the Atom ...
727 ++atBegin;
728 }
729 \endcode
730 */
731 ATOM_ITER_PAIR getVertices();
732 //! returns an iterator pair for looping over all Bonds
733 /*!
734
735 <b>Usage</b>
736 \code
737
738 ROMol::EDGE_ITER firstB,lastB;
739 boost::tie(firstB,lastB) = mol.getEdges();
740 while(firstB!=lastB){
741 BOND_SPTR bond = mol[*firstB];
742 ... do something with the Bond ...
743 ++firstB;
744 }
745 \endcode
746 */
747 BOND_ITER_PAIR getEdges();
748 //! \overload
749 ATOM_ITER_PAIR getVertices() const;
750 //! \overload
751 BOND_ITER_PAIR getEdges() const;
752
753 //! brief returns a pointer to our underlying BGL object
754 /*!
755 This can be useful if you need to call other BGL algorithms:
756
757 Here's an example:
758 \code
759 ... mol is a const ROMol ...
760 ... mapping is an INT_VECT ...
761 mapping.resize(mol.getNumAtoms());
762 const MolGraph &G_p = mol.getTopology();
763 int res = boost::connected_components(G_p,&mapping[0]);
764 \endcode
765 */
766 MolGraph const &getTopology() const { return d_graph; }
767 //! @}
768
769 //! \name Iterators
770 //! @{
771
772 //! get an AtomIterator pointing at our first Atom
773 AtomIterator beginAtoms();
774 //! \overload
775 ConstAtomIterator beginAtoms() const;
776 //! get an AtomIterator pointing at the end of our Atoms
777 AtomIterator endAtoms();
778 //! \overload
779 ConstAtomIterator endAtoms() const;
780 //! get a BondIterator pointing at our first Bond
781 BondIterator beginBonds();
782 //! \overload
783 ConstBondIterator beginBonds() const;
784 //! get a BondIterator pointing at the end of our Bonds
785 BondIterator endBonds();
786 //! \overload
787 ConstBondIterator endBonds() const;
788
789 //! get an AtomIterator pointing at our first aromatic Atom
790 AromaticAtomIterator beginAromaticAtoms();
791 //! \overload
792 ConstAromaticAtomIterator beginAromaticAtoms() const;
793 //! get an AtomIterator pointing at the end of our Atoms
794 AromaticAtomIterator endAromaticAtoms();
795 //! \overload
796 ConstAromaticAtomIterator endAromaticAtoms() const;
797
798 //! get an AtomIterator pointing at our first hetero Atom
799 HeteroatomIterator beginHeteros();
800 //! \overload
801 ConstHeteroatomIterator beginHeteros() const;
802 //! get an AtomIterator pointing at the end of our Atoms
803 HeteroatomIterator endHeteros();
804 //! \overload
805 ConstHeteroatomIterator endHeteros() const;
806
807 //! if the Mol has any Query atoms or bonds
808 bool hasQuery() const;
809
810 //! get an AtomIterator pointing at our first Atom that matches \c query
811 QueryAtomIterator beginQueryAtoms(QueryAtom const *query);
812 //! \overload
813 ConstQueryAtomIterator beginQueryAtoms(QueryAtom const *) const;
814 //! get an AtomIterator pointing at the end of our Atoms
815 QueryAtomIterator endQueryAtoms();
816 //! \overload
817 ConstQueryAtomIterator endQueryAtoms() const;
818
819 //! get an AtomIterator pointing at our first Atom that matches \c query
820 MatchingAtomIterator beginMatchingAtoms(bool (*query)(Atom *));
821 //! \overload
822 ConstMatchingAtomIterator beginMatchingAtoms(
823 bool (*query)(const Atom *)) const;
824 //! get an AtomIterator pointing at the end of our Atoms
825 MatchingAtomIterator endMatchingAtoms();
826 //! \overload
827 ConstMatchingAtomIterator endMatchingAtoms() const;
828
829 inline ConformerIterator beginConformers() { return d_confs.begin(); }
830
831 inline ConformerIterator endConformers() { return d_confs.end(); }
832
833 inline ConstConformerIterator beginConformers() const {
834 return d_confs.begin();
835 }
836
837 inline ConstConformerIterator endConformers() const { return d_confs.end(); }
838
839 //! @}
840
841 //! \name Properties
842 //! @{
843
844 //! clears all of our \c computed \c properties
845 void clearComputedProps(bool includeRings = true) const;
846 //! calculates any of our lazy \c properties
847 /*!
848 <b>Notes:</b>
849 - this calls \c updatePropertyCache() on each of our Atoms and Bonds
850 */
851 void updatePropertyCache(bool strict = true);
852
855
856 //! @}
857
858 //! \name Misc
859 //! @{
860 //! sends some debugging info to a stream
861 void debugMol(std::ostream &str) const;
862 //! @}
863
864 Atom *operator[](const vertex_descriptor &v) { return d_graph[v]; }
865 const Atom *operator[](const vertex_descriptor &v) const {
866 return d_graph[v];
867 }
868
869 Bond *operator[](const edge_descriptor &e) { return d_graph[e]; }
870 const Bond *operator[](const edge_descriptor &e) const { return d_graph[e]; }
871
872 //! Gets a reference to the groups of atoms with relative stereochemistry
873 /*!
874 Stereo groups are also called enhanced stereochemistry in the SDF/Mol3000
875 file format.
876 */
877 const std::vector<StereoGroup> &getStereoGroups() const {
878 return d_stereo_groups;
879 }
880
881 //! Sets groups of atoms with relative stereochemistry
882 /*!
883 \param stereo_groups the new set of stereo groups. All will be replaced.
884
885 Stereo groups are also called enhanced stereochemistry in the SDF/Mol3000
886 file format. stereo_groups should be std::move()ed into this function.
887 */
888 void setStereoGroups(std::vector<StereoGroup> stereo_groups);
889
890#ifdef RDK_USE_BOOST_SERIALIZATION
891 //! \name boost::serialization support
892 //! @{
893 template <class Archive>
894 void save(Archive &ar, const unsigned int version) const;
895 template <class Archive>
896 void load(Archive &ar, const unsigned int version);
897 BOOST_SERIALIZATION_SPLIT_MEMBER()
898 //! @}
899#endif
900
901 private:
902 MolGraph d_graph;
903 ATOM_BOOKMARK_MAP d_atomBookmarks;
904 BOND_BOOKMARK_MAP d_bondBookmarks;
905 RingInfo *dp_ringInfo = nullptr;
906 CONF_SPTR_LIST d_confs;
907 std::vector<SubstanceGroup> d_sgroups;
908 std::vector<StereoGroup> d_stereo_groups;
909 std::unique_ptr<boost::dynamic_bitset<>> dp_delAtoms = nullptr;
910 std::unique_ptr<boost::dynamic_bitset<>> dp_delBonds = nullptr;
911
912 friend RDKIT_GRAPHMOL_EXPORT std::vector<SubstanceGroup> &getSubstanceGroups(
913 ROMol &);
914 friend RDKIT_GRAPHMOL_EXPORT const std::vector<SubstanceGroup> &
916 void clearSubstanceGroups() { d_sgroups.clear(); }
917
918 protected:
919 unsigned int numBonds{0};
920#ifndef WIN32
921
922 private:
923#endif
924 void initMol();
925 virtual void destroy();
926 //! adds an Atom to our collection
927 /*!
928 \param atom pointer to the Atom to add
929 \param updateLabel (optional) if this is true, the new Atom will be
930 our \c activeAtom
931 \param takeOwnership (optional) if this is true, we take ownership of \c
932 atom
933 instead of copying it.
934
935 \return the index of the new atom
936 */
937 unsigned int addAtom(Atom *atom, bool updateLabel = true,
938 bool takeOwnership = false);
939 //! adds a Bond to our collection
940 /*!
941 \param bond pointer to the Bond to add
942 \param takeOwnership (optional) if this is true, we take ownership of \c
943 bond
944 instead of copying it.
945
946 \return the new number of bonds
947 */
948 unsigned int addBond(Bond *bond, bool takeOwnership = false);
949
950 //! adds a Bond to our collection
951 /*!
952 \param bond pointer to the Bond to add
953
954 \return the new number of bonds
955
956 <b>Note:</b> since this is using a smart pointer, we don't need to worry
957 about
958 issues of ownership.
959 */
960 void initFromOther(const ROMol &other, bool quickCopy, int confId);
961};
962
963typedef std::vector<ROMol> MOL_VECT;
964typedef boost::shared_ptr<ROMol> ROMOL_SPTR;
965typedef std::vector<ROMol *> MOL_PTR_VECT;
966typedef std::vector<ROMOL_SPTR> MOL_SPTR_VECT;
967
968typedef MOL_PTR_VECT::const_iterator MOL_PTR_VECT_CI;
969typedef MOL_PTR_VECT::iterator MOL_PTR_VECT_I;
970
971}; // namespace RDKit
972#endif
Defines the Atom class and associated typedefs.
#define rdcast
Definition Invariant.h:191
Defines the class StereoGroup which stores relationships between the absolute configurations of atoms...
Defines the SubstanceGroup class.
Iterate over aromatic atoms, this is bidirectional.
A general random access iterator.
The class for representing atoms.
Definition Atom.h:74
iterator for a molecule's bonds, currently BiDirectional, but it theoretically ought to be RandomAcce...
class for representing a bond
Definition Bond.h:46
The class for representing 2D or 3D conformation of a molecule.
Definition Conformer.h:46
const iterator for a molecule's bonds, currently BiDirectional, but it theoretically ought to be Rand...
Iterate over heteroatoms, this is bidirectional.
Iterate over atoms matching a query function. This is bidirectional.
handles pickling (serializing) molecules
Definition MolPickler.h:67
Iterate over atoms matching a query. This is bidirectional.
Class for storing atomic queries.
Definition QueryAtom.h:28
Class for storing Bond queries.
Definition QueryBond.h:28
RDProps & operator=(const RDProps &rhs)
Definition RDProps.h:27
ConstAromaticAtomIterator endAromaticAtoms() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ADJ_ITER_PAIR getAtomNeighbors(Atom const *at) const
provides access to all neighbors around an Atom
ConstQueryAtomIterator endQueryAtoms() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool needsUpdatePropertyCache() const
OBOND_ITER_PAIR getAtomBonds(Atom const *at) const
provides access to all Bond objects connected to an Atom
unsigned int getNumBonds(bool onlyHeavy=1) const
returns our number of Bonds
CXXAtomIterator< const MolGraph, Atom *const > atoms() const
Definition ROMol.h:365
void clearAtomBookmark(int mark)
removes a bookmark from our collection
unsigned int getNumHeavyAtoms() const
returns our number of heavy atoms (atomic number > 1)
void clearAtomBookmark(int mark, const Atom *atom)
removes a particular Atom from the list associated with the bookmark
Atom * getAtomWithIdx(unsigned int idx)
returns a pointer to a particular Atom
unsigned int getNumConformers() const
Definition ROMol.h:654
AtomIterator endAtoms()
get an AtomIterator pointing at the end of our Atoms
BOND_PTR_LIST & getAllBondsWithBookmark(int mark)
returns all bonds associated with the bookmark provided
const std::vector< StereoGroup > & getStereoGroups() const
Gets a reference to the groups of atoms with relative stereochemistry.
Definition ROMol.h:877
ConstAtomIterator endAtoms() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
BondIterator beginBonds()
get a BondIterator pointing at our first Bond
bool hasAtomBookmark(int mark) const
queries whether or not any atoms are associated with a bookmark
Definition ROMol.h:596
unsigned int numBonds
Definition ROMol.h:919
Atom * getAtomWithIdx(const U idx)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition ROMol.h:519
ConstQueryAtomIterator beginQueryAtoms(QueryAtom const *) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend class RWMol
Definition ROMol.h:288
unsigned int getNumAtoms() const
returns our number of atoms
Definition ROMol.h:507
ConstConformerIterator endConformers() const
Definition ROMol.h:837
ConstMatchingAtomIterator beginMatchingAtoms(bool(*query)(const Atom *)) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ROMol & operator=(ROMol &&o) noexcept
Definition ROMol.h:459
ROMol(const ROMol &other, bool quickCopy=false, int confId=-1)
copy constructor with a twist
Definition ROMol.h:421
ROMol & operator=(const ROMol &)=delete
Bond * getUniqueBondWithBookmark(int mark)
ConstMatchingAtomIterator endMatchingAtoms() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
BOND_ITER_PAIR getEdges()
returns an iterator pair for looping over all Bonds
void clearConformers()
Clear all the conformations on the molecule.
Definition ROMol.h:640
void setBondBookmark(Bond *bond, int mark)
associates a Bond pointer with a bookmark
Definition ROMol.h:601
void updatePropertyCache(bool strict=true)
calculates any of our lazy properties
CXXAtomIterator< MolGraph, Atom * > atoms()
C++11 Range iterator.
Definition ROMol.h:363
Atom * getAtomWithBookmark(int mark)
returns the first Atom associated with the bookmark provided
CXXAtomIterator< const MolGraph, Atom *const, MolGraph::adjacency_iterator > atomNeighbors(Atom const *at) const
Definition ROMol.h:370
BOND_BOOKMARK_MAP * getBondBookmarks()
returns a pointer to all of our bond bookmarks
Definition ROMol.h:621
Conformer & getConformer(int id=-1)
QueryAtomIterator endQueryAtoms()
get an AtomIterator pointing at the end of our Atoms
const Conformer & getConformer(int id=-1) const
unsigned int addConformer(Conformer *conf, bool assignId=false)
Add a new conformation to the molecule.
const Atom * operator[](const vertex_descriptor &v) const
Definition ROMol.h:865
bool hasQuery() const
if the Mol has any Query atoms or bonds
void clearAllBondBookmarks()
blows out all bond bookmarks
Definition ROMol.h:617
const Atom * getAtomWithIdx(const U idx) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition ROMol.h:524
CXXBondIterator< MolGraph, Bond * > bonds()
Definition ROMol.h:402
ATOM_ITER_PAIR getVertices()
returns an iterator pair for looping over all Atoms
friend RDKIT_GRAPHMOL_EXPORT std::vector< SubstanceGroup > & getSubstanceGroups(ROMol &)
BOND_ITER_PAIR getEdges() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void clearComputedProps(bool includeRings=true) const
clears all of our computed properties
friend class MolPickler
Definition ROMol.h:287
const Bond * getBondWithIdx(const U idx) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition ROMol.h:547
ATOM_PTR_LIST & getAllAtomsWithBookmark(int mark)
returns all Atoms associated with the bookmark provided
const Bond * getBondBetweenAtoms(unsigned int idx1, unsigned int idx2) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ROMol(const std::string &binStr, unsigned int propertyFlags)
construct a molecule from a pickle string
Bond * getBondWithIdx(const U idx)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition ROMol.h:542
ATOM_BOOKMARK_MAP * getAtomBookmarks()
returns a pointer to all of our atom bookmarks
Definition ROMol.h:598
ConstAtomIterator beginAtoms() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ConstAromaticAtomIterator beginAromaticAtoms() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
CXXAtomIterator< MolGraph, Atom *, MolGraph::adjacency_iterator > atomNeighbors(Atom const *at)
Definition ROMol.h:375
void debugMol(std::ostream &str) const
const Bond * getBondBetweenAtoms(const U idx1, const V idx2) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition ROMol.h:562
void setAtomBookmark(Atom *at, int mark)
associates an Atom pointer with a bookmark
Definition ROMol.h:573
MatchingAtomIterator endMatchingAtoms()
get an AtomIterator pointing at the end of our Atoms
ConstBondIterator beginBonds() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
BondIterator endBonds()
get a BondIterator pointing at the end of our Bonds
ROMol(const std::string &binStr)
construct a molecule from a pickle string
Atom * getUniqueAtomWithBookmark(int mark)
QueryAtomIterator beginQueryAtoms(QueryAtom const *query)
get an AtomIterator pointing at our first Atom that matches query
ConstHeteroatomIterator endHeteros() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void clearPropertyCache()
void replaceAtomBookmark(Atom *at, int mark)
associates an Atom pointer with a bookmark
Definition ROMol.h:577
Bond * getBondWithBookmark(int mark)
returns the first Bond associated with the bookmark provided
CXXBondIterator< const MolGraph, Bond *const, MolGraph::out_edge_iterator > atomBonds(Atom const *at) const
Definition ROMol.h:382
unsigned int getAtomDegree(const Atom *at) const
returns the degree (number of neighbors) of an Atom in the graph
void setStereoGroups(std::vector< StereoGroup > stereo_groups)
Sets groups of atoms with relative stereochemistry.
CXXBondIterator< MolGraph, Bond *, MolGraph::out_edge_iterator > atomBonds(Atom const *at)
Definition ROMol.h:387
AromaticAtomIterator endAromaticAtoms()
get an AtomIterator pointing at the end of our Atoms
RingInfo * getRingInfo() const
Definition ROMol.h:663
void clearAllAtomBookmarks()
blows out all atomic bookmarks
Definition ROMol.h:594
const Bond * operator[](const edge_descriptor &e) const
Definition ROMol.h:870
Bond * operator[](const edge_descriptor &e)
Definition ROMol.h:869
Bond * getBondWithIdx(unsigned int idx)
returns a pointer to a particular Bond
virtual ~ROMol()
Definition ROMol.h:500
ConformerIterator beginConformers()
Definition ROMol.h:829
ConstBondIterator endBonds() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
unsigned int getNumAtoms(bool onlyExplicit) const
HeteroatomIterator endHeteros()
get an AtomIterator pointing at the end of our Atoms
ROMol(ROMol &&o) noexcept
Definition ROMol.h:432
Bond * getBondBetweenAtoms(const U idx1, const V idx2)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition ROMol.h:556
ConstConformerIterator beginConformers() const
Definition ROMol.h:833
void clearBondBookmark(int mark, const Bond *bond)
removes a particular Bond from the list associated with the bookmark
MatchingAtomIterator beginMatchingAtoms(bool(*query)(Atom *))
get an AtomIterator pointing at our first Atom that matches query
MolGraph const & getTopology() const
brief returns a pointer to our underlying BGL object
Definition ROMol.h:766
bool hasBondBookmark(int mark) const
queries whether or not any bonds are associated with a bookmark
Definition ROMol.h:619
const Bond * getBondWithIdx(unsigned int idx) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
AtomIterator beginAtoms()
get an AtomIterator pointing at our first Atom
const Atom * getAtomWithIdx(unsigned int idx) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void removeConformer(unsigned int id)
Delete the conformation with the specified ID.
AromaticAtomIterator beginAromaticAtoms()
get an AtomIterator pointing at our first aromatic Atom
Atom * operator[](const vertex_descriptor &v)
Definition ROMol.h:864
ConstHeteroatomIterator beginHeteros() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ConformerIterator endConformers()
Definition ROMol.h:831
ATOM_ITER_PAIR getVertices() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend RDKIT_GRAPHMOL_EXPORT const std::vector< SubstanceGroup > & getSubstanceGroups(const ROMol &)
void clearBondBookmark(int mark)
removes a bookmark from our collection
HeteroatomIterator beginHeteros()
get an AtomIterator pointing at our first hetero Atom
Bond * getBondBetweenAtoms(unsigned int idx1, unsigned int idx2)
returns a pointer to the bond between two atoms, Null on failure
CXXBondIterator< const MolGraph, Bond *const > bonds() const
Definition ROMol.h:404
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
The class for representing SubstanceGroups.
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:257
Std stuff.
std::vector< ROMol > MOL_VECT
Definition ROMol.h:963
MOL_PTR_VECT::const_iterator MOL_PTR_VECT_CI
Definition ROMol.h:968
RDKIT_GRAPHMOL_EXPORT const int ci_RIGHTMOST_ATOM
RDKIT_GRAPHMOL_EXPORT const int ci_ATOM_HOLDER
std::vector< ROMol * > MOL_PTR_VECT
Definition ROMol.h:965
boost::shared_ptr< ROMol > ROMOL_SPTR
MOL_PTR_VECT::iterator MOL_PTR_VECT_I
Definition ROMol.h:969
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, Atom *, Bond * > MolGraph
This is the BGL type used to store the topology:
Definition ROMol.h:57
std::vector< boost::shared_ptr< ROMol > > MOL_SPTR_VECT
RDKIT_GRAPHMOL_EXPORT const int ci_LEADING_BOND
bool operator!=(const CXXAtomIter &other) const
Definition ROMol.h:188
bool operator<(const CXXAtomIter &other) const
Definition ROMol.h:191
CXXAtomIter & operator+=(difference_type n)
Definition ROMol.h:170
CXXAtomIter operator-(difference_type n) const
Definition ROMol.h:161
CXXAtomIter operator+(difference_type n) const
Definition ROMol.h:158
std::random_access_iterator_tag iterator_category
Definition ROMol.h:122
bool operator>=(const CXXAtomIter &other) const
Definition ROMol.h:194
friend CXXAtomIter operator+(difference_type n, const CXXAtomIter &it)
Definition ROMol.h:181
CXXAtomIter & operator-=(difference_type n)
Definition ROMol.h:174
const_reference operator*() const
Definition ROMol.h:138
difference_type operator-(const CXXAtomIter &other) const
Definition ROMol.h:178
bool operator==(const CXXAtomIter &other) const
Definition ROMol.h:185
CXXAtomIter(Graph *graph, Iterator pos)
Definition ROMol.h:134
bool operator<=(const CXXAtomIter &other) const
Definition ROMol.h:192
bool operator>(const CXXAtomIter &other) const
Definition ROMol.h:193
const_reference operator[](difference_type n) const
Definition ROMol.h:141
CXXAtomIter end()
Definition ROMol.h:203
CXXAtomIterator(Graph *graph, Iterator start, Iterator end)
Definition ROMol.h:200
CXXAtomIterator(Graph *graph)
Definition ROMol.h:197
size_t size() const
Definition ROMol.h:204
CXXAtomIter begin()
Definition ROMol.h:202
const_reference operator*() const
Definition ROMol.h:235
std::bidirectional_iterator_tag iterator_category
Definition ROMol.h:220
CXXBondIter(Graph *graph, Iterator pos)
Definition ROMol.h:232
bool operator==(const CXXBondIter &other) const
Definition ROMol.h:254
bool operator!=(const CXXBondIter &other) const
Definition ROMol.h:257
CXXBondIter begin()
Definition ROMol.h:269
size_t size() const
Definition ROMol.h:271
CXXBondIterator(Graph *graph)
Definition ROMol.h:262
CXXBondIterator(Graph *graph, Iterator start, Iterator end)
Definition ROMol.h:267
CXXBondIter end()
Definition ROMol.h:270