Loading...
Searching...
No Matches
EnumerateBase.h
Go to the documentation of this file.
//
// 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 = nullptr)
: 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() : nullptr),
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
} // namespace RDKit
#endif
CartesianProduct.h
EnumerateTypes.h
EnumerationPickler.h
EnumerationStrategyBase.h
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
MolPickler.h
RDKit::CartesianProductStrategy
Definition CartesianProduct.h:71
RDKit::ChemicalReaction
This is a class for storing and applying general chemical reactions.
Definition Reaction.h:121
RDKit::ChemicalReaction::getNumAgentTemplates
unsigned int getNumAgentTemplates() const
Definition Reaction.h:309
RDKit::ChemicalReaction::getNumReactantTemplates
unsigned int getNumReactantTemplates() const
Definition Reaction.h:303
RDKit::ChemicalReaction::initReactantMatchers
void initReactantMatchers(bool silent=false)
initializes our internal reactant-matching datastructures.
RDKit::ChemicalReaction::getNumProductTemplates
unsigned int getNumProductTemplates() const
Definition Reaction.h:306
RDKit::EnumerateLibraryBase
Definition EnumerateBase.h:63
RDKit::EnumerateLibraryBase::getState
std::string getState() const
RDKit::EnumerateLibraryBase::getEnumerator
const EnumerationStrategyBase & getEnumerator()
return the current enumeration strategy
Definition EnumerateBase.h:107
RDKit::EnumerateLibraryBase::initFromStream
virtual void initFromStream(std::istream &ss)=0
initializes from a stream pickle
RDKit::EnumerateLibraryBase::m_rxn
ChemicalReaction m_rxn
Definition EnumerateBase.h:65
RDKit::EnumerateLibraryBase::~EnumerateLibraryBase
virtual ~EnumerateLibraryBase()
Definition EnumerateBase.h:88
RDKit::EnumerateLibraryBase::Serialize
virtual std::string Serialize() const
returns a string with a serialized (pickled) representation
Definition EnumerateBase.h:147
RDKit::EnumerateLibraryBase::m_enumerator
boost::shared_ptr< EnumerationStrategyBase > m_enumerator
Definition EnumerateBase.h:66
RDKit::EnumerateLibraryBase::next
virtual std::vector< MOL_SPTR_VECT > next()=0
RDKit::EnumerateLibraryBase::EnumerateLibraryBase
EnumerateLibraryBase(const EnumerateLibraryBase &rhs)
Copy constructor.
Definition EnumerateBase.h:83
RDKit::EnumerateLibraryBase::EnumerateLibraryBase
EnumerateLibraryBase()
default constructor
Definition EnumerateBase.h:71
RDKit::EnumerateLibraryBase::getPosition
const EnumerationTypes::RGROUPS & getPosition() const
RDKit::EnumerateLibraryBase::nextSmiles
virtual std::vector< std::vector< std::string > > nextSmiles()
RDKit::EnumerateLibraryBase::initFromString
virtual void initFromString(const std::string &text)
initializes from a string pickle
Definition EnumerateBase.h:157
RDKit::EnumerateLibraryBase::toStream
virtual void toStream(std::ostream &ss) const =0
serializes (pickles) to a stream
RDKit::EnumerateLibraryBase::reset
void reset()
reset the enumeration to the beginning.
Definition EnumerateBase.h:97
RDKit::EnumerateLibraryBase::EnumerateLibraryBase
EnumerateLibraryBase(const ChemicalReaction &rxn, EnumerationStrategyBase *enumerator=nullptr)
construct with a chemical reaction and an enumeration strategy
Definition EnumerateBase.h:74
RDKit::EnumerateLibraryBase::setState
void setState(const std::string &)
RDKit::EnumerateLibraryBase::resetState
void resetState()
Reset the enumerator to the beginning.
RDKit::EnumerateLibraryBase::m_initialEnumerator
boost::shared_ptr< EnumerationStrategyBase > m_initialEnumerator
Definition EnumerateBase.h:67
RDKit::EnumerateLibraryBase::getReaction
const ChemicalReaction & getReaction() const
returns the underlying chemical reaction
Definition EnumerateBase.h:104
RDKit::EnumerationStrategyBase
Definition EnumerationStrategyBase.h:123
ValueErrorException
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition Exceptions.h:40
export.h
RDKIT_CHEMREACTIONS_EXPORT
#define RDKIT_CHEMREACTIONS_EXPORT
Definition export.h:49
RDKit::EnumerationStrategyPickler::pickle
RDKIT_CHEMREACTIONS_EXPORT void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
RDKit::EnumerationTypes::RGROUPS
std::vector< boost::uint64_t > RGROUPS
Definition EnumerateTypes.h:56
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::rdvalue_is
bool rdvalue_is(const RDValue_cast_t)
Definition RDValue-doublemagic.h:372