| 1 | /********************************************************************** | 
| 2 | data.h - Global data and resource file parsers. | 
| 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_DATA_H | 
| 21 | #define OB_DATA_H | 
| 22 |  | 
| 23 | #include "babelconfig.hpp" | 
| 24 |  | 
| 25 | #include <stdio.h> | 
| 26 |  | 
| 27 | #if HAVE_IOSTREAM | 
| 28 | #include <iostream> | 
| 29 | #elif HAVE_IOSTREAM_H | 
| 30 | #include <iostream.h> | 
| 31 | #endif | 
| 32 |  | 
| 33 | #if HAVE_FSTREAM | 
| 34 | #include <fstream> | 
| 35 | #elif HAVE_FSTREAM_H | 
| 36 | #include <fstream.h> | 
| 37 | #endif | 
| 38 |  | 
| 39 | #include <vector> | 
| 40 | #include <string> | 
| 41 |  | 
| 42 | namespace OpenBabel | 
| 43 | { | 
| 44 |  | 
| 45 | class OBElement; | 
| 46 | class OBAtom; | 
| 47 | class OBElementTable; | 
| 48 |  | 
| 49 | class OBMol; | 
| 50 | class OBBitVec; | 
| 51 |  | 
| 52 | //! \brief Base data table class, handles reading data files | 
| 53 | //! | 
| 54 | //! Base data table class--reads ASCII data files in various formats | 
| 55 | //! -# Checks for the environment variable _envvar (defaults to "BABEL_DATADIR") | 
| 56 | //!     - Tries the _subdir directory if defined (def. "data") and then the main directory | 
| 57 | //! -# Checks for the directory _dir (def. determined by the build environment) | 
| 58 | //!     - Tries the subdirectory corresponding to this version, then the main directory | 
| 59 | //! -# Reverts to the compiled-in default data | 
| 60 | class OBAPI OBGlobalDataBase | 
| 61 | { | 
| 62 | protected: | 
| 63 | bool         _init;         //!< whether the data been read already | 
| 64 | const char  *_dataptr;      //!< default data table if file is unreadable | 
| 65 | std::string  _filename;     //!< file to search for | 
| 66 | std::string  _dir;          //!< data directory for file if _envvar fails | 
| 67 | std::string  _subdir;       //!< subdirectory (if using environment variable) | 
| 68 | std::string  _envvar;       //!< environment variable to check first | 
| 69 |  | 
| 70 | public: | 
| 71 | //! Constructor | 
| 72 | OBGlobalDataBase() | 
| 73 | { | 
| 74 | _init = false; | 
| 75 | _dataptr = (char*)NULL; | 
| 76 | } | 
| 77 | //! Destructor | 
| 78 | virtual ~OBGlobalDataBase()                  {} | 
| 79 | //! Read in the data file, falling back as needed | 
| 80 | void  Init(); | 
| 81 | //! \return the size of the database (for error checking) | 
| 82 | virtual unsigned int GetSize()                 { return 0;} | 
| 83 | //! Set the directory before calling Init() | 
| 84 | void  SetReadDirectory(char *dir)            { _dir = dir;    } | 
| 85 | //! Set the environment variable to use before calling Init() | 
| 86 | void  SetEnvironmentVariable(char *var)      { _envvar = var; } | 
| 87 | //! Specified by particular table classes (parses an individual data line) | 
| 88 | virtual void ParseLine(const char*)          {} | 
| 89 | }; | 
| 90 |  | 
| 91 | //! \brief Individual element data type | 
| 92 | //! | 
| 93 | //! Stores a variety of data about an individual element | 
| 94 | class OBAPI OBElement | 
| 95 | { | 
| 96 | int _num; | 
| 97 | char _symbol[3]; | 
| 98 | std::string _name; | 
| 99 | double _Rcov,_Rvdw,_mass,_elNeg,_ionize,_elAffinity; | 
| 100 | double _red, _green, _blue; | 
| 101 | int _maxbonds; | 
| 102 | public: | 
| 103 | OBElement()    {} | 
| 104 | OBElement(int num, const char *sym, double rcov, double rvdw, | 
| 105 | int maxbo, double mass, double elNeg, double ionize, | 
| 106 | double elAffin, double red, double green, double blue, | 
| 107 | std::string name) : | 
| 108 | _num(num), _name(name), _Rcov(rcov), _Rvdw(rvdw), _mass(mass), | 
| 109 | _elNeg(elNeg), _ionize(ionize), _elAffinity(elAffin), | 
| 110 | _red(red), _green(green), _blue(blue), | 
| 111 | _maxbonds(maxbo) | 
| 112 | { | 
| 113 | strncpy(_symbol, sym, 3); | 
| 114 | } | 
| 115 |  | 
| 116 | //! \return the atomic number of this element | 
| 117 | int GetAtomicNum()         {       return(_num);    } | 
| 118 | //! \return the atomic symbol for this element | 
| 119 | char *GetSymbol()          {       return(_symbol); } | 
| 120 | //! \return the covalent radius of this element | 
| 121 | double GetCovalentRad()    {       return(_Rcov);   } | 
| 122 | //! \return the van der Waals radius of this element | 
| 123 | double GetVdwRad()         {       return(_Rvdw);   } | 
| 124 | //! \return the standard atomic mass for this element (in amu) | 
| 125 | double GetMass()           {       return(_mass);   } | 
| 126 | //! \return the maximum expected number of bonds to this element | 
| 127 | int GetMaxBonds()          {       return(_maxbonds);} | 
| 128 | //! \return the Pauling electronegativity for this element | 
| 129 | double GetElectroNeg()     {       return(_elNeg);  } | 
| 130 | //! \return the ionization potential (in eV) of this element | 
| 131 | double GetIonization()     {       return(_ionize);  } | 
| 132 | //! \return the electron affinity (in eV) of this element | 
| 133 | double GetElectronAffinity(){      return(_elAffinity);  } | 
| 134 | //! \return the name of this element (in English) | 
| 135 | std::string GetName()      {       return(_name);    } | 
| 136 | //! \return the red component of this element's default visualization color | 
| 137 | double GetRed()            {       return(_red);     } | 
| 138 | //! \return the green component of this element's default color | 
| 139 | double GetGreen()          {       return(_green);   } | 
| 140 | //! \return the blue component of this element's default color | 
| 141 | double GetBlue()           {       return(_blue);    } | 
| 142 | }; | 
| 143 |  | 
| 144 | // class introduction in data.cpp | 
| 145 | class OBAPI OBElementTable : public OBGlobalDataBase | 
| 146 | { | 
| 147 | std::vector<OBElement*> _element; | 
| 148 |  | 
| 149 | public: | 
| 150 |  | 
| 151 | OBElementTable(void); | 
| 152 | ~OBElementTable(); | 
| 153 |  | 
| 154 | void  ParseLine(const char*); | 
| 155 |  | 
| 156 | //! \return the number of elements in the periodic table | 
| 157 | unsigned int                GetNumberOfElements(); | 
| 158 | unsigned int GetSize() { return GetNumberOfElements(); } | 
| 159 |  | 
| 160 | //! \deprecated Does not properly handle 'D' or 'T' hydrogen isotopes | 
| 161 | int   GetAtomicNum(const char *); | 
| 162 | //! \return the atomic number matching the element symbol passed | 
| 163 | //! or 0 if not defined. For 'D' or 'T' hydrogen isotopes, will return | 
| 164 | //! a value in the second argument | 
| 165 | int   GetAtomicNum(const char *, int &iso); | 
| 166 | //! \return the element symbol matching the atomic number passed | 
| 167 | char *GetSymbol(int); | 
| 168 | //! \return the van der Waals radius for this atomic number | 
| 169 | double GetVdwRad(int); | 
| 170 | //! \return the covalent radius for this atomic number | 
| 171 | double GetCovalentRad(int); | 
| 172 | //! \return the average atomic mass for this element. | 
| 173 | //! For exact isotope masses, use OpenBabel::OBIsotopeTable | 
| 174 | double GetMass(int); | 
| 175 | //! \return a "corrected" bonding radius based on the hybridization. | 
| 176 | //! Scales the covalent radius by 0.95 for sp2 and 0.90 for sp hybrids | 
| 177 | double CorrectedBondRad(int,int = 3); // atomic #, hybridization | 
| 178 | //! \return a "corrected" vdW radius based on the hybridization. | 
| 179 | //! Scales the van der Waals radius by 0.95 for sp2 and 0.90 for sp hybrids | 
| 180 | double CorrectedVdwRad(int,int = 3); // atomic #, hybridization | 
| 181 | //! \return the maximum expected number of bonds to this element | 
| 182 | int GetMaxBonds(int); | 
| 183 | //! \return the Pauling electronegativity for this element | 
| 184 | double GetElectroNeg(int); | 
| 185 | //! \return the ionization potential (in eV) for this element | 
| 186 | double GetIonization(int); | 
| 187 | //! \return the electron affinity (in eV) for this element | 
| 188 | double GetElectronAffinity(int); | 
| 189 | //! \return a vector with red, green, blue color values for this element | 
| 190 | std::vector<double> GetRGB(int); | 
| 191 | //! \return the name of this element | 
| 192 | std::string GetName(int); | 
| 193 | }; | 
| 194 |  | 
| 195 | // class introduction in data.cpp | 
| 196 | class OBAPI OBIsotopeTable : public OBGlobalDataBase | 
| 197 | { | 
| 198 | std::vector<std::vector<std::pair <unsigned int, double> > > _isotopes; | 
| 199 |  | 
| 200 | public: | 
| 201 |  | 
| 202 | OBIsotopeTable(void); | 
| 203 | ~OBIsotopeTable()    {} | 
| 204 |  | 
| 205 | //! \return the number of elements in the isotope table | 
| 206 | unsigned int GetSize() { return _isotopes.size(); } | 
| 207 |  | 
| 208 | void        ParseLine(const char*); | 
| 209 | //! \return the exact masss of the isotope | 
| 210 | //!   (or by default (i.e. "isotope 0") the most abundant isotope) | 
| 211 | double      GetExactMass(const unsigned int atomicNum, | 
| 212 | const unsigned int isotope = 0); | 
| 213 | }; | 
| 214 |  | 
| 215 | // class introduction in data.cpp | 
| 216 | class OBAPI OBTypeTable : public OBGlobalDataBase | 
| 217 | { | 
| 218 | int    _linecount; | 
| 219 | unsigned int    _ncols,_nrows; | 
| 220 | int             _from,_to; | 
| 221 | std::vector<std::string> _colnames; | 
| 222 | std::vector<std::vector<std::string> > _table; | 
| 223 |  | 
| 224 | public: | 
| 225 |  | 
| 226 | OBTypeTable(void); | 
| 227 | ~OBTypeTable() {} | 
| 228 |  | 
| 229 | void ParseLine(const char*); | 
| 230 |  | 
| 231 | //! \return the number of atom types in the translation table | 
| 232 | unsigned int GetSize() { return _table.size(); } | 
| 233 |  | 
| 234 | //! Set the initial atom type to be translated | 
| 235 | bool SetFromType(const char*); | 
| 236 | //! Set the destination atom type for translation | 
| 237 | bool SetToType(const char*); | 
| 238 | //! Translate atom types | 
| 239 | bool Translate(char *to, const char *from); // to, from | 
| 240 | //! Translate atom types | 
| 241 | bool Translate(std::string &to, const std::string &from); // to, from | 
| 242 |  | 
| 243 | //! \return the initial atom type to be translated | 
| 244 | std::string GetFromType(); | 
| 245 | //! \return the destination atom type for translation | 
| 246 | std::string GetToType(); | 
| 247 | }; | 
| 248 |  | 
| 249 | //! \brief Table of common biomolecule residues (for PDB or other files). | 
| 250 | //!   Can assign atom types and bond orders for arbitrary residues | 
| 251 | class OBAPI OBResidueData : public OBGlobalDataBase | 
| 252 | { | 
| 253 | int                                               _resnum; | 
| 254 | std::vector<std::string>                          _resname; | 
| 255 | std::vector<std::vector<std::string> >            _resatoms; | 
| 256 | std::vector<std::vector<std::pair<std::string,int> > > _resbonds; | 
| 257 |  | 
| 258 | //variables used only temporarily for parsing resdata.txt | 
| 259 | std::vector<std::string>                          _vatmtmp; | 
| 260 | std::vector<std::pair<std::string,int> >          _vtmp; | 
| 261 | public: | 
| 262 |  | 
| 263 | OBResidueData(); | 
| 264 | void ParseLine(const char*); | 
| 265 |  | 
| 266 | //! \return the number of residues in the table | 
| 267 | unsigned int GetSize() { return _resname.size(); } | 
| 268 |  | 
| 269 | //! Sets the table to access the residue information for a specified | 
| 270 | //!  residue name | 
| 271 | //! \return whether this residue name is in the table | 
| 272 | bool SetResName(const std::string &); | 
| 273 | //! \return the bond order for the bond specified in the current residue | 
| 274 | //! \deprecated Easier to use the two-argument form | 
| 275 | int  LookupBO(const std::string &); | 
| 276 | //! \return the bond order for the bond specified between the two specified | 
| 277 | //! atom labels | 
| 278 | int  LookupBO(const std::string &, const std::string&); | 
| 279 | //! Look up the atom type and hybridization for the atom label specified | 
| 280 | //! in the first argument for the current residue | 
| 281 | //! \return whether the atom label specified is found in the current residue | 
| 282 | bool LookupType(const std::string &,std::string&,int&); | 
| 283 | //! Assign bond orders, atom types and residues for the supplied OBMol | 
| 284 | //! based on the residue information assigned to atoms | 
| 285 | //! \deprecated second OBBitVec argument is ignored | 
| 286 | bool AssignBonds(OBMol &,OBBitVec &); | 
| 287 | }; | 
| 288 |  | 
| 289 | // Used by other code for reading files | 
| 290 | #ifdef WIN32 | 
| 291 | #define FILE_SEP_CHAR "\\" | 
| 292 | #else | 
| 293 | #define FILE_SEP_CHAR "/" | 
| 294 | #endif | 
| 295 |  | 
| 296 | } // end namespace OpenBabel | 
| 297 |  | 
| 298 | #endif //DATA_H | 
| 299 |  | 
| 300 | //! \file data.h | 
| 301 | //! \brief Global data and resource file parsers. |