RDKit
Open-source cheminformatics and machine learning.
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 #ifndef RDKIT_ENUMERATEBASE_H
33 #define RDKIT_ENUMERATEBASE_H
34 
35 #include <vector>
36 #include "EnumerateTypes.h"
37 #include "../Reaction.h"
38 #include "EnumerationPickler.h"
39 
41 #include "CartesianProduct.h"
42 #include "../ReactionPickler.h"
43 #include <GraphMol/MolPickler.h>
44 
45 namespace RDKit {
46 //! Base class for enumerating chemical reactions from collections of
47 // building blocks and reagents.
48 /*!
49  basic usage:
50 
51  \verbatim
52  EnumerateLibraryBase &enumerator;
53  while (enumerator) {
54  MOL_SPTR_VECT res = enumerator.next();
55  // do something with enumeration products here
56  }
57  \endverbatim
58 
59  See Reaction.h for more details on how ChemicalReactions are
60  used.
61 */
63  protected:
65  boost::shared_ptr<EnumerationStrategyBase> m_enumerator;
66  boost::shared_ptr<EnumerationStrategyBase> m_initialEnumerator;
67 
68  public:
69  //! default constructor
70  EnumerateLibraryBase() : m_rxn(), m_enumerator(), m_initialEnumerator() {}
71 
72  //! construct with a chemical reaction and an enumeration strategy
74  EnumerationStrategyBase *enumerator = 0)
75  : m_rxn(rxn),
76  m_enumerator(enumerator ? enumerator : new CartesianProductStrategy),
77  m_initialEnumerator(m_enumerator->copy()) {
78  m_rxn.initReactantMatchers();
79  }
80 
81  //! Copy constructor
83  : m_rxn(rhs.m_rxn),
84  m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : 0),
85  m_initialEnumerator(m_enumerator->copy()) {}
86 
87  virtual ~EnumerateLibraryBase() {}
88 
89  //! Are there any enumerations left?
90  virtual operator bool() const {
91  PRECONDITION(m_enumerator.get(), "Null enumeration strategy");
92  return static_cast<bool>(*m_enumerator);
93  }
94 
95  //! reset the enumeration to the beginning.
96  void reset() {
97  if (m_initialEnumerator.get()) {
98  m_enumerator.reset(m_initialEnumerator->copy());
99  }
100  }
101 
102  //! returns the underlying chemical reaction
103  const ChemicalReaction &getReaction() const { return m_rxn; }
104 
105  //! return the current enumeration strategy
107  PRECONDITION(m_enumerator.get(), "Null Enumerator");
108  return *m_enumerator;
109  }
110 
111  //! get the next set of products (See run_Reactants) for details
112  // This returns a vector of a vector of molecules.
113  // Each result vector corresponds for a product template.
114  // i.e.
115  // res = library.next();
116  // res[0] are the results for library.getReaction().getProdcts()[0]
117  virtual std::vector<MOL_SPTR_VECT> next() = 0;
118 
119  //! get the next set of products as smiles
120  // This returns a vector of a vector strings.
121  // Each result vector corresponds for a product template.
122  virtual std::vector<std::vector<std::string> > nextSmiles();
123 
124  //! Get the current position into the reagent vectors
125  // Use getState/setState to save/restart the enumeration
126  // from this position.
127  const EnumerationTypes::RGROUPS &getPosition() const;
128 
129  //! Get the current state of the enumerator
130  // This is the position of the enumerator and the enumerators
131  // state that can be used to restart enumerating
132  // from this position.
133  std::string getState() const;
134 
135  //! Set the current state of the enumerator
136  // Restart the enumerator from this position.
137  void setState(const std::string &);
138 
139  //! Reset the enumerator to the beginning
140  void resetState();
141 
142  //! serializes (pickles) to a stream
143  virtual void toStream(std::ostream &ss) const = 0;
144 
145  //! returns a string with a serialized (pickled) representation
146  virtual std::string Serialize() const {
147  std::stringstream ss;
148  toStream(ss);
149  return ss.str();
150  }
151 
152  //! initializes from a stream pickle
153  virtual void initFromStream(std::istream &ss) = 0;
154 
155  //! initializes from a string pickle
156  virtual void initFromString(const std::string &text) {
157  std::stringstream ss(text);
158  initFromStream(ss);
159  }
160 
161  private:
162 #ifdef RDK_USE_BOOST_SERIALIZATION
163  friend class boost::serialization::access;
164  template <class Archive>
165  void save(Archive &ar, const unsigned int) const {
166  std::string pickle;
167  ReactionPickler::pickleReaction(m_rxn, pickle);
168  ar &pickle;
169  ar &m_enumerator;
170  // we handle the m_initialEnumerator from a string
171  // for backwards compatibility with a unreleased
172  // version
173  EnumerationStrategyPickler::pickle(m_initialEnumerator, pickle);
174  ar &pickle;
175  }
176  template <class Archive>
177  void load(Archive &ar, const unsigned int /*version*/) {
178  // this should only be called on non-initialized reactions
179  if (m_rxn.getNumReactantTemplates() || m_rxn.getNumProductTemplates() ||
180  m_rxn.getNumAgentTemplates()) {
181  throw ValueErrorException("EnumerateBase already created from archive.");
182  }
183  std::string pickle;
184  ar &pickle;
186  ar &m_enumerator;
187  ar &pickle;
188  m_initialEnumerator = EnumerationStrategyPickler::fromPickle(pickle);
189  }
190 
191  BOOST_SERIALIZATION_SPLIT_MEMBER();
192 #endif
193 };
194 
195 #ifdef RDK_USE_BOOST_SERIALIZATION
196 BOOST_SERIALIZATION_ASSUME_ABSTRACT(EnumerateLibraryBase)
197 #endif
198 }
199 #endif
const ChemicalReaction & getReaction() const
returns the underlying chemical reaction
void initReactantMatchers()
initializes our internal reactant-matching datastructures.
virtual std::vector< MOL_SPTR_VECT > next()=0
get the next set of products (See run_Reactants) for details
EnumerateLibraryBase()
default constructor
Definition: EnumerateBase.h:70
virtual std::string Serialize() const
returns a string with a serialized (pickled) representation
std::string getState() const
Get the current state of the enumerator.
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
boost::shared_ptr< EnumerationStrategyBase > fromPickle(std::istream &pickle)
static void reactionFromPickle(const std::string &pickle, ChemicalReaction *rxn)
constructs a reaction from a pickle stored in a string
EnumerateLibraryBase(const ChemicalReaction &rxn, EnumerationStrategyBase *enumerator=0)
construct with a chemical reaction and an enumeration strategy
Definition: EnumerateBase.h:73
void reset()
reset the enumeration to the beginning.
Definition: EnumerateBase.h:96
unsigned int getNumProductTemplates() const
Definition: Reaction.h:276
Std stuff.
Definition: Atom.h:29
Base class for enumerating chemical reactions from collections of.
Definition: EnumerateBase.h:62
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
Copy constructor.
Definition: EnumerateBase.h:82
boost::shared_ptr< EnumerationStrategyBase > m_enumerator
Definition: EnumerateBase.h:65
const EnumerationStrategyBase & getEnumerator()
return the current enumeration strategy
virtual std::vector< std::vector< std::string > > nextSmiles()
get the next set of products as smiles
void setState(const std::string &)
Set the current state of the enumerator.
std::vector< boost::uint64_t > RGROUPS
#define PRECONDITION(expr, mess)
Definition: Invariant.h:107
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition: Exceptions.h:32
This is a class for enumerating reagents using Cartesian Products of.
unsigned int getNumAgentTemplates() const
Definition: Reaction.h:279
static void pickleReaction(const ChemicalReaction *rxn, std::ostream &ss)
pickles a reaction and sends the results to stream ss
virtual void initFromStream(std::istream &ss)=0
initializes from a stream pickle
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
const EnumerationTypes::RGROUPS & getPosition() const
Get the current position into the reagent vectors.
void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
boost::shared_ptr< EnumerationStrategyBase > m_initialEnumerator
Definition: EnumerateBase.h:66
void resetState()
Reset the enumerator to the beginning.
unsigned int getNumReactantTemplates() const
Definition: Reaction.h:273