33#ifndef __RD_FILTER_MATCHER_H__
34#define __RD_FILTER_MATCHER_H__
42#ifdef RDK_USE_BOOST_SERIALIZATION
44#include <boost/serialization/shared_ptr.hpp>
51std::string
getArgName(
const boost::shared_ptr<FilterMatcherBase> &
arg) {
53 return arg->getName();
55 return "<nullmatcher>";
59namespace FilterMatchOps {
61 boost::shared_ptr<FilterMatcherBase> arg1;
62 boost::shared_ptr<FilterMatcherBase> arg2;
74 And(boost::shared_ptr<FilterMatcherBase> arg1,
75 boost::shared_ptr<FilterMatcherBase> arg2)
77 arg1(std::move(arg1)),
78 arg2(std::move(arg2)) {}
84 return "(" + getArgName(arg1) +
" " + FilterMatcherBase::getName() +
" " +
85 getArgName(arg2) +
")";
89 return arg1.get() && arg2.get() && arg1->isValid() && arg2->isValid();
94 "FilterMatchOps::And is not valid, null arg1 or arg2");
95 return arg1->hasMatch(mol) && arg2->hasMatch(mol);
99 std::vector<FilterMatch> &matchVect)
const override {
101 "FilterMatchOps::And is not valid, null arg1 or arg2");
102 std::vector<FilterMatch> matches;
103 if (arg1->getMatches(mol, matches) && arg2->getMatches(mol, matches)) {
110 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
111 return boost::shared_ptr<FilterMatcherBase>(
new And(*
this));
115#ifdef RDK_USE_BOOST_SERIALIZATION
116 friend class boost::serialization::access;
117 template <
class Archive>
118 void serialize(Archive &ar,
const unsigned int version) {
120 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
129 boost::shared_ptr<FilterMatcherBase> arg1;
130 boost::shared_ptr<FilterMatcherBase> arg2;
141 Or(boost::shared_ptr<FilterMatcherBase> arg1,
142 boost::shared_ptr<FilterMatcherBase> arg2)
148 return "(" + getArgName(arg1) +
" " + FilterMatcherBase::getName() +
" " +
149 getArgName(arg2) +
")";
153 return arg1.get() && arg2.get() && arg1->isValid() && arg2->isValid();
157 PRECONDITION(isValid(),
"Or is not valid, null arg1 or arg2");
158 return arg1->hasMatch(mol) || arg2->hasMatch(mol);
162 std::vector<FilterMatch> &matchVect)
const override {
164 "FilterMatchOps::Or is not valid, null arg1 or arg2");
167 bool res1 = arg1->getMatches(mol, matchVect);
168 bool res2 = arg2->getMatches(mol, matchVect);
172 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
173 return boost::shared_ptr<FilterMatcherBase>(
new Or(*
this));
176#ifdef RDK_USE_BOOST_SERIALIZATION
177 friend class boost::serialization::access;
178 template <
class Archive>
179 void serialize(Archive &ar,
const unsigned int version) {
181 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
189 boost::shared_ptr<FilterMatcherBase> arg1;
202 Not(boost::shared_ptr<FilterMatcherBase> arg1)
208 return "(" + FilterMatcherBase::getName() +
" " + getArgName(arg1) +
")";
211 bool isValid()
const override {
return arg1.get() && arg1->isValid(); }
214 PRECONDITION(isValid(),
"FilterMatchOps::Not: arg1 is null");
215 return !arg1->hasMatch(mol);
219 PRECONDITION(isValid(),
"FilterMatchOps::Not: arg1 is null");
222 std::vector<FilterMatch> matchVect;
223 return !arg1->getMatches(mol, matchVect);
226 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
227 return boost::shared_ptr<FilterMatcherBase>(
new Not(*
this));
231#ifdef RDK_USE_BOOST_SERIALIZATION
232 friend class boost::serialization::access;
233 template <
class Archive>
234 void serialize(Archive &ar,
const unsigned int version) {
236 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
246 unsigned int d_min_count{0};
247 unsigned int d_max_count;
255 d_max_count(UINT_MAX) {}
268 unsigned int maxCount = UINT_MAX);
283 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
297 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
311 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
316 bool isValid()
const override {
return d_pattern.get(); }
337 std::vector<FilterMatch> &matchVect)
const override;
339 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
340 return boost::shared_ptr<FilterMatcherBase>(
new SmartsMatcher(*
this));
344#ifdef RDK_USE_BOOST_SERIALIZATION
345 friend class boost::serialization::access;
346 template <
class Archive>
347 void save(Archive &ar,
const unsigned int version)
const {
349 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
351 MolPickler::pickleMol(*d_pattern.get(), res);
356 template <
class Archive>
357 void load(Archive &ar,
const unsigned int version) {
358 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
362 d_pattern = boost::shared_ptr<ROMol>(
new ROMol(res));
366 BOOST_SERIALIZATION_SPLIT_MEMBER();
384 std::vector<boost::shared_ptr<FilterMatcherBase>> d_offPatterns;
397 ExclusionList(std::vector<boost::shared_ptr<FilterMatcherBase>> offPatterns)
399 d_offPatterns(std::move(offPatterns)) {}
403 res =
"(" + FilterMatcherBase::getName();
404 for (
size_t i = 0; i < d_offPatterns.size(); ++i) {
405 res +=
" " + d_offPatterns[i]->getName();
412 for (
size_t i = 0; i < d_offPatterns.size(); ++i) {
413 if (!d_offPatterns[i]->isValid()) {
422 d_offPatterns.push_back(base.
copy());
426 const std::vector<boost::shared_ptr<FilterMatcherBase>> &offPatterns) {
427 d_offPatterns = offPatterns;
432 "ExclusionList: one of the exclusion pattens is invalid");
434 for (
size_t i = 0; i < d_offPatterns.size() && result; ++i) {
435 result &= !d_offPatterns[i]->hasMatch(mol);
443 "ExclusionList: one of the exclusion pattens is invalid");
445 for (
size_t i = 0; i < d_offPatterns.size() && result; ++i) {
446 result &= !d_offPatterns[i]->hasMatch(mol);
452 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
453 return boost::shared_ptr<FilterMatcherBase>(
new ExclusionList(*
this));
457#ifdef RDK_USE_BOOST_SERIALIZATION
458 friend class boost::serialization::access;
459 template <
class Archive>
460 void serialize(Archive &ar,
const unsigned int version) {
462 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
470 std::vector<boost::shared_ptr<FilterHierarchyMatcher>> d_children;
471 boost::shared_ptr<FilterMatcherBase> d_matcher;
489 if (d_matcher.get()) {
490 return d_matcher->getName();
492 return "FilterMatcherHierarchy root";
496 bool isValid()
const override {
return d_matcher->isValid(); }
504 d_matcher = matcher.
copy();
505 PRECONDITION(getName() == d_matcher->getName(),
"Opps");
514 boost::shared_ptr<FilterHierarchyMatcher>
addChild(
516 PRECONDITION(hierarchy.d_matcher.get() && hierarchy.d_matcher->isValid(),
517 "Only one root node is allowed in a FilterHierarchyMatcher");
519 d_children.push_back(boost::shared_ptr<FilterHierarchyMatcher>(
521 return d_children.back();
530 std::vector<FilterMatch> &matches)
const override;
537 std::vector<FilterMatch> temp;
538 return getMatches(mol, temp);
542 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
543 return boost::shared_ptr<FilterMatcherBase>(
548#ifdef RDK_USE_BOOST_SERIALIZATION
549 friend class boost::serialization::access;
550 template <
class Archive>
551 void serialize(Archive &ar,
const unsigned int version) {
553 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
560#ifdef RDK_USE_BOOST_SERIALIZATION
562template <
class Archive>
564 ar.register_type(
static_cast<FilterMatchOps::And *
>(
nullptr));
565 ar.register_type(
static_cast<FilterMatchOps::Or *
>(
nullptr));
566 ar.register_type(
static_cast<FilterMatchOps::Not *
>(
nullptr));
567 ar.register_type(
static_cast<SmartsMatcher *
>(
nullptr));
568 ar.register_type(
static_cast<ExclusionList *
>(
nullptr));
569 ar.register_type(
static_cast<FilterHierarchyMatcher *
>(
nullptr));
574#ifdef RDK_USE_BOOST_SERIALIZATION
576BOOST_CLASS_VERSION(
RDKit::ExclusionList, 1)
577BOOST_CLASS_VERSION(
RDKit::FilterHierarchyMatcher, 1)
#define RDUNUSED_PARAM(x)
#define PRECONDITION(expr, mess)
pulls in the core RDKit functionality
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &) const override
getMatches
bool hasMatch(const ROMol &mol) const override
hasMatches
void setExclusionPatterns(const std::vector< boost::shared_ptr< FilterMatcherBase > > &offPatterns)
ExclusionList(std::vector< boost::shared_ptr< FilterMatcherBase > > offPatterns)
void addPattern(const FilterMatcherBase &base)
bool isValid() const override
std::string getName() const override
boost::shared_ptr< FilterMatcherBase > copy() const override
void setPattern(const FilterMatcherBase &matcher)
Set a new FilterMatcherBase for this node.
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &matches) const override
returns the FilterMatches against the given molecule
bool isValid() const override
returns true if this node has a valid matcher
boost::shared_ptr< FilterMatcherBase > copy() const override
copys the FilterHierarchyMatcher into a FilterMatcherBase
std::string getName() const override
Return the name for this node (from the underlying FilterMatcherBase)
FilterHierarchyMatcher(const FilterMatcherBase &matcher)
boost::shared_ptr< FilterHierarchyMatcher > addChild(const FilterHierarchyMatcher &hierarchy)
bool hasMatch(const ROMol &mol) const override
Does this node match the molecule.
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &matchVect) const override
getMatches
And(const FilterMatcherBase &arg1, const FilterMatcherBase &arg2)
std::string getName() const override
bool hasMatch(const ROMol &mol) const override
hasMatches
bool isValid() const override
boost::shared_ptr< FilterMatcherBase > copy() const override
And(boost::shared_ptr< FilterMatcherBase > arg1, boost::shared_ptr< FilterMatcherBase > arg2)
bool isValid() const override
bool hasMatch(const ROMol &mol) const override
hasMatches
std::string getName() const override
Not(const FilterMatcherBase &arg1)
boost::shared_ptr< FilterMatcherBase > copy() const override
Not(boost::shared_ptr< FilterMatcherBase > arg1)
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &) const override
getMatches
Or(boost::shared_ptr< FilterMatcherBase > arg1, boost::shared_ptr< FilterMatcherBase > arg2)
bool hasMatch(const ROMol &mol) const override
hasMatches
bool isValid() const override
boost::shared_ptr< FilterMatcherBase > copy() const override
Or(const FilterMatcherBase &arg1, const FilterMatcherBase &arg2)
std::string getName() const override
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &matchVect) const override
getMatches
virtual boost::shared_ptr< FilterMatcherBase > copy() const =0
virtual bool isValid() const =0
bool isValid() const override
Returns True if the Smarts pattern is valid.
void setPattern(const ROMol &mol)
Set the query molecule for the matcher.
unsigned int getMaxCount() const
Get the maximum match count for the pattern to be true.
unsigned int getMinCount() const
Get the minimum match count for the pattern to be true.
bool hasMatch(const ROMol &mol) const override
hasMatches
SmartsMatcher(const ROMol &pattern, unsigned int minCount=1, unsigned int maxCount=UINT_MAX)
Construct a SmartsMatcher from a query molecule.
void setPattern(const std::string &smarts)
Set the smarts pattern for the matcher.
const ROMOL_SPTR & getPattern() const
Return the shared_ptr to the underlying query molecule.
boost::shared_ptr< FilterMatcherBase > copy() const override
void setPattern(const ROMOL_SPTR &pat)
Set the shared query molecule for the matcher.
void setMinCount(unsigned int val)
Set the minimum match count for the pattern to be true.
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &matchVect) const override
getMatches
void setMaxCount(unsigned int val)
Set the maximum match count for the pattern to be true.
SmartsMatcher(const std::string &name, ROMOL_SPTR onPattern, unsigned int minCount=1, unsigned int maxCount=UINT_MAX)
Construct a SmartsMatcher from a shared_ptr.
SmartsMatcher(const std::string &name=SMARTS_MATCH_NAME_DEFAULT)
Construct a SmartsMatcher.
SmartsMatcher(const std::string &name, const ROMol &pattern, unsigned int minCount=1, unsigned int maxCount=UINT_MAX)
Construct a SmartsMatcher.
SmartsMatcher(const SmartsMatcher &rhs)
SmartsMatcher(const std::string &name, const std::string &smarts, unsigned int minCount=1, unsigned int maxCount=UINT_MAX)
Construct a SmartsMatcher from a smarts pattern.
#define RDKIT_FILTERCATALOG_EXPORT
bool rdvalue_is(const RDValue_cast_t)
boost::shared_ptr< ROMol > ROMOL_SPTR
RDKIT_FILTERCATALOG_EXPORT const char * SMARTS_MATCH_NAME_DEFAULT