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
25#include <boost/smart_ptr.hpp>
27
28#ifdef RDK_USE_BOOST_SERIALIZATION
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>
37#endif
38
39// our stuff
41#include <GraphMol/MolPickler.h>
42
43namespace RDKit {
44class ROMol;
45
46inline 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*/
59class MolBundle : public RDProps {
60 public:
62
63 //! copy constructor
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 PRECONDITION(0, "Boost SERIALIZATION is not enabled")
102#else
103 boost::archive::text_oarchive ar(ss);
104 ar << *this;
105#endif
106 };
107 //! returns a string with a serialized (pickled) representation
108 std::string serialize() const {
109 std::stringstream ss;
110 toStream(ss);
111 return ss.str();
112 };
113 //! initializes from a stream pickle
114 void initFromStream(std::istream &ss) {
115#ifndef RDK_USE_BOOST_SERIALIZATION
116 PRECONDITION(0, "Boost SERIALIZATION is not enabled")
117#else
118 boost::archive::text_iarchive ar(ss);
119 ar >> *this;
120#endif
121 };
122 //! initializes from a string pickle
123 void initFromString(const std::string &text) {
124 std::stringstream ss(text);
126 };
127
128#ifdef RDK_USE_BOOST_SERIALIZATION
129 // FIX: we don't currently serialize properties
130 template <class Archive>
131 void save(Archive &ar, const unsigned int version) const {
133 std::vector<std::string> pkls;
134 for (const auto &mol : d_mols) {
135 std::string pkl;
137 pkls.push_back(pkl);
138 }
139 ar << pkls;
140 }
141
142 template <class Archive>
143 void load(Archive &ar, const unsigned int version) {
145
146 std::vector<std::string> pkls;
147 ar >> pkls;
148 d_mols.clear();
149 for (const auto &pkl : pkls) {
150 d_mols.push_back(ROMOL_SPTR(new ROMol(pkl)));
151 }
152 }
154#endif
155
156 protected:
157 std::vector<boost::shared_ptr<ROMol>> d_mols;
158};
159
160//! FixedMolSizeMolBundle contains a collection of ROMols with the same
161//! number of atoms and bonds.
162/*!
163 This is designed to allow handling things like enhanced stereochemistry,
164 but can no doubt be (ab)used in other ways.
165
166 Implementation note: at the moment this isn't taking advantage of the fact
167 that the number of atoms and bonds remains constant. This may be used in the
168 future to allow this to be more efficient.
169
170*/
172 public:
174
175 //! copy constructor
178
179 ~FixedMolSizeMolBundle() override = default;
180
181 //! adds a new molecule and returns the total number of molecules
182 //! enforces that the new molecule has the same number of atoms and bonds
183 //! as the molecules that are already there.
184 size_t addMol(boost::shared_ptr<ROMol> nmol) override {
185 PRECONDITION(nmol.get(), "bad mol pointer");
186 if (d_mols.size()) {
187 if (nmol->getNumAtoms() != d_mols[0]->getNumAtoms()) {
189 "all molecules in a bundle must have the same number of atoms");
190 }
191 // REVIEW: should we allow different numbers of bonds?
192 if (nmol->getNumBonds() != d_mols[0]->getNumBonds()) {
194 "all molecules in a bundle must have the same number of bonds");
195 }
196 }
197 d_mols.push_back(nmol);
198 return (d_mols.size());
199 }
200};
201
202}; // namespace RDKit
203#endif
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
#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:176
~FixedMolSizeMolBundle() override=default
size_t addMol(boost::shared_ptr< ROMol > nmol) override
Definition MolBundle.h:184
MolBundle contains a collection of related ROMols.
Definition MolBundle.h:59
std::vector< boost::shared_ptr< ROMol > > d_mols
Definition MolBundle.h:157
virtual const boost::shared_ptr< ROMol > operator[](size_t idx) const
returns a particular molecule from the bundle
Definition MolBundle.h:94
virtual size_t size() const
returns the number of molecules from the bundle
Definition MolBundle.h:82
virtual bool empty() const
returns whether or not the bundle is empty
Definition MolBundle.h:84
MolBundle(const MolBundle &other)
copy constructor
Definition MolBundle.h:64
MolBundle(const std::string &pkl)
Definition MolBundle.h:65
void toStream(std::ostream &ss) const
serializes (pickles) to a stream
Definition MolBundle.h:99
void initFromString(const std::string &text)
initializes from a string pickle
Definition MolBundle.h:123
virtual const std::vector< boost::shared_ptr< ROMol > > & getMols() const
returns our molecules
Definition MolBundle.h:71
virtual const boost::shared_ptr< ROMol > getMol(size_t idx) const
returns a particular molecule in the bundle
Definition MolBundle.h:87
void initFromStream(std::istream &ss)
initializes from a stream pickle
Definition MolBundle.h:114
MolBundle & operator=(const MolBundle &other)=default
std::string serialize() const
returns a string with a serialized (pickled) representation
Definition MolBundle.h:108
virtual ~MolBundle()
Definition MolBundle.h:66
virtual size_t addMol(boost::shared_ptr< ROMol > nmol)
adds a new molecule and returns the total number of molecules
Definition MolBundle.h:76
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition Exceptions.h:40
Std stuff.
bool MolBundleCanSerialize()
Definition MolBundle.h:46
bool rdvalue_is(const RDValue_cast_t)
boost::shared_ptr< ROMol > ROMOL_SPTR