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