RDKit
Open-source cheminformatics and machine learning.
MolPickler.h
Go to the documentation of this file.
1 ///
2 // Copyright (C) 2001-2008 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 // Std stuff
22 #include <iostream>
23 #include <string>
24 #include <sstream>
25 #include <exception>
26 #ifdef WIN32
27 #include <ios>
28 #endif
29 #include <cstdint>
30 
31 namespace RDKit {
32 class ROMol;
33 class RingInfo;
34 
35 //! used to indicate exceptions whilst pickling (serializing) molecules
36 class RDKIT_GRAPHMOL_EXPORT MolPicklerException : public std::exception {
37  public:
38  MolPicklerException(const char *msg) : _msg(msg){};
39  MolPicklerException(const std::string msg) : _msg(msg){};
40  const char *what() const noexcept override { return _msg.c_str(); };
41  ~MolPicklerException() noexcept {};
42 
43  private:
44  std::string _msg;
45 };
46 
47 namespace PicklerOps {
48 typedef enum {
49  NoProps = 0, // no data pickled (default pickling, single-precision coords)
50  MolProps = BOOST_BINARY(1), // only public non computed properties
51  AtomProps = BOOST_BINARY(10),
52  BondProps = BOOST_BINARY(100),
53  QueryAtomData = BOOST_BINARY(
54  10), // n.b. DEPRECATED and set to AtomProps (does the same work)
55  PrivateProps = BOOST_BINARY(10000),
56  ComputedProps = BOOST_BINARY(100000),
57  AllProps = 0x0000FFFF, // all data pickled
58  CoordsAsDouble = 0x0001FFFF // save coordinates in double precision
60 }
61 
62 //! handles pickling (serializing) molecules
64  public:
65  static const std::int32_t versionMajor; //!< mark the pickle major version
66  static const std::int32_t versionMinor; //!< mark the pickle minor version
67  static const std::int32_t versionPatch; //!< mark the pickle patch version
68  static const std::int32_t endianId; //! mark the endian-ness of the pickle
69 
70  //! the pickle format is tagged using these tags:
71  //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
72  // otherwise
73  //! you will break old pickles.
74  typedef enum {
75  VERSION = 0,
141  // add new entries above here
142  INVALID_TAG = 255
143  } Tags;
144 
145  static unsigned int getDefaultPickleProperties();
146  static void setDefaultPickleProperties(unsigned int);
147 
149  static void addCustomPropHandler(const CustomPropHandler &handler);
150 
151  //! pickles a molecule and sends the results to stream \c ss
152  static void pickleMol(const ROMol *mol, std::ostream &ss);
153  static void pickleMol(const ROMol *mol, std::ostream &ss,
154  unsigned int propertyFlags);
155 
156  static void pickleMol(const ROMol &mol, std::ostream &ss);
157 
158  static void pickleMol(const ROMol &mol, std::ostream &ss,
159  unsigned int propertyFlags) {
160  MolPickler::pickleMol(&mol, ss, propertyFlags);
161  };
162 
163  //! pickles a molecule and adds the results to string \c res
164  static void pickleMol(const ROMol *mol, std::string &res);
165  static void pickleMol(const ROMol *mol, std::string &res,
166  unsigned int propertyFlags);
167  static void pickleMol(const ROMol &mol, std::string &res);
168  static void pickleMol(const ROMol &mol, std::string &res,
169  unsigned int propertyFlags) {
170  MolPickler::pickleMol(&mol, res, propertyFlags);
171  };
172 
173  //! constructs a molecule from a pickle stored in a string
174  static void molFromPickle(const std::string &pickle, ROMol *mol);
175  static void molFromPickle(const std::string &pickle, ROMol &mol) {
177  };
178 
179  //! constructs a molecule from a pickle stored in a stream
180  static void molFromPickle(std::istream &ss, ROMol *mol);
181  static void molFromPickle(std::istream &ss, ROMol &mol) {
182  MolPickler::molFromPickle(ss, &mol);
183  };
184 
185  private:
186  //! Pickle nonquery atom data
187  static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
188  //! depickle nonquery atom data
189  static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
190 
191  static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
192 
193  //! do the actual work of pickling a molecule
194  template <typename T>
195  static void _pickle(const ROMol *mol, std::ostream &ss,
196  unsigned int propertyFlags);
197 
198  //! do the actual work of pickling an Atom
199  template <typename T>
200  static void _pickleAtom(std::ostream &ss, const Atom *atom);
201 
202  //! do the actual work of pickling a Bond
203  template <typename T>
204  static void _pickleBond(std::ostream &ss, const Bond *bond,
205  std::map<int, int> &atomIdxMap);
206 
207  //! do the actual work of pickling an SSSR structure
208  template <typename T>
209  static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
210  std::map<int, int> &atomIdxMap);
211 
212  //! do the actual work of pickling a SubstanceGroup
213  template <typename T>
214  static void _pickleSubstanceGroup(std::ostream &ss,
215  const SubstanceGroup &sgroup,
216  std::map<int, int> &atomIdxMap,
217  std::map<int, int> &bondIdxMap);
218 
219  //! do the actual work of pickling Stereo Group data
220  template <typename T>
221  static void _pickleStereo(std::ostream &ss,
222  const std::vector<StereoGroup> &groups,
223  std::map<int, int> &atomIdxMap);
224 
225  //! do the actual work of pickling a Conformer
226  template <typename T, typename C>
227  static void _pickleConformer(std::ostream &ss, const Conformer *conf);
228 
229  //! do the actual work of de-pickling a molecule
230  template <typename T>
231  static void _depickle(std::istream &ss, ROMol *mol, int version,
232  int numAtoms);
233 
234  //! extract atomic data from a pickle and add the resulting Atom to the
235  // molecule
236  template <typename T>
237  static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
238  RDGeom::Point3D &pos, int version,
239  bool directMap = false);
240 
241  //! extract bond data from a pickle and add the resulting Bond to the molecule
242  template <typename T>
243  static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
244  bool directMap = false);
245 
246  //! extract ring info from a pickle and add the resulting RingInfo to the
247  // molecule
248  template <typename T>
249  static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
250  bool directMap = false);
251 
252  //! extract a SubstanceGroup from a pickle
253  template <typename T>
254  static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss,
255  ROMol *mol, int version);
256 
257  template <typename T>
258  static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
259 
260  //! extract a conformation from a pickle
261  template <typename T, typename C>
262  static Conformer *_conformerFromPickle(std::istream &ss, int version);
263 
264  //! pickle standard properties
265  static void _pickleProperties(std::ostream &ss, const RDProps &props,
266  unsigned int pickleFlags);
267  //! unpickle standard properties
268  static void _unpickleProperties(std::istream &ss, RDProps &props);
269 
270  //! backwards compatibility
271  static void _pickleV1(const ROMol *mol, std::ostream &ss);
272  //! backwards compatibility
273  static void _depickleV1(std::istream &ss, ROMol *mol);
274  //! backwards compatibility
275  static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
276  //! backwards compatibility
277  static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
278 };
279 }; // namespace RDKit
280 
281 #endif
Defines the Atom class and associated typedefs.
The class for representing atoms.
Definition: Atom.h:69
class for representing a bond
Definition: Bond.h:47
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:43
used to indicate exceptions whilst pickling (serializing) molecules
Definition: MolPickler.h:36
~MolPicklerException() noexcept
Definition: MolPickler.h:41
const char * what() const noexcept override
Definition: MolPickler.h:40
MolPicklerException(const std::string msg)
Definition: MolPickler.h:39
MolPicklerException(const char *msg)
Definition: MolPickler.h:38
handles pickling (serializing) molecules
Definition: MolPickler.h:63
static const std::int32_t endianId
Definition: MolPickler.h:68
static void molFromPickle(std::istream &ss, ROMol *mol)
constructs a molecule from a pickle stored in a stream
static void pickleMol(const ROMol *mol, std::string &res, unsigned int propertyFlags)
static void molFromPickle(const std::string &pickle, ROMol *mol)
constructs a molecule from a pickle stored in a string
@ ATOM_PDB_RESIDUE_SERIALNUMBER
Definition: MolPickler.h:121
@ ATOM_PDB_RESIDUE_INSERTIONCODE
Definition: MolPickler.h:125
@ ATOM_PDB_RESIDUE_RESIDUENUMBER
Definition: MolPickler.h:130
@ ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE
Definition: MolPickler.h:129
@ ATOM_PDB_RESIDUE_SEGMENTNUMBER
Definition: MolPickler.h:131
@ ATOM_PDB_RESIDUE_RESIDUENAME
Definition: MolPickler.h:123
@ ATOM_PDB_RESIDUE_ISHETEROATOM
Definition: MolPickler.h:128
static void pickleMol(const ROMol &mol, std::ostream &ss)
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition: MolPickler.h:168
static void pickleMol(const ROMol *mol, std::ostream &ss, unsigned int propertyFlags)
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:175
static const CustomPropHandlerVec & getCustomPropHandlers()
static void setDefaultPickleProperties(unsigned int)
static const std::int32_t versionMajor
mark the pickle major version
Definition: MolPickler.h:65
static const std::int32_t versionMinor
mark the pickle minor version
Definition: MolPickler.h:66
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition: MolPickler.h:181
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition: MolPickler.h:158
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:67
static void pickleMol(const ROMol &mol, std::string &res)
A class to store information about a molecule's rings.
Definition: RingInfo.h:28
The class for representing SubstanceGroups.
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:346
RDKIT_CHEMREACTIONS_EXPORT void pickle(const boost::shared_ptr< EnumerationStrategyBase > &enumerator, std::ostream &ss)
pickles a EnumerationStrategy and adds the results to a stream ss
Std stuff.
Definition: Abbreviations.h:17
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec
Definition: StreamOps.h:369