Loading...
Searching...
No Matches
fingerprint.h
Go to the documentation of this file.
/**********************************************************************
fingerprint.h - Base class for fingerprints and fast searching
Copyright (C) 2005 by Chris Morley
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifndef OB_FINGERPRINT_H
#define OB_FINGERPRINT_H
#include <list>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <openbabel/plugin.h>
#ifndef OBFPRT
#define OBFPRT
#endif
namespace OpenBabel
{
class OBBase; //Forward declaration; used only as pointer.
class OBFPRT OBFingerprint : public OBPlugin
{
//see end of cpp file for detailed documentation
MAKE_PLUGIN(OBFingerprint)
const char* TypeID()
{
return "fingerprints";
}
//Rest of OBFingerprints declarations
public:
virtual ~OBFingerprint(){}
void SetBit(std::vector<unsigned int>& vec, const unsigned int n);
bool GetBit(const std::vector<unsigned int>& vec, const unsigned int n);
void Fold(std::vector<unsigned int>& vec, unsigned int nbits);
virtual bool GetFingerprint(OBBase* pOb, std::vector<unsigned int>& fp, int nbits=0)=0;
enum FptFlag{FPT_UNIQUEBITS=1, FPT_NOINFO=2};
virtual unsigned int Flags() { return 0;};
virtual void SetFlags(unsigned int){}
virtual std::string DescribeBits(const std::vector<unsigned int> /* fp */,
bool /* bSet */ =true)
{
std::string txt("");
return txt;
}
static double Tanimoto(const std::vector<unsigned int>& vec1, const std::vector<unsigned int>& vec2);
static double Tanimoto(const std::vector<unsigned int>& vec1, const unsigned int* p2)
{
int andbits=0, orbits=0;
unsigned int i;
for (i=0;i<vec1.size();++i)
{
int andfp = vec1[i] & p2[i];
int orfp = vec1[i] | p2[i];
// Count bits
/* GCC 3.4 supports a "population count" builtin, which on many targets is
implemented with a single instruction. There is a fallback definition
in libgcc in case a target does not have one, which should be just as
good as the static function below. */
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
andbits += __builtin_popcount(andfp);
orbits += __builtin_popcount(orfp);
#else
for(;andfp;andfp=andfp<<1)
if(andfp<0) ++andbits;
for(;orfp;orfp=orfp<<1)
if(orfp<0) ++orbits;
#endif
}
return((double)andbits/(double)orbits);
};
static unsigned int Getbitsperint(){ return bitsperint; }
private:
struct bit_or
{
unsigned int operator()(const unsigned int a, const unsigned int b)
{
return a | b;
}
};
public:
static OBFingerprint* FindFingerprint(const char* ID){ return FindType(ID);}
private:
static const unsigned int bitsperint;// = 8 * sizeof(unsigned int);
};
//Fast search routines
struct OBFPRT FptIndexHeader
{
unsigned int headerlength;
unsigned int nEntries;
unsigned int words;
char fpid[15];
char seek64; //if true, seek data consists of 64bit long values (only zero in legacy indices)
char datafilename[256];
};
struct OBFPRT FptIndex
{
FptIndexHeader header;
std::vector<unsigned int> fptdata;
std::vector<unsigned long> seekdata;
bool Read(std::istream* pIndexstream);
bool ReadIndex(std::istream* pIndexstream);
bool ReadHeader(std::istream* pIndexstream);
OBFingerprint* CheckFP();
};
class OBFPRT FastSearch
{
//see end of cpp file for detailed documentation
public:
std::string ReadIndexFile(std::string IndexFilename);
std::string ReadIndex(std::istream* pIndexstream);
virtual ~FastSearch(){};
bool Find(OBBase* pOb, std::vector<unsigned long>& SeekPositions, unsigned int MaxCandidates);
bool FindMatch(OBBase* pOb, std::vector<unsigned long>& SeekPositions,
unsigned int MaxCandidates);
bool FindSimilar(OBBase* pOb, std::multimap<double, unsigned long>& SeekposMap,
double MinTani, double MaxTani = 1.1 );
bool FindSimilar(OBBase* pOb, std::multimap<double, unsigned long>& SeekposMap,
int nCandidates=0);
OBFingerprint* GetFingerprint() const{ return _pFP;};
const FptIndexHeader& GetIndexHeader() const{ return _index.header;};
private:
FptIndex _index;
OBFingerprint* _pFP;
};
class OBFPRT FastSearchIndexer
{
//see end of cpp file for detailed documentation
public:
FastSearchIndexer(std::string& datafilename, std::ostream* os, std::string& fpid,
int FptBits=0, int nmols=0);
FastSearchIndexer(FptIndex* pindex, std::ostream* os, int nmols=0);
~FastSearchIndexer();
bool Add(OBBase* pOb, std::streampos seekpos);
private:
std::ostream* _indexstream;
FptIndex* _pindex;
OBFingerprint* _pFP;
int _nbits;
};
} //namespace OpenBabel
#endif
OpenBabel::FastSearchIndexer::FastSearchIndexer
FastSearchIndexer(std::string &datafilename, std::ostream *os, std::string &fpid, int FptBits=0, int nmols=0)
Constructor with a new index.
Definition fingerprint.cpp:389
OpenBabel::FastSearchIndexer::Add
bool Add(OBBase *pOb, std::streampos seekpos)
Called for each object.
Definition fingerprint.cpp:456
OpenBabel::FastSearch
Class to search fingerprint index files.
Definition fingerprint.h:166
OpenBabel::FastSearch::~FastSearch
virtual ~FastSearch()
Definition fingerprint.h:173
OpenBabel::FastSearch::GetFingerprint
OBFingerprint * GetFingerprint() const
Definition fingerprint.h:194
OpenBabel::FastSearch::GetIndexHeader
const FptIndexHeader & GetIndexHeader() const
Definition fingerprint.h:197
OpenBabel::FastSearch::ReadIndexFile
std::string ReadIndexFile(std::string IndexFilename)
Loads an index from a file and returns the name of the datafile.
Definition fingerprint.cpp:313
OpenBabel::FastSearch::ReadIndex
std::string ReadIndex(std::istream *pIndexstream)
Definition fingerprint.cpp:300
OpenBabel::OBBase
Base Class.
Definition base.h:240
OpenBabel::OBFingerprint
The base class for fingerprints.
Definition fingerprint.h:40
OpenBabel::OBFingerprint::GetFingerprint
virtual bool GetFingerprint(OBBase *pOb, std::vector< unsigned int > &fp, int nbits=0)=0
OpenBabel::OBFingerprint::FptFlag
FptFlag
Optional flags.
Definition fingerprint.h:68
OpenBabel::OBFingerprint::FPT_NOINFO
@ FPT_NOINFO
Definition fingerprint.h:68
OpenBabel::OBFingerprint::FPT_UNIQUEBITS
@ FPT_UNIQUEBITS
Definition fingerprint.h:68
OpenBabel::OBFingerprint::DescribeBits
virtual std::string DescribeBits(const std::vector< unsigned int >, bool=true)
Definition fingerprint.h:75
OpenBabel::OBFingerprint::~OBFingerprint
virtual ~OBFingerprint()
Definition fingerprint.h:53
OpenBabel::OBFingerprint::SetFlags
virtual void SetFlags(unsigned int)
Definition fingerprint.h:71
OpenBabel::OBFingerprint::FindFingerprint
static OBFingerprint * FindFingerprint(const char *ID)
For backward compatibility; a synonym of OBFingerprint::FindType.
Definition fingerprint.h:129
OpenBabel::OBFingerprint::Flags
virtual unsigned int Flags()
Definition fingerprint.h:69
OpenBabel::OBFingerprint::Getbitsperint
static unsigned int Getbitsperint()
Definition fingerprint.h:113
OpenBabel::OBFingerprint::Tanimoto
static double Tanimoto(const std::vector< unsigned int > &vec1, const unsigned int *p2)
Inline version of Tanimoto() taking a pointer for the second vector.
Definition fingerprint.h:86
OpenBabel::OBPlugin
Base class for all types of dynamic classes discovered at runtime.
Definition plugin.h:53
MAKE_PLUGIN
#define MAKE_PLUGIN(BaseClass)
Definition plugin.h:195
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
OpenBabel::Tanimoto
double Tanimoto(const OBBitVec &bv1, const OBBitVec &bv2)
The Tanimoto coefficient, which may be regarded as the proportion of the "on-bits" which are shared.
Definition bitvec.cpp:660
plugin.h
Simplify 'plugin' classes to be discovered and/or loaded at runtime.
OpenBabel::FptIndexHeader
Header for fastsearch index file.
Definition fingerprint.h:139
OpenBabel::FptIndexHeader::seek64
char seek64
Definition fingerprint.h:144
OpenBabel::FptIndexHeader::fpid
char fpid[15]
ID of the fingerprint type.
Definition fingerprint.h:143
OpenBabel::FptIndexHeader::datafilename
char datafilename[256]
the data that this is an index to
Definition fingerprint.h:145
OpenBabel::FptIndexHeader::headerlength
unsigned int headerlength
offset to data: sizeof(FptIndexHeader)
Definition fingerprint.h:140
OpenBabel::FptIndexHeader::nEntries
unsigned int nEntries
number of fingerprints
Definition fingerprint.h:141
OpenBabel::FptIndexHeader::words
unsigned int words
number 32bit words per fingerprint
Definition fingerprint.h:142
OpenBabel::FptIndex
Structure of fastsearch index files.
Definition fingerprint.h:151
OpenBabel::FptIndex::Read
bool Read(std::istream *pIndexstream)
Definition fingerprint.cpp:326
OpenBabel::FptIndex::fptdata
std::vector< unsigned int > fptdata
Definition fingerprint.h:153
OpenBabel::FptIndex::ReadIndex
bool ReadIndex(std::istream *pIndexstream)
OpenBabel::FptIndex::ReadHeader
bool ReadHeader(std::istream *pIndexstream)
Definition fingerprint.cpp:362
OpenBabel::FptIndex::CheckFP
OBFingerprint * CheckFP()
Definition fingerprint.cpp:374
OpenBabel::FptIndex::header
FptIndexHeader header
Definition fingerprint.h:152
OpenBabel::FptIndex::seekdata
std::vector< unsigned long > seekdata
Definition fingerprint.h:154