-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamnatate.cpp
1209 lines (1064 loc) · 52.9 KB
/
amnatate.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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <iomanip>
#include <map>
#include <omp.h>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <btllib/aahash.hpp>
#include <btllib/seq.hpp>
#include <btllib/seq_reader.hpp>
#include <btllib/mi_bloom_filter.hpp>
#include <Sequence/Translate.hpp>
size_t calc_optimal_size(size_t entries, unsigned hash_num, double occupancy)
{
size_t non64ApproxVal =
size_t(-double(entries) * double(hash_num) / log(1.0 - occupancy));
return non64ApproxVal + (64 - non64ApproxVal % 64);
}
std::vector<std::string> sixframe_translate(const std::string &dna)
{
std::vector<std::string> protein;
std::string rev_dna = btllib::get_reverse_complement(dna);
protein.push_back(Sequence::Translate(dna.begin(), dna.end()));
protein.push_back(Sequence::Translate(dna.begin() + 1, dna.end()));
protein.push_back(Sequence::Translate(dna.begin() + 2, dna.end()));
protein.push_back(Sequence::Translate(rev_dna.begin(), rev_dna.end()));
protein.push_back(Sequence::Translate(rev_dna.begin() + 1, rev_dna.end()));
protein.push_back(Sequence::Translate(rev_dna.begin() + 2, rev_dna.end()));
return protein;
}
btllib::MIBloomFilter<uint64_t> make_mibf(const std::string& seq, const size_t hash_num, const size_t kmer_size)
{
btllib::MIBloomFilter<uint64_t> mi_bf(calc_optimal_size(seq.size() * 3, 1, 0.1), 1);
for (int stage = 0; stage < 3; stage++)
{
btllib::AAHash itr(seq, hash_num, kmer_size, 1);
btllib::AAHash itr2(seq, hash_num, kmer_size, 2);
btllib::AAHash itr3(seq, hash_num, kmer_size, 3);
size_t miBf_ID = 1;
while (itr.roll() && itr2.roll() && itr3.roll())
{
if (stage == 0)
{
mi_bf.insert_bv(itr.hashes());
mi_bf.insert_bv(itr2.hashes());
mi_bf.insert_bv(itr3.hashes());
}
else if (stage == 1)
{
uint64_t new_ID = (uint64_t)miBf_ID << 32 | itr.get_pos();
mi_bf.insert_id(itr.hashes(), new_ID);
mi_bf.insert_id(itr2.hashes(), new_ID);
mi_bf.insert_id(itr3.hashes(), new_ID);
}
else
{
uint64_t new_ID = (uint64_t)miBf_ID << 32 | itr.get_pos();
mi_bf.insert_saturation(itr.hashes(), new_ID);
mi_bf.insert_saturation(itr2.hashes(), new_ID);
mi_bf.insert_saturation(itr3.hashes(), new_ID);
}
}
if (stage == 0)
{
mi_bf.complete_bv_insertion();
}
}
return mi_bf;
}
void fill_in_gaps(std::vector<std::tuple<size_t, size_t>>& start_end_pos_vec, std::vector<std::tuple<size_t, size_t>>& start_end_pos_in_tar_space_vec, size_t& adjusted_kmer_counts, const std::string& seq, size_t hash_num, size_t rescue_kmer_size, const std::vector<std::string>& sixframed_xlated_proteins, size_t ori)
{
//sort start_end_pos_vec by start_pos
std::sort(start_end_pos_vec.begin(), start_end_pos_vec.end(), [](const std::tuple<size_t, size_t> &a, const std::tuple<size_t, size_t> &b)
{
return std::get<0>(a) < std::get<0>(b);
});
// make a new vector and find all the gaps between the start and end pos that are larger than kmer_size
std::vector<std::tuple<size_t, size_t>> gap_vec;
for (size_t i = 0; i < start_end_pos_vec.size() - 1; ++i)
{
if (std::get<0>(start_end_pos_vec[i + 1]) - std::get<1>(start_end_pos_vec[i]) >= rescue_kmer_size)
{
gap_vec.emplace_back(std::make_tuple(std::get<1>(start_end_pos_vec[i]), std::get<0>(start_end_pos_vec[i + 1])));
}
}
// make a new vector and find all the gaps between the start and end pos in tar that are larger than kmer_size
std::vector<std::tuple<size_t, size_t>> gap_in_tar_space_vec;
for (size_t i = 0; i < start_end_pos_in_tar_space_vec.size() - 1; ++i)
{
if (std::get<0>(start_end_pos_in_tar_space_vec[i + 1]) - std::get<1>(start_end_pos_in_tar_space_vec[i]) >= rescue_kmer_size)
{
gap_in_tar_space_vec.emplace_back(std::make_tuple(std::get<1>(start_end_pos_in_tar_space_vec[i]), std::get<0>(start_end_pos_in_tar_space_vec[i + 1])));
}
}
// make an unordered_set of all the pos in the gaps in start_end_pos_in_tar_space_vec
std::unordered_set<size_t> gap_index_set;
for (auto &gap : gap_in_tar_space_vec)
{
for (size_t i = std::get<0>(gap); i < std::get<1>(gap); ++i)
{
gap_index_set.insert(i);
}
}
// make small mibf
auto small_mi_bf = make_mibf(seq, hash_num, rescue_kmer_size);
// iterate through gap_index_set and check if the pos is in the mibf
// if it is, add the pos to the start_end_pos_vec
size_t start_of_first_gap = std::get<0>(gap_vec[0]);
size_t end_of_last_gap = std::get<1>(gap_vec[gap_vec.size() - 1]);
for (size_t frame = 0; frame < 3; ++frame)
{
btllib::AAHash aahash(sixframed_xlated_proteins[frame + ori * 3], hash_num, rescue_kmer_size, 1, start_of_first_gap - 1);
aahash.roll();
bool prev_contains = false;
while(aahash.get_pos() <= end_of_last_gap + 1)
{
if (small_mi_bf.bv_contains(aahash.hashes()))
{
// get pos of mibf entry
auto temp_ID_pos = small_mi_bf.get_id(aahash.hashes());
for (auto &ID_pos : temp_ID_pos)
{
auto pos = ID_pos & 0xFFFFFFFF;
if (gap_index_set.find(pos) != gap_index_set.end())
{
// remove entry
gap_index_set.erase(pos);
adjusted_kmer_counts++;
break;
}
}
prev_contains = true;
} else {
if (prev_contains) {
// add to start_end_pos_vec
adjusted_kmer_counts = adjusted_kmer_counts + rescue_kmer_size + 1;
}
prev_contains = false;
}
}
}
for (auto &pos : gap_index_set)
{
btllib::AAHash aahash(sixframed_xlated_proteins[0 + ori * 3], hash_num, rescue_kmer_size, 1, pos);
if (small_mi_bf.bv_contains(aahash.hashes()))
{
start_end_pos_vec.emplace_back(std::make_tuple(pos, pos + rescue_kmer_size));
}
}
}
bool explore_frame(btllib::MIBloomFilter<uint64_t> &mi_bf, btllib::AAHash &aahash, std::deque<std::vector<uint32_t>> &miBf_IDs_snapshot, std::deque<std::vector<uint32_t>> &miBf_pos_snapshot, std::unordered_map<uint32_t, size_t> &id_to_count)
{
// check size of miBf_IDs_snapshot and miBf_pos_snapshot
// if size is more than 10, pop front
std::unordered_set<uint32_t> id_set;
if (miBf_IDs_snapshot.size() >= 5)
{
// insert id into id_set before removing
for (size_t i = 0; i < miBf_IDs_snapshot.front().size(); ++i)
{
id_set.insert(miBf_IDs_snapshot.front()[i]);
}
// remove id from id_to_count
for (auto &ID : id_set)
{
id_to_count[ID]--;
if (id_to_count[ID] == 0)
{
id_to_count.erase(ID);
}
}
id_set.clear();
miBf_IDs_snapshot.pop_front();
miBf_pos_snapshot.pop_front();
}
// push back new vector
// consider circular linked list
miBf_IDs_snapshot.emplace_back(std::vector<uint32_t>());
miBf_pos_snapshot.emplace_back(std::vector<uint32_t>());
if (!mi_bf.bv_contains(aahash.hashes()))
{
// clear snapshots and id_to_count
miBf_IDs_snapshot.clear();
miBf_pos_snapshot.clear();
id_to_count.clear();
return false;
}
// query mibf and insert into both deques
auto temp_ID_pos = mi_bf.get_id(aahash.hashes());
for (auto &ID_pos : temp_ID_pos)
{
miBf_IDs_snapshot.back().push_back(ID_pos >> 32);
miBf_pos_snapshot.back().push_back(ID_pos & 0xFFFFFFFF);
}
for (size_t j = 0; j < miBf_IDs_snapshot.back().size(); ++j)
{
id_set.insert(miBf_IDs_snapshot.back()[j]);
}
for (auto &ID : id_set)
{
if (id_to_count.find(ID) == id_to_count.end())
{
id_to_count[ID] = 1;
}
else
{
id_to_count[ID]++;
}
}
id_set.clear();
if (miBf_IDs_snapshot.size() < 5)
{
return false;
}
uint32_t temp_mibf_ID = 0;
size_t temp_max_count = 0;
// iterate id_to_count and find the ID with the highest count
for (auto &ID_count : id_to_count)
{
if (ID_count.second > temp_max_count)
{
temp_mibf_ID = ID_count.first;
temp_max_count = ID_count.second;
}
}
if (temp_mibf_ID == 0 || temp_max_count < 5)
{
return false;
}
else
{
std::vector<uint32_t> ids_to_check;
for (auto &ID_count : id_to_count)
{
if (ID_count.second == temp_max_count)
{
ids_to_check.push_back(ID_count.first);
}
}
// check to see if the ID with the highest count has consecutive positions
// if not, return false
// if yes, return true
for (auto &ID : ids_to_check)
{
std::set<uint32_t> temp_pos_set;
for (size_t i = 0; i < miBf_IDs_snapshot.size(); ++i)
{
for (size_t j = 0; j < miBf_IDs_snapshot[i].size(); ++j)
{
if (miBf_IDs_snapshot[i][j] == ID)
{
temp_pos_set.insert(miBf_pos_snapshot[i][j]);
}
}
}
// iterate through temp_pos_set and check if it is incrementing by 1
uint32_t prev_pos = 0;
size_t counter = 0;
bool init = false;
for (auto &pos : temp_pos_set)
{
if (!init)
{
prev_pos = pos;
init = true;
continue;
}
if (pos - prev_pos == 1)
{
++counter;
}
else
{
counter = 0;
}
prev_pos = pos;
if (counter >= 4)
{
return true;
}
}
}
/*std::set<uint32_t> temp_pos_set;
for (size_t i = 0; i < miBf_IDs_snapshot.size(); ++i)
{
for (size_t j = 0; j < miBf_IDs_snapshot[i].size(); ++j)
{
if (miBf_IDs_snapshot[i][j] == temp_mibf_ID)
{
temp_pos_set.insert(miBf_pos_snapshot[i][j]);
}
}
}
// iterate through temp_pos_set and check if it is incrementing by 1
uint32_t prev_pos = 0;
size_t counter = 0;
for (auto &pos : temp_pos_set)
{
if (prev_pos == 0)
{
prev_pos = pos;
continue;
}
if (pos - prev_pos == 1)
{
++counter;
}
else
{
counter = 0;
}
prev_pos = pos;
}
if (counter < 9)
{
return false;
}*/
/*////std::cerr << "check pos before return true" << std::endl;
for (auto &pos : temp_pos_set)
{
//std::cerr << "pos: " << pos << std::endl;
}*/
}
return false;
}
// main function that takes in command line arguments using getopt
int main(int argc, char **argv)
{
// declare variables
int opt;
int option_index = 0;
int verbose_flag = 0;
int help_flag = 0;
size_t threads = 1;
std::string input_file = "";
std::string reference_path = "";
std::string output_prefix = "_";
uint8_t hash_num = 1;
uint8_t kmer_size = 10;
size_t rescue_kmer_size = 4;
uint64_t genome_size = 0;
static struct option long_options[] = {
{"help", no_argument, &help_flag, 1},
{"verbose", no_argument, &verbose_flag, 1},
{"threads", required_argument, 0, 't'},
{"input", required_argument, 0, 'i'},
{"reference", required_argument, 0, 'r'},
{"output", required_argument, 0, 'o'},
{"hash", required_argument, 0, 'h'},
{"kmer", required_argument, 0, 'k'},
{"genome", required_argument, 0, 'g'},
{0, 0, 0, 0}};
// loop through command line arguments
while ((opt = getopt_long(argc, argv, "g:h:i:t:o:r:k:", long_options, &option_index)) != -1)
{
switch (opt)
{
case 0:
if (long_options[option_index].flag != 0)
{
break;
}
std::cout << "option " << long_options[option_index].name;
if (optarg)
{
std::cout << " with arg " << optarg;
}
std::cout << std::endl;
break;
case 't':
threads = std::stoul(optarg);
break;
case 'i':
input_file = optarg;
break;
case 'r':
reference_path = optarg;
break;
case 'o':
output_prefix = optarg;
break;
case 'h':
hash_num = std::stoi(optarg);
break;
case 'k':
kmer_size = std::stoi(optarg);
break;
case 'g':
genome_size = std::stoul(optarg);
break;
case '?':
break;
default:
std::cout << "Unknown option: " << opt << std::endl;
break;
}
}
// print help message with required arguments
if (help_flag)
{
//std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
//std::cerr << "Options:" << std::endl;
//std::cerr << " -h, --help\t\t\tPrint this help message" << std::endl;
//std::cerr << " -i, --input\t\t\tInput file name" << std::endl;
//std::cerr << " -o, --output\t\t\tOutput prefix" << std::endl;
//std::cerr << " -r, --reference\t\tReference path" << std::endl;
//std::cerr << " -t, --threads\t\t\tNumber of threads to use (default: 1)" << std::endl;
//std::cerr << " -v, --verbose\t\t\tVerbose output" << std::endl;
exit(0);
}
// print error message if input file is not provided
if (input_file.empty())
{
//std::cerr << "Input file is required. Use -h or --help for more information." << std::endl;
exit(1);
}
// print error message if reference path is not provided
if (reference_path.empty())
{
//std::cerr << "Reference path is required. Use -h or --help for more information." << std::endl;
exit(1);
}
// print error message if threads is not provided
if (threads == 0)
{
//std::cerr << "Threads is required. Use -h or --help for more information." << std::endl;
exit(1);
}
// print log of current parameters if verbose flag is set
if (verbose_flag)
{
std::cerr << "Input file: " << input_file << "\n"
<< "Output prefix: " << output_prefix << "\n"
<< "Reference path: " << reference_path << "\n"
<< "Threads: " << threads << "\n"
<< "Hash number: " << (uint64_t)hash_num << "\n"
<< "Kmer size: " << (uint64_t)kmer_size << "\n"
<< "Genome size: " << genome_size << std::endl;
}
omp_set_num_threads(threads);
if (verbose_flag)
{
std::cerr << "Reading reference file: " << reference_path << std::endl;
}
// read through reference file which is a fasta file and count the number of characters in the sequences and assign it genome_size
btllib::SeqReader ref_reader(reference_path, btllib::SeqReader::Flag::LONG_MODE);
for (const auto record : ref_reader)
{
genome_size += record.seq.size();
}
btllib::MIBloomFilter<uint64_t> mi_bf(calc_optimal_size(std::max<size_t>(genome_size * 3, 1000000), hash_num, 0.1), hash_num);
// btllib::MIBloomFilter<uint64_t> mi_bf(calc_optimal_size(1000000000, hash_num, 0.1), hash_num);
if (verbose_flag)
{
std::cerr << "Creating seq_id to ID table" << std::endl;
}
std::unordered_map<std::string, uint32_t> seq_ID_to_miBf_ID;
std::unordered_map<uint32_t, std::pair<std::string, size_t>> miBf_ID_to_seq_ID_and_len;
std::unordered_map<uint32_t, std::string> miBf_ID_to_seq;
{
uint32_t miBf_ID = 1;
btllib::SeqReader reader(reference_path, btllib::SeqReader::Flag::LONG_MODE);
for (const auto record : reader)
{
// insert record.id into seq_ID_to_miBf_ID with value miBf_ID
seq_ID_to_miBf_ID[record.id] = miBf_ID;
// insert miBf_ID into miBf_ID_to_seq_ID_and_len with value record.id and record.seq.size()
miBf_ID_to_seq_ID_and_len[miBf_ID] = std::make_pair(record.id, record.seq.size());
miBf_ID_to_seq[miBf_ID] = record.seq;
/*//std::cerr << "seq name: " << record.id << " miBf_ID: " << miBf_ID << std::endl;
//std::cerr << "seq size: " << record.seq.size() << std::endl;
//std::cerr << "seq: " << record.seq << std::endl; */
++miBf_ID;
}
}
if (verbose_flag)
{
std::cerr << "Making miBF" << std::endl;
}
auto sTime = omp_get_wtime();
for (int stage = 0; stage < 3; stage++)
{
if (verbose_flag)
{
std::cerr << "stage:" << stage << std::endl;
}
btllib::SeqReader reader(reference_path, btllib::SeqReader::Flag::LONG_MODE);
#pragma omp parallel
for (const auto record : reader)
{
btllib::AAHash itr(record.seq, hash_num, kmer_size, 1);
btllib::AAHash itr2(record.seq, hash_num, kmer_size, 2);
btllib::AAHash itr3(record.seq, hash_num, kmer_size, 3);
auto &miBf_ID = seq_ID_to_miBf_ID[record.id];
while (itr.roll() && itr2.roll() && itr3.roll())
//while (itr.roll())
{
if (stage == 0)
{
mi_bf.insert_bv(itr.hashes());
mi_bf.insert_bv(itr2.hashes());
mi_bf.insert_bv(itr3.hashes());
}
else if (stage == 1)
{
uint64_t new_ID = (uint64_t)miBf_ID << 32 | itr.get_pos();
mi_bf.insert_id(itr.hashes(), new_ID);
mi_bf.insert_id(itr2.hashes(), new_ID);
mi_bf.insert_id(itr3.hashes(), new_ID);
}
else
{
uint64_t new_ID = (uint64_t)miBf_ID << 32 | itr.get_pos();
mi_bf.insert_saturation(itr.hashes(), new_ID);
mi_bf.insert_saturation(itr2.hashes(), new_ID);
mi_bf.insert_saturation(itr3.hashes(), new_ID);
}
}
}
if (stage == 0)
{
mi_bf.complete_bv_insertion();
}
}
std::cerr << "finished making MiBF" << std::endl;
std::cerr << "in " << std::setprecision(4) << std::fixed << omp_get_wtime() - sTime
<< "\n";
std::vector<std::ofstream> output_files(3);
std::vector<std::ofstream> gff_files(3);
// Open each output file stream with a unique filename based on output_prefix
for (int i = 0; i < 3; ++i) {
std::string filename = output_prefix + "_lvl" + std::to_string(i + 1) + ".results.tsv";
output_files[i].open(filename);
output_files[i] << "name\tcomplete copies\tincomplete copies\texpected k-mer counts\thighest adjusted incomplete k-mer hits" << std::endl;
gff_files[i].open(output_prefix + "_lvl" + std::to_string(i + 1) + ".gff");
gff_files[i] << "##gff-version 3" << std::endl;
}
/*pragma omp parallel
for (const auto record : reader) {
std::vector<std::string> protein = sixframe_translate(record.seq);
std::vector<std::map<uint32_t, size_t>> frame_to_id_to_hits(6);
size_t expected_hits = protein[0].size() - kmer_size + 1;
for (uint8_t i = 0; i < protein.size(); i++) {
AAHash itr(protein[i], hash_num, kmer_size);
auto& id_to_hits = frame_to_id_to_hits[i];
while (itr != AAHash::end()) {
auto temp_ID_hits = mi_bf.get_id(*itr); // change this to avoid reallocating memory
for (auto& ID_hits : temp_ID_hits) {
if (id_to_hits.find(ID_hits) == id_to_hits.end()) {
id_to_hits[ID_hits] = 1;
} else {
id_to_hits[ID_hits]++;
}
}
++itr;
}
*/
// make a gff set sorted by seq name and start pos
// the columns are seq name, start pos, end pos, score, strand,
// source, type missing because always the same, no attribute,phase
// compartor for gff set, sorted by seq name and start pos
auto gff_comparator = [](const std::tuple<std::string, size_t, size_t, double, std::string, std::string> &a, const std::tuple<std::string, size_t, size_t, double, std::string, std::string> &b)
{
if (std::get<0>(a) == std::get<0>(b))
{
return std::get<1>(a) < std::get<1>(b);
}
return std::get<0>(a) < std::get<0>(b);
};
std::vector<std::set<std::tuple<std::string, size_t, size_t, double, std::string, std::string>, decltype(gff_comparator)>> gff_set_vector;
// Create and insert three different instances of gff_set into the vector
for (int i = 0; i < 3; ++i) {
gff_set_vector.emplace_back(gff_comparator);
}
btllib::SeqReader reader(input_file, btllib::SeqReader::Flag::LONG_MODE);
if (verbose_flag)
{
std::cerr << "Reading input file: " << input_file << std::endl;
}
// std::unordered_map<std::string, std::tuple<size_t, size_t, size_t, size_t>> seq_name_to_completeness;
struct completeness_struct
{
size_t complete_copies = 0;
size_t incomplete_copies = 0;
size_t expected_kmer_counts = 0;
size_t highest_adjusted_kmer_counts = 0;
};
std::vector<std::unordered_map<std::string, completeness_struct>> seq_name_to_completeness_vec(3);
// Populate each map in the vector with empty entries
for (auto &seq_name_to_completeness : seq_name_to_completeness_vec) {
for (const auto &seq_ID : seq_ID_to_miBf_ID) {
seq_name_to_completeness[seq_ID.first] = completeness_struct();
}
}
#pragma omp parallel num_threads(threads / 2)
for (const auto record : reader)
{
// //std::cerr << "seq name: " << record.id << std::endl;
std::vector<std::string> sixframed_xlated_proteins = sixframe_translate(record.seq);
/*std::cerr << "length of original sequence: " << record.seq.size() << std::endl;
std::cerr << "length of protein 1: " << sixframed_xlated_proteins[0].size() << std::endl;
std::cerr << "length of protein 2: " << sixframed_xlated_proteins[1].size() << std::endl;
std::cerr << "length of protein 3: " << sixframed_xlated_proteins[2].size() << std::endl;
// crate a protein fa file and write all proteins to it
std::ofstream protein_fa_file(output_prefix + record.id + ".fa");
protein_fa_file << ">" << record.id << "_1" << std::endl;
protein_fa_file << sixframed_xlated_proteins[0] << std::endl;
protein_fa_file << ">" << record.id << "_2" << std::endl;
protein_fa_file << sixframed_xlated_proteins[1] << std::endl;
protein_fa_file << ">" << record.id << "_3" << std::endl;
protein_fa_file << sixframed_xlated_proteins[2] << std::endl;
protein_fa_file << ">" << record.id << "_4" << std::endl;
protein_fa_file << sixframed_xlated_proteins[3] << std::endl;
protein_fa_file << ">" << record.id << "_5" << std::endl;
protein_fa_file << sixframed_xlated_proteins[4] << std::endl;
protein_fa_file << ">" << record.id << "_6" << std::endl;
protein_fa_file << sixframed_xlated_proteins[5] << std::endl;
protein_fa_file.close();*/
// //std::cerr << "protein 4: " << sixframed_xlated_proteins[3] << std::endl;
// //std::cerr << "protein 5: " << sixframed_xlated_proteins[4] << std::endl;
// //std::cerr << "protein 6: " << sixframed_xlated_proteins[5] << std::endl;
#pragma omp parallel for num_threads(2)
for (size_t ori = 0; ori < 2; ++ori)
{
// frame to block id to id and smallest pos and largest pos
std::vector<std::unordered_map<size_t, std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>>>> frame_to_block_id_to_id_and_pos_vec(3);
// id to count across all frames sorted by count largest to smallest
std::vector<std::map<uint32_t, size_t, std::greater<size_t>>> id_to_count_across_all_frames_vec(3);
// custom comparator for set of pair of size_t and pair of size_t and size_t to sort by seq pos
auto custom_comparator = [](const std::tuple<size_t, size_t, size_t> &a, const std::tuple<size_t, size_t, size_t> &b)
{
return std::get<2>(a) < std::get<2>(b);
};
// id to set of frame and block id and seq pos, set is sorted by seq pos
std::vector<std::unordered_map<uint32_t, std::set<std::tuple<size_t, size_t, size_t>, decltype(custom_comparator)>>> id_to_frame_block_id_and_seq_pos_vec(3);
for (size_t curr_lvl = 1; curr_lvl <= 3; ++curr_lvl) {
auto& frame_to_block_id_to_id_and_pos = frame_to_block_id_to_id_and_pos_vec[curr_lvl - 1];
auto& id_to_count_across_all_frames = id_to_count_across_all_frames_vec[curr_lvl - 1];
auto& id_to_frame_block_id_and_seq_pos = id_to_frame_block_id_and_seq_pos_vec[curr_lvl - 1];
auto& gff_set = gff_set_vector[curr_lvl - 1];
auto& seq_name_to_completeness = seq_name_to_completeness_vec[curr_lvl - 1];
for (size_t frame = 0; frame < 3; ++frame)
{
/*if (verbose_flag) {
std::cerr << "frame: " << frame << " ori: " << ori << std::endl;
btllib::AAHash aahash_test(sixframed_xlated_proteins[frame + ori * 3], hash_num, kmer_size, 1);
btllib::AAHash aahash_test2(sixframed_xlated_proteins[frame + ori * 3], hash_num, kmer_size, 2);
aahash_test.roll();
aahash_test2.roll();
for (size_t i = 0; i < 6000; ++i) {
if (mi_bf.bv_contains(aahash_test.hashes())) {
std::cerr << "lvl 1" << std::endl;
std::cerr << "bv contains" << i << " th hash" << std::endl;
// get id and pos
auto temp_ID_pos = mi_bf.get_id(aahash_test.hashes());
for (auto &ID_pos : temp_ID_pos) {
std::cerr << "ID: " << (ID_pos >> 32) << " pos: " << (ID_pos & 0xFFFFFFFF) << std::endl;
}
}
if (mi_bf.bv_contains(aahash_test2.hashes())) {
std::cerr << "lvl 2" << std::endl;
std::cerr << "bv contains" << i << " th hash" << std::endl;
// get id and pos
auto temp_ID_pos = mi_bf.get_id(aahash_test2.hashes());
for (auto &ID_pos : temp_ID_pos) {
std::cerr << "ID: " << (ID_pos >> 32) << " pos: " << (ID_pos & 0xFFFFFFFF) << std::endl;
}
}
aahash_test.roll();
aahash_test2.roll();
}
}
*/
btllib::AAHash aahash(sixframed_xlated_proteins[frame + ori * 3], hash_num, kmer_size, curr_lvl);
aahash.roll();
std::deque<std::vector<uint32_t>> miBf_IDs_snapshot;
std::deque<std::vector<uint32_t>> miBf_pos_snapshot;
std::unordered_map<uint32_t, size_t> id_to_count;
size_t block_id = 0;
std::unordered_set<uint32_t> id_set;
while (aahash.get_pos() != std::numeric_limits<size_t>::max())
{
//std::cerr << "checkpoint 1" << std::endl;
while (!explore_frame(mi_bf, aahash, miBf_IDs_snapshot, miBf_pos_snapshot, id_to_count) && aahash.get_pos() != std::numeric_limits<size_t>::max())
{
// //std::cerr << "explore_frame returned false" << std::endl;
aahash.roll();
}
//std::cerr << "checkpoint 2" << std::endl;
if (aahash.get_pos() == std::numeric_limits<size_t>::max())
{
break;
}
//std::cerr << "checkpoint 3" << std::endl;
size_t seq_pos = aahash.get_pos() - kmer_size + 1;
// find the largest count in id_to_count
size_t temp_max_count = 0;
for (auto &ID_count : id_to_count)
{
if (ID_count.second > temp_max_count)
{
temp_max_count = ID_count.second;
}
}
// insert id into id_set if count is equal to temp_max_count
for (auto &ID_count : id_to_count)
{
if (ID_count.second == temp_max_count)
{
id_set.insert(ID_count.first);
}
}
std::unordered_map<uint32_t, std::set<uint32_t>> id_to_pos_set;
for (size_t i = 0; i < miBf_IDs_snapshot.size(); ++i)
{
for (size_t j = 0; j < miBf_IDs_snapshot[i].size(); ++j)
{
if (id_set.find(miBf_IDs_snapshot[i][j]) != id_set.end())
{
id_to_pos_set[miBf_IDs_snapshot[i][j]].insert(miBf_pos_snapshot[i][j]);
}
}
}
aahash.roll();
bool extend_block = true;
while (extend_block && aahash.get_pos() != std::numeric_limits<size_t>::max())
{
std::vector<uint32_t> ids_vec;
std::vector<uint32_t> temp_pos_vec;
if (mi_bf.bv_contains(aahash.hashes()))
{
auto temp_ID_pos = mi_bf.get_id(aahash.hashes());
bool found = false;
for (auto &ID : id_set)
{
for (auto &ID_pos : temp_ID_pos)
{
if (ID == (ID_pos >> 32))
{
found = true;
break;
}
}
if (found)
{
break;
}
}
if (found)
{
for (auto &ID_pos : temp_ID_pos)
{
ids_vec.push_back(ID_pos >> 32);
temp_pos_vec.push_back(ID_pos & 0xFFFFFFFF);
}
for (size_t i = 0; i < ids_vec.size(); ++i)
{
// check if ID is in id_set
std::set<uint32_t> temp_pos_set;
if (id_set.find(ids_vec[i]) != id_set.end())
{
temp_pos_set.insert(temp_pos_vec[i]);
}
for (auto &pos : temp_pos_set)
{
if (id_to_pos_set[ids_vec[i]].size() != 0 && pos == *id_to_pos_set[ids_vec[i]].rbegin() + 1)
{
id_to_pos_set[ids_vec[i]].insert(pos);
break;
}
}
}
// check if the pos set in id_to_pos_set are all the same size
/*size_t temp_size = 0;
for (auto &ID_pos_set : id_to_pos_set)
{
if (temp_size == 0)
{
temp_size = ID_pos_set.second.size();
}
else if (temp_size != ID_pos_set.second.size())
{
extend_block = false;
break;
}
}*/
////std::cerr << "checkpoint 12" << std::endl;
}
else
{
extend_block = false;
}
}
else
{
extend_block = false;
}
if (extend_block)
{
aahash.roll();
}
}
// log the block id, id, and smallest and largest pos
for (auto &ID_pos_set : id_to_pos_set)
{
if (ID_pos_set.second.size() == 0)
{
continue;
}
frame_to_block_id_to_id_and_pos[frame][block_id] = std::make_pair(*ID_pos_set.second.begin(), *ID_pos_set.second.rbegin());
id_to_frame_block_id_and_seq_pos[ID_pos_set.first].insert(std::make_tuple(frame, block_id, seq_pos));
#pragma omp critical
{
if (id_to_count_across_all_frames.find(ID_pos_set.first) == id_to_count_across_all_frames.end())
{
//std::cerr << "checkpoint x.8" << std::endl;
id_to_count_across_all_frames[ID_pos_set.first] = ID_pos_set.second.size();
//std::cerr << "checkpoint x.9" << std::endl;
}
else
{
//std::cerr << "checkpoint x.10" << std::endl;
id_to_count_across_all_frames[ID_pos_set.first] += ID_pos_set.second.size();
//std::cerr << "checkpoint x.11" << std::endl;
}
}
++block_id;
}
//std::cerr << "checkpoint y" << std::endl;
// clear miBf_IDs_snapshot, miBf_pos_snapshot, and id_to_count
miBf_IDs_snapshot.clear();
miBf_pos_snapshot.clear();
id_to_count.clear();
}
}
// iterate through id_to_count_across_all_frames and log the completeness
// print id_to_count_across_all_frames
if (verbose_flag) {
for (auto &ID_count : id_to_count_across_all_frames)
{
std::cerr << "ID: " << ID_count.first << " count: " << ID_count.second << std::endl;
}
// print id_to_frame_block_id_and_seq_pos
for (auto &ID_frame_block_id_seq_pos : id_to_frame_block_id_and_seq_pos)
{
std::cerr << "ID: " << ID_frame_block_id_seq_pos.first << std::endl;
std::cerr << "name: " << miBf_ID_to_seq_ID_and_len[ID_frame_block_id_seq_pos.first].first << std::endl;
for (auto &frame_block_id_seq_pos : ID_frame_block_id_seq_pos.second)
{
std::cerr << "frame: " << std::get<0>(frame_block_id_seq_pos) << " block_id: " << std::get<1>(frame_block_id_seq_pos) << " seq_pos: " << std::get<2>(frame_block_id_seq_pos) << std::endl;
}
}
// print frame_to_block_id_to_id_and_pos
for (auto &frame_block_id_to_id_and_pos : frame_to_block_id_to_id_and_pos)
{
std::cerr << "frame: " << frame_block_id_to_id_and_pos.first << std::endl;
for (auto &block_id_to_id_and_pos : frame_block_id_to_id_and_pos.second)
{
std::cerr << "block_id: " << block_id_to_id_and_pos.first << " smallest pos: " << block_id_to_id_and_pos.second.first << " largest pos: " << block_id_to_id_and_pos.second.second << std::endl;
}
}
}
std::string strand = "+";
if (ori == 1)
{
strand = "-";
}
for (auto &ID_count : id_to_count_across_all_frames)
{
uint32_t miBf_ID = ID_count.first;
if (verbose_flag){
std::cerr << "ID: " << ID_count.first << " count: " << ID_count.second << std::endl;
}
// iterate through id to frame block id and seq pos and log the completeness
std::string seq_name = miBf_ID_to_seq_ID_and_len[miBf_ID].first;
size_t complete_copies = 0;
size_t incomplete_copies = 0;
size_t expected_kmer_counts = miBf_ID_to_seq_ID_and_len[miBf_ID].second - kmer_size + 1;
size_t adjusted_kmer_counts = 0;