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 <iostream>
26#include <string>
27#include <sstream>
28#include <exception>
29#ifdef WIN32
30#include <ios>
31#endif
32#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 {
51typedef enum {
52 NoProps = 0, // no data pickled (default pickling, single-precision coords)
53 MolProps = 0x1, // only public non computed properties
54 AtomProps = 0x2,
55 BondProps = 0x4,
57 0x2, // n.b. DEPRECATED and set to AtomProps (does the same work)
60 AllProps = 0x0000FFFF, // all data pickled
61 CoordsAsDouble = 0x00010000, // save coordinates in double precision
63 0x00020000 // do not include conformers or associated properties
64} PropertyPickleOptions;
65} // namespace PicklerOps
66
67//! handles pickling (serializing) molecules
69 public:
70 static const std::int32_t versionMajor; //!< mark the pickle major version
71 static const std::int32_t versionMinor; //!< mark the pickle minor version
72 static const std::int32_t versionPatch; //!< mark the pickle patch version
73 static const std::int32_t endianId; //!< mark the endian-ness of the pickle
74
75 //! the pickle format is tagged using these tags:
76 //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
77 /// otherwise
78 //! you will break old pickles.
152
153 static unsigned int getDefaultPickleProperties();
154 static void setDefaultPickleProperties(unsigned int);
155
157 static void addCustomPropHandler(const CustomPropHandler &handler);
158
159 //! pickles a molecule and sends the results to stream \c ss
160 static void pickleMol(const ROMol *mol, std::ostream &ss);
161 static void pickleMol(const ROMol *mol, std::ostream &ss,
162 unsigned int propertyFlags);
163
164 static void pickleMol(const ROMol &mol, std::ostream &ss);
165
166 static void pickleMol(const ROMol &mol, std::ostream &ss,
167 unsigned int propertyFlags) {
168 MolPickler::pickleMol(&mol, ss, propertyFlags);
169 }
170
171 //! pickles a molecule and adds the results to string \c res
172 static void pickleMol(const ROMol *mol, std::string &res);
173 static void pickleMol(const ROMol *mol, std::string &res,
174 unsigned int propertyFlags);
175 static void pickleMol(const ROMol &mol, std::string &res);
176 static void pickleMol(const ROMol &mol, std::string &res,
177 unsigned int propertyFlags) {
178 MolPickler::pickleMol(&mol, res, propertyFlags);
179 }
180
181 //! constructs a molecule from a pickle stored in a string
182 static void molFromPickle(const std::string &pickle, ROMol *mol,
183 unsigned int propertyFlags);
184 static void molFromPickle(const std::string &pickle, ROMol &mol,
185 unsigned int propertyFlags) {
186 MolPickler::molFromPickle(pickle, &mol, propertyFlags);
187 }
188 static void molFromPickle(const std::string &pickle, ROMol *mol) {
189 MolPickler::molFromPickle(pickle, mol, PicklerOps::AllProps);
190 }
191 static void molFromPickle(const std::string &pickle, ROMol &mol) {
192 MolPickler::molFromPickle(pickle, &mol, PicklerOps::AllProps);
193 }
194
195 //! constructs a molecule from a pickle stored in a stream
196 static void molFromPickle(std::istream &ss, ROMol *mol,
197 unsigned int propertyFlags);
198 static void molFromPickle(std::istream &ss, ROMol &mol,
199 unsigned int propertyFlags) {
200 MolPickler::molFromPickle(ss, &mol, propertyFlags);
201 }
202 static void molFromPickle(std::istream &ss, ROMol *mol) {
203 MolPickler::molFromPickle(ss, mol, PicklerOps::AllProps);
204 }
205 static void molFromPickle(std::istream &ss, ROMol &mol) {
206 MolPickler::molFromPickle(ss, &mol, PicklerOps::AllProps);
207 }
208
209 private:
210 //! Pickle nonquery atom data
211 static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
212 //! depickle nonquery atom data
213 static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
214
215 static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
216
217 //! do the actual work of pickling a molecule
218 template <typename T>
219 static void _pickle(const ROMol *mol, std::ostream &ss,
220 unsigned int propertyFlags);
221
222 //! do the actual work of pickling an Atom
223 template <typename T>
224 static void _pickleAtom(std::ostream &ss, const Atom *atom);
225
226 //! do the actual work of pickling a Bond
227 template <typename T>
228 static void _pickleBond(std::ostream &ss, const Bond *bond,
229 std::map<int, int> &atomIdxMap);
230
231 //! do the actual work of pickling an SSSR structure
232 template <typename T>
233 static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
234 std::map<int, int> &atomIdxMap);
235
236 //! do the actual work of pickling a SubstanceGroup
237 template <typename T>
238 static void _pickleSubstanceGroup(std::ostream &ss,
239 const SubstanceGroup &sgroup,
240 std::map<int, int> &atomIdxMap,
241 std::map<int, int> &bondIdxMap);
242
243 //! do the actual work of pickling Stereo Group data
244 template <typename T>
245 static void _pickleStereo(std::ostream &ss, std::vector<StereoGroup> groups,
246 std::map<int, int> &atomIdxMap,
247 std::map<int, int> &bondIdxMap);
248
249 //! do the actual work of pickling a Conformer
250 template <typename T, typename C>
251 static void _pickleConformer(std::ostream &ss, const Conformer *conf);
252
253 //! do the actual work of de-pickling a molecule
254 template <typename T>
255 static void _depickle(std::istream &ss, ROMol *mol, int version, int numAtoms,
256 unsigned int propertyFlags);
257
258 //! extract atomic data from a pickle and add the resulting Atom to the
259 /// molecule
260 template <typename T>
261 static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
262 RDGeom::Point3D &pos, int version,
263 bool directMap = false);
264
265 //! extract bond data from a pickle and add the resulting Bond to the molecule
266 template <typename T>
267 static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
268 bool directMap = false);
269
270 //! extract ring info from a pickle and add the resulting RingInfo to the
271 /// molecule
272 template <typename T>
273 static void _addRingInfoFromPickle(
274 std::istream &ss, ROMol *mol, int version, bool directMap = false,
275 FIND_RING_TYPE ringType =
276 FIND_RING_TYPE::FIND_RING_TYPE_OTHER_OR_UNKNOWN);
277
278 //! extract a SubstanceGroup from a pickle
279 template <typename T>
280 static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss,
281 ROMol *mol, int version);
282
283 template <typename T>
284 static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
285
286 //! extract a conformation from a pickle
287 template <typename T, typename C>
288 static Conformer *_conformerFromPickle(std::istream &ss, int version);
289
290 //! pickle standard properties
291 static void _pickleProperties(std::ostream &ss, const RDProps &props,
292 unsigned int pickleFlags);
293 //! unpickle standard properties
294 static void _unpickleProperties(std::istream &ss, RDProps &props,
295 int version);
296
297 //! backwards compatibility
298 static void _pickleV1(const ROMol *mol, std::ostream &ss);
299 //! backwards compatibility
300 static void _depickleV1(std::istream &ss, ROMol *mol);
301 //! backwards compatibility
302 static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
303 //! backwards compatibility
304 static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
305};
306
307namespace PicklerOps {
308using QueryDetails = boost::variant<
309 MolPickler::Tags, std::tuple<MolPickler::Tags, int32_t>,
310 std::tuple<MolPickler::Tags, int32_t, int32_t>,
311 std::tuple<MolPickler::Tags, int32_t, int32_t, int32_t, char>,
312 std::tuple<MolPickler::Tags, std::set<int32_t>>>;
313template <class T>
315
316} // namespace PicklerOps
317
318}; // namespace RDKit
319
320#endif
Defines the Atom class and associated typedefs.
Pulls in all the query types.
Base class for all queries.
Definition Query.h:45
The class for representing atoms.
Definition Atom.h:75
class for representing a bond
Definition Bond.h:47
The class for representing 2D or 3D conformation of a molecule.
Definition Conformer.h:46
used to indicate exceptions whilst pickling (serializing) molecules
Definition MolPickler.h:39
~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:68
static const std::int32_t endianId
mark the endian-ness of the pickle
Definition MolPickler.h:73
static void molFromPickle(std::istream &ss, ROMol *mol)
Definition MolPickler.h:202
static void molFromPickle(const std::string &pickle, ROMol &mol, unsigned int propertyFlags)
Definition MolPickler.h:184
static void molFromPickle(std::istream &ss, ROMol &mol, unsigned int propertyFlags)
Definition MolPickler.h:198
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:188
@ ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
Definition MolPickler.h:134
static void pickleMol(const ROMol &mol, std::ostream &ss)
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition MolPickler.h:176
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:191
static void setDefaultPickleProperties(unsigned int)
static const std::int32_t versionMajor
mark the pickle major version
Definition MolPickler.h:70
static const std::int32_t versionMinor
mark the pickle minor version
Definition MolPickler.h:71
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition MolPickler.h:205
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition MolPickler.h:166
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:72
static void pickleMol(const ROMol &mol, std::string &res)
The class for representing SubstanceGroups.
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:233
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 > > > QueryDetails
Definition MolPickler.h:308
Std stuff.
FIND_RING_TYPE
A class to store information about a molecule's rings.
Definition RingInfo.h:31
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec
Definition StreamOps.h:382