Loading...
Searching...
No Matches
QueryAtom.h
Go to the documentation of this file.
//
// Copyright (C) 2001-2022 Greg Landrum and other RDKit contributors
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef RD_QUERYATOM_H
#define RD_QUERYATOM_H
#include <utility>
#include "Atom.h"
#include <Query/QueryObjects.h>
#include <GraphMol/QueryOps.h>
namespace RDKit {
//! Class for storing atomic queries
/*!
QueryAtom objects are derived from Atom objects, so they can be
added to molecules and the like, but they have much fancier
querying capabilities.
*/
class RDKIT_GRAPHMOL_EXPORT QueryAtom : public Atom {
public:
typedef Queries::Query<int, Atom const *, true> QUERYATOM_QUERY;
QueryAtom() : Atom() {}
explicit QueryAtom(int num) : Atom(num), dp_query(makeAtomNumQuery(num)) {}
explicit QueryAtom(const Atom &other)
: Atom(other), dp_query(makeAtomNumQuery(other.getAtomicNum())) {
if (other.getIsotope()) {
this->expandQuery(makeAtomIsotopeQuery(other.getIsotope()),
Queries::CompositeQueryType::COMPOSITE_AND);
}
if (other.getFormalCharge()) {
this->expandQuery(makeAtomFormalChargeQuery(other.getFormalCharge()),
Queries::CompositeQueryType::COMPOSITE_AND);
}
if (other.getNumRadicalElectrons()) {
this->expandQuery(
makeAtomNumRadicalElectronsQuery(other.getNumRadicalElectrons()),
Queries::CompositeQueryType::COMPOSITE_AND);
}
}
QueryAtom(const QueryAtom &other) : Atom(other) {
if (other.dp_query) {
dp_query = other.dp_query->copy();
} else {
dp_query = nullptr;
}
}
QueryAtom &operator=(const QueryAtom &other) {
if (this == &other) {
return *this;
}
Atom::operator=(other);
delete dp_query;
if (other.dp_query) {
dp_query = other.dp_query->copy();
} else {
dp_query = nullptr;
}
return *this;
}
QueryAtom(QueryAtom &&other) noexcept : Atom(std::move(other)) {
dp_query = std::exchange(other.dp_query, nullptr);
}
QueryAtom &operator=(QueryAtom &&other) noexcept {
if (this == &other) {
return *this;
}
QueryAtom::operator=(std::move(other));
dp_query = std::exchange(other.dp_query, nullptr);
return *this;
}
~QueryAtom() override;
//! returns a copy of this query, owned by the caller
Atom *copy() const override;
// This method can be used to distinguish query atoms from standard atoms:
bool hasQuery() const override { return dp_query != nullptr; }
//! returns the label associated to this query
std::string getQueryType() const override { return dp_query->getTypeLabel(); }
//! replaces our current query with the value passed in
void setQuery(QUERYATOM_QUERY *what) override {
delete dp_query;
dp_query = what;
}
//! returns our current query
QUERYATOM_QUERY *getQuery() const override { return dp_query; }
//! expands our current query
/*!
\param what the Queries::Query to be added. The ownership of
the query is passed to the current object, where it
might be deleted, so that the pointer should not be
used again in the calling code.
\param how the operator to be used in the expansion
\param maintainOrder (optional) flags whether the relative order of
the queries needs to be maintained, if this is
false, the order is reversed
<b>Notes:</b>
- \c what should probably be constructed using one of the functions
defined in QueryOps.h
- the \c maintainOrder option can be useful because the combination
operators short circuit when possible.
*/
void expandQuery(QUERYATOM_QUERY *what,
Queries::CompositeQueryType how = Queries::COMPOSITE_AND,
bool maintainOrder = true) override;
//! returns true if we match Atom \c what
bool Match(Atom const *what) const override;
//! returns true if our query details match those of QueryAtom \c what
bool QueryMatch(QueryAtom const *what) const;
private:
QUERYATOM_QUERY *dp_query{nullptr};
}; // end o' class
namespace detail {
inline std::string qhelper(const Atom::QUERYATOM_QUERY *q, unsigned int depth) {
std::string res = "";
if (q) {
for (unsigned int i = 0; i < depth; ++i) {
res += " ";
}
res += q->getFullDescription() + "\n";
for (const auto &child :
boost::make_iterator_range(q->beginChildren(), q->endChildren())) {
res += qhelper(child.get(), depth + 1);
}
}
return res;
}
} // namespace detail
inline std::string describeQuery(const Atom *atom) {
PRECONDITION(atom, "bad atom");
std::string res = "";
if (atom->hasQuery()) {
res = detail::qhelper(atom->getQuery(), 0);
}
return res;
}
}; // namespace RDKit
#endif
Atom.h
Defines the Atom class and associated typedefs.
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
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:75
RDKit::Atom::operator=
Atom & operator=(const Atom &other)
RDKit::Atom::QUERYATOM_QUERY
Queries::Query< int, Atom const *, true > QUERYATOM_QUERY
Definition Atom.h:85
RDKit::Atom::getNumRadicalElectrons
unsigned int getNumRadicalElectrons() const
returns the number of radical electrons for this Atom
Definition Atom.h:203
RDKit::Atom::getAtomicNum
int getAtomicNum() const
returns our atomic number
Definition Atom.h:141
RDKit::Atom::hasQuery
virtual bool hasQuery() const
Definition Atom.h:264
RDKit::Atom::getFormalCharge
int getFormalCharge() const
returns the formal charge of this atom
Definition Atom.h:207
RDKit::Atom::getIsotope
unsigned int getIsotope() const
returns our isotope number
Definition Atom.h:233
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