Loading...
Searching...
No Matches
SubstructLibrarySerialization.h
Go to the documentation of this file.
// Copyright (c) 2019, 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.
//
// n.b. must be included at the END of SubstructLibrary.h
#ifndef RDK_SUBSTRUCT_LIBRARY_SERIALIZATION
#define RDK_SUBSTRUCT_LIBRARY_SERIALIZATION
#ifdef RDK_USE_BOOST_SERIALIZATION
#include <RDGeneral/BoostStartInclude.h>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/archive_exception.hpp>
#include <RDGeneral/BoostEndInclude.h>
BOOST_SERIALIZATION_ASSUME_ABSTRACT(RDKit::MolHolderBase)
BOOST_SERIALIZATION_ASSUME_ABSTRACT(RDKit::FPHolderBase)
namespace boost {
namespace serialization {
template <class Archive>
void serialize(Archive &, RDKit::MolHolderBase &, const unsigned int) {}
template <class Archive>
void save(Archive &ar, const RDKit::MolHolder &molholder,
const unsigned int version) {
ar &boost::serialization::base_object<RDKit::MolHolderBase>(molholder);
if (version < 2) {
std::int64_t pkl_count = molholder.getMols().size();
ar &pkl_count;
for (auto &mol : molholder.getMols()) {
std::string pkl;
RDKit::MolPickler::pickleMol(*mol.get(), pkl);
ar << pkl;
}
} else {
ar &molholder.getMols();
}
}
template <class Archive>
void load(Archive &ar, RDKit::MolHolder &molholder,
const unsigned int version) {
ar &boost::serialization::base_object<RDKit::MolHolderBase>(molholder);
std::vector<boost::shared_ptr<RDKit::ROMol>> &mols = molholder.getMols();
mols.clear();
if (version < 2) {
std::int64_t pkl_count = -1;
ar &pkl_count;
for (std::int64_t i = 0; i < pkl_count; ++i) {
std::string pkl;
ar >> pkl;
mols.push_back(boost::make_shared<RDKit::ROMol>(pkl));
}
} else {
ar &mols;
}
}
template <class Archive, class MolHolder>
void serialize_strings(Archive &ar, MolHolder &molholder,
const unsigned int version) {
RDUNUSED_PARAM(version);
ar &boost::serialization::base_object<RDKit::MolHolderBase>(molholder);
ar &molholder.getMols();
}
template <class Archive>
void serialize(Archive &ar, RDKit::CachedMolHolder &molholder,
const unsigned int version) {
serialize_strings(ar, molholder, version);
}
template <class Archive>
void serialize(Archive &ar, RDKit::CachedSmilesMolHolder &molholder,
const unsigned int version) {
serialize_strings(ar, molholder, version);
}
template <class Archive>
void serialize(Archive &ar, RDKit::CachedTrustedSmilesMolHolder &molholder,
const unsigned int version) {
serialize_strings(ar, molholder, version);
}
template <class Archive>
void save(Archive &ar, const RDKit::FPHolderBase &fpholder,
const unsigned int version) {
RDUNUSED_PARAM(version);
std::vector<std::string> pickles;
for (auto &fp : fpholder.getFingerprints()) {
pickles.push_back(fp->toString());
}
ar &pickles;
}
template <class Archive>
void load(Archive &ar, RDKit::FPHolderBase &fpholder,
const unsigned int version) {
RDUNUSED_PARAM(version);
std::vector<std::string> pickles;
std::vector<ExplicitBitVect *> &fps = fpholder.getFingerprints();
ar &pickles;
for (size_t i = 0; i < fps.size(); ++i) {
delete fps[i];
}
fps.clear();
for (auto &pkl : pickles) {
fps.push_back(new ExplicitBitVect(pkl));
}
}
template <class Archive>
void serialize(Archive &ar, RDKit::PatternHolder &pattern_holder,
const unsigned int version) {
RDUNUSED_PARAM(version);
ar &boost::serialization::base_object<RDKit::FPHolderBase>(pattern_holder);
if (Archive::is_saving::value &&
pattern_holder.getNumBits() != RDKit::PatternHolder::defaultNumBits()) {
ar &pattern_holder.getNumBits();
} else if (Archive::is_loading::value) {
try {
ar &pattern_holder.getNumBits();
} catch (boost::archive::archive_exception &) {
pattern_holder.getNumBits() = RDKit::PatternHolder::defaultNumBits();
}
}
}
template <class Archive>
void serialize(Archive &ar, RDKit::TautomerPatternHolder &pattern_holder,
const unsigned int version) {
RDUNUSED_PARAM(version);
ar &boost::serialization::base_object<RDKit::FPHolderBase>(pattern_holder);
ar &pattern_holder.getNumBits();
}
template <class Archive>
void serialize(Archive &, RDKit::KeyHolderBase &, const unsigned int) {}
template <class Archive>
void serialize(Archive &ar, RDKit::KeyFromPropHolder &key_holder,
const unsigned int) {
ar &boost::serialization::base_object<RDKit::KeyHolderBase>(key_holder);
ar &key_holder.getPropName();
ar &key_holder.getKeys();
}
template <class Archive>
void registerSubstructLibraryTypes(Archive &ar) {
ar.register_type(static_cast<RDKit::MolHolder *>(nullptr));
ar.register_type(static_cast<RDKit::CachedMolHolder *>(nullptr));
ar.register_type(static_cast<RDKit::CachedSmilesMolHolder *>(nullptr));
ar.register_type(static_cast<RDKit::CachedTrustedSmilesMolHolder *>(nullptr));
ar.register_type(static_cast<RDKit::PatternHolder *>(nullptr));
ar.register_type(static_cast<RDKit::TautomerPatternHolder *>(nullptr));
ar.register_type(static_cast<RDKit::KeyFromPropHolder *>(nullptr));
}
template <class Archive>
void save(Archive &ar, const RDKit::SubstructLibrary &slib,
const unsigned int version) {
RDUNUSED_PARAM(version);
registerSubstructLibraryTypes(ar);
ar &slib.getSearchOrder();
ar &slib.getKeyHolder();
ar &slib.getMolHolder();
ar &slib.getFpHolder();
}
template <class Archive>
void load(Archive &ar, RDKit::SubstructLibrary &slib,
const unsigned int version) {
RDUNUSED_PARAM(version);
registerSubstructLibraryTypes(ar);
if (version > 1) {
ar &slib.getSearchOrder();
ar &slib.getKeyHolder();
}
ar &slib.getMolHolder();
ar &slib.getFpHolder();
slib.resetHolders();
}
} // end namespace serialization
} // end namespace boost
BOOST_CLASS_VERSION(RDKit::MolHolder, 2);
BOOST_CLASS_VERSION(RDKit::CachedMolHolder, 1);
BOOST_CLASS_VERSION(RDKit::CachedSmilesMolHolder, 1);
BOOST_CLASS_VERSION(RDKit::CachedTrustedSmilesMolHolder, 1);
BOOST_CLASS_VERSION(RDKit::PatternHolder, 1);
BOOST_CLASS_VERSION(RDKit::TautomerPatternHolder, 1);
BOOST_CLASS_VERSION(RDKit::SubstructLibrary, 2);
BOOST_SERIALIZATION_SPLIT_FREE(RDKit::MolHolder);
BOOST_SERIALIZATION_SPLIT_FREE(RDKit::FPHolderBase);
BOOST_SERIALIZATION_SPLIT_FREE(RDKit::SubstructLibrary);
#endif
#endif
BoostEndInclude.h
BoostStartInclude.h
RDUNUSED_PARAM
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
ExplicitBitVect
a class for bit vectors that are densely occupied
Definition ExplicitBitVect.h:29
RDKit::CachedMolHolder
Concrete class that holds binary cached molecules in memory.
Definition SubstructLibrary.h:121
RDKit::CachedSmilesMolHolder
Concrete class that holds smiles strings in memory.
Definition SubstructLibrary.h:167
RDKit::CachedTrustedSmilesMolHolder
Concrete class that holds trusted smiles strings in memory.
Definition SubstructLibrary.h:218
RDKit::FPHolderBase
Base FPI for the fingerprinter used to rule out impossible matches.
Definition SubstructLibrary.h:258
RDKit::FPHolderBase::getFingerprints
std::vector< ExplicitBitVect * > & getFingerprints()
Definition SubstructLibrary.h:313
RDKit::KeyFromPropHolder
Definition SubstructLibrary.h:373
RDKit::KeyFromPropHolder::getKeys
std::vector< std::string > & getKeys()
Definition SubstructLibrary.h:385
RDKit::KeyFromPropHolder::getPropName
std::string & getPropName()
Definition SubstructLibrary.h:382
RDKit::KeyHolderBase
Definition SubstructLibrary.h:351
RDKit::MolHolderBase
Base class API for holding molecules to substructure search.
Definition SubstructLibrary.h:67
RDKit::MolHolder
Concrete class that holds molecules in memory.
Definition SubstructLibrary.h:87
RDKit::MolHolder::getMols
std::vector< boost::shared_ptr< ROMol > > & getMols()
Definition SubstructLibrary.h:109
RDKit::MolPickler::pickleMol
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
RDKit::PatternHolder
Definition SubstructLibrary.h:319
RDKit::PatternHolder::getNumBits
const unsigned int & getNumBits() const
Definition SubstructLibrary.h:329
RDKit::PatternHolder::defaultNumBits
static unsigned int defaultNumBits()
Definition SubstructLibrary.h:331
RDKit::SubstructLibrary
Substructure Search a library of molecules.
Definition SubstructLibrary.h:519
RDKit::SubstructLibrary::getMolHolder
boost::shared_ptr< MolHolderBase > & getMolHolder()
Get the underlying molecule holder implementation.
Definition SubstructLibrary.h:596
RDKit::SubstructLibrary::getFpHolder
boost::shared_ptr< FPHolderBase > & getFpHolder()
Get the underlying molecule holder implementation.
Definition SubstructLibrary.h:603
RDKit::SubstructLibrary::getKeyHolder
boost::shared_ptr< KeyHolderBase > & getKeyHolder()
Get the underlying molecule holder implementation.
Definition SubstructLibrary.h:611
RDKit::SubstructLibrary::getSearchOrder
const std::vector< unsigned int > & getSearchOrder() const
Definition SubstructLibrary.h:943
RDKit::SubstructLibrary::resetHolders
void resetHolders()
access required for serialization
Definition SubstructLibrary.h:949
RDKit::TautomerPatternHolder
Definition SubstructLibrary.h:338
boost
Definition RDLog.h:25