RDKit: MolBundle.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
MolBundle.h
Go to the documentation of this file.
1//
2// Copyright (C) 2017-2023 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 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
24#include <RDGeneral/BoostStartInclude.h>
25#include <boost/smart_ptr.hpp>
26#include <RDGeneral/BoostEndInclude.h>
27
28#ifdef RDK_USE_BOOST_SERIALIZATION
29#include <RDGeneral/BoostStartInclude.h>
30#include <boost/serialization/vector.hpp>
31#include <boost/serialization/shared_ptr.hpp>
32#include <boost/serialization/split_member.hpp>
33#include <boost/archive/text_oarchive.hpp>
34#include <boost/archive/text_iarchive.hpp>
35#include <boost/archive/archive_exception.hpp>
36#include <RDGeneral/BoostEndInclude.h>
37#endif
38
39// our stuff
40#include <RDGeneral/Exceptions.h>
41#include <GraphMol/MolPickler.h>
42
43namespace RDKit {
44class ROMol;
45
46 inline bool MolBundleCanSerialize() {
47#ifdef RDK_USE_BOOST_SERIALIZATION
48 return true;
49#else
50 return false;
51#endif
52};
53
54//! MolBundle contains a collection of related ROMols
55/*!
56 This is designed to allow handling things like enumerating link nodes,
57 polymers, etc.
58*/
59 class MolBundle : public RDProps {
60 public:
61 MolBundle() : RDProps() {}
62
63 //! copy constructor
64 MolBundle(const MolBundle &other) : RDProps(other) { d_mols = other.d_mols; }
65 MolBundle(const std::string &pkl) { initFromString(pkl); }
66 virtual ~MolBundle() {}
67
68 MolBundle &operator=(const MolBundle &other) = default;
69
70 //! returns our molecules
71 virtual const std::vector<boost::shared_ptr<ROMol>> &getMols() const {
72 return d_mols;
73 }
74
75 //! adds a new molecule and returns the total number of molecules
76 virtual size_t addMol(boost::shared_ptr<ROMol> nmol) {
77 PRECONDITION(nmol.get(), "bad mol pointer");
78 d_mols.push_back(nmol);
79 return (d_mols.size());
80 }
81 //! returns the number of molecules from the bundle
82 virtual size_t size() const { return d_mols.size(); }
83 //! returns whether or not the bundle is empty
84 virtual bool empty() const { return d_mols.empty(); }
85
86 //! returns a particular molecule in the bundle
87 virtual const boost::shared_ptr<ROMol> getMol(size_t idx) const {
88 if (idx >= d_mols.size()) {
89 throw IndexErrorException(static_cast<int>(idx));
90 }
91 return d_mols[idx];
92 }
93 //! returns a particular molecule from the bundle
94 virtual const boost::shared_ptr<ROMol> operator[](size_t idx) const {
95 return getMol(idx);
96 }
97
98 //! serializes (pickles) to a stream
99 void toStream(std::ostream &ss) const {
100#ifndef RDK_USE_BOOST_SERIALIZATION
101 RDUNUSED_PARAM(ss);
102 PRECONDITION(0, "Boost SERIALIZATION is not enabled")
103#else
104 boost::archive::text_oarchive ar(ss);
105 ar << *this;
106#endif
107 };
108 //! returns a string with a serialized (pickled) representation
109 std::string serialize() const {
110 std::stringstream ss;
111 toStream(ss);
112 return ss.str();
113 };
114 //! initializes from a stream pickle
115 void initFromStream(std::istream &ss) {
116#ifndef RDK_USE_BOOST_SERIALIZATION
117 RDUNUSED_PARAM(ss);
118 PRECONDITION(0, "Boost SERIALIZATION is not enabled")
119#else
120 boost::archive::text_iarchive ar(ss);
121 ar >> *this;
122#endif
123 };
124 //! initializes from a string pickle
125 void initFromString(const std::string &text) {
126 std::stringstream ss(text);
127 initFromStream(ss);
128 };
129
130#ifdef RDK_USE_BOOST_SERIALIZATION
131 // FIX: we don't currently serialize properties
132 template <class Archive>
133 void save(Archive &ar, const unsigned int version) const {
134 RDUNUSED_PARAM(version);
135 std::vector<std::string> pkls;
136 for (const auto &mol : d_mols) {
137 std::string pkl;
138 MolPickler::pickleMol(*mol, pkl);
139 pkls.push_back(pkl);
140 }
141 ar << pkls;
142 }
143
144 template <class Archive>
145 void load(Archive &ar, const unsigned int version) {
146 RDUNUSED_PARAM(version);
147
148 std::vector<std::string> pkls;
149 ar >> pkls;
150 d_mols.clear();
151 for (const auto &pkl : pkls) {
152 d_mols.push_back(ROMOL_SPTR(new ROMol(pkl)));
153 }
154 }
155 BOOST_SERIALIZATION_SPLIT_MEMBER()
156#endif
157
158 protected:
159 std::vector<boost::shared_ptr<ROMol>> d_mols;
160};
161
162//! FixedMolSizeMolBundle contains a collection of ROMols with the same
163//! number of atoms and bonds.
164/*!
165 This is designed to allow handling things like enhanced stereochemistry,
166 but can no doubt be (ab)used in other ways.
167
168 Implementation note: at the moment this isn't taking advantage of the fact
169 that the number of atoms and bonds remains constant. This may be used in the
170 future to allow this to be more efficient.
171
172*/
173 class FixedMolSizeMolBundle : public MolBundle {
174 public:
175 FixedMolSizeMolBundle() : MolBundle() {}
176
177 //! copy constructor
178 FixedMolSizeMolBundle(const FixedMolSizeMolBundle &other)
179 : MolBundle(other) {}
180
181 ~FixedMolSizeMolBundle() override = default;
182
183 //! adds a new molecule and returns the total number of molecules
184 //! enforces that the new molecule has the same number of atoms and bonds
185 //! as the molecules that are already there.
186 size_t addMol(boost::shared_ptr<ROMol> nmol) override {
187 PRECONDITION(nmol.get(), "bad mol pointer");
188 if (d_mols.size()) {
189 if (nmol->getNumAtoms() != d_mols[0]->getNumAtoms()) {
190 throw ValueErrorException(
191 "all molecules in a bundle must have the same number of atoms");
192 }
193 // REVIEW: should we allow different numbers of bonds?
194 if (nmol->getNumBonds() != d_mols[0]->getNumBonds()) {
195 throw ValueErrorException(
196 "all molecules in a bundle must have the same number of bonds");
197 }
198 }
199 d_mols.push_back(nmol);
200 return (d_mols.size());
201 }
202};
203
204}; // namespace RDKit
205#endif
BoostEndInclude.h
BoostStartInclude.h
Exceptions.h
RDUNUSED_PARAM
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
MolPickler.h
IndexErrorException
Class to allow us to throw an IndexError from C++ and have it make it back to Python.
Definition Exceptions.h:20
RDKit::FixedMolSizeMolBundle::FixedMolSizeMolBundle
FixedMolSizeMolBundle(const FixedMolSizeMolBundle &other)
copy constructor
Definition MolBundle.h:178
RDKit::FixedMolSizeMolBundle::~FixedMolSizeMolBundle
~FixedMolSizeMolBundle() override=default
RDKit::FixedMolSizeMolBundle::addMol
size_t addMol(boost::shared_ptr< ROMol > nmol) override
Definition MolBundle.h:186
RDKit::FixedMolSizeMolBundle::FixedMolSizeMolBundle
FixedMolSizeMolBundle()
Definition MolBundle.h:175
RDKit::MolBundle::d_mols
std::vector< boost::shared_ptr< ROMol > > d_mols
Definition MolBundle.h:159
RDKit::MolBundle::operator[]
virtual const boost::shared_ptr< ROMol > operator[](size_t idx) const
returns a particular molecule from the bundle
Definition MolBundle.h:94
RDKit::MolBundle::size
virtual size_t size() const
returns the number of molecules from the bundle
Definition MolBundle.h:82
RDKit::MolBundle::empty
virtual bool empty() const
returns whether or not the bundle is empty
Definition MolBundle.h:84
RDKit::MolBundle::MolBundle
MolBundle()
Definition MolBundle.h:61
RDKit::MolBundle::MolBundle
MolBundle(const MolBundle &other)
copy constructor
Definition MolBundle.h:64
RDKit::MolBundle::MolBundle
MolBundle(const std::string &pkl)
Definition MolBundle.h:65
RDKit::MolBundle::toStream
void toStream(std::ostream &ss) const
serializes (pickles) to a stream
Definition MolBundle.h:99
RDKit::MolBundle::initFromString
void initFromString(const std::string &text)
initializes from a string pickle
Definition MolBundle.h:125
RDKit::MolBundle::getMols
virtual const std::vector< boost::shared_ptr< ROMol > > & getMols() const
returns our molecules
Definition MolBundle.h:71
RDKit::MolBundle::getMol
virtual const boost::shared_ptr< ROMol > getMol(size_t idx) const
returns a particular molecule in the bundle
Definition MolBundle.h:87
RDKit::MolBundle::initFromStream
void initFromStream(std::istream &ss)
initializes from a stream pickle
Definition MolBundle.h:115
RDKit::MolBundle::operator=
MolBundle & operator=(const MolBundle &other)=default
RDKit::MolBundle::serialize
std::string serialize() const
returns a string with a serialized (pickled) representation
Definition MolBundle.h:109
RDKit::MolBundle::~MolBundle
virtual ~MolBundle()
Definition MolBundle.h:66
RDKit::MolBundle::addMol
virtual size_t addMol(boost::shared_ptr< ROMol > nmol)
adds a new molecule and returns the total number of molecules
Definition MolBundle.h:76
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RDKit::RDProps::RDProps
RDProps()
Definition RDProps.h:22
RDKit::ROMol
Definition ROMol.h:199
ValueErrorException
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition Exceptions.h:40
export.h
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::MolBundleCanSerialize
bool MolBundleCanSerialize()
Definition MolBundle.h:46
RDKit::ROMOL_SPTR
boost::shared_ptr< ROMol > ROMOL_SPTR
Definition ChemTransforms.h:22