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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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("query")
.about("Query a BIGSIG on one or more fasta/fastq.gz files")
.arg_required_else_help(true)
.version("0.1.0")
.arg(
Arg::new("bigsi")
.short('b')
.long("bigsi")
.value_name("FILE")
.help("Sets the name of the index file 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("reverse")
.short('r')
.long("reverse")
.value_name("FILE")
.help("Reverse file(-s) fastq.gz")
.required(false)
.num_args(1..)
.default_value("none"),
)
.arg(
Arg::new("filter")
.short('f')
.long("filter")
.value_name("INT")
.help("Set minimum k-mer frequency")
.required(false),
)
.arg(
Arg::new("shared_kmers")
.short('p')
.long("p_shared")
.value_name("FLOAT")
.help("Set minimum proportion of shared k-mers with a reference")
.required(false),
)
.arg(
Arg::new("gene_search")
.short('g')
.long("gene_search")
.action(clap::ArgAction::SetTrue)
.help("If set, the proportion of kmers from the query matching the entries in the index will be reported"),
)
.arg(
Arg::new("perfect_search")
.short('s')
.long("perfect_search")
.action(clap::ArgAction::SetTrue)
.help("If set, the fast 'perfect match' algorithm will be used"),
)
.arg(
Arg::new("multi_fasta")
.short('m')
.long("multi_fasta")
.action(clap::ArgAction::SetTrue)
.help("If set, each accession in a multifasta will be treated as a separate query, currently only with the -s option"),
)
.arg(
Arg::new("quality")
.short('Q')
.long("quality")
.value_name("INT")
.help("Minimum phred score to keep basepairs within read (default 15)")
.default_value("15"),
),
)
.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,
},
);
}
}
// query
if let Some(matches) = matches.subcommand_matches("query") {
let files1: Vec<&str> = matches
.get_many::<String>("query")
.unwrap()
.map(|s| s.as_str())
.collect();
let files2: Vec<&str> = if matches.get_one::<String>("reverse").unwrap() == "none" {
vec![]
} else {
matches
.get_many::<String>("reverse")
.unwrap()
.map(|s| s.as_str())
.collect()
};
let filter = matches
.get_one::<String>("filter")
.unwrap_or(&"-1".to_string())
.parse::<isize>()
.unwrap();
let cov = matches
.get_one::<String>("shared_kmers")
.unwrap_or(&"0.35".to_string())
.parse::<f64>()
.unwrap();
let gene_search = matches.get_flag("gene_search");
let perfect_search = matches.get_flag("perfect_search");
let multi_fasta = matches.get_flag("multi_fasta");
let quality = matches
.get_one::<String>("quality")
.unwrap_or(&"15".to_string())
.parse::<u8>()
.unwrap();
if matches.get_one::<String>("bigsi").unwrap().ends_with(".mxi") {
eprintln!(
"Error: An index with minimizers (.mxi) is used, but not available for this function"
);
} else {
let bigsi_time = SystemTime::now();
eprintln!("Loading index");
let index = bigsi::read_bigsi(matches.get_one::<String>("bigsi").unwrap());
match bigsi_time.elapsed() {
Ok(elapsed) => {
eprintln!("Index loaded in {} seconds", elapsed.as_secs());
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
if perfect_search {
if multi_fasta {
bigsig::perfect_search::batch_search_mf(
files1,
&index.map,
&index.colors,
&index.n_ref_kmers,
index.bloom_size,
index.num_hash,
index.k_size,
cov,
)
} else {
bigsig::perfect_search::batch_search(
files1,
&index.map,
&index.colors,
&index.n_ref_kmers,
index.bloom_size,
index.num_hash,
index.k_size,
cov,
)
}
} else {
bigsig::batch_search_pe::batch_search(
files1,
files2,
&index.map,
&index.colors,
&index.n_ref_kmers,
index.bloom_size,
index.num_hash,
index.k_size,
filter,
cov,
gene_search,
quality,
)
}
}
}
// 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(())
}