FPBReader.h
Go to the documentation of this file.
//
// Copyright (c) 2016 Greg Landrum
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef RD_FPBREADER_H_DEC2015
#define RD_FPBREADER_H_DEC2015
/*! \file FPBReader.h
\brief contains a simple class for reading and searching FPB files
\b Note that this functionality is experimental and the API may change
in future releases.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <RDGeneral/BadFileException.h>
#include <DataStructs/ExplicitBitVect.h>
#include <cstdint>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
namespace RDKit {
namespace detail {
struct FPBReader_impl;
}
//! class for reading and searching FPB files
/*!
basic usage:
\code
FPBReader reader("foo.fpb");
reader.init();
boost::shared_ptr<ExplicitBitVect> ebv = reader.getFP(95);
std::vector<std::pair<double, unsigned int> > nbrs =
reader.getTanimotoNeighbors(*ebv.get(), 0.70);
\endcode
\b Note: this functionality is experimental and the API may change
in future releases.
<b>Note on thread safety</b>
Operations that involve reading from the FPB file are not thread safe.
This means that the \c init() method is not thread safe and none of the
search operations are thread safe when an \c FPBReader is initialized in
\c lazyRead mode.
*/
class RDKIT_DATASTRUCTS_EXPORT FPBReader {
public:
FPBReader() {}
//! ctor for reading from a named file
/*!
\param fname the name of the file to reads
\param lazyRead if set to \c false all fingerprints from the file will be read
into memory when \c init() is called.
*/
FPBReader(const char *fname, bool lazyRead = false) {
_initFromFilename(fname, lazyRead);
}
//! \overload
FPBReader(const std::string &fname, bool lazyRead = false) {
_initFromFilename(fname.c_str(), lazyRead);
}
//! ctor for reading from an open istream
/*!
\param inStream the stream to read from
\param takeOwnership if set, we will take over ownership of the stream pointer
\param lazyRead if set to \c false all fingerprints from the file will be read
into memory when \c init() is called.
Some additional notes:
- if \c lazyRead is set, \c inStream must support the \c seekg() and \c
tellg() operations.
*/
FPBReader(std::istream *inStream, bool takeOwnership = true,
bool lazyRead = false)
: dp_istrm(inStream),
dp_impl(nullptr),
df_owner(takeOwnership),
df_init(false),
df_lazyRead(lazyRead) {}
~FPBReader() {
destroy();
if (df_owner) delete dp_istrm;
dp_istrm = nullptr;
df_init = false;
}
//! Read the data from the file and initialize internal data structures
/*!
This must be called before most of the other methods of this class.
Some notes:
\li if \c lazyRead is not set, all fingerprints will be read into memory. This
can require substantial amounts of memory for large files.
\li For large files, this can take a long time.
\li If \c lazyRead and \c takeOwnership are both \c false it is safe to close
and delete inStream after calling \c init()
*/
void init();
//! cleanup
/*!
Cleans up whatever memory was allocated during init()
*/
void cleanup() {
if (!df_init) return;
destroy();
df_init = false;
}
//! returns the requested fingerprint as an \c ExplicitBitVect
boost::shared_ptr<ExplicitBitVect> getFP(unsigned int idx) const;
//! returns the requested fingerprint as an array of bytes
boost::shared_array<std::uint8_t> getBytes(unsigned int idx) const;
//! returns the id of the requested fingerprint
std::string getId(unsigned int idx) const;
//! returns the fingerprint and id of the requested fingerprint
std::pair<boost::shared_ptr<ExplicitBitVect>, std::string> operator[](
unsigned int idx) const {
return std::make_pair(getFP(idx), getId(idx));
}
//! returns beginning and end indices of fingerprints having on-bit counts
//! within the range (including end points)
std::pair<unsigned int, unsigned int> getFPIdsInCountRange(
unsigned int minCount, unsigned int maxCount);
//! returns the number of fingerprints
unsigned int length() const;
//! returns the number of bits in our fingerprints
unsigned int nBits() const;
//! returns the tanimoto similarity between the specified fingerprint and the
//! provided fingerprint
double getTanimoto(unsigned int idx, const std::uint8_t *bv) const;
//! \overload
double getTanimoto(unsigned int idx,
boost::shared_array<std::uint8_t> bv) const {
return getTanimoto(idx, bv.get());
}
//! \overload
double getTanimoto(unsigned int idx, const ExplicitBitVect &ebv) const;
//! returns tanimoto neighbors that are within a similarity threshold
/*!
The result vector of (similarity,index) pairs is sorted in order
of decreasing similarity
\param bv the query fingerprint
\param threshold the minimum similarity to return
\param usePopcountScreen if this is true (the default) the popcount of the
neighbors will be used to reduce the number of calculations that need
to be done
*/
std::vector<std::pair<double, unsigned int>> getTanimotoNeighbors(
const std::uint8_t *bv, double threshold = 0.7,
bool usePopcountScreen = true) const;
//! \overload
std::vector<std::pair<double, unsigned int>> getTanimotoNeighbors(
boost::shared_array<std::uint8_t> bv, double threshold = 0.7,
bool usePopcountScreen = true) const {
return getTanimotoNeighbors(bv.get(), threshold, usePopcountScreen);
}
//! \overload
std::vector<std::pair<double, unsigned int>> getTanimotoNeighbors(
const ExplicitBitVect &ebv, double threshold = 0.7,
bool usePopcountScreen = true) const;
//! returns the Tversky similarity between the specified fingerprint and the
//! provided fingerprint
/*!
\param idx the fingerprint to compare to
\param bv the query fingerprint
\param ca the Tversky a coefficient
\param cb the Tversky a coefficient
*/
double getTversky(unsigned int idx, const std::uint8_t *bv, double ca,
double cb) const;
//! \overload
double getTversky(unsigned int idx, boost::shared_array<std::uint8_t> bv,
double ca, double cb) const {
return getTversky(idx, bv.get(), ca, cb);
}
//! \overload
double getTversky(unsigned int idx, const ExplicitBitVect &ebv, double ca,
double cb) const;
//! returns Tversky neighbors that are within a similarity threshold
/*!
The result vector of (similarity,index) pairs is sorted in order
of decreasing similarity
\param bv the query fingerprint
\param ca the Tversky a coefficient
\param cb the Tversky a coefficient
\param threshold the minimum similarity to return
\param usePopcountScreen if this is true (the default) the popcount of the
neighbors will be used to reduce the number of calculations that need
to be done
*/
std::vector<std::pair<double, unsigned int>> getTverskyNeighbors(
const std::uint8_t *bv, double ca, double cb, double threshold = 0.7,
bool usePopcountScreen = true) const;
//! \overload
std::vector<std::pair<double, unsigned int>> getTverskyNeighbors(
boost::shared_array<std::uint8_t> bv, double ca, double cb,
double threshold = 0.7, bool usePopcountScreen = true) const {
return getTverskyNeighbors(bv.get(), ca, cb, threshold, usePopcountScreen);
}
//! \overload
std::vector<std::pair<double, unsigned int>> getTverskyNeighbors(
const ExplicitBitVect &ebv, double ca, double cb, double threshold = 0.7,
bool usePopcountScreen = true) const;
//! returns indices of all fingerprints that completely contain this one
/*! (i.e. where all the bits set in the query are also set in the db
molecule)
*/
std::vector<unsigned int> getContainingNeighbors(
const std::uint8_t *bv) const;
//! \overload
std::vector<unsigned int> getContainingNeighbors(
boost::shared_array<std::uint8_t> bv) const {
return getContainingNeighbors(bv.get());
}
//! \overload
std::vector<unsigned int> getContainingNeighbors(
const ExplicitBitVect &ebv) const;
private:
std::istream *dp_istrm{nullptr};
detail::FPBReader_impl *dp_impl{nullptr}; // implementation details
bool df_owner{false};
bool df_init{false};
bool df_lazyRead{false};
// disable automatic copy constructors and assignment operators
// for this class and its subclasses. They will likely be
// carrying around stream pointers and copying those is a recipe
// for disaster.
FPBReader(const FPBReader &);
FPBReader &operator=(const FPBReader &);
void destroy();
void _initFromFilename(const char *fname, bool lazyRead) {
std::istream *tmpStream = static_cast<std::istream *>(
new std::ifstream(fname, std::ios_base::binary));
if (!(*tmpStream) || (tmpStream->bad())) {
std::ostringstream errout;
errout << "Bad input file " << fname;
delete tmpStream;
throw BadFileException(errout.str());
}
dp_istrm = tmpStream;
dp_impl = nullptr;
df_owner = true;
df_init = false;
df_lazyRead = lazyRead;
}
};
} // namespace RDKit
#endif
BadFileException.h
ExplicitBitVect.h
ExplicitBitVect
a class for bit vectors that are densely occupied
Definition: ExplicitBitVect.h:29
RDKit::FPBReader
class for reading and searching FPB files
Definition: FPBReader.h:58
RDKit::FPBReader::cleanup
void cleanup()
cleanup
Definition: FPBReader.h:117
RDKit::FPBReader::getTversky
double getTversky(unsigned int idx, const std::uint8_t *bv, double ca, double cb) const
RDKit::FPBReader::~FPBReader
~FPBReader()
Definition: FPBReader.h:94
RDKit::FPBReader::getTanimotoNeighbors
std::vector< std::pair< double, unsigned int > > getTanimotoNeighbors(const ExplicitBitVect &ebv, double threshold=0.7, bool usePopcountScreen=true) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
RDKit::FPBReader::getFPIdsInCountRange
std::pair< unsigned int, unsigned int > getFPIdsInCountRange(unsigned int minCount, unsigned int maxCount)
RDKit::FPBReader::length
unsigned int length() const
returns the number of fingerprints
RDKit::FPBReader::getTanimoto
double getTanimoto(unsigned int idx, const std::uint8_t *bv) const
RDKit::FPBReader::getFP
boost::shared_ptr< ExplicitBitVect > getFP(unsigned int idx) const
returns the requested fingerprint as an ExplicitBitVect
RDKit::FPBReader::getBytes
boost::shared_array< std::uint8_t > getBytes(unsigned int idx) const
returns the requested fingerprint as an array of bytes
RDKit::FPBReader::getTanimoto
double getTanimoto(unsigned int idx, boost::shared_array< std::uint8_t > bv) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: FPBReader.h:149
RDKit::FPBReader::getTversky
double getTversky(unsigned int idx, boost::shared_array< std::uint8_t > bv, double ca, double cb) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: FPBReader.h:195
RDKit::FPBReader::getTverskyNeighbors
std::vector< std::pair< double, unsigned int > > getTverskyNeighbors(const ExplicitBitVect &ebv, double ca, double cb, double threshold=0.7, bool usePopcountScreen=true) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
RDKit::FPBReader::getTversky
double getTversky(unsigned int idx, const ExplicitBitVect &ebv, double ca, double cb) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
RDKit::FPBReader::FPBReader
FPBReader(std::istream *inStream, bool takeOwnership=true, bool lazyRead=false)
ctor for reading from an open istream
Definition: FPBReader.h:87
RDKit::FPBReader::getContainingNeighbors
std::vector< unsigned int > getContainingNeighbors(const ExplicitBitVect &ebv) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
RDKit::FPBReader::FPBReader
FPBReader(const char *fname, bool lazyRead=false)
ctor for reading from a named file
Definition: FPBReader.h:68
RDKit::FPBReader::FPBReader
FPBReader(const std::string &fname, bool lazyRead=false)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: FPBReader.h:72
RDKit::FPBReader::getContainingNeighbors
std::vector< unsigned int > getContainingNeighbors(boost::shared_array< std::uint8_t > bv) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: FPBReader.h:238
RDKit::FPBReader::getTverskyNeighbors
std::vector< std::pair< double, unsigned int > > getTverskyNeighbors(const std::uint8_t *bv, double ca, double cb, double threshold=0.7, bool usePopcountScreen=true) const
returns Tversky neighbors that are within a similarity threshold
RDKit::FPBReader::nBits
unsigned int nBits() const
returns the number of bits in our fingerprints
RDKit::FPBReader::getContainingNeighbors
std::vector< unsigned int > getContainingNeighbors(const std::uint8_t *bv) const
returns indices of all fingerprints that completely contain this one
RDKit::FPBReader::getTanimoto
double getTanimoto(unsigned int idx, const ExplicitBitVect &ebv) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
RDKit::FPBReader::getId
std::string getId(unsigned int idx) const
returns the id of the requested fingerprint
RDKit::FPBReader::operator[]
std::pair< boost::shared_ptr< ExplicitBitVect >, std::string > operator[](unsigned int idx) const
returns the fingerprint and id of the requested fingerprint
Definition: FPBReader.h:130
RDKit::FPBReader::getTanimotoNeighbors
std::vector< std::pair< double, unsigned int > > getTanimotoNeighbors(boost::shared_array< std::uint8_t > bv, double threshold=0.7, bool usePopcountScreen=true) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: FPBReader.h:172
RDKit::FPBReader::init
void init()
Read the data from the file and initialize internal data structures.
RDKit::FPBReader::getTanimotoNeighbors
std::vector< std::pair< double, unsigned int > > getTanimotoNeighbors(const std::uint8_t *bv, double threshold=0.7, bool usePopcountScreen=true) const
returns tanimoto neighbors that are within a similarity threshold
RDKit::FPBReader::getTverskyNeighbors
std::vector< std::pair< double, unsigned int > > getTverskyNeighbors(boost::shared_array< std::uint8_t > bv, double ca, double cb, double threshold=0.7, bool usePopcountScreen=true) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: FPBReader.h:221
RDKit::FPBReader::FPBReader
FPBReader()
Definition: FPBReader.h:60
export.h
RDKIT_DATASTRUCTS_EXPORT
#define RDKIT_DATASTRUCTS_EXPORT
Definition: export.h:81
RDKit
Std stuff.
Definition: Abbreviations.h:18
RDKit::getFP
RDKIT_FINGERPRINTS_EXPORT ExplicitBitVect * getFP(const ROMol &mol, FPType fPType)