RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MolPickler.h
Go to the documentation of this file.
1///
2// Copyright (C) 2001-2021 Greg Landrum and Rational Discovery LLC
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10#include <RDGeneral/export.h>
11#ifndef RD_MOLPICKLE_H
12#define RD_MOLPICKLE_H
13
14#include <Geometry/point.h>
15#include <GraphMol/Atom.h>
16#include <GraphMol/QueryAtom.h>
17#include <GraphMol/Bond.h>
18#include <GraphMol/QueryBond.h>
19#include <RDGeneral/StreamOps.h>
20#include <boost/utility/binary.hpp>
21#include <boost/variant.hpp>
22#include <Query/QueryObjects.h>
23
24// Std stuff
25#include <string>
26#include <sstream>
27#include <exception>
28#ifdef WIN32
29#include <ios>
30#endif
31#include <cstdint>
33
34namespace RDKit {
35class ROMol;
36class RingInfo;
37
38//! used to indicate exceptions whilst pickling (serializing) molecules
39class RDKIT_GRAPHMOL_EXPORT MolPicklerException : public std::exception {
40 public:
41 MolPicklerException(const char *msg) : _msg(msg) {}
42 MolPicklerException(const std::string msg) : _msg(msg) {}
43 const char *what() const noexcept override { return _msg.c_str(); }
44 ~MolPicklerException() noexcept override = default;
45
46 private:
47 std::string _msg;
48};
49
50namespace PicklerOps {
52 PropertyPickleOptions, unsigned int,
53 NoProps = 0, // no data pickled (default pickling, single-precision coords)
54 MolProps = 0x1, // only public non computed properties
55 AtomProps = 0x2, BondProps = 0x4,
56 QueryAtomData =
57 0x2, // n.b. DEPRECATED and set to AtomProps (does the same work)
58 PrivateProps = 0x10, ComputedProps = 0x20,
59 AllProps = 0x0000FFFF, // all data pickled
60 CoordsAsDouble = 0x00010000, // save coordinates in double precision
61 NoConformers =
62 0x00020000 // do not include conformers or associated properties
63);
64} // namespace PicklerOps
65
66//! handles pickling (serializing) molecules
68 public:
69 static const std::int32_t versionMajor; //!< mark the pickle major version
70 static const std::int32_t versionMinor; //!< mark the pickle minor version
71 static const std::int32_t versionPatch; //!< mark the pickle patch version
72 static const std::int32_t endianId; //!< mark the endian-ness of the pickle
73
74 //! the pickle format is tagged using these tags:
75 //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
76 /// otherwise
77 //! you will break old pickles.
78 typedef enum {
150 // add new entries above here
152 } Tags;
153
154 static unsigned int getDefaultPickleProperties();
155 static void setDefaultPickleProperties(unsigned int);
156
158 static void addCustomPropHandler(const CustomPropHandler &handler);
159
160 //! pickles a molecule and sends the results to stream \c ss
161 static void pickleMol(const ROMol *mol, std::ostream &ss);
162 static void pickleMol(const ROMol *mol, std::ostream &ss,
163 unsigned int propertyFlags);
164
165 static void pickleMol(const ROMol &mol, std::ostream &ss);
166
167 static void pickleMol(const ROMol &mol, std::ostream &ss,
168 unsigned int propertyFlags) {
169 MolPickler::pickleMol(&mol, ss, propertyFlags);
170 }
171
172 //! pickles a molecule and adds the results to string \c res
173 static void pickleMol(const ROMol *mol, std::string &res);
174 static void pickleMol(const ROMol *mol, std::string &res,
175 unsigned int propertyFlags);
176 static void pickleMol(const ROMol &mol, std::string &res);
177 static void pickleMol(const ROMol &mol, std::string &res,
178 unsigned int propertyFlags) {
179 MolPickler::pickleMol(&mol, res, propertyFlags);
180 }
181
182 //! constructs a molecule from a pickle stored in a string
183 static void molFromPickle(const std::string &pickle, ROMol *mol,
184 unsigned int propertyFlags);
185 static void molFromPickle(const std::string &pickle, ROMol &mol,
186 unsigned int propertyFlags) {
187 MolPickler::molFromPickle(pickle, &mol, propertyFlags);
188 }
189 static void molFromPickle(const std::string &pickle, ROMol *mol) {
190 MolPickler::molFromPickle(pickle, mol,
191 PicklerOps::PropertyPickleOptions::AllProps);
192 }
193 static void molFromPickle(const std::string &pickle, ROMol &mol) {
194 MolPickler::molFromPickle(pickle, &mol,
195 PicklerOps::PropertyPickleOptions::AllProps);
196 }
197
198 //! constructs a molecule from a pickle stored in a stream
199 static void molFromPickle(std::istream &ss, ROMol *mol,
200 unsigned int propertyFlags);
201 static void molFromPickle(std::istream &ss, ROMol &mol,
202 unsigned int propertyFlags) {
203 MolPickler::molFromPickle(ss, &mol, propertyFlags);
204 }
205 static void molFromPickle(std::istream &ss, ROMol *mol) {
207 PicklerOps::PropertyPickleOptions::AllProps);
208 }
209 static void molFromPickle(std::istream &ss, ROMol &mol) {
211 PicklerOps::PropertyPickleOptions::AllProps);
212 }
213
214 private:
215 //! Pickle nonquery atom data
216 static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
217 //! depickle nonquery atom data
218 static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
219
220 static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
221
222 //! do the actual work of pickling a molecule
223 template <typename T>
224 static void _pickle(const ROMol *mol, std::ostream &ss,
225 unsigned int propertyFlags);
226
227 //! do the actual work of pickling an Atom
228 template <typename T>
229 static void _pickleAtom(std::ostream &ss, const Atom *atom);
230
231 //! do the actual work of pickling a Bond
232 template <typename T>
233 static void _pickleBond(std::ostream &ss, const Bond *bond,
234 std::map<int, int> &atomIdxMap);
235
236 //! do the actual work of pickling an SSSR structure
237 template <typename T>
238 static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
239 std::map<int, int> &atomIdxMap);
240
241 //! do the actual work of pickling a SubstanceGroup
242 template <typename T>
243 static void _pickleSubstanceGroup(std::ostream &ss,
244 const SubstanceGroup &sgroup,
245 std::map<int, int> &atomIdxMap,
246 std::map<int, int> &bondIdxMap);
247
248 //! do the actual work of pickling Stereo Group data
249 template <typename T>
250 static void _pickleStereo(std::ostream &ss, std::vector<StereoGroup> groups,
251 std::map<int, int> &atomIdxMap,
252 std::map<int, int> &bondIdxMap);
253
254 //! do the actual work of pickling a Conformer
255 template <typename T, typename C>
256 static void _pickleConformer(std::ostream &ss, const Conformer *conf);
257
258 //! do the actual work of de-pickling a molecule
259 template <typename T>
260 static void _depickle(std::istream &ss, ROMol *mol, int version, int numAtoms,
261 unsigned int propertyFlags);
262
263 //! extract atomic data from a pickle and add the resulting Atom to the
264 /// molecule
265 template <typename T>
266 static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
267 RDGeom::Point3D &pos, int version,
268 bool directMap = false);
269
270 //! extract bond data from a pickle and add the resulting Bond to the molecule
271 template <typename T>
272 static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
273 bool directMap = false);
274
275 //! extract ring info from a pickle and add the resulting RingInfo to the
276 /// molecule
277 template <typename T>
278 static void _addRingInfoFromPickle(
279 std::istream &ss, ROMol *mol, int version, bool directMap = false,
280 FIND_RING_TYPE ringType =
282
283 //! extract a SubstanceGroup from a pickle
284 template <typename T>
285 static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss,
286 ROMol *mol, int version);
287
288 template <typename T>
289 static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
290
291 //! extract a conformation from a pickle
292 template <typename T, typename C>
293 static Conformer *_conformerFromPickle(std::istream &ss, int version);
294
295 //! pickle standard properties
296 static void _pickleProperties(std::ostream &ss, const RDProps &props,
297 unsigned int pickleFlags);
298 //! unpickle standard properties
299 static void _unpickleProperties(std::istream &ss, RDProps &props,
300 int version);
301
302 //! backwards compatibility
303 static void _pickleV1(const ROMol *mol, std::ostream &ss);
304 //! backwards compatibility
305 static void _depickleV1(std::istream &ss, ROMol *mol);
306 //! backwards compatibility
307 static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
308 //! backwards compatibility
309 static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
310};
311
312namespace PicklerOps {
313// clang-format off
314using QueryDetails = boost::variant<
315 MolPickler::Tags, std::tuple<MolPickler::Tags, int32_t>,
316 std::tuple<MolPickler::Tags, int32_t, int32_t>,
317 std::tuple<MolPickler::Tags, int32_t, int32_t, int32_t, char>,
318 std::tuple<MolPickler::Tags, std::set<int32_t>>,
319 std::tuple<MolPickler::Tags, std::string>,
320 std::tuple<MolPickler::Tags, PairHolder, double>>;
321// clang-format on
322template <class T>
324
325} // namespace PicklerOps
326
327}; // namespace RDKit
328
329#endif
Defines the Atom class and associated typedefs.
#define BETTER_ENUM(Enum, Underlying,...)
Definition BetterEnums.h:17
Pulls in all the query types.
Base class for all queries.
Definition Query.h:45
The class for representing atoms.
Definition Atom.h:74
class for representing a bond
Definition Bond.h:46
The class for representing 2D or 3D conformation of a molecule.
Definition Conformer.h:46
~MolPicklerException() noexcept override=default
const char * what() const noexcept override
Definition MolPickler.h:43
MolPicklerException(const std::string msg)
Definition MolPickler.h:42
MolPicklerException(const char *msg)
Definition MolPickler.h:41
handles pickling (serializing) molecules
Definition MolPickler.h:67
static const std::int32_t endianId
mark the endian-ness of the pickle
Definition MolPickler.h:72
static void molFromPickle(std::istream &ss, ROMol *mol)
Definition MolPickler.h:205
static void molFromPickle(const std::string &pickle, ROMol &mol, unsigned int propertyFlags)
Definition MolPickler.h:185
static void molFromPickle(std::istream &ss, ROMol &mol, unsigned int propertyFlags)
Definition MolPickler.h:201
static void pickleMol(const ROMol *mol, std::string &res, unsigned int propertyFlags)
static void molFromPickle(std::istream &ss, ROMol *mol, unsigned int propertyFlags)
constructs a molecule from a pickle stored in a stream
static void molFromPickle(const std::string &pickle, ROMol *mol)
Definition MolPickler.h:189
@ ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
Definition MolPickler.h:133
static void pickleMol(const ROMol &mol, std::ostream &ss)
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition MolPickler.h:177
static void pickleMol(const ROMol *mol, std::ostream &ss, unsigned int propertyFlags)
static void molFromPickle(const std::string &pickle, ROMol *mol, unsigned int propertyFlags)
constructs a molecule from a pickle stored in a string
static const CustomPropHandlerVec & getCustomPropHandlers()
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition MolPickler.h:193
static void setDefaultPickleProperties(unsigned int)
static const std::int32_t versionMajor
mark the pickle major version
Definition MolPickler.h:69
static const std::int32_t versionMinor
mark the pickle minor version
Definition MolPickler.h:70
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition MolPickler.h:209
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition MolPickler.h:167
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
static void pickleMol(const ROMol *mol, std::string &res)
pickles a molecule and adds the results to string res
static unsigned int getDefaultPickleProperties()
static void addCustomPropHandler(const CustomPropHandler &handler)
static const std::int32_t versionPatch
mark the pickle patch version
Definition MolPickler.h:71
static void pickleMol(const ROMol &mol, std::string &res)
The class for representing SubstanceGroups.
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:249
QueryDetails getQueryDetails(const Queries::Query< int, T const *, true > *query)
boost::variant< MolPickler::Tags, std::tuple< MolPickler::Tags, int32_t >, std::tuple< MolPickler::Tags, int32_t, int32_t >, std::tuple< MolPickler::Tags, int32_t, int32_t, int32_t, char >, std::tuple< MolPickler::Tags, std::set< int32_t > >, std::tuple< MolPickler::Tags, std::string >, std::tuple< MolPickler::Tags, PairHolder, double > > QueryDetails
Definition MolPickler.h:314
Std stuff.
FIND_RING_TYPE
A class to store information about a molecule's rings.
Definition RingInfo.h:31
@ FIND_RING_TYPE_OTHER_OR_UNKNOWN
Definition RingInfo.h:35
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec
Definition StreamOps.h:387