RDKit
Open-source cheminformatics and machine learning.
MolBundle.h
Go to the documentation of this file.
1//
2// Copyright (C) 2017-2020 Greg Landrum
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 MolBundle.h
11
12 \brief Defines a class for managing bundles of molecules
13
14*/
15
16#include <RDGeneral/export.h>
17#ifndef RD_MOLBUNDLE_AUG2017
18#define RD_MOLBUNDLE_AUG2017
19
20/// Std stuff
21#include <vector>
22
23// boost stuff
25#include <boost/smart_ptr.hpp>
27
28// our stuff
30
31namespace RDKit {
32class ROMol;
33
34//! MolBundle contains a collection of related ROMols
35/*!
36 This is designed to allow handling things like enumerating link nodes,
37 polymers, etc.
38*/
39class MolBundle : public RDProps {
40 public:
42
43 //! copy constructor
44 MolBundle(const MolBundle &other) : RDProps(other) { d_mols = other.d_mols; }
45 // FIX: need serialization/deserialization
46
47 virtual ~MolBundle() {}
48
49 //! returns our molecules
50 virtual const std::vector<boost::shared_ptr<ROMol>> &getMols() const {
51 return d_mols;
52 }
53
54 //! adds a new molecule and returns the total number of molecules
55 virtual size_t addMol(boost::shared_ptr<ROMol> nmol) {
56 PRECONDITION(nmol.get(), "bad mol pointer");
57 d_mols.push_back(nmol);
58 return (d_mols.size());
59 }
60 //! returns the number of molecules from the bundle
61 virtual size_t size() const { return d_mols.size(); }
62 //! returns a particular molecule in the bundle
63 virtual const boost::shared_ptr<ROMol> getMol(size_t idx) const {
64 if (idx >= d_mols.size()) {
65 throw IndexErrorException(static_cast<int>(idx));
66 }
67 return d_mols[idx];
68 }
69 //! returns a particular molecule from the bundle
70 virtual const boost::shared_ptr<ROMol> operator[](size_t idx) const {
71 return getMol(idx);
72 }
73
74 protected:
75 std::vector<boost::shared_ptr<ROMol>> d_mols;
76};
77
78//! FixedMolSizeMolBundle contains a collection of ROMols with the same
79//! number of atoms and bonds.
80/*!
81 This is designed to allow handling things like enhanced stereochemistry,
82 but can no doubt be (ab)used in other ways.
83
84 Implementation note: at the moment this isn't taking advantage of the fact
85 that the number of atoms and bonds remains constant. This may be used in the
86 future to allow this to be more efficient.
87
88*/
90 public:
92
93 //! copy constructor
95 : MolBundle(other) {}
96
97 ~FixedMolSizeMolBundle() override = default;
98
99 //! adds a new molecule and returns the total number of molecules
100 //! enforces that the new molecule has the same number of atoms and bonds
101 //! as the molecules that are already there.
102 size_t addMol(boost::shared_ptr<ROMol> nmol) override {
103 PRECONDITION(nmol.get(), "bad mol pointer");
104 if (d_mols.size()) {
105 if (nmol->getNumAtoms() != d_mols[0]->getNumAtoms()) {
107 "all molecules in a bundle must have the same number of atoms");
108 }
109 // REVIEW: should we allow different numbers of bonds?
110 if (nmol->getNumBonds() != d_mols[0]->getNumBonds()) {
112 "all molecules in a bundle must have the same number of bonds");
113 }
114 }
115 d_mols.push_back(nmol);
116 return (d_mols.size());
117 }
118};
119
120}; // namespace RDKit
121#endif
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
Class to allow us to throw an IndexError from C++ and have it make it back to Python.
Definition: Exceptions.h:20
FixedMolSizeMolBundle(const FixedMolSizeMolBundle &other)
copy constructor
Definition: MolBundle.h:94
~FixedMolSizeMolBundle() override=default
size_t addMol(boost::shared_ptr< ROMol > nmol) override
Definition: MolBundle.h:102
MolBundle contains a collection of related ROMols.
Definition: MolBundle.h:39
std::vector< boost::shared_ptr< ROMol > > d_mols
Definition: MolBundle.h:75
virtual const boost::shared_ptr< ROMol > operator[](size_t idx) const
returns a particular molecule from the bundle
Definition: MolBundle.h:70
virtual size_t size() const
returns the number of molecules from the bundle
Definition: MolBundle.h:61
MolBundle(const MolBundle &other)
copy constructor
Definition: MolBundle.h:44
virtual const std::vector< boost::shared_ptr< ROMol > > & getMols() const
returns our molecules
Definition: MolBundle.h:50
virtual const boost::shared_ptr< ROMol > getMol(size_t idx) const
returns a particular molecule in the bundle
Definition: MolBundle.h:63
virtual ~MolBundle()
Definition: MolBundle.h:47
virtual size_t addMol(boost::shared_ptr< ROMol > nmol)
adds a new molecule and returns the total number of molecules
Definition: MolBundle.h:55
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition: Exceptions.h:40
Std stuff.
Definition: Abbreviations.h:18