RDKit
Open-source cheminformatics and machine learning.
Enumerate.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.n
31 //
32 #ifndef RDKIT_ENUMERATE_H
33 #define RDKIT_ENUMERATE_H
34 #include "EnumerateBase.h"
35 
36 /*! \file Enumerate.h
37 
38 \brief Contains the public API of the for the reaction enumeration engine
39 
40 \b Note that this should be considered beta and that the API may change in
41 future releases.
42 
43 */
44 
45 
46 namespace RDKit {
47 
48 //! This is a class for providing enumeration options that control
49 // how enumerations are performed.
50 /*!
51  Option
52  reagentMaxMatchCount [default INT_MAX]
53  This specifies how many times the reactant template can match a reagent.
54 
55  sanePartialProducts [default false]
56  If true, forces all products of the reagent plus the product templates\n\
57  pass chemical sanitization. Note that if the product template itself\n\
58  does not pass sanitization, then none of the products will.
59 */
61 {
65  reagentMaxMatchCount(INT_MAX), sanePartialProducts(false) {
66  }
67 
69  reagentMaxMatchCount(rhs.reagentMaxMatchCount),
70  sanePartialProducts(rhs.sanePartialProducts) {
71  }
72 };
73 
74 
75 //! Helper function, remove reagents that are incompatible
76 // with the reaction.
77 // rxn must be sanitized, initialized and preprocessed.
78 // this happens automatically in EnumerateLibrary
80  const ChemicalReaction &rxn,
82  const EnumerationParams &params=EnumerationParams());
83 
84 //! This is a class for running reactions on sets of reagents.
85 /*!
86  This class is a fully self contained reaction engine that can be
87  serialized and restarted. For example, a million products can
88  be generated, the engine can be saved for later and reloaded
89  to retrieve the next million products.
90 
91  basic usage will be something like:
92  \verbatim
93  ChemicalReaction rxn = ...
94  BBS bbs(num_rgroups);
95  ... somehow LoadRGroups(bbs[0]);
96  ... somehow LoadRGroups(bbs[1]..);
97  ...
98  EnumerateLibrary enumerator(en, bbs);
99  for(; (bool)en; ++i) {
100  // This is the same as rxn.run_Reactants( reagents );
101  std::vector<MOL_SPTR_VECT> products = en.next();
102  ...
103  }
104  \endverbatim
105 
106  In general, reactions will enumerate to more products than desired,
107  a standard use is:
108 
109  \verbatim
110  for(int i=0;i<num_samples && (bool)en; ++i) {
111  std::vector<MOL_SPTR_VECT> products = en.next();
112  ...
113  }
114  \endverbatim
115  */
116 
117 
119  EnumerationTypes::BBS m_bbs;
120 
121  public:
123  EnumerateLibrary(const std::string &s) : EnumerateLibraryBase(), m_bbs() {
124  initFromString(s);
125  }
126 
128  const EnumerationTypes::BBS &reagents,
129  const EnumerationParams & params = EnumerationParams());
131  const EnumerationTypes::BBS &reagents,
132  const EnumerationStrategyBase &enumerator,
133  const EnumerationParams & params = EnumerationParams());
135 
136  //! Return the reagents used in the library
137  const EnumerationTypes::BBS &getReagents() const { return m_bbs; }
138 
139  //! Get the next product set
140  std::vector<MOL_SPTR_VECT> next();
141 
142  void toStream(std::ostream &ss) const;
143  void initFromStream(std::istream &ss);
144 
145  private:
146 #ifdef RDK_USE_BOOST_SERIALIZATION
147  friend class boost::serialization::access;
148  template <class Archive>
149  void save(Archive &ar, const unsigned int /*version*/) const {
150  ar &boost::serialization::base_object<EnumerateLibraryBase>(*this);
151  size_t sz = m_bbs.size();
152  ar &sz;
153 
154  std::string pickle;
155  for (size_t i = 0; i < m_bbs.size(); ++i) {
156  sz = m_bbs[i].size();
157  ar &sz;
158  for (size_t j = 0; j < m_bbs[i].size(); ++j) {
159  MolPickler::pickleMol(*m_bbs[i][j], pickle);
160  ar &pickle;
161  }
162  }
163  }
164  template <class Archive>
165  void load(Archive &ar, const unsigned int /*version*/) {
166  ar &boost::serialization::base_object<EnumerateLibraryBase>(*this);
167 
168  size_t sz;
169  ar &sz;
170 
171  m_bbs.resize(sz);
172 
173  for (size_t i = 0; i < m_bbs.size(); ++i) {
174  ar &sz;
175  m_bbs[i].resize(sz);
176  std::string pickle;
177  for (size_t j = 0; j < m_bbs[i].size(); ++j) {
178  ar &pickle;
179  RWMol *mol = new RWMol();
180  MolPickler::molFromPickle(pickle, *mol);
181  m_bbs[i][j].reset(mol);
182  }
183  }
184  }
185 
186  BOOST_SERIALIZATION_SPLIT_MEMBER();
187 #endif
188 };
189 
191 
192 }
193 #endif
This is a class for providing enumeration options that control.
Definition: Enumerate.h:60
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
EnumerationTypes::BBS removeNonmatchingReagents(const ChemicalReaction &rxn, EnumerationTypes::BBS bbs, const EnumerationParams &params=EnumerationParams())
Helper function, remove reagents that are incompatible.
RWMol is a molecule class that is intended to be edited.
Definition: RWMol.h:30
This is a class for running reactions on sets of reagents.
Definition: Enumerate.h:118
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
EnumerateLibrary(const std::string &s)
Definition: Enumerate.h:123
std::vector< MOL_SPTR_VECT > BBS
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
const EnumerationTypes::BBS & getReagents() const
Return the reagents used in the library.
Definition: Enumerate.h:137
EnumerationParams(const EnumerationParams &rhs)
Definition: Enumerate.h:68
Base class for enumerating chemical reactions from collections of.
Definition: EnumerateBase.h:62
static void molFromPickle(const std::string &pickle, ROMol *mol)
constructs a molecule from a pickle stored in a string
std::ostream & toStream(std::ostream &)
void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
bool EnumerateLibraryCanSerialize()