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
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
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);
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 {
135 std::vector<std::string> pkls;
136 for (const auto &mol : d_mols) {
137 std::string 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) {
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 }
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*/
174 public:
176
177 //! copy constructor
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()) {
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()) {
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
#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:178
~FixedMolSizeMolBundle() override=default
size_t addMol(boost::shared_ptr< ROMol > nmol) override
Definition MolBundle.h:186
MolBundle contains a collection of related ROMols.
Definition MolBundle.h:59
std::vector< boost::shared_ptr< ROMol > > d_mols
Definition MolBundle.h:159
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:125
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:115
MolBundle & operator=(const MolBundle &other)=default
std::string serialize() const
returns a string with a serialized (pickled) representation
Definition MolBundle.h:109
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