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 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 */
64  : reagentMaxMatchCount(INT_MAX), sanePartialProducts(false) {}
65 
67  : reagentMaxMatchCount(rhs.reagentMaxMatchCount),
68  sanePartialProducts(rhs.sanePartialProducts) {}
69 };
70 
71 //! Helper function, remove reagents that are incompatible
72 // with the reaction.
73 // rxn must be sanitized, initialized and preprocessed.
74 // this happens automatically in EnumerateLibrary
76  const ChemicalReaction &rxn, EnumerationTypes::BBS bbs,
77  const EnumerationParams &params = EnumerationParams());
78 
79 //! This is a class for running reactions on sets of reagents.
80 /*!
81  This class is a fully self contained reaction engine that can be
82  serialized and restarted. For example, a million products can
83  be generated, the engine can be saved for later and reloaded
84  to retrieve the next million products.
85 
86  basic usage will be something like:
87  \verbatim
88  ChemicalReaction rxn = ...
89  BBS bbs(num_rgroups);
90  ... somehow LoadRGroups(bbs[0]);
91  ... somehow LoadRGroups(bbs[1]..);
92  ...
93  EnumerateLibrary enumerator(en, bbs);
94  for(; (bool)en; ++i) {
95  // This is the same as rxn.run_Reactants( reagents );
96  std::vector<MOL_SPTR_VECT> products = en.next();
97  ...
98  }
99  \endverbatim
100 
101  In general, reactions will enumerate to more products than desired,
102  a standard use is:
103 
104  \verbatim
105  for(int i=0;i<num_samples && (bool)en; ++i) {
106  std::vector<MOL_SPTR_VECT> products = en.next();
107  ...
108  }
109  \endverbatim
110  */
111 
113  : public EnumerateLibraryBase {
114  EnumerationTypes::BBS m_bbs;
115 
116  public:
118  EnumerateLibrary(const std::string &s) : EnumerateLibraryBase(), m_bbs() {
119  initFromString(s);
120  }
121 
123  const EnumerationTypes::BBS &reagents,
124  const EnumerationParams &params = EnumerationParams());
126  const EnumerationTypes::BBS &reagents,
127  const EnumerationStrategyBase &enumerator,
128  const EnumerationParams &params = EnumerationParams());
130 
131  //! Return the reagents used in the library
132  const EnumerationTypes::BBS &getReagents() const { return m_bbs; }
133 
134  //! Get the next product set
135  std::vector<MOL_SPTR_VECT> next();
136 
137  void toStream(std::ostream &ss) const;
138  void initFromStream(std::istream &ss);
139 
140  private:
141 #ifdef RDK_USE_BOOST_SERIALIZATION
142  friend class boost::serialization::access;
143  template <class Archive>
144  void save(Archive &ar, const unsigned int /*version*/) const {
145  ar &boost::serialization::base_object<EnumerateLibraryBase>(*this);
146  size_t sz = m_bbs.size();
147  ar &sz;
148 
149  std::string pickle;
150  for (size_t i = 0; i < m_bbs.size(); ++i) {
151  sz = m_bbs[i].size();
152  ar &sz;
153  for (size_t j = 0; j < m_bbs[i].size(); ++j) {
154  MolPickler::pickleMol(*m_bbs[i][j], pickle);
155  ar &pickle;
156  }
157  }
158  }
159  template <class Archive>
160  void load(Archive &ar, const unsigned int /*version*/) {
161  ar &boost::serialization::base_object<EnumerateLibraryBase>(*this);
162 
163  size_t sz;
164  ar &sz;
165 
166  m_bbs.resize(sz);
167 
168  for (size_t i = 0; i < m_bbs.size(); ++i) {
169  ar &sz;
170  m_bbs[i].resize(sz);
171  std::string pickle;
172  for (size_t j = 0; j < m_bbs[i].size(); ++j) {
173  ar &pickle;
174  RWMol *mol = new RWMol();
176  m_bbs[i][j].reset(mol);
177  }
178  }
179  }
180 
181  BOOST_SERIALIZATION_SPLIT_MEMBER();
182 #endif
183 };
184 
186 
187 } // namespace RDKit
188 #endif
RDKit::EnumerateLibrary::EnumerateLibrary
EnumerateLibrary()
Definition: Enumerate.h:117
EnumerateBase.h
RDKit::EnumerationStrategyPickler::pickle
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
RDKit::EnumerateLibraryBase
Base class for enumerating chemical reactions from collections of.
Definition: EnumerateBase.h:63
RDKit::EnumerationParams::sanePartialProducts
bool sanePartialProducts
Definition: Enumerate.h:62
RDKit::EnumerateLibrary::EnumerateLibrary
EnumerateLibrary(const std::string &s)
Definition: Enumerate.h:118
RDKit::EnumerateLibrary::getReagents
const EnumerationTypes::BBS & getReagents() const
Return the reagents used in the library.
Definition: Enumerate.h:132
RDKit::MolPickler::molFromPickle
static void molFromPickle(const std::string &pickle, ROMol *mol)
constructs a molecule from a pickle stored in a string
RDKit::EnumerationTypes::BBS
std::vector< MOL_SPTR_VECT > BBS
Definition: EnumerateTypes.h:42
RDKit::removeNonmatchingReagents
RDKIT_CHEMREACTIONS_EXPORT EnumerationTypes::BBS removeNonmatchingReagents(const ChemicalReaction &rxn, EnumerationTypes::BBS bbs, const EnumerationParams &params=EnumerationParams())
Helper function, remove reagents that are incompatible.
RDKit::ChemicalReaction
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:119
RDKit::EnumerateLibraryCanSerialize
RDKIT_CHEMREACTIONS_EXPORT bool EnumerateLibraryCanSerialize()
RDKit::EnumerationStrategyBase
Definition: EnumerationStrategyBase.h:120
RDKit::EnumerationParams::EnumerationParams
EnumerationParams(const EnumerationParams &rhs)
Definition: Enumerate.h:66
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RDKit::EnumerateLibrary
This is a class for running reactions on sets of reagents.
Definition: Enumerate.h:112
RDKit
Std stuff.
Definition: Atom.h:30
RDKIT_CHEMREACTIONS_EXPORT
#define RDKIT_CHEMREACTIONS_EXPORT
Definition: export.h:60
RDLog::toStream
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
RDKit::EnumerationParams::reagentMaxMatchCount
int reagentMaxMatchCount
Definition: Enumerate.h:61
RDKit::EnumerationParams
This is a class for providing enumeration options that control.
Definition: Enumerate.h:60
RDKit::EnumerationParams::EnumerationParams
EnumerationParams()
Definition: Enumerate.h:63
export.h