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