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