| 1 |
/********************************************************************** |
| 2 |
parsmart.h - Daylight SMARTS parser. |
| 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_PARSMART_H |
| 21 |
#define OB_PARSMART_H |
| 22 |
|
| 23 |
#include <string> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "mol.hpp" |
| 27 |
|
| 28 |
/*==========================*/ |
| 29 |
/* SMARTS Data Structures */ |
| 30 |
/*==========================*/ |
| 31 |
|
| 32 |
#define AE_LEAF 0x01 |
| 33 |
#define AE_RECUR 0x02 |
| 34 |
#define AE_NOT 0x03 |
| 35 |
#define AE_ANDHI 0x04 |
| 36 |
#define AE_OR 0x05 |
| 37 |
#define AE_ANDLO 0x06 |
| 38 |
|
| 39 |
#define AL_CONST 0x01 |
| 40 |
#define AL_MASS 0x02 |
| 41 |
#define AL_AROM 0x03 |
| 42 |
#define AL_ELEM 0x04 |
| 43 |
#define AL_HCOUNT 0x05 |
| 44 |
#define AL_NEGATIVE 0x06 |
| 45 |
#define AL_POSITIVE 0x07 |
| 46 |
#define AL_CONNECT 0x08 |
| 47 |
#define AL_DEGREE 0x09 |
| 48 |
#define AL_IMPLICIT 0x0a |
| 49 |
#define AL_RINGS 0x0b |
| 50 |
#define AL_SIZE 0x0c |
| 51 |
#define AL_VALENCE 0x0d |
| 52 |
#define AL_CHIRAL 0x0e |
| 53 |
#define AL_HYB 0x0f |
| 54 |
#define AL_CLOCKWISE 1 |
| 55 |
#define AL_ANTICLOCKWISE 2 |
| 56 |
|
| 57 |
namespace OpenBabel |
| 58 |
{ |
| 59 |
|
| 60 |
//! \brief A SMARTS parser internal atomic expression |
| 61 |
typedef union _AtomExpr { |
| 62 |
int type; |
| 63 |
struct |
| 64 |
{ |
| 65 |
int type; |
| 66 |
int prop; |
| 67 |
int value; |
| 68 |
} |
| 69 |
leaf; |
| 70 |
struct |
| 71 |
{ |
| 72 |
int type; |
| 73 |
void *recur; |
| 74 |
} |
| 75 |
recur; |
| 76 |
struct |
| 77 |
{ |
| 78 |
int type; |
| 79 |
union _AtomExpr *arg; |
| 80 |
} |
| 81 |
mon; |
| 82 |
struct |
| 83 |
{ |
| 84 |
int type; |
| 85 |
union _AtomExpr *lft; |
| 86 |
union _AtomExpr *rgt; |
| 87 |
} |
| 88 |
bin; |
| 89 |
} AtomExpr; |
| 90 |
|
| 91 |
#define BE_LEAF 0x01 |
| 92 |
#define BE_ANDHI 0x02 |
| 93 |
#define BE_ANDLO 0x03 |
| 94 |
#define BE_NOT 0x04 |
| 95 |
#define BE_OR 0x05 |
| 96 |
|
| 97 |
#define BL_CONST 0x01 |
| 98 |
#define BL_TYPE 0x02 |
| 99 |
|
| 100 |
#define BT_SINGLE 0x01 |
| 101 |
#define BT_DOUBLE 0x02 |
| 102 |
#define BT_TRIPLE 0x03 |
| 103 |
#define BT_AROM 0x04 |
| 104 |
#define BT_UP 0x05 |
| 105 |
#define BT_DOWN 0x06 |
| 106 |
#define BT_UPUNSPEC 0x07 |
| 107 |
#define BT_DOWNUNSPEC 0x08 |
| 108 |
#define BT_RING 0x09 |
| 109 |
|
| 110 |
//! \brief A SMARTS parser internal bond expression |
| 111 |
typedef union _BondExpr { |
| 112 |
int type; |
| 113 |
struct |
| 114 |
{ |
| 115 |
int type; |
| 116 |
int prop; |
| 117 |
int value; |
| 118 |
} |
| 119 |
leaf; |
| 120 |
struct |
| 121 |
{ |
| 122 |
int type; |
| 123 |
union _BondExpr *arg; |
| 124 |
} |
| 125 |
mon; |
| 126 |
struct |
| 127 |
{ |
| 128 |
int type; |
| 129 |
union _BondExpr *lft; |
| 130 |
union _BondExpr *rgt; |
| 131 |
} |
| 132 |
bin; |
| 133 |
} BondExpr; |
| 134 |
|
| 135 |
//! \brief A SMARTS parser internal bond specification |
| 136 |
typedef struct |
| 137 |
{ |
| 138 |
BondExpr *expr; |
| 139 |
int src,dst; |
| 140 |
int visit; |
| 141 |
bool grow; |
| 142 |
} |
| 143 |
BondSpec; |
| 144 |
|
| 145 |
//! \brief A SMARTS parser internal bond specification |
| 146 |
typedef struct |
| 147 |
{ |
| 148 |
AtomExpr *expr; |
| 149 |
int visit; |
| 150 |
int part; |
| 151 |
int chiral_flag; |
| 152 |
int vb; |
| 153 |
} |
| 154 |
AtomSpec; |
| 155 |
|
| 156 |
//! \brief A SMARTS parser internal pattern |
| 157 |
typedef struct |
| 158 |
{ |
| 159 |
int aalloc,acount; |
| 160 |
int balloc,bcount; |
| 161 |
bool ischiral; |
| 162 |
AtomSpec *atom; |
| 163 |
BondSpec *bond; |
| 164 |
int parts; |
| 165 |
} |
| 166 |
Pattern; |
| 167 |
|
| 168 |
// class introduction in parsmart.cpp |
| 169 |
//! \brief SMARTS (SMiles ARbitrary Target Specification) substructure searching |
| 170 |
class OBAPI OBSmartsPattern |
| 171 |
{ |
| 172 |
protected: |
| 173 |
std::vector<bool> _growbond; |
| 174 |
std::vector<std::vector<int> > _mlist; |
| 175 |
Pattern *_pat; |
| 176 |
std::string _str; |
| 177 |
|
| 178 |
public: |
| 179 |
OBSmartsPattern() |
| 180 |
{ |
| 181 |
_pat=NULL; |
| 182 |
} |
| 183 |
virtual ~OBSmartsPattern(); |
| 184 |
|
| 185 |
OBSmartsPattern(const OBSmartsPattern& cp) |
| 186 |
{ |
| 187 |
_pat = NULL; |
| 188 |
*this = cp; |
| 189 |
} |
| 190 |
OBSmartsPattern& operator=(const OBSmartsPattern& cp) |
| 191 |
{ |
| 192 |
if (_pat) |
| 193 |
delete [] _pat; |
| 194 |
_pat = NULL; |
| 195 |
std::string s = cp._str; |
| 196 |
Init(s); |
| 197 |
return (*this); |
| 198 |
} |
| 199 |
|
| 200 |
unsigned int NumMatches() const |
| 201 |
{ |
| 202 |
return (unsigned int)_mlist.size(); |
| 203 |
} |
| 204 |
unsigned int NumAtoms() const |
| 205 |
{ |
| 206 |
return _pat ? _pat->acount : 0; |
| 207 |
} |
| 208 |
unsigned int NumBonds() const |
| 209 |
{ |
| 210 |
return _pat ? _pat->bcount : 0; |
| 211 |
} |
| 212 |
|
| 213 |
int GetAtomicNum(int); |
| 214 |
void GetBond(int&,int&,int&,int); |
| 215 |
int GetCharge(int); |
| 216 |
const std::string &GetSMARTS() const |
| 217 |
{ |
| 218 |
return _str; |
| 219 |
} |
| 220 |
std::string &GetSMARTS() |
| 221 |
{ |
| 222 |
return _str; |
| 223 |
} |
| 224 |
int GetVectorBinding(int idx) const |
| 225 |
{ |
| 226 |
return(_pat->atom[idx].vb); |
| 227 |
} |
| 228 |
bool Empty() const |
| 229 |
{ |
| 230 |
return(_pat == NULL); |
| 231 |
} |
| 232 |
bool IsValid() const |
| 233 |
{ |
| 234 |
return(_pat != NULL); |
| 235 |
} |
| 236 |
bool Init(const char*); |
| 237 |
bool Init(const std::string&); |
| 238 |
void WriteMapList(std::ostream&); |
| 239 |
|
| 240 |
bool Match(OBMol &mol, bool single=false); |
| 241 |
bool RestrictedMatch(OBMol &mol, std::vector<std::pair<int,int> > &pairs, bool single=false); |
| 242 |
bool RestrictedMatch(OBMol &mol, OBBitVec &bv, bool single=false); |
| 243 |
|
| 244 |
std::vector<std::vector<int> > &GetMapList() |
| 245 |
{ |
| 246 |
return(_mlist); |
| 247 |
} |
| 248 |
std::vector<std::vector<int> > &GetUMapList(); |
| 249 |
std::vector<std::vector<int> >::iterator BeginMList() |
| 250 |
{ |
| 251 |
return(_mlist.begin()); |
| 252 |
} |
| 253 |
std::vector<std::vector<int> >::iterator EndMList() |
| 254 |
{ |
| 255 |
return(_mlist.end()); |
| 256 |
} |
| 257 |
}; |
| 258 |
|
| 259 |
//! Performs fast, exhaustive matching used to find just a single match in match() using recursion and explicit stack handling. |
| 260 |
class OBAPI OBSSMatch //used for fast exhaustive matching |
| 261 |
{ |
| 262 |
protected: |
| 263 |
bool *_uatoms; |
| 264 |
OBMol *_mol; |
| 265 |
Pattern *_pat; |
| 266 |
std::vector<int> _map; |
| 267 |
|
| 268 |
public: |
| 269 |
OBSSMatch(OBMol&,Pattern*); |
| 270 |
~OBSSMatch(); |
| 271 |
void Match(std::vector<std::vector<int> > &v, int bidx=-1); |
| 272 |
}; |
| 273 |
|
| 274 |
void SmartsLexReplace(std::string &, |
| 275 |
std::vector<std::pair<std::string,std::string> > &); |
| 276 |
|
| 277 |
} // end namespace OpenBabel |
| 278 |
|
| 279 |
#endif // OB_PARSMART_H |
| 280 |
|
| 281 |
//! \file parsmart.h |
| 282 |
//! \brief Daylight SMARTS parser. |