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 &operator=(const QueryAtom &other) {
39  if (this == &other) return *this;
40  Atom::operator=(other);
41  delete dp_query;
42  dp_query = other.dp_query->copy();
43  return *this;
44  }
45  ~QueryAtom();
46 
47  //! returns a copy of this query, owned by the caller
48  Atom *copy() const;
49 
50  // This method can be used to distinguish query atoms from standard atoms:
51  bool hasQuery() const { return dp_query != 0; };
52 
53  //! replaces our current query with the value passed in
54  void setQuery(QUERYATOM_QUERY *what) {
55  delete dp_query;
56  dp_query = what;
57  }
58  //! returns our current query
59  QUERYATOM_QUERY *getQuery() const { return dp_query; };
60 
61  //! expands our current query
62  /*!
63  \param what the Queries::Query to be added. The ownership of
64  the query is passed to the current object, where it
65  might be deleted, so that the pointer should not be
66  used again in the calling code.
67  \param how the operator to be used in the expansion
68  \param maintainOrder (optional) flags whether the relative order of
69  the queries needs to be maintained, if this is
70  false, the order is reversed
71  <b>Notes:</b>
72  - \c what should probably be constructed using one of the functions
73  defined in QueryOps.h
74  - the \c maintainOrder option can be useful because the combination
75  operators short circuit when possible.
76 
77  */
78  void expandQuery(QUERYATOM_QUERY *what,
80  bool maintainOrder = true);
81 
82  //! returns true if we match Atom \c what
83  bool Match(Atom const *what) const;
84 
85  //! returns true if our query details match those of QueryAtom \c what
86  bool QueryMatch(QueryAtom const *what) const;
87 
88  private:
89  QUERYATOM_QUERY *dp_query;
90 
91 }; // end o' class
92 
93 namespace detail {
94 inline std::string qhelper(Atom::QUERYATOM_QUERY *q, unsigned int depth) {
95  std::string res = "";
96  if (q) {
97  for (unsigned int i = 0; i < depth; ++i) res += " ";
98  res += q->getFullDescription() + "\n";
100  ci != q->endChildren(); ++ci) {
101  res += qhelper((*ci).get(), depth + 1);
102  }
103  }
104  return res;
105 }
106 } // namespace detail
107 inline std::string describeQuery(const Atom *atom) {
108  PRECONDITION(atom, "bad atom");
109  std::string res = "";
110  if (atom->hasQuery()) {
111  res = detail::qhelper(atom->getQuery(), 0);
112  }
113  return res;
114 }
115 
116 }; // namespace RDKit
117 
118 #endif
Queries::Query::CHILD_VECT_CI
CHILD_VECT::const_iterator CHILD_VECT_CI
Definition: Query.h:53
Queries::COMPOSITE_AND
@ COMPOSITE_AND
Definition: QueryObjects.h:36
Queries::Query::copy
virtual Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition: Query.h:127
QueryObjects.h
Pulls in all the query types.
RDKit::Atom::hasQuery
virtual bool hasQuery() const
Definition: Atom.h:263
RDKit::QueryAtom::setQuery
void setQuery(QUERYATOM_QUERY *what)
replaces our current query with the value passed in
Definition: QueryAtom.h:54
Queries::CompositeQueryType
CompositeQueryType
Definition: QueryObjects.h:36
RDKit::describeQuery
std::string describeQuery(const Atom *atom)
Definition: QueryAtom.h:107
RDKit::Atom::getQuery
virtual QUERYATOM_QUERY * getQuery() const
NOT CALLABLE.
RDKit::Atom
The class for representing atoms.
Definition: Atom.h:69
Atom.h
Defines the Atom class and associated typedefs.
RDKit::detail::qhelper
std::string qhelper(Atom::QUERYATOM_QUERY *q, unsigned int depth)
Definition: QueryAtom.h:94
RDKit::QueryAtom::QueryAtom
QueryAtom(const Atom &other)
Definition: QueryAtom.h:33
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:307
QueryOps.h
RDKit::QueryAtom::getQuery
QUERYATOM_QUERY * getQuery() const
returns our current query
Definition: QueryAtom.h:59
RDKit::QueryAtom::hasQuery
bool hasQuery() const
Definition: QueryAtom.h:51
Queries::Query::endChildren
CHILD_VECT_CI endChildren() const
returns an iterator for the end of our child vector
Definition: Query.h:105
RDKit
Std stuff.
Definition: Atom.h:30
Queries::Query::getFullDescription
virtual std::string getFullDescription() const
returns a fuller text description
Definition: Query.h:78
Queries::Query::beginChildren
CHILD_VECT_CI beginChildren() const
returns an iterator for the beginning of our child vector
Definition: Query.h:103
PRECONDITION
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
RDKit::QueryAtom::operator=
QueryAtom & operator=(const QueryAtom &other)
Definition: QueryAtom.h:38
Queries::Query
Base class for all queries.
Definition: Query.h:46
RDKit::QueryAtom::QUERYATOM_QUERY
Queries::Query< int, Atom const *, true > QUERYATOM_QUERY
Definition: QueryAtom.h:29
RDKit::QueryAtom
Class for storing atomic queries.
Definition: QueryAtom.h:27
RDKit::QueryAtom::QueryAtom
QueryAtom(int num)
Definition: QueryAtom.h:32
RDKit::QueryAtom::QueryAtom
QueryAtom(const QueryAtom &other)
Definition: QueryAtom.h:35
RDKit::makeAtomNumQuery
T * makeAtomNumQuery(int what, const std::string &descr)
returns a Query for matching atomic number
Definition: QueryOps.h:338
RDKit::Atom::operator=
Atom & operator=(const Atom &other)
RDKit::QueryAtom::QueryAtom
QueryAtom()
Definition: QueryAtom.h:31
export.h