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 #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 
46 namespace 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 = 0)
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
84  : m_rxn(rhs.m_rxn),
85  m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : 0),
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
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.
128  const EnumerationTypes::RGROUPS &getPosition() const;
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
141  void resetState();
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;
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
197 BOOST_SERIALIZATION_ASSUME_ABSTRACT(EnumerateLibraryBase)
198 #endif
199 }
200 #endif
const ChemicalReaction & getReaction() const
returns the underlying chemical reaction
void initReactantMatchers()
initializes our internal reactant-matching datastructures.
EnumerateLibraryBase()
default constructor
Definition: EnumerateBase.h:71
virtual std::string Serialize() const
returns a string with a serialized (pickled) representation
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:118
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
static void reactionFromPickle(const std::string &pickle, ChemicalReaction *rxn)
RDKIT_CHEMREACTIONS_EXPORT boost::shared_ptr< EnumerationStrategyBase > fromPickle(std::istream &pickle)
EnumerateLibraryBase(const ChemicalReaction &rxn, EnumerationStrategyBase *enumerator=0)
construct with a chemical reaction and an enumeration strategy
Definition: EnumerateBase.h:74
void reset()
reset the enumeration to the beginning.
Definition: EnumerateBase.h:97
unsigned int getNumProductTemplates() const
Definition: Reaction.h:283
Std stuff.
Definition: Atom.h:30
static void pickleReaction(const ChemicalReaction *rxn, std::ostream &ss, unsigned int propertyFlags)
pickles a reaction and sends the results to stream ss
Base class for enumerating chemical reactions from collections of.
Definition: EnumerateBase.h:63
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
Copy constructor.
Definition: EnumerateBase.h:83
boost::shared_ptr< EnumerationStrategyBase > m_enumerator
Definition: EnumerateBase.h:66
const EnumerationStrategyBase & getEnumerator()
return the current enumeration strategy
std::vector< boost::uint64_t > RGROUPS
#define RDKIT_CHEMREACTIONS_EXPORT
Definition: export.h:60
#define PRECONDITION(expr, mess)
Definition: Invariant.h:108
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition: Exceptions.h:33
This is a class for enumerating reagents using Cartesian Products of.
unsigned int getNumAgentTemplates() const
Definition: Reaction.h:286
virtual void initFromString(const std::string &text)
initializes from a string pickle
boost::shared_ptr< EnumerationStrategyBase > m_initialEnumerator
Definition: EnumerateBase.h:67
unsigned int getNumReactantTemplates() const
Definition: Reaction.h:280