RDKit: CartesianProduct.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

CartesianProduct.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
33 #ifndef CARTESIANPRODUCT_H
34 #define CARTESIANPRODUCT_H
35
36 #include "EnumerationStrategyBase.h"
37
38 namespace RDKit {
39 //! This is a class for enumerating reagents using Cartesian Products of
40 // reagents.
41 /*!
42 CartesianProductStrategy produces a standard walk through all possible
43 reagent combinations:
44
45 (0,0,0), (1,0,0), (2,0,0) ...
46
47 basic usage:
48
49 \verbatim
50 std::vector<MOL_SPTR_VECT> bbs;
51 bbs.push_back( bbs_for_reactants_1 );
52 bbs.push_back( bbs_for_reactants_2 );
53
54 RGRUOPS num_bbs;
55 num_bbs.push_back(bbs[0].size());
56 num_bbs.push_back(bbs[1].size());
57
58 CartesianProductStrategy rgroups(num_bbs);
59 for(size_t i=0; i<num_samples && rgroups; ++i) {
60 MOL_SPTR_VECT rvect = getReactantsFromRGroups(bbs, rgroups.next());
61 std::vector<MOL_SPTR_VECT> lprops = rxn.RunReactants(rvect);
62 ...
63 }
64 \endverbatim
65
66 See EnumerationStrategyBase for more details and usage.
67 */
68
69 class CartesianProductStrategy : public EnumerationStrategyBase {
70 size_t m_numPermutationsProcessed;
71
72 public:
73 CartesianProductStrategy()
74 : EnumerationStrategyBase(), m_numPermutationsProcessed() {}
75
76 using EnumerationStrategyBase::initialize;
77
78 virtual void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &) {
79 m_numPermutationsProcessed = 0;
80 }
81
82 virtual const char *type() const { return "CartesianProductStrategy"; }
83
84 //! The current permutation {r1, r2, ...}
85 virtual const EnumerationTypes::RGROUPS &next() {
86 if (m_numPermutationsProcessed) {
87 increment();
88 } else
89 ++m_numPermutationsProcessed;
90
91 return m_permutation;
92 }
93
94 virtual boost::uint64_t getPermutationIdx() const {
95 return m_numPermutationsProcessed; }
96
97 virtual operator bool() const { return hasNext(); }
98
99 EnumerationStrategyBase *copy() const {
100 return new CartesianProductStrategy(*this);
101 }
102
103 private:
104 void increment() {
105 next(0);
106 ++m_numPermutationsProcessed;
107 }
108
109 bool hasNext() const {
110 // Fix me -> use multiprecision int here???
111 if (m_numPermutations == EnumerationStrategyBase::EnumerationOverflow ||
112 m_numPermutationsProcessed < rdcast<size_t>(m_numPermutations)) {
113 return true;
114 } else {
115 return false;
116 }
117 }
118
119 void next(size_t rowToIncrement) {
120 if (!hasNext()) return;
121 m_permutation[rowToIncrement] += 1;
122 size_t max_index_of_row = m_permutationSizes[rowToIncrement] - 1;
123 if (m_permutation[rowToIncrement] > max_index_of_row) {
124 m_permutation[rowToIncrement] = 0;
125 next(rowToIncrement + 1);
126 }
127 }
128
129 private:
130 #ifdef RDK_USE_BOOST_SERIALIZATION
131 friend class boost::serialization::access;
132 template <class Archive>
133 void serialize(Archive &ar, const unsigned int /*version*/) {
134 ar &boost::serialization::base_object<EnumerationStrategyBase>(*this);
135 ar &m_numPermutationsProcessed;
136 }
137 #endif
138 };
139 }
140
141 #ifdef RDK_USE_BOOST_SERIALIZATION
142 BOOST_CLASS_VERSION(RDKit::CartesianProductStrategy, 1)
143 #endif
144
145 #endif
EnumerationStrategyBase.h
RDKit::CartesianProductStrategy::getPermutationIdx
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
Definition: CartesianProduct.h:94
RDKit::EnumerationStrategyBase::initialize
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
Definition: EnumerationStrategyBase.h:135
RDKit::ChemicalReaction
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
RDKit::EnumerationStrategyBase::EnumerationOverflow
static const boost::uint64_t EnumerationOverflow
Definition: EnumerationStrategyBase.h:123
RDKit::EnumerationStrategyBase::m_numPermutations
boost::uint64_t m_numPermutations
Definition: EnumerationStrategyBase.h:120
RDKit::EnumerationStrategyBase::m_permutation
EnumerationTypes::RGROUPS m_permutation
Definition: EnumerationStrategyBase.h:116
RDKit::EnumerationTypes::BBS
std::vector< MOL_SPTR_VECT > BBS
Definition: EnumerateTypes.h:41
RDKit
Std stuff.
Definition: Atom.h:29
RDKit::CartesianProductStrategy::next
virtual const EnumerationTypes::RGROUPS & next()
The current permutation {r1, r2, ...}.
Definition: CartesianProduct.h:85
RDKit::EnumerationStrategyBase
Definition: EnumerationStrategyBase.h:114
RDKit::EnumerationTypes::RGROUPS
std::vector< boost::uint64_t > RGROUPS
Definition: EnumerateTypes.h:55
RDKit::CartesianProductStrategy::CartesianProductStrategy
CartesianProductStrategy()
Definition: CartesianProduct.h:73
RDKit::CartesianProductStrategy
This is a class for enumerating reagents using Cartesian Products of.
Definition: CartesianProduct.h:69
RDKit::CartesianProductStrategy::initializeStrategy
virtual void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &)
Definition: CartesianProduct.h:78
RDKit::EnumerationStrategyBase::m_permutationSizes
EnumerationTypes::RGROUPS m_permutationSizes
Definition: EnumerationStrategyBase.h:118
RDKit::CartesianProductStrategy::type
virtual const char * type() const
Definition: CartesianProduct.h:82
RDKit::CartesianProductStrategy::copy
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state
Definition: CartesianProduct.h:99