-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKmerBloomFilter.hpp
77 lines (68 loc) · 1.84 KB
/
KmerBloomFilter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
*
* BloomFilter.hpp
*
* Created on: Aug 10, 2012
* Author: cjustin
*/
#ifndef KMERBLOOMFILTER_H_
#define KMERBLOOMFILTER_H_
#include "BloomFilter.hpp"
#include "vendor/nthash.hpp"
using namespace std;
class KmerBloomFilter : public BloomFilter
{
public:
/*
* Default constructor.
*/
KmerBloomFilter()
: BloomFilter()
{}
/* De novo filter constructor.
*
* preconditions:
* filterSize must be a multiple of 64
*
* kmerSize refers to the number of bases the kmer has
*/
KmerBloomFilter(size_t filterSize, unsigned hashNum, unsigned kmerSize)
: BloomFilter(filterSize, hashNum, kmerSize)
{}
KmerBloomFilter(const string& filterFilePath)
: BloomFilter(filterFilePath)
{}
using BloomFilter::contains;
using BloomFilter::insert;
/*
* Single pass filtering, computes hash values on the fly
*/
bool contains(const char* kmer) const
{
uint64_t hVal = NTC64(kmer, m_kmerSize);
size_t normalizedValue = hVal % m_size;
unsigned char bit = bitMask[normalizedValue % bitsPerChar];
if ((m_filter[normalizedValue / bitsPerChar] & bit) == 0)
return false;
for (unsigned i = 1; i < m_hashNum; i++) {
normalizedValue = NTE64(hVal, m_kmerSize, i) % m_size;
unsigned char bit = bitMask[normalizedValue % bitsPerChar];
if ((m_filter[normalizedValue / bitsPerChar] & bit) == 0)
return false;
}
return true;
}
void insert(const char* kmer)
{
uint64_t hVal = NTC64(kmer, m_kmerSize);
size_t normalizedValue = hVal % m_size;
__sync_fetch_and_or(
&m_filter[normalizedValue / bitsPerChar], bitMask[normalizedValue % bitsPerChar]);
for (unsigned i = 1; i < m_hashNum; i++) {
size_t normalizedValue = NTE64(hVal, m_kmerSize, i) % m_size;
__sync_fetch_and_or(
&m_filter[normalizedValue / bitsPerChar], bitMask[normalizedValue % bitsPerChar]);
}
}
};
#endif /* KMERBLOOMFILTER_H_ */