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