Files
rdkit/Code/DataStructs/FPBReader.h
2016-01-12 12:09:54 +01:00

103 lines
3.2 KiB
C++

//
// Copyright (c) 2015 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.
//
#ifndef RD_FPBREADER_H_DEC2015
#define RD_FPBREADER_H_DEC2015
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <RDGeneral/BadFileException.h>
#include <DataStructs/ExplicitBitVect.h>
#include <boost/cstdint.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
namespace RDKit {
namespace detail {
struct FPBReader_impl;
}
class FPBReader {
public:
FPBReader()
: dp_istrm(NULL), dp_impl(NULL), df_owner(false), df_init(false){};
FPBReader(const char *fname) { _initFromFilename(fname); };
FPBReader(const std::string &fname) { _initFromFilename(fname.c_str()); };
FPBReader(std::istream *inStream, bool takeOwnership = true)
: dp_istrm(inStream), df_owner(takeOwnership), df_init(false){};
~FPBReader() {
destroy();
if (df_owner) delete dp_istrm;
dp_istrm = NULL;
df_init = false;
};
void init();
boost::shared_ptr<ExplicitBitVect> getFP(unsigned int idx) const;
boost::shared_array<boost::uint8_t> getBytes(unsigned int idx) const;
std::string getId(unsigned int idx) const;
std::pair<boost::shared_ptr<ExplicitBitVect>, std::string> operator[](
unsigned int idx) const {
return std::make_pair(getFP(idx), getId(idx));
};
// returns the beginning and end index 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);
unsigned int length() const;
unsigned int nBits() const;
double getTanimoto(unsigned int idx, const boost::uint8_t *bv) const;
double getTanimoto(unsigned int idx,
boost::shared_array<boost::uint8_t> bv) const {
return getTanimoto(idx, bv.get());
};
std::vector<std::pair<double, unsigned int> > getTanimotoNeighbors(
const boost::uint8_t *bv, double threshold = 0.7,
unsigned int topN = 0) const;
std::vector<std::pair<double, unsigned int> > getTanimotoNeighbors(
boost::shared_array<boost::uint8_t> bv, double threshold = 0.7,
unsigned int topN = 0) const {
return getTanimotoNeighbors(bv.get(), threshold, topN);
};
private:
std::istream *dp_istrm;
detail::FPBReader_impl *dp_impl; // implementation details
bool df_owner;
bool df_init;
// 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) {
std::istream *tmpStream = static_cast<std::istream *>(
new std::ifstream(fname, std::ios_base::binary));
if (!tmpStream || (!(*tmpStream)) || (tmpStream->bad())) {
std::ostringstream errout;
errout << "Bad input file " << fname;
throw BadFileException(errout.str());
}
dp_istrm = tmpStream;
df_owner = true;
df_init = false;
}
};
}
#endif