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