RDKit
Open-source cheminformatics and machine learning.
RandomSampleAllBBs.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 RGROUP_RANDOM_SAMPLE_ALLBBS_H
34 #define RGROUP_RANDOM_SAMPLE_ALLBBS_H
35 
37 #include <boost/random.hpp>
38 #include <boost/random/uniform_int_distribution.hpp>
39 #include <sstream>
40 
41 namespace RDKit {
42 //! RandomSampleAllBBsStrategy
43 //! Randomly sample rgroup indices
44 
45 //! This is a class for randomly enumerating reagents that ensures all reagents
46 // are sampled.
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  RandomSampleAllBBsStrategy rgroups;
56  rgroups.initialize(rxn, bbs);
57  for(size_t i=0; i<num_samples && rgroups; ++i) {
58  MOL_SPTR_VECT rvect = getReactantsFromRGroups(bbs, rgroups.next());
59  std::vector<MOL_SPTR_VECT> lprops = rxn.RunReactants(rvect);
60  ...
61  }
62  \endverbatim
63 
64  See EnumerationStrategyBase for more details and usage.
65 */
66 
68  boost::uint64_t m_numPermutationsProcessed;
69  size_t m_offset;
70  size_t m_maxoffset;
71 
72  boost::minstd_rand m_rng;
73  std::vector<boost::random::uniform_int_distribution<> > m_distributions;
74 
75  public:
78  m_numPermutationsProcessed(0),
79  m_offset(0),
80  m_maxoffset(0),
81  m_rng(),
82  m_distributions() {
83  for (size_t i = 0; i < m_permutation.size(); ++i) {
84  m_distributions.push_back(
85  boost::random::uniform_int_distribution<>(0, m_permutation[i] - 1));
86  }
87  }
89 
91  m_distributions.clear();
92  m_permutation.resize(m_permutationSizes.size());
94  m_offset = 0;
95  m_maxoffset =
96  *std::max_element(m_permutationSizes.begin(), m_permutationSizes.end());
97  for (size_t i = 0; i < m_permutationSizes.size(); ++i) {
98  m_distributions.push_back(boost::random::uniform_int_distribution<>(
99  0, m_permutationSizes[i] - 1));
100  }
101 
102  m_numPermutationsProcessed = 0;
103  }
104 
105  virtual const char *type() const { return "RandomSampleAllBBsStrategy"; }
106 
107  //! The current permutation {r1, r2, ...}
108  virtual const EnumerationTypes::RGROUPS &next() {
109  if (m_offset >= m_maxoffset) {
110  for (size_t i = 0; i < m_permutation.size(); ++i) {
111  m_permutation[i] = m_distributions[i](m_rng);
112  }
113  m_offset = 0;
114  } else {
115  for (size_t i = 0; i < m_permutation.size(); ++i) {
117  }
118  ++m_offset;
119  }
120  ++m_numPermutationsProcessed;
121 
122  return m_permutation;
123  }
124 
125  virtual boost::uint64_t getPermutationIdx() const {
126  return m_numPermutationsProcessed; }
127 
128  virtual operator bool() const { return true; }
129 
131  return new RandomSampleAllBBsStrategy(*this);
132  }
133 
134  private:
135 #ifdef RDK_USE_BOOST_SERIALIZATION
136  friend class boost::serialization::access;
137 
138  template <class Archive>
139  void save(Archive &ar, const unsigned int /*version*/) const {
140  // invoke serialization of the base class
141  ar << boost::serialization::base_object<const EnumerationStrategyBase>(
142  *this);
143  ar << m_numPermutationsProcessed;
144 
145  std::stringstream random;
146  random << m_rng;
147  std::string s = random.str();
148  ar << s;
149 
150  ar << m_offset;
151  ar << m_maxoffset;
152  }
153 
154  template <class Archive>
155  void load(Archive &ar, const unsigned int /*version*/) {
156  // invoke serialization of the base class
157  ar >> boost::serialization::base_object<EnumerationStrategyBase>(*this);
158  ar >> m_numPermutationsProcessed;
159  std::string s;
160  ar >> s;
161  std::stringstream random(s);
162  random >> m_rng;
163  ar >> m_offset;
164  ar >> m_maxoffset;
165 
166  // reset the uniform distributions
167  m_distributions.clear();
168  for (size_t i = 0; i < m_permutationSizes.size(); ++i) {
169  m_distributions.push_back(boost::random::uniform_int_distribution<>(
170  0, m_permutationSizes[i] - 1));
171  }
172  }
173 
174  template <class Archive>
175  void serialize(Archive &ar, const unsigned int file_version) {
176  boost::serialization::split_member(ar, *this, file_version);
177  }
178 #endif
179 };
180 }
181 
182 #ifdef RDK_USE_BOOST_SERIALIZATION
183 BOOST_CLASS_VERSION(RDKit::RandomSampleAllBBsStrategy, 1)
184 #endif
185 
186 #endif
virtual boost::uint64_t getPermutationIdx() const
Returns how many permutations have been processed by this strategy.
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &)
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:116
EnumerationTypes::RGROUPS m_permutation
EnumerationStrategyBase * copy() const
copy the enumeration strategy complete with current state
std::vector< MOL_SPTR_VECT > BBS
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
virtual const EnumerationTypes::RGROUPS & next()
The current permutation {r1, r2, ...}.
std::vector< boost::uint64_t > RGROUPS
virtual const char * type() const
This is a class for randomly enumerating reagents that ensures all reagents.
EnumerationTypes::RGROUPS m_permutationSizes