BALL  1.5.0
smartsParser.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_STRUCTURE_SMARTES_PARSER_H
6 #define BALL_STRUCTURE_SMARTES_PARSER_H
7 
8 #ifndef BALL_COMMON_H
9  # include <BALL/common.h>
10 #endif
11 
12 #include <map>
13 #include <set>
14 
15 // needed for MSVC:
16 #undef CW_DEFAULT
17 
18 namespace BALL
19 {
20  // forward declarations
21  class Bond;
22  class Atom;
23  class Element;
24 
36  {
37  public:
38 
40  {
41  ANY_ZE = 1,
43  Z,
44  E
45  };
46 
49  {
50  CHIRAL_CLASS_UNSPECIFIED = 1,
53  CW_DEFAULT, // TH
55  CCW_DEFAULT, // TH
57  CW_TH, // tetrahdral
61  CW_AL, // allene-like
65  CW_SP, // square planar
69  CW_TB, //trigonal bipyramidal
73  CW_OH, // octahedral
76  CCW_OH_OR_UNSPECIFIED
77  };
78 
87  {
88  AND,
89  OR,
91  NOOP
92  };
93 
94 
96  class SPAtom;
97 
106  {
107  public:
108 
111  {
112  SINGLE = 1,
123  ANY
124  };
125 
131 
133  SPBond(SPBondOrder bond_order);
134 
136  SPBond(SPAtom* first, SPAtom* second, SPBondOrder bond_order);
137 
139  virtual ~SPBond() ;
141 
146  ZEIsomerType getZEType() const { return ze_type_; }
147 
149  void setZEType(ZEIsomerType type) { ze_type_ = type; }
150 
152  void setBondOrder(SPBondOrder bond_order);
153 
155  SPBondOrder getBondOrder() const { return bond_order_; }
156 
158  bool isNot() const { return not_; }
159 
161  void setNot(bool is_not) { not_ = is_not; }
162 
163  // returns true if the SPBond matches the given bond
164  bool equals(const Bond* bond) const;
166 
167  protected:
168 
171 
174 
176  bool not_;
177  };
178 
189  {
190  public:
191 
194  {
195  ISOTOPE = 1,
209  SYMBOL
210  };
211 
214  {
219  };
220 
222  struct Property
223  {
224  public:
225 
230  Property(PropertyType type, int value);
231 
233  Property(PropertyType type, bool value);
234 
236  Property(PropertyType type, const Element* value);
237 
240 
242  virtual ~Property();
244 
246  void operator = (const Property& rhs);
247 
252  PropertyType getType() const { return type_; }
253 
255  PropertyValue getValue() const { return value_; }
257 
258  private:
259 
261  Property();
262 
264  PropertyType type_;
265 
267  PropertyValue value_;
268  };
269 
309 
311  SPAtom(const String& symbol);
312 
314  virtual ~SPAtom() ;
316 
317 
322  void setProperty(PropertyType type, int int_value);
323 
325  void setProperty(PropertyType type, bool flag);
326 
328  void setProperty(PropertyType type, const Element* element);
329 
331  void setProperty(PropertyType type, ChiralClass chirality);
332 
334  void setProperty(Property property);
335 
338 
341 
343  bool hasProperty(PropertyType type) const;
344 
347 
350 
352  Size getDefaultValence(const Atom* atom) const;
353 
355  Size countRealValences(const Atom* atom) const;
356 
360 
365  bool equals(const Atom* atom) const;
367 
368  protected:
369 
372 
374  std::map<PropertyType, PropertyValue> properties_;
375 
377  std::set<PropertyType> not_properties_;
378  };
379 
381  class SPNode;
382 
386  {
387  public:
388 
394 
396  SPEdge(const SPEdge& sp_edge);
397 
399  virtual ~SPEdge();
401 
406  bool isInternal() const { return internal_; }
407 
409  void setInternal(bool internal) { internal_ = internal; }
410 
412  void setSPBond(SPBond* sp_bond) { bond_ = sp_bond; }
413 
415  SPBond* getSPBond() const { return bond_; }
416 
418  void setFirstSPNode(SPNode* first) { first_ = first; }
419 
421  SPNode* getFirstSPNode() const { return first_; }
422 
424  void setSecondSPNode(SPNode* second) { second_ = second; }
425 
427  SPNode* getSecondSPNode() const { return second_; }
428 
430  SPNode* getPartnerSPNode(SPNode* node) { return node == first_ ? second_ : first_; }
431 
433  bool isNot() const { return is_not_; }
434 
436  void setNot(bool is_not) { is_not_ = is_not; }
437 
439  void setFirstSPEdge(SPEdge* first) { first_edge_ = first; }
440 
442  SPEdge* getFirstSPEdge() const { return first_edge_; }
443 
445  void setSecondSPEdge(SPEdge* second) { second_edge_ = second; }
446 
448  SPEdge* getSecondSPEdge() const { return second_edge_; }
449 
451  void setLogicalOperator(LogicalOperator log_op) { log_op_ = log_op; }
452 
454  LogicalOperator getLogicalOperator() const { return log_op_; }
456 
457  protected:
458 
460  bool internal_;
461 
463  bool is_not_;
464 
467 
470 
473 
476 
479 
482  };
483 
487  {
488  public:
489 
494  typedef std::vector<SPEdge*>::iterator EdgeIterator;
495 
497  typedef std::vector<SPEdge*>::const_iterator EdgeConstIterator;
499 
500 
506 
508  SPNode(SPAtom* atom);
509 
511  SPNode(SPNode* first, LogicalOperator log_op, SPNode* second);
512 
514  SPNode(const SPNode& sp_node);
515 
517  virtual ~SPNode();
519 
520 
525  bool isInternal() const { return internal_; }
526 
528  void setInternal(bool internal) { internal_ = internal; }
529 
531  bool isRecursive() const { return recursive_; }
532 
534  void setRecursive(bool recursive);
535 
537  void setComponentNumber(int no) { component_no_ = no; }
538 
540  Size getComponentNumber() const { return component_no_; }
541 
543  SPAtom* getSPAtom() const { return sp_atom_; }
544 
546  void setSPAtom(SPAtom* sp_atom) { sp_atom_ = sp_atom; }
547 
549  SPEdge* getFirstEdge() const { return first_edge_; }
550 
552  void setFirstEdge(SPEdge* first) { first_edge_ = first; }
553 
555  SPEdge* getSecondEdge() const { return second_edge_; }
556 
558  void setSecondEdge(SPEdge* second) { second_edge_ = second; }
559 
561  bool getNot() const { return is_not_; }
562 
564  void setNot(bool is_not) { is_not_ = is_not; }
565 
566 
568  //void setInBrackets() { in_brackets_ = true; }
569 
571  void addSPEdge(SPEdge* sp_edge) { edges_.push_back(sp_edge); }
572 
574  void setLogicalOperator(LogicalOperator log_op) { log_op_ = log_op; }
575 
577  LogicalOperator getLogicalOperator() const { return log_op_; }
578 
580  Size countEdges() const { return edges_.size(); }
582 
587  EdgeIterator begin() { return edges_.begin(); }
588 
590  EdgeIterator end() { return edges_.end(); }
591 
593  EdgeConstIterator begin() const { return edges_.begin(); }
594 
596  EdgeConstIterator end() const { return edges_.end(); }
598 
599  protected:
600 
602  bool internal_;
603 
605  bool is_not_;
606 
609 
611  //bool in_brackets_;
612 
615 
617  std::vector<SPEdge*> edges_;
618 
621 
624 
627 
630  };
631 
632 
638 
640  SmartsParser(const SmartsParser& parser);
641 
643  virtual ~SmartsParser();
645 
652  void parse(const String& s);
653 
658  SPAtom* createAtom(const String& symbol, bool in_bracket = false);
659 
661  void setRoot(SPNode* root) { root_ = root; }
662 
664  SPNode* getRoot() const { return root_; }
665 
667  void dumpTree();
668 
670  void clear();
671 
673  void addRingConnection(SPNode* spnode, Size index);
674 
676  std::map<Size, std::vector<SPNode*> > getRingConnections() const;
677 
679  void setSSSR(const std::vector<std::vector<Atom*> >& sssr);
680 
682  void setNeedsSSSR(bool needs_sssr) { needs_SSSR_ = needs_sssr; }
683 
685  bool getNeedsSSSR() const { return needs_SSSR_; }
686 
688  void setRecursive(bool recursive) { recursive_ = recursive; }
689 
691  bool isRecursive() const { return recursive_; }
692 
694  void setComponentGrouping(bool component_grouping) { component_grouping_ = component_grouping; }
695 
697  bool hasComponentGrouping() const { return component_grouping_; }
698 
700  struct State
701  {
704  const char* buffer;
705  };
706 
708  static State state;
709 
711  const std::set<SPNode*>& getNodes() const { return nodes_; }
712 
714  const std::set<SPEdge*>& getEdges() const { return edges_; }
715 
717  void addEdge(SPEdge* edge) { edges_.insert(edge); }
718 
720  void addNode(SPNode* node) { nodes_.insert(node); }
721 
723  bool hasRecursiveEdge(SPEdge* edge) const { return rec_edges_.find(edge) != rec_edges_.end(); }
724 
726  void addRecursiveEdge(SPEdge* edge) { rec_edges_.insert(edge); }
727 
731 
732  protected:
733 
736 
739 
742 
744  static vector<std::set<const Atom*> >* sssr_;
745 
747  void dumpTreeRecursive_(SPNode* node, Size depth);
748 
750  void dumpTreeRecursive_(SPEdge* edge, Size depth);
751 
753  std::map<Size, std::vector<SPNode*> > ring_connections_;
754 
757 
759  std::set<SPEdge*> edges_;
760 
762  std::set<SPNode*> nodes_;
763 
765  std::set<SPEdge*> rec_edges_;
766 
769 
772  };
773 
774 } // namespace BALL
775 
776 #endif // BALL_STRUCTURE_SMARTS_PARSER_H
777 
Definition: constants.h:13
BALL_SIZE_TYPE Property
BALL_EXTERN_VARIABLE const double E
Euler's number - base of the natural logarithm.
Definition: constants.h:38
char Atom[5]
Definition: PDBdefs.h:257
std::set< SPEdge * > rec_edges_
the recursive edges
Definition: smartsParser.h:765
void dumpTree()
dumps the tree to cerr
bool hasComponentGrouping() const
returns true if the component level grouping was enabled
Definition: smartsParser.h:697
const std::set< SPNode * > & getNodes() const
returns the eodes stored in the tree
Definition: smartsParser.h:711
int component_no_
the actual component number
Definition: smartsParser.h:771
std::set< SPNode * > nodes_
the nodes
Definition: smartsParser.h:762
SmartsParser()
Default constructor.
void setNextComponentNumberToSubTree(SPNode *spnode)
gets the next component no and assigns it to the subtree
bool recursive_
recursive flag
Definition: smartsParser.h:738
bool component_grouping_
component level grouping flag
Definition: smartsParser.h:741
SPNode * root_
the root node of the tree
Definition: smartsParser.h:768
std::map< Size, std::vector< SPNode * > > ring_connections_
the ring connection sorted by index of the SMARTS pattern
Definition: smartsParser.h:753
bool needs_SSSR_
sssr needed flag
Definition: smartsParser.h:735
void setSSSR(const std::vector< std::vector< Atom * > > &sssr)
sets the SSSR
ChiralClass
chiral class definitions CW = clock wise, CCW = counter clock wise
Definition: smartsParser.h:49
void addEdge(SPEdge *edge)
adds an edge to the tree
Definition: smartsParser.h:717
void setRecursive(bool recursive)
sets the recursive flag
Definition: smartsParser.h:688
std::map< Size, std::vector< SPNode * > > getRingConnections() const
returns the ring connections sorted by index from SMARTS pattern
static SmartsParser * current_parser_
current instance
Definition: smartsParser.h:756
SPAtom * createAtom(const String &symbol, bool in_bracket=false)
creates a new atom of symbol
bool getNeedsSSSR() const
returns true if the SMARTS pattern contains ring related parts
Definition: smartsParser.h:685
static State state
static member for the parser itself
Definition: smartsParser.h:708
void setNeedsSSSR(bool needs_sssr)
sets the sssr needed flag
Definition: smartsParser.h:682
bool isRecursive() const
returns true if the tree represents a recursive SMARTS pattern
Definition: smartsParser.h:691
void addRecursiveEdge(SPEdge *edge)
adds a recursive edge to the tree
Definition: smartsParser.h:726
SPNode * getRoot() const
returns the root SPNode of the tree
Definition: smartsParser.h:664
void setRoot(SPNode *root)
sets the root SPNode of the tree
Definition: smartsParser.h:661
const std::set< SPEdge * > & getEdges() const
returns the edges stored in the tree
Definition: smartsParser.h:714
void addRingConnection(SPNode *spnode, Size index)
adds a ring connection, SPNode with an index used in the SMARTS pattern
SmartsParser(const SmartsParser &parser)
Copy constructor.
void dumpTreeRecursive_(SPEdge *edge, Size depth)
dump method for the tree
void addNode(SPNode *node)
adds a node to the tree
Definition: smartsParser.h:720
void parse(const String &s)
virtual ~SmartsParser()
Destructor.
std::set< SPEdge * > edges_
the edges
Definition: smartsParser.h:759
void clear()
clear the tree
bool hasRecursiveEdge(SPEdge *edge) const
returns true if the tree has the given recursive edge
Definition: smartsParser.h:723
void dumpTreeRecursive_(SPNode *node, Size depth)
dump method for the tree
static vector< std::set< const Atom * > > * sssr_
the sssr
Definition: smartsParser.h:744
void setComponentGrouping(bool component_grouping)
sets the component level flag
Definition: smartsParser.h:694
Bond representation of the smarts parser.
Definition: smartsParser.h:106
void setBondOrder(SPBondOrder bond_order)
sets the bond order
void setZEType(ZEIsomerType type)
sets the Z/E isomer type
Definition: smartsParser.h:149
virtual ~SPBond()
Destructor.
SPBondOrder bond_order_
the bond order
Definition: smartsParser.h:173
SPBond()
Default constructor.
ZEIsomerType getZEType() const
returns the Z/E isomer type
Definition: smartsParser.h:146
SPBondOrder getBondOrder() const
returns the bond order
Definition: smartsParser.h:155
SPBond(SPAtom *first, SPAtom *second, SPBondOrder bond_order)
Detailed constructor with.
bool isNot() const
return true if a general negation is set
Definition: smartsParser.h:158
SPBond(SPBondOrder bond_order)
Detailed constructor with bond order.
SPBondOrder
the bond orders supported by SMARTS-patterns
Definition: smartsParser.h:111
void setNot(bool is_not)
set the general negation to the bool given
Definition: smartsParser.h:161
ZEIsomerType ze_type_
Z/E isomer type.
Definition: smartsParser.h:170
bool equals(const Bond *bond) const
bool not_
general negation flag
Definition: smartsParser.h:176
Smarts Parser Atom class.
Definition: smartsParser.h:189
PropertyValue getProperty(PropertyType type)
returns a value of the given property type
void setProperty(PropertyType type, ChiralClass chirality)
sets a chirality value
void setNotProperty(PropertyType type)
negotiates the property definition
Size getDefaultValence(const Atom *atom) const
return the number of valences of the given atom
SPAtom(const String &symbol)
copy constructor
Size getNumberOfImplicitHydrogens(const Atom *atom) const
returns the number of implicit hydrogens of the given atom
void setProperty(PropertyType type, const Element *element)
sets a Element
std::map< PropertyType, PropertyValue > properties_
the properties of this SPAtom
Definition: smartsParser.h:374
Size countProperties() const
returns the number of properties
SPAtom()
Default constructor.
Size countRealValences(const Atom *atom) const
returns the number of available valences of the given atom
PropertyType
enum of all properties possible for a smarts parser atom
Definition: smartsParser.h:194
std::set< PropertyType > not_properties_
the properties which are negated
Definition: smartsParser.h:377
bool hasProperty(PropertyType type) const
returns true if the property is set
void setProperty(PropertyType type, int int_value)
sets an int property
Atom * atom_
the atom which this sp_atom belongs to
Definition: smartsParser.h:371
void setProperty(PropertyType type, bool flag)
sets a flag
void setProperty(Property property)
sets a property
bool equals(const Atom *atom) const
returns true if the local properties of the atoms match
virtual ~SPAtom()
destructor
void addPropertiesFromSPAtom(SPAtom *sp_atom)
adds properties from another SPAtom
possible types of the properties
Definition: smartsParser.h:214
Property struct of smarts parser atom.
Definition: smartsParser.h:223
Property(PropertyType type, const Element *value)
Detailed constructor with type and Element.
Property(PropertyType type, int value)
Detailed constructor with type and int value.
PropertyValue getValue() const
returns the value of the property
Definition: smartsParser.h:255
Property(PropertyType type, ChiralClass value)
Detailed constructor with type and chiral class definition.
Property(PropertyType type, bool value)
Detailed constructor with type and flag.
PropertyType getType() const
returns the type of the property
Definition: smartsParser.h:252
Edge representation of the smarts parser graph.
Definition: smartsParser.h:386
SPNode * getFirstSPNode() const
returns the first SPNode of this edge
Definition: smartsParser.h:421
void setFirstSPNode(SPNode *first)
set the first SPNode of this edge
Definition: smartsParser.h:418
SPEdge(const SPEdge &sp_edge)
Copy constructor.
void setSecondSPEdge(SPEdge *second)
set the second SPEdge (second tree child)
Definition: smartsParser.h:445
SPNode * getPartnerSPNode(SPNode *node)
returns the partner; either the first or the second SPNode
Definition: smartsParser.h:430
LogicalOperator getLogicalOperator() const
returns the asociated logical operator (for the child edges)
Definition: smartsParser.h:454
virtual ~SPEdge()
Destructor.
SPEdge * first_edge_
first SPEdge
Definition: smartsParser.h:475
SPBond * bond_
associated bond
Definition: smartsParser.h:472
SPEdge * getFirstSPEdge() const
returns the first SPEdge (first tree child)
Definition: smartsParser.h:442
SPNode * getSecondSPNode() const
returns the second SPNode of this edge
Definition: smartsParser.h:427
bool isInternal() const
returns true if this is a internal edge
Definition: smartsParser.h:406
void setNot(bool is_not)
set the negation flag
Definition: smartsParser.h:436
SPEdge * second_edge_
second SPEdge
Definition: smartsParser.h:478
SPNode * first_
first SPNode
Definition: smartsParser.h:466
bool is_not_
negation flag
Definition: smartsParser.h:463
void setSPBond(SPBond *sp_bond)
sets the corresponding SPBond of this edge
Definition: smartsParser.h:412
void setSecondSPNode(SPNode *second)
sets the second SPNode of this edge
Definition: smartsParser.h:424
bool internal_
internal flag
Definition: smartsParser.h:460
bool isNot() const
returns true if negation is enabled
Definition: smartsParser.h:433
SPNode * second_
second SPNode
Definition: smartsParser.h:469
SPEdge()
Default constructor.
void setInternal(bool internal)
set this edge to a internal edge
Definition: smartsParser.h:409
SPBond * getSPBond() const
returns the corresponding SPBond of this edge
Definition: smartsParser.h:415
SPEdge * getSecondSPEdge() const
returns the second SPEdge (second tree child)
Definition: smartsParser.h:448
void setFirstSPEdge(SPEdge *first)
set the first SPEdge (first tree child)
Definition: smartsParser.h:439
void setLogicalOperator(LogicalOperator log_op)
sets the associated logical operator (for the child edges)
Definition: smartsParser.h:451
LogicalOperator log_op_
logical operator associated with the SPEdges
Definition: smartsParser.h:481
Representation of a node in the smarts parser graph.
Definition: smartsParser.h:487
std::vector< SPEdge * >::const_iterator EdgeConstIterator
constant edge iterator
Definition: smartsParser.h:497
SPEdge * getFirstEdge() const
returns the first edge (for tree use)
Definition: smartsParser.h:549
Size getComponentNumber() const
returns the component number
Definition: smartsParser.h:540
bool internal_
internal flag
Definition: smartsParser.h:602
void setSecondEdge(SPEdge *second)
sets the second edge (for tree use)
Definition: smartsParser.h:558
SPAtom * getSPAtom() const
returns the associated SPAtom
Definition: smartsParser.h:543
EdgeIterator end()
mutable access to end of edges list
Definition: smartsParser.h:590
bool isInternal() const
returns true if the SPNode is an internal node
Definition: smartsParser.h:525
void setInternal(bool internal)
sets the internal flag
Definition: smartsParser.h:528
std::vector< SPEdge * > edges_
edges list
Definition: smartsParser.h:617
bool isRecursive() const
returns true if the SPNode is a recursive node (from recursive SMARTS)
Definition: smartsParser.h:531
void setFirstEdge(SPEdge *first)
sets the first edge (for tree use)
Definition: smartsParser.h:552
void addSPEdge(SPEdge *sp_edge)
flag whether the pattern is in brackets
Definition: smartsParser.h:571
void setRecursive(bool recursive)
sets the recursive flag
EdgeIterator begin()
mutable access to begin of edges list
Definition: smartsParser.h:587
std::vector< SPEdge * >::iterator EdgeIterator
non-constant edge iterator
Definition: smartsParser.h:494
SPAtom * sp_atom_
SPAtom associated with this SPNode.
Definition: smartsParser.h:626
void setNot(bool is_not)
sets the negation flag
Definition: smartsParser.h:564
int component_no_
component level
Definition: smartsParser.h:629
bool is_not_
negotiation flag
Definition: smartsParser.h:605
void setComponentNumber(int no)
set the component no of the component level grouping
Definition: smartsParser.h:537
Size countEdges() const
counts the number of edges
Definition: smartsParser.h:580
EdgeConstIterator begin() const
non-mutable access to begin of edges list
Definition: smartsParser.h:593
SPNode(const SPNode &sp_node)
Copy constructor.
void setSPAtom(SPAtom *sp_atom)
set the associated SPAtom
Definition: smartsParser.h:546
SPNode(SPAtom *atom)
Detailed constructor with an atom.
SPNode()
Default constructor.
SPEdge * second_edge_
second edge
Definition: smartsParser.h:623
LogicalOperator getLogicalOperator() const
returns the logical operator of the SPNode
Definition: smartsParser.h:577
LogicalOperator log_op_
in brackets flag
Definition: smartsParser.h:614
EdgeConstIterator end() const
non-mutable access to end of edges list
Definition: smartsParser.h:596
void setLogicalOperator(LogicalOperator log_op)
sets the logical operator associated with the SPNode
Definition: smartsParser.h:574
bool recursive_
recursive flag
Definition: smartsParser.h:608
virtual ~SPNode()
Destructor.
SPEdge * getSecondEdge() const
returns the second edge (for tree use)
Definition: smartsParser.h:555
bool getNot() const
returns the negation flag
Definition: smartsParser.h:561
SPEdge * first_edge_
first edge
Definition: smartsParser.h:620
SPNode(SPNode *first, LogicalOperator log_op, SPNode *second)
Detailed constructor with two nodes and a logical operator.
Parser state (used by the parser itself)
Definition: smartsParser.h:701
SmartsParser * current_parser
Definition: smartsParser.h:703
#define BALL_EXPORT
Definition: COMMON/global.h:50