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