1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
//
// Copyright (c) 2015, Novartis Institutes for BioMedical Research Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Novartis Institutes for BioMedical Research Inc.
// nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include <RDGeneral/export.h>
#ifndef RDKIT_ENUMERATEBASE_H
#define RDKIT_ENUMERATEBASE_H
#include <vector>
#include "EnumerateTypes.h"
#include "../Reaction.h"
#include "EnumerationPickler.h"
#include "EnumerationStrategyBase.h"
#include "CartesianProduct.h"
#include "../ReactionPickler.h"
#include <GraphMol/MolPickler.h>
namespace RDKit {
//! Base class for enumerating chemical reactions from collections of
// building blocks and reagents.
/*!
basic usage:
\verbatim
EnumerateLibraryBase &enumerator;
while (enumerator) {
MOL_SPTR_VECT res = enumerator.next();
// do something with enumeration products here
}
\endverbatim
See Reaction.h for more details on how ChemicalReactions are
used.
*/
class RDKIT_CHEMREACTIONS_EXPORT EnumerateLibraryBase {
protected:
ChemicalReaction m_rxn;
boost::shared_ptr<EnumerationStrategyBase> m_enumerator;
boost::shared_ptr<EnumerationStrategyBase> m_initialEnumerator;
public:
//! default constructor
EnumerateLibraryBase() : m_rxn(), m_enumerator(), m_initialEnumerator() {}
//! construct with a chemical reaction and an enumeration strategy
EnumerateLibraryBase(const ChemicalReaction &rxn,
EnumerationStrategyBase *enumerator = 0)
: m_rxn(rxn),
m_enumerator(enumerator ? enumerator : new CartesianProductStrategy),
m_initialEnumerator(m_enumerator->copy()) {
m_rxn.initReactantMatchers();
}
//! Copy constructor
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
: m_rxn(rhs.m_rxn),
m_enumerator(rhs.m_enumerator ? rhs.m_enumerator->copy() : 0),
m_initialEnumerator(m_enumerator->copy()) {}
virtual ~EnumerateLibraryBase() {}
//! Are there any enumerations left?
virtual operator bool() const {
PRECONDITION(m_enumerator.get(), "Null enumeration strategy");
return static_cast<bool>(*m_enumerator);
}
//! reset the enumeration to the beginning.
void reset() {
if (m_initialEnumerator.get()) {
m_enumerator.reset(m_initialEnumerator->copy());
}
}
//! returns the underlying chemical reaction
const ChemicalReaction &getReaction() const { return m_rxn; }
//! return the current enumeration strategy
const EnumerationStrategyBase &getEnumerator() {
PRECONDITION(m_enumerator.get(), "Null Enumerator");
return *m_enumerator;
}
//! get the next set of products (See run_Reactants) for details
// This returns a vector of a vector of molecules.
// Each result vector corresponds for a product template.
// i.e.
// res = library.next();
// res[0] are the results for library.getReaction().getProdcts()[0]
virtual std::vector<MOL_SPTR_VECT> next() = 0;
//! get the next set of products as smiles
// This returns a vector of a vector strings.
// Each result vector corresponds for a product template.
virtual std::vector<std::vector<std::string> > nextSmiles();
//! Get the current position into the reagent vectors
// Use getState/setState to save/restart the enumeration
// from this position.
const EnumerationTypes::RGROUPS &getPosition() const;
//! Get the current state of the enumerator
// This is the position of the enumerator and the enumerators
// state that can be used to restart enumerating
// from this position.
std::string getState() const;
//! Set the current state of the enumerator
// Restart the enumerator from this position.
void setState(const std::string &);
//! Reset the enumerator to the beginning
void resetState();
//! serializes (pickles) to a stream
virtual void toStream(std::ostream &ss) const = 0;
//! returns a string with a serialized (pickled) representation
virtual std::string Serialize() const {
std::stringstream ss;
toStream(ss);
return ss.str();
}
//! initializes from a stream pickle
virtual void initFromStream(std::istream &ss) = 0;
//! initializes from a string pickle
virtual void initFromString(const std::string &text) {
std::stringstream ss(text);
initFromStream(ss);
}
private:
#ifdef RDK_USE_BOOST_SERIALIZATION
friend class boost::serialization::access;
template <class Archive>
void save(Archive &ar, const unsigned int) const {
std::string pickle;
ReactionPickler::pickleReaction(m_rxn, pickle);
ar &pickle;
ar &m_enumerator;
// we handle the m_initialEnumerator from a string
// for backwards compatibility with a unreleased
// version
EnumerationStrategyPickler::pickle(m_initialEnumerator, pickle);
ar &pickle;
}
template <class Archive>
void load(Archive &ar, const unsigned int /*version*/) {
// this should only be called on non-initialized reactions
if (m_rxn.getNumReactantTemplates() || m_rxn.getNumProductTemplates() ||
m_rxn.getNumAgentTemplates()) {
throw ValueErrorException("EnumerateBase already created from archive.");
}
std::string pickle;
ar &pickle;
ReactionPickler::reactionFromPickle(pickle, m_rxn);
ar &m_enumerator;
ar &pickle;
m_initialEnumerator = EnumerationStrategyPickler::fromPickle(pickle);
}
BOOST_SERIALIZATION_SPLIT_MEMBER();
#endif
};
#ifdef RDK_USE_BOOST_SERIALIZATION
BOOST_SERIALIZATION_ASSUME_ABSTRACT(EnumerateLibraryBase)
#endif
}
#endif
|