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

Generated on Sat May 24 08:36:32 2008 for RDCode by  doxygen 1.5.3