RDKit: QueryAtom.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
QueryAtom.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2022 Greg Landrum and other RDKit contributors
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 <utility>
15#include "Atom.h"
16#include <Query/QueryObjects.h>
17#include <GraphMol/QueryOps.h>
18
19namespace RDKit {
20
21//! Class for storing atomic queries
22/*!
23 QueryAtom objects are derived from Atom objects, so they can be
24 added to molecules and the like, but they have much fancier
25 querying capabilities.
26
27 */
28 class RDKIT_GRAPHMOL_EXPORT QueryAtom : public Atom {
29 public:
30 typedef Queries::Query<int, Atom const *, true> QUERYATOM_QUERY;
31
32 QueryAtom() : Atom() {}
33 explicit QueryAtom(int num) : Atom(num), dp_query(makeAtomNumQuery(num)) {}
34 explicit QueryAtom(const Atom &other)
35 : Atom(other), dp_query(makeAtomNumQuery(other.getAtomicNum())) {
36 if (other.getIsotope()) {
37 this->expandQuery(makeAtomIsotopeQuery(other.getIsotope()),
38 Queries::CompositeQueryType::COMPOSITE_AND);
39 }
40 if (other.getFormalCharge()) {
41 this->expandQuery(makeAtomFormalChargeQuery(other.getFormalCharge()),
42 Queries::CompositeQueryType::COMPOSITE_AND);
43 }
44 if (other.getNumRadicalElectrons()) {
45 this->expandQuery(
46 makeAtomNumRadicalElectronsQuery(other.getNumRadicalElectrons()),
47 Queries::CompositeQueryType::COMPOSITE_AND);
48 }
49 }
50 QueryAtom(const QueryAtom &other) : Atom(other) {
51 if (other.dp_query) {
52 dp_query = other.dp_query->copy();
53 } else {
54 dp_query = nullptr;
55 }
56 }
57 QueryAtom &operator=(const QueryAtom &other) {
58 if (this == &other) {
59 return *this;
60 }
61 Atom::operator=(other);
62 delete dp_query;
63 if (other.dp_query) {
64 dp_query = other.dp_query->copy();
65 } else {
66 dp_query = nullptr;
67 }
68 return *this;
69 }
70
71 QueryAtom(QueryAtom &&other) noexcept : Atom(std::move(other)) {
72 dp_query = std::exchange(other.dp_query, nullptr);
73 }
74 QueryAtom &operator=(QueryAtom &&other) noexcept {
75 if (this == &other) {
76 return *this;
77 }
78 QueryAtom::operator=(std::move(other));
79 dp_query = std::exchange(other.dp_query, nullptr);
80 return *this;
81 }
82
83 ~QueryAtom() override;
84
85 //! returns a copy of this query, owned by the caller
86 Atom *copy() const override;
87
88 // This method can be used to distinguish query atoms from standard atoms:
89 bool hasQuery() const override { return dp_query != nullptr; }
90
91 //! returns the label associated to this query
92 std::string getQueryType() const override { return dp_query->getTypeLabel(); }
93
94 //! replaces our current query with the value passed in
95 void setQuery(QUERYATOM_QUERY *what) override {
96 delete dp_query;
97 dp_query = what;
98 }
99 //! returns our current query
100 QUERYATOM_QUERY *getQuery() const override { return dp_query; }
101
102 //! expands our current query
103 /*!
104 \param what the Queries::Query to be added. The ownership of
105 the query is passed to the current object, where it
106 might be deleted, so that the pointer should not be
107 used again in the calling code.
108 \param how the operator to be used in the expansion
109 \param maintainOrder (optional) flags whether the relative order of
110 the queries needs to be maintained, if this is
111 false, the order is reversed
112 <b>Notes:</b>
113 - \c what should probably be constructed using one of the functions
114 defined in QueryOps.h
115 - the \c maintainOrder option can be useful because the combination
116 operators short circuit when possible.
117
118 */
119 void expandQuery(QUERYATOM_QUERY *what,
120 Queries::CompositeQueryType how = Queries::COMPOSITE_AND,
121 bool maintainOrder = true) override;
122
123 //! returns true if we match Atom \c what
124 bool Match(Atom const *what) const override;
125
126 //! returns true if our query details match those of QueryAtom \c what
127 bool QueryMatch(QueryAtom const *what) const;
128
129 private:
130 QUERYATOM_QUERY *dp_query{nullptr};
131
132}; // end o' class
133
134namespace detail {
135 inline std::string qhelper(const Atom::QUERYATOM_QUERY *q, unsigned int depth) {
136 std::string res = "";
137 if (q) {
138 for (unsigned int i = 0; i < depth; ++i) {
139 res += " ";
140 }
141 res += q->getFullDescription() + "\n";
142 for (const auto &child :
143 boost::make_iterator_range(q->beginChildren(), q->endChildren())) {
144 res += qhelper(child.get(), depth + 1);
145 }
146 }
147 return res;
148}
149} // namespace detail
150 inline std::string describeQuery(const Atom *atom) {
151 PRECONDITION(atom, "bad atom");
152 std::string res = "";
153 if (atom->hasQuery()) {
154 res = detail::qhelper(atom->getQuery(), 0);
155 }
156 return res;
157}
158
159}; // namespace RDKit
160
161#endif
Atom.h
Defines the Atom class and associated typedefs.
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
QueryObjects.h
Pulls in all the query types.
QueryOps.h
Queries::Query
Base class for all queries.
Definition Query.h:45
Queries::Query::getFullDescription
virtual std::string getFullDescription() const
returns a fuller text description
Definition Query.h:72
Queries::Query::endChildren
CHILD_VECT_CI endChildren() const
returns an iterator for the end of our child vector
Definition Query.h:107
Queries::Query::beginChildren
CHILD_VECT_CI beginChildren() const
returns an iterator for the beginning of our child vector
Definition Query.h:105
Queries::Query::copy
virtual Query< MatchFuncArgType, DataFuncArgType, needsConversion > * copy() const
returns a copy of this Query
Definition Query.h:131
RDKit::Atom
The class for representing atoms.
Definition Atom.h:74
RDKit::Atom::operator=
Atom & operator=(const Atom &other)
RDKit::Atom::QUERYATOM_QUERY
Queries::Query< int, Atom const *, true > QUERYATOM_QUERY
Definition Atom.h:89
RDKit::Atom::getNumRadicalElectrons
unsigned int getNumRadicalElectrons() const
returns the number of radical electrons for this Atom
Definition Atom.h:207
RDKit::Atom::getAtomicNum
int getAtomicNum() const
returns our atomic number
Definition Atom.h:145
RDKit::Atom::hasQuery
virtual bool hasQuery() const
Definition Atom.h:268
RDKit::Atom::getFormalCharge
int getFormalCharge() const
returns the formal charge of this atom
Definition Atom.h:211
RDKit::Atom::getIsotope
unsigned int getIsotope() const
returns our isotope number
Definition Atom.h:237
RDKit::Atom::getQuery
virtual QUERYATOM_QUERY * getQuery() const
NOT CALLABLE.
RDKit::Atom::Atom
Atom()
RDKit::QueryAtom::hasQuery
bool hasQuery() const override
Definition QueryAtom.h:89
RDKit::QueryAtom::QueryMatch
bool QueryMatch(QueryAtom const *what) const
returns true if our query details match those of QueryAtom what
RDKit::QueryAtom::operator=
QueryAtom & operator=(const QueryAtom &other)
Definition QueryAtom.h:57
RDKit::QueryAtom::setQuery
void setQuery(QUERYATOM_QUERY *what) override
replaces our current query with the value passed in
Definition QueryAtom.h:95
RDKit::QueryAtom::QueryAtom
QueryAtom(int num)
Definition QueryAtom.h:33
RDKit::QueryAtom::QueryAtom
QueryAtom(QueryAtom &&other) noexcept
Definition QueryAtom.h:71
RDKit::QueryAtom::Match
bool Match(Atom const *what) const override
returns true if we match Atom what
RDKit::QueryAtom::QueryAtom
QueryAtom()
Definition QueryAtom.h:32
RDKit::QueryAtom::QUERYATOM_QUERY
Queries::Query< int, Atom const *, true > QUERYATOM_QUERY
Definition QueryAtom.h:30
RDKit::QueryAtom::operator=
QueryAtom & operator=(QueryAtom &&other) noexcept
Definition QueryAtom.h:74
RDKit::QueryAtom::QueryAtom
QueryAtom(const QueryAtom &other)
Definition QueryAtom.h:50
RDKit::QueryAtom::QueryAtom
QueryAtom(const Atom &other)
Definition QueryAtom.h:34
RDKit::QueryAtom::~QueryAtom
~QueryAtom() override
RDKit::QueryAtom::expandQuery
void expandQuery(QUERYATOM_QUERY *what, Queries::CompositeQueryType how=Queries::COMPOSITE_AND, bool maintainOrder=true) override
expands our current query
RDKit::QueryAtom::getQueryType
std::string getQueryType() const override
returns the label associated to this query
Definition QueryAtom.h:92
RDKit::QueryAtom::getQuery
QUERYATOM_QUERY * getQuery() const override
returns our current query
Definition QueryAtom.h:100
RDKit::QueryAtom::copy
Atom * copy() const override
returns a copy of this query, owned by the caller
export.h
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:249
Queries::CompositeQueryType
CompositeQueryType
Definition QueryObjects.h:36
Queries::COMPOSITE_AND
@ COMPOSITE_AND
Definition QueryObjects.h:36
RDKit::detail::qhelper
std::string qhelper(const Atom::QUERYATOM_QUERY *q, unsigned int depth)
Definition QueryAtom.h:135
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::describeQuery
std::string describeQuery(const Atom *atom)
Definition QueryAtom.h:150
RDKit::makeAtomNumQuery
T * makeAtomNumQuery(int what, const std::string &descr)
returns a Query for matching atomic number
Definition QueryOps.h:364