RDKit
Open-source cheminformatics and machine learning.
QueryAtom.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2017 Greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef _RD_QUERYATOM_H_
11 #define _RD_QUERYATOM_H_
12 
13 #include "Atom.h"
14 #include <Query/QueryObjects.h>
15 #include <GraphMol/QueryOps.h>
16 
17 namespace RDKit {
18 
19 //! Class for storing atomic queries
20 /*!
21  QueryAtom objects are derived from Atom objects, so they can be
22  added to molecules and the like, but they have much fancier
23  querying capabilities.
24 
25  */
26 class QueryAtom : public Atom {
27  public:
29 
30  QueryAtom() : Atom(), dp_query(NULL){};
31  explicit QueryAtom(int num) : Atom(num), dp_query(makeAtomNumQuery(num)){};
32  explicit QueryAtom(const Atom &other)
33  : Atom(other), dp_query(makeAtomNumQuery(other.getAtomicNum())){};
34  QueryAtom(const QueryAtom &other) : Atom(other) {
35  dp_query = other.dp_query->copy();
36  };
37  ~QueryAtom();
38 
39  //! returns a copy of this query, owned by the caller
40  Atom *copy() const;
41 
42  // This method can be used to distinguish query atoms from standard atoms:
43  bool hasQuery() const { return dp_query != 0; };
44 
45  //! replaces our current query with the value passed in
46  void setQuery(QUERYATOM_QUERY *what) {
47  delete dp_query;
48  dp_query = what;
49  }
50  //! returns our current query
51  QUERYATOM_QUERY *getQuery() const { return dp_query; };
52 
53  //! expands our current query
54  /*!
55  \param what the Queries::Query to be added
56  \param how the operator to be used in the expansion
57  \param maintainOrder (optional) flags whether the relative order of
58  the queries needs to be maintained, if this is
59  false, the order is reversed
60  <b>Notes:</b>
61  - \c what should probably be constructed using one of the functions
62  defined in QueryOps.h
63  - the \c maintainOrder option can be useful because the combination
64  operators short circuit when possible.
65 
66  */
67  void expandQuery(QUERYATOM_QUERY *what,
69  bool maintainOrder = true);
70 
71  //! returns true if we match Atom \c what
72  bool Match(const Atom::ATOM_SPTR &what) const;
73  //! \overload
74  bool Match(Atom const *what) const;
75 
76  //! returns true if our query details match those of QueryAtom \c what
77  bool QueryMatch(QueryAtom const *what) const;
78 
79  private:
80  QUERYATOM_QUERY *dp_query;
81 
82 }; // end o' class
83 
84 namespace detail {
85 inline std::string qhelper(Atom::QUERYATOM_QUERY *q, unsigned int depth) {
86  std::string res = "";
87  if (q) {
88  for (unsigned int i = 0; i < depth; ++i) res += " ";
89  res += q->getFullDescription() + "\n";
91  ci != q->endChildren(); ++ci) {
92  res += qhelper((*ci).get(), depth + 1);
93  }
94  }
95  return res;
96 }
97 } // end of detail namespace
98 inline std::string describeQuery(const Atom *atom) {
99  PRECONDITION(atom, "bad atom");
100  std::string res = "";
101  if (atom->hasQuery()) {
102  res = detail::qhelper(atom->getQuery(), 0);
103  }
104  return res;
105 }
106 
107 }; // end o' namespace
108 
109 #endif
virtual QUERYATOM_QUERY * getQuery() const
NOT CALLABLE.
virtual std::string getFullDescription() const
returns a fuller text description
Definition: Query.h:76
QUERYATOM_QUERY * getQuery() const
returns our current query
Definition: QueryAtom.h:51
QueryAtom(int num)
Definition: QueryAtom.h:31
CompositeQueryType
Definition: QueryObjects.h:35
std::string qhelper(Atom::QUERYATOM_QUERY *q, unsigned int depth)
Definition: QueryAtom.h:85
Class for storing atomic queries.
Definition: QueryAtom.h:26
CHILD_VECT_CI endChildren() const
returns an iterator for the end of our child vector
Definition: Query.h:103
void setQuery(QUERYATOM_QUERY *what)
replaces our current query with the value passed in
Definition: QueryAtom.h:46
Queries::Query< int, Atom const *, true > QUERYATOM_QUERY
Definition: QueryAtom.h:28
std::string describeQuery(const Atom *atom)
Definition: QueryAtom.h:98
QueryAtom(const QueryAtom &other)
Definition: QueryAtom.h:34
Std stuff.
Definition: Atom.h:29
int getAtomicNum() const
returns our atomic number
Definition: Atom.h:116
T * makeAtomNumQuery(int what, const std::string &descr)
returns a Query for matching atomic number
Definition: QueryOps.h:226
virtual bool hasQuery() const
Definition: Atom.h:260
CHILD_VECT_CI beginChildren() const
returns an iterator for the beginning of our child vector
Definition: Query.h:101
boost::shared_ptr< Atom > ATOM_SPTR
Definition: Atom.h:74
CHILD_VECT::const_iterator CHILD_VECT_CI
Definition: Query.h:51
#define PRECONDITION(expr, mess)
Definition: Invariant.h:107
bool Match(const Atom::ATOM_SPTR &what) const
returns true if we match Atom what
Defines the Atom class and associated typedefs.
Pulls in all the query types.
bool hasQuery() const
Definition: QueryAtom.h:43
QueryAtom(const Atom &other)
Definition: QueryAtom.h:32
Base class for all queries.
Definition: Query.h:45
Atom * copy() const
returns a copy of this query, owned by the caller
The class for representing atoms.
Definition: Atom.h:68
void expandQuery(QUERYATOM_QUERY *what, Queries::CompositeQueryType how=Queries::COMPOSITE_AND, bool maintainOrder=true)
expands our current query
bool QueryMatch(QueryAtom const *what) const
returns true if our query details match those of QueryAtom what
virtual Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: Query.h:125