| 1 | tim | 741 | /********************************************************************** | 
| 2 |  |  | grid.h - Handle grids of values. | 
| 3 |  |  |  | 
| 4 |  |  | Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. | 
| 5 |  |  | Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison | 
| 6 |  |  |  | 
| 7 |  |  | This file is part of the Open Babel project. | 
| 8 |  |  | For more information, see <http://openbabel.sourceforge.net/> | 
| 9 |  |  |  | 
| 10 |  |  | This program is free software; you can redistribute it and/or modify | 
| 11 |  |  | it under the terms of the GNU General Public License as published by | 
| 12 |  |  | the Free Software Foundation version 2 of the License. | 
| 13 |  |  |  | 
| 14 |  |  | This program is distributed in the hope that it will be useful, | 
| 15 |  |  | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 16 |  |  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 17 |  |  | GNU General Public License for more details. | 
| 18 |  |  | ***********************************************************************/ | 
| 19 |  |  |  | 
| 20 |  |  | #ifndef OB_GRID_H | 
| 21 |  |  | #define OB_GRID_H | 
| 22 |  |  |  | 
| 23 | gezelter | 751 | #include "config.h" | 
| 24 | tim | 741 |  | 
| 25 |  |  | #if HAVE_IOSTREAM | 
| 26 |  |  | #include <iostream> | 
| 27 |  |  | #elif HAVE_IOSTREAM_H | 
| 28 |  |  | #include <iostream.h> | 
| 29 |  |  | #endif | 
| 30 |  |  |  | 
| 31 |  |  | #include <algorithm> | 
| 32 |  |  | #include <vector> | 
| 33 |  |  | #include <string> | 
| 34 |  |  |  | 
| 35 |  |  | #ifndef OBPolarGrid | 
| 36 |  |  | #define OBPolarGrid 0x01 /* polar interactions? */ | 
| 37 |  |  | #endif //OBPolarGrid | 
| 38 |  |  |  | 
| 39 |  |  | #ifndef OBLipoGrid | 
| 40 |  |  | #define OBLipoGrid 0x02 /* lipophilicity? */ | 
| 41 |  |  | #endif //OBLipoGrid | 
| 42 |  |  |  | 
| 43 |  |  | namespace OpenBabel | 
| 44 |  |  | { | 
| 45 |  |  |  | 
| 46 |  |  | //! A grid for determining the proximity of a given point to atoms in an OBMol | 
| 47 |  |  | class OBAPI OBProxGrid | 
| 48 |  |  | { | 
| 49 |  |  | int _gridtype; | 
| 50 |  |  | int _nxinc,_nyinc,_nzinc,_maxinc; | 
| 51 |  |  | double _xmin,_xmax,_ymin,_ymax,_zmin,_zmax,_inc; | 
| 52 |  |  | std::vector<std::vector<int> > cell; | 
| 53 |  |  |  | 
| 54 |  |  | public: | 
| 55 |  |  |  | 
| 56 |  |  | OBProxGrid(int gridtype=0) | 
| 57 |  |  | { | 
| 58 |  |  | _gridtype=gridtype; | 
| 59 |  |  | } | 
| 60 |  |  | ~OBProxGrid() | 
| 61 |  |  | {} | 
| 62 |  |  | void Setup(OBMol &mol,OBMol &box, double cutoff,double resolution = 0.5); | 
| 63 |  |  | void Setup(OBMol &mol,OBMol &box, double cutoff, | 
| 64 |  |  | std::vector<bool> &use,double resolution = 0.5); | 
| 65 |  |  | std::vector<int> *GetProxVector(double,double,double); | 
| 66 |  |  | std::vector<int> *GetProxVector(double*); | 
| 67 |  |  |  | 
| 68 |  |  | bool LipoGrid() | 
| 69 |  |  | { | 
| 70 |  |  | return((_gridtype&OBLipoGrid) ? true : false); | 
| 71 |  |  | } | 
| 72 |  |  | bool PolarGrid() | 
| 73 |  |  | { | 
| 74 |  |  | return(_gridtype&OBPolarGrid); | 
| 75 |  |  | } | 
| 76 |  |  | void SetGridType(int gridtype) | 
| 77 |  |  | { | 
| 78 |  |  | _gridtype = gridtype; | 
| 79 |  |  | } | 
| 80 |  |  | bool PointIsInBox(double x,double y,double z) | 
| 81 |  |  | { | 
| 82 |  |  | return (x>=_xmin) && (x<=_xmax) && | 
| 83 |  |  | (y>=_ymin) && (y<=_ymax) && | 
| 84 |  |  | (z>=_zmin) && (z<=_zmax); | 
| 85 |  |  | } | 
| 86 |  |  | }; | 
| 87 |  |  |  | 
| 88 |  |  | //! Handle floating-point 3D grids (i.e., charge density around an OBMol) | 
| 89 |  |  | class OBAPI OBFloatGrid | 
| 90 |  |  | { | 
| 91 |  |  | protected: | 
| 92 |  |  | double *_val;             //!< doubleing point values | 
| 93 |  |  | int   *_ival;            //!< for integer values (deprecated) | 
| 94 |  |  | double _midz,_midx,_midy;   //!< center of grid in world coordinates | 
| 95 |  |  | int _ydim,_xdim,_zdim;     //!< grid dimensions | 
| 96 |  |  | double _spacing,_inv_spa;  //!< spacing between grid points and its invers | 
| 97 |  |  | double _xmin,_xmax,_ymin,_ymax,_zmin,_zmax; //!< the min/max values in XYZ axes (i.e., the box) | 
| 98 |  |  | double _halfSpace;         //!< half of the grid spacing */ | 
| 99 |  |  |  | 
| 100 |  |  | public: | 
| 101 |  |  |  | 
| 102 |  |  | OBFloatGrid() : _halfSpace(0.0) | 
| 103 |  |  | { | 
| 104 |  |  | _val=NULL; | 
| 105 |  |  | _ival=NULL; | 
| 106 |  |  | } | 
| 107 |  |  | ~OBFloatGrid() | 
| 108 |  |  | { | 
| 109 |  |  | if (_ival) | 
| 110 |  |  | { | 
| 111 |  |  | delete [] _ival; | 
| 112 |  |  | _ival = NULL; | 
| 113 |  |  | } | 
| 114 |  |  | if (_val) | 
| 115 |  |  | { | 
| 116 |  |  | delete [] _val; | 
| 117 |  |  | _val = NULL; | 
| 118 |  |  | } | 
| 119 |  |  | } | 
| 120 |  |  | //! Initialize the grid using this molecule as a box (plus a padding) | 
| 121 |  |  | //!  with the supplied spacing between points | 
| 122 |  |  | void Init(OBMol &box,double spacing, double pad= 0.0); | 
| 123 |  |  | //! \return whether the supplied XYZ coordinates fall within the box | 
| 124 |  |  | bool PointIsInBox(double x,double y,double z) | 
| 125 |  |  | { | 
| 126 |  |  | return (x>=_xmin) && (x<=_xmax) && | 
| 127 |  |  | (y>=_ymin) && (y<=_ymax) && | 
| 128 |  |  | (z>=_zmin) && (z<=_zmax); | 
| 129 |  |  | } | 
| 130 |  |  | //! \return true if the point falls within the box | 
| 131 |  |  | bool PointIsInBox(double *c) | 
| 132 |  |  | { | 
| 133 |  |  | return (c[0]>=_xmin) && (c[0]<=_xmax) && | 
| 134 |  |  | (c[1]>=_ymin) && (c[1]<=_ymax) && | 
| 135 |  |  | (c[2]>=_zmin) && (c[2]<=_zmax); | 
| 136 |  |  | } | 
| 137 |  |  | double GetXmin() const    { return(_xmin);    } | 
| 138 |  |  | double GetYmin() const    { return(_ymin);    } | 
| 139 |  |  | double GetZmin() const    { return(_zmin);    } | 
| 140 |  |  | double GetXmax() const    { return(_xmax);    } | 
| 141 |  |  | double GetYmax() const    { return(_ymax);    } | 
| 142 |  |  | double GetZmax() const    { return(_zmax);    } | 
| 143 |  |  | void GetMin(double *a) | 
| 144 |  |  | { | 
| 145 |  |  | a[0]=_xmin; | 
| 146 |  |  | a[1]=_ymin; | 
| 147 |  |  | a[2]=_zmin; | 
| 148 |  |  | } | 
| 149 |  |  | void GetMax(double *a) | 
| 150 |  |  | { | 
| 151 |  |  | a[0]=_xmax; | 
| 152 |  |  | a[1]=_ymax; | 
| 153 |  |  | a[2]=_zmax; | 
| 154 |  |  | } | 
| 155 |  |  |  | 
| 156 |  |  | double GetSpacing() const { return(_spacing); } | 
| 157 |  |  | void GetSpacing(double &s) | 
| 158 |  |  | { | 
| 159 |  |  | s=_spacing; | 
| 160 |  |  | } | 
| 161 |  |  | double GetScale() const   { return(_inv_spa); } | 
| 162 |  |  | double GetHalfSpace() const {return(_halfSpace);} | 
| 163 |  |  | int GetXdim() const       { return(_xdim);    } | 
| 164 |  |  | int GetYdim() const       { return(_ydim);    } | 
| 165 |  |  | int GetZdim() const       { return(_zdim);    } | 
| 166 |  |  | void GetDim(int *a) | 
| 167 |  |  | { | 
| 168 |  |  | a[0]=_xdim; | 
| 169 |  |  | a[1]=_ydim; | 
| 170 |  |  | a[2]=_zdim; | 
| 171 |  |  | } | 
| 172 |  |  | vector3 GetMidpointVector() | 
| 173 |  |  | { | 
| 174 |  |  | vector3 v; | 
| 175 |  |  | v.Set(_midx,_midy,_midz); | 
| 176 |  |  | return(v); | 
| 177 |  |  | } | 
| 178 |  |  | double *GetVals()    {        return(_val);    } | 
| 179 |  |  | void SetVals(double *ptr)    {  _val = ptr;    } | 
| 180 |  |  | vector3 Center() | 
| 181 |  |  | { | 
| 182 |  |  | return vector3(_midx,_midy,_midz); | 
| 183 |  |  | } | 
| 184 |  |  |  | 
| 185 |  |  | friend std::ostream& operator<< ( std::ostream&, const OBFloatGrid& ) ; | 
| 186 |  |  | friend std::istream& operator>> ( std::istream&,OBFloatGrid& ) ; | 
| 187 |  |  |  | 
| 188 |  |  | //! \return the value at the given point (rounding as needed) | 
| 189 |  |  | double Inject(double x,double y,double z); | 
| 190 |  |  |  | 
| 191 |  |  | void IndexToCoords(int idx, double &x, double &y, double &z); | 
| 192 |  |  | void CoordsToIndex(int*,double*); | 
| 193 |  |  | int CoordsToIndex(double &x, double &y, double &z); | 
| 194 |  |  | //! \return the interpolated value for the given point | 
| 195 |  |  | double Interpolate(double,double,double); | 
| 196 |  |  | //! \return the interpolated value for the given point and set the dx, dy, dz derivatives | 
| 197 |  |  | double InterpolateDerivatives(double,double,double,double *derivatives); | 
| 198 |  |  | }; | 
| 199 |  |  |  | 
| 200 |  |  | // scoring function used: PLP = Piecewise Linear Potential or ChemScore algorithm | 
| 201 |  |  | typedef enum { Undefined = -1, PLP, ChemScore } score_t; | 
| 202 |  |  |  | 
| 203 |  |  | //! A base class for scoring docking interactions between multiple molecules | 
| 204 |  |  | class OBAPI OBScoreGrid | 
| 205 |  |  | { | 
| 206 |  |  | protected: | 
| 207 |  |  |  | 
| 208 |  |  | score_t gridtype; | 
| 209 |  |  | bool verbose; | 
| 210 |  |  |  | 
| 211 |  |  | public: | 
| 212 |  |  |  | 
| 213 |  |  | double score; | 
| 214 |  |  |  | 
| 215 |  |  | OBScoreGrid(void) | 
| 216 |  |  | { | 
| 217 |  |  | verbose = false; | 
| 218 |  |  | } | 
| 219 |  |  | virtual ~OBScoreGrid(void) | 
| 220 |  |  | {} | 
| 221 |  |  |  | 
| 222 |  |  | void    SetVerbose(bool v) | 
| 223 |  |  | { | 
| 224 |  |  | verbose = v; | 
| 225 |  |  | } | 
| 226 |  |  | void    SetType(score_t type) | 
| 227 |  |  | { | 
| 228 |  |  | gridtype = type; | 
| 229 |  |  | } | 
| 230 |  |  | score_t GetType(void) | 
| 231 |  |  | { | 
| 232 |  |  | return gridtype; | 
| 233 |  |  | } | 
| 234 |  |  |  | 
| 235 |  |  | virtual void   Clear(void) | 
| 236 |  |  | { } | 
| 237 |  |  | virtual double  Eval(double *) | 
| 238 |  |  | { | 
| 239 |  |  | return -1; | 
| 240 |  |  | } | 
| 241 |  |  | virtual double  Eval(OBMol &mol) | 
| 242 |  |  | { | 
| 243 |  |  | return Eval(mol.GetCoordinates()); | 
| 244 |  |  | } | 
| 245 |  |  | virtual void   Init(OBMol &, OBMol &, std::string &, double) | 
| 246 |  |  | {} | 
| 247 |  |  | virtual void   Setup(OBMol &) | 
| 248 |  |  | {} | 
| 249 |  |  | virtual void   Setup(OBMol &, std::vector<int> &) | 
| 250 |  |  | {} | 
| 251 |  |  | virtual void   Setup(std::vector<int> &) | 
| 252 |  |  | {} | 
| 253 |  |  | virtual void   Config(std::string) | 
| 254 |  |  | {} | 
| 255 |  |  | virtual bool   Read(std::string) | 
| 256 |  |  | { | 
| 257 |  |  | return false; | 
| 258 |  |  | } | 
| 259 |  |  | virtual bool   Write(std::string) | 
| 260 |  |  | { | 
| 261 |  |  | return false; | 
| 262 |  |  | } | 
| 263 |  |  | virtual vector3 Center() | 
| 264 |  |  | { | 
| 265 |  |  | return VZero; | 
| 266 |  |  | } | 
| 267 |  |  | virtual vector3 CenterMol(OBMol &) | 
| 268 |  |  | { | 
| 269 |  |  | return VZero; | 
| 270 |  |  | } | 
| 271 |  |  | }; | 
| 272 |  |  |  | 
| 273 |  |  | } // end namespace OpenBabel | 
| 274 |  |  |  | 
| 275 |  |  | #endif // OB_GRID_H | 
| 276 |  |  |  | 
| 277 |  |  | //! \file grid.h | 
| 278 |  |  | //! \brief Handle grids of values. |