RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MultiFPBReader.h
Go to the documentation of this file.
1//
2// Copyright (c) 2016-2022 Greg Landrum
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10#include <RDGeneral/export.h>
11#ifndef RD_MULTIFPBREADER_H_APR2016
12#define RD_MULTIFPBREADER_H_APR2016
13#include <RDGeneral/Invariant.h>
14/*! \file MultiFPBReader.h
15
16 \brief contains a class for reading and searching collections of FPB files
17
18 \b Note that this functionality is experimental and the API may change
19 in future releases.
20*/
21
25#include <tuple>
26
27namespace RDKit {
28
29//! class for reading and searching multiple FPB files
30/*!
31 basic usage:
32 \code
33 FPBReader r1("foo1.fpb"),r2("foo2.fpb");
34 std::vector<FPBReader *> readers;
35 readers.append(&r1);
36 readers.append(&r2);
37 MultiFPBReader fpbs(readers);
38 fpbs.init();
39 boost::shared_ptr<ExplicitBitVect> ebv = fpbs.getReader(0)->getFP(95);
40 std::vector<std::tuple<double,unsigned int, unsigned int> > nbrs =
41 fpbs.getTanimotoNeighbors(*ebv.get(), 0.70);
42 \endcode
43
44 \b Note: this functionality is experimental and the API may change
45 in future releases.
46
47 <b>Note on thread safety</b>
48 Operations that involve reading from FPB files are not thread safe.
49 This means that the \c init() method is not thread safe and none of the
50 search operations are thread safe when an \c FPBReader is initialized in
51 \c lazyRead mode.
52
53*/
55 public:
56 typedef std::tuple<double, unsigned int, unsigned int> ResultTuple;
58
59 /*!
60 \param initOnSearch: if this is true, the \c init() method on child readers
61 will not be called until the first search is done. This is useful with large
62 FPB readers.
63 */
64 MultiFPBReader(bool initOnSearch)
65 : df_init(false),
66 df_initOnSearch(initOnSearch),
67 df_takeOwnership(false) {}
68 /*!
69 \param readers: the set of FPBReader objects to use.
70 \param takeOwnership: if true, we own the memory for the FPBReaders
71 \param initOnSearch: if this is true, the \c init() method on child readers
72 will not be called until the first search is done. This is useful with large
73 FPB readers.
74 */
75 MultiFPBReader(std::vector<FPBReader *> &readers, bool takeOwnership = false,
76 bool initOnSearch = false);
77
79 df_init = false;
80 if (df_takeOwnership) {
81 for (auto &rdr : d_readers) {
82 delete rdr;
83 }
84 d_readers.clear();
85 }
86 }
87
88 //! Read the data from the file and initialize internal data structures
89 /*!
90 This must be called before most of the other methods of this class.
91 It calls the \c init() method on each of the child FPBReaders
92
93 */
94 void init();
95
96 //! returns the number of readers
97 unsigned int length() const { return d_readers.size(); }
98 //! returns the number of bits in our fingerprints (all readers are expected
99 //! to have the same length)
100 unsigned int nBits() const;
101
102 //! returns a particular reader
103 /*!
104
105 \param which: the reader to return
106
107 */
108 FPBReader *getReader(unsigned int which);
109
110 //! adds a new FPBReader to our list
111 /*!
112
113 This does no error checking on the reader, so be careful.
114
115 If \c takeOwnership is \c true then we will take ownership of the memory.
116
117 \param rdr: the reader to add. If we have already been initialized, the
118 reader's \c init() method will be called
119
120 \returns a count of the current number of readers
121 */
122 unsigned int addReader(FPBReader *rdr) {
123 PRECONDITION(rdr, "no reader provided");
124 d_readers.push_back(rdr);
125 if (df_init) {
126 rdr->init();
127 }
128 return d_readers.size();
129 }
130
131 //! returns tanimoto neighbors that are within a similarity threshold
132 /*!
133 The result vector of (similarity,index,reader) tuples is sorted in order
134 of decreasing similarity
135
136 \param bv the query fingerprint
137 \param threshold the minimum similarity to return
138 \param numThreads Sets the number of threads to use (more than one thread
139 will only be used if the RDKit was build with multithread support) If set to
140 zero, the max supported by the system will be used.
141
142 */
143 std::vector<ResultTuple> getTanimotoNeighbors(const std::uint8_t *bv,
144 double threshold = 0.7,
145 int numThreads = 1) const;
146 //! \overload
147 std::vector<ResultTuple> getTanimotoNeighbors(
148 boost::shared_array<std::uint8_t> bv, double threshold = 0.7,
149 int numThreads = 1) const {
150 return getTanimotoNeighbors(bv.get(), threshold, numThreads);
151 }
152 //! \overload
153 std::vector<ResultTuple> getTanimotoNeighbors(const ExplicitBitVect &ebv,
154 double threshold = 0.7,
155 int numThreads = 1) const;
156
157 //! returns Tversky neighbors that are within a similarity threshold
158 /*!
159 The result vector of (similarity,index) pairs is sorted in order
160 of decreasing similarity
161
162 \param bv the query fingerprint
163 \param ca the Tversky a coefficient
164 \param cb the Tversky a coefficient
165 \param threshold the minimum similarity to return
166 \param numThreads Sets the number of threads to use (more than one thread
167 will only be used if the RDKit was build with multithread support) If set to
168 zero, the max supported by the system will be used.
169
170 */
171 std::vector<ResultTuple> getTverskyNeighbors(const std::uint8_t *bv,
172 double ca, double cb,
173 double threshold = 0.7,
174 int numThreads = 1) const;
175 //! \overload
176 std::vector<ResultTuple> getTverskyNeighbors(
177 boost::shared_array<std::uint8_t> bv, double ca, double cb,
178 double threshold = 0.7, int numThreads = 1) const {
179 return getTverskyNeighbors(bv.get(), ca, cb, threshold, numThreads);
180 }
181 //! \overload
182 std::vector<ResultTuple> getTverskyNeighbors(const ExplicitBitVect &ebv,
183 double ca, double cb,
184 double threshold = 0.7,
185 int numThreads = 1) const;
186
187 //! returns indices of all fingerprints that completely contain this one
188 /*! (i.e. where all the bits set in the query are also set in the db
189 molecule)
190 */
191 std::vector<std::pair<unsigned int, unsigned int>> getContainingNeighbors(
192 const std::uint8_t *bv, int numThreads = 1) const;
193 //! \overload
194 std::vector<std::pair<unsigned int, unsigned int>> getContainingNeighbors(
195 boost::shared_array<std::uint8_t> bv, int numThreads = 1) const {
196 return getContainingNeighbors(bv.get(), numThreads);
197 }
198 //! \overload
199 std::vector<std::pair<unsigned int, unsigned int>> getContainingNeighbors(
200 const ExplicitBitVect &ebv, int numThreads = 1) const;
201
202 private:
203 std::vector<FPBReader *> d_readers;
204 bool df_init{false}, df_initOnSearch{false}, df_takeOwnership{false};
205
206 // disable automatic copy constructors and assignment operators
207 // for this class and its subclasses. They will likely be
208 // carrying around stream pointers and copying those is a recipe
209 // for disaster.
210 MultiFPBReader(const MultiFPBReader &);
211 MultiFPBReader &operator=(const MultiFPBReader &);
212};
213} // namespace RDKit
214#endif
contains a simple class for reading and searching FPB files
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
a class for bit vectors that are densely occupied
class for reading and searching FPB files
Definition FPBReader.h:57
void init()
Read the data from the file and initialize internal data structures.
std::vector< ResultTuple > getTanimotoNeighbors(const std::uint8_t *bv, double threshold=0.7, int numThreads=1) const
returns tanimoto neighbors that are within a similarity threshold
std::vector< ResultTuple > getTverskyNeighbors(const std::uint8_t *bv, double ca, double cb, double threshold=0.7, int numThreads=1) const
returns Tversky neighbors that are within a similarity threshold
unsigned int length() const
returns the number of readers
std::vector< ResultTuple > getTverskyNeighbors(const ExplicitBitVect &ebv, double ca, double cb, double threshold=0.7, int numThreads=1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
unsigned int addReader(FPBReader *rdr)
adds a new FPBReader to our list
void init()
Read the data from the file and initialize internal data structures.
MultiFPBReader(std::vector< FPBReader * > &readers, bool takeOwnership=false, bool initOnSearch=false)
MultiFPBReader(bool initOnSearch)
std::vector< std::pair< unsigned int, unsigned int > > getContainingNeighbors(boost::shared_array< std::uint8_t > bv, int numThreads=1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::tuple< double, unsigned int, unsigned int > ResultTuple
unsigned int nBits() const
std::vector< ResultTuple > getTanimotoNeighbors(const ExplicitBitVect &ebv, double threshold=0.7, int numThreads=1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< ResultTuple > getTverskyNeighbors(boost::shared_array< std::uint8_t > bv, double ca, double cb, double threshold=0.7, int numThreads=1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< std::pair< unsigned int, unsigned int > > getContainingNeighbors(const std::uint8_t *bv, int numThreads=1) const
returns indices of all fingerprints that completely contain this one
std::vector< std::pair< unsigned int, unsigned int > > getContainingNeighbors(const ExplicitBitVect &ebv, int numThreads=1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< ResultTuple > getTanimotoNeighbors(boost::shared_array< std::uint8_t > bv, double threshold=0.7, int numThreads=1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
FPBReader * getReader(unsigned int which)
returns a particular reader
#define RDKIT_DATASTRUCTS_EXPORT
Definition export.h:111
Std stuff.