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 <utility>
36
37#include <RDGeneral/types.h> // For Dict
38#include <GraphMol/RDKitBase.h>
41
42#ifdef RDK_USE_BOOST_SERIALIZATION
44#include <boost/archive/text_oarchive.hpp>
45#include <boost/archive/text_iarchive.hpp>
46#include <boost/serialization/vector.hpp>
47#include <boost/serialization/shared_ptr.hpp>
49#endif
50
51#include "FilterMatchers.h"
52
53namespace RDKit {
54typedef std::map<std::string, std::string> STRING_PROPS;
55
58 private:
59 boost::shared_ptr<FilterMatcherBase> d_matcher;
60 Dict d_props;
61
62 public:
63 FilterCatalogEntry() : d_matcher(), d_props() {}
64
65 FilterCatalogEntry(const std::string &name, const FilterMatcherBase &matcher)
66 : RDCatalog::CatalogEntry(), d_matcher(matcher.copy()) {
67 setDescription(name);
68 }
69
70 FilterCatalogEntry(const std::string &name,
71 boost::shared_ptr<FilterMatcherBase> matcher)
72 : RDCatalog::CatalogEntry(), d_matcher(std::move(matcher)) {
73 setDescription(name);
74 }
75
77 : RDCatalog::CatalogEntry(rhs),
78 d_matcher(rhs.d_matcher),
79 d_props(rhs.d_props) {}
80
81 ~FilterCatalogEntry() override {}
82
83 //------------------------------------
84 //! Returns true if the Filters stored in this catalog entry are valid
85
86 bool isValid() const { return d_matcher.get() && d_matcher->isValid(); }
87
88 //------------------------------------
89 //! Returns the description of the catalog entry
90 std::string getDescription() const override;
91
92 //------------------------------------
93 //! Sets the description of the catalog entry
94 /*
95 \param const std::string & description
96 */
97 void setDescription(const std::string &description);
98
99 //! \name Properties
100 //! @{
101
102 //! returns a list with the names of our \c properties
103 STR_VECT getPropList() const { return d_props.keys(); }
104
105 //! sets a \c property value
106 /*!
107 \param key the name under which the \c property should be stored.
108 If a \c property is already stored under this name, it will be
109 replaced.
110 \param val the value to be stored
111 */
112 template <typename T>
113 void setProp(const char *key, T val) {
114 std::string what(key);
115 setProp(what, val);
116 }
117 //! \overload
118 template <typename T>
119 void setProp(const std::string &key, T val) {
120 d_props.setVal(key, val);
121 }
122
123 //! allows retrieval of a particular property value
124 /*!
125
126 \param key the name under which the \c property should be stored.
127 If a \c property is already stored under this name, it will be
128 replaced.
129 \param res a reference to the storage location for the value.
130
131 <b>Notes:</b>
132 - if no \c property with name \c key exists, a KeyErrorException will be
133 thrown.
134 - the \c boost::lexical_cast machinery is used to attempt type
135 conversions.
136 If this fails, a \c boost::bad_lexical_cast exception will be thrown.
137
138 */
139 template <typename T>
140 void getProp(const char *key, T &res) const {
141 d_props.getVal(key, res);
142 }
143 //! \overload
144 template <typename T>
145 void getProp(const std::string &key, T &res) const {
146 d_props.getVal(key, res);
147 }
148 //! \overload
149 template <typename T>
150 T getProp(const char *key) const {
151 return d_props.getVal<T>(key);
152 }
153 //! \overload
154 template <typename T>
155 T getProp(const std::string &key) const {
156 return d_props.getVal<T>(key);
157 }
158
159 //! returns whether or not we have a \c property with name \c key
160 //! and assigns the value if we do
161 template <typename T>
162 bool getPropIfPresent(const char *key, T &res) const {
163 return d_props.getValIfPresent(key, res);
164 }
165 //! \overload
166 template <typename T>
167 bool getPropIfPresent(const std::string &key, T &res) const {
168 return d_props.getValIfPresent(key, res);
169 }
170
171 //! returns whether or not we have a \c property with name \c key
172 bool hasProp(const char *key) const { return d_props.hasVal(key); }
173 //! \overload
174 bool hasProp(const std::string &key) const {
175 return d_props.hasVal(key);
176 // return hasProp(key.c_str());
177 }
178
179 //! clears the value of a \c property
180 void clearProp(const char *key) {
181 std::string what(key);
182 clearProp(what);
183 }
184 //! \overload
185 void clearProp(const std::string &key) { d_props.clearVal(key); }
186
187 // -------------------------------------------
188 //! Properties usually contain the reference and source
189 //! for the catalog entry.
190
191 Dict &getProps() { return d_props; }
192 const Dict &getProps() const { return d_props; }
193 void setProps(const Dict &props) { d_props = props; }
194
195 //------------------------------------
196 //! Returns the matching filters for this catalog entry
197 /*
198 \param mol The molecule to match against
199 \param std::vector<FilterMatch> the resulting FilterMatches
200 */
201 bool getFilterMatches(const ROMol &mol,
202 std::vector<FilterMatch> &matchVect) const {
203 return this->isValid() && d_matcher->getMatches(mol, matchVect);
204 }
205
206 //------------------------------------
207 //! Returns true if the filters in this catalog entry match the molecule
208 /*
209 \param mol The molecule to match against
210 */
211
212 bool hasFilterMatch(const ROMol &mol) const {
213 return this->isValid() && d_matcher->hasMatch(mol);
214 }
215
216 //! serializes (pickles) to a stream
217 void toStream(std::ostream &ss) const override;
218 //! returns a string with a serialized (pickled) representation
219 std::string Serialize() const override;
220 //! initializes from a stream pickle
221 void initFromStream(std::istream &ss) override;
222 //! initializes from a string pickle
223 void initFromString(const std::string &text) override;
224
225 private:
226#ifdef RDK_USE_BOOST_SERIALIZATION
227 friend class boost::serialization::access;
228 template <class Archive>
229 void save(Archive &ar, const unsigned int version) const {
230 RDUNUSED_PARAM(version);
231 registerFilterMatcherTypes(ar);
232
233 ar & d_matcher;
234 // we only save string based props here...
235 STR_VECT keys = d_props.keys();
236 std::vector<std::string> string_props;
237 for (size_t i = 0; i < keys.size(); ++i) {
238 std::string val;
239 try {
240 if (d_props.getValIfPresent<std::string>(keys[i], val)) {
241 string_props.push_back(keys[i]);
242 string_props.push_back(val);
243 }
244 } catch (const std::bad_any_cast &) {
245 // pass, can't serialize
246 // warning, this changes properties types, see Dict.cpp
247 }
248 }
249 ar & string_props;
250 }
251
252 template <class Archive>
253 void load(Archive &ar, const unsigned int version) {
254 RDUNUSED_PARAM(version);
255 registerFilterMatcherTypes(ar);
256
257 ar & d_matcher;
258 std::vector<std::string> string_props;
259 ar & string_props;
260 d_props.reset();
261
262 for (size_t i = 0; i < string_props.size() / 2; ++i) {
263 d_props.setVal(string_props[i * 2], string_props[i * 2 + 1]);
264 }
265 }
266
267 BOOST_SERIALIZATION_SPLIT_MEMBER();
268#endif
269};
270} // namespace RDKit
271
272#ifdef RDK_USE_BOOST_SERIALIZATION
273BOOST_CLASS_VERSION(RDKit::FilterCatalogEntry, 1);
274#endif
275
276#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:329
STR_VECT keys() const
Returns the set of keys in the dictionary.
Definition Dict.h:160
bool hasVal(const std::string &what) const
Returns whether or not the dictionary contains a particular key.
Definition Dict.h:146
bool getValIfPresent(const std::string &what, T &res) const
Potentially gets the value associated with a particular key returns true on success/false on failure.
Definition Dict.h:224
void getVal(const std::string &what, T &res) const
Gets the value associated with a particular key.
Definition Dict.h:183
void clearVal(const std::string &what)
Clears the value associated with a particular key, removing the key from the dictionary.
Definition Dict.h:314
void setVal(const std::string &what, T &val)
Sets the value associated with a key.
Definition Dict.h:259
bool hasProp(const std::string &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void getProp(const char *key, T &res) const
allows retrieval of a particular property value
void getProp(const std::string &key, T &res) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void toStream(std::ostream &ss) const override
serializes (pickles) to a stream
void clearProp(const std::string &key)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void initFromString(const std::string &text) override
initializes from a string pickle
void setProp(const char *key, T val)
sets a property value
bool getPropIfPresent(const char *key, T &res) const
bool getPropIfPresent(const std::string &key, T &res) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
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.
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)
T getProp(const char *key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void clearProp(const char *key)
clears the value of a property
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
void setProp(const std::string &key, T val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool hasProp(const char *key) const
returns whether or not we have a property with name key
T getProp(const std::string &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
#define RDKIT_FILTERCATALOG_EXPORT
Definition export.h:169
Std stuff.
std::vector< std::string > STR_VECT
Definition Dict.h:29
std::map< std::string, std::string > STRING_PROPS