RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
FilterCatalogEntry.h
Go to the documentation of this file.
1// Copyright (c) 2015, Novartis Institutes for BioMedical Research Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following
12// disclaimer in the documentation and/or other materials provided
13// with the distribution.
14// * Neither the name of Novartis Institutes for BioMedical Research Inc.
15// nor the names of its contributors may be used to endorse or promote
16// products derived from this software without specific prior written
17// permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30//
31
32#include <RDGeneral/export.h>
33#ifndef __RD_FILTER_CATALOG_H__
34#define __RD_FILTER_CATALOG_H__
35#include <string_view>
36#include <utility>
37
38#include <RDGeneral/types.h> // For Dict
39#include <GraphMol/RDKitBase.h>
42
43#ifdef RDK_USE_BOOST_SERIALIZATION
45#include <boost/archive/text_oarchive.hpp>
46#include <boost/archive/text_iarchive.hpp>
47#include <boost/serialization/vector.hpp>
48#include <boost/serialization/shared_ptr.hpp>
50#endif
51
52#include "FilterMatchers.h"
53
54namespace RDKit {
55typedef std::map<std::string, std::string> STRING_PROPS;
56
59 private:
60 boost::shared_ptr<FilterMatcherBase> d_matcher;
61 Dict d_props;
62
63 public:
64 FilterCatalogEntry() : d_matcher(), d_props() {}
65
66 FilterCatalogEntry(const std::string &name, const FilterMatcherBase &matcher)
67 : RDCatalog::CatalogEntry(), d_matcher(matcher.copy()) {
68 setDescription(name);
69 }
70
71 FilterCatalogEntry(const std::string &name,
72 boost::shared_ptr<FilterMatcherBase> matcher)
73 : RDCatalog::CatalogEntry(), d_matcher(std::move(matcher)) {
74 setDescription(name);
75 }
76
78 : RDCatalog::CatalogEntry(rhs),
79 d_matcher(rhs.d_matcher),
80 d_props(rhs.d_props) {}
81
82 ~FilterCatalogEntry() override {}
83
84 //------------------------------------
85 //! Returns true if the Filters stored in this catalog entry are valid
86
87 bool isValid() const { return d_matcher.get() && d_matcher->isValid(); }
88
89 //------------------------------------
90 //! Returns the description of the catalog entry
91 std::string getDescription() const override;
92
93 //------------------------------------
94 //! Sets the description of the catalog entry
95 /*
96 \param const std::string & description
97 */
98 void setDescription(const std::string &description);
99
100 //! \name Properties
101 //! @{
102
103 //! returns a list with the names of our \c properties
104 STR_VECT getPropList() const { return d_props.keys(); }
105
106 //! sets a \c property value
107 /*!
108 \param key the name under which the \c property should be stored.
109 If a \c property is already stored under this name, it will be
110 replaced.
111 \param val the value to be stored
112 */
113 template <typename T>
114 void setProp(const std::string_view key, T val) {
115 d_props.setVal(key, val);
116 }
117
118 //! allows retrieval of a particular property value
119 /*!
120
121 \param key the name under which the \c property should be stored.
122 If a \c property is already stored under this name, it will be
123 replaced.
124 \param res a reference to the storage location for the value.
125
126 <b>Notes:</b>
127 - if no \c property with name \c key exists, a KeyErrorException will be
128 thrown.
129 - the \c boost::lexical_cast machinery is used to attempt type
130 conversions.
131 If this fails, a \c boost::bad_lexical_cast exception will be thrown.
132
133 */
134 template <typename T>
135 void getProp(const std::string_view key, T &res) const {
136 d_props.getVal(key, res);
137 }
138 //! \overload
139 template <typename T>
140 T getProp(const std::string_view key) const {
141 return d_props.getVal<T>(key);
142 }
143
144 //! returns whether or not we have a \c property with name \c key
145 //! and assigns the value if we do
146 template <typename T>
147 bool getPropIfPresent(const std::string_view key, T &res) const {
148 return d_props.getValIfPresent(key, res);
149 }
150
151 //! returns whether or not we have a \c property with name \c key
152 bool hasProp(const std::string_view key) const { return d_props.hasVal(key); }
153
154 //! clears the value of a \c property
155 void clearProp(const std::string_view key) { d_props.clearVal(key); }
156
157 // -------------------------------------------
158 //! Properties usually contain the reference and source
159 //! for the catalog entry.
160
161 Dict &getProps() { return d_props; }
162 const Dict &getProps() const { return d_props; }
163 void setProps(const Dict &props) { d_props = props; }
164
165 //------------------------------------
166 //! Returns the matching filters for this catalog entry
167 /*
168 \param mol The molecule to match against
169 \param std::vector<FilterMatch> the resulting FilterMatches
170 */
171 bool getFilterMatches(const ROMol &mol,
172 std::vector<FilterMatch> &matchVect) const {
173 return this->isValid() && d_matcher->getMatches(mol, matchVect);
174 }
175
176 //------------------------------------
177 //! Returns true if the filters in this catalog entry match the molecule
178 /*
179 \param mol The molecule to match against
180 */
181
182 bool hasFilterMatch(const ROMol &mol) const {
183 return this->isValid() && d_matcher->hasMatch(mol);
184 }
185
186 //! serializes (pickles) to a stream
187 void toStream(std::ostream &ss) const override;
188 //! returns a string with a serialized (pickled) representation
189 std::string Serialize() const override;
190 //! initializes from a stream pickle
191 void initFromStream(std::istream &ss) override;
192 //! initializes from a string pickle
193 void initFromString(const std::string &text) override;
194
195 private:
196#ifdef RDK_USE_BOOST_SERIALIZATION
197 friend class boost::serialization::access;
198 template <class Archive>
199 void save(Archive &ar, const unsigned int version) const {
200 RDUNUSED_PARAM(version);
201 registerFilterMatcherTypes(ar);
202
203 ar & d_matcher;
204 // we only save string based props here...
205 STR_VECT keys = d_props.keys();
206 std::vector<std::string> string_props;
207 for (size_t i = 0; i < keys.size(); ++i) {
208 std::string val;
209 try {
210 if (d_props.getValIfPresent<std::string>(keys[i], val)) {
211 string_props.push_back(keys[i]);
212 string_props.push_back(val);
213 }
214 } catch (const std::bad_any_cast &) {
215 // pass, can't serialize
216 // warning, this changes properties types, see Dict.cpp
217 }
218 }
219 ar & string_props;
220 }
221
222 template <class Archive>
223 void load(Archive &ar, const unsigned int version) {
224 RDUNUSED_PARAM(version);
225 registerFilterMatcherTypes(ar);
226
227 ar & d_matcher;
228 std::vector<std::string> string_props;
229 ar & string_props;
230 d_props.reset();
231
232 for (size_t i = 0; i < string_props.size() / 2; ++i) {
233 d_props.setVal(string_props[i * 2], string_props[i * 2 + 1]);
234 }
235 }
236
237 BOOST_SERIALIZATION_SPLIT_MEMBER();
238#endif
239};
240} // namespace RDKit
241
242#ifdef RDK_USE_BOOST_SERIALIZATION
243BOOST_CLASS_VERSION(RDKit::FilterCatalogEntry, 1);
244#endif
245
246#endif //__RD_FILTER_CATALOG_H__
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
pulls in the core RDKit functionality
Abstract base class to be used to represent an entry in a Catalog.
The Dict class can be used to store objects of arbitrary type keyed by strings.
Definition Dict.h:36
void reset()
Clears all keys (and values) from the dictionary.
Definition Dict.h:392
void setVal(const std::string_view what, T &val)
Sets the value associated with a key.
Definition Dict.h:291
void setProp(const std::string_view key, T val)
sets a property value
void toStream(std::ostream &ss) const override
serializes (pickles) to a stream
void initFromString(const std::string &text) override
initializes from a string pickle
FilterCatalogEntry(const std::string &name, boost::shared_ptr< FilterMatcherBase > matcher)
bool isValid() const
Returns true if the Filters stored in this catalog entry are valid.
bool getFilterMatches(const ROMol &mol, std::vector< FilterMatch > &matchVect) const
Returns the matching filters for this catalog entry.
bool getPropIfPresent(const std::string_view key, T &res) const
T getProp(const std::string_view key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void clearProp(const std::string_view key)
clears the value of a property
std::string Serialize() const override
returns a string with a serialized (pickled) representation
void setDescription(const std::string &description)
Sets the description of the catalog entry.
STR_VECT getPropList() const
returns a list with the names of our properties
bool hasFilterMatch(const ROMol &mol) const
Returns true if the filters in this catalog entry match the molecule.
void setProps(const Dict &props)
const Dict & getProps() const
FilterCatalogEntry(const FilterCatalogEntry &rhs)
std::string getDescription() const override
Returns the description of the catalog entry.
FilterCatalogEntry(const std::string &name, const FilterMatcherBase &matcher)
void initFromStream(std::istream &ss) override
initializes from a stream pickle
bool hasProp(const std::string_view key) const
returns whether or not we have a property with name key
void getProp(const std::string_view key, T &res) const
allows retrieval of a particular property value
#define RDKIT_FILTERCATALOG_EXPORT
Definition export.h:185
Std stuff.
std::vector< std::string > STR_VECT
Definition Dict.h:29
std::map< std::string, std::string > STRING_PROPS