RDKit
Open-source cheminformatics and machine learning.
QueryBond.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2021 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 
18 namespace 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 
29  public:
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() override;
46 
47  //! returns a copy of this query, owned by the caller
48  Bond *copy() const override;
49 
50  QueryBond &operator=(const QueryBond &other);
51 
52  //! sets the BondType of this query:
54  //! sets the BondDir of this query:
55  void setBondDir(BondDir bD);
56 
57  //! returns true if we match Bond \c what
58  bool Match(Bond const *what) const override;
59 
60  //! returns true if our query details match those of QueryBond \c what
61  bool QueryMatch(QueryBond const *what) const;
62 
63  // This method can be used to distinguish query bonds from standard bonds
64  bool hasQuery() const override { return dp_query != nullptr; }
65 
66  //! returns our current query
67  QUERYBOND_QUERY *getQuery() const override { return dp_query; }
68  //! replaces our current query with the value passed in
69  void setQuery(QUERYBOND_QUERY *what) override {
70  // free up any existing query (Issue255):
71  delete dp_query;
72  dp_query = what;
73  }
74 
75  //! expands our current query
76  /*!
77  \param what the Queries::Query to be added. The ownership of
78  the query is passed to the current object, where it
79  might be deleted, so that the pointer should not be
80  used again in the calling code.
81  \param how the operator to be used in the expansion
82  \param maintainOrder (optional) flags whether the relative order of
83  the queries needs to be maintained, if this is
84  false, the order is reversed
85 
86  <b>Notes:</b>
87  - \c what should probably be constructed using one of the functions
88  defined in QueryOps.h
89  - the \c maintainOrder option can be useful because the combination
90  operators short circuit when possible.
91 
92  */
95  bool maintainOrder = true) override;
96 
97  //! returns our contribution to the explicit valence of an Atom
98  /*!
99  <b>Notes:</b>
100  - requires an owning molecule
101  */
102  double getValenceContrib(const Atom *at) const override;
103 
104  protected:
105  QUERYBOND_QUERY *dp_query{nullptr};
106 };
107 
108 namespace detail {
109 inline std::string qhelper(Bond::QUERYBOND_QUERY *q, unsigned int depth) {
110  std::string res;
111  if (q) {
112  for (unsigned int i = 0; i < depth; ++i) res += " ";
113  res += q->getFullDescription() + "\n";
115  ci != q->endChildren(); ++ci) {
116  res += qhelper((*ci).get(), depth + 1);
117  }
118  }
119  return res;
120 }
121 } // namespace detail
122 inline std::string describeQuery(const Bond *bond) {
123  PRECONDITION(bond, "bad bond");
124  std::string res = "";
125  if (bond->hasQuery()) {
126  res = detail::qhelper(bond->getQuery(), 0);
127  }
128  return res;
129 }
130 }; // namespace RDKit
131 
132 #endif
#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
class for representing a bond
Definition: Bond.h:46
BondType
the type of Bond
Definition: Bond.h:55
virtual bool hasQuery() const
Definition: Bond.h:244
BondDir
the bond's direction (for chirality)
Definition: Bond.h:82
virtual QUERYBOND_QUERY * getQuery() const
NOT CALLABLE.
Class for storing Bond queries.
Definition: QueryBond.h:28
void setBondDir(BondDir bD)
sets the BondDir of this query:
void setBondType(BondType bT)
sets the BondType of this query:
QUERYBOND_QUERY * getQuery() const override
returns our current query
Definition: QueryBond.h:67
Bond * copy() const override
returns a copy of this query, owned by the caller
QueryBond & operator=(const QueryBond &other)
QueryBond(BondType bT)
initialize with a particular bond order
bool hasQuery() const override
Definition: QueryBond.h:64
void expandQuery(QUERYBOND_QUERY *what, Queries::CompositeQueryType how=Queries::COMPOSITE_AND, bool maintainOrder=true) override
expands our current query
QueryBond(const Bond &other)
initialize from a bond
Definition: QueryBond.h:36
void setQuery(QUERYBOND_QUERY *what) override
replaces our current query with the value passed in
Definition: QueryBond.h:69
Queries::Query< int, Bond const *, true > QUERYBOND_QUERY
Definition: QueryBond.h:30
double getValenceContrib(const Atom *at) const override
returns our contribution to the explicit valence of an Atom
QueryBond(const QueryBond &other)
Definition: QueryBond.h:38
~QueryBond() override
bool QueryMatch(QueryBond const *what) const
returns true if our query details match those of QueryBond what
QUERYBOND_QUERY * dp_query
Definition: QueryBond.h:105
bool Match(Bond const *what) const override
returns true if we match Bond what
#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
std::string describeQuery(const Atom *atom)
Definition: QueryAtom.h:129
RDKIT_GRAPHMOL_EXPORT BOND_EQUALS_QUERY * makeBondOrderEqualsQuery(Bond::BondType what)
returns a Query for matching bond orders