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