RDKit: RandomSampleAllBBs.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
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
37#include "EnumerationStrategyBase.h"
38#include <boost/random.hpp>
39#include <boost/random/uniform_int_distribution.hpp>
40#include <sstream>
41
42namespace 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
68 class RDKIT_CHEMREACTIONS_EXPORT RandomSampleAllBBsStrategy
69 : public EnumerationStrategyBase {
70 boost::uint64_t m_numPermutationsProcessed{0};
71 size_t m_offset{0};
72 size_t m_maxoffset{0};
73
74 boost::minstd_rand m_rng;
75 std::vector<boost::random::uniform_int_distribution<>> m_distributions;
76
77 public:
78 RandomSampleAllBBsStrategy()
79 : EnumerationStrategyBase(),
80
81 m_rng(),
82 m_distributions() {
83 for (size_t i = 0; i < m_permutation.size(); ++i) {
84 m_distributions.emplace_back(0, m_permutation[i] - 1);
85 }
86 }
87 using EnumerationStrategyBase::initialize;
88
89 void initializeStrategy(const ChemicalReaction &,
90 const EnumerationTypes::BBS &) override {
91 m_distributions.clear();
92 m_permutation.resize(m_permutationSizes.size());
93 m_offset = 0;
94 m_maxoffset =
95 *std::max_element(m_permutationSizes.begin(), m_permutationSizes.end());
96 for (size_t i = 0; i < m_permutationSizes.size(); ++i) {
97 m_distributions.emplace_back(0, m_permutationSizes[i] - 1);
98 }
99
100 m_numPermutationsProcessed = 0;
101 }
102
103 const char *type() const override { return "RandomSampleAllBBsStrategy"; }
104
105 //! The current permutation {r1, r2, ...}
106 const EnumerationTypes::RGROUPS &next() override {
107 if (m_offset >= m_maxoffset) {
108 for (size_t i = 0; i < m_permutation.size(); ++i) {
109 m_permutation[i] = m_distributions[i](m_rng);
110 }
111 m_offset = 0;
112 } else {
113 for (size_t i = 0; i < m_permutation.size(); ++i) {
114 m_permutation[i] = (m_permutation[i] + 1) % m_permutationSizes[i];
115 }
116 ++m_offset;
117 }
118 ++m_numPermutationsProcessed;
119
120 return m_permutation;
121 }
122
123 boost::uint64_t getPermutationIdx() const override {
124 return m_numPermutationsProcessed;
125 }
126
127 operator bool() const override { return true; }
128
129 EnumerationStrategyBase *copy() const override {
130 return new RandomSampleAllBBsStrategy(*this);
131 }
132
133 private:
134#ifdef RDK_USE_BOOST_SERIALIZATION
135 friend class boost::serialization::access;
136
137 template <class Archive>
138 void save(Archive &ar, const unsigned int /*version*/) const {
139 // invoke serialization of the base class
140 ar << boost::serialization::base_object<const EnumerationStrategyBase>(
141 *this);
142 ar << m_numPermutationsProcessed;
143
144 std::stringstream random;
145 random << m_rng;
146 std::string s = random.str();
147 ar << s;
148
149 ar << m_offset;
150 ar << m_maxoffset;
151 }
152
153 template <class Archive>
154 void load(Archive &ar, const unsigned int /*version*/) {
155 // invoke serialization of the base class
156 ar >> boost::serialization::base_object<EnumerationStrategyBase>(*this);
157 ar >> m_numPermutationsProcessed;
158 std::string s;
159 ar >> s;
160 std::stringstream random(s);
161 random >> m_rng;
162 ar >> m_offset;
163 ar >> m_maxoffset;
164
165 // reset the uniform distributions
166 m_distributions.clear();
167 for (size_t i = 0; i < m_permutationSizes.size(); ++i) {
168 m_distributions.emplace_back(0, m_permutationSizes[i] - 1);
169 }
170 }
171
172 template <class Archive>
173 void serialize(Archive &ar, const unsigned int file_version) {
174 boost::serialization::split_member(ar, *this, file_version);
175 }
176#endif
177};
178} // namespace RDKit
179
180#ifdef RDK_USE_BOOST_SERIALIZATION
181BOOST_CLASS_VERSION(RDKit::RandomSampleAllBBsStrategy, 1)
182#endif
183
184#endif
EnumerationStrategyBase.h
RDKit::ChemicalReaction
This is a class for storing and applying general chemical reactions.
Definition Reaction.h:121
RDKit::EnumerationStrategyBase::initialize
void initialize(const ChemicalReaction &reaction, const EnumerationTypes::BBS &building_blocks)
Definition EnumerationStrategyBase.h:145
RDKit::EnumerationStrategyBase::m_permutation
EnumerationTypes::RGROUPS m_permutation
Definition EnumerationStrategyBase.h:125
RDKit::EnumerationStrategyBase::m_permutationSizes
EnumerationTypes::RGROUPS m_permutationSizes
Definition EnumerationStrategyBase.h:127
RDKit::EnumerationStrategyBase::EnumerationStrategyBase
EnumerationStrategyBase()
Definition EnumerationStrategyBase.h:134
RDKit::RandomSampleAllBBsStrategy
Definition RandomSampleAllBBs.h:69
RDKit::RandomSampleAllBBsStrategy::type
const char * type() const override
Definition RandomSampleAllBBs.h:103
RDKit::RandomSampleAllBBsStrategy::next
const EnumerationTypes::RGROUPS & next() override
The current permutation {r1, r2, ...}.
Definition RandomSampleAllBBs.h:106
RDKit::RandomSampleAllBBsStrategy::getPermutationIdx
boost::uint64_t getPermutationIdx() const override
Returns how many permutations have been processed by this strategy.
Definition RandomSampleAllBBs.h:123
RDKit::RandomSampleAllBBsStrategy::copy
EnumerationStrategyBase * copy() const override
copy the enumeration strategy complete with current state
Definition RandomSampleAllBBs.h:129
RDKit::RandomSampleAllBBsStrategy::RandomSampleAllBBsStrategy
RandomSampleAllBBsStrategy()
Definition RandomSampleAllBBs.h:78
RDKit::RandomSampleAllBBsStrategy::initializeStrategy
void initializeStrategy(const ChemicalReaction &, const EnumerationTypes::BBS &) override
Definition RandomSampleAllBBs.h:89
export.h
RDKIT_CHEMREACTIONS_EXPORT
#define RDKIT_CHEMREACTIONS_EXPORT
Definition export.h:57
RDKit::EnumerationTypes::RGROUPS
std::vector< boost::uint64_t > RGROUPS
Definition EnumerateTypes.h:56
RDKit::EnumerationTypes::BBS
std::vector< MOL_SPTR_VECT > BBS
Definition EnumerateTypes.h:42
RDKit::MolStandardize::Operations::serialize
RDKIT_MOLSTANDARDIZE_EXPORT void serialize(RWMOL_SPTR_PAIR output, PipelineResult &result, const PipelineOptions &options)
RDKit
Std stuff.
Definition Abbreviations.h:19