-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
762 lines (650 loc) · 28.9 KB
/
main.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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include <iostream>
#include "unix_tools.hpp"
#include "config.hpp"
#include "sequence_io.hpp"
#include "annotated_dbg.hpp"
#include "dbg_hash.hpp"
#include "dbg_hash_ordered.hpp"
#include "dbg_sd.hpp"
#include "annotate_row_compressed.hpp"
#include "annotate_column_compressed.hpp"
#include "static_annotators_def.hpp"
#include "annotation_converters.hpp"
#include "kmc_parser.hpp"
typedef annotate::MultiLabelAnnotation<uint64_t, std::string> Annotator;
const size_t kNumCachedColumns = 10;
template <class Graph = DBGHashOrdered>
Graph* load_critical_graph_from_file(const std::string &filename) {
auto *graph = new Graph(0);
if (!graph->load(filename)) {
std::cerr << "ERROR: can't load graph from file " << filename << std::endl;
delete graph;
exit(1);
}
return graph;
}
std::string get_filter_filename(std::string filename,
size_t k,
size_t max_unreliable_abundance,
size_t unreliable_kmers_threshold,
bool critical = true) {
filename = filename + ".filter_k" + std::to_string(k)
+ "_s" + std::to_string(max_unreliable_abundance)
+ (max_unreliable_abundance
? std::string("_")
+ std::to_string(unreliable_kmers_threshold)
: "");
if (!critical || std::ifstream(filename).good()) {
return filename;
} else if (max_unreliable_abundance == 0) {
return "";
}
std::cerr << "ERROR: read filter "
<< filename << " does not exist."
<< " Filter reads first." << std::endl;
exit(1);
}
void annotate_data(const std::vector<std::string> &files,
AnnotatedDBG *anno_graph,
bool reverse,
bool use_kmc,
size_t filter_k,
size_t max_unreliable_abundance,
size_t unreliable_kmers_threshold,
bool filename_anno,
bool fasta_anno,
const std::string &fasta_header_delimiter,
const std::vector<std::string> &anno_labels,
bool verbose) {
size_t total_seqs = 0;
std::unique_ptr<Timer> timer;
if (verbose) {
timer.reset(new Timer());
}
// iterate over input files
for (const auto &file : files) {
if (verbose) {
std::cout << std::endl << "Parsing " << file << std::endl;
}
// read files
if (use_kmc) {
std::vector<std::string> labels;
if (filename_anno) {
labels.push_back(file);
}
for (const auto &label : anno_labels) {
labels.push_back(label);
}
kmc::read_kmers(file, [&](std::string&& sequence) {
anno_graph->annotate_sequence(std::move(sequence), labels);
total_seqs += 1;
if (verbose && total_seqs % 10000 == 0) {
std::cout << "processed " << total_seqs << " sequences"
<< ", trying to annotate as ";
for (const auto &label : labels) {
std::cout << "<" << label << ">";
}
std::cout << ", " << timer->elapsed() << "sec" << std::endl;
}
}, max_unreliable_abundance + 1);
if (verbose) {
std::cout << "Finished extracting sequences from file " << file
<< " in " << timer->elapsed() << "sec" << std::endl;
}
} else if (utils::get_filetype(file) == "FASTA"
|| utils::get_filetype(file) == "FASTQ") {
read_fasta_file_critical(file,
[&](kseq_t *read_stream) {
std::vector<std::string> labels;
if (fasta_anno) {
labels = utils::split_string(read_stream->name.s,
fasta_header_delimiter);
}
if (filename_anno) {
labels.push_back(file);
}
for (const auto &label : anno_labels) {
labels.push_back(label);
}
anno_graph->annotate_sequence(read_stream->seq.s, labels);
total_seqs += 1;
if (verbose && total_seqs % 10000 == 0) {
std::cout << "processed " << total_seqs << " sequences"
<< ", last was " << read_stream->name.s
<< ", trying to annotate as ";
for (const auto &label : labels) {
std::cout << "<" << label << ">";
}
std::cout << ", " << timer->elapsed() << "sec" << std::endl;
}
},
reverse,
get_filter_filename(
file, filter_k,
max_unreliable_abundance,
unreliable_kmers_threshold
)
);
} else {
std::cerr << "ERROR: Filetype unknown for file "
<< file << std::endl;
exit(1);
}
if (verbose) {
std::cout << "Finished extracting sequences from file " << file
<< " in " << timer->elapsed() << "sec" << std::endl;
}
}
// join threads if any were initialized
anno_graph->join();
}
void execute_query(std::string seq_name,
std::string sequence,
bool count_labels,
bool suppress_unlabeled,
size_t num_top_labels,
double discovery_fraction,
std::string anno_labels_delimiter,
const AnnotatedDBG &anno_graph) {
std::ostringstream oss;
if (count_labels) {
auto top_labels = anno_graph.get_top_labels(sequence, num_top_labels);
if (!top_labels.size() && suppress_unlabeled)
return;
oss << seq_name << "\t";
if (top_labels.size()) {
oss << "<" << top_labels[0].first << ">:" << top_labels[0].second;
}
for (size_t i = 1; i < top_labels.size(); ++i) {
oss << "\t<" << top_labels[i].first << ">:" << top_labels[i].second;
}
oss << "\n";
} else {
auto labels_discovered
= anno_graph.get_labels(sequence, discovery_fraction);
if (!labels_discovered.size() && suppress_unlabeled)
return;
oss << seq_name << "\t"
<< utils::join_strings(labels_discovered,
anno_labels_delimiter) << "\n";
}
std::cout << oss.str();
}
std::unique_ptr<Annotator> initialize_annotation(const Config &config) {
std::unique_ptr<Annotator> annotation;
switch (config.anno_type) {
case Config::ColumnCompressed: {
annotation.reset(
new annotate::ColumnCompressed<>(
0, kNumCachedColumns, config.verbose
)
);
break;
}
case Config::RowCompressed: {
annotation.reset(new annotate::RowCompressed<>(0, config.sparse));
break;
}
case Config::BRWT: {
annotation.reset(new annotate::BRWTCompressed<>());
break;
}
case Config::BinRelWT_sdsl: {
annotation.reset(new annotate::BinRelWT_sdslAnnotator());
break;
}
case Config::BinRelWT: {
annotation.reset(new annotate::BinRelWTAnnotator());
break;
}
case Config::RowFlat: {
annotation.reset(new annotate::RowFlatAnnotator());
break;
}
case Config::RBFish: {
annotation.reset(new annotate::RainbowfishAnnotator());
break;
}
}
return annotation;
}
int main(int argc, const char *argv[]) {
std::unique_ptr<Config> config { new Config(argc, argv) };
if (config->verbose) {
std::cout << "#############################\n"
<< "### Welcome to AnnoGraph! ###\n"
<< "#############################\n" << std::endl;
}
const auto &files = config->fname;
switch (config->identity) {
case Config::BUILD: {
if (config->complete) {
if (config->verbose)
std::cout << "Build complete De Bruijn graph with k-mer size k="
<< config->k << std::endl;
Timer timer;
std::unique_ptr<DeBruijnGraph> graph {
new DBGSD(config->k, config->canonical_mode)
};
std::cout << "Graph with " << graph->num_nodes()
<< " k-mers was built in "
<< timer.elapsed() << "sec" << std::endl;
if (config->outfbase.size()) {
timer.reset();
graph->serialize(config->outfbase);
std::cout << timer.elapsed() << "sec" << std::endl;
}
return 0;
}
std::unique_ptr<DeBruijnGraph> graph {
new DBGHashOrdered(config->k, config->canonical_mode)
};
if (config->verbose)
std::cout << "Build De Bruijn Graph with k-mer size k="
<< graph->get_k() << std::endl;
Timer timer;
if (config->verbose) {
std::cout << "Start reading data and extracting k-mers" << std::endl;
}
for (const auto &file : files) {
if (config->verbose) {
std::cout << std::endl << "Parsing " << file << std::endl;
}
Timer data_reading_timer;
if (config->use_kmc) {
bool warning_different_k = false;
kmc::read_kmers(file, [&](std::string&& sequence) {
if (!warning_different_k && sequence.size() != graph->get_k()) {
std::cerr << "Warning: k-mers parsed from KMC database "
<< file << " have length " << sequence.size()
<< " but graph is constructed for k=" << graph->get_k()
<< std::endl;
warning_different_k = true;
}
graph->add_sequence(sequence);
}, config->max_unreliable_abundance + 1);
} else if (utils::get_filetype(file) == "FASTA"
|| utils::get_filetype(file) == "FASTQ") {
read_fasta_file_critical(file,
[&](kseq_t *read_stream) {
graph->add_sequence(read_stream->seq.s);
},
false,
get_filter_filename(file, config->filter_k,
config->max_unreliable_abundance,
config->unreliable_kmers_threshold)
);
} else {
std::cerr << "ERROR: Filetype unknown for file "
<< file << std::endl;
exit(1);
}
if (config->verbose) {
std::cout << "Finished extracting sequences from file " << file
<< " in " << timer.elapsed() << "sec"
<< ", number of k-mers in graph: " << graph->num_nodes() << std::endl;
}
if (config->verbose) {
std::cout << "File processed in "
<< data_reading_timer.elapsed()
<< "sec, current mem usage: "
<< get_curr_mem2() / (1<<20) << " MB"
<< ", total time: " << timer.elapsed()
<< "sec" << std::endl;
}
}
std::cout << "Graph with " << graph->num_nodes()
<< " k-mers was built in "
<< timer.elapsed() << "sec" << std::endl;
if (config->outfbase.size()) {
timer.reset();
graph->serialize(config->outfbase);
std::cout << timer.elapsed() << "sec" << std::endl;
}
return 0;
}
case Config::ANNOTATE: {
// load graph
DeBruijnGraph *graph = nullptr;
if (config->complete) {
graph = load_critical_graph_from_file<DBGSD>(config->infbase);
} else {
graph = load_critical_graph_from_file(config->infbase);
}
AnnotatedDBG anno_dbg(
graph,
config->parallel
);
// initialize empty annotation
if (config->anno_type == Config::RowCompressed) {
anno_dbg.set_annotation(
new annotate::RowCompressed<>(anno_dbg.num_anno_rows(),
config->sparse)
);
} else if (config->anno_type == Config::ColumnCompressed) {
anno_dbg.set_annotation(
new annotate::ColumnCompressed<>(anno_dbg.num_anno_rows(),
kNumCachedColumns,
config->verbose)
);
}
if (config->infbase_annotators.size()
&& !anno_dbg.get_annotation().merge_load(config->infbase_annotators)) {
std::cerr << "ERROR: can't load annotations" << std::endl;
exit(1);
}
if (!anno_dbg.check_compatibility(true)) {
std::cerr << "ERROR: graph cannot be annotated" << std::endl;
exit(1);
}
annotate_data(files,
&anno_dbg,
config->reverse,
config->use_kmc,
config->filter_k,
config->max_unreliable_abundance,
config->unreliable_kmers_threshold,
config->filename_anno,
config->fasta_anno,
config->fasta_header_delimiter,
config->anno_labels,
config->verbose);
anno_dbg.get_annotation().serialize(config->outfbase);
return 0;
}
case Config::MERGE_ANNOTATIONS: {
std::unique_ptr<Annotator> annotation;
if (config->anno_type == Config::RowCompressed) {
throw std::runtime_error("To be implemented");
annotation.reset(new annotate::RowCompressed<>(0, config->sparse));
} else {
annotation.reset(
new annotate::ColumnCompressed<>(
0, kNumCachedColumns, config->verbose
)
);
}
if (!annotation->merge_load(files)) {
std::cerr << "ERROR: can't load annotations" << std::endl;
exit(1);
}
annotation->serialize(config->outfbase);
return 0;
}
case Config::CLASSIFY: {
// load graph
DeBruijnGraph *graph = nullptr;
if (config->complete) {
graph = load_critical_graph_from_file<DBGSD>(config->infbase);
} else {
graph = load_critical_graph_from_file(config->infbase);
}
AnnotatedDBG anno_dbg(
graph,
config->parallel
);
anno_dbg.set_annotation(initialize_annotation(*config).release());
if (!anno_dbg.get_annotation().merge_load(config->infbase_annotators)) {
std::cerr << "ERROR: can't load annotations for graph "
<< config->infbase + ".dbg"
<< ", file corrupted" << std::endl;
exit(1);
}
utils::ThreadPool thread_pool(std::max(1u, config->parallel) - 1);
std::unique_ptr<Timer> timer { config->verbose ? new Timer() : NULL };
if (!anno_dbg.check_compatibility(true)) {
std::cerr << "ERROR: graph cannot be annotated" << std::endl;
exit(1);
}
// iterate over input files
for (const auto &file : files) {
if (config->verbose) {
std::cout << std::endl << "Parsing " << file << std::endl;
}
size_t seq_count = 0;
read_fasta_file_critical(file,
[&](kseq_t *read_stream) {
thread_pool.enqueue(execute_query,
std::to_string(seq_count++) + "\t"
+ std::string(read_stream->name.s),
std::string(read_stream->seq.s),
config->count_labels,
config->suppress_unlabeled,
config->num_top_labels,
config->discovery_fraction,
config->anno_labels_delimiter,
std::ref(anno_dbg)
);
},
config->reverse,
get_filter_filename(file, config->filter_k,
config->max_unreliable_abundance,
config->unreliable_kmers_threshold)
);
if (config->verbose) {
std::cout << "Finished extracting sequences from file " << file
<< " in " << timer->elapsed() << "sec" << std::endl;
}
// wait while all threads finish processing the current file
thread_pool.join();
}
return 0;
}
case Config::STATS: {
for (const auto &file : files) {
DeBruijnGraph *graph = nullptr;
if (config->complete) {
graph = load_critical_graph_from_file<DBGSD>(file);
} else {
graph = load_critical_graph_from_file(file);
}
AnnotatedDBG anno_dbg(
graph,
config->parallel
);
std::cout << "Statistics for graph " << file << std::endl;
std::cout << "nodes: " << graph->num_nodes() << std::endl;
std::cout << "k: " << graph->get_k() << std::endl;
if (dynamic_cast<DBGHashOrdered*>(graph)) {
std::cout << "canonical: "
<< (dynamic_cast<DBGHashOrdered&>(*graph).is_canonical_mode()
? "yes"
: "no")
<< std::endl;
} else if (dynamic_cast<DBGSD*>(graph)) {
std::cout << "canonical: "
<< (dynamic_cast<DBGSD&>(*graph).is_canonical_mode()
? "yes"
: "no")
<< std::endl;
}
}
for (const auto &file : config->infbase_annotators) {
auto annotation = initialize_annotation(*config);
if (!annotation->load(file)) {
std::cerr << "ERROR: can't load annotation from file "
<< file << std::endl;
exit(1);
}
std::cout << "Statistics for annotation " << file << std::endl;
std::cout << "labels: " << annotation->num_labels() << std::endl;
std::cout << "density: " << std::scientific
<< static_cast<double>(annotation->num_relations())
/ annotation->num_objects()
/ annotation->num_labels() << std::endl;
std::cout << "representation: "
<< config->annotype_to_string(config->anno_type) << std::endl;
}
return 0;
}
case Config::TRANSFORM_ANNOTATION: {
Timer timer;
auto annotator = std::make_unique<annotate::ColumnCompressed<>>(
0, kNumCachedColumns, config->verbose
);
if (config->verbose)
std::cout << "Loading annotator...\t" << std::flush;
if (!annotator->merge_load(files)) {
std::cerr << "ERROR: can't load annotations" << std::endl;
exit(1);
}
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
if (config->rename_instructions_file.size()) {
if (config->verbose)
std::cout << "Renaming...\t" << std::flush;
std::map<std::string, std::string> dict;
std::ifstream instream(config->rename_instructions_file);
if (!instream.is_open()) {
std::cerr << "ERROR: Can't open file "
<< config->rename_instructions_file << std::endl;
exit(1);
}
std::string old_name;
std::string new_name;
while (instream.good() && !(instream >> old_name).eof()) {
instream >> new_name;
if (instream.fail() || instream.eof()) {
std::cerr << "ERROR: wrong format of the rules for"
<< " renaming annotation columns passed in file "
<< config->rename_instructions_file << std::endl;
exit(1);
}
dict[old_name] = new_name;
}
annotator->rename_columns(dict);
annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
}
switch (config->anno_type) {
case Config::ColumnCompressed:
break;
case Config::RowCompressed: {
if (config->verbose)
std::cout << "Converting...\t" << std::flush;
annotate::RowCompressed<> row_annotator(0);
annotator->convert_to_row_annotator(&row_annotator,
config->parallel);
annotator.reset();
row_annotator.serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
break;
}
case Config::BRWT: {
if (config->verbose)
std::cout << "Converting...\t" << std::flush;
auto brwt_annotator = config->greedy_brwt
? annotate::convert_to_greedy_BRWT<annotate::BRWTCompressed<>>(
std::move(*annotator),
config->parallel)
: annotate::convert_to_simple_BRWT<annotate::BRWTCompressed<>>(
std::move(*annotator),
config->arity_brwt,
config->parallel);
annotator.reset();
brwt_annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
break;
}
case Config::BinRelWT_sdsl: {
if (config->verbose)
std::cout << "Converting...\t" << std::flush;
auto binrelwt_sdsl_annotator
= annotate::convert<annotate::BinRelWT_sdslAnnotator>(
std::move(*annotator)
);
annotator.reset();
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
if (config->verbose)
std::cout << "Serializing to " << config->outfbase
<< "...\t" << std::flush;
binrelwt_sdsl_annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
break;
}
case Config::BinRelWT: {
if (config->verbose)
std::cout << "Converting...\t" << std::flush;
auto binrelwt_annotator = annotate::convert<annotate::BinRelWTAnnotator>(
std::move(*annotator)
);
annotator.reset();
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
if (config->verbose)
std::cout << "Serializing to " << config->outfbase
<< "...\t" << std::flush;
binrelwt_annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
break;
}
case Config::RowFlat: {
if (config->verbose)
std::cout << "Converting to flat annotator...\t" << std::flush;
auto flat_annotator = annotate::convert<annotate::RowFlatAnnotator>(
std::move(*annotator)
);
annotator.reset();
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
if (config->verbose)
std::cout << "Serializing to " << config->outfbase
<< "...\t" << std::flush;
flat_annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
break;
}
case Config::RBFish: {
if (config->verbose)
std::cout << "Converting to rainbowfish annotator...\t" << std::flush;
auto flat_annotator = annotate::convert<annotate::RainbowfishAnnotator>(
std::move(*annotator)
);
annotator.reset();
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
if (config->verbose)
std::cout << "Serializing to " << config->outfbase
<< "...\t" << std::flush;
flat_annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
break;
}
}
return 0;
}
case Config::RELAX_BRWT: {
Timer timer;
auto annotator = std::make_unique<annotate::BRWTCompressed<>>();
if (config->verbose)
std::cout << "Loading annotator...\t" << std::flush;
if (!annotator->merge_load(files)) {
std::cerr << "ERROR: can't load annotations" << std::endl;
exit(1);
}
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
if (config->verbose)
std::cout << "Relaxing BRWT tree...\t" << std::flush;
annotate::relax_BRWT<annotate::BRWTCompressed<>>(annotator.get(),
config->relax_arity_brwt,
config->parallel);
annotator->serialize(config->outfbase);
if (config->verbose)
std::cout << timer.elapsed() << "sec" << std::endl;
return 0;
}
default:
std::cerr << "This mode is not supported in AnnoGraph" << std::endl;
exit(1);
}
return 0;
}