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); 00363 void SetInternalCoord(std::vector<OBInternalCoord*> int_coord) 00364 { _internals = int_coord; } 00366 void SetAutomaticFormalCharge(bool val) 00367 { _autoFormalCharge=val; } 00369 void SetAutomaticPartialCharge(bool val) 00370 { _autoPartialCharge=val; } 00371 00373 void SetAromaticPerceived() { SetFlag(OB_AROMATIC_MOL); } 00375 void SetSSSRPerceived() { SetFlag(OB_SSSR_MOL); } 00377 void SetLSSRPerceived() { SetFlag(OB_LSSR_MOL); } 00379 void SetRingAtomsAndBondsPerceived(){SetFlag(OB_RINGFLAGS_MOL);} 00381 void SetAtomTypesPerceived() { SetFlag(OB_ATOMTYPES_MOL); } 00383 void SetRingTypesPerceived() { SetFlag(OB_RINGTYPES_MOL); } 00385 void SetChainsPerceived() { SetFlag(OB_CHAINS_MOL); } 00387 void SetChiralityPerceived() { SetFlag(OB_CHIRALITY_MOL); } 00389 void SetPartialChargesPerceived(){ SetFlag(OB_PCHARGE_MOL); } 00391 void SetHybridizationPerceived() { SetFlag(OB_HYBRID_MOL); } 00393 void SetImplicitValencePerceived(){ SetFlag(OB_IMPVAL_MOL); } 00395 void SetKekulePerceived() { SetFlag(OB_KEKULE_MOL); } 00397 void SetClosureBondsPerceived(){ SetFlag(OB_CLOSURE_MOL); } 00399 void SetHydrogensAdded() { SetFlag(OB_H_ADDED_MOL); } 00400 void SetCorrectedForPH() { SetFlag(OB_PH_CORRECTED_MOL);} 00401 void SetAromaticCorrected() { SetFlag(OB_AROM_CORRECTED_MOL);} 00402 void SetSpinMultiplicityAssigned(){ SetFlag(OB_TSPIN_MOL); } 00403 void SetFlags(int flags) { _flags = flags; } 00404 00405 void UnsetAromaticPerceived() { _flags &= (~(OB_AROMATIC_MOL)); } 00406 void UnsetSSSRPerceived() { _flags &= (~(OB_SSSR_MOL)); } 00407 void UnsetRingTypesPerceived() { _flags &= (~(OB_RINGTYPES_MOL)); } 00408 void UnsetPartialChargesPerceived(){ _flags &= (~(OB_PCHARGE_MOL));} 00409 void UnsetImplicitValencePerceived(){_flags &= (~(OB_IMPVAL_MOL)); } 00410 void UnsetHydrogensAdded() { UnsetFlag(OB_H_ADDED_MOL); } 00411 void UnsetFlag(int flag) { _flags &= (~(flag)); } 00413 00415 00416 // Description in transform.cpp (command-line transformations to this molecule) 00417 virtual OBBase* DoTransformations(const std::map<std::string,std::string>* pOptions,OBConversion* pConv); 00418 // Ditto (documentation on transformation options) 00419 static const char* ClassDescription(); 00421 bool Clear(); 00423 void RenumberAtoms(std::vector<OBAtom*>&); 00425 void RenumberAtoms(std::vector<int>); 00428 void SetCoordinates(double *c); 00430 void ToInertialFrame(int conf, double *rmat); 00432 void ToInertialFrame(); 00434 void Translate(const vector3 &v); 00436 void Translate(const vector3 &v, int conf); 00438 void Rotate(const double u[3][3]); 00440 void Rotate(const double m[9]); 00442 void Rotate(const double m[9],int nconf); 00444 void Center(); 00446 00447 bool Kekulize(); 00448 bool PerceiveKekuleBonds(); 00449 00450 void NewPerceiveKekuleBonds(); 00451 00454 bool DeleteHydrogens(); 00457 bool DeleteHydrogens(OBAtom*); 00460 bool DeleteNonPolarHydrogens(); 00463 bool DeleteHydrogen(OBAtom*); 00470 bool AddHydrogens(bool polaronly=false,bool correctForPH=false, double pH=7.4); 00472 bool AddHydrogens(OBAtom*); 00474 bool AddPolarHydrogens(); 00475 00479 bool StripSalts(int threshold=0); 00481 std::vector<OBMol> Separate(int StartIndex=1); 00483 bool GetNextFragment( OpenBabel::OBMolAtomDFSIter& iter, OBMol& newMol ); 00485 bool ConvertDativeBonds(); 00486 00488 bool CorrectForPH(double pH=7.4); 00489 // docs in mol.cpp 00490 bool AssignSpinMultiplicity(bool NoImplicitH=false); 00492 void SetIsPatternStructure() { SetFlag(OB_PATTERN_STRUCTURE);} 00493 00496 vector3 Center(int nconf); 00502 void SetTorsion(OBAtom*,OBAtom*,OBAtom*,OBAtom*,double ang); 00504 00506 00507 00508 void FindSSSR(); 00510 void FindLSSR(); 00512 void FindRingAtomsAndBonds(); 00515 void FindChiralCenters() { IsChiral(); } 00516 // documented in mol.cpp -- locates all atom indexes which can reach 'end' 00517 void FindChildren(std::vector<int> & children,int bgnIdx,int endIdx); 00518 // documented in mol.cpp -- locates all atoms which can reach 'end' 00519 void FindChildren(std::vector<OBAtom*>& children,OBAtom* bgn,OBAtom* end); 00524 void FindLargestFragment(OBBitVec &frag); 00527 void ContigFragList(std::vector<std::vector<int> >&); 00529 void Align(OBAtom*,OBAtom*,vector3&,vector3&); 00531 void ConnectTheDots(); 00533 void PerceiveBondOrders(); 00535 void FindAngles(); 00537 void FindTorsions(); 00538 // documented in mol.cpp: graph-theoretical distance for each atom 00539 bool GetGTDVector(std::vector<int> &); 00540 // documented in mol.cpp: graph-invariant index for each atom 00541 void GetGIVector(std::vector<unsigned int> &); 00542 // documented in mol.cpp: calculate symmetry-unique identifiers 00543 void GetGIDVector(std::vector<unsigned int> &); 00545 00547 00548 00549 bool Has2D(bool Not3D=false); 00551 bool Has3D(); 00553 bool HasNonZeroCoords(); 00555 bool HasAromaticPerceived() { return(HasFlag(OB_AROMATIC_MOL)); } 00557 bool HasSSSRPerceived() { return(HasFlag(OB_SSSR_MOL)); } 00559 bool HasLSSRPerceived() { return(HasFlag(OB_LSSR_MOL)); } 00561 bool HasRingAtomsAndBondsPerceived(){return(HasFlag(OB_RINGFLAGS_MOL));} 00563 bool HasAtomTypesPerceived() { return(HasFlag(OB_ATOMTYPES_MOL));} 00565 bool HasRingTypesPerceived() { return(HasFlag(OB_RINGTYPES_MOL));} 00567 bool HasChiralityPerceived() { return(HasFlag(OB_CHIRALITY_MOL));} 00569 bool HasPartialChargesPerceived() { return(HasFlag(OB_PCHARGE_MOL));} 00571 bool HasHybridizationPerceived() { return(HasFlag(OB_HYBRID_MOL)); } 00573 bool HasImplicitValencePerceived() { return(HasFlag(OB_IMPVAL_MOL));} 00575 bool HasKekulePerceived() { return(HasFlag(OB_KEKULE_MOL)); } 00577 bool HasClosureBondsPerceived() { return(HasFlag(OB_CLOSURE_MOL)); } 00579 bool HasChainsPerceived() { return(HasFlag(OB_CHAINS_MOL)); } 00581 bool HasHydrogensAdded() { return(HasFlag(OB_H_ADDED_MOL)); } 00583 bool HasAromaticCorrected() { return(HasFlag(OB_AROM_CORRECTED_MOL));} 00585 bool IsCorrectedForPH() { return(HasFlag(OB_PH_CORRECTED_MOL)); } 00587 bool HasSpinMultiplicityAssigned() { return(HasFlag(OB_TSPIN_MOL)); } 00589 bool IsChiral(); 00591 bool Empty() { return(_natoms == 0); } 00593 00595 00596 00597 int NumConformers() { return((_vconf.empty())?0:static_cast<int> (_vconf.size())); } 00599 void SetConformers(std::vector<double*> &v); 00601 void AddConformer(double *f) { _vconf.push_back(f); } 00604 void SetConformer(int i); 00607 void CopyConformer(double* c,int nconf); 00609 void DeleteConformer(int nconf); 00611 double *GetConformer(int i) { return(_vconf[i]); } 00613 void SetEnergies(std::vector<double> &energies); 00615 std::vector<double> GetEnergies(); 00618 double GetEnergy(int ci); 00621 double *BeginConformer(std::vector<double*>::iterator&i) 00622 { i = _vconf.begin(); 00623 return((i == _vconf.end()) ? NULL:*i); } 00626 double *NextConformer(std::vector<double*>::iterator&i) 00627 { ++i; 00628 return((i == _vconf.end()) ? NULL:*i); } 00630 std::vector<double*> &GetConformers() { return(_vconf); } 00632 00634 00635 00636 OBAtomIterator BeginAtoms() { return _vatom.begin(); } 00638 OBAtomIterator EndAtoms() { return _vatom.begin() + NumAtoms() ; } 00640 OBBondIterator BeginBonds() { return _vbond.begin(); } 00642 OBBondIterator EndBonds() { return _vbond.begin() + NumBonds() ; } 00644 OBResidueIterator BeginResidues() { return _residue.begin(); } 00646 OBResidueIterator EndResidues() { return _residue.end(); } 00647 00650 OBAtom *BeginAtom(OBAtomIterator &i); 00653 OBAtom *NextAtom(OBAtomIterator &i); 00656 OBBond *BeginBond(OBBondIterator &i); 00659 OBBond *NextBond(OBBondIterator &i); 00662 OBResidue *BeginResidue(OBResidueIterator &i) 00663 { 00664 i = _residue.begin(); 00665 return((i == _residue.end()) ? NULL:*i); 00666 } 00669 OBResidue *NextResidue(OBResidueIterator &i) 00670 { 00671 ++i; 00672 return((i == _residue.end()) ? NULL:*i); 00673 } 00677 OBInternalCoord *BeginInternalCoord(std::vector<OBInternalCoord*>::iterator &i) 00678 { 00679 i = _internals.begin(); 00680 return((i == _internals.end()) ? NULL:*i); 00681 } 00685 OBInternalCoord *NextInternalCoord(std::vector<OBInternalCoord*>::iterator &i) 00686 { 00687 ++i; 00688 return((i == _internals.end()) ? NULL:*i); 00689 } 00691 00692 }; 00693 00694 // Utility function prototypes 00695 //tokenize and Trim declarations moved to base.h 00696 // Deprecated -- use OBMessageHandler class instead (docs in obutil.cpp) 00697 OBAPI void ThrowError(char *str); 00698 // Deprecated -- use OBMessageHandler class instead (docs in obutil.cpp) 00699 OBAPI void ThrowError(std::string &str); 00701 OBAPI void CartesianToInternal(std::vector<OBInternalCoord*>&,OBMol&); 00703 OBAPI void InternalToCartesian(std::vector<OBInternalCoord*>&,OBMol&); 00704 // Replace the last extension in str with a new one (docs in obutil.cpp) 00705 OBAPI std::string NewExtension(std::string&,char*); 00706 00707 //global definitions 00709 EXTERN OBElementTable etab; 00712 EXTERN OBTypeTable ttab; 00714 EXTERN OBIsotopeTable isotab; 00716 EXTERN OBAromaticTyper aromtyper; 00719 EXTERN OBAtomTyper atomtyper; 00721 EXTERN OBChainsParser chainsparser; 00723 OBERROR extern OBMessageHandler obErrorLog; 00725 EXTERN OBResidueData resdat; 00726 00728 namespace detail { 00731 template<typename T, int size = sizeof(T)> 00732 struct max_value 00733 { 00734 static const T result = (static_cast<T>(0xFF) << (size-1)*8) + max_value<T, size-1>::result; 00735 }; 00736 00738 template<typename T> 00739 struct max_value<T, 0> 00740 { 00741 static const T result = 0; 00742 }; 00743 } 00744 00745 // No unique id 00746 static const unsigned long NoId = detail::max_value<unsigned long>::result; 00747 00748 //Utility Macros 00749 00750 #ifndef BUFF_SIZE 00751 #define BUFF_SIZE 32768 00752 #endif 00753 00754 #ifndef EQ 00755 #define EQ(a,b) (!strcmp((a), (b))) 00756 #endif 00757 00758 #ifndef EQn 00759 #define EQn(a,b,n) (!strncmp((a), (b), (n))) 00760 #endif 00761 00762 #ifndef SQUARE 00763 #define SQUARE(x) ((x)*(x)) 00764 #endif 00765 00766 #ifndef IsUnsatType 00767 #define IsUnsatType(x) (EQ(x,"Car") || EQ(x,"C2") || EQ(x,"Sox") || EQ(x,"Sac") || EQ(x,"Pac") || EQ(x,"So2")) 00768 #endif 00769 00770 #ifndef __KCC 00771 extern "C" 00772 { 00773 OBAPI void get_rmat(double*,double*,double*,int); 00774 OBAPI void ob_make_rmat(double mat[3][3],double rmat[9]); 00775 OBAPI void qtrfit (double *r,double *f,int size,double u[3][3]); 00776 OBAPI double superimpose(double*,double*,int); 00777 } 00778 #else 00779 OBAPI void get_rmat(double*,double*,double*,int); 00780 OBAPI void ob_make_rmat(double mat[3][3],double rmat[9]); 00781 OBAPI void qtrfit (double *r,double *f,int size,double u[3][3]); 00782 OBAPI double superimpose(double*,double*,int); 00783 #endif // __KCC 00784 00785 // extern OBMol* (*CreateMolecule) (void); 00786 00787 } // end namespace OpenBabel 00788 00789 #endif // OB_MOL_H 00790
This file is part of the documentation for Open Babel, version 2.3.