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

Generated on Tue Oct 7 06:10:11 2008 for RDCode by  doxygen 1.5.5