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.sourceforge.net/>
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 
00033 #include <vector>
00034 #include <string>
00035 #include <map>
00036 
00037 // Currently includes many headers for 2.x backwards compatibility
00038 // \deprecated -- this will be cleaned up in 3.0 efforts 
00039 //      to improve compile time significantly. 
00040 // Only include necessary headers and class declaration stubs.
00041 #include <openbabel/atom.h>
00042 #include <openbabel/bond.h>
00043 #include <openbabel/base.h>
00044 #include <openbabel/data.h>
00045 #include <openbabel/chains.h>
00046 #include <openbabel/math/vector3.h>
00047 #include <openbabel/bitvec.h>
00048 #include <openbabel/residue.h>
00049 #include <openbabel/ring.h>
00050 #include <openbabel/generic.h>
00051 #include <openbabel/typer.h>
00052 #include <openbabel/oberror.h>
00053 #include <openbabel/obiter.h>
00054 #include <openbabel/internalcoord.h>
00055 
00056 namespace OpenBabel
00057 {
00058 
00059   class OBAtom;
00060   class OBBond;
00061   class OBInternalCoord;
00062 
00063   // Class OBMol
00064   //MOL Property Macros (flags) -- 32+ bits
00066 #define OB_SSSR_MOL              (1<<1)
00068 #define OB_RINGFLAGS_MOL         (1<<2)
00070 #define OB_AROMATIC_MOL          (1<<3)
00072 #define OB_ATOMTYPES_MOL         (1<<4)
00074 #define OB_CHIRALITY_MOL         (1<<5)
00076 #define OB_PCHARGE_MOL           (1<<6)
00078 #define OB_HYBRID_MOL            (1<<8)
00080 #define OB_IMPVAL_MOL            (1<<9)
00082 #define OB_KEKULE_MOL            (1<<10)
00084 #define OB_CLOSURE_MOL           (1<<11)
00086 #define OB_H_ADDED_MOL           (1<<12)
00088 #define OB_PH_CORRECTED_MOL      (1<<13)
00090 #define OB_AROM_CORRECTED_MOL    (1<<14)
00092 #define OB_CHAINS_MOL            (1<<15)
00094 #define OB_TCHARGE_MOL                 (1<<16)
00096 #define OB_TSPIN_MOL             (1<<17)
00097   // flags 18-32 unspecified
00098 #define OB_CURRENT_CONFORMER     -1
00099 
00100   // class introduction in mol.cpp
00101  class OBAPI OBMol: public OBBase
00102   {
00103   protected:
00104     int                           _flags;       
00105     bool                          _autoPartialCharge;
00106     bool                          _autoFormalCharge;
00107     std::string                   _title;       
00108     std::vector<OBAtom*>          _vatom;       
00109     std::vector<OBBond*>          _vbond;       
00110     unsigned short int            _dimension;   
00111     double                        _energy;      
00112     int                           _totalCharge; 
00113     unsigned int                  _totalSpin;   
00114     double                       *_c;             
00115     std::vector<double*>          _vconf;       
00116     unsigned int                  _natoms;      
00117     unsigned int                  _nbonds;      
00118     std::vector<OBResidue*>       _residue;     
00119     std::vector<OBInternalCoord*> _internals;   
00120     unsigned short int            _mod;         
00121 
00122     bool  HasFlag(int flag)    { return((_flags & flag) ? true : false); }
00123     void  SetFlag(int flag)    { _flags |= flag; }
00124 
00126 
00127     void start_kekulize(std::vector <OBAtom*> &cycle, std::vector<int> &electron);
00128     int expand_kekulize(OBAtom *atom1, OBAtom *atom2, std::vector<int> &currentState, std::vector<int> &initState, std::vector<int> &bcurrentState, std::vector<int> &binitState, std::vector<bool> &mark);
00129     int getorden(OBAtom *atom);
00130     void expandcycle(OBAtom *atom, OBBitVec &avisit);
00132 
00133   public:
00134 
00136 
00137 
00138     OBMol();
00140     OBMol(const OBMol &);
00142     virtual ~OBMol();
00144     OBMol &operator=(const OBMol &mol);      
00146     OBMol &operator+=(const OBMol &mol);
00147 
00150     void ReserveAtoms(int natoms)
00151     {
00152       if (natoms && _mod)
00153         _vatom.reserve(natoms);
00154     }
00155     
00158     virtual OBAtom *CreateAtom(void);
00161     virtual OBBond *CreateBond(void);
00164     virtual OBResidue *CreateResidue(void);
00167     virtual void DestroyAtom(OBAtom*);
00170     virtual void DestroyBond(OBBond*);
00173     virtual void DestroyResidue(OBResidue*);
00174 
00177     bool AddAtom(OBAtom&);
00180     bool InsertAtom(OBAtom &);
00188     bool AddBond(int beginIdx, int endIdx, int order, 
00189                  int flags=0,int insertpos=-1);
00192     bool AddBond(OBBond&);
00195     bool AddResidue(OBResidue&);
00196 
00199     virtual OBAtom    *NewAtom();
00202     virtual OBBond    *NewBond();
00204     virtual OBResidue *NewResidue();
00209     bool DeleteAtom(OBAtom*);
00212     bool DeleteBond(OBBond*);
00215     bool DeleteResidue(OBResidue*);
00217 
00219 
00220 
00221 
00222 
00223     virtual void BeginModify(void);
00227     virtual void EndModify(bool nukePerceivedData=true);
00229     int GetMod()           {      return(_mod);    }
00232     void IncrementMod()    {      _mod++;          }
00235     void DecrementMod()    {      _mod--;          }
00237 
00239 
00240 
00241     int          GetFlags()               { return(_flags); }
00243     const char  *GetTitle() const         { return(_title.c_str()); }
00245     unsigned int NumAtoms() const         {  return(_natoms); }
00247     unsigned int NumBonds() const         {  return(_nbonds); }
00249     unsigned int NumHvyAtoms();
00251     unsigned int NumResidues() const      { return(_residue.size()); }
00253     unsigned int NumRotors();
00254     
00257     OBAtom      *GetAtom(int idx);
00260     OBAtom      *GetFirstAtom();
00263     OBBond      *GetBond(int idx);
00266     OBBond      *GetBond(int a, int b);
00267     // The safer version of the above method
00269     OBBond      *GetBond(OBAtom* bgn, OBAtom* end);
00272     OBResidue   *GetResidue(int);
00273     std::vector<OBInternalCoord*> GetInternalCoord();
00275     double       GetTorsion(int,int,int,int);
00277     double       GetTorsion(OBAtom* a,OBAtom* b,OBAtom* c,OBAtom* d);
00280     double GetAngle(OBAtom* a, OBAtom* b, OBAtom* c);
00282     std::string  GetFormula();
00284     std::string  GetSpacedFormula(int ones=0, const char* sp=" ");
00286     double       GetEnergy() const { return(_energy); }
00288     double       GetMolWt();
00290     double       GetExactMass();
00292     int          GetTotalCharge();
00294     unsigned int GetTotalSpinMultiplicity();
00296     unsigned short int GetDimension() const { return _dimension; }
00298     double      *GetCoordinates() { return(_c); }
00300     std::vector<OBRing*> &GetSSSR();
00302     bool AutomaticFormalCharge()   { return(_autoFormalCharge);  }
00304     bool AutomaticPartialCharge()  { return(_autoPartialCharge); }
00306 
00307 
00309 
00310 
00311     void   SetTitle(const char *title);
00313     void   SetTitle(std::string &title);
00315     void   SetFormula(std::string molFormula);
00317     void   SetEnergy(double energy) { _energy = energy; }
00319     void   SetDimension(unsigned short int d) { _dimension = d; }
00321     void   SetTotalCharge(int charge);
00324     void   SetTotalSpinMultiplicity(unsigned int spin);
00327     void   SetInternalCoord(std::vector<OBInternalCoord*> int_coord)
00328     { _internals = int_coord; }
00330     void SetAutomaticFormalCharge(bool val)
00331     { _autoFormalCharge=val;  }
00333     void SetAutomaticPartialCharge(bool val)
00334     { _autoPartialCharge=val; }
00335 
00337     void   SetAromaticPerceived()    { SetFlag(OB_AROMATIC_MOL);    }
00339     void   SetSSSRPerceived()        { SetFlag(OB_SSSR_MOL);        }
00341     void   SetRingAtomsAndBondsPerceived(){SetFlag(OB_RINGFLAGS_MOL);}
00343     void   SetAtomTypesPerceived()   { SetFlag(OB_ATOMTYPES_MOL);   }
00345     void   SetChainsPerceived()      { SetFlag(OB_CHAINS_MOL);      }
00347     void   SetChiralityPerceived()   { SetFlag(OB_CHIRALITY_MOL);   }
00349     void   SetPartialChargesPerceived(){ SetFlag(OB_PCHARGE_MOL);   }
00351     void   SetHybridizationPerceived() { SetFlag(OB_HYBRID_MOL);    }
00353     void   SetImplicitValencePerceived(){ SetFlag(OB_IMPVAL_MOL);   }
00355     void   SetKekulePerceived()      { SetFlag(OB_KEKULE_MOL);      }
00357     void   SetClosureBondsPerceived(){ SetFlag(OB_CLOSURE_MOL);     }
00359     void   SetHydrogensAdded()       { SetFlag(OB_H_ADDED_MOL);     }
00360     void   SetCorrectedForPH()       { SetFlag(OB_PH_CORRECTED_MOL);}
00361     void   SetAromaticCorrected()    { SetFlag(OB_AROM_CORRECTED_MOL);}
00362     void   SetSpinMultiplicityAssigned(){ SetFlag(OB_TSPIN_MOL);    }
00363     void   SetFlags(int flags)       { _flags = flags;              }
00364 
00365     void   UnsetAromaticPerceived()  { _flags &= (~(OB_AROMATIC_MOL));   }
00366     void   UnsetPartialChargesPerceived(){ _flags &= (~(OB_PCHARGE_MOL));}
00367     void   UnsetImplicitValencePerceived(){_flags &= (~(OB_IMPVAL_MOL)); }
00368     void   UnsetHydrogensAdded()       { UnsetFlag(OB_H_ADDED_MOL);     }
00369     void   UnsetFlag(int flag)       { _flags &= (~(flag));              }
00370 
00372 
00373     // Description in transform.cpp (command-line transformations to this molecule)
00374     virtual OBBase*    DoTransformations(const std::map<std::string,std::string>* pOptions);
00375     // Ditto (documentation on transformation options)
00376     static const char* ClassDescription();
00378     bool Clear();
00380     void RenumberAtoms(std::vector<OBAtom*>&);
00382     void ToInertialFrame(int conf, double *rmat);
00384     void ToInertialFrame();
00386     void Translate(const vector3 &v);
00388     void Translate(const vector3 &v, int conf);
00390     void Rotate(const double u[3][3]);
00392     void Rotate(const double m[9]);
00394     void Rotate(const double m[9],int nconf);
00396     void Center();
00398     
00399     bool Kekulize();
00400     bool PerceiveKekuleBonds();
00401 
00402     void NewPerceiveKekuleBonds();
00403 
00406     bool DeleteHydrogens();
00409     bool DeleteHydrogens(OBAtom*);
00412     bool DeleteNonPolarHydrogens();
00415     bool DeleteHydrogen(OBAtom*);
00421     bool AddHydrogens(bool polaronly=false,bool correctForPH=true);
00423     bool AddHydrogens(OBAtom*);
00425     bool AddPolarHydrogens();
00426 
00428     bool StripSalts();
00430     std::vector<OBMol> Separate(int StartIndex=1);
00432     bool ConvertDativeBonds();
00433 
00435     bool CorrectForPH();
00436     // docs in mol.cpp
00437     bool AssignSpinMultiplicity();
00440     vector3 Center(int nconf);
00442     void SetTorsion(OBAtom*,OBAtom*,OBAtom*,OBAtom*,double);
00444 
00446 
00447 
00448     void FindSSSR();
00450     void FindRingAtomsAndBonds();
00452     void FindChiralCenters();
00453     // documented in mol.cpp -- locates all atom indexes which can reach 'end'
00454     void FindChildren(std::vector<int> & children,int bgnIdx,int endIdx);
00455     // documented in mol.cpp -- locates all atoms which can reach 'end'
00456     void FindChildren(std::vector<OBAtom*>& children,OBAtom* bgn,OBAtom* end);
00461     void FindLargestFragment(OBBitVec &frag);
00464     void ContigFragList(std::vector<std::vector<int> >&);
00466     void Align(OBAtom*,OBAtom*,vector3&,vector3&);
00468     void ConnectTheDots();
00470     void PerceiveBondOrders();
00472     void FindAngles();
00474     void FindTorsions();
00475     // documented in mol.cpp: graph-theoretical distance for each atom
00476     bool         GetGTDVector(std::vector<int> &);
00477     // documented in mol.cpp: graph-invariant index for each atom
00478     void         GetGIVector(std::vector<unsigned int> &);
00479     // documented in mol.cpp: calculate symmetry-unique identifiers
00480     void         GetGIDVector(std::vector<unsigned int> &);
00482 
00484 
00485 
00486     bool Has2D();
00488     bool Has3D();
00490     bool HasNonZeroCoords();
00492     bool HasAromaticPerceived()     { return(HasFlag(OB_AROMATIC_MOL)); }
00494     bool HasSSSRPerceived()         { return(HasFlag(OB_SSSR_MOL));     }
00496     bool HasRingAtomsAndBondsPerceived(){return(HasFlag(OB_RINGFLAGS_MOL));}
00498     bool HasAtomTypesPerceived()    { return(HasFlag(OB_ATOMTYPES_MOL));}
00500     bool HasChiralityPerceived()    { return(HasFlag(OB_CHIRALITY_MOL));}
00502     bool HasPartialChargesPerceived() { return(HasFlag(OB_PCHARGE_MOL));}
00504     bool HasHybridizationPerceived() { return(HasFlag(OB_HYBRID_MOL));  }
00506     bool HasImplicitValencePerceived() { return(HasFlag(OB_IMPVAL_MOL));}
00508     bool HasKekulePerceived() { return(HasFlag(OB_KEKULE_MOL));         }
00510     bool HasClosureBondsPerceived() { return(HasFlag(OB_CLOSURE_MOL));  }
00512     bool HasChainsPerceived() { return(HasFlag(OB_CHAINS_MOL));         }
00514     bool HasHydrogensAdded() { return(HasFlag(OB_H_ADDED_MOL));         }
00516     bool HasAromaticCorrected() { return(HasFlag(OB_AROM_CORRECTED_MOL));}
00518     bool IsCorrectedForPH() { return(HasFlag(OB_PH_CORRECTED_MOL));     }
00520     bool HasSpinMultiplicityAssigned() { return(HasFlag(OB_TSPIN_MOL)); }
00522     bool IsChiral();
00524     bool Empty()                       { return(_natoms == 0);          }
00526 
00528 
00529 
00530     int     NumConformers()    { return((_vconf.empty())?0:_vconf.size()); }
00532     void    SetConformers(std::vector<double*> &v);
00534     void    AddConformer(double *f)    {  _vconf.push_back(f);    }
00537     void    SetConformer(int i);
00540     void    CopyConformer(double* c,int nconf);
00542     void    DeleteConformer(int nconf);
00544     double  *GetConformer(int i)       {  return(_vconf[i]);      }
00547     double  *BeginConformer(std::vector<double*>::iterator&i)
00548     { i = _vconf.begin();
00549       return((i == _vconf.end()) ? NULL:*i); }
00552     double  *NextConformer(std::vector<double*>::iterator&i)
00553     { ++i;
00554       return((i == _vconf.end()) ? NULL:*i); }
00556     std::vector<double*> &GetConformers() {   return(_vconf);     }
00558 
00560 
00561 
00562     OBAtomIterator BeginAtoms()   { return _vatom.begin(); }
00564     OBAtomIterator EndAtoms()     { return _vatom.end();   }
00566     OBBondIterator BeginBonds()   { return _vbond.begin(); }
00568     OBBondIterator EndBonds()     { return _vbond.end();   }
00570     OBResidueIterator BeginResidues() { return _residue.begin(); }
00572     OBResidueIterator EndResidues()   { return _residue.end();   }
00573 
00576     OBAtom *BeginAtom(OBAtomIterator &i);
00579     OBAtom *NextAtom(OBAtomIterator &i);
00582     OBBond *BeginBond(OBBondIterator &i);
00585     OBBond *NextBond(OBBondIterator &i);
00588     OBResidue *BeginResidue(OBResidueIterator &i)
00589     {
00590       i = _residue.begin();
00591       return((i == _residue.end()) ? NULL:*i);
00592     }
00595     OBResidue *NextResidue(OBResidueIterator &i)
00596     {
00597       ++i;
00598       return((i == _residue.end()) ? NULL:*i);
00599     }
00603     OBInternalCoord *BeginInternalCoord(std::vector<OBInternalCoord*>::iterator &i)
00604     {
00605       i = _internals.begin();
00606       return((i == _internals.end()) ? NULL:*i);
00607     }
00611     OBInternalCoord *NextInternalCoord(std::vector<OBInternalCoord*>::iterator &i)
00612     {
00613       ++i;
00614       return((i == _internals.end()) ? NULL:*i);
00615     }
00617 
00618   };
00619 
00620   // Utility function prototypes
00621   OBAPI bool tokenize(std::vector<std::string>&, const char *buf, const char *delimstr=" \t\n");
00622   OBAPI bool tokenize(std::vector<std::string>&, std::string&, const char *delimstr=" \t\n", int limit=-1);
00623   // Remove leading and trailing whitespace from a string (docs in tokenst.cpp)
00624   OBAPI std::string& Trim(std::string& txt);
00625   // Deprecated -- use OBMessageHandler class instead (docs in obutil.cpp)
00626   OBAPI void ThrowError(const char *str);
00627   // Deprecated -- use OBMessageHandler class instead (docs in obutil.cpp)
00628   OBAPI void ThrowError(std::string &str);
00630   OBAPI void CartesianToInternal(std::vector<OBInternalCoord*>&,OBMol&);
00632   OBAPI void InternalToCartesian(std::vector<OBInternalCoord*>&,OBMol&);
00633   // Replace the last extension in str with a new one (docs in obutil.cpp)
00634   OBAPI std::string NewExtension(std::string&,char*);
00635 
00636   //global definitions
00638   EXTERN  OBElementTable   etab;
00641   EXTERN  OBTypeTable      ttab;
00643   EXTERN  OBIsotopeTable   isotab;
00645   EXTERN  OBAromaticTyper  aromtyper;
00648   EXTERN  OBAtomTyper      atomtyper;
00650   EXTERN  OBChainsParser   chainsparser;
00652   OBERROR extern  OBMessageHandler obErrorLog;
00654   EXTERN  OBResidueData    resdat;
00655 
00656   //Utility Macros
00657 
00658 #ifndef BUFF_SIZE
00659 #define BUFF_SIZE 32768
00660 #endif
00661 
00662 #ifndef EQ
00663 #define EQ(a,b) (!strcmp((a), (b)))
00664 #endif
00665 
00666 #ifndef EQn
00667 #define EQn(a,b,n) (!strncmp((a), (b), (n)))
00668 #endif
00669 
00670 #ifndef SQUARE
00671 #define SQUARE(x) ((x)*(x))
00672 #endif
00673 
00674 #ifndef IsUnsatType
00675 #define IsUnsatType(x)  (EQ(x,"Car") || EQ(x,"C2") || EQ(x,"Sox") || EQ(x,"Sac") || EQ(x,"Pac") || EQ(x,"So2"))
00676 #endif
00677 
00678 #ifndef __KCC
00679   extern "C"
00680   {
00681     OBAPI void  get_rmat(double*,double*,double*,int);
00682     OBAPI void  ob_make_rmat(double mat[3][3],double rmat[9]);
00683     OBAPI void  qtrfit (double *r,double *f,int size,double u[3][3]);
00684     OBAPI double superimpose(double*,double*,int);
00685   }
00686 #else
00687   OBAPI void get_rmat(double*,double*,double*,int);
00688   OBAPI void ob_make_rmat(double mat[3][3],double rmat[9]);
00689   OBAPI void qtrfit (double *r,double *f,int size,double u[3][3]);
00690   OBAPI double superimpose(double*,double*,int);
00691 #endif // __KCC
00692 
00693 } // end namespace OpenBabel
00694 
00695 #endif // OB_MOL_H
00696