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 {
157 // add new entries above here
159 } Tags;
160
161 static unsigned int getDefaultPickleProperties();
162 static void setDefaultPickleProperties(unsigned int);
163
165 static void addCustomPropHandler(const CustomPropHandler &handler);
166
167 //! pickles a molecule and sends the results to stream \c ss
168 static void pickleMol(const ROMol *mol, std::ostream &ss);
169 static void pickleMol(const ROMol *mol, std::ostream &ss,
170 unsigned int propertyFlags);
171
172 static void pickleMol(const ROMol &mol, std::ostream &ss);
173
174 static void pickleMol(const ROMol &mol, std::ostream &ss,
175 unsigned int propertyFlags) {
176 MolPickler::pickleMol(&mol, ss, propertyFlags);
177 }
178
179 //! pickles a molecule and adds the results to string \c res
180 static void pickleMol(const ROMol *mol, std::string &res);
181 static void pickleMol(const ROMol *mol, std::string &res,
182 unsigned int propertyFlags);
183 static void pickleMol(const ROMol &mol, std::string &res);
184 static void pickleMol(const ROMol &mol, std::string &res,
185 unsigned int propertyFlags) {
186 MolPickler::pickleMol(&mol, res, propertyFlags);
187 }
188
189 //! constructs a molecule from a pickle stored in a string
190 static void molFromPickle(const std::string &pickle, ROMol *mol,
191 unsigned int propertyFlags);
192 static void molFromPickle(const std::string &pickle, ROMol &mol,
193 unsigned int propertyFlags) {
194 MolPickler::molFromPickle(pickle, &mol, propertyFlags);
195 }
196 static void molFromPickle(const std::string &pickle, ROMol *mol) {
197 MolPickler::molFromPickle(pickle, mol,
198 PicklerOps::PropertyPickleOptions::AllProps);
199 }
200 static void molFromPickle(const std::string &pickle, ROMol &mol) {
201 MolPickler::molFromPickle(pickle, &mol,
202 PicklerOps::PropertyPickleOptions::AllProps);
203 }
204
205 //! constructs a molecule from a pickle stored in a stream
206 static void molFromPickle(std::istream &ss, ROMol *mol,
207 unsigned int propertyFlags);
208 static void molFromPickle(std::istream &ss, ROMol &mol,
209 unsigned int propertyFlags) {
210 MolPickler::molFromPickle(ss, &mol, propertyFlags);
211 }
212 static void molFromPickle(std::istream &ss, ROMol *mol) {
214 PicklerOps::PropertyPickleOptions::AllProps);
215 }
216 static void molFromPickle(std::istream &ss, ROMol &mol) {
218 PicklerOps::PropertyPickleOptions::AllProps);
219 }
220
221 private:
222 //! Pickle nonquery atom data
223 static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
224 //! depickle nonquery atom data
225 static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
226
227 static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
228
229 //! do the actual work of pickling a molecule
230 template <typename T>
231 static void _pickle(const ROMol *mol, std::ostream &ss,
232 unsigned int propertyFlags);
233
234 //! do the actual work of pickling an Atom
235 template <typename T>
236 static void _pickleAtom(std::ostream &ss, const Atom *atom);
237
238 //! do the actual work of pickling a Bond
239 template <typename T>
240 static void _pickleBond(std::ostream &ss, const Bond *bond,
241 std::map<int, int> &atomIdxMap);
242
243 //! do the actual work of pickling an SSSR structure
244 template <typename T>
245 static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
246 std::map<int, int> &atomIdxMap);
247
248 //! do the actual work of pickling a SubstanceGroup
249 template <typename T>
250 static void _pickleSubstanceGroup(std::ostream &ss,
251 const SubstanceGroup &sgroup,
252 std::map<int, int> &atomIdxMap,
253 std::map<int, int> &bondIdxMap);
254
255 //! do the actual work of pickling Stereo Group data
256 template <typename T>
257 static void _pickleStereo(std::ostream &ss, std::vector<StereoGroup> groups,
258 std::map<int, int> &atomIdxMap,
259 std::map<int, int> &bondIdxMap);
260
261 //! do the actual work of pickling a Conformer
262 template <typename T, typename C>
263 static void _pickleConformer(std::ostream &ss, const Conformer *conf);
264
265 //! do the actual work of de-pickling a molecule
266 template <typename T>
267 static void _depickle(std::istream &ss, ROMol *mol, int version, int numAtoms,
268 unsigned int propertyFlags);
269
270 //! extract atomic data from a pickle and add the resulting Atom to the
271 /// molecule
272 template <typename T>
273 static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
274 RDGeom::Point3D &pos, int version,
275 bool directMap = false);
276
277 //! extract bond data from a pickle and add the resulting Bond to the molecule
278 template <typename T>
279 static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
280 bool directMap = false);
281
282 //! extract ring info from a pickle and add the resulting RingInfo to the
283 /// molecule
284 template <typename T>
285 static void _addRingInfoFromPickle(
286 std::istream &ss, ROMol *mol, int version, bool directMap = false,
287 FIND_RING_TYPE ringType =
289
290 //! extract a SubstanceGroup from a pickle
291 template <typename T>
292 static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss,
293 ROMol *mol, int version);
294
295 template <typename T>
296 static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
297
298 //! extract a conformation from a pickle
299 template <typename T, typename C>
300 static Conformer *_conformerFromPickle(std::istream &ss, int version);
301
302 //! pickle standard properties
303 static void _pickleProperties(std::ostream &ss, const RDProps &props,
304 unsigned int pickleFlags);
305 //! unpickle standard properties
306 static void _unpickleProperties(std::istream &ss, RDProps &props,
307 int version);
308
309 //! backwards compatibility
310 static void _pickleV1(const ROMol *mol, std::ostream &ss);
311 //! backwards compatibility
312 static void _depickleV1(std::istream &ss, ROMol *mol);
313 //! backwards compatibility
314 static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
315 //! backwards compatibility
316 static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
317};
318
319namespace PicklerOps {
320// clang-format off
321using QueryDetails = boost::variant<
322 MolPickler::Tags, std::tuple<MolPickler::Tags, int32_t>,
323 std::tuple<MolPickler::Tags, int32_t, int32_t>,
324 std::tuple<MolPickler::Tags, int32_t, int32_t, int32_t, char>,
325 std::tuple<MolPickler::Tags, std::set<int32_t>>,
326 std::tuple<MolPickler::Tags, std::string>,
327 std::tuple<MolPickler::Tags, PairHolder, double>>;
328// clang-format on
329template <class T>
331
332} // namespace PicklerOps
333
334}; // namespace RDKit
335
336#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:48
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:212
static void molFromPickle(const std::string &pickle, ROMol &mol, unsigned int propertyFlags)
Definition MolPickler.h:192
static void molFromPickle(std::istream &ss, ROMol &mol, unsigned int propertyFlags)
Definition MolPickler.h:208
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:196
@ ATOM_MONOMER_INFO_RESIDUENUMBER
Definition MolPickler.h:153
@ 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:184
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:200
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:216
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition MolPickler.h:174
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:257
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:321
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