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
29 #include <RDGeneral/Exceptions.h>
30 
31 namespace RDKit {
32 class 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 */
39 class MolBundle : public RDProps {
40  public:
41  MolBundle() : RDProps(){};
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()) throw IndexErrorException(static_cast<int>(idx));
65  return d_mols[idx];
66  };
67  //! returns a particular molecule from the bundle
68  virtual const boost::shared_ptr<ROMol> operator[](size_t idx) const {
69  return getMol(idx);
70  };
71 
72  protected:
73  std::vector<boost::shared_ptr<ROMol>> d_mols;
74 };
75 
76 //! FixedMolSizeMolBundle contains a collection of ROMols with the same
77 //! number of atoms and bonds.
78 /*!
79  This is designed to allow handling things like enhanced stereochemistry,
80  but can no doubt be (ab)used in other ways.
81 
82  Implementation note: at the moment this isn't taking advantage of the fact
83  that the number of atoms and bonds remains constant. This may be used in the
84  future to allow this to be more efficient.
85 
86 */
88  public:
90 
91  //! copy constructor
93  : MolBundle(other){};
94 
95  ~FixedMolSizeMolBundle() override{};
96 
97  //! adds a new molecule and returns the total number of molecules
98  //! enforces that the new molecule has the same number of atoms and bonds
99  //! as the molecules that are already there.
100  size_t addMol(boost::shared_ptr<ROMol> nmol) override {
101  PRECONDITION(nmol.get(), "bad mol pointer");
102  if (d_mols.size()) {
103  if (nmol->getNumAtoms() != d_mols[0]->getNumAtoms())
104  throw ValueErrorException(
105  "all molecules in a bundle must have the same number of atoms");
106  // REVIEW: should we allow different numbers of bonds?
107  if (nmol->getNumBonds() != d_mols[0]->getNumBonds())
108  throw ValueErrorException(
109  "all molecules in a bundle must have the same number of bonds");
110  }
111  d_mols.push_back(nmol);
112  return (d_mols.size());
113  }
114 };
115 
116 }; // namespace RDKit
117 #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:19
FixedMolSizeMolBundle(const FixedMolSizeMolBundle &other)
copy constructor
Definition: MolBundle.h:92
~FixedMolSizeMolBundle() override
Definition: MolBundle.h:95
size_t addMol(boost::shared_ptr< ROMol > nmol) override
Definition: MolBundle.h:100
MolBundle contains a collection of related ROMols.
Definition: MolBundle.h:39
std::vector< boost::shared_ptr< ROMol > > d_mols
Definition: MolBundle.h:70
virtual size_t size() const
returns the number of molecules from the bundle
Definition: MolBundle.h:61
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
MolBundle(const MolBundle &other)
copy constructor
Definition: MolBundle.h:44
virtual const boost::shared_ptr< ROMol > operator[](size_t idx) const
returns a particular molecule from the bundle
Definition: MolBundle.h:68
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:39
Std stuff.
Definition: Abbreviations.h:17