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  public:
68  //! default constructor
70  m_enumerator(),
71  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  {
80  m_rxn.initReactantMatchers();
81  }
82 
83  //! Copy constructor
85  : m_rxn(rhs.m_rxn),
86  m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : 0),
87  m_initialEnumerator( m_enumerator->copy() ) {}
88 
89  virtual ~EnumerateLibraryBase() {}
90 
91  //! Are there any enumerations left?
92  virtual operator bool() const {
93  PRECONDITION(m_enumerator.get(), "Null enumeration strategy");
94  return static_cast<bool>(*m_enumerator);
95  }
96 
97  //! reset the enumeration to the beginning.
98  void reset() {
99  if(m_initialEnumerator.get()) {
100  m_enumerator.reset(m_initialEnumerator->copy());
101  }
102  }
103 
104  //! returns the underlying chemical reaction
105  const ChemicalReaction &getReaction() const { return m_rxn; }
106 
107  //! return the current enumeration strategy
109  PRECONDITION(m_enumerator.get(), "Null Enumerator");
110  return *m_enumerator;
111  }
112 
113  //! get the next set of products (See run_Reactants) for details
114  // This returns a vector of a vector of molecules.
115  // Each result vector corresponds for a product template.
116  // i.e.
117  // res = library.next();
118  // res[0] are the results for library.getReaction().getProdcts()[0]
119  virtual std::vector<MOL_SPTR_VECT> next() = 0;
120 
121  //! get the next set of products as smiles
122  // This returns a vector of a vector strings.
123  // Each result vector corresponds for a product template.
124  virtual std::vector<std::vector<std::string> > nextSmiles();
125 
126  //! Get the current position into the reagent vectors
127  // Use getState/setState to save/restart the enumeration
128  // from this position.
129  const EnumerationTypes::RGROUPS &getPosition() const;
130 
131  //! Get the current state of the enumerator
132  // This is the position of the enumerator and the enumerators
133  // state that can be used to restart enumerating
134  // from this position.
135  std::string getState() const;
136 
137  //! Set the current state of the enumerator
138  // Restart the enumerator from this position.
139  void setState(const std::string &);
140 
141  //! Reset the enumerator to the beginning
142  void resetState();
143 
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,
177  pickle);
178  ar &pickle;
179  }
180  template <class Archive>
181  void load(Archive &ar, const unsigned int /*version*/) {
182  std::string pickle;
183  ar &pickle;
185  ar &m_enumerator;
186  ar &pickle;
187  m_initialEnumerator = \
189 
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
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
std::string getState() const
Get the current state of the enumerator.
EnumerateLibraryBase()
default constructor
Definition: EnumerateBase.h:69
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
boost::shared_ptr< EnumerationStrategyBase > fromPickle(std::istream &pickle)
virtual std::string Serialize() const
returns a string with a serialized (pickled) representation
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:74
void reset()
reset the enumeration to the beginning.
Definition: EnumerateBase.h:98
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
Base class for enumerating chemical reactions from collections of.
Definition: EnumerateBase.h:62
const EnumerationTypes::RGROUPS & getPosition() const
Get the current position into the reagent vectors.
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
Copy constructor.
Definition: EnumerateBase.h:84
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
This is a class for enumerating reagents using Cartesian Products of.
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
const ChemicalReaction & getReaction() const
returns the underlying chemical reaction
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 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.