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
71 EnumerateLibraryBase() : m_rxn(), m_enumerator(), m_initialEnumerator() {}
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),
78 m_initialEnumerator(m_enumerator->copy()) {
80 }
81
82 //! Copy constructor
84 : m_rxn(rhs.m_rxn),
85 m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : nullptr),
86 m_initialEnumerator(m_enumerator->copy()) {}
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
126 /// Use getState/setState to save/restart the enumeration
127 /// from this position.
129
130 //! Get the current state of the enumerator
131 /// This is the position of the enumerator and the enumerators
132 /// state that can be used to restart enumerating
133 /// from this position.
134 std::string getState() const;
135
136 //! Set the current state of the enumerator
137 /// Restart the enumerator from this position.
138 void setState(const std::string &);
139
140 //! Reset the enumerator to the beginning
142
143 //! serializes (pickles) to a stream
144 virtual void toStream(std::ostream &ss) const = 0;
145
146 //! returns a string with a serialized (pickled) representation
147 virtual std::string Serialize() const {
148 std::stringstream ss;
149 toStream(ss);
150 return ss.str();
151 }
152
153 //! initializes from a stream pickle
154 virtual void initFromStream(std::istream &ss) = 0;
155
156 //! initializes from a string pickle
157 virtual void initFromString(const std::string &text) {
158 std::stringstream ss(text);
159 initFromStream(ss);
160 }
161
162 private:
163#ifdef RDK_USE_BOOST_SERIALIZATION
164 friend class boost::serialization::access;
165 template <class Archive>
166 void save(Archive &ar, const unsigned int) const {
167 std::string pickle;
168 ReactionPickler::pickleReaction(m_rxn, pickle);
169 ar & pickle;
170 ar & m_enumerator;
171 // we handle the m_initialEnumerator from a string
172 // for backwards compatibility with a unreleased
173 // version
174 EnumerationStrategyPickler::pickle(m_initialEnumerator, pickle);
175 ar & pickle;
176 }
177 template <class Archive>
178 void load(Archive &ar, const unsigned int /*version*/) {
179 // this should only be called on non-initialized reactions
180 if (m_rxn.getNumReactantTemplates() || m_rxn.getNumProductTemplates() ||
181 m_rxn.getNumAgentTemplates()) {
182 throw ValueErrorException("EnumerateBase already created from archive.");
183 }
184 std::string pickle;
185 ar & pickle;
186 ReactionPickler::reactionFromPickle(pickle, m_rxn);
187 ar & m_enumerator;
188 ar & pickle;
189 m_initialEnumerator = EnumerationStrategyPickler::fromPickle(pickle);
190 }
191
192 BOOST_SERIALIZATION_SPLIT_MEMBER();
193#endif
194};
195
196#ifdef RDK_USE_BOOST_SERIALIZATION
197BOOST_SERIALIZATION_ASSUME_ABSTRACT(EnumerateLibraryBase)
198#endif
199} // namespace RDKit
200#endif
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
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
void initReactantMatchers(bool silent=false)
initializes our internal reactant-matching datastructures.
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
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:49
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.
bool rdvalue_is(const RDValue_cast_t)