MolPickler.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2001-2008 Greg Landrum and Rational Discovery LLC
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 #ifndef _RD_MOLPICKLE_H
00011 #define _RD_MOLPICKLE_H
00012 
00013 #include <Geometry/point.h>
00014 #include <GraphMol/Atom.h>
00015 #include <GraphMol/QueryAtom.h>
00016 #include <GraphMol/Bond.h>
00017 #include <GraphMol/QueryBond.h>
00018 
00019 // Std stuff
00020 #include <iostream>
00021 #include <string>
00022 #include <sstream>
00023 #include <exception>
00024 #ifdef WIN32
00025 #include <ios>
00026 #endif
00027 #include <boost/cstdint.hpp>
00028 
00029 namespace RDKit{
00030   class ROMol;
00031   class RingInfo;
00032 
00033   //! used to indicate exceptions whilst pickling (serializing) molecules
00034   class MolPicklerException : public std::exception {
00035     public :
00036       MolPicklerException(const char *msg) : _msg(msg) {};
00037       MolPicklerException(const std::string msg) : _msg(msg) {};
00038       const char *message () const { return _msg.c_str(); };
00039       ~MolPicklerException () throw () {};
00040     
00041     private :
00042       std::string _msg;
00043   };
00044 
00045   //! handles pickling (serializing) molecules
00046   class MolPickler{
00047   public:
00048     static const boost::int32_t versionMajor,versionMinor,versionPatch; //!< mark the pickle version
00049     static const boost::int32_t endianId;  //! mark the endian-ness of the pickle
00050 
00051     //! the pickle format is tagged using these tags:
00052     //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM, otherwise
00053     //! you will break old pickles.
00054     typedef enum {
00055       VERSION=0,
00056       BEGINATOM,
00057       ATOM_INDEX,
00058       ATOM_NUMBER,
00059       ATOM_POS,
00060       ATOM_CHARGE,
00061       ATOM_NEXPLICIT,
00062       ATOM_CHIRALTAG,
00063       ATOM_MASS,
00064       ATOM_ISAROMATIC,
00065       ENDATOM,
00066       BEGINBOND,
00067       BOND_INDEX,
00068       BOND_BEGATOMIDX,
00069       BOND_ENDATOMIDX,
00070       BOND_TYPE,
00071       BOND_DIR,
00072       ENDBOND,
00073       BEGINPROPS,
00074       ENDPROPS,
00075       BEGINSSSR,
00076       ENDSSSR,
00077       ENDMOL,
00078       BEGINCONFS,
00079       ATOM_MAPNUMBER,
00080       BEGINQUERY,
00081       QUERY_VALUE,
00082       QUERY_ISNEGATED,
00083       QUERY_NUMCHILDREN,
00084       QUERY_BOOL,
00085       QUERY_AND,
00086       QUERY_OR,
00087       QUERY_XOR,
00088       QUERY_EQUALS,
00089       QUERY_GREATER,
00090       QUERY_GREATEREQUAL,
00091       QUERY_LESS,
00092       QUERY_LESSEQUAL,
00093       QUERY_RANGE,
00094       QUERY_SET,
00095       QUERY_NULL,
00096       QUERY_ATOMRING,
00097       QUERY_RECURSIVE,
00098       ENDQUERY,
00099     } Tags;
00100 
00101     //! pickles a molecule and sends the results to stream \c ss
00102     static void pickleMol(const ROMol *mol,std::ostream &ss);
00103     static void pickleMol(const ROMol &mol,std::ostream &ss) {MolPickler::pickleMol(&mol,ss);};
00104     //! pickles a molecule and adds the results to string \c res
00105     static void pickleMol(const ROMol *mol,std::string &res);
00106     static void pickleMol(const ROMol &mol,std::string &res) {MolPickler::pickleMol(&mol,res);};
00107 
00108     //! constructs a molecule from a pickle stored in a string
00109     static void molFromPickle(const std::string &pickle,ROMol *mol);
00110     static void molFromPickle(const std::string &pickle,ROMol &mol) {MolPickler::molFromPickle(pickle,&mol);};
00111 
00112     //! constructs a molecule from a pickle stored in a stream
00113     static void molFromPickle(std::istream &ss,ROMol *mol);
00114     static void molFromPickle(std::istream &ss,ROMol &mol) { MolPickler::molFromPickle(ss,&mol); };
00115   private:
00116     //! do the actual work of pickling a molecule
00117     template <typename T>
00118     static void _pickle(const ROMol *mol,std::ostream &ss);
00119 
00120     //! do the actual work of pickling an Atom
00121     template <typename T>
00122     static void _pickleAtom(std::ostream &ss,const Atom *atom);
00123 
00124     //! do the actual work of pickling a Bond
00125     template <typename T>
00126     static void _pickleBond(std::ostream &ss,const Bond *bond,
00127                             std::map<int,int> &atomIdxMap);
00128 
00129     //! do the actual work of pickling an SSSR structure
00130     template <typename T>
00131     static void _pickleSSSR(std::ostream &ss,const RingInfo *ringInfo,
00132                             std::map<int,int> &atomIdxMap);
00133 
00134     //! do the actual work of pickling a Conformer
00135     template <typename T>
00136     static void _pickleConformer(std::ostream &ss,const Conformer *conf);
00137 
00138     //! do the actual work of de-pickling a molecule
00139     template <typename T>
00140     static void _depickle(std::istream &ss,ROMol *mol, int version,int numAtoms);
00141 
00142 
00143     //! extract atomic data from a pickle and add the resulting Atom to the molecule
00144     template <typename T>
00145     static Atom *_addAtomFromPickle(std::istream &ss,ROMol *mol, RDGeom::Point3D &pos,
00146                                     int version,
00147                                     bool directMap=false);
00148 
00149     //! extract bond data from a pickle and add the resulting Bond to the molecule
00150     template <typename T>
00151     static Bond *_addBondFromPickle(std::istream &ss,ROMol *mol,
00152                                     int version,
00153                                     bool directMap=false);
00154 
00155     //! extract ring info from a pickle and add the resulting RingInfo to the molecule
00156     template <typename T>
00157     static void _addRingInfoFromPickle(std::istream &ss,ROMol *mol,
00158                                        int version,
00159                                        bool directMap=false);
00160 
00161     //! extract a conformation from a pickle
00162     template <typename T> 
00163       static Conformer *_conformerFromPickle(std::istream &ss,int version);
00164 
00165     //! backwards compatibility
00166     static void _pickleV1(const ROMol *mol,std::ostream &ss);
00167     //! backwards compatibility
00168     static void _depickleV1(std::istream &ss,ROMol *mol);
00169     //! backwards compatibility
00170     static void _addAtomFromPickleV1(std::istream &ss,ROMol *mol);
00171     //! backwards compatibility
00172     static void _addBondFromPickleV1(std::istream &ss,ROMol *mol);
00173 
00174   };  
00175   
00176 };
00177 
00178 
00179 #endif