RDKit: FilterCatalogEntry.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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