33#ifndef __RD_FILTER_MATCHER_H__
34#define __RD_FILTER_MATCHER_H__
45std::string getArgName(
const boost::shared_ptr<FilterMatcherBase> &arg) {
47 return arg->getName();
49 return "<nullmatcher>";
53namespace FilterMatchOps {
55 boost::shared_ptr<FilterMatcherBase> arg1;
56 boost::shared_ptr<FilterMatcherBase> arg2;
68 And(boost::shared_ptr<FilterMatcherBase> arg1,
69 boost::shared_ptr<FilterMatcherBase> arg2)
71 arg1(std::move(arg1)),
72 arg2(std::move(arg2)) {}
79 getArgName(arg2) +
")";
83 return arg1.get() && arg2.get() && arg1->isValid() && arg2->isValid();
88 "FilterMatchOps::And is not valid, null arg1 or arg2");
89 return arg1->hasMatch(mol) && arg2->hasMatch(mol);
93 std::vector<FilterMatch> &matchVect)
const override {
95 "FilterMatchOps::And is not valid, null arg1 or arg2");
96 std::vector<FilterMatch> matches;
97 if (arg1->getMatches(mol, matches) && arg2->getMatches(mol, matches)) {
104 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
105 return boost::shared_ptr<FilterMatcherBase>(
new And(*
this));
109#ifdef RDK_USE_BOOST_SERIALIZATION
110 friend class boost::serialization::access;
111 template <
class Archive>
112 void serialize(Archive &ar,
const unsigned int version) {
114 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
123 boost::shared_ptr<FilterMatcherBase> arg1;
124 boost::shared_ptr<FilterMatcherBase> arg2;
135 Or(boost::shared_ptr<FilterMatcherBase> arg1,
136 boost::shared_ptr<FilterMatcherBase> arg2)
143 getArgName(arg2) +
")";
147 return arg1.get() && arg2.get() && arg1->isValid() && arg2->isValid();
151 PRECONDITION(isValid(),
"Or is not valid, null arg1 or arg2");
152 return arg1->hasMatch(mol) || arg2->hasMatch(mol);
156 std::vector<FilterMatch> &matchVect)
const override {
158 "FilterMatchOps::Or is not valid, null arg1 or arg2");
161 bool res1 = arg1->getMatches(mol, matchVect);
162 bool res2 = arg2->getMatches(mol, matchVect);
166 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
167 return boost::shared_ptr<FilterMatcherBase>(
new Or(*
this));
170#ifdef RDK_USE_BOOST_SERIALIZATION
171 friend class boost::serialization::access;
172 template <
class Archive>
173 void serialize(Archive &ar,
const unsigned int version) {
175 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
183 boost::shared_ptr<FilterMatcherBase> arg1;
196 Not(boost::shared_ptr<FilterMatcherBase> arg1)
205 bool isValid()
const override {
return arg1.get() && arg1->isValid(); }
208 PRECONDITION(isValid(),
"FilterMatchOps::Not: arg1 is null");
209 return !arg1->hasMatch(mol);
213 PRECONDITION(isValid(),
"FilterMatchOps::Not: arg1 is null");
216 std::vector<FilterMatch> matchVect;
217 return !arg1->getMatches(mol, matchVect);
220 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
221 return boost::shared_ptr<FilterMatcherBase>(
new Not(*
this));
225#ifdef RDK_USE_BOOST_SERIALIZATION
226 friend class boost::serialization::access;
227 template <
class Archive>
228 void serialize(Archive &ar,
const unsigned int version) {
230 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
240 unsigned int d_min_count{0};
241 unsigned int d_max_count;
249 d_max_count(UINT_MAX) {}
262 unsigned int maxCount = UINT_MAX);
277 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
291 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
305 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
310 bool isValid()
const override {
return d_pattern.get(); }
331 std::vector<FilterMatch> &matchVect)
const override;
333 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
334 return boost::shared_ptr<FilterMatcherBase>(
new SmartsMatcher(*
this));
338#ifdef RDK_USE_BOOST_SERIALIZATION
339 friend class boost::serialization::access;
340 template <
class Archive>
341 void save(Archive &ar,
const unsigned int version)
const {
343 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
350 template <
class Archive>
351 void load(Archive &ar,
const unsigned int version) {
352 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
357 d_pattern = boost::shared_ptr<ROMol>(
new ROMol(res));
362 BOOST_SERIALIZATION_SPLIT_MEMBER();
380 std::vector<boost::shared_ptr<FilterMatcherBase>> d_offPatterns;
393 ExclusionList(std::vector<boost::shared_ptr<FilterMatcherBase>> offPatterns)
395 d_offPatterns(std::move(offPatterns)) {}
400 for (
size_t i = 0; i < d_offPatterns.size(); ++i) {
401 res +=
" " + d_offPatterns[i]->getName();
408 for (
size_t i = 0; i < d_offPatterns.size(); ++i) {
409 if (!d_offPatterns[i]->isValid()) {
418 d_offPatterns.push_back(base.
copy());
422 const std::vector<boost::shared_ptr<FilterMatcherBase>> &offPatterns) {
423 d_offPatterns = offPatterns;
428 "ExclusionList: one of the exclusion pattens is invalid");
430 for (
size_t i = 0; i < d_offPatterns.size() && result; ++i) {
431 result &= !d_offPatterns[i]->hasMatch(mol);
439 "ExclusionList: one of the exclusion pattens is invalid");
441 for (
size_t i = 0; i < d_offPatterns.size() && result; ++i) {
442 result &= !d_offPatterns[i]->hasMatch(mol);
448 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
449 return boost::shared_ptr<FilterMatcherBase>(
new ExclusionList(*
this));
453#ifdef RDK_USE_BOOST_SERIALIZATION
454 friend class boost::serialization::access;
455 template <
class Archive>
456 void serialize(Archive &ar,
const unsigned int version) {
458 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
466 std::vector<boost::shared_ptr<FilterHierarchyMatcher>> d_children;
467 boost::shared_ptr<FilterMatcherBase> d_matcher;
485 if (d_matcher.get()) {
486 return d_matcher->getName();
488 return "FilterMatcherHierarchy root";
492 bool isValid()
const override {
return d_matcher->isValid(); }
500 d_matcher = matcher.
copy();
501 PRECONDITION(getName() == d_matcher->getName(),
"Opps");
510 boost::shared_ptr<FilterHierarchyMatcher>
addChild(
512 PRECONDITION(hierarchy.d_matcher.get() && hierarchy.d_matcher->isValid(),
513 "Only one root node is allowed in a FilterHierarchyMatcher");
515 d_children.push_back(boost::shared_ptr<FilterHierarchyMatcher>(
517 return d_children.back();
526 std::vector<FilterMatch> &matches)
const override;
533 std::vector<FilterMatch> temp;
534 return getMatches(mol, temp);
538 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
539 return boost::shared_ptr<FilterMatcherBase>(
544#ifdef RDK_USE_BOOST_SERIALIZATION
545 friend class boost::serialization::access;
546 template <
class Archive>
547 void serialize(Archive &ar,
const unsigned int version) {
549 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
556#ifdef RDK_USE_BOOST_SERIALIZATION
558template <
class Archive>
559void registerFilterMatcherTypes(Archive &ar) {
560 ar.register_type(
static_cast<FilterMatchOps::And *
>(
nullptr));
561 ar.register_type(
static_cast<FilterMatchOps::Or *
>(
nullptr));
562 ar.register_type(
static_cast<FilterMatchOps::Not *
>(
nullptr));
563 ar.register_type(
static_cast<SmartsMatcher *
>(
nullptr));
564 ar.register_type(
static_cast<ExclusionList *
>(
nullptr));
565 ar.register_type(
static_cast<FilterHierarchyMatcher *
>(
nullptr));
570#ifdef RDK_USE_BOOST_SERIALIZATION
572BOOST_CLASS_VERSION(
RDKit::ExclusionList, 1)
573BOOST_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
virtual std::string getName() const
static void pickleMol(const ROMol *mol, std::ostream &ss)
pickles a molecule and sends the results to stream ss
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
boost::shared_ptr< ROMol > ROMOL_SPTR
RDKIT_FILTERCATALOG_EXPORT const char * SMARTS_MATCH_NAME_DEFAULT