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
use clap::{Arg, Command};
use bigsig::{bigsi, build};
use std::alloc::System;
use std::io::Result;
use std::time::SystemTime;
use env_logger::Builder;
use rayon::ThreadPoolBuilder;
pub fn init_log() -> u64 {
Builder::from_default_env().init();
println!("\n ************** initializing logger *****************\n");
1
}
#[global_allocator]
static GLOBAL: System = System;
fn main() -> Result<()> {
let _ = init_log();
let matches = Command::new("bigsig")
.version("0.3.1")
.about("Large-scale Sequence Search with BItsliced Genomic Signature Index (BIGSIG)")
.arg_required_else_help(true)
.subcommand(
Command::new("construct")
.about("Construct a BIGSIG")
.version("0.3.1")
.arg_required_else_help(true)
.arg(
Arg::new("bigsi")
.short('b')
.long("bigsi")
.value_name("FILE")
.help("BIGSI file to output (base name; .bxi/.mxi will be appended)")
.required(true),
)
.arg(
Arg::new("ref_file")
.short('r')
.long("refs")
.value_name("FILE")
.help("Sets the input reference file to use")
.required(true),
)
.arg(
Arg::new("k-mer_size")
.short('k')
.long("kmer")
.value_name("INT")
.help("Sets k-mer size, or window size in minimizer mode")
.required(true),
)
.arg(
Arg::new("num_hashes")
.short('n')
.long("num_hashes")
.value_name("INT")
.help("Sets number of hashes for bloom filter")
.required(true),
)
.arg(
Arg::new("length_bloom")
.short('s')
.long("bloom")
.value_name("INT")
.help("Sets the length of the bloom filter")
.required(true),
)
.arg(
Arg::new("minimizer")
.short('m')
.long("minimizer")
.action(clap::ArgAction::SetTrue)
.help("Build index with minimizers"),
)
.arg(
Arg::new("value")
.short('v')
.long("value")
.value_name("INT")
.help("Sets minimizer length (default 15)")
.default_value("15"),
)
.arg(
Arg::new("threads")
.short('t')
.long("threads")
.value_name("INT")
.help("Number of threads to use")
.default_missing_value("1"),
)
.arg(
Arg::new("quality")
.short('Q')
.long("quality")
.value_name("INT")
.help("Minimum phred score to keep basepairs within read")
.default_value("15"),
)
.arg(
Arg::new("filter")
.short('f')
.long("filter")
.value_name("INT")
.help("Minimum coverage kmer threshold")
.default_missing_value("-1"),
),
)
.subcommand(
Command::new("identify")
.about("Identify reads based on probability")
.version("0.1.0")
.arg(
Arg::new("bigsi")
.short('b')
.long("bigsi")
.help("Index to be used for search")
.required(true),
)
.arg(
Arg::new("query")
.short('q')
.long("query")
.help("Query file(-s) fastq.gz")
.required(true)
.num_args(1..),
)
.arg(
Arg::new("batch")
.short('c')
.long("batch")
.help("Sets size of batch of reads to be processed in parallel (default 50,000)")
.required(false)
.default_value("50000"),
)
.arg(
Arg::new("threads")
.short('t')
.long("threads")
.value_name("INT")
.help("Number of threads to use, if not set the maximum available number threads will be used")
.required(false)
.default_value("0"),
)
.arg(
Arg::new("prefix")
.short('n')
.long("prefix")
.help("Prefix for output file(-s)")
.required(true),
)
.arg(
Arg::new("down_sample")
.short('d')
.long("down_sample")
.help("Down-sample k-mers used for read classification, default 1; increases speed at cost of decreased sensitivity")
.default_value("1"),
)
.arg(
Arg::new("high_mem_load")
.short('H')
.long("high_mem_load")
.action(clap::ArgAction::SetTrue)
.help("When this flag is set, a faster, but less memory efficient method to load the index is used"),
)
.arg(
Arg::new("fp_correct")
.short('p')
.long("fp_correct")
.required(false)
.help("Parameter to correct for false positives, default 3 (= 0.001), may be increased for larger searches")
.default_value("3.0"),
)
.arg(
Arg::new("quality")
.short('Q')
.long("quality")
.help("kmers with nucleotides below this minimum phred score will be excluded from the analyses (default 15)")
.default_value("15"),
)
.arg(
Arg::new("bitvector_sample")
.short('B')
.long("bitvector_sample")
.help("Collects matches for subset of kmers indicated (default=3), using this subset to more rapidly find hits for the remainder of the kmers")
.default_value("3"),
),
)
.get_matches();
// construct
if let Some(matches) = matches.subcommand_matches("construct") {
let ref_file = matches.get_one::<String>("ref_file").expect("required");
let bigsi_file: String = matches
.get_one::<String>("bigsi")
.expect("required")
.to_string();
let kmer_size = matches
.get_one::<String>("k-mer_size")
.expect("required")
.parse::<usize>()
.expect("Integer required");
let num_hashes = matches
.get_one::<String>("num_hashes")
.expect("required")
.parse::<usize>()
.expect("Integer required");
let length_bloom = matches
.get_one::<String>("length_bloom")
.expect("required")
.parse::<usize>()
.expect("Integer required");
let threads = matches
.get_one::<String>("threads")
.unwrap_or(&"0".to_string())
.parse::<usize>()
.expect("Integer required");
let quality = matches
.get_one::<String>("quality")
.unwrap_or(&"15".to_string())
.parse::<u8>()
.expect("Integer required");
let minimizer = matches.get_flag("minimizer");
let minimizer_value = matches
.get_one::<String>("value")
.unwrap_or(&"21".to_string())
.parse::<usize>()
.expect("Integer required");
let filter = matches
.get_one::<String>("filter")
.unwrap_or(&"-1".to_string())
.parse::<isize>()
.expect("Integer required");
let map = build::tab_to_map(ref_file.to_string());
if minimizer && kmer_size <= minimizer_value {
eprintln!(
"Error: in minimizer mode, --kmer (window length) must be > --value (minimizer length)."
);
std::process::exit(1);
}
if minimizer {
println!("Building with minimizers, minimizer size: {}", minimizer_value);
let (bigsi_map, colors_accession, n_ref_kmers) = if threads == 1 {
build::build_single_mini(
&map,
length_bloom,
num_hashes,
kmer_size,
minimizer_value,
quality,
filter,
)
} else {
build::build_multi_mini(
&map,
length_bloom,
num_hashes,
kmer_size,
minimizer_value,
threads,
quality,
filter,
)
};
let out_path = format!("{}.mxi", bigsi_file);
println!("Saving BIGSI to file: {}", out_path);
bigsi::save_bigsi_mini(
&out_path,
&bigsi::BigsyMapMiniNew {
map: bigsi_map,
colors: colors_accession,
n_ref_kmers,
bloom_size: length_bloom,
num_hash: num_hashes,
k_size: kmer_size,
m_size: minimizer_value,
},
);
} else {
let (bigsi_map, colors_accession, n_ref_kmers) = if threads == 1 {
build::build_single(
&map,
length_bloom,
num_hashes,
kmer_size,
quality,
filter,
)
} else {
build::build_multi(
&map,
length_bloom,
num_hashes,
kmer_size,
threads,
quality,
filter,
)
};
let out_path = format!("{}.bxi", bigsi_file);
println!("Saving BIGSI to file: {}", out_path);
bigsi::save_bigsi(
&out_path,
&bigsi::BigsyMapNew {
map: bigsi_map,
colors: colors_accession,
n_ref_kmers,
bloom_size: length_bloom,
num_hash: num_hashes,
k_size: kmer_size,
},
);
}
}
// identify
if let Some(matches) = matches.subcommand_matches("identify") {
let bigsi_time = SystemTime::now();
let fq: Vec<&str> = matches
.get_many::<String>("query")
.unwrap()
.map(|s| s.as_str())
.collect();
let threads: usize = matches
.get_one::<String>("threads")
.unwrap_or(&"0".to_string())
.parse()
.expect("Invalid threads number");
let down_sample: usize = matches
.get_one::<String>("down_sample")
.map(|s| s.parse::<usize>().expect("Invalid down_sample value"))
.unwrap_or(1);
let correct: f64 = matches
.get_one::<String>("fp_correct")
.unwrap_or(&"3.0".to_string())
.parse()
.expect("Invalid fp_correct value");
let fp_correct = 10f64.powf(-correct);
let index = matches.get_one::<String>("bigsi").unwrap();
let prefix = matches.get_one::<String>("prefix").unwrap();
let quality: u8 = matches
.get_one::<String>("quality")
.unwrap_or(&"15".to_string())
.parse()
.expect("Invalid quality value");
let batch: usize = matches
.get_one::<String>("batch")
.unwrap_or(&"50000".to_string())
.parse()
.expect("Invalid batch value");
let high_mem_load = matches.get_flag("high_mem_load");
let bitvector_sample: usize = matches
.get_one::<String>("bitvector_sample")
.unwrap_or(&"3".to_string())
.parse()
.expect("Invalid bitvector_sample value");
ThreadPoolBuilder::new()
.num_threads(threads)
.build_global()
.expect("Can't initialize ThreadPoolBuilder");
if index.ends_with(".mxi") {
let bigsi = if high_mem_load {
bigsi::read_bigsi_mini_highmem(index)
} else {
bigsi::read_bigsi_mini(index)
};
match bigsi_time.elapsed() {
Ok(elapsed) => {
eprintln!("Index loaded in {} seconds", elapsed.as_secs());
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
if fq[0].ends_with(".gz") {
if fq.len() > 1 {
bigsig::read_id_mt_pe::per_read_stream_pe(
fq,
&bigsi.map,
&bigsi.colors,
&bigsi.n_ref_kmers,
bigsi.bloom_size,
bigsi.num_hash,
bigsi.k_size,
bigsi.m_size,
threads,
down_sample,
fp_correct,
batch,
prefix,
quality,
bitvector_sample,
)
} else {
bigsig::read_id_mt_pe::per_read_stream_se(
fq,
&bigsi.map,
&bigsi.colors,
&bigsi.n_ref_kmers,
bigsi.bloom_size,
bigsi.num_hash,
bigsi.k_size,
bigsi.m_size,
threads,
down_sample,
fp_correct,
batch,
prefix,
quality,
bitvector_sample,
)
};
} else {
bigsig::read_id_mt_pe::stream_fasta(
fq,
&bigsi.map,
&bigsi.colors,
&bigsi.n_ref_kmers,
bigsi.bloom_size,
bigsi.num_hash,
bigsi.k_size,
bigsi.m_size,
threads,
down_sample,
fp_correct,
batch,
prefix,
bitvector_sample,
);
}
} else {
let bigsi = if high_mem_load {
bigsi::read_bigsi_highmem(index)
} else {
bigsi::read_bigsi(index)
};
match bigsi_time.elapsed() {
Ok(elapsed) => {
eprintln!("Index loaded in {} seconds", elapsed.as_secs());
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
if fq[0].ends_with(".gz") {
if fq.len() > 1 {
bigsig::read_id_mt_pe::per_read_stream_pe(
fq,
&bigsi.map,
&bigsi.colors,
&bigsi.n_ref_kmers,
bigsi.bloom_size,
bigsi.num_hash,
bigsi.k_size,
0,
threads,
down_sample,
fp_correct,
batch,
prefix,
quality,
bitvector_sample,
)
} else {
bigsig::read_id_mt_pe::per_read_stream_se(
fq,
&bigsi.map,
&bigsi.colors,
&bigsi.n_ref_kmers,
bigsi.bloom_size,
bigsi.num_hash,
bigsi.k_size,
0,
threads,
down_sample,
fp_correct,
batch,
prefix,
quality,
bitvector_sample,
)
};
} else {
bigsig::read_id_mt_pe::stream_fasta(
fq,
&bigsi.map,
&bigsi.colors,
&bigsi.n_ref_kmers,
bigsi.bloom_size,
bigsi.num_hash,
bigsi.k_size,
0,
threads,
down_sample,
fp_correct,
batch,
prefix,
bitvector_sample,
);
}
}
bigsig::reports::read_counts_five_fields(prefix.to_owned() + "_reads.txt", prefix);
}
Ok(())
}