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