33 #ifndef __RD_FILTER_MATCHER_H__
34 #define __RD_FILTER_MATCHER_H__
45 std::string getArgName(
const boost::shared_ptr<FilterMatcherBase> &arg) {
46 if (arg.get())
return arg->getName();
47 return "<nullmatcher>";
51 namespace FilterMatchOps {
53 boost::shared_ptr<FilterMatcherBase> arg1;
54 boost::shared_ptr<FilterMatcherBase> arg2;
66 And(boost::shared_ptr<FilterMatcherBase> arg1,
67 boost::shared_ptr<FilterMatcherBase> arg2)
69 arg1(std::move(arg1)),
70 arg2(std::move(arg2)) {}
77 getArgName(arg2) +
")";
81 return arg1.get() && arg2.get() && arg1->isValid() && arg2->isValid();
86 "FilterMatchOps::And is not valid, null arg1 or arg2");
87 return arg1->hasMatch(mol) && arg2->hasMatch(mol);
91 std::vector<FilterMatch> &matchVect)
const override {
93 "FilterMatchOps::And is not valid, null arg1 or arg2");
94 std::vector<FilterMatch> matches;
95 if (arg1->getMatches(mol, matches) && arg2->getMatches(mol, matches)) {
102 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
103 return boost::shared_ptr<FilterMatcherBase>(
new And(*
this));
107 #ifdef RDK_USE_BOOST_SERIALIZATION
108 friend class boost::serialization::access;
109 template <
class Archive>
110 void serialize(Archive &ar,
const unsigned int version) {
112 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
121 boost::shared_ptr<FilterMatcherBase> arg1;
122 boost::shared_ptr<FilterMatcherBase> arg2;
133 Or(boost::shared_ptr<FilterMatcherBase> arg1,
134 boost::shared_ptr<FilterMatcherBase> arg2)
141 getArgName(arg2) +
")";
145 return arg1.get() && arg2.get() && arg1->isValid() && arg2->isValid();
149 PRECONDITION(isValid(),
"Or is not valid, null arg1 or arg2");
150 return arg1->hasMatch(mol) || arg2->hasMatch(mol);
154 std::vector<FilterMatch> &matchVect)
const override {
156 "FilterMatchOps::Or is not valid, null arg1 or arg2");
159 bool res1 = arg1->getMatches(mol, matchVect);
160 bool res2 = arg2->getMatches(mol, matchVect);
164 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
165 return boost::shared_ptr<FilterMatcherBase>(
new Or(*
this));
168 #ifdef RDK_USE_BOOST_SERIALIZATION
169 friend class boost::serialization::access;
170 template <
class Archive>
171 void serialize(Archive &ar,
const unsigned int version) {
173 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
181 boost::shared_ptr<FilterMatcherBase> arg1;
194 Not(boost::shared_ptr<FilterMatcherBase> arg1)
203 bool isValid()
const override {
return arg1.get() && arg1->isValid(); }
206 PRECONDITION(isValid(),
"FilterMatchOps::Not: arg1 is null");
207 return !arg1->hasMatch(mol);
211 PRECONDITION(isValid(),
"FilterMatchOps::Not: arg1 is null");
214 std::vector<FilterMatch> matchVect;
215 return !arg1->getMatches(mol, matchVect);
218 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
219 return boost::shared_ptr<FilterMatcherBase>(
new Not(*
this));
223 #ifdef RDK_USE_BOOST_SERIALIZATION
224 friend class boost::serialization::access;
225 template <
class Archive>
226 void serialize(Archive &ar,
const unsigned int version) {
228 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
238 unsigned int d_min_count{0};
239 unsigned int d_max_count;
247 d_max_count(UINT_MAX) {}
260 unsigned int maxCount = UINT_MAX);
275 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
289 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
303 unsigned int minCount = 1,
unsigned int maxCount = UINT_MAX);
308 bool isValid()
const override {
return d_pattern.get(); }
329 std::vector<FilterMatch> &matchVect)
const override;
331 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
332 return boost::shared_ptr<FilterMatcherBase>(
new SmartsMatcher(*
this));
336 #ifdef RDK_USE_BOOST_SERIALIZATION
337 friend class boost::serialization::access;
338 template <
class Archive>
339 void save(Archive &ar,
const unsigned int version)
const {
341 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
348 template <
class Archive>
349 void load(Archive &ar,
const unsigned int version) {
350 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
355 d_pattern = boost::shared_ptr<ROMol>(
new ROMol(res));
360 BOOST_SERIALIZATION_SPLIT_MEMBER();
378 std::vector<boost::shared_ptr<FilterMatcherBase>> d_offPatterns;
391 ExclusionList(std::vector<boost::shared_ptr<FilterMatcherBase>> offPatterns)
393 d_offPatterns(std::move(offPatterns)) {}
398 for (
size_t i = 0; i < d_offPatterns.size(); ++i) {
399 res +=
" " + d_offPatterns[i]->getName();
406 for (
size_t i = 0; i < d_offPatterns.size(); ++i)
407 if (!d_offPatterns[i]->isValid())
return false;
413 d_offPatterns.push_back(base.
copy());
417 const std::vector<boost::shared_ptr<FilterMatcherBase>> &offPatterns) {
418 d_offPatterns = offPatterns;
423 "ExclusionList: one of the exclusion pattens is invalid");
425 for (
size_t i = 0; i < d_offPatterns.size() && result; ++i) {
426 result &= !d_offPatterns[i]->hasMatch(mol);
434 "ExclusionList: one of the exclusion pattens is invalid");
436 for (
size_t i = 0; i < d_offPatterns.size() && result; ++i) {
437 result &= !d_offPatterns[i]->hasMatch(mol);
443 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
444 return boost::shared_ptr<FilterMatcherBase>(
new ExclusionList(*
this));
448 #ifdef RDK_USE_BOOST_SERIALIZATION
449 friend class boost::serialization::access;
450 template <
class Archive>
451 void serialize(Archive &ar,
const unsigned int version) {
453 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
461 std::vector<boost::shared_ptr<FilterHierarchyMatcher>> d_children;
462 boost::shared_ptr<FilterMatcherBase> d_matcher;
480 if (d_matcher.get()) {
481 return d_matcher->getName();
483 return "FilterMatcherHierarchy root";
487 bool isValid()
const override {
return d_matcher->isValid(); }
495 d_matcher = matcher.
copy();
496 PRECONDITION(getName() == d_matcher->getName(),
"Opps");
505 boost::shared_ptr<FilterHierarchyMatcher>
addChild(
507 PRECONDITION(hierarchy.d_matcher.get() && hierarchy.d_matcher->isValid(),
508 "Only one root node is allowed in a FilterHierarchyMatcher");
510 d_children.push_back(boost::shared_ptr<FilterHierarchyMatcher>(
512 return d_children.back();
521 std::vector<FilterMatch> &matches)
const override;
528 std::vector<FilterMatch> temp;
529 return getMatches(mol, temp);
533 boost::shared_ptr<FilterMatcherBase>
copy()
const override {
534 return boost::shared_ptr<FilterMatcherBase>(
539 #ifdef RDK_USE_BOOST_SERIALIZATION
540 friend class boost::serialization::access;
541 template <
class Archive>
542 void serialize(Archive &ar,
const unsigned int version) {
544 ar &boost::serialization::base_object<FilterMatcherBase>(*
this);
551 #ifdef RDK_USE_BOOST_SERIALIZATION
553 template <
class Archive>
554 void registerFilterMatcherTypes(Archive &ar) {
555 ar.register_type(
static_cast<FilterMatchOps::And *
>(
nullptr));
556 ar.register_type(
static_cast<FilterMatchOps::Or *
>(
nullptr));
557 ar.register_type(
static_cast<FilterMatchOps::Not *
>(
nullptr));
558 ar.register_type(
static_cast<SmartsMatcher *
>(
nullptr));
559 ar.register_type(
static_cast<ExclusionList *
>(
nullptr));
560 ar.register_type(
static_cast<FilterHierarchyMatcher *
>(
nullptr));
565 #ifdef RDK_USE_BOOST_SERIALIZATION
567 BOOST_CLASS_VERSION(
RDKit::ExclusionList, 1)
568 BOOST_CLASS_VERSION(
RDKit::FilterHierarchyMatcher, 1)
#define RDUNUSED_PARAM(x)
#define PRECONDITION(expr, mess)
pulls in the core RDKit functionality
boost::shared_ptr< FilterMatcherBase > copy() const override
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &) const override
getMatches
bool hasMatch(const ROMol &mol) const override
hasMatches
ExclusionList(std::vector< boost::shared_ptr< FilterMatcherBase >> offPatterns)
void setExclusionPatterns(const std::vector< boost::shared_ptr< FilterMatcherBase >> &offPatterns)
void addPattern(const FilterMatcherBase &base)
bool isValid() const override
std::string getName() 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
boost::shared_ptr< FilterHierarchyMatcher > addChild(const FilterHierarchyMatcher &hierarchy)
std::string getName() const override
Return the name for this node (from the underlying FilterMatcherBase)
FilterHierarchyMatcher(const FilterMatcherBase &matcher)
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
And(boost::shared_ptr< FilterMatcherBase > arg1, boost::shared_ptr< FilterMatcherBase > arg2)
boost::shared_ptr< FilterMatcherBase > copy() const override
boost::shared_ptr< FilterMatcherBase > copy() const override
bool isValid() const override
bool hasMatch(const ROMol &mol) const override
hasMatches
std::string getName() const override
Not(const FilterMatcherBase &arg1)
Not(boost::shared_ptr< FilterMatcherBase > arg1)
bool getMatches(const ROMol &mol, std::vector< FilterMatch > &) const override
getMatches
boost::shared_ptr< FilterMatcherBase > copy() const override
Or(boost::shared_ptr< FilterMatcherBase > arg1, boost::shared_ptr< FilterMatcherBase > arg2)
bool hasMatch(const ROMol &mol) const override
hasMatches
bool isValid() 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 bool isValid() const =0
virtual std::string getName() const
virtual boost::shared_ptr< FilterMatcherBase > copy() const =0
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.
boost::shared_ptr< FilterMatcherBase > copy() const override
const ROMOL_SPTR & getPattern() const
Return the shared_ptr to the underlying query molecule.
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