BALL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GAFFCESParser.h
Go to the documentation of this file.
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 
23  {
24  public:
25 
26  //atomic property string
27  class APSMatcher
28  {
29  public:
30  //encode Ringatomtypes
31  enum APSType
32  {
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 
80  //contain the number of occurence of a given feature
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"
117 
120  };
121 
122 
123 
124  //chemical environment string
126  {
127  public:
128  //encode wildcard elements
130  {
131  XA, XB, XC, XD, XX
132  };
133 
135  : parent(0),
136  parser_(parser)
137  {
139  };
140 
141  virtual ~CESPredicate();
142 
143  //initialize stringToWildcard map
144  void initStringToWildcard();
145  std::map<String, CESwildcards> getStringToWildcard();
146 
147  //add a CESwildcardsConnectionPredicate to "predicate tree"
148  void addCESwildcardsConnectionPredicate(String wildcard, Size partners);
149  //add a CESwilddcardsPredicate to "predicate tree"
150  void addCESwildcardsPredicate(String wildcard);
151  //add an CESelementPredicate to "predicate tree"
152  void addCESelementPredicate(String name);
153  //add an CESelementConnectionPredicate to "predicate tree"
154  void addCESelementConnectionPredicate(Size partners, String name);
155  // add a TruePredicate to "predicate tree"
156  void addTruePredicate() const;
157 
158  //check if atom and its environment match predicates
159  virtual bool operator () (Atom& atom);
160  //check if atom matches "predicates in predicate-tree"
161  virtual bool match(Atom&){return false;};
162  //delete children
163  void clear();
164 
165  // check whether this atom is contained on a path to the root
166  bool alreadySeenThisAtom(Atom* atom);
167 
168  //to expand aps_term in aps_matcher object
169  void addNewAND();
170  void addNewOR(APSMatcher::APSType aps, int feature_number);
171 
172  //store existing atomic property string
174 
175  //all CESPredicates for current_predicate ->children of current_predicate
176  std::vector<CESPredicate*> children;
177  //CESPredicate, that has current_predicate in his children vector
179 
180  //The atom we are trying to match to this predicate
182  protected:
183  //map to convert String into wildcard-element
184  std::map<String, CESwildcards > stringToWildcard_;
186  };
187 
188  //element-name of the partnerAtom and number of its connected atoms
190  {
191  public:
193  : CESPredicate(parser),
194  numberOfPartners_(0),
195  elementName_("NoName")
196  {};
197 
199 
200  void setNumberOfPartners(Size number);
201  void setElementName(String name);
204  //check if atom matches predicate
205  bool match(Atom& atom);
206  protected:
209  };
210 
211  //element-name of the partnerAtom
213  {
214  public:
216  : CESPredicate(parser),
217  elementName_("NoName")
218  {
219  };
220 
222 
223  void setElementName(String name);
225  //check if atom matches predicate
226  bool match(Atom& atom);
227  protected:
229  };
230 
231  //XA,XB,XC,XD,XX
233  {
234  public:
236  : CESPredicate(parser)
237  {};
238 
240 
241  void setWildcards(String new_wildcard);
243  //checks if atom matches the given wildcard-element (XA, XB, XC, XD, XX)
244  bool matchWildcards(Atom& atom);
245  //check if atom matches predicate
246  bool match(Atom& atom);
247  protected:
249  };
250 
251  //XA,XB,XC,XD,XX and number of partnerAtoms
253  {
254  public:
256  : CESPredicate(parser),
258  {};
260 
261  void setNumberOfPartners(Size number);
262  void setWildcards(String new_wildcard);
265  //checks if atom matches the given wildcard-element (XA, XB, XC, XD, XX)
266  bool matchWildcards(Atom& atom);
267  //check if atom matches predicate
268  bool match(Atom& atom);
269  protected:
272  };
273 
274  //string is "*" which means always true
276  {
277  public:
279  : CESPredicate(parser)
280  {};
282  //check if atom matches predicate (always true!)
283  bool match(Atom&) { return true; }
284  };
285 
286  //Parser-match-Function checking if atom's environment matches the "predicate tree"
287  bool match(Atom& atom) const;
288 
289  //initialize Set of Elementsymbols
290  void initElementSymbols();
291  const std::set<String>& getElementSymbols();
292 
293  GAFFCESParser();
294  GAFFCESParser(const String& cesstring);
295  ~GAFFCESParser();
296 
297  //for lexer/parser
298  Size read(char* buf, Size max_size);
299 
300  static State state;
301 
304  //fixed root of the "predicate tree"
306  //parent-predicate of current_predicate
308  //"predicate-node" in the "predicate tree" we actually considering
310 
311 
312  //parse chemical environment string
313  bool parse(const String& cesstring);
314  //check if any atom matches parsed ces_string
315  bool GAFFCESatomMatcher(Atom& atom, const String& cesstring);
316  //start filling the children vector for a current predicate
317  void startChildPredicates();
318  //end up filling the children vector for a current predicate
319  void endChildPredicates();
320 
321  protected:
322  //current chemical environment string
324  //set with all valid element symbols
325  std::set<String> element_symbols_;
326  //for Parser/Lexer function YYINPUT
328  };
329 
330 }
331 
332 #endif