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,
139  } Tags;
140 
141  static unsigned int getDefaultPickleProperties();
142  static void setDefaultPickleProperties(unsigned int);
143 
144  static const CustomPropHandlerVec &getCustomPropHandlers();
145  static void addCustomPropHandler(const CustomPropHandler &handler);
146 
147  //! pickles a molecule and sends the results to stream \c ss
148  static void pickleMol(const ROMol *mol, std::ostream &ss);
149  static void pickleMol(const ROMol *mol, std::ostream &ss,
150  unsigned int propertyFlags);
151 
152  static void pickleMol(const ROMol &mol, std::ostream &ss);
153 
154  static void pickleMol(const ROMol &mol, std::ostream &ss,
155  unsigned int propertyFlags) {
156  MolPickler::pickleMol(&mol, ss, propertyFlags);
157  };
158 
159  //! pickles a molecule and adds the results to string \c res
160  static void pickleMol(const ROMol *mol, std::string &res);
161  static void pickleMol(const ROMol *mol, std::string &res,
162  unsigned int propertyFlags);
163  static void pickleMol(const ROMol &mol, std::string &res);
164  static void pickleMol(const ROMol &mol, std::string &res,
165  unsigned int propertyFlags) {
166  MolPickler::pickleMol(&mol, res, propertyFlags);
167  };
168 
169  //! constructs a molecule from a pickle stored in a string
170  static void molFromPickle(const std::string &pickle, ROMol *mol);
171  static void molFromPickle(const std::string &pickle, ROMol &mol) {
172  MolPickler::molFromPickle(pickle, &mol);
173  };
174 
175  //! constructs a molecule from a pickle stored in a stream
176  static void molFromPickle(std::istream &ss, ROMol *mol);
177  static void molFromPickle(std::istream &ss, ROMol &mol) {
178  MolPickler::molFromPickle(ss, &mol);
179  };
180 
181  private:
182  //! Pickle nonquery atom data
183  static std::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
184  //! depickle nonquery atom data
185  static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
186 
187  static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
188 
189  //! do the actual work of pickling a molecule
190  template <typename T>
191  static void _pickle(const ROMol *mol, std::ostream &ss,
192  unsigned int propertyFlags);
193 
194  //! do the actual work of pickling an Atom
195  template <typename T>
196  static void _pickleAtom(std::ostream &ss, const Atom *atom);
197 
198  //! do the actual work of pickling a Bond
199  template <typename T>
200  static void _pickleBond(std::ostream &ss, const Bond *bond,
201  std::map<int, int> &atomIdxMap);
202 
203  //! do the actual work of pickling an SSSR structure
204  template <typename T>
205  static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
206  std::map<int, int> &atomIdxMap);
207 
208  //! do the actual work of pickling a SubstanceGroup
209  template <typename T>
210  static void _pickleSubstanceGroup(std::ostream &ss,
211  const SubstanceGroup &sgroup,
212  std::map<int, int> &atomIdxMap,
213  std::map<int, int> &bondIdxMap);
214 
215  //! do the actual work of pickling Stereo Group data
216  template <typename T>
217  static void _pickleStereo(std::ostream &ss,
218  const std::vector<StereoGroup> &groups,
219  std::map<int, int> &atomIdxMap);
220 
221  //! do the actual work of pickling a Conformer
222  template <typename T>
223  static void _pickleConformer(std::ostream &ss, const Conformer *conf);
224 
225  //! do the actual work of de-pickling a molecule
226  template <typename T>
227  static void _depickle(std::istream &ss, ROMol *mol, int version,
228  int numAtoms);
229 
230  //! extract atomic data from a pickle and add the resulting Atom to the
231  // molecule
232  template <typename T>
233  static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
234  RDGeom::Point3D &pos, int version,
235  bool directMap = false);
236 
237  //! extract bond data from a pickle and add the resulting Bond to the molecule
238  template <typename T>
239  static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
240  bool directMap = false);
241 
242  //! extract ring info from a pickle and add the resulting RingInfo to the
243  // molecule
244  template <typename T>
245  static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
246  bool directMap = false);
247 
248  //! extract a SubstanceGroup from a pickle
249  template <typename T>
250  static SubstanceGroup _getSubstanceGroupFromPickle(std::istream &ss,
251  ROMol *mol, int version);
252 
253  template <typename T>
254  static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
255 
256  //! extract a conformation from a pickle
257  template <typename T>
258  static Conformer *_conformerFromPickle(std::istream &ss, int version);
259 
260  //! pickle standard properties
261  static void _pickleProperties(std::ostream &ss, const RDProps &props,
262  unsigned int pickleFlags);
263  //! unpickle standard properties
264  static void _unpickleProperties(std::istream &ss, RDProps &props);
265 
266  //! backwards compatibility
267  static void _pickleV1(const ROMol *mol, std::ostream &ss);
268  //! backwards compatibility
269  static void _depickleV1(std::istream &ss, ROMol *mol);
270  //! backwards compatibility
271  static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
272  //! backwards compatibility
273  static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
274 };
275 }; // namespace RDKit
276 
277 #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:164
Tags
mark the endian-ness of the pickle
Definition: MolPickler.h:74
std::vector< std::shared_ptr< const CustomPropHandler > > CustomPropHandlerVec
Definition: StreamOps.h:332
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:171
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:28
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:43
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:177
Defines the Atom class and associated typedefs.
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition: MolPickler.h:154
The class for representing atoms.
Definition: Atom.h:69