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 *message() const { return _msg.c_str(); };
41  ~MolPicklerException() throw(){};
42 
43  private:
44  std::string _msg;
45 };
46 
47 namespace PicklerOps {
48 typedef enum {
49  NoProps = 0, // no data pickled
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),
58  0x7FFFFFFF, // all data pickled (only 31 bit flags in case enum==int)
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,
138  } Tags;
139 
140  static unsigned int getDefaultPickleProperties();
141  static void setDefaultPickleProperties(unsigned int);
142 
143  static const CustomPropHandlerVec &getCustomPropHandlers();
144  static void addCustomPropHandler(const CustomPropHandler &handler);
145 
146  //! pickles a molecule and sends the results to stream \c ss
147  static void pickleMol(const ROMol *mol, std::ostream &ss);
148  static void pickleMol(const ROMol *mol, std::ostream &ss,
149  unsigned int propertyFlags);
150 
151  static void pickleMol(const ROMol &mol, std::ostream &ss);
152 
153  static void pickleMol(const ROMol &mol, std::ostream &ss,
154  unsigned int propertyFlags) {
155  MolPickler::pickleMol(&mol, ss, propertyFlags);
156  };
157 
158  //! pickles a molecule and adds the results to string \c res
159  static void pickleMol(const ROMol *mol, std::string &res);
160  static void pickleMol(const ROMol *mol, std::string &res,
161  unsigned int propertyFlags);
162  static void pickleMol(const ROMol &mol, std::string &res);
163  static void pickleMol(const ROMol &mol, std::string &res,
164  unsigned int propertyFlags) {
165  MolPickler::pickleMol(&mol, res, propertyFlags);
166  };
167 
168  //! constructs a molecule from a pickle stored in a string
169  static void molFromPickle(const std::string &pickle, ROMol *mol);
170  static void molFromPickle(const std::string &pickle, ROMol &mol) {
171  MolPickler::molFromPickle(pickle, &mol);
172  };
173 
174  //! constructs a molecule from a pickle stored in a stream
175  static void molFromPickle(std::istream &ss, ROMol *mol);
176  static void molFromPickle(std::istream &ss, ROMol &mol) {
177  MolPickler::molFromPickle(ss, &mol);
178  };
179 
180  private:
181  //! Pickle nonquery atom data
182  static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
183  //! depickle nonquery atom data
184  static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
185 
186  static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
187 
188  //! do the actual work of pickling a molecule
189  template <typename T>
190  static void _pickle(const ROMol *mol, std::ostream &ss,
191  unsigned int propertyFlags);
192 
193  //! do the actual work of pickling an Atom
194  template <typename T>
195  static void _pickleAtom(std::ostream &ss, const Atom *atom);
196 
197  //! do the actual work of pickling a Bond
198  template <typename T>
199  static void _pickleBond(std::ostream &ss, const Bond *bond,
200  std::map<int, int> &atomIdxMap);
201 
202  //! do the actual work of pickling an SSSR structure
203  template <typename T>
204  static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
205  std::map<int, int> &atomIdxMap);
206 
207  //! do the actual work of pickling a SubstanceGroup
208  template <typename T>
209  static void _pickleSubstanceGroup(std::ostream &ss, const SubstanceGroup &sgroup,
210  std::map<int, int> &atomIdxMap,
211  std::map<int, int> &bondIdxMap);
212 
213  //! do the actual work of pickling Stereo Group data
214  template <typename T>
215  static void _pickleStereo(std::ostream &ss,
216  const std::vector<StereoGroup> &groups,
217  std::map<int, int> &atomIdxMap);
218 
219  //! do the actual work of pickling a Conformer
220  template <typename T>
221  static void _pickleConformer(std::ostream &ss, const Conformer *conf);
222 
223  //! do the actual work of de-pickling a molecule
224  template <typename T>
225  static void _depickle(std::istream &ss, ROMol *mol, int version,
226  int numAtoms);
227 
228  //! extract atomic data from a pickle and add the resulting Atom to the
229  // molecule
230  template <typename T>
231  static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
232  RDGeom::Point3D &pos, int version,
233  bool directMap = false);
234 
235  //! extract bond data from a pickle and add the resulting Bond to the molecule
236  template <typename T>
237  static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
238  bool directMap = false);
239 
240  //! extract ring info from a pickle and add the resulting RingInfo to the
241  // molecule
242  template <typename T>
243  static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
244  bool directMap = false);
245 
246  //! extract a SubstanceGroup from a pickle
247  template <typename T>
248  static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss, ROMol *mol, int version);
249 
250  template <typename T>
251  static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
252 
253  //! extract a conformation from a pickle
254  template <typename T>
255  static Conformer *_conformerFromPickle(std::istream &ss, int version);
256 
257  //! pickle standard properties
258  static void _pickleProperties(std::ostream &ss, const RDProps &props,
259  unsigned int pickleFlags);
260  //! unpickle standard properties
261  static void _unpickleProperties(std::istream &ss, RDProps &props);
262 
263  //! backwards compatibility
264  static void _pickleV1(const ROMol *mol, std::ostream &ss);
265  //! backwards compatibility
266  static void _depickleV1(std::istream &ss, ROMol *mol);
267  //! backwards compatibility
268  static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
269  //! backwards compatibility
270  static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
271 };
272 }; // namespace RDKit
273 
274 #endif
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
MolPicklerException(const char *msg)
Definition: MolPickler.h:38
The class for representing SubstanceGroups.
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition: MolPickler.h:163
Tags
mark the endian-ness of the pickle
Definition: MolPickler.h:74
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec
Definition: StreamOps.h:331
static const std::int32_t versionMinor
mark the pickle minor version
Definition: MolPickler.h:66
static const std::int32_t versionMajor
mark the pickle major version
Definition: MolPickler.h:65
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
const char * message() const
Definition: MolPickler.h:40
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:307
static const std::int32_t versionPatch
mark the pickle patch version
Definition: MolPickler.h:67
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:170
static const std::int32_t endianId
Definition: MolPickler.h:68
Std stuff.
Definition: Atom.h:30
A class to store information about a molecule&#39;s rings.
Definition: RingInfo.h:22
used to indicate exceptions whilst pickling (serializing) molecules
Definition: MolPickler.h:36
class for representing a bond
Definition: Bond.h:47
MolPicklerException(const std::string msg)
Definition: MolPickler.h:39
handles pickling (serializing) molecules
Definition: MolPickler.h:63
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:42
static void molFromPickle(const std::string &pickle, ROMol *mol)
constructs a molecule from a pickle stored in a string
static void molFromPickle(std::istream &ss, ROMol &mol)
Definition: MolPickler.h:176
Defines the Atom class and associated typedefs.
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition: MolPickler.h:153
The class for representing atoms.
Definition: Atom.h:69