RDKit
Open-source cheminformatics and machine learning.
FilterCatalog.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_PARAMS_
34 #define _RD_FILTER_CATALOG_PARAMS_
35 
36 #include <Catalogs/Catalog.h>
37 #include <Catalogs/CatalogParams.h>
38 #include "FilterCatalogEntry.h"
39 
40 namespace RDKit {
41 class FilterCatalog;
43  public:
45  PAINS_A = (1u << 1),
46  PAINS_B = (1u << 2),
47  PAINS_C = (1u << 3),
48  PAINS = PAINS_A | PAINS_B | PAINS_C,
49 
50  BRENK = (1u << 4),
51  NIH = (1u << 5),
52  ZINC = (1u << 6),
53 
54  ALL = PAINS | BRENK | NIH | ZINC
55  };
56 
57  FilterCatalogParams() : RDCatalog::CatalogParams() {
58  setTypeStr("Filter Catalog Parameters");
59  }
60 
61  FilterCatalogParams(FilterCatalogs catalogs) : RDCatalog::CatalogParams() {
62  setTypeStr("Filter Catalog Parameters");
63  addCatalog(catalogs);
64  }
65 
67  : RDCatalog::CatalogParams(other), d_catalogs(other.d_catalogs) {}
68 
69  virtual ~FilterCatalogParams() {}
70 
71  //------------------------------------
72  //! Adds an existing FilterCatalog specification to be used in the
73  // FilterCatalog
74  //
75  /*!
76  Specifies an existing filter catalog to be used.
77 
78  \param catalogs One of the enumerated known FilterCatalogs
79  */
80  virtual bool addCatalog(FilterCatalogs catalogs);
81 
82  //------------------------------------
83  //! Returns the existing list of FilterCatalogs to be used.
84  const std::vector<FilterCatalogs> &getCatalogs() const { return d_catalogs; }
85  //! Fill a catalog with the appropriate entries
86  virtual void fillCatalog(FilterCatalog &catalog) const;
87 
88  //! serializes (pickles) to a stream
89  virtual void toStream(std::ostream &ss) const;
90  //! returns a string with a serialized (pickled) representation
91  virtual std::string Serialize() const;
92  //! initializes from a stream pickle
93  virtual void initFromStream(std::istream &ss);
94  //! initializes from a string pickle
95  virtual void initFromString(const std::string &text);
96 
97  private:
98  std::vector<FilterCatalogs> d_catalogs;
99 
100 #ifdef RDK_USE_BOOST_SERIALIZATION
101  friend class boost::serialization::access;
102  template <class Archive>
103  void serialize(Archive &ar, const unsigned int version) {
104  RDUNUSED_PARAM(version);
105  ar &d_catalogs;
106  }
107 #endif
108 };
109 
111 class RDKIT_FILTERCATALOG_EXPORT FilterCatalog : public FCatalog {
112  public:
113  // syntactic sugar for getMatch(es) return values.
114  typedef boost::shared_ptr<FilterCatalogEntry> SENTRY;
115 
116 #if BOOST_VERSION / 100000 >= 1 && (BOOST_VERSION / 100 % 1000) > 44
117 #define BOOST_PYTHON_SUPPORT_SHARED_CONST
118 #endif
119 
120 #ifdef BOOST_PYTHON_SUPPORT_SHARED_CONST
121  // If boost::python can support shared_ptr of const objects
122  // we can enable support for this feature
123  typedef boost::shared_ptr<const entryType_t> CONST_SENTRY;
124 #else
125  typedef boost::shared_ptr<entryType_t> CONST_SENTRY;
126 #endif
127 
128  FilterCatalog() : FCatalog(), d_entries() {}
129 
131  : FCatalog(), d_entries() {
132  paramType_t temp_params(catalogs);
133  setCatalogParams(&temp_params);
134  }
135 
136  FilterCatalog(const FilterCatalogParams &params) : FCatalog(), d_entries() {
137  setCatalogParams(&params);
138  }
139 
141  : FCatalog(rhs), d_entries(rhs.d_entries) {}
142 
143  FilterCatalog(const std::string &binStr);
144 
145  ~FilterCatalog();
146 
147  virtual std::string Serialize() const;
148 
149  // Adds a new FilterCatalogEntry to the catalog
150  /*!
151  Adds a new FilterCatalogEntry to the catalog The catalog
152  owns the entry
153 
154  \param entry The FilterCatalogEntry to add.
155  \param updateFPLength unused in the FilterCatalog object.
156  */
157 
158  virtual unsigned int addEntry(FilterCatalogEntry *entry,
159  bool updateFPLength = true);
160 
161  // Adds a new FilterCatalogEntry to the catalog
162  /*!
163  Adds a new FilterCatalogEntry to the catalog The catalog
164  owns the entry
165 
166  \param entry The shared_ptr of the FilterCatalogEntry to add.
167  \param updateFPLength unused in the FilterCatalog object.
168  */
169 
170  virtual unsigned int addEntry(SENTRY entry, bool updateFPLength = true);
171 
172  // Removes a FilterCatalogEntry to the catalog by description
173  /*!
174  Removes a FilterCatalogEntry from the catalog.
175 
176  \param idx The FilterCatalogEntry index for the entry to remove.
177  n.b. removing an entry may change the indicies of other entries.
178  To safely remove entries, remove entries with the highest idx
179  first.
180  */
181  bool removeEntry(unsigned int idx);
182  bool removeEntry(CONST_SENTRY entry);
183 
184  //------------------------------------
185  //! returns a particular FilterCatalogEntry in the Catalog
186  //! required by Catalog.h API
187  virtual const FilterCatalogEntry *getEntryWithIdx(unsigned int idx) const;
188 
189  //------------------------------------
190  //! returns a particular FilterCatalogEntry in the Catalog
191  //! memory safe version of getEntryWithIdx
192  CONST_SENTRY getEntry(unsigned int idx) const;
193 
194  //------------------------------------
195  //! returns the idx of the given entry, UINT_MAX if not found.
196 
197  unsigned int getIdxForEntry(const FilterCatalogEntry *entry) const;
198  unsigned int getIdxForEntry(CONST_SENTRY entry) const;
199 
200  //------------------------------------
201  //! returns the number of entries in the catalog
202  virtual unsigned int getNumEntries() const { return static_cast<unsigned int>(d_entries.size()); }
203 
204  //------------------------------------
205  //! Reset the current catalog to match the specified FilterCatalogParameters
206  /*
207  \param params The new FilterCatalogParams specifying the new state of the
208  catalog
209  */
210  virtual void setCatalogParams(const FilterCatalogParams *params);
211 
212  //------------------------------------
213  //! Returns true if the molecule matches any entry in the catalog
214  /*
215  \param mol ROMol to match against the catalog
216  */
217  bool hasMatch(const ROMol &mol) const;
218 
219  //------------------------------------
220  //! Returns the first match against the catalog
221  /*
222  \param mol ROMol to match against the catalog
223  */
224  CONST_SENTRY getFirstMatch(const ROMol &mol) const;
225 
226  //-------------------------------------------
227  //! Returns all entry matches to the molecule
228  /*
229  \param mol ROMol to match against the catalog
230  */
231  const std::vector<CONST_SENTRY> getMatches(const ROMol &mol) const;
232 
233  //--------------------------------------------
234  //! Returns all FilterMatches for the molecule
235  /*
236  \param mol ROMol to match against the catalog
237  */
238  const std::vector<FilterMatch> getFilterMatches(const ROMol &mol) const;
239 
240  private:
241  void Clear();
242  std::vector<SENTRY> d_entries;
243 };
244 
246 }
247 
248 #endif
paramType paramType_t
Definition: Catalog.h:43
RDKIT_FILTERCATALOG_EXPORT bool FilterCatalogCanSerialize()
boost::shared_ptr< entryType_t > CONST_SENTRY
const std::vector< FilterCatalogs > & getCatalogs() const
Returns the existing list of FilterCatalogs to be used.
Definition: FilterCatalog.h:84
FilterCatalog(const FilterCatalog &rhs)
RDCatalog::Catalog< FilterCatalogEntry, FilterCatalogParams > FCatalog
abstract base class for a catalog object
Definition: Catalog.h:40
FilterCatalogParams(const FilterCatalogParams &other)
Definition: FilterCatalog.h:66
Std stuff.
Definition: Atom.h:30
#define RDKIT_FILTERCATALOG_EXPORT
Definition: export.h:229
#define RDUNUSED_PARAM(x)
Definition: Invariant.h:195
FilterCatalog(const FilterCatalogParams &params)
boost::shared_ptr< FilterCatalogEntry > SENTRY
RDKIT_RDGENERAL_EXPORT std::ostream & toStream(std::ostream &)
virtual unsigned int getNumEntries() const
returns the number of entries in the catalog
abstract base class for the container used to create a catalog
Definition: CatalogParams.h:18
FilterCatalogParams(FilterCatalogs catalogs)
Definition: FilterCatalog.h:61
FilterCatalog(FilterCatalogParams::FilterCatalogs catalogs)