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 <boost/utility/binary.hpp>
20 // Std stuff
21 #include <iostream>
22 #include <string>
23 #include <sstream>
24 #include <exception>
25 #ifdef WIN32
26 #include <ios>
27 #endif
28 #include <boost/cstdint.hpp>
29 
30 namespace RDKit {
31 class ROMol;
32 class RingInfo;
33 
34 //! used to indicate exceptions whilst pickling (serializing) molecules
35 class RDKIT_GRAPHMOL_EXPORT MolPicklerException : public std::exception {
36  public:
37  MolPicklerException(const char *msg) : _msg(msg){};
38  MolPicklerException(const std::string msg) : _msg(msg){};
39  const char *message() const { return _msg.c_str(); };
40  ~MolPicklerException() throw(){};
41 
42  private:
43  std::string _msg;
44 };
45 
46 namespace PicklerOps {
47 typedef enum {
48  NoProps = 0, // no data pickled
49  MolProps = BOOST_BINARY(1), // only public non computed properties
50  AtomProps = BOOST_BINARY(10),
51  BondProps = BOOST_BINARY(100),
52  QueryAtomData = BOOST_BINARY(
53  10), // n.b. DEPRECATED and set to AtomProps (does the same work)
54  PrivateProps = BOOST_BINARY(10000),
55  ComputedProps = BOOST_BINARY(100000),
57  0x7FFFFFFF, // all data pickled (only 31 bit flags in case enum==int)
59 }
60 
61 //! handles pickling (serializing) molecules
63  public:
64  static const boost::int32_t versionMajor; //!< mark the pickle major version
65  static const boost::int32_t versionMinor; //!< mark the pickle minor version
66  static const boost::int32_t versionPatch; //!< mark the pickle patch version
67  static const boost::int32_t endianId; //! mark the endian-ness of the pickle
68 
69  //! the pickle format is tagged using these tags:
70  //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM,
71  // otherwise
72  //! you will break old pickles.
73  typedef enum {
74  VERSION = 0,
136  } Tags;
137 
138  static unsigned int getDefaultPickleProperties();
139  static void setDefaultPickleProperties(unsigned int);
140 
141  //! pickles a molecule and sends the results to stream \c ss
142  static void pickleMol(const ROMol *mol, std::ostream &ss);
143  static void pickleMol(const ROMol *mol, std::ostream &ss,
144  unsigned int propertyFlags);
145 
146  static void pickleMol(const ROMol &mol, std::ostream &ss);
147 
148  static void pickleMol(const ROMol &mol, std::ostream &ss,
149  unsigned int propertyFlags) {
150  MolPickler::pickleMol(&mol, ss, propertyFlags);
151  };
152 
153  //! pickles a molecule and adds the results to string \c res
154  static void pickleMol(const ROMol *mol, std::string &res);
155  static void pickleMol(const ROMol *mol, std::string &res,
156  unsigned int propertyFlags);
157  static void pickleMol(const ROMol &mol, std::string &res);
158  static void pickleMol(const ROMol &mol, std::string &res,
159  unsigned int propertyFlags) {
160  MolPickler::pickleMol(&mol, res, propertyFlags);
161  };
162 
163  //! constructs a molecule from a pickle stored in a string
164  static void molFromPickle(const std::string &pickle, ROMol *mol);
165  static void molFromPickle(const std::string &pickle, ROMol &mol) {
166  MolPickler::molFromPickle(pickle, &mol);
167  };
168 
169  //! constructs a molecule from a pickle stored in a stream
170  static void molFromPickle(std::istream &ss, ROMol *mol);
171  static void molFromPickle(std::istream &ss, ROMol &mol) {
172  MolPickler::molFromPickle(ss, &mol);
173  };
174 
175  private:
176  //! Pickle nonquery atom data
177  static boost::int32_t _pickleAtomData(std::ostream &tss, const Atom *atom);
178  //! depickle nonquery atom data
179  static void _unpickleAtomData(std::istream &tss, Atom *atom, int version);
180 
181  static void _pickleQueryAtomData(std::ostream &tss, const Atom *atom);
182 
183  //! do the actual work of pickling a molecule
184  template <typename T>
185  static void _pickle(const ROMol *mol, std::ostream &ss,
186  unsigned int propertyFlags);
187 
188  //! do the actual work of pickling an Atom
189  template <typename T>
190  static void _pickleAtom(std::ostream &ss, const Atom *atom);
191 
192  //! do the actual work of pickling a Bond
193  template <typename T>
194  static void _pickleBond(std::ostream &ss, const Bond *bond,
195  std::map<int, int> &atomIdxMap);
196 
197  //! do the actual work of pickling an SSSR structure
198  template <typename T>
199  static void _pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
200  std::map<int, int> &atomIdxMap);
201 
202  //! do the actual work of pickling Stereo Group data
203  template <typename T>
204  static void _pickleStereo(std::ostream &ss,
205  const std::vector<StereoGroup> &groups,
206  std::map<int, int> &atomIdxMap);
207 
208  //! do the actual work of pickling a Conformer
209  template <typename T>
210  static void _pickleConformer(std::ostream &ss, const Conformer *conf);
211 
212  //! do the actual work of de-pickling a molecule
213  template <typename T>
214  static void _depickle(std::istream &ss, ROMol *mol, int version,
215  int numAtoms);
216 
217  //! extract atomic data from a pickle and add the resulting Atom to the
218  // molecule
219  template <typename T>
220  static Atom *_addAtomFromPickle(std::istream &ss, ROMol *mol,
221  RDGeom::Point3D &pos, int version,
222  bool directMap = false);
223 
224  //! extract bond data from a pickle and add the resulting Bond to the molecule
225  template <typename T>
226  static Bond *_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
227  bool directMap = false);
228 
229  //! extract ring info from a pickle and add the resulting RingInfo to the
230  // molecule
231  template <typename T>
232  static void _addRingInfoFromPickle(std::istream &ss, ROMol *mol, int version,
233  bool directMap = false);
234 
235  template <typename T>
236  static void _depickleStereo(std::istream &ss, ROMol *mol, int version);
237 
238  //! extract a conformation from a pickle
239  template <typename T>
240  static Conformer *_conformerFromPickle(std::istream &ss, int version);
241 
242  //! pickle standard properties
243  static void _pickleProperties(std::ostream &ss, const RDProps &props,
244  unsigned int pickleFlags);
245  //! unpickle standard properties
246  static void _unpickleProperties(std::istream &ss, RDProps &props);
247 
248  //! backwards compatibility
249  static void _pickleV1(const ROMol *mol, std::ostream &ss);
250  //! backwards compatibility
251  static void _depickleV1(std::istream &ss, ROMol *mol);
252  //! backwards compatibility
253  static void _addAtomFromPickleV1(std::istream &ss, ROMol *mol);
254  //! backwards compatibility
255  static void _addBondFromPickleV1(std::istream &ss, ROMol *mol);
256 };
257 }; // namespace RDKit
258 
259 #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:37
static void pickleMol(const ROMol &mol, std::string &res, unsigned int propertyFlags)
Definition: MolPickler.h:158
Tags
mark the endian-ness of the pickle
Definition: MolPickler.h:73
static const boost::int32_t versionMajor
mark the pickle major version
Definition: MolPickler.h:64
static const boost::int32_t versionMinor
mark the pickle minor version
Definition: MolPickler.h:65
static const boost::int32_t versionPatch
mark the pickle patch version
Definition: MolPickler.h:66
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:39
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:294
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:165
static const boost::int32_t endianId
Definition: MolPickler.h:67
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:35
class for representing a bond
Definition: Bond.h:47
MolPicklerException(const std::string msg)
Definition: MolPickler.h:38
handles pickling (serializing) molecules
Definition: MolPickler.h:62
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:171
Defines the Atom class and associated typedefs.
static void pickleMol(const ROMol &mol, std::ostream &ss, unsigned int propertyFlags)
Definition: MolPickler.h:148
The class for representing atoms.
Definition: Atom.h:69