rotor.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 rotor.h - Rotate torsional according to rotor rules.
00003  
00004 Copyright (C) 1998-2000 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_ROTOR_H
00021 #define OB_ROTOR_H
00022 
00023 #include "parsmart.h"
00024 #include "typer.h"
00025 
00026 namespace OpenBabel
00027 {
00028 
00029 class OBRotor;
00030 class OBRotorList;
00031 class OBRotorRule;
00032 class OBRotorRules;
00033 
00040 class OBAPI OBRotorRule
00041 {
00042     int              _ref[4];
00043     double            _delta;
00044     std::string           _s;
00045     OBSmartsPattern* _sp;
00046     std::vector<double>    _vals;
00047 public:
00048 
00049     OBRotorRule(char *buffer,int ref[4],std::vector<double> &vals,double d):
00050        _delta(d), _s(buffer), _vals(vals)
00051     {
00052         _sp = new OBSmartsPattern;
00053         _sp->Init(buffer);
00054         memcpy(_ref,ref,sizeof(int)*4);
00055     }
00056     ~OBRotorRule()
00057     {
00058         if (_sp)
00059         {
00060             delete _sp;
00061             _sp = NULL;
00062         }
00063     }
00064 
00065     bool    IsValid()    {        return(_sp->IsValid());       }
00066     void    GetReferenceAtoms(int ref[4]) { memcpy(ref,_ref,sizeof(int)*4); }
00067     void    SetDelta(double d)    {       _delta = d;           }
00068     double  GetDelta()            {       return(_delta);       }
00069     std::string  &GetSmartsString(){      return(_s);           }
00070     std::vector<double>   &GetTorsionVals()    { return(_vals); }
00071     OBSmartsPattern *GetSmartsPattern() {  return(_sp);         }
00072 };
00073 
00075 class OBAPI OBRotorRules : public OBGlobalDataBase
00076 {
00077     bool                 _quiet;
00078     std::vector<OBRotorRule*> _vr;
00079     std::vector<double>        _sp3sp3;
00080     std::vector<double>        _sp3sp2;
00081     std::vector<double>        _sp2sp2;
00082 public:
00083     OBRotorRules();
00084     ~OBRotorRules();
00085 
00086     void ParseLine(const char*);
00088     unsigned int GetSize()                 { return _vr.size();}
00089 
00091     void SetFilename(std::string &s)       { _filename = s;    }
00092 
00093     void GetRotorIncrements(OBMol&,OBBond*,int [4],std::vector<double>&,double &delta);
00094     void Quiet()                           { _quiet=true;      }
00095 };
00096 
00098 class OBAPI OBRotor
00099 {
00100     int _idx,_ref[4];
00101     int *_rotatoms,_size,_numcoords;
00102     double _delta;
00103     double _imag,_refang;
00104     OBBond *_bond;
00105     std::vector<int> _torsion;
00106     OBBitVec _fixedatoms,_evalatoms;
00107     std::vector<double> _res;  
00108     std::vector<double> _invmag;
00109     std::vector<std::vector<double> > _sn,_cs,_t;
00110 public:
00111     OBRotor();
00112     ~OBRotor()
00113     {
00114         if (_rotatoms)
00115             delete [] _rotatoms;
00116     }
00117     int     Size()
00118     {
00119         return((_res.empty())?0:_res.size());
00120     }
00121     int     GetIdx() const
00122     {
00123         return(_idx);
00124     }
00125     void    SetNumCoords(int nc)
00126     {
00127         _numcoords = nc;
00128     }
00129     void    SetBond(OBBond *bond)
00130     {
00131         _bond = bond;
00132     }
00133     void    SetEvalAtoms(OBBitVec &bv)
00134     {
00135         _evalatoms = bv;
00136     }
00137     void    SetDihedralAtoms(std::vector<int> &vi)
00138     {
00139         _torsion = vi;
00140     }
00141     void    SetDelta(double d)
00142     {
00143         _delta = d;
00144     }
00145     void    SetDihedralAtoms(int ref[4]);
00146     void    SetRotAtoms(std::vector<int>&);
00147     inline void SetToAngle(double *c,double setang)
00148     {
00149         double dx,dy,dz,sn,cs,t,ang,mag;
00150         ang = setang - CalcTorsion(c);
00151         if (fabs(ang) < 1e-5)
00152             return;
00153 
00154         sn = sin(ang);
00155         cs = cos(ang);
00156         t = 1 - cs;
00157         dx = c[_torsion[1]]   - c[_torsion[2]];
00158         dy = c[_torsion[1]+1] - c[_torsion[2]+1];
00159         dz = c[_torsion[1]+2] - c[_torsion[2]+2];
00160         mag = sqrt(SQUARE(dx) + SQUARE(dy) + SQUARE(dz));
00161         Set(c,sn,cs,t,1.0/mag);
00162     }
00163     void    SetRotor(double *,int,int prev=-1);
00164     void    Set(double*,int);
00165     void    Precompute(double*);
00166     void    Set(double *c,int ridx,int cidx)
00167     {
00168         Set(c,_sn[cidx][ridx],_cs[cidx][ridx],_t[cidx][ridx],_invmag[cidx]);
00169     }
00170     void    Set(double*,double,double,double,double);
00171     void    Precalc(std::vector<double*>&);
00172     void    SetIdx(int idx)
00173     {
00174         _idx = idx;
00175     }
00176     void    SetFixedAtoms(OBBitVec &bv)
00177     {
00178         _fixedatoms = bv;
00179     }
00180     void    SetTorsionValues(std::vector<double> &tmp)
00181     {
00182         _res = tmp;
00183     }
00184     void    RemoveSymTorsionValues(int);
00185     void    GetDihedralAtoms(int ref[4])
00186     {
00187         for (int i=0;i<4;i++)
00188             ref[i]=_ref[i];
00189     }
00190     void    *GetRotAtoms()
00191     {
00192         return(_rotatoms);
00193     }
00194     double   CalcTorsion(double *);
00195     double   CalcBondLength(double*);
00196     double   GetDelta()
00197     {
00198         return(_delta);
00199     }
00200     OBBond *GetBond()
00201     {
00202         return(_bond);
00203     }
00204     std::vector<int> &GetDihedralAtoms()
00205     {
00206         return(_torsion);
00207     }
00208     std::vector<double> &GetResolution()
00209     {
00210         return(_res);
00211     }
00212     std::vector<double>::iterator BeginTorIncrement()
00213     {
00214         return(_res.begin());
00215     }
00216     std::vector<double>::iterator EndTorIncrement()
00217     {
00218         return(_res.end());
00219     }
00220     OBBitVec &GetEvalAtoms()
00221     {
00222         return(_evalatoms);
00223     }
00224     OBBitVec &GetFixedAtoms()
00225     {
00226         return(_fixedatoms);
00227     }
00228 };
00229 
00231 class OBAPI OBRotorList
00232 {
00233     bool _quiet,_removesym;
00234     OBBitVec _fix;
00235     OBRotorRules _rr;
00236     std::vector<int> _dffv;         
00237     std::vector<OBRotor*> _rotor;
00238     std::vector<std::pair<OBSmartsPattern*,std::pair<int,int> > > _vsym2;
00239     std::vector<std::pair<OBSmartsPattern*,std::pair<int,int> > > _vsym3;
00240 public:
00241     OBRotorList();
00242     ~OBRotorList();
00243 
00244     int    Size()
00245     {
00246         return((_rotor.empty()) ? 0: _rotor.size());
00247     }
00248     void   Init(std::string &fname)
00249     {
00250         _rr.SetFilename(fname);
00251         _rr.Init();
00252     }
00253     void   SetFixAtoms(OBBitVec &fix)
00254     {
00255         _fix = fix;
00256     }
00257     void   SetQuiet()
00258     {
00259         _quiet=true;
00260         _rr.Quiet();
00261     }
00262     void   IgnoreSymmetryRemoval()
00263     {
00264         _removesym=false;
00265     }
00266     void   Clear();
00267     void   RemoveSymVals(OBMol&);
00268     void   SetRotAtomsByFix(OBMol&);
00269     bool   SetRotAtoms(OBMol&);
00270     bool   Setup(OBMol &);
00271     bool   FindRotors(OBMol &);
00272     bool   IdentifyEvalAtoms(OBMol &);
00273     bool   SetEvalAtoms(OBMol&);
00274     bool   AssignTorVals(OBMol &);
00275     bool   IsFixedBond(OBBond*);
00276     bool   HasFixedAtoms()
00277     {
00278         return(!_fix.Empty());
00279     }
00280     OBRotor *BeginRotor(std::vector<OBRotor*>::iterator &i)
00281     {
00282         i = _rotor.begin();
00283         return((i ==_rotor.end()) ? NULL:*i);
00284     }
00285     OBRotor *NextRotor(std::vector<OBRotor*>::iterator &i)
00286     {
00287         i++;
00288         return((i ==_rotor.end()) ? NULL:*i);
00289     }
00290     std::vector<OBRotor*>::iterator BeginRotors()
00291     {
00292         return(_rotor.begin());
00293     }
00294     std::vector<OBRotor*>::iterator EndRotors()
00295     {
00296         return(_rotor.end());
00297     }
00298 };
00299 
00300 } // end namespace OpenBabel
00301 
00302 #ifndef SQUARE
00303 #define SQUARE(x) ((x)*(x))
00304 #endif
00305 
00306 #endif // OB_ROTOR_H
00307