1 |
tim |
2440 |
/********************************************************************** |
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 |
gezelter |
3057 |
// mark this so that SWIG will not attempt to wrap for scripting languages |
61 |
|
|
|
62 |
|
|
#ifndef SWIG |
63 |
|
|
|
64 |
tim |
2440 |
//! \brief A SMARTS parser internal atomic expression |
65 |
|
|
typedef union _AtomExpr { |
66 |
|
|
int type; |
67 |
|
|
struct |
68 |
|
|
{ |
69 |
|
|
int type; |
70 |
|
|
int prop; |
71 |
|
|
int value; |
72 |
|
|
} |
73 |
|
|
leaf; |
74 |
|
|
struct |
75 |
|
|
{ |
76 |
|
|
int type; |
77 |
|
|
void *recur; |
78 |
|
|
} |
79 |
|
|
recur; |
80 |
|
|
struct |
81 |
|
|
{ |
82 |
|
|
int type; |
83 |
|
|
union _AtomExpr *arg; |
84 |
|
|
} |
85 |
|
|
mon; |
86 |
|
|
struct |
87 |
|
|
{ |
88 |
|
|
int type; |
89 |
|
|
union _AtomExpr *lft; |
90 |
|
|
union _AtomExpr *rgt; |
91 |
|
|
} |
92 |
|
|
bin; |
93 |
|
|
} AtomExpr; |
94 |
|
|
|
95 |
|
|
#define BE_LEAF 0x01 |
96 |
|
|
#define BE_ANDHI 0x02 |
97 |
|
|
#define BE_ANDLO 0x03 |
98 |
|
|
#define BE_NOT 0x04 |
99 |
|
|
#define BE_OR 0x05 |
100 |
|
|
|
101 |
|
|
#define BL_CONST 0x01 |
102 |
|
|
#define BL_TYPE 0x02 |
103 |
|
|
|
104 |
|
|
#define BT_SINGLE 0x01 |
105 |
|
|
#define BT_DOUBLE 0x02 |
106 |
|
|
#define BT_TRIPLE 0x03 |
107 |
|
|
#define BT_AROM 0x04 |
108 |
|
|
#define BT_UP 0x05 |
109 |
|
|
#define BT_DOWN 0x06 |
110 |
|
|
#define BT_UPUNSPEC 0x07 |
111 |
|
|
#define BT_DOWNUNSPEC 0x08 |
112 |
|
|
#define BT_RING 0x09 |
113 |
|
|
|
114 |
|
|
//! \brief A SMARTS parser internal bond expression |
115 |
|
|
typedef union _BondExpr { |
116 |
|
|
int type; |
117 |
|
|
struct |
118 |
|
|
{ |
119 |
|
|
int type; |
120 |
|
|
int prop; |
121 |
|
|
int value; |
122 |
|
|
} |
123 |
|
|
leaf; |
124 |
|
|
struct |
125 |
|
|
{ |
126 |
|
|
int type; |
127 |
|
|
union _BondExpr *arg; |
128 |
|
|
} |
129 |
|
|
mon; |
130 |
|
|
struct |
131 |
|
|
{ |
132 |
|
|
int type; |
133 |
|
|
union _BondExpr *lft; |
134 |
|
|
union _BondExpr *rgt; |
135 |
|
|
} |
136 |
|
|
bin; |
137 |
|
|
} BondExpr; |
138 |
|
|
|
139 |
|
|
//! \brief A SMARTS parser internal bond specification |
140 |
|
|
typedef struct |
141 |
|
|
{ |
142 |
|
|
BondExpr *expr; |
143 |
|
|
int src,dst; |
144 |
|
|
int visit; |
145 |
|
|
bool grow; |
146 |
|
|
} |
147 |
|
|
BondSpec; |
148 |
|
|
|
149 |
|
|
//! \brief A SMARTS parser internal bond specification |
150 |
|
|
typedef struct |
151 |
|
|
{ |
152 |
|
|
AtomExpr *expr; |
153 |
|
|
int visit; |
154 |
|
|
int part; |
155 |
|
|
int chiral_flag; |
156 |
|
|
int vb; |
157 |
|
|
} |
158 |
|
|
AtomSpec; |
159 |
|
|
|
160 |
|
|
//! \brief A SMARTS parser internal pattern |
161 |
|
|
typedef struct |
162 |
|
|
{ |
163 |
|
|
int aalloc,acount; |
164 |
|
|
int balloc,bcount; |
165 |
|
|
bool ischiral; |
166 |
|
|
AtomSpec *atom; |
167 |
|
|
BondSpec *bond; |
168 |
|
|
int parts; |
169 |
|
|
} |
170 |
|
|
Pattern; |
171 |
gezelter |
3057 |
#else |
172 |
|
|
// for SWIG, just forward declare that we have some Pattern struct |
173 |
|
|
// (but this is private and not wrapped for scripting languages) |
174 |
|
|
struct Pattern; |
175 |
|
|
#endif |
176 |
tim |
2440 |
|
177 |
|
|
// class introduction in parsmart.cpp |
178 |
|
|
//! \brief SMARTS (SMiles ARbitrary Target Specification) substructure searching |
179 |
|
|
class OBAPI OBSmartsPattern |
180 |
|
|
{ |
181 |
|
|
protected: |
182 |
|
|
std::vector<bool> _growbond; |
183 |
|
|
std::vector<std::vector<int> > _mlist; |
184 |
|
|
Pattern *_pat; |
185 |
|
|
std::string _str; |
186 |
|
|
|
187 |
|
|
public: |
188 |
|
|
OBSmartsPattern() |
189 |
|
|
{ |
190 |
|
|
_pat=NULL; |
191 |
|
|
} |
192 |
|
|
virtual ~OBSmartsPattern(); |
193 |
|
|
|
194 |
|
|
OBSmartsPattern(const OBSmartsPattern& cp) |
195 |
|
|
{ |
196 |
|
|
_pat = NULL; |
197 |
|
|
*this = cp; |
198 |
|
|
} |
199 |
|
|
OBSmartsPattern& operator=(const OBSmartsPattern& cp) |
200 |
|
|
{ |
201 |
|
|
if (_pat) |
202 |
|
|
delete [] _pat; |
203 |
|
|
_pat = NULL; |
204 |
|
|
std::string s = cp._str; |
205 |
|
|
Init(s); |
206 |
|
|
return (*this); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
unsigned int NumMatches() const |
210 |
|
|
{ |
211 |
|
|
return (unsigned int)_mlist.size(); |
212 |
|
|
} |
213 |
|
|
unsigned int NumAtoms() const |
214 |
|
|
{ |
215 |
|
|
return _pat ? _pat->acount : 0; |
216 |
|
|
} |
217 |
|
|
unsigned int NumBonds() const |
218 |
|
|
{ |
219 |
|
|
return _pat ? _pat->bcount : 0; |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
int GetAtomicNum(int); |
223 |
|
|
void GetBond(int&,int&,int&,int); |
224 |
|
|
int GetCharge(int); |
225 |
|
|
const std::string &GetSMARTS() const |
226 |
|
|
{ |
227 |
|
|
return _str; |
228 |
|
|
} |
229 |
|
|
std::string &GetSMARTS() |
230 |
|
|
{ |
231 |
|
|
return _str; |
232 |
|
|
} |
233 |
|
|
int GetVectorBinding(int idx) const |
234 |
|
|
{ |
235 |
|
|
return(_pat->atom[idx].vb); |
236 |
|
|
} |
237 |
|
|
bool Empty() const |
238 |
|
|
{ |
239 |
|
|
return(_pat == NULL); |
240 |
|
|
} |
241 |
|
|
bool IsValid() const |
242 |
|
|
{ |
243 |
|
|
return(_pat != NULL); |
244 |
|
|
} |
245 |
|
|
bool Init(const char*); |
246 |
|
|
bool Init(const std::string&); |
247 |
|
|
void WriteMapList(std::ostream&); |
248 |
|
|
|
249 |
|
|
bool Match(OBMol &mol, bool single=false); |
250 |
|
|
bool RestrictedMatch(OBMol &mol, std::vector<std::pair<int,int> > &pairs, bool single=false); |
251 |
|
|
bool RestrictedMatch(OBMol &mol, OBBitVec &bv, bool single=false); |
252 |
|
|
|
253 |
|
|
std::vector<std::vector<int> > &GetMapList() |
254 |
|
|
{ |
255 |
|
|
return(_mlist); |
256 |
|
|
} |
257 |
|
|
std::vector<std::vector<int> > &GetUMapList(); |
258 |
|
|
std::vector<std::vector<int> >::iterator BeginMList() |
259 |
|
|
{ |
260 |
|
|
return(_mlist.begin()); |
261 |
|
|
} |
262 |
|
|
std::vector<std::vector<int> >::iterator EndMList() |
263 |
|
|
{ |
264 |
|
|
return(_mlist.end()); |
265 |
|
|
} |
266 |
|
|
}; |
267 |
|
|
|
268 |
|
|
//! Performs fast, exhaustive matching used to find just a single match in match() using recursion and explicit stack handling. |
269 |
|
|
class OBAPI OBSSMatch //used for fast exhaustive matching |
270 |
|
|
{ |
271 |
|
|
protected: |
272 |
|
|
bool *_uatoms; |
273 |
|
|
OBMol *_mol; |
274 |
|
|
Pattern *_pat; |
275 |
|
|
std::vector<int> _map; |
276 |
|
|
|
277 |
|
|
public: |
278 |
|
|
OBSSMatch(OBMol&,Pattern*); |
279 |
|
|
~OBSSMatch(); |
280 |
|
|
void Match(std::vector<std::vector<int> > &v, int bidx=-1); |
281 |
|
|
}; |
282 |
|
|
|
283 |
|
|
void SmartsLexReplace(std::string &, |
284 |
|
|
std::vector<std::pair<std::string,std::string> > &); |
285 |
|
|
|
286 |
|
|
} // end namespace OpenBabel |
287 |
|
|
|
288 |
|
|
#endif // OB_PARSMART_H |
289 |
|
|
|
290 |
|
|
//! \file parsmart.h |
291 |
|
|
//! \brief Daylight SMARTS parser. |