mol.h
Go to the documentation of this file.
00001 /**********************************************************************
00002 mol.h - Handle molecules. Declarations of OBMol, OBAtom, OBBond, OBResidue.
00003         (the main header for Open Babel)
00004 
00005 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00006 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
00007 Some portions Copyright (C) 2003 by Michael Banck
00008 
00009 This file is part of the Open Babel project.
00010 For more information, see <http://openbabel.org/>
00011 
00012 This program is free software; you can redistribute it and/or modify
00013 it under the terms of the GNU General Public License as published by
00014 the Free Software Foundation version 2 of the License.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 ***********************************************************************/
00021 
00022 #ifndef OB_MOL_H
00023 #define OB_MOL_H
00024 
00025 #include <openbabel/babelconfig.h>
00026 
00027 #ifndef EXTERN
00028 #  define EXTERN extern
00029 #endif
00030 
00031 #include <math.h>
00032 #include <float.h>
00033 
00034 #include <vector>
00035 #include <string>
00036 #include <map>
00037 
00038 // Currently includes many headers for 2.x backwards compatibility
00039 // \deprecated -- this will be cleaned up in 3.0 efforts
00040 //      to improve compile time significantly.
00041 // Only include necessary headers and class declaration stubs.
00042 #include <openbabel/atom.h>
00043 #include <openbabel/bond.h>
00044 #include <openbabel/base.h>
00045 #include <openbabel/data.h>
00046 #include <openbabel/chains.h>
00047 #include <openbabel/math/vector3.h>
00048 #include <openbabel/bitvec.h>
00049 #include <openbabel/residue.h>
00050 #include <openbabel/ring.h>
00051 #include <openbabel/generic.h>
00052 #include <openbabel/typer.h>
00053 #include <openbabel/oberror.h>
00054 #include <openbabel/obiter.h>
00055 #include <openbabel/internalcoord.h>
00056 
00057 namespace OpenBabel
00058 {
00059 
00060   class OBAtom;
00061   class OBBond;
00062   class OBInternalCoord;
00063   class OBConversion; //used only as a pointer
00064 
00065   // Class OBMol
00066   //MOL Property Macros (flags) -- 32+ bits
00068 #define OB_SSSR_MOL              (1<<1)
00069 
00070 #define OB_RINGFLAGS_MOL         (1<<2)
00071 
00072 #define OB_AROMATIC_MOL          (1<<3)
00073 
00074 #define OB_ATOMTYPES_MOL         (1<<4)
00075 
00076 #define OB_CHIRALITY_MOL         (1<<5)
00077 
00078 #define OB_PCHARGE_MOL           (1<<6)
00079 
00080 #define OB_HYBRID_MOL            (1<<8)
00081 
00082 #define OB_IMPVAL_MOL            (1<<9)
00083 
00084 #define OB_KEKULE_MOL            (1<<10)
00085 
00086 #define OB_CLOSURE_MOL           (1<<11)
00087 
00088 #define OB_H_ADDED_MOL           (1<<12)
00089 
00090 #define OB_PH_CORRECTED_MOL      (1<<13)
00091 
00092 #define OB_AROM_CORRECTED_MOL    (1<<14)
00093 
00094 #define OB_CHAINS_MOL            (1<<15)
00095 
00096 #define OB_TCHARGE_MOL                 (1<<16)
00097 
00098 #define OB_TSPIN_MOL             (1<<17)
00099 
00100 #define OB_RINGTYPES_MOL         (1<<18)
00101 
00102 #define OB_PATTERN_STRUCTURE     (1<<19)
00103 
00104 #define OB_LSSR_MOL              (1<<20)
00105   // flags 21-32 unspecified
00106 #define OB_CURRENT_CONFORMER     -1
00107 
00108   // class introduction in mol.cpp
00109  class OBAPI OBMol: public OBBase
00110   {
00111   protected:
00112     int                           _flags;       
00113     bool                          _autoPartialCharge;
00114     bool                          _autoFormalCharge;
00115     std::string                   _title;       
00116     std::vector<OBAtom*>          _vatom;       
00117     std::vector<OBAtom*>          _atomIds;     
00118     std::vector<OBBond*>          _vbond;       
00119     std::vector<OBBond*>          _bondIds;     
00120     unsigned short int            _dimension;   
00121     int                           _totalCharge; 
00122     unsigned int                  _totalSpin;   
00123     double                        *_c;          
00124     std::vector<double*>          _vconf;       
00125     double                        _energy;      
00126     unsigned int                  _natoms;      
00127     unsigned int                  _nbonds;      
00128     std::vector<OBResidue*>       _residue;     
00129     std::vector<OBInternalCoord*> _internals;   
00130     unsigned short int            _mod;         
00131 
00132     bool  HasFlag(int flag)    { return((_flags & flag) ? true : false); }
00133     void  SetFlag(int flag)    { _flags |= flag; }
00134 
00136 
00137     void start_kekulize(std::vector <OBAtom*> &cycle, std::vector<int> &electron);
00138     bool expand_kekulize(int bond_idx, std::vector<int> &atomState, std::vector<int> &bondState);
00139     bool has_no_leftover_electrons(std::vector<int> &atomState);
00140     int getorden(OBAtom *atom);
00141     bool expandcycle(OBAtom *atom, OBBitVec &avisit, const OBBitVec &potAromBonds);
00143 
00144   public:
00145 
00147 
00148 
00149     OBMol();
00151     OBMol(const OBMol &);
00153     virtual ~OBMol();
00155     OBMol &operator=(const OBMol &mol);
00157     OBMol &operator+=(const OBMol &mol);
00158 
00161     void ReserveAtoms(int natoms)
00162     {
00163       if (natoms > 0 && _mod) {
00164         _vatom.reserve(natoms);
00165         _atomIds.reserve(natoms);
00166       }
00167     }
00168 
00171     virtual OBAtom *CreateAtom(void);
00174     virtual OBBond *CreateBond(void);
00177     virtual OBResidue *CreateResidue(void);
00180     virtual void DestroyAtom(OBAtom*);
00183     virtual void DestroyBond(OBBond*);
00186     virtual void DestroyResidue(OBResidue*);
00187 
00190     bool AddAtom(OBAtom&);
00193     bool InsertAtom(OBAtom &);
00201     bool AddBond(int beginIdx, int endIdx, int order,
00202                  int flags=0,int insertpos=-1);
00205     bool AddBond(OBBond&);
00208     bool AddResidue(OBResidue&);
00209 
00213     OBAtom    *NewAtom();
00217     OBAtom    *NewAtom(unsigned long id);
00221     OBBond    *NewBond();
00225     OBBond    *NewBond(unsigned long id);
00227     OBResidue *NewResidue();
00232     bool DeleteAtom(OBAtom*, bool destroyAtom = true);
00235     bool DeleteBond(OBBond*, bool destroyBond = true);
00238     bool DeleteResidue(OBResidue*, bool destroyResidue = true);
00240 
00242 
00243 
00244 
00245 
00246     virtual void BeginModify(void);
00250     virtual void EndModify(bool nukePerceivedData=true);
00252     int GetMod()           {      return(_mod);    }
00255     void IncrementMod()    {      _mod++;          }
00258     void DecrementMod()    {      _mod--;          }
00260 
00262 
00263 
00264     int          GetFlags()               { return(_flags); }
00267     const char  *GetTitle(bool replaceNewlines = true) const;
00269     unsigned int NumAtoms() const         {  return(_natoms); }
00271     unsigned int NumBonds() const         {  return(_nbonds); }
00273     unsigned int NumHvyAtoms();
00275     unsigned int NumResidues() const      { return(static_cast<unsigned int> (_residue.size())); }
00277     unsigned int NumRotors();
00278 
00281     OBAtom      *GetAtom(int idx) const;
00283     OBAtom      *GetAtomById(unsigned long id) const;
00286     OBAtom      *GetFirstAtom() const;
00289     OBBond      *GetBond(int idx) const;
00291     OBBond      *GetBondById(unsigned long id) const;
00294     OBBond      *GetBond(int a, int b) const;
00295     // The safer version of the above method
00297     OBBond      *GetBond(OBAtom* bgn, OBAtom* end) const;
00300     OBResidue   *GetResidue(int idx) const;
00301     std::vector<OBInternalCoord*> GetInternalCoord();
00306     double       GetTorsion(int,int,int,int);
00311     double       GetTorsion(OBAtom* a,OBAtom* b,OBAtom* c,OBAtom* d);
00314     double GetAngle(OBAtom* a, OBAtom* b, OBAtom* c);
00316     std::string  GetFormula();
00318     std::string  GetSpacedFormula(int ones=0, const char* sp=" ", bool implicitH = true);
00320     double       GetEnergy() const { return _energy; }
00322     double       GetMolWt(bool implicitH = true);
00324     double       GetExactMass(bool implicitH = true);
00326     int          GetTotalCharge();
00328     unsigned int GetTotalSpinMultiplicity();
00330     unsigned short int GetDimension() const { return _dimension; }
00332     double      *GetCoordinates() { return(_c); }
00334     std::vector<OBRing*> &GetSSSR();
00336     std::vector<OBRing*> &GetLSSR();
00338     bool AutomaticFormalCharge()   { return(_autoFormalCharge);  }
00340     bool AutomaticPartialCharge()  { return(_autoPartialCharge); }
00342 
00343 
00345 
00346 
00347     void   SetTitle(const char *title);
00349     void   SetTitle(std::string &title);
00351     void   SetFormula(std::string molFormula);
00353     void   SetEnergy(double energy) { _energy = energy; }
00355     void   SetDimension(unsigned short int d) { _dimension = d; }
00357     void   SetTotalCharge(int charge);
00360     void   SetTotalSpinMultiplicity(unsigned int spinMultiplicity);
00366     void SetInternalCoord(std::vector<OBInternalCoord*> int_coord);
00368     void SetAutomaticFormalCharge(bool val)
00369     { _autoFormalCharge=val;  }
00371     void SetAutomaticPartialCharge(bool val)
00372     { _autoPartialCharge=val; }
00373 
00375     void   SetAromaticPerceived()    { SetFlag(OB_AROMATIC_MOL);    }
00377     void   SetSSSRPerceived()        { SetFlag(OB_SSSR_MOL);        }
00379     void   SetLSSRPerceived()        { SetFlag(OB_LSSR_MOL);        }
00381     void   SetRingAtomsAndBondsPerceived(){SetFlag(OB_RINGFLAGS_MOL);}
00383     void   SetAtomTypesPerceived()   { SetFlag(OB_ATOMTYPES_MOL);   }
00385     void   SetRingTypesPerceived()   { SetFlag(OB_RINGTYPES_MOL);   }
00387     void   SetChainsPerceived()      { SetFlag(OB_CHAINS_MOL);      }
00389     void   SetChiralityPerceived()   { SetFlag(OB_CHIRALITY_MOL);   }
00391     void   SetPartialChargesPerceived(){ SetFlag(OB_PCHARGE_MOL);   }
00393     void   SetHybridizationPerceived() { SetFlag(OB_HYBRID_MOL);    }
00395     void   SetImplicitValencePerceived(){ SetFlag(OB_IMPVAL_MOL);   }
00397     void   SetKekulePerceived()      { SetFlag(OB_KEKULE_MOL);      }
00399     void   SetClosureBondsPerceived(){ SetFlag(OB_CLOSURE_MOL);     }
00401     void   SetHydrogensAdded()       { SetFlag(OB_H_ADDED_MOL);     }
00402     void   SetCorrectedForPH()       { SetFlag(OB_PH_CORRECTED_MOL);}
00403     void   SetAromaticCorrected()    { SetFlag(OB_AROM_CORRECTED_MOL);}
00404     void   SetSpinMultiplicityAssigned(){ SetFlag(OB_TSPIN_MOL);    }
00405     void   SetFlags(int flags)       { _flags = flags;              }
00406 
00407     void   UnsetAromaticPerceived()  { _flags &= (~(OB_AROMATIC_MOL));   }
00408     void   UnsetSSSRPerceived()  { _flags &= (~(OB_SSSR_MOL));   }
00409     void   UnsetRingTypesPerceived()  { _flags &= (~(OB_RINGTYPES_MOL));   }
00410     void   UnsetPartialChargesPerceived(){ _flags &= (~(OB_PCHARGE_MOL));}
00411     void   UnsetImplicitValencePerceived(){_flags &= (~(OB_IMPVAL_MOL)); }
00412     void   UnsetHydrogensAdded()       { UnsetFlag(OB_H_ADDED_MOL);     }
00413     void   UnsetFlag(int flag)       { _flags &= (~(flag));              }
00415 
00417 
00418     // Description in transform.cpp (command-line transformations to this molecule)
00419     virtual OBBase*    DoTransformations(const std::map<std::string,std::string>* pOptions,OBConversion* pConv);
00420     // Ditto (documentation on transformation options)
00421     static const char* ClassDescription();
00423     bool Clear();
00425     void RenumberAtoms(std::vector<OBAtom*>&);
00427     void RenumberAtoms(std::vector<int>);
00430     void SetCoordinates(double *c);
00432     void ToInertialFrame(int conf, double *rmat);
00434     void ToInertialFrame();
00436     void Translate(const vector3 &v);
00438     void Translate(const vector3 &v, int conf);
00440     void Rotate(const double u[3][3]);
00442     void Rotate(const double m[9]);
00444     void Rotate(const double m[9],int nconf);
00446     void Center();
00448 
00449     bool Kekulize();
00450     bool PerceiveKekuleBonds();
00451 
00452     void NewPerceiveKekuleBonds();
00453 
00456     bool DeleteHydrogens();
00459     bool DeleteHydrogens(OBAtom*);
00462     bool DeleteNonPolarHydrogens();
00465     bool DeleteHydrogen(OBAtom*);
00472     bool AddHydrogens(bool polaronly=false,bool correctForPH=false, double pH=7.4);
00474     bool AddHydrogens(OBAtom*);
00476     bool AddPolarHydrogens();
00477 
00481     bool StripSalts(int threshold=0);
00483     std::vector<OBMol> Separate(int StartIndex=1);
00485     bool GetNextFragment( OpenBabel::OBMolAtomDFSIter& iter, OBMol& newMol );
00487     bool ConvertDativeBonds();
00488 
00490     bool CorrectForPH(double pH=7.4);
00491     // docs in mol.cpp
00492     bool AssignSpinMultiplicity(bool NoImplicitH=false);
00494     void   SetIsPatternStructure()       { SetFlag(OB_PATTERN_STRUCTURE);}
00495 
00498     vector3 Center(int nconf);
00504     void SetTorsion(OBAtom*,OBAtom*,OBAtom*,OBAtom*,double ang);
00506 
00508 
00509 
00510     void FindSSSR();
00512     void FindLSSR();
00514     void FindRingAtomsAndBonds();
00517     void FindChiralCenters() { IsChiral(); }
00518     // documented in mol.cpp -- locates all atom indexes which can reach 'end'
00519     void FindChildren(std::vector<int> & children,int bgnIdx,int endIdx);
00520     // documented in mol.cpp -- locates all atoms which can reach 'end'
00521     void FindChildren(std::vector<OBAtom*>& children,OBAtom* bgn,OBAtom* end);
00526     void FindLargestFragment(OBBitVec &frag);
00529     void ContigFragList(std::vector<std::vector<int> >&);
00531     void Align(OBAtom*,OBAtom*,vector3&,vector3&);
00533     void ConnectTheDots();
00535     void PerceiveBondOrders();
00537     void FindAngles();
00539     void FindTorsions();
00540     // documented in mol.cpp: graph-theoretical distance for each atom
00541     bool         GetGTDVector(std::vector<int> &);
00542     // documented in mol.cpp: graph-invariant index for each atom
00543     void         GetGIVector(std::vector<unsigned int> &);
00544     // documented in mol.cpp: calculate symmetry-unique identifiers
00545     void         GetGIDVector(std::vector<unsigned int> &);
00547 
00549 
00550 
00551     bool Has2D(bool Not3D=false);
00553     bool Has3D();
00555     bool HasNonZeroCoords();
00557     bool HasAromaticPerceived()     { return(HasFlag(OB_AROMATIC_MOL)); }
00559     bool HasSSSRPerceived()         { return(HasFlag(OB_SSSR_MOL));     }
00561     bool HasLSSRPerceived()         { return(HasFlag(OB_LSSR_MOL));     }
00563     bool HasRingAtomsAndBondsPerceived(){return(HasFlag(OB_RINGFLAGS_MOL));}
00565     bool HasAtomTypesPerceived()    { return(HasFlag(OB_ATOMTYPES_MOL));}
00567     bool HasRingTypesPerceived()    { return(HasFlag(OB_RINGTYPES_MOL));}
00569     bool HasChiralityPerceived()    { return(HasFlag(OB_CHIRALITY_MOL));}
00571     bool HasPartialChargesPerceived() { return(HasFlag(OB_PCHARGE_MOL));}
00573     bool HasHybridizationPerceived() { return(HasFlag(OB_HYBRID_MOL));  }
00575     bool HasImplicitValencePerceived() { return(HasFlag(OB_IMPVAL_MOL));}
00577     bool HasKekulePerceived() { return(HasFlag(OB_KEKULE_MOL));         }
00579     bool HasClosureBondsPerceived() { return(HasFlag(OB_CLOSURE_MOL));  }
00581     bool HasChainsPerceived() { return(HasFlag(OB_CHAINS_MOL));         }
00583     bool HasHydrogensAdded() { return(HasFlag(OB_H_ADDED_MOL));         }
00585     bool HasAromaticCorrected() { return(HasFlag(OB_AROM_CORRECTED_MOL));}
00587     bool IsCorrectedForPH() { return(HasFlag(OB_PH_CORRECTED_MOL));     }
00589     bool HasSpinMultiplicityAssigned() { return(HasFlag(OB_TSPIN_MOL)); }
00591     bool IsChiral();
00593     bool Empty()                       { return(_natoms == 0);          }
00595 
00597 
00598 
00599     int     NumConformers()    { return((_vconf.empty())?0:static_cast<int> (_vconf.size())); }
00601     void    SetConformers(std::vector<double*> &v);
00603     void    AddConformer(double *f)    {  _vconf.push_back(f);    }
00606     void    SetConformer(int i);
00609     void    CopyConformer(double* c,int nconf);
00611     void    DeleteConformer(int nconf);
00613     double  *GetConformer(int i)       {  return(_vconf[i]);      }
00615     void    SetEnergies(std::vector<double> &energies);
00617     std::vector<double> GetEnergies();
00620     double  GetEnergy(int ci);
00623     double  *BeginConformer(std::vector<double*>::iterator&i)
00624     { i = _vconf.begin();
00625       return((i == _vconf.end()) ? NULL:*i); }
00628     double  *NextConformer(std::vector<double*>::iterator&i)
00629     { ++i;
00630       return((i == _vconf.end()) ? NULL:*i); }
00632     std::vector<double*> &GetConformers() {   return(_vconf);     }
00634 
00636 
00637 
00638     OBAtomIterator BeginAtoms()   { return _vatom.begin(); }
00640     OBAtomIterator EndAtoms()    { return _vatom.begin() + NumAtoms() ; }
00642     OBBondIterator BeginBonds()   { return _vbond.begin(); }
00644     OBBondIterator EndBonds()     { return _vbond.begin() + NumBonds() ; }
00646     OBResidueIterator BeginResidues() { return _residue.begin(); }
00648     OBResidueIterator EndResidues()   { return _residue.end();   }
00649 
00652     OBAtom *BeginAtom(OBAtomIterator &i);
00655     OBAtom *NextAtom(OBAtomIterator &i);
00658     OBBond *BeginBond(OBBondIterator &i);
00661     OBBond *NextBond(OBBondIterator &i);
00664     OBResidue *BeginResidue(OBResidueIterator &i)
00665     {
00666       i = _residue.begin();
00667       return((i == _residue.end()) ? NULL:*i);
00668     }
00671     OBResidue *NextResidue(OBResidueIterator &i)
00672     {
00673       ++i;
00674       return((i == _residue.end()) ? NULL:*i);
00675     }
00679     OBInternalCoord *BeginInternalCoord(std::vector<OBInternalCoord*>::iterator &i)
00680     {
00681       i = _internals.begin();
00682       return((i == _internals.end()) ? NULL:*i);
00683     }
00687     OBInternalCoord *NextInternalCoord(std::vector<OBInternalCoord*>::iterator &i)
00688     {
00689       ++i;
00690       return((i == _internals.end()) ? NULL:*i);
00691     }
00693 
00694   };
00695 
00696   // Utility function prototypes
00697   //tokenize and Trim declarations moved to base.h
00698   // Deprecated -- use OBMessageHandler class instead (docs in obutil.cpp)
00699   OBAPI void ThrowError(char *str);
00700   // Deprecated -- use OBMessageHandler class instead (docs in obutil.cpp)
00701   OBAPI void ThrowError(std::string &str);
00703   OBAPI void CartesianToInternal(std::vector<OBInternalCoord*>&,OBMol&);
00705   OBAPI void InternalToCartesian(std::vector<OBInternalCoord*>&,OBMol&);
00706   // Replace the last extension in str with a new one (docs in obutil.cpp)
00707   OBAPI std::string NewExtension(std::string&,char*);
00708 
00709   //global definitions
00711   EXTERN  OBElementTable   etab;
00714   EXTERN  OBTypeTable      ttab;
00716   EXTERN  OBIsotopeTable   isotab;
00718   EXTERN  OBAromaticTyper  aromtyper;
00721   EXTERN  OBAtomTyper      atomtyper;
00723   EXTERN  OBChainsParser   chainsparser;
00725   OBERROR extern  OBMessageHandler obErrorLog;
00727   EXTERN  OBResidueData    resdat;
00728 
00730   namespace detail {
00733     template<typename T, int size = sizeof(T)>
00734     struct max_value
00735     {
00736       static const T result = (static_cast<T>(0xFF) << (size-1)*8) + max_value<T, size-1>::result;
00737     };
00738 
00740     template<typename T>
00741     struct max_value<T, 0>
00742     {
00743       static const T result = 0;
00744     };
00745   }
00746 
00747   // No unique id
00748   static const unsigned long NoId = detail::max_value<unsigned long>::result;
00749 
00750   //Utility Macros
00751 
00752 #ifndef BUFF_SIZE
00753 #define BUFF_SIZE 32768
00754 #endif
00755 
00756 #ifndef EQ
00757 #define EQ(a,b) (!strcmp((a), (b)))
00758 #endif
00759 
00760 #ifndef EQn
00761 #define EQn(a,b,n) (!strncmp((a), (b), (n)))
00762 #endif
00763 
00764 #ifndef SQUARE
00765 #define SQUARE(x) ((x)*(x))
00766 #endif
00767 
00768 #ifndef IsUnsatType
00769 #define IsUnsatType(x)  (EQ(x,"Car") || EQ(x,"C2") || EQ(x,"Sox") || EQ(x,"Sac") || EQ(x,"Pac") || EQ(x,"So2"))
00770 #endif
00771 
00772 #ifndef __KCC
00773   extern "C"
00774   {
00775     OBAPI void  get_rmat(double*,double*,double*,int);
00776     OBAPI void  ob_make_rmat(double mat[3][3],double rmat[9]);
00777     OBAPI void  qtrfit (double *r,double *f,int size,double u[3][3]);
00778     OBAPI double superimpose(double*,double*,int);
00779   }
00780 #else
00781   OBAPI void get_rmat(double*,double*,double*,int);
00782   OBAPI void ob_make_rmat(double mat[3][3],double rmat[9]);
00783   OBAPI void qtrfit (double *r,double *f,int size,double u[3][3]);
00784   OBAPI double superimpose(double*,double*,int);
00785 #endif // __KCC
00786 
00787 //  extern OBMol* (*CreateMolecule) (void);
00788 
00789 } // end namespace OpenBabel
00790 
00791 #endif // OB_MOL_H
00792 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines