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