00001 /********************************************************************** 00002 parsmart.h - Daylight SMARTS parser. 00003 00004 Copyright (C) 1998-2001 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_PARSMART_H 00021 #define OB_PARSMART_H 00022 00023 #include <string> 00024 #include <vector> 00025 00026 #include <openbabel/mol.h> 00027 00028 /*==========================*/ 00029 /* SMARTS Data Structures */ 00030 /*==========================*/ 00031 00032 namespace OpenBabel 00033 { 00034 00035 // mark this so that SWIG will not attempt to wrap for scripting languages 00036 00037 #ifndef SWIG 00038 00041 typedef union _AtomExpr { 00042 int type; 00043 struct 00044 { 00045 int type; 00046 int prop; 00047 int value; 00048 } 00049 leaf; 00050 struct 00051 { 00052 int type; 00053 void *recur; 00054 } 00055 recur; 00056 struct 00057 { 00058 int type; 00059 union _AtomExpr *arg; 00060 } 00061 mon; 00062 struct 00063 { 00064 int type; 00065 union _AtomExpr *lft; 00066 union _AtomExpr *rgt; 00067 } 00068 bin; 00069 } AtomExpr; 00070 00071 #define BE_LEAF 0x01 00072 #define BE_ANDHI 0x02 00073 #define BE_ANDLO 0x03 00074 #define BE_NOT 0x04 00075 #define BE_OR 0x05 00076 00077 #define BL_CONST 0x01 00078 #define BL_TYPE 0x02 00079 00080 #define BT_SINGLE 0x01 00081 #define BT_DOUBLE 0x02 00082 #define BT_TRIPLE 0x03 00083 #define BT_AROM 0x04 00084 #define BT_UP 0x05 00085 #define BT_DOWN 0x06 00086 #define BT_UPUNSPEC 0x07 00087 #define BT_DOWNUNSPEC 0x08 00088 #define BT_RING 0x09 00089 00092 typedef union _BondExpr { 00093 int type; 00094 struct 00095 { 00096 int type; 00097 int prop; 00098 int value; 00099 } 00100 leaf; 00101 struct 00102 { 00103 int type; 00104 union _BondExpr *arg; 00105 } 00106 mon; 00107 struct 00108 { 00109 int type; 00110 union _BondExpr *lft; 00111 union _BondExpr *rgt; 00112 } 00113 bin; 00114 } BondExpr; 00115 00118 typedef struct 00119 { 00120 BondExpr *expr; 00121 int src,dst; 00122 int visit; 00123 bool grow; 00124 } 00125 BondSpec; 00126 00129 typedef struct 00130 { 00131 AtomExpr *expr; 00132 int visit; 00133 int part; 00134 int chiral_flag; 00135 int vb; 00136 } 00137 AtomSpec; 00138 00141 typedef struct 00142 { 00143 int aalloc,acount; 00144 int balloc,bcount; 00145 bool ischiral; 00146 AtomSpec *atom; 00147 BondSpec *bond; 00148 int parts; 00149 bool hasExplicitH; 00150 } 00151 Pattern; 00152 #else 00153 // for SWIG, just forward declare that we have some Pattern struct 00154 // (but this is private and not wrapped for scripting languages) 00155 struct Pattern; 00156 #endif 00157 00158 // class introduction in parsmart.cpp 00160 class OBAPI OBSmartsPattern 00161 { 00162 protected: 00163 std::vector<bool> _growbond; 00164 std::vector<std::vector<int> > _mlist; 00165 Pattern *_pat; 00166 std::string _str; 00167 00168 public: 00169 OBSmartsPattern() : _pat(NULL) { } 00170 virtual ~OBSmartsPattern(); 00171 00172 OBSmartsPattern(const OBSmartsPattern& cp): _pat(NULL) 00173 { 00174 *this = cp; 00175 } 00176 OBSmartsPattern& operator=(const OBSmartsPattern& cp) 00177 { 00178 if (_pat) 00179 delete [] _pat; 00180 _pat = NULL; 00181 std::string s = cp._str; 00182 Init(s); 00183 return (*this); 00184 } 00185 00187 00188 00189 00190 bool Init(const char* pattern); 00193 bool Init(const std::string& pattern); 00195 00197 00198 00199 const std::string &GetSMARTS() const { return _str; } 00201 std::string &GetSMARTS() { return _str; } 00202 00204 bool Empty() const { return(_pat == NULL); } 00206 bool IsValid() const { return(_pat != NULL); } 00207 00209 unsigned int NumAtoms() const 00210 { 00211 return _pat ? _pat->acount : 0; 00212 } 00214 unsigned int NumBonds() const 00215 { 00216 return _pat ? _pat->bcount : 0; 00217 } 00218 00224 void GetBond(int& src,int& dst,int& ord,int idx); 00226 int GetAtomicNum(int idx); 00228 int GetCharge(int idx); 00229 00231 int GetVectorBinding(int idx) const 00232 { 00233 return(_pat->atom[idx].vb); 00234 } 00236 00238 00239 00240 00241 00242 00243 bool Match(OBMol &mol, bool single=false); 00244 00245 bool RestrictedMatch(OBMol &mol, std::vector<std::pair<int,int> > &pairs, bool single=false); 00246 00247 bool RestrictedMatch(OBMol &mol, OBBitVec &bv, bool single=false); 00250 unsigned int NumMatches() const 00251 { 00252 return (unsigned int)_mlist.size(); 00253 } 00254 00257 std::vector<std::vector<int> > &GetMapList() 00258 { 00259 return(_mlist); 00260 } 00262 std::vector<std::vector<int> >::iterator BeginMList() 00263 { 00264 return(_mlist.begin()); 00265 } 00267 std::vector<std::vector<int> >::iterator EndMList() 00268 { 00269 return(_mlist.end()); 00270 } 00271 00273 00283 std::vector<std::vector<int> > &GetUMapList(); 00285 00287 void WriteMapList(std::ostream&); 00288 }; 00289 00293 class OBAPI OBSSMatch 00294 { 00295 protected: 00296 bool *_uatoms; 00297 OBMol *_mol; 00298 Pattern *_pat; 00299 std::vector<int> _map; 00300 00301 public: 00302 OBSSMatch(OBMol&,Pattern*); 00303 ~OBSSMatch(); 00304 void Match(std::vector<std::vector<int> > &v, int bidx=-1); 00305 }; 00306 00307 OBAPI void SmartsLexReplace(std::string &, 00308 std::vector<std::pair<std::string,std::string> > &); 00309 00310 } // end namespace OpenBabel 00311 00312 #endif // OB_PARSMART_H 00313
This file is part of the documentation for Open Babel, version 2.2.0.