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