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);
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  setCatalogParams(new paramType_t(catalogs));
133  }
134 
135  FilterCatalog(const FilterCatalogParams &params) : FCatalog(), d_entries() {
136  setCatalogParams(new paramType_t(params));
137  }
138 
140  : FCatalog(rhs), d_entries(rhs.d_entries) {}
141 
142  FilterCatalog(const std::string &binStr);
143 
144  ~FilterCatalog();
145 
146  virtual std::string Serialize() const;
147 
148  // Adds a new FilterCatalogEntry to the catalog
149  /*!
150  Adds a new FilterCatalogEntry to the catalog The catalog
151  owns the entry
152 
153  \param entry The FilterCatalogEntry to add.
154  \param updateFPLength unused in the FilterCatalog object.
155  */
156 
157  virtual unsigned int addEntry(FilterCatalogEntry *entry,
158  bool updateFPLength = true);
159 
160  // Adds a new FilterCatalogEntry to the catalog
161  /*!
162  Adds a new FilterCatalogEntry to the catalog The catalog
163  owns the entry
164 
165  \param entry The shared_ptr of the FilterCatalogEntry to add.
166  \param updateFPLength unused in the FilterCatalog object.
167  */
168 
169  virtual unsigned int addEntry(SENTRY entry, bool updateFPLength = true);
170 
171  // Removes a FilterCatalogEntry to the catalog by description
172  /*!
173  Removes a FilterCatalogEntry from the catalog.
174 
175  \param idx The FilterCatalogEntry index for the entry to remove.
176  n.b. removing an entry may change the indicies of other entries.
177  To safely remove entries, remove entries with the highest idx
178  first.
179  */
180  bool removeEntry(unsigned int idx);
181  bool removeEntry(CONST_SENTRY entry);
182 
183  //------------------------------------
184  //! returns a particular FilterCatalogEntry in the Catalog
185  //! required by Catalog.h API
186  virtual const FilterCatalogEntry *getEntryWithIdx(unsigned int idx) const;
187 
188  //------------------------------------
189  //! returns a particular FilterCatalogEntry in the Catalog
190  //! memory safe version of getEntryWithIdx
191  CONST_SENTRY getEntry(unsigned int idx) const;
192 
193  //------------------------------------
194  //! returns the idx of the given entry, UINT_MAX if not found.
195 
196  unsigned int getIdxForEntry(const FilterCatalogEntry *entry) const;
197  unsigned int getIdxForEntry(CONST_SENTRY entry) const;
198 
199  //------------------------------------
200  //! returns the number of entries in the catalog
201  virtual unsigned int getNumEntries() const { return static_cast<unsigned int>(d_entries.size()); }
202 
203  //------------------------------------
204  //! Reset the current catalog to match the specified FilterCatalogParameters
205  /*
206  \param params The new FilterCatalogParams specifying the new state of the
207  catalog
208  */
209  virtual void setCatalogParams(FilterCatalogParams *params);
210 
211  //------------------------------------
212  //! Returns true if the molecule matches any entry in the catalog
213  /*
214  \param mol ROMol to match against the catalog
215  */
216  bool hasMatch(const ROMol &mol) const;
217 
218  //------------------------------------
219  //! Returns the first match against the catalog
220  /*
221  \param mol ROMol to match against the catalog
222  */
223  CONST_SENTRY getFirstMatch(const ROMol &mol) const;
224 
225  //-------------------------------------------
226  //! Returns all entry matches to the molecule
227  /*
228  \param mol ROMol to match against the catalog
229  */
230  const std::vector<CONST_SENTRY> getMatches(const ROMol &mol) const;
231 
232  //--------------------------------------------
233  //! Returns all FilterMatches for the molecule
234  /*
235  \param mol ROMol to match against the catalog
236  */
237  const std::vector<FilterMatch> getFilterMatches(const ROMol &mol) const;
238 
239  private:
240  void Clear();
241  std::vector<SENTRY> d_entries;
242 };
243 
245 }
246 
247 #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:216
#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)