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  //! handles pickling (serializing) molecules
46  class MolPickler{
47  public:
48  static const boost::int32_t versionMajor,versionMinor,versionPatch; //!< mark the pickle version
49  static const boost::int32_t endianId; //! mark the endian-ness of the pickle
50 
51  //! the pickle format is tagged using these tags:
52  //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM, otherwise
53  //! you will break old pickles.
54  typedef enum {
55  VERSION=0,
113  } Tags;
114 
115  //! pickles a molecule and sends the results to stream \c ss
116  static void pickleMol(const ROMol *mol,std::ostream &ss);
117  static void pickleMol(const ROMol &mol,std::ostream &ss) {MolPickler::pickleMol(&mol,ss);};
118  //! pickles a molecule and adds the results to string \c res
119  static void pickleMol(const ROMol *mol,std::string &res);
120  static void pickleMol(const ROMol &mol,std::string &res) {MolPickler::pickleMol(&mol,res);};
121 
122  //! constructs a molecule from a pickle stored in a string
123  static void molFromPickle(const std::string &pickle,ROMol *mol);
124  static void molFromPickle(const std::string &pickle,ROMol &mol) {MolPickler::molFromPickle(pickle,&mol);};
125 
126  //! constructs a molecule from a pickle stored in a stream
127  static void molFromPickle(std::istream &ss,ROMol *mol);
128  static void molFromPickle(std::istream &ss,ROMol &mol) { MolPickler::molFromPickle(ss,&mol); };
129  private:
130  //! do the actual work of pickling a molecule
131  template <typename T>
132  static void _pickle(const ROMol *mol,std::ostream &ss);
133 
134  //! do the actual work of pickling an Atom
135  template <typename T>
136  static void _pickleAtom(std::ostream &ss,const Atom *atom);
137 
138  //! do the actual work of pickling a Bond
139  template <typename T>
140  static void _pickleBond(std::ostream &ss,const Bond *bond,
141  std::map<int,int> &atomIdxMap);
142 
143  //! do the actual work of pickling an SSSR structure
144  template <typename T>
145  static void _pickleSSSR(std::ostream &ss,const RingInfo *ringInfo,
146  std::map<int,int> &atomIdxMap);
147 
148  //! do the actual work of pickling a Conformer
149  template <typename T>
150  static void _pickleConformer(std::ostream &ss,const Conformer *conf);
151 
152  //! do the actual work of de-pickling a molecule
153  template <typename T>
154  static void _depickle(std::istream &ss,ROMol *mol, int version,int numAtoms);
155 
156 
157  //! extract atomic data from a pickle and add the resulting Atom to the molecule
158  template <typename T>
159  static Atom *_addAtomFromPickle(std::istream &ss,ROMol *mol, RDGeom::Point3D &pos,
160  int version,
161  bool directMap=false);
162 
163  //! extract bond data from a pickle and add the resulting Bond to the molecule
164  template <typename T>
165  static Bond *_addBondFromPickle(std::istream &ss,ROMol *mol,
166  int version,
167  bool directMap=false);
168 
169  //! extract ring info from a pickle and add the resulting RingInfo to the molecule
170  template <typename T>
171  static void _addRingInfoFromPickle(std::istream &ss,ROMol *mol,
172  int version,
173  bool directMap=false);
174 
175  //! extract a conformation from a pickle
176  template <typename T>
177  static Conformer *_conformerFromPickle(std::istream &ss,int version);
178 
179  //! backwards compatibility
180  static void _pickleV1(const ROMol *mol,std::ostream &ss);
181  //! backwards compatibility
182  static void _depickleV1(std::istream &ss,ROMol *mol);
183  //! backwards compatibility
184  static void _addAtomFromPickleV1(std::istream &ss,ROMol *mol);
185  //! backwards compatibility
186  static void _addBondFromPickleV1(std::istream &ss,ROMol *mol);
187 
188  };
189 
190 };
191 
192 
193 #endif
const int versionMinor
Definition: Catalog.h:31
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)
Definition: MolPickler.h:120
Tags
mark the endian-ness of the pickle
Definition: MolPickler.h:54
static const boost::int32_t versionPatch
mark the pickle version
Definition: MolPickler.h:48
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:105
static void molFromPickle(const std::string &pickle, ROMol &mol)
Definition: MolPickler.h:124
static const boost::int32_t endianId
Definition: MolPickler.h:49
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
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:46
MolPicklerException(const std::string msg)
Definition: MolPickler.h:37
handles pickling (serializing) molecules
Definition: MolPickler.h:46
const int versionMajor
Definition: Catalog.h:30
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:128
Defines the Atom class and associated typedefs.
static void pickleMol(const ROMol &mol, std::ostream &ss)
Definition: MolPickler.h:117
The class for representing atoms.
Definition: Atom.h:67