RDKit: QueryBond.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
QueryBond.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_QUERYBOND_H
12#define _RD_QUERYBOND_H
13
14#include <Query/QueryObjects.h>
15#include "Bond.h"
16#include "QueryOps.h"
17
18namespace RDKit {
19
20//! Class for storing Bond queries
21/*!
22 QueryBond objects are derived from Bond objects, so they can be
23 added to molecules and the like, but they have much fancier
24 querying capabilities.
25
26 */
27
28 class RDKIT_GRAPHMOL_EXPORT QueryBond : public Bond {
29 public:
30 typedef Queries::Query<int, Bond const *, true> QUERYBOND_QUERY;
31
32 QueryBond() : Bond() {}
33 //! initialize with a particular bond order
34 explicit QueryBond(BondType bT);
35 //! initialize from a bond
36 explicit QueryBond(const Bond &other)
37 : Bond(other), dp_query(makeBondOrderEqualsQuery(other.getBondType())) {}
38 QueryBond(const QueryBond &other) : Bond(other) {
39 if (other.dp_query) {
40 dp_query = other.dp_query->copy();
41 } else {
42 dp_query = nullptr;
43 }
44 }
45 QueryBond(QueryBond &&other) noexcept : Bond(std::move(other)) {
46 dp_query = std::move(other.dp_query);
47 }
48 QueryBond &operator=(QueryBond &&other) noexcept {
49 if (this == &other) {
50 return *this;
51 }
52 QueryBond::operator=(std::move(other));
53 dp_query = std::move(other.dp_query);
54 return *this;
55 }
56
57 ~QueryBond() override;
58
59 //! returns a copy of this query, owned by the caller
60 Bond *copy() const override;
61
62 QueryBond &operator=(const QueryBond &other);
63
64 //! sets the BondType of this query:
65 void setBondType(BondType bT);
66 //! sets the BondDir of this query:
67 void setBondDir(BondDir bD);
68
69 //! returns true if we match Bond \c what
70 bool Match(Bond const *what) const override;
71
72 //! returns true if our query details match those of QueryBond \c what
73 bool QueryMatch(QueryBond const *what) const;
74
75 // This method can be used to distinguish query bonds from standard bonds
76 bool hasQuery() const override { return dp_query != nullptr; }
77
78 //! returns our current query
79 QUERYBOND_QUERY *getQuery() const override { return dp_query; }
80 //! replaces our current query with the value passed in
81 void setQuery(QUERYBOND_QUERY *what) override {
82 // free up any existing query (Issue255):
83 delete dp_query;
84 dp_query = what;
85 }
86
87 //! expands our current query
88 /*!
89 \param what the Queries::Query to be added. The ownership of
90 the query is passed to the current object, where it
91 might be deleted, so that the pointer should not be
92 used again in the calling code.
93 \param how the operator to be used in the expansion
94 \param maintainOrder (optional) flags whether the relative order of
95 the queries needs to be maintained, if this is
96 false, the order is reversed
97
98 <b>Notes:</b>
99 - \c what should probably be constructed using one of the functions
100 defined in QueryOps.h
101 - the \c maintainOrder option can be useful because the combination
102 operators short circuit when possible.
103
104 */
105 void expandQuery(QUERYBOND_QUERY *what,
106 Queries::CompositeQueryType how = Queries::COMPOSITE_AND,
107 bool maintainOrder = true) override;
108
109 //! returns our contribution to the explicit valence of an Atom
110 /*!
111 <b>Notes:</b>
112 - requires an owning molecule
113 */
114 double getValenceContrib(const Atom *at) const override;
115
116 protected:
117 QUERYBOND_QUERY *dp_query{nullptr};
118};
119
120namespace detail {
121 inline std::string qhelper(const Bond::QUERYBOND_QUERY *q, unsigned int depth) {
122 std::string res;
123 if (q) {
124 for (unsigned int i = 0; i < depth; ++i) {
125 res += " ";
126 }
127 res += q->getFullDescription() + "\n";
128 for (const auto &child :
129 boost::make_iterator_range(q->beginChildren(), q->endChildren())) {
130 res += qhelper(child.get(), depth + 1);
131 }
132 }
133 return res;
134}
135} // namespace detail
136 inline std::string describeQuery(const Bond *bond) {
137 PRECONDITION(bond, "bad bond");
138 std::string res = "";
139 if (bond->hasQuery()) {
140 res = detail::qhelper(bond->getQuery(), 0);
141 }
142 return res;
143}
144}; // namespace RDKit
145
146#endif
Bond.h
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
RDKit::Atom
The class for representing atoms.
Definition Atom.h:74
RDKit::Bond
class for representing a bond
Definition Bond.h:46
RDKit::Bond::BondType
BondType
the type of Bond
Definition Bond.h:55
RDKit::Bond::getQuery
virtual QUERYBOND_QUERY * getQuery() const
NOT CALLABLE.
RDKit::Bond::hasQuery
virtual bool hasQuery() const
Definition Bond.h:286
RDKit::Bond::QUERYBOND_QUERY
Queries::Query< int, Bond const *, true > QUERYBOND_QUERY
Definition Bond.h:52
RDKit::Bond::getBondType
BondType getBondType() const
returns our bondType
Definition Bond.h:159
RDKit::Bond::Bond
Bond()
RDKit::Bond::BondDir
BondDir
the bond's direction (for chirality)
Definition Bond.h:82
RDKit::QueryBond::setBondDir
void setBondDir(BondDir bD)
sets the BondDir of this query:
RDKit::QueryBond::setBondType
void setBondType(BondType bT)
sets the BondType of this query:
RDKit::QueryBond::QueryBond
QueryBond()
Definition QueryBond.h:32
RDKit::QueryBond::QueryBond
QueryBond(BondType bT)
initialize with a particular bond order
RDKit::QueryBond::hasQuery
bool hasQuery() const override
Definition QueryBond.h:76
RDKit::QueryBond::expandQuery
void expandQuery(QUERYBOND_QUERY *what, Queries::CompositeQueryType how=Queries::COMPOSITE_AND, bool maintainOrder=true) override
expands our current query
RDKit::QueryBond::getQuery
QUERYBOND_QUERY * getQuery() const override
returns our current query
Definition QueryBond.h:79
RDKit::QueryBond::QueryBond
QueryBond(const Bond &other)
initialize from a bond
Definition QueryBond.h:36
RDKit::QueryBond::setQuery
void setQuery(QUERYBOND_QUERY *what) override
replaces our current query with the value passed in
Definition QueryBond.h:81
RDKit::QueryBond::copy
Bond * copy() const override
returns a copy of this query, owned by the caller
RDKit::QueryBond::QUERYBOND_QUERY
Queries::Query< int, Bond const *, true > QUERYBOND_QUERY
Definition QueryBond.h:30
RDKit::QueryBond::getValenceContrib
double getValenceContrib(const Atom *at) const override
returns our contribution to the explicit valence of an Atom
RDKit::QueryBond::QueryBond
QueryBond(const QueryBond &other)
Definition QueryBond.h:38
RDKit::QueryBond::~QueryBond
~QueryBond() override
RDKit::QueryBond::QueryBond
QueryBond(QueryBond &&other) noexcept
Definition QueryBond.h:45
RDKit::QueryBond::QueryMatch
bool QueryMatch(QueryBond const *what) const
returns true if our query details match those of QueryBond what
RDKit::QueryBond::dp_query
QUERYBOND_QUERY * dp_query
Definition QueryBond.h:117
RDKit::QueryBond::Match
bool Match(Bond const *what) const override
returns true if we match Bond what
RDKit::QueryBond::operator=
QueryBond & operator=(const QueryBond &other)
RDKit::QueryBond::operator=
QueryBond & operator=(QueryBond &&other) noexcept
Definition QueryBond.h:48
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::makeBondOrderEqualsQuery
RDKIT_GRAPHMOL_EXPORT BOND_EQUALS_QUERY * makeBondOrderEqualsQuery(Bond::BondType what)
returns a Query for matching bond orders
RDKit::describeQuery
std::string describeQuery(const Atom *atom)
Definition QueryAtom.h:150