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  size_t m_numPermutationsProcessed;
72 
73  public:
75  : EnumerationStrategyBase(), m_numPermutationsProcessed() {}
76 
78 
80  m_numPermutationsProcessed = 0;
81  }
82 
83  virtual const char *type() const { return "CartesianProductStrategy"; }
84 
85  //! The current permutation {r1, r2, ...}
86  virtual const EnumerationTypes::RGROUPS &next() {
87  if (m_numPermutationsProcessed) {
88  increment();
89  } else
90  ++m_numPermutationsProcessed;
91 
92  return m_permutation;
93  }
94 
95  virtual boost::uint64_t getPermutationIdx() const {
96  return m_numPermutationsProcessed; }
97 
98  virtual operator bool() const { return hasNext(); }
99 
101  return new CartesianProductStrategy(*this);
102  }
103 
104  private:
105  void increment() {
106  next(0);
107  ++m_numPermutationsProcessed;
108  }
109 
110  bool hasNext() const {
111  // Fix me -> use multiprecision int here???
112  if (m_numPermutations == EnumerationStrategyBase::EnumerationOverflow ||
113  m_numPermutationsProcessed < rdcast<size_t>(m_numPermutations)) {
114  return true;
115  } else {
116  return false;
117  }
118  }
119 
120  void next(size_t rowToIncrement) {
121  if (!hasNext()) return;
122  m_permutation[rowToIncrement] += 1;
123  size_t max_index_of_row = m_permutationSizes[rowToIncrement] - 1;
124  if (m_permutation[rowToIncrement] > max_index_of_row) {
125  m_permutation[rowToIncrement] = 0;
126  next(rowToIncrement + 1);
127  }
128  }
129 
130  private:
131 #ifdef RDK_USE_BOOST_SERIALIZATION
132  friend class boost::serialization::access;
133  template <class Archive>
134  void serialize(Archive &ar, const unsigned int /*version*/) {
135  ar &boost::serialization::base_object<EnumerationStrategyBase>(*this);
136  ar &m_numPermutationsProcessed;
137  }
138 #endif
139 };
140 }
141 
142 #ifdef RDK_USE_BOOST_SERIALIZATION
143 BOOST_CLASS_VERSION(RDKit::CartesianProductStrategy, 1)
144 #endif
145 
146 #endif
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:118
static const boost::uint64_t EnumerationOverflow
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state
std::vector< MOL_SPTR_VECT > BBS
Std stuff.
Definition: Atom.h:30
virtual const EnumerationTypes::RGROUPS & next()
The current permutation {r1, r2, ...}.
std::vector< boost::uint64_t > RGROUPS
#define RDKIT_CHEMREACTIONS_EXPORT
Definition: export.h:60
This is a class for enumerating reagents using Cartesian Products of.
virtual void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &)
virtual const char * type() const