-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbloom.cpp
348 lines (240 loc) · 8.02 KB
/
bloom.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <sstream>
#include <math.h>
#include <zlib.h> // For crc32
#include "bloom.h"
#include "sra_accession.h"
using namespace std;
BloomParam optimal_bloom_param(const uint32_t &m_kmer_len, const size_t &m_num_kmer, const float &m_p,
const HashFunction &m_func, const uint32_t &m_min_log_2_filter_len,
const uint32_t &m_max_log_2_filter_len)
{
// There are some SRA datasets that contain reads that are *less* than the size of a kmer.
// These datasets will not yeild any valid kmers
if(m_num_kmer == 0){
throw __FILE__ ":optimal_bloom_param: No kmers found";
}
BloomParam ret;
// Even though the optimal bloom filter parameters do not depend on the hash function
// (we assume that all hash functions are "ideal"), we set the hash function to make sure
// it gets included in the BloomParam output.
ret.hash_func = m_func;
ret.kmer_len = m_kmer_len;
// These filter parameters are not yet valid
bool valid = false;
// Perform a grid search to identify the *smallest* Bloom filter
// that is less than the specified false positive rate.
//
// For Bloom filters of the same length, find the optimal number of hash
// functions (within the specified search limits).
for(ret.log_2_filter_len = m_min_log_2_filter_len;
ret.log_2_filter_len <= m_max_log_2_filter_len;++ret.log_2_filter_len){
float best_p = 10.0f; // Any value > 1.0 to initialize
// Find the optimal number of hash functions for this filter length
for(uint32_t num_hash = MIN_NUM_HASH;num_hash <= MAX_NUM_HASH;++num_hash){
const uint64_t len = 1ULL << ret.log_2_filter_len;
// The per-filter, per-k-mer probability of a false positive
const double p = pow(1.0 - pow(1.0 - 1.0/len, m_num_kmer*num_hash), num_hash);
if( (p <= m_p) && (p < best_p) ){
best_p = p;
ret.num_hash = num_hash;
valid = true;
}
}
if(valid == true){
// Since we are searching in order of ascending filter
// length, return as soon as we satisfy the false
// positive requirement.
return ret;
}
}
throw __FILE__ ":optimal_bloom_param: Unable to satisfy Bloom filter probability bound";
return ret;
}
// Estimate the maximum number of unqiue kmers before we can't find a valid set of
// Bloom filter paramters.
size_t approximate_max_kmers(const float &m_p,
const HashFunction &m_func, const uint32_t &m_min_log_2_filter_len,
const uint32_t &m_max_log_2_filter_len)
{
// Test the largest value a size_t can store
const size_t max_bits = 8*sizeof(size_t);
for(size_t log_2_num_kmer = 1;log_2_num_kmer < max_bits;++log_2_num_kmer){
const size_t num_kmer = 1ULL << log_2_num_kmer;
// Perform a grid search to identify the *smallest* Bloom filter
// that is less than the specified false positive rate.
//
// For Bloom filters of the same length, find the optimal number of hash
// functions (within the specified search limits).
bool valid = false;
for(size_t log_2_filter_len = m_min_log_2_filter_len;
(log_2_filter_len <= m_max_log_2_filter_len) && !valid;++log_2_filter_len){
float best_p = 10.0f; // Any value > 1.0 to initialize
// Find the optimal number of hash functions for this filter length
for(uint32_t num_hash = MIN_NUM_HASH;(num_hash <= MAX_NUM_HASH) && !valid;++num_hash){
const uint64_t len = 1ULL << log_2_filter_len;
// The per-filter, per-k-mer probability of a false positive
const double p = pow(1.0 - pow(1.0 - 1.0/len, num_kmer*num_hash), num_hash);
if( (p <= m_p) && (p < best_p) ){
// We found valid Bloom filter parameters
valid = true;
}
}
}
// Return the upper-bound size -- i.e. the smallest number of kmers for which we cannot
// find valid Bloom filter parameters
if(!valid){
return num_kmer;
}
}
// Could not estimate the max number of kmers -- return the largest possible value
return 0xFFFFFFFFFFFFFFFF;
}
// Output a limited subset of metadata in CSV
string FilterInfo::csv_string() const
{
return accession_to_str(run_accession);
}
string FilterInfo::json_string(const string &m_prefix) const
{
stringstream ssout;
bool wrote_value = false;
if( run_accession != INVALID_ACCESSION){
ssout << m_prefix << "\"run\": \"" << accession_to_str(run_accession) << '"';
wrote_value = true;
}
if( date_received.is_valid() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"date received\": \"" << date_received << '"';
wrote_value = true;
}
if(experiment_accession != INVALID_ACCESSION){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment\": \"" << accession_to_str(experiment_accession) << '"';
wrote_value = true;
}
if( !experiment_title.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment title\": \"" << experiment_title << '"';
wrote_value = true;
}
if( !experiment_design_description.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment design\": \"" << experiment_design_description << '"';
wrote_value = true;
}
if( !experiment_library_name.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment library name\": \"" << experiment_library_name << '"';
wrote_value = true;
}
if( !experiment_library_strategy.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment library strategy\": \"" << experiment_library_strategy << '"';
wrote_value = true;
}
if( !experiment_library_source.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment library source\": \"" << experiment_library_source << '"';
wrote_value = true;
}
if( !experiment_library_selection.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment library selection\": \"" << experiment_library_selection << '"';
wrote_value = true;
}
if( !experiment_instrument_model.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"experiment instrument model\": \"" << experiment_instrument_model << '"';
wrote_value = true;
}
if(sample_accession != INVALID_ACCESSION){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"sample\": \"" << accession_to_str(sample_accession) << '"';
wrote_value = true;
}
if( !sample_taxa.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"sample taxa\": \"" << sample_taxa << '"';
wrote_value = true;
}
if( !sample_attributes.empty() ){
if(wrote_value){
ssout << ",\n";
}
bool wrote_attribute_value = false;
ssout << m_prefix << "\"sample attributes\": [\n";
for(MAP<string, string>::const_iterator i = sample_attributes.begin();i != sample_attributes.end();++i){
if(wrote_attribute_value){
ssout << ",\n";
}
ssout << m_prefix << "\t{\n";
ssout << m_prefix << "\t\t\"tag\": \"" << i->first << "\",\n";
ssout << m_prefix << "\t\t\"value\": \"" << i->second << "\"\n";
ssout << m_prefix << "\t}";
wrote_attribute_value = true;
}
ssout <<'\n' << m_prefix << ']';
wrote_value = true;
}
if(study_accession != INVALID_ACCESSION){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"study\": \"" << accession_to_str(study_accession) << '"';
wrote_value = true;
}
if( !study_title.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"study title\": \"" << study_title << '"';
wrote_value = true;
}
if( !study_abstract.empty() ){
if(wrote_value){
ssout << ",\n";
}
ssout << m_prefix << "\"study abstract\": \"" << study_abstract << '"';
wrote_value = true;
}
return ssout.str();
}
unsigned int BitVector::crc32() const
{
// Use the crc32 function provided by zlib.
// The crc32 value must be initialized with a call
// to crc32_z with a zero length buffer.
const unsigned int init_crc32 = ::crc32_z(0L, Z_NULL, 0);
return ::crc32_z( init_crc32, buffer, num_block() );
};
unsigned int BloomFilter::update_crc32()
{
bloom_crc32 = BitVector::crc32();
return bloom_crc32;
}
bool BloomFilter::test_crc32() const
{
return ( bloom_crc32 == BitVector::crc32() );
}