-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_make_bv.cc
523 lines (422 loc) · 14.3 KB
/
cmd_make_bv.cc
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// cmd_make_bv.cc-- convert a sequence file to a bit vector
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <vector>
#include "utilities.h"
#include "hash.h"
#include "jelly_kmers.h"
#include "bit_vector.h"
#include "bloom_filter.h"
#include "support.h"
#include "commands.h"
#include "cmd_make_bv.h"
using std::string;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;
#define u32 std::uint32_t
#define u64 std::uint64_t
void MakeBVCommand::short_description
(std::ostream& s)
{
s << commandName << "-- convert a sequence file to a bit vector" << endl;
}
void MakeBVCommand::usage
(std::ostream& s,
const string& message)
{
if (!message.empty())
{
s << message << endl;
s << endl;
}
short_description(s);
s << "usage: " << commandName << " <filename> [<filename>..] [options]" << endl;
// 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
s << " <filename> (cumulative) a sequence file, e.g. fasta or fastq" << endl;
s << " (one bloom filter is created, for the union of the" << endl;
s << " sequence files)" << endl;
s << " --kmersin input files are kmers" << endl;
s << " (by default input files are expected to be fasta or fastq)" << endl;
s << " --out=<filename> name for bit vector file; the bit vector's compression" << endl;
s << " type is determined by the file extension (e.g. .bv, .rrr" << endl;
s << " or .roar)" << endl;
s << " (by default this is derived from first sequence filename," << endl;
s << " and an uncompressed bit vector is created)" << endl;
s << " --list=<filename> file containing a list of bit vectors to create; this is" << endl;
s << " used in place of the <filename>s on the command line; the" << endl;
s << " file format is described below" << endl;
s << " --asper=<filename> name of an existing bloom filter file to extract settings" << endl;
s << " from; that file's --k, --seed, and --bits will be used if" << endl;
s << " they are not otherwise specified on the command line" << endl;
s << " --k=<N> kmer size (number of nucleotides in a kmer)" << endl;
s << " (default is " << defaultKmerSize << ")" << endl;
s << " --min=<N> kmers occuring fewer than N times are left out of the" << endl;
s << " bloom filter" << endl;
s << " (default is " << defaultMinAbundance << ")" << endl;
s << " --threads=<N> number of threads to use during kmerization" << endl;
s << " (default is " << defaultNumThreads << ")" << endl;
s << " --seed=<number> the hash function's 64-bit seed" << endl;
s << " --bits=<N> number of bits in the bloom filter" << endl;
s << " (default is " << defaultNumBits << ")" << endl;
s << endl;
s << "When --list is used, each line of the file corresponds to a bit vector. The" << endl;
s << "format of each line is" << endl;
s << " <filename> [<filename>..] [--kmersin] [--out=<filename>]" << endl;
s << "with meaning the same as on the command line. No other options (e.g. --k or" << endl;
s << "--bits) are allowed in the file. These are specified on the command line and" << endl;
s << "will affect all the bit vectors." << endl;
s << endl;
s << "When --kmersin is used, each line of the sequence input files is a single kmer," << endl;
s << "as the first field in the line. Any additional fields on the line are ignored." << endl;
s << "For example, with --k=20 this might be" << endl;
s << " ATGACCAGATATGTACTTGC" << endl;
s << " TCTGCGAACCCAGACTTGGT" << endl;
s << " CAAGACCTATGAGTAGAACG" << endl;
s << " ..." << endl;
s << "Every kmer in the file(s) is added to the bit vector. No counting is performed," << endl;
s << "and --min is not allowed." << endl;
}
void MakeBVCommand::debug_help
(std::ostream& s)
{
s << "--debug= options" << endl;
s << " settings" << endl;
s << " kmers" << endl;
s << " strings" << endl;
s << " count" << endl;
}
void MakeBVCommand::parse
(int _argc,
char** _argv)
{
int argc;
char** argv;
vector<string> tempBvFilenames;
listFilename = "";
inputIsKmers = false;
bvFilename = "";
kmerSize = defaultKmerSize; bool kmerSizeSet = false;
minAbundance = defaultMinAbundance; minAbundanceSet = false;
numThreads = defaultNumThreads;
hashSeed = 0; bool hashSeedSet = false;
numBits = defaultNumBits; bool numBitsSet = false;
#ifdef useJellyHash
hashSeed = JellyHashSeed;
#endif
// skip command name
argv = _argv+1; argc = _argc - 1;
if (argc <= 0) chastise ();
//////////
// scan arguments
//////////
for (int argIx=0 ; argIx<argc ; argIx++)
{
string arg = argv[argIx];
string argVal;
if (arg.empty()) continue;
string::size_type argValIx = arg.find('=');
if (argValIx == string::npos) argVal = "";
else argVal = arg.substr(argValIx+1);
// --help, etc.
if ((arg == "--help")
|| (arg == "-help")
|| (arg == "--h")
|| (arg == "-h")
|| (arg == "?")
|| (arg == "-?")
|| (arg == "--?"))
{ usage (cerr); std::exit (EXIT_SUCCESS); }
if ((arg == "--help=debug")
|| (arg == "--help:debug")
|| (arg == "?debug"))
{ debug_help(cerr); std::exit (EXIT_SUCCESS); }
// --kmersin
if ((arg == "--kmersin")
|| (arg == "--askmers="))
{ inputIsKmers = true; continue; }
// --out=<filename>
if ((is_prefix_of (arg, "--out="))
|| (is_prefix_of (arg, "--output=")))
{ bvFilename = argVal; continue; }
// --list=<filename>
if (is_prefix_of (arg, "--list="))
{ listFilename = argVal; continue; }
// --k=<N>
if ((is_prefix_of (arg, "K="))
|| (is_prefix_of (arg, "--K="))
|| (is_prefix_of (arg, "k="))
|| (is_prefix_of (arg, "--k="))
|| (is_prefix_of (arg, "--kmer="))
|| (is_prefix_of (arg, "--kmersize=")))
{
kmerSize = string_to_u32(argVal);
kmerSizeSet = true;
continue;
}
// --min=<N>
if ((is_prefix_of (arg, "--min="))
|| (is_prefix_of (arg, "--abundance=")))
{
minAbundance = string_to_u32(argVal);
if (minAbundance < 1) minAbundance = 1;
minAbundanceSet = (minAbundance > 1);
continue;
}
// --threads=<N>
if ((is_prefix_of (arg, "--threads="))
|| (is_prefix_of (arg, "T="))
|| (is_prefix_of (arg, "--T=")))
{
numThreads = string_to_u32(argVal);
if (numThreads == 0)
chastise ("(in \"" + arg + "\") number of threads cannot be zero");
continue;
}
// --seed=<number>
if ((is_prefix_of (arg, "--seed="))
|| (is_prefix_of (arg, "S="))
|| (is_prefix_of (arg, "--S=")))
{
hashSeed = string_to_u64 (argVal);
hashSeedSet = true;
continue;
}
// --bits=<N>
if ((is_prefix_of (arg, "--bits="))
|| (is_prefix_of (arg, "B="))
|| (is_prefix_of (arg, "--B=")))
{
numBits = string_to_unitized_u64(argVal);
numBitsSet = true;
continue;
}
// (unadvertised) debug options
if (arg == "--debug")
{ debug.insert ("debug"); continue; }
if (is_prefix_of (arg, "--debug="))
{
for (const auto& field : parse_comma_list(argVal))
debug.insert(to_lower(field));
continue;
}
// unrecognized --option
if (is_prefix_of (arg, "--"))
chastise ("unrecognized option: \"" + arg + "\"");
// <filename>
seqFilenames.emplace_back(strip_blank_ends(arg));
}
// if an "as per" file was given, modify settings that weren't otherwise
// specified
if (not asPerFilename.empty())
{
BloomFilter bf(asPerFilename);
bf.load();
if (!kmerSizeSet) { kmerSize = bf.kmerSize; kmerSizeSet = true; }
if (!hashSeedSet) { hashSeed = bf.hashSeed1; hashSeedSet = true; }
if (!numBitsSet) { numBits = bf.hashModulus; numBitsSet = true; } // (yes, hashModulus)
}
// sanity checks
if (listFilename.empty())
{
if (seqFilenames.empty())
chastise ("at least one sequence filename is required");
}
else
{
if (not seqFilenames.empty())
chastise ("cannot use --list with sequence filenames (e.g. " + seqFilenames[0] + ") in the command");
if (not bvFilename.empty())
chastise ("cannot use --list with a vector filename (" + bvFilename + ") in the command");
}
if (kmerSize == 0)
chastise ("kmer size cannot be zero");
if (numBits < 2)
chastise ("number of bits must be at least 2");
if ((inputIsKmers) and (minAbundanceSet))
chastise ("cannot use --kmersin with --min");
if (contains(debug,"settings"))
{
cerr << "kmerSize = " << kmerSize << endl;
cerr << "hashSeed = " << hashSeed << endl;
cerr << "numBits = " << numBits << endl;
}
return;
}
MakeBVCommand::~MakeBVCommand()
{
if (hasher != nullptr) delete hasher;
}
int MakeBVCommand::execute()
{
// $$$ add trackMemory to hash constructor/destructor
hasher = new HashCanonical(kmerSize,hashSeed);
// if we're to make a single vector, do so
if (listFilename.empty())
{
if (inputIsKmers) make_bit_vector_kmers ();
else make_bit_vector_fasta ();
}
// otherwise, make a series of vectors according to each line specified in
// a file
else
{
std::ifstream in (listFilename);
if (not in)
fatal ("error: failed to open \"" + listFilename + "\"");
string line;
int lineNum = 0;
while (std::getline (in, line))
{
lineNum++;
seqFilenames.clear();
inputIsKmers = false;
bvFilename = "";
vector<string> tokens = tokenize(line);
for (size_t argIx=0 ; argIx<tokens.size() ; argIx++)
{
string arg = tokens[argIx];
string argVal;
string::size_type argValIx = arg.find('=');
if (argValIx == string::npos) argVal = "";
else argVal = arg.substr(argValIx+1);
if ((arg == "--kmersin")
|| (arg == "--askmers="))
{
if (minAbundanceSet)
fatal ("cannot use --kmersin, with --min on the command line"
" (at line " + std::to_string(lineNum)
+ " in " + listFilename + ")");
inputIsKmers = true;
continue;
}
if ((is_prefix_of (arg, "--out="))
|| (is_prefix_of (arg, "--output=")))
{ bvFilename = argVal; continue; }
if (is_prefix_of (arg, "--"))
fatal ("unrecognized field: \"" + arg + "\""
+ " at line " + std::to_string(lineNum)
+ " in " + listFilename);
seqFilenames.emplace_back(strip_blank_ends(arg));
}
if (inputIsKmers) make_bit_vector_kmers ();
else make_bit_vector_fasta ();
}
in.close();
}
return EXIT_SUCCESS;
}
void MakeBVCommand::make_bit_vector_fasta() // this also supports fastq
{
string bvOutFilename = bvFilename;
if (bvOutFilename.empty())
{
string seqFilename = seqFilenames[0];
string::size_type dotIx = seqFilename.find_last_of(".");
if (dotIx == string::npos)
bvOutFilename = seqFilename + ".bv";
else
bvOutFilename = seqFilename.substr(0,dotIx) + ".bv";
}
// create the hash table, with jellyfish defaults
const u64 hashSize = 10*1000*1000;
const u32 numReprobes = 126;
const u32 counterLen = 7;
unsigned int savedKmerSize = jellyfish::mer_dna::k();
jellyfish::mer_dna::k(kmerSize);
mer_hash_type merHash (hashSize, kmerSize*2, counterLen, numThreads, numReprobes);
// count the kmers
// nota bene: MerCounter internally discards kmers containing any non-ACGT
// $$$ ERROR_CHECK need to trap exceptions from the jellyfish stuff
// $$$ ERROR_CHECK does jellyfish give us any indication if one of the sequence files doesn't exist?
MerCounter counter(numThreads, merHash, seqFilenames.begin(), seqFilenames.end());
counter.exec_join (numThreads);
// build the bit vector
BitVector* bv = BitVector::bit_vector (bvOutFilename);
bv->new_bits (numBits);
u64 onesCount = 0;
const auto jfAry = merHash.ary();
const auto end = jfAry->end();
for (auto kmer=jfAry->begin() ; kmer!=end ; ++kmer)
{
auto& keyValuePair = *kmer;
if (keyValuePair.second >= minAbundance)
{
if (contains(debug,"kmers"))
cerr << keyValuePair.first << " " << keyValuePair.second << endl;
u64 pos;
if (contains(debug,"strings"))
{
string key = keyValuePair.first.to_str();
pos = hasher->hash(key) % numBits;
}
else
{
u64* merData = (u64*) keyValuePair.first.data();
pos = hasher->hash(merData) % numBits;
}
if ((*bv)[pos] == 0)
{ bv->write_bit (pos); onesCount++; }
}
}
if (contains(debug,"count"))
cerr << "generated " << bv->identity() << " with " << onesCount << " 1s"<< endl;
jellyfish::mer_dna::k(savedKmerSize); // restore jellyfish kmer size
bv->reportSave = true;
bv->save();
delete bv;
}
void MakeBVCommand::make_bit_vector_kmers()
{
string bvOutFilename = bvFilename;
if (bvOutFilename.empty())
{
string seqFilename = seqFilenames[0];
string::size_type dotIx = seqFilename.find_last_of(".");
if (dotIx == string::npos)
bvOutFilename = seqFilename + ".bv";
else
bvOutFilename = seqFilename.substr(0,dotIx) + ".bv";
}
// build the bit vector
BitVector* bv = BitVector::bit_vector (bvOutFilename);
bv->new_bits (numBits);
u64 onesCount = 0;
for (const auto& kmersFilename : seqFilenames)
{
std::ifstream in (kmersFilename);
if (not in)
fatal ("error: failed to open \"" + kmersFilename + "\"");
string line, kmer;
int lineNum = 0;
while (std::getline (in, line))
{
lineNum++;
line = strip_blank_ends (line);
if (line.empty()) continue;
string::size_type whitespaceIx = line.find_first_of(" \t");
if (whitespaceIx == string::npos) kmer = line;
else kmer = line.substr(0,whitespaceIx);
if (kmer.length() != kmerSize)
fatal ("error: expected " + std::to_string(kmerSize) + "-mer"
+ " but encountered " + std::to_string(kmer.length()) + "-mer"
+ " (at line " + std::to_string(lineNum)
+ " in " + kmersFilename + ")");
string::size_type badIx = kmer.find_first_not_of("ACGTacgt");
if (badIx != string::npos) continue;
if (contains(debug,"kmers"))
cerr << kmer << endl;
u64 pos = hasher->hash(kmer) % numBits;
if ((*bv)[pos] == 0)
{ bv->write_bit (pos); onesCount++; }
}
}
if (contains(debug,"count"))
cerr << "generated " << bv->identity() << " with " << onesCount << " 1s"<< endl;
bv->reportSave = true;
bv->save();
delete bv;
}