Debian Codegraph @

include/BALL/MOLMEC/AMBER/GAFFCESParser.h

raw symbols SCIP
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3
4 #ifndef BALL_MOLMEC_AMBER_GAFFCESPARSER_H
5 #define BALL_MOLMEC_AMBER_GAFFCESPARSER_H
6
7 #ifndef BALL_KERNEL_ATOM_H
8 #include <BALL/KERNEL/atom.h>
9 #endif
10
11 #ifndef BALL_DATATYPE_STRING_H
12 #include <BALL/DATATYPE/string.h>
13 #endif
14
15 #include <set>
16 #include <map>
17 #include <vector>
18
19 namespace BALL
20 {
21
22 class GAFFCESParser
23 {
24 public:
25
26 //atomic property string
27 class APSMatcher
28 {
29 public:
30 //encode Ringatomtypes
31 enum APSType
32 {
33 IS_RING_ATOM,
34 IS_NON_RING_ATOM,
35 IS_PLANAR,
36 IS_PLANAR_WITH_DB_TO_NR,
37 IS_PURELY_AROMATIC,
38 IS_PURELY_ALIPHATIC,
39 IS_OTHER_RING,
40 IS_3_RING_ATOM,
41 IS_4_RING_ATOM,
42 IS_5_RING_ATOM,
43 IS_6_RING_ATOM,
44 IS_7_RING_ATOM,
45 IS_8_RING_ATOM,
46 IS_9_RING_ATOM,
47 PURE_SINGLE_BOND,
48 PURE_SINGLE_BOND_TO_PARENT,
49 NO_PURE_SINGLE_BOND_TO_PARENT,
50 SINGLE_BOND,
51 SINGLE_BOND_TO_PARENT,
52 NO_SINGLE_BOND_TO_PARENT,
53 PURE_DOUBLE_BOND,
54 PURE_DOUBLE_BOND_TO_PARENT,
55 NO_PURE_DOUBLE_BOND_TO_PARENT,
56 DOUBLE_BOND,
57 DOUBLE_BOND_TO_PARENT,
58 NO_DOUBLE_BOND_TO_PARENT,
59 TRIPLE_BOND,
60 TRIPLE_BOND_TO_PARENT,
61 NO_TRIPLE_BOND_TO_PARENT,
62 DELOCALIZED_BOND,
63 DELOCALIZED_BOND_TO_PARENT,
64 NO_DELOCALIZED_BOND_TO_PARENT,
65 AROMATIC_BOND,
66 AROMATIC_BOND_TO_PARENT,
67 NO_AROMATIC_BOND_TO_PARENT,
68 APS_TRUE
69 };
70
71 class APSTerm
72 {
73 public:
74 APSTerm(APSType new_type, int new_feature_number)
75 : type(new_type),
76 feature_number(new_feature_number)
77 {}
78
79 APSType type;
80 //contain the number of occurence of a given feature
81 int feature_number;
82 };
83
84 //check if current atom is in a ring
85 bool isRingAtom(Atom& atom);
86 //check if the current atom is in a ring with size n
87 //and return the number of occurence
88 int isNRingAtom(Size size, Atom& atom);
89 //check if the current atom is not a ringatom or
90 //in a ten-membered or larger ring
91 bool isNonRingAtom(Atom& atom);
92
93 bool checkGAFFProperties(Atom& atom, Atom& predecessor, APSTerm aps);
94
95
96 APSMatcher();
97 ~APSMatcher();
98
99 String printAPS();
100
101 // check if atom matches atomic property string
102 bool operator() (Atom& atom, Atom& predecessor);
103
104 //store atomic property string
105 //external vector: all AND (",") types
106 //internal vector: all OR (".")types
107 std::vector < std::vector< APSTerm> > aps_terms;
108
109 protected:
110 bool hasBond_(Atom* atom, Atom* predecessor, int bond_type, int feature_number);
111 };
112
113 struct State
114 {
115 //not "thread-safe"
116 GAFFCESParser* current_parser;
117
118 APSMatcher::APSType current_aps_type;
119 int feature_number;
120 };
121
122
123
124 //chemical environment string
125 class CESPredicate
126 {
127 public:
128 //encode wildcard elements
129 enum CESwildcards
130 {
131 XA, XB, XC, XD, XX
132 };
133
134 CESPredicate(GAFFCESParser* parser)
135 : parent(0),
136 atom_to_test(0),
137 parser_(parser)
138 {
139 initStringToWildcard();
140 };
141
142 virtual ~CESPredicate();
143
144 //initialize stringToWildcard map
145 void initStringToWildcard();
146 std::map<String, CESwildcards> getStringToWildcard();
147
148 //add a CESwildcardsConnectionPredicate to "predicate tree"
149 void addCESwildcardsConnectionPredicate(String wildcard, Size partners);
150 //add a CESwilddcardsPredicate to "predicate tree"
151 void addCESwildcardsPredicate(String wildcard);
152 //add an CESelementPredicate to "predicate tree"
153 void addCESelementPredicate(String name);
154 //add an CESelementConnectionPredicate to "predicate tree"
155 void addCESelementConnectionPredicate(Size partners, String name);
156 // add a TruePredicate to "predicate tree"
157 void addTruePredicate() const;
158
159 //check if atom and its environment match predicates
160 virtual bool operator () (Atom& atom);
161 //check if atom matches "predicates in predicate-tree"
162 virtual bool match(Atom&){return false;};
163 //delete children
164 void clear();
165
166 // check whether this atom is contained on a path to the root
167 bool alreadySeenThisAtom(Atom* atom);
168
169 //to expand aps_term in aps_matcher object
170 void addNewAND();
171 void addNewOR(APSMatcher::APSType aps, int feature_number);
172
173 //store existing atomic property string
174 APSMatcher aps_matcher;
175
176 //all CESPredicates for current_predicate ->children of current_predicate
177 std::vector<CESPredicate*> children;
178 //CESPredicate, that has current_predicate in his children vector
179 CESPredicate* parent;
180
181 //The atom we are trying to match to this predicate
182 Atom* atom_to_test;
183 protected:
184 //map to convert String into wildcard-element
185 std::map<String, CESwildcards > stringToWildcard_;
186 GAFFCESParser* parser_;
187 };
188
189 //element-name of the partnerAtom and number of its connected atoms
190 class CESelementConnectionPredicate : public CESPredicate
191 {
192 public:
193 CESelementConnectionPredicate(GAFFCESParser* parser)
194 : CESPredicate(parser),
195 numberOfPartners_(0),
196 elementName_("NoName")
197 {};
198
199 ~CESelementConnectionPredicate();
200
201 void setNumberOfPartners(Size number);
202 void setElementName(String name);
203 Size getNumberOfPartners();
204 String getElementName();
205 //check if atom matches predicate
206 bool match(Atom& atom);
207 protected:
208 Size numberOfPartners_;
209 String elementName_;
210 };
211
212 //element-name of the partnerAtom
213 class CESelementPredicate : public CESPredicate
214 {
215 public:
216 CESelementPredicate(GAFFCESParser* parser)
217 : CESPredicate(parser),
218 elementName_("NoName")
219 {
220 };
221
222 ~CESelementPredicate();
223
224 void setElementName(String name);
225 String getElementName();
226 //check if atom matches predicate
227 bool match(Atom& atom);
228 protected:
229 String elementName_;
230 };
231
232 //XA,XB,XC,XD,XX
233 class CESwildcardsPredicate : public CESPredicate
234 {
235 public:
236 CESwildcardsPredicate(GAFFCESParser* parser)
237 : CESPredicate(parser)
238 {};
239
240 ~CESwildcardsPredicate();
241
242 void setWildcards(String new_wildcard);
243 CESwildcards getWildcards();
244 //checks if atom matches the given wildcard-element (XA, XB, XC, XD, XX)
245 bool matchWildcards(Atom& atom);
246 //check if atom matches predicate
247 bool match(Atom& atom);
248 protected:
249 CESwildcards wildcards_;
250 };
251
252 //XA,XB,XC,XD,XX and number of partnerAtoms
253 class CESwildcardsConnectionPredicate : public CESPredicate
254 {
255 public:
256 CESwildcardsConnectionPredicate(GAFFCESParser* parser)
257 : CESPredicate(parser),
258 numberOfPartners_(0)
259 {};
260 ~CESwildcardsConnectionPredicate();
261
262 void setNumberOfPartners(Size number);
263 void setWildcards(String new_wildcard);
264 Size getNumberOfPartners();
265 CESwildcards getWildcards();
266 //checks if atom matches the given wildcard-element (XA, XB, XC, XD, XX)
267 bool matchWildcards(Atom& atom);
268 //check if atom matches predicate
269 bool match(Atom& atom);
270 protected:
271 CESwildcards wildcards_;
272 Size numberOfPartners_;
273 };
274
275 //string is "*" which means always true
276 class TruePredicate : public CESPredicate
277 {
278 public:
279 TruePredicate(GAFFCESParser* parser)
280 : CESPredicate(parser)
281 {};
282 ~TruePredicate() {};
283 //check if atom matches predicate (always true!)
284 bool match(Atom&) { return true; }
285 };
286
287 //Parser-match-Function checking if atom's environment matches the "predicate tree"
288 bool match(Atom& atom) const;
289
290 //initialize Set of Elementsymbols
291 void initElementSymbols();
292 const std::set<String>& getElementSymbols();
293
294 GAFFCESParser();
295 GAFFCESParser(const String& cesstring);
296 ~GAFFCESParser();
297
298 //for lexer/parser
299 Size read(char* buf, Size max_size);
300
301 static State state;
302
303 /// The root atom has no CESPredicate to test, so it's considered always true
304 TruePredicate root;
305 //fixed root of the "predicate tree"
306 CESPredicate* root_predicate;
307 //parent-predicate of current_predicate
308 CESPredicate* current_root_predicate;
309 //"predicate-node" in the "predicate tree" we actually considering
310 CESPredicate* current_predicate;
311
312
313 //parse chemical environment string
314 bool parse(const String& cesstring);
315 //check if any atom matches parsed ces_string
316 bool GAFFCESatomMatcher(Atom& atom, const String& cesstring);
317 //start filling the children vector for a current predicate
318 void startChildPredicates();
319 //end up filling the children vector for a current predicate
320 void endChildPredicates();
321
322 protected:
323 //current chemical environment string
324 String cesstring_;
325 //set with all valid element symbols
326 std::set<String> element_symbols_;
327 //for Parser/Lexer function YYINPUT
328 Position read_start_;
329 };
330
331 }
332
333 #endif