RDKit: EnumerateBase.h Source File

| 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
41#include "EnumerationStrategyBase.h"
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*/
63 class RDKIT_CHEMREACTIONS_EXPORT EnumerateLibraryBase {
64 protected:
65 ChemicalReaction m_rxn;
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
74 EnumerateLibraryBase(const ChemicalReaction &rxn,
75 EnumerationStrategyBase *enumerator = nullptr)
76 : m_rxn(rxn),
77 m_enumerator(enumerator ? enumerator : new CartesianProductStrategy),
78 m_initialEnumerator(m_enumerator->copy()) {
79 m_rxn.initReactantMatchers();
80 }
81
82 //! Copy constructor
83 EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
84 : m_rxn(rhs.m_rxn),
85 m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : nullptr),
86 m_initialEnumerator(m_enumerator->copy()) {}
87
88 virtual ~EnumerateLibraryBase() {}
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
107 const EnumerationStrategyBase &getEnumerator() {
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.
130 const EnumerationTypes::RGROUPS &getPosition() const;
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
143 void resetState();
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
CartesianProduct.h
EnumerateTypes.h
EnumerationPickler.h
EnumerationStrategyBase.h
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
MolPickler.h
ReactionPickler.h
Reaction.h
RDKit::CartesianProductStrategy
Definition CartesianProduct.h:71
RDKit::ChemicalReaction
This is a class for storing and applying general chemical reactions.
Definition Reaction.h:121
RDKit::ChemicalReaction::getNumAgentTemplates
unsigned int getNumAgentTemplates() const
Definition Reaction.h:309
RDKit::ChemicalReaction::getNumReactantTemplates
unsigned int getNumReactantTemplates() const
Definition Reaction.h:303
RDKit::ChemicalReaction::getNumProductTemplates
unsigned int getNumProductTemplates() const
Definition Reaction.h:306
RDKit::EnumerateLibraryBase
Definition EnumerateBase.h:63
RDKit::EnumerateLibraryBase::getState
std::string getState() const
RDKit::EnumerateLibraryBase::getEnumerator
const EnumerationStrategyBase & getEnumerator()
return the current enumeration strategy
Definition EnumerateBase.h:107
RDKit::EnumerateLibraryBase::initFromStream
virtual void initFromStream(std::istream &ss)=0
initializes from a stream pickle
RDKit::EnumerateLibraryBase::m_rxn
ChemicalReaction m_rxn
Definition EnumerateBase.h:65
RDKit::EnumerateLibraryBase::~EnumerateLibraryBase
virtual ~EnumerateLibraryBase()
Definition EnumerateBase.h:88
RDKit::EnumerateLibraryBase::Serialize
virtual std::string Serialize() const
returns a string with a serialized (pickled) representation
Definition EnumerateBase.h:149
RDKit::EnumerateLibraryBase::m_enumerator
boost::shared_ptr< EnumerationStrategyBase > m_enumerator
Definition EnumerateBase.h:66
RDKit::EnumerateLibraryBase::next
virtual std::vector< MOL_SPTR_VECT > next()=0
RDKit::EnumerateLibraryBase::EnumerateLibraryBase
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
Copy constructor.
Definition EnumerateBase.h:83
RDKit::EnumerateLibraryBase::EnumerateLibraryBase
EnumerateLibraryBase()
default constructor
Definition EnumerateBase.h:71
RDKit::EnumerateLibraryBase::getPosition
const EnumerationTypes::RGROUPS & getPosition() const
RDKit::EnumerateLibraryBase::nextSmiles
virtual std::vector< std::vector< std::string > > nextSmiles()
RDKit::EnumerateLibraryBase::initFromString
virtual void initFromString(const std::string &text)
initializes from a string pickle
Definition EnumerateBase.h:159
RDKit::EnumerateLibraryBase::toStream
virtual void toStream(std::ostream &ss) const =0
serializes (pickles) to a stream
RDKit::EnumerateLibraryBase::reset
void reset()
reset the enumeration to the beginning.
Definition EnumerateBase.h:97
RDKit::EnumerateLibraryBase::EnumerateLibraryBase
EnumerateLibraryBase(const ChemicalReaction &rxn, EnumerationStrategyBase *enumerator=nullptr)
construct with a chemical reaction and an enumeration strategy
Definition EnumerateBase.h:74
RDKit::EnumerateLibraryBase::setState
void setState(const std::string &)
RDKit::EnumerateLibraryBase::resetState
void resetState()
Reset the enumerator to the beginning.
RDKit::EnumerateLibraryBase::m_initialEnumerator
boost::shared_ptr< EnumerationStrategyBase > m_initialEnumerator
Definition EnumerateBase.h:67
RDKit::EnumerateLibraryBase::getReaction
const ChemicalReaction & getReaction() const
returns the underlying chemical reaction
Definition EnumerateBase.h:104
RDKit::EnumerationStrategyBase
Definition EnumerationStrategyBase.h:123
RDKit::ReactionPickler::pickleReaction
static void pickleReaction(const ChemicalReaction *rxn, std::ostream &ss, unsigned int propertyFlags)
pickles a reaction and sends the results to stream ss
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_CHEMREACTIONS_EXPORT
#define RDKIT_CHEMREACTIONS_EXPORT
Definition export.h:57
RDKit::EnumerationStrategyPickler::pickle
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
RDKit::EnumerationTypes::RGROUPS
std::vector< boost::uint64_t > RGROUPS
Definition EnumerateTypes.h:56
RDKit
Std stuff.
Definition Abbreviations.h:19