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