RDKit
Open-source cheminformatics and machine learning.
EvenSamplePairs.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016, 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 RGROUP_EVEN_SAMPLE_H
34 #define RGROUP_EVEN_SAMPLE_H
35 
37 #ifdef RDK_USE_BOOST_SERIALIZATION
38 #include <boost/serialization/set.hpp>
39 #endif
40 #include <boost/cstdint.hpp>
41 
42 namespace RDKit {
43 //! EvenSamplePairsStrategy
44 /*! Randomly sample Pairs evenly from a collection of building blocks
45  This is a good strategy for choosing a relatively small selection
46  of building blocks from a larger set. As the amount of work needed
47  to retrieve the next evenly sample building block grows with the
48  number of samples, this method performs progressively worse as the
49  number of samples gets larger.
50 
51  See EnumeartionStrategyBase for more details.
52 */
53 
55  boost::uint64_t m_numPermutationsProcessed;
56 
57  std::vector<boost::int64_t> used_count;
58  std::vector<std::vector<size_t> > var_used;
59  std::vector<std::vector<size_t> > pair_used;
60  std::vector<std::vector<size_t> > pair_counts;
61  std::set<size_t> selected;
62 
63  size_t seed; // last seed for permutation (starts at 0)
64  size_t M, a, b; // random number stuff
65  size_t nslack, min_nslack;
66  size_t rejected_period, rejected_unique;
67  size_t rejected_slack_condition, rejected_bb_sampling_condition;
68 
69  public:
72  m_numPermutationsProcessed(),
73  used_count(),
74  var_used(),
75  pair_used(),
76  pair_counts(),
77  selected(),
78  seed(),
79  M(),
80  a(),
81  b(),
82  nslack(),
83  min_nslack(),
84  rejected_period(),
85  rejected_unique(),
86  rejected_slack_condition(),
87  rejected_bb_sampling_condition() {}
88 
91  m_numPermutationsProcessed(rhs.m_numPermutationsProcessed),
92  used_count(rhs.used_count),
93  var_used(rhs.var_used),
94  pair_used(rhs.pair_used),
95  pair_counts(rhs.pair_counts),
96  selected(rhs.selected),
97  seed(rhs.seed),
98  M(rhs.M),
99  a(rhs.a),
100  b(rhs.b),
101  nslack(rhs.nslack),
102  min_nslack(rhs.min_nslack),
103  rejected_period(rhs.rejected_period),
104  rejected_unique(rhs.rejected_unique),
105  rejected_slack_condition(rhs.rejected_slack_condition),
106  rejected_bb_sampling_condition(rhs.rejected_bb_sampling_condition) {}
107 
108  virtual const char *type() const { return "EvenSamplePairsStrategy"; }
109 
110  //! This is a class for enumerating RGroups using Cartesian Products of
111  //! reagents.
112  /*!
113  basic usage:
114 
115  \verbatim
116  std::vector<MOL_SPTR_VECT> bbs;
117  bbs.push_back( bbs_for_reactants_1 );
118  bbs.push_back( bbs_for_reactants_2 );
119 
120  EvenSamplePairsStrategy rgroups;
121  rgroups.initialize(rxn, bbs);
122  for(size_t i=0; i<num_samples && rgroups; ++i) {
123  MOL_SPTR_VECT rvect = getReactantsFromRGroups(bbs, rgroups.next());
124  std::vector<MOL_SPTR_VECT> lprops = rxn.RunReactants(rvect);
125  ...
126  }
127  \endverbatim
128  */
130 
131  virtual void initializeStrategy(const ChemicalReaction &,
132  const EnumerationTypes::BBS &);
133 
134  //! The current permutation {r1, r2, ...}
135  virtual const EnumerationTypes::RGROUPS &next();
136 
137  virtual boost::uint64_t getPermutationIdx() const {
138  return m_numPermutationsProcessed;
139  }
140 
141  virtual operator bool() const { return true; }
142 
144  return new EvenSamplePairsStrategy(*this);
145  }
146 
147  std::string stats() const;
148 
149  private:
151 
152  // decode a packed integer into an RGroup selection
153  const EnumerationTypes::RGROUPS &decode(size_t seed) {
154  for (boost::int64_t j = m_permutationSizes.size() - 1; j >= 0; j--) {
155  m_permutation[j] = seed % m_permutationSizes[j];
156  seed /= m_permutationSizes[j];
157  }
158  return m_permutation;
159  }
160 
161  bool try_add(size_t seed);
162 
163  public:
164 #ifdef RDK_USE_BOOST_SERIALIZATION
165  template <class Archive>
166  void serialize(Archive &ar, const unsigned int /*version*/) {
167  // invoke serialization of the base class
168  ar &boost::serialization::base_object<EnumerationStrategyBase>(*this);
169  ar &m_numPermutationsProcessed;
170  ar &used_count;
171  ar &var_used;
172  ar &pair_used;
173  ar &pair_counts;
174  ar &selected;
175 
176  ar &seed;
177 
178  ar &M;
179  ar &a;
180  ar &b;
181 
182  ar &nslack;
183  ar &min_nslack;
184  ar &rejected_period;
185  ar &rejected_unique;
186  ar &rejected_slack_condition;
187  ar &rejected_bb_sampling_condition;
188  }
189 #endif
190 };
191 }
192 
193 BOOST_CLASS_VERSION(RDKit::EvenSamplePairsStrategy, 1)
194 
195 #endif
virtual const EnumerationTypes::RGROUPS & next()
The current permutation {r1, r2, ...}.
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
EvenSamplePairsStrategy(const EvenSamplePairsStrategy &rhs)
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
EnumerationTypes::RGROUPS m_permutation
std::string stats() const
EvenSamplePairsStrategy.
std::vector< MOL_SPTR_VECT > BBS
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
virtual const char * type() const
std::vector< boost::uint64_t > RGROUPS
EnumerationTypes::RGROUPS m_permutationSizes
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state
friend class boost::serialization::access
virtual void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &)