RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
EnumerateBase.h
Go to the documentation of this file.
1//
2// Copyright (c) 2015, Novartis Institutes for BioMedical Research Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following
13// disclaimer in the documentation and/or other materials provided
14// with the distribution.
15// * Neither the name of Novartis Institutes for BioMedical Research Inc.
16// nor the names of its contributors may be used to endorse or promote
17// products derived from this software without specific prior written
18// permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//
32#include <RDGeneral/export.h>
33#ifndef RDKIT_ENUMERATEBASE_H
34#define RDKIT_ENUMERATEBASE_H
35
36#include <vector>
37#include "EnumerateTypes.h"
38#include "../Reaction.h"
39#include "EnumerationPickler.h"
40
42#include "CartesianProduct.h"
43#include "../ReactionPickler.h"
44#include <GraphMol/MolPickler.h>
45
46namespace RDKit {
47//! Base class for enumerating chemical reactions from collections of
48/// building blocks and reagents.
49/*!
50 basic usage:
51
52 \verbatim
53 EnumerateLibraryBase &enumerator;
54 while (enumerator) {
55 MOL_SPTR_VECT res = enumerator.next();
56 // do something with enumeration products here
57 }
58 \endverbatim
59
60 See Reaction.h for more details on how ChemicalReactions are
61 used.
62*/
64 protected:
66 boost::shared_ptr<EnumerationStrategyBase> m_enumerator;
67 boost::shared_ptr<EnumerationStrategyBase> m_initialEnumerator;
68
69 public:
70 //! default constructor
72
73 //! construct with a chemical reaction and an enumeration strategy
75 EnumerationStrategyBase *enumerator = nullptr)
76 : m_rxn(rxn),
77 m_enumerator(enumerator ? enumerator : new CartesianProductStrategy),
79 m_rxn.initReactantMatchers();
80 }
81
82 //! Copy constructor
84 : m_rxn(rhs.m_rxn),
85 m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : nullptr),
87
89
90 //! Are there any enumerations left?
91 virtual operator bool() const {
92 PRECONDITION(m_enumerator.get(), "Null enumeration strategy");
93 return static_cast<bool>(*m_enumerator);
94 }
95
96 //! reset the enumeration to the beginning.
97 void reset() {
98 if (m_initialEnumerator.get()) {
99 m_enumerator.reset(m_initialEnumerator->copy());
100 }
101 }
102
103 //! returns the underlying chemical reaction
104 const ChemicalReaction &getReaction() const { return m_rxn; }
105
106 //! return the current enumeration strategy
108 PRECONDITION(m_enumerator.get(), "Null Enumerator");
109 return *m_enumerator;
110 }
111
112 //! get the next set of products (See run_Reactants) for details
113 /// This returns a vector of a vector of molecules.
114 /// Each result vector corresponds for a product template.
115 /// i.e.
116 /// res = library.next();
117 /// res[0] are the results for library.getReaction().getProdcts()[0]
118 virtual std::vector<MOL_SPTR_VECT> next() = 0;
119
120 //! get the next set of products as smiles
121 /// This returns a vector of a vector strings.
122 /// Each result vector corresponds for a product template.
123 virtual std::vector<std::vector<std::string>> nextSmiles();
124
125 //! Get the current position into the reagent vectors as returned by
126 /// getReagents. It is not necessarily the index into the input reagents
127 /// as it is only for the reagents compatible with the reaction.
128 /// Use getState/setState to save/restart the enumeration
129 /// from this position.
131
132 //! Get the current state of the enumerator
133 /// This is the position of the enumerator and the enumerators
134 /// state that can be used to restart enumerating
135 /// from this position.
136 std::string getState() const;
137
138 //! Set the current state of the enumerator
139 /// Restart the enumerator from this position.
140 void setState(const std::string &);
141
142 //! Reset the enumerator to the beginning
144
145 //! serializes (pickles) to a stream
146 virtual void toStream(std::ostream &ss) const = 0;
147
148 //! returns a string with a serialized (pickled) representation
149 virtual std::string Serialize() const {
150 std::stringstream ss;
151 toStream(ss);
152 return ss.str();
153 }
154
155 //! initializes from a stream pickle
156 virtual void initFromStream(std::istream &ss) = 0;
157
158 //! initializes from a string pickle
159 virtual void initFromString(const std::string &text) {
160 std::stringstream ss(text);
161 initFromStream(ss);
162 }
163
164 private:
165#ifdef RDK_USE_BOOST_SERIALIZATION
166 friend class boost::serialization::access;
167 template <class Archive>
168 void save(Archive &ar, const unsigned int) const {
169 std::string pickle;
170 ReactionPickler::pickleReaction(m_rxn, pickle);
171 ar & pickle;
172 ar & m_enumerator;
173 // we handle the m_initialEnumerator from a string
174 // for backwards compatibility with a unreleased
175 // version
176 EnumerationStrategyPickler::pickle(m_initialEnumerator, pickle);
177 ar & pickle;
178 }
179 template <class Archive>
180 void load(Archive &ar, const unsigned int /*version*/) {
181 // this should only be called on non-initialized reactions
182 if (m_rxn.getNumReactantTemplates() || m_rxn.getNumProductTemplates() ||
183 m_rxn.getNumAgentTemplates()) {
184 throw ValueErrorException("EnumerateBase already created from archive.");
185 }
186 std::string pickle;
187 ar & pickle;
188 ReactionPickler::reactionFromPickle(pickle, m_rxn);
189 ar & m_enumerator;
190 ar & pickle;
191 m_initialEnumerator = EnumerationStrategyPickler::fromPickle(pickle);
192 }
193
194 BOOST_SERIALIZATION_SPLIT_MEMBER();
195#endif
196};
197
198#ifdef RDK_USE_BOOST_SERIALIZATION
199BOOST_SERIALIZATION_ASSUME_ABSTRACT(EnumerateLibraryBase)
200#endif
201} // namespace RDKit
202#endif
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
This is a class for storing and applying general chemical reactions.
Definition Reaction.h:121
unsigned int getNumAgentTemplates() const
Definition Reaction.h:309
unsigned int getNumReactantTemplates() const
Definition Reaction.h:303
unsigned int getNumProductTemplates() const
Definition Reaction.h:306
std::string getState() const
const EnumerationStrategyBase & getEnumerator()
return the current enumeration strategy
virtual void initFromStream(std::istream &ss)=0
initializes from a stream pickle
virtual std::string Serialize() const
returns a string with a serialized (pickled) representation
boost::shared_ptr< EnumerationStrategyBase > m_enumerator
virtual std::vector< MOL_SPTR_VECT > next()=0
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
Copy constructor.
EnumerateLibraryBase()
default constructor
const EnumerationTypes::RGROUPS & getPosition() const
virtual std::vector< std::vector< std::string > > nextSmiles()
virtual void initFromString(const std::string &text)
initializes from a string pickle
virtual void toStream(std::ostream &ss) const =0
serializes (pickles) to a stream
void reset()
reset the enumeration to the beginning.
EnumerateLibraryBase(const ChemicalReaction &rxn, EnumerationStrategyBase *enumerator=nullptr)
construct with a chemical reaction and an enumeration strategy
void setState(const std::string &)
void resetState()
Reset the enumerator to the beginning.
boost::shared_ptr< EnumerationStrategyBase > m_initialEnumerator
const ChemicalReaction & getReaction() const
returns the underlying chemical reaction
static void pickleReaction(const ChemicalReaction *rxn, std::ostream &ss, unsigned int propertyFlags)
pickles a reaction 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
#define RDKIT_CHEMREACTIONS_EXPORT
Definition export.h:57
RDKIT_CHEMREACTIONS_EXPORT void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
std::vector< boost::uint64_t > RGROUPS
Std stuff.