RDKit
Open-source cheminformatics and machine learning.
SubstructLibrarySerialization.h
Go to the documentation of this file.
1 // Copyright (c) 2019, Novartis Institutes for BioMedical Research Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following
12 // disclaimer in the documentation and/or other materials provided
13 // with the distribution.
14 // * Neither the name of Novartis Institutes for BioMedical Research Inc.
15 // nor the names of its contributors may be used to endorse or promote
16 // products derived from this software without specific prior written
17 // permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //
31 // n.b. must be included at the END of SubstructLibrary.h
32 #ifndef RDK_SUBSTRUCT_LIBRARY_SERIALIZATION
33 #define RDK_SUBSTRUCT_LIBRARY_SERIALIZATION
34 
35 #ifdef RDK_USE_BOOST_SERIALIZATION
37 #include <boost/archive/text_oarchive.hpp>
38 #include <boost/archive/text_iarchive.hpp>
39 #include <boost/serialization/vector.hpp>
40 #include <boost/serialization/shared_ptr.hpp>
42 
43 
44 
45 BOOST_SERIALIZATION_ASSUME_ABSTRACT(RDKit::MolHolderBase)
46 BOOST_SERIALIZATION_ASSUME_ABSTRACT(RDKit::FPHolderBase)
47 
48 namespace boost {
49 namespace serialization {
50 
51 template <class Archive>
52 void serialize(Archive &ar, RDKit::MolHolderBase &, const unsigned int version) {
53  RDUNUSED_PARAM(version);
54  RDUNUSED_PARAM(ar);
55 }
56 
57 template <class Archive>
58 void save(Archive &ar, const RDKit::MolHolder& molholder,
59  const unsigned int version) {
60  RDUNUSED_PARAM(version);
61  ar &boost::serialization::base_object<RDKit::MolHolderBase>(molholder);
62 
63  std::int64_t pkl_count = molholder.getMols().size();
64  ar & pkl_count;
65 
66  for(auto &mol: molholder.getMols()) {
67  std::string pkl;
68  RDKit::MolPickler::pickleMol(*mol.get(), pkl);
69  ar << pkl;
70  }
71 }
72 
73 template <class Archive>
74 void load(Archive &ar, RDKit::MolHolder& molholder,
75  const unsigned int version) {
76  RDUNUSED_PARAM(version);
77  ar &boost::serialization::base_object<RDKit::MolHolderBase>(molholder);
78 
79  std::vector<boost::shared_ptr<RDKit::ROMol>> &mols = molholder.getMols();
80  mols.clear();
81 
82  std::int64_t pkl_count = -1;
83  ar & pkl_count;
84 
85  for(std::int64_t i = 0; i<pkl_count; ++i) {
86  std::string pkl;
87  ar >> pkl;
88  mols.push_back(boost::make_shared<RDKit::ROMol>(pkl));
89  }
90 }
91 
92 template<class Archive, class MolHolder>
93 void serialize_strings(Archive &ar, MolHolder &molholder,
94  const unsigned int version) {
95  RDUNUSED_PARAM(version);
96  ar &boost::serialization::base_object<RDKit::MolHolderBase>(molholder);
97  ar &molholder.getMols();
98 }
99 
100 template <class Archive>
101 void serialize(Archive &ar, RDKit::CachedMolHolder &molholder,
102  const unsigned int version) {
103  serialize_strings(ar, molholder, version);
104 }
105 
106 template <class Archive>
107 void serialize(Archive &ar, RDKit::CachedSmilesMolHolder &molholder,
108  const unsigned int version) {
109  serialize_strings(ar, molholder, version);
110 }
111 
112 template <class Archive>
113 void serialize(Archive &ar, RDKit::CachedTrustedSmilesMolHolder &molholder,
114  const unsigned int version) {
115  serialize_strings(ar, molholder, version);
116 }
117 
118 template <class Archive>
119 void save(Archive &ar, const RDKit::FPHolderBase &fpholder,
120  const unsigned int version) {
121  RDUNUSED_PARAM(version);
122  std::vector<std::string> pickles;
123  for(auto &fp: fpholder.getFingerprints()) {
124  pickles.push_back(fp->toString());
125  }
126  ar &pickles;
127 }
128 
129 template <class Archive>
130 void load(Archive &ar, RDKit::FPHolderBase &fpholder,
131  const unsigned int version) {
132  RDUNUSED_PARAM(version);
133  std::vector<std::string> pickles;
134  std::vector<ExplicitBitVect *> &fps = fpholder.getFingerprints();
135 
136  ar &pickles;
137  for (size_t i = 0; i < fps.size(); ++i) delete fps[i];
138  fps.clear();
139 
140  for(auto &pkl: pickles) {
141  fps.push_back( new ExplicitBitVect(pkl) );
142  }
143 }
144 
145 template <class Archive>
146 void serialize(Archive &ar, RDKit::PatternHolder &pattern_holder,
147  const unsigned int version) {
148  RDUNUSED_PARAM(version);
149  ar &boost::serialization::base_object<RDKit::FPHolderBase>(pattern_holder);
150 }
151 
152 template <class Archive>
153 void registerSubstructLibraryTypes(Archive &ar) {
154  ar.register_type(static_cast<RDKit::MolHolder *>(NULL));
155  ar.register_type(static_cast<RDKit::CachedMolHolder *>(NULL));
156  ar.register_type(static_cast<RDKit::CachedSmilesMolHolder *>(NULL));
157  ar.register_type(static_cast<RDKit::CachedTrustedSmilesMolHolder *>(NULL));
158  ar.register_type(static_cast<RDKit::PatternHolder *>(NULL));
159 }
160 
161 
162 template <class Archive>
163 void save(Archive &ar, const RDKit::SubstructLibrary &slib,
164  const unsigned int version) {
165  RDUNUSED_PARAM(version);
166  registerSubstructLibraryTypes(ar);
167  ar & slib.getMolHolder();
168  ar & slib.getFpHolder();
169 }
170 
171 template <class Archive>
172 void load(Archive &ar, RDKit::SubstructLibrary &slib,
173  const unsigned int version) {
174  RDUNUSED_PARAM(version);
175  registerSubstructLibraryTypes(ar);
176  ar & slib.getMolHolder();
177  ar & slib.getFpHolder();
178  slib.resetHolders();
179 }
180 
181 } // end namespace serialization
182 } // end namespace boost
183 
184 BOOST_CLASS_VERSION(RDKit::MolHolder, 1);
185 BOOST_CLASS_VERSION(RDKit::CachedMolHolder, 1);
186 BOOST_CLASS_VERSION(RDKit::CachedSmilesMolHolder, 1);
187 BOOST_CLASS_VERSION(RDKit::CachedTrustedSmilesMolHolder, 1);
188 BOOST_CLASS_VERSION(RDKit::PatternHolder, 1);
189 BOOST_CLASS_VERSION(RDKit::SubstructLibrary, 1);
190 
191 BOOST_SERIALIZATION_SPLIT_FREE(RDKit::MolHolder);
192 BOOST_SERIALIZATION_SPLIT_FREE(RDKit::FPHolderBase);
193 BOOST_SERIALIZATION_SPLIT_FREE(RDKit::SubstructLibrary);
194 
195 #endif
196 #endif
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
Definition: RDLog.h:21
Concrete class that holds molecules in memory.
boost::shared_ptr< MolHolderBase > & getMolHolder()
Get the underlying molecule holder implementation.
Concrete class that holds trusted smiles strings in memory.
Base FPI for the fingerprinter used to rule out impossible matches.
boost::shared_ptr< FPHolderBase > & getFpHolder()
Get the underlying molecule holder implementation.
Base class API for holding molecules to substructure search.
std::vector< ExplicitBitVect * > & getFingerprints()
#define RDUNUSED_PARAM(x)
Definition: Invariant.h:195
std::vector< boost::shared_ptr< ROMol > > & getMols()
Concrete class that holds binary cached molecules in memory.
Uses the pattern fingerprinter to rule out matches.
a class for bit vectors that are densely occupied
void resetHolders()
access required for serialization
Concrete class that holds smiles strings in memory.
Substructure Search a library of molecules.