-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathntedit.cpp
2604 lines (2446 loc) · 87.5 KB
/
ntedit.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
#define PROGRAM "ntEdit v2.1.1" // NOLINT
// clang-format off
#include <iostream> //NOLINT(llvm-include-order)
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <utility>
#include <getopt.h>
#include <zlib.h>
#include <unordered_map>
#include <vector>
#include <queue>
#include <string>
#include <cmath>
#include <cassert>
#include <cerrno>
#include <unistd.h>
#include <map>
#include "lib/kseq.h"
#include <btllib/bloom_filter.hpp>
#include <btllib/counting_bloom_filter.hpp>
#include <btllib/nthash.hpp>
#if _OPENMP
#include <omp.h>
#endif
//RLW 19AUG2023
#include <iterator>
#include <regex>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
// clang-format on
// NOLINTNEXTLINE
KSEQ_INIT(gzFile, gzread)
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
static const char VERSION_MESSAGE[] =
PROGRAM "\n"
"written by Rene L Warren, Jessica Zhang, Murathan T Goktas, Hamid Mohamadi,"
"Parham Kazemi and Johnathan Wong.\n"
"copyright 2018-current Canada's Michael Smith Genome Science Centre\n";
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
static const char USAGE_MESSAGE[] = PROGRAM
"\n"
"\n"
" Fast, lightweight, scalable genome sequence polishing and SNV detection & annotation\n"
"\n"
" Options:\n"
" -t, number of threads [default=4]\n"
" -f, draft genome assembly (FASTA, Multi-FASTA, and/or gzipped compatible), REQUIRED\n"
" -r, Bloom filter (BF) or counting BF (CBF) file (generated from ntStat v1.0.0+), REQUIRED\n"
" -e, secondary BF with k-mers to reject (generated from ntStat v1.0.0+), OPTIONAL - NOT "
"NEEDED with CBF\n"
" -b, output file prefix, OPTIONAL\n"
" -z, minimum contig length [default=100]\n"
" -i, maximum number of insertion bases to try, range 0-5, [default=5]\n"
" -d, maximum number of deletions bases to try, range 0-10, [default=5]\n"
" -x, k/x ratio for the number of k-mers that should be missing, [default=5.000]\n"
" -y, k/y ratio for the number of edited k-mers that should be present, [default=9.000]\n"
" -X, ratio of number of k-mers in the k subset that should be missing in order to "
"attempt fix (higher=stringent), [default=0.5]\n"
" -Y, ratio of number of k-mers in the k subset that should be present to accept an edit "
"(higher=stringent), [default=0.5]\n"
" -c, cap for the number of base insertions that can be made at one position, "
"[default=k*1.5]\n"
" -j, controls size of k-mer subset. When checking subset of k-mers, check every jth "
"k-mer, "
"[default=3]\n"
" -m, mode of editing, range 0-2, [default=0]\n"
" 0: best substitution, or first good indel\n"
" 1: best substitution, or best indel\n"
" 2: best edit overall (suggestion that you reduce i and d for performance)\n"
" -s, SNV mode. Overrides draft k-mer checks, forcing reassessment at each position (-s "
"1 "
"= yes, default = 0, no)\n"
" -l, input VCF file with annotated variants (e.g., clinvar.vcf), OPTIONAL\n"
" -a, soft masks missing k-mer positions having no fix (-v 1 = yes, default = 0, no)\n"
" -v, verbose mode (-v 1 = yes, default = 0, no)\n"
"\n"
" -p, minimum k-mer coverage threshold (CBF only) [default=minimum of counting Bloom filter\n"
"counts, cannot be larger than 255]\n"
" -q, maximum k-mer coverage threshold (CBF only) [default=255, largest possible value]\n"
" --help, display this message and exit \n"
" --version, output version information and exit\n"
"\n"
" If one of X/Y is set, ntEdit will use those parameters instead. Otherwise, it uses x/y by "
"default.\n"
"\n";
namespace opt {
/* Defining magical numbers. */
constexpr int default_min_contig_len = 100;
constexpr int default_max_insertions = 5;
constexpr int default_max_deletions = 5;
constexpr float default_edit_threshold = 9.0000;
constexpr float default_missing_threshold = 5.0000;
constexpr float default_insertion_cap_ratio = 1.5;
float edit_ratio = 0.5;
float missing_ratio = 0.5;
bool use_ratio = false;
unsigned jump = 3;
unsigned nthreads = 4;
std::string draft_filename; // NOLINT
std::string vcf_filename; // NOLINT
std::string bloom_filename; // NOLINT
std::string bloomrep_filename; // NOLINT
std::string outfile_prefix; // NOLINT
unsigned k;
unsigned h = 0;
unsigned e = 0;
unsigned min_contig_len = default_min_contig_len;
unsigned max_insertions = default_max_insertions;
unsigned max_deletions = default_max_deletions;
float edit_threshold = default_edit_threshold;
float missing_threshold = default_missing_threshold;
unsigned insertion_cap;
int mode = 0;
int snv = 0;
int mask = 0; // RLW2021
int verbose = 0;
int secbf = 0;
unsigned min_threshold = 1;
unsigned max_threshold = 255;
} // namespace opt
static const char shortopts[] = "t:f:s:k:z:b:r:v:d:i:X:Y:x:y:m:c:j:s:e:a:l:p:q:"; // RLW2021
enum
{
OPT_HELP = 1,
OPT_VERSION
};
static const struct option longopts[] = {
{ "threads", required_argument, nullptr, 't' },
{ "draft_file", required_argument, nullptr, 'f' },
{ "k", required_argument, nullptr, 'k' },
{ "minimum_contig_length", required_argument, nullptr, 'z' },
{ "maximum_insertions", required_argument, nullptr, 'i' },
{ "maximum_deletions", required_argument, nullptr, 'd' },
{ "insertion_cap", required_argument, nullptr, 'c' },
{ "edit_threshold", required_argument, nullptr, 'y' },
{ "missing_threshold", required_argument, nullptr, 'x' },
{ "edit_ratio", required_argument, nullptr, 'Y' },
{ "missing_ratio", required_argument, nullptr, 'X' },
{ "jump", required_argument, nullptr, 'j' },
{ "bloom_filename", required_argument, nullptr, 'r' },
{ "bloomrep_filename", required_argument, nullptr, 'e' },
{ "outfile_prefix", required_argument, nullptr, 'b' },
{ "mode", required_argument, nullptr, 'm' },
{ "snv", required_argument, nullptr, 's' },
{ "vcf_file", required_argument, nullptr, 'l' },
{ "mask", required_argument, nullptr, 'a' },
{ "verbose", required_argument, nullptr, 'v' },
{ "minimum_kmer_coverage", required_argument, nullptr, 'p' },
{ "maximum_kmer_coverage", required_argument, nullptr, 'q' },
{ "help", no_argument, nullptr, OPT_HELP },
{ "version", no_argument, nullptr, OPT_VERSION },
{ nullptr, 0, nullptr, 0 }
};
// Setting up the number of tries when for each number of base insertion
std::vector<unsigned> num_tries = { 0, 1, 5, 21, 85, 341 }; // NOLINT
// Initialize current base array
// NOLINTNEXTLINE
std::unordered_map<unsigned char, std::vector<unsigned char>> current_bases_array;
// Setting up polish base array
// NOLINTNEXTLINE
std::unordered_map<unsigned char, std::vector<unsigned char>> polish_bases_array = {
{ 'A', { 'T', 'C', 'G' } }, { 'T', { 'A', 'C', 'G' } }, { 'C', { 'A', 'T', 'G' } },
{ 'G', { 'A', 'T', 'C' } }, { 'R', { 'T', 'C' } }, { 'Y', { 'A', 'G' } },
{ 'S', { 'A', 'T' } }, { 'W', { 'C', 'G' } }, { 'K', { 'A', 'C' } },
{ 'M', { 'T', 'G' } }, { 'B', { 'A' } }, { 'D', { 'C' } },
{ 'H', { 'G' } }, { 'V', { 'T' } }, { 'N', { 'A', 'T', 'C', 'G' } }
};
// setting up snv base array XXRLWnov2020
// NOLINTNEXTLINE
std::unordered_map<unsigned char, std::vector<unsigned char>> snv_bases_array = {
{ 'A', { 'T', 'C', 'G' } }, { 'T', { 'A', 'C', 'G' } },
{ 'C', { 'A', 'T', 'G' } }, { 'G', { 'A', 'T', 'C' } },
{ 'R', { 'A', 'T', 'C', 'G' } }, { 'Y', { 'A', 'T', 'C', 'G' } },
{ 'S', { 'A', 'T', 'C', 'G' } }, { 'W', { 'A', 'T', 'C', 'G' } },
{ 'K', { 'A', 'T', 'C', 'G' } }, { 'M', { 'A', 'T', 'C', 'G' } },
{ 'B', { 'A', 'T', 'C', 'G' } }, { 'D', { 'A', 'T', 'C', 'G' } },
{ 'H', { 'A', 'T', 'C', 'G' } }, { 'V', { 'A', 'T', 'C', 'G' } },
{ 'N', { 'A', 'T', 'C', 'G' } }
};
// Setting all the indel combos
// NOLINTNEXTLINE
std::unordered_map<unsigned char, std::vector<std::string>> multi_possible_bases = {
{ 'A',
{ "A", "AA", "AC", "AG", "AT", "AAA", "AAC", "AAG", "AAT", "ACA",
"ACC", "ACG", "ACT", "AGA", "AGC", "AGG", "AGT", "ATA", "ATC", "ATG",
"ATT", "AAAA", "AAAC", "AAAG", "AAAT", "AACA", "AACC", "AACG", "AACT", "AAGA",
"AAGC", "AAGG", "AAGT", "AATA", "AATC", "AATG", "AATT", "ACAA", "ACAC", "ACAG",
"ACAT", "ACCA", "ACCC", "ACCG", "ACCT", "ACGA", "ACGC", "ACGG", "ACGT", "ACTA",
"ACTC", "ACTG", "ACTT", "AGAA", "AGAC", "AGAG", "AGAT", "AGCA", "AGCC", "AGCG",
"AGCT", "AGGA", "AGGC", "AGGG", "AGGT", "AGTA", "AGTC", "AGTG", "AGTT", "ATAA",
"ATAC", "ATAG", "ATAT", "ATCA", "ATCC", "ATCG", "ATCT", "ATGA", "ATGC", "ATGG",
"ATGT", "ATTA", "ATTC", "ATTG", "ATTT", "AAAAA", "AAAAC", "AAAAG", "AAAAT", "AAACA",
"AAACC", "AAACG", "AAACT", "AAAGA", "AAAGC", "AAAGG", "AAAGT", "AAATA", "AAATC", "AAATG",
"AAATT", "AACAA", "AACAC", "AACAG", "AACAT", "AACCA", "AACCC", "AACCG", "AACCT", "AACGA",
"AACGC", "AACGG", "AACGT", "AACTA", "AACTC", "AACTG", "AACTT", "AAGAA", "AAGAC", "AAGAG",
"AAGAT", "AAGCA", "AAGCC", "AAGCG", "AAGCT", "AAGGA", "AAGGC", "AAGGG", "AAGGT", "AAGTA",
"AAGTC", "AAGTG", "AAGTT", "AATAA", "AATAC", "AATAG", "AATAT", "AATCA", "AATCC", "AATCG",
"AATCT", "AATGA", "AATGC", "AATGG", "AATGT", "AATTA", "AATTC", "AATTG", "AATTT", "ACAAA",
"ACAAC", "ACAAG", "ACAAT", "ACACA", "ACACC", "ACACG", "ACACT", "ACAGA", "ACAGC", "ACAGG",
"ACAGT", "ACATA", "ACATC", "ACATG", "ACATT", "ACCAA", "ACCAC", "ACCAG", "ACCAT", "ACCCA",
"ACCCC", "ACCCG", "ACCCT", "ACCGA", "ACCGC", "ACCGG", "ACCGT", "ACCTA", "ACCTC", "ACCTG",
"ACCTT", "ACGAA", "ACGAC", "ACGAG", "ACGAT", "ACGCA", "ACGCC", "ACGCG", "ACGCT", "ACGGA",
"ACGGC", "ACGGG", "ACGGT", "ACGTA", "ACGTC", "ACGTG", "ACGTT", "ACTAA", "ACTAC", "ACTAG",
"ACTAT", "ACTCA", "ACTCC", "ACTCG", "ACTCT", "ACTGA", "ACTGC", "ACTGG", "ACTGT", "ACTTA",
"ACTTC", "ACTTG", "ACTTT", "AGAAA", "AGAAC", "AGAAG", "AGAAT", "AGACA", "AGACC", "AGACG",
"AGACT", "AGAGA", "AGAGC", "AGAGG", "AGAGT", "AGATA", "AGATC", "AGATG", "AGATT", "AGCAA",
"AGCAC", "AGCAG", "AGCAT", "AGCCA", "AGCCC", "AGCCG", "AGCCT", "AGCGA", "AGCGC", "AGCGG",
"AGCGT", "AGCTA", "AGCTC", "AGCTG", "AGCTT", "AGGAA", "AGGAC", "AGGAG", "AGGAT", "AGGCA",
"AGGCC", "AGGCG", "AGGCT", "AGGGA", "AGGGC", "AGGGG", "AGGGT", "AGGTA", "AGGTC", "AGGTG",
"AGGTT", "AGTAA", "AGTAC", "AGTAG", "AGTAT", "AGTCA", "AGTCC", "AGTCG", "AGTCT", "AGTGA",
"AGTGC", "AGTGG", "AGTGT", "AGTTA", "AGTTC", "AGTTG", "AGTTT", "ATAAA", "ATAAC", "ATAAG",
"ATAAT", "ATACA", "ATACC", "ATACG", "ATACT", "ATAGA", "ATAGC", "ATAGG", "ATAGT", "ATATA",
"ATATC", "ATATG", "ATATT", "ATCAA", "ATCAC", "ATCAG", "ATCAT", "ATCCA", "ATCCC", "ATCCG",
"ATCCT", "ATCGA", "ATCGC", "ATCGG", "ATCGT", "ATCTA", "ATCTC", "ATCTG", "ATCTT", "ATGAA",
"ATGAC", "ATGAG", "ATGAT", "ATGCA", "ATGCC", "ATGCG", "ATGCT", "ATGGA", "ATGGC", "ATGGG",
"ATGGT", "ATGTA", "ATGTC", "ATGTG", "ATGTT", "ATTAA", "ATTAC", "ATTAG", "ATTAT", "ATTCA",
"ATTCC", "ATTCG", "ATTCT", "ATTGA", "ATTGC", "ATTGG", "ATTGT", "ATTTA", "ATTTC", "ATTTG",
"ATTTT" } },
{ 'C',
{ "C", "CA", "CC", "CG", "CT", "CAA", "CAC", "CAG", "CAT", "CCA",
"CCC", "CCG", "CCT", "CGA", "CGC", "CGG", "CGT", "CTA", "CTC", "CTG",
"CTT", "CAAA", "CAAC", "CAAG", "CAAT", "CACA", "CACC", "CACG", "CACT", "CAGA",
"CAGC", "CAGG", "CAGT", "CATA", "CATC", "CATG", "CATT", "CCAA", "CCAC", "CCAG",
"CCAT", "CCCA", "CCCC", "CCCG", "CCCT", "CCGA", "CCGC", "CCGG", "CCGT", "CCTA",
"CCTC", "CCTG", "CCTT", "CGAA", "CGAC", "CGAG", "CGAT", "CGCA", "CGCC", "CGCG",
"CGCT", "CGGA", "CGGC", "CGGG", "CGGT", "CGTA", "CGTC", "CGTG", "CGTT", "CTAA",
"CTAC", "CTAG", "CTAT", "CTCA", "CTCC", "CTCG", "CTCT", "CTGA", "CTGC", "CTGG",
"CTGT", "CTTA", "CTTC", "CTTG", "CTTT", "CAAAA", "CAAAC", "CAAAG", "CAAAT", "CAACA",
"CAACC", "CAACG", "CAACT", "CAAGA", "CAAGC", "CAAGG", "CAAGT", "CAATA", "CAATC", "CAATG",
"CAATT", "CACAA", "CACAC", "CACAG", "CACAT", "CACCA", "CACCC", "CACCG", "CACCT", "CACGA",
"CACGC", "CACGG", "CACGT", "CACTA", "CACTC", "CACTG", "CACTT", "CAGAA", "CAGAC", "CAGAG",
"CAGAT", "CAGCA", "CAGCC", "CAGCG", "CAGCT", "CAGGA", "CAGGC", "CAGGG", "CAGGT", "CAGTA",
"CAGTC", "CAGTG", "CAGTT", "CATAA", "CATAC", "CATAG", "CATAT", "CATCA", "CATCC", "CATCG",
"CATCT", "CATGA", "CATGC", "CATGG", "CATGT", "CATTA", "CATTC", "CATTG", "CATTT", "CCAAA",
"CCAAC", "CCAAG", "CCAAT", "CCACA", "CCACC", "CCACG", "CCACT", "CCAGA", "CCAGC", "CCAGG",
"CCAGT", "CCATA", "CCATC", "CCATG", "CCATT", "CCCAA", "CCCAC", "CCCAG", "CCCAT", "CCCCA",
"CCCCC", "CCCCG", "CCCCT", "CCCGA", "CCCGC", "CCCGG", "CCCGT", "CCCTA", "CCCTC", "CCCTG",
"CCCTT", "CCGAA", "CCGAC", "CCGAG", "CCGAT", "CCGCA", "CCGCC", "CCGCG", "CCGCT", "CCGGA",
"CCGGC", "CCGGG", "CCGGT", "CCGTA", "CCGTC", "CCGTG", "CCGTT", "CCTAA", "CCTAC", "CCTAG",
"CCTAT", "CCTCA", "CCTCC", "CCTCG", "CCTCT", "CCTGA", "CCTGC", "CCTGG", "CCTGT", "CCTTA",
"CCTTC", "CCTTG", "CCTTT", "CGAAA", "CGAAC", "CGAAG", "CGAAT", "CGACA", "CGACC", "CGACG",
"CGACT", "CGAGA", "CGAGC", "CGAGG", "CGAGT", "CGATA", "CGATC", "CGATG", "CGATT", "CGCAA",
"CGCAC", "CGCAG", "CGCAT", "CGCCA", "CGCCC", "CGCCG", "CGCCT", "CGCGA", "CGCGC", "CGCGG",
"CGCGT", "CGCTA", "CGCTC", "CGCTG", "CGCTT", "CGGAA", "CGGAC", "CGGAG", "CGGAT", "CGGCA",
"CGGCC", "CGGCG", "CGGCT", "CGGGA", "CGGGC", "CGGGG", "CGGGT", "CGGTA", "CGGTC", "CGGTG",
"CGGTT", "CGTAA", "CGTAC", "CGTAG", "CGTAT", "CGTCA", "CGTCC", "CGTCG", "CGTCT", "CGTGA",
"CGTGC", "CGTGG", "CGTGT", "CGTTA", "CGTTC", "CGTTG", "CGTTT", "CTAAA", "CTAAC", "CTAAG",
"CTAAT", "CTACA", "CTACC", "CTACG", "CTACT", "CTAGA", "CTAGC", "CTAGG", "CTAGT", "CTATA",
"CTATC", "CTATG", "CTATT", "CTCAA", "CTCAC", "CTCAG", "CTCAT", "CTCCA", "CTCCC", "CTCCG",
"CTCCT", "CTCGA", "CTCGC", "CTCGG", "CTCGT", "CTCTA", "CTCTC", "CTCTG", "CTCTT", "CTGAA",
"CTGAC", "CTGAG", "CTGAT", "CTGCA", "CTGCC", "CTGCG", "CTGCT", "CTGGA", "CTGGC", "CTGGG",
"CTGGT", "CTGTA", "CTGTC", "CTGTG", "CTGTT", "CTTAA", "CTTAC", "CTTAG", "CTTAT", "CTTCA",
"CTTCC", "CTTCG", "CTTCT", "CTTGA", "CTTGC", "CTTGG", "CTTGT", "CTTTA", "CTTTC", "CTTTG",
"CTTTT" } },
{ 'G',
{ "G", "GA", "GC", "GG", "GT", "GAA", "GAC", "GAG", "GAT", "GCA",
"GCC", "GCG", "GCT", "GGA", "GGC", "GGG", "GGT", "GTA", "GTC", "GTG",
"GTT", "GAAA", "GAAC", "GAAG", "GAAT", "GACA", "GACC", "GACG", "GACT", "GAGA",
"GAGC", "GAGG", "GAGT", "GATA", "GATC", "GATG", "GATT", "GCAA", "GCAC", "GCAG",
"GCAT", "GCCA", "GCCC", "GCCG", "GCCT", "GCGA", "GCGC", "GCGG", "GCGT", "GCTA",
"GCTC", "GCTG", "GCTT", "GGAA", "GGAC", "GGAG", "GGAT", "GGCA", "GGCC", "GGCG",
"GGCT", "GGGA", "GGGC", "GGGG", "GGGT", "GGTA", "GGTC", "GGTG", "GGTT", "GTAA",
"GTAC", "GTAG", "GTAT", "GTCA", "GTCC", "GTCG", "GTCT", "GTGA", "GTGC", "GTGG",
"GTGT", "GTTA", "GTTC", "GTTG", "GTTT", "GAAAA", "GAAAC", "GAAAG", "GAAAT", "GAACA",
"GAACC", "GAACG", "GAACT", "GAAGA", "GAAGC", "GAAGG", "GAAGT", "GAATA", "GAATC", "GAATG",
"GAATT", "GACAA", "GACAC", "GACAG", "GACAT", "GACCA", "GACCC", "GACCG", "GACCT", "GACGA",
"GACGC", "GACGG", "GACGT", "GACTA", "GACTC", "GACTG", "GACTT", "GAGAA", "GAGAC", "GAGAG",
"GAGAT", "GAGCA", "GAGCC", "GAGCG", "GAGCT", "GAGGA", "GAGGC", "GAGGG", "GAGGT", "GAGTA",
"GAGTC", "GAGTG", "GAGTT", "GATAA", "GATAC", "GATAG", "GATAT", "GATCA", "GATCC", "GATCG",
"GATCT", "GATGA", "GATGC", "GATGG", "GATGT", "GATTA", "GATTC", "GATTG", "GATTT", "GCAAA",
"GCAAC", "GCAAG", "GCAAT", "GCACA", "GCACC", "GCACG", "GCACT", "GCAGA", "GCAGC", "GCAGG",
"GCAGT", "GCATA", "GCATC", "GCATG", "GCATT", "GCCAA", "GCCAC", "GCCAG", "GCCAT", "GCCCA",
"GCCCC", "GCCCG", "GCCCT", "GCCGA", "GCCGC", "GCCGG", "GCCGT", "GCCTA", "GCCTC", "GCCTG",
"GCCTT", "GCGAA", "GCGAC", "GCGAG", "GCGAT", "GCGCA", "GCGCC", "GCGCG", "GCGCT", "GCGGA",
"GCGGC", "GCGGG", "GCGGT", "GCGTA", "GCGTC", "GCGTG", "GCGTT", "GCTAA", "GCTAC", "GCTAG",
"GCTAT", "GCTCA", "GCTCC", "GCTCG", "GCTCT", "GCTGA", "GCTGC", "GCTGG", "GCTGT", "GCTTA",
"GCTTC", "GCTTG", "GCTTT", "GGAAA", "GGAAC", "GGAAG", "GGAAT", "GGACA", "GGACC", "GGACG",
"GGACT", "GGAGA", "GGAGC", "GGAGG", "GGAGT", "GGATA", "GGATC", "GGATG", "GGATT", "GGCAA",
"GGCAC", "GGCAG", "GGCAT", "GGCCA", "GGCCC", "GGCCG", "GGCCT", "GGCGA", "GGCGC", "GGCGG",
"GGCGT", "GGCTA", "GGCTC", "GGCTG", "GGCTT", "GGGAA", "GGGAC", "GGGAG", "GGGAT", "GGGCA",
"GGGCC", "GGGCG", "GGGCT", "GGGGA", "GGGGC", "GGGGG", "GGGGT", "GGGTA", "GGGTC", "GGGTG",
"GGGTT", "GGTAA", "GGTAC", "GGTAG", "GGTAT", "GGTCA", "GGTCC", "GGTCG", "GGTCT", "GGTGA",
"GGTGC", "GGTGG", "GGTGT", "GGTTA", "GGTTC", "GGTTG", "GGTTT", "GTAAA", "GTAAC", "GTAAG",
"GTAAT", "GTACA", "GTACC", "GTACG", "GTACT", "GTAGA", "GTAGC", "GTAGG", "GTAGT", "GTATA",
"GTATC", "GTATG", "GTATT", "GTCAA", "GTCAC", "GTCAG", "GTCAT", "GTCCA", "GTCCC", "GTCCG",
"GTCCT", "GTCGA", "GTCGC", "GTCGG", "GTCGT", "GTCTA", "GTCTC", "GTCTG", "GTCTT", "GTGAA",
"GTGAC", "GTGAG", "GTGAT", "GTGCA", "GTGCC", "GTGCG", "GTGCT", "GTGGA", "GTGGC", "GTGGG",
"GTGGT", "GTGTA", "GTGTC", "GTGTG", "GTGTT", "GTTAA", "GTTAC", "GTTAG", "GTTAT", "GTTCA",
"GTTCC", "GTTCG", "GTTCT", "GTTGA", "GTTGC", "GTTGG", "GTTGT", "GTTTA", "GTTTC", "GTTTG",
"GTTTT" } },
{ 'T',
{ "T", "TA", "TC", "TG", "TT", "TAA", "TAC", "TAG", "TAT", "TCA",
"TCC", "TCG", "TCT", "TGA", "TGC", "TGG", "TGT", "TTA", "TTC", "TTG",
"TTT", "TAAA", "TAAC", "TAAG", "TAAT", "TACA", "TACC", "TACG", "TACT", "TAGA",
"TAGC", "TAGG", "TAGT", "TATA", "TATC", "TATG", "TATT", "TCAA", "TCAC", "TCAG",
"TCAT", "TCCA", "TCCC", "TCCG", "TCCT", "TCGA", "TCGC", "TCGG", "TCGT", "TCTA",
"TCTC", "TCTG", "TCTT", "TGAA", "TGAC", "TGAG", "TGAT", "TGCA", "TGCC", "TGCG",
"TGCT", "TGGA", "TGGC", "TGGG", "TGGT", "TGTA", "TGTC", "TGTG", "TGTT", "TTAA",
"TTAC", "TTAG", "TTAT", "TTCA", "TTCC", "TTCG", "TTCT", "TTGA", "TTGC", "TTGG",
"TTGT", "TTTA", "TTTC", "TTTG", "TTTT", "TAAAA", "TAAAC", "TAAAG", "TAAAT", "TAACA",
"TAACC", "TAACG", "TAACT", "TAAGA", "TAAGC", "TAAGG", "TAAGT", "TAATA", "TAATC", "TAATG",
"TAATT", "TACAA", "TACAC", "TACAG", "TACAT", "TACCA", "TACCC", "TACCG", "TACCT", "TACGA",
"TACGC", "TACGG", "TACGT", "TACTA", "TACTC", "TACTG", "TACTT", "TAGAA", "TAGAC", "TAGAG",
"TAGAT", "TAGCA", "TAGCC", "TAGCG", "TAGCT", "TAGGA", "TAGGC", "TAGGG", "TAGGT", "TAGTA",
"TAGTC", "TAGTG", "TAGTT", "TATAA", "TATAC", "TATAG", "TATAT", "TATCA", "TATCC", "TATCG",
"TATCT", "TATGA", "TATGC", "TATGG", "TATGT", "TATTA", "TATTC", "TATTG", "TATTT", "TCAAA",
"TCAAC", "TCAAG", "TCAAT", "TCACA", "TCACC", "TCACG", "TCACT", "TCAGA", "TCAGC", "TCAGG",
"TCAGT", "TCATA", "TCATC", "TCATG", "TCATT", "TCCAA", "TCCAC", "TCCAG", "TCCAT", "TCCCA",
"TCCCC", "TCCCG", "TCCCT", "TCCGA", "TCCGC", "TCCGG", "TCCGT", "TCCTA", "TCCTC", "TCCTG",
"TCCTT", "TCGAA", "TCGAC", "TCGAG", "TCGAT", "TCGCA", "TCGCC", "TCGCG", "TCGCT", "TCGGA",
"TCGGC", "TCGGG", "TCGGT", "TCGTA", "TCGTC", "TCGTG", "TCGTT", "TCTAA", "TCTAC", "TCTAG",
"TCTAT", "TCTCA", "TCTCC", "TCTCG", "TCTCT", "TCTGA", "TCTGC", "TCTGG", "TCTGT", "TCTTA",
"TCTTC", "TCTTG", "TCTTT", "TGAAA", "TGAAC", "TGAAG", "TGAAT", "TGACA", "TGACC", "TGACG",
"TGACT", "TGAGA", "TGAGC", "TGAGG", "TGAGT", "TGATA", "TGATC", "TGATG", "TGATT", "TGCAA",
"TGCAC", "TGCAG", "TGCAT", "TGCCA", "TGCCC", "TGCCG", "TGCCT", "TGCGA", "TGCGC", "TGCGG",
"TGCGT", "TGCTA", "TGCTC", "TGCTG", "TGCTT", "TGGAA", "TGGAC", "TGGAG", "TGGAT", "TGGCA",
"TGGCC", "TGGCG", "TGGCT", "TGGGA", "TGGGC", "TGGGG", "TGGGT", "TGGTA", "TGGTC", "TGGTG",
"TGGTT", "TGTAA", "TGTAC", "TGTAG", "TGTAT", "TGTCA", "TGTCC", "TGTCG", "TGTCT", "TGTGA",
"TGTGC", "TGTGG", "TGTGT", "TGTTA", "TGTTC", "TGTTG", "TGTTT", "TTAAA", "TTAAC", "TTAAG",
"TTAAT", "TTACA", "TTACC", "TTACG", "TTACT", "TTAGA", "TTAGC", "TTAGG", "TTAGT", "TTATA",
"TTATC", "TTATG", "TTATT", "TTCAA", "TTCAC", "TTCAG", "TTCAT", "TTCCA", "TTCCC", "TTCCG",
"TTCCT", "TTCGA", "TTCGC", "TTCGG", "TTCGT", "TTCTA", "TTCTC", "TTCTG", "TTCTT", "TTGAA",
"TTGAC", "TTGAG", "TTGAT", "TTGCA", "TTGCC", "TTGCG", "TTGCT", "TTGGA", "TTGGC", "TTGGG",
"TTGGT", "TTGTA", "TTGTC", "TTGTG", "TTGTT", "TTTAA", "TTTAC", "TTTAG", "TTTAT", "TTTCA",
"TTTCC", "TTTCG", "TTTCT", "TTTGA", "TTTGC", "TTTGG", "TTTGT", "TTTTA", "TTTTC", "TTTTG",
"TTTTT" } }
};
class
BFWrapper // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init,-warnings-as-errors)
{
public:
BFWrapper(const std::string& path) // NOLINT
{
is_cbf = btllib::BloomFilter::check_file_signature(
path, btllib::KMER_COUNTING_BLOOM_FILTER_SIGNATURE);
if (is_cbf) {
cbf = std::make_unique<btllib::KmerCountingBloomFilter8>(path);
} else {
bf = std::make_unique<btllib::KmerBloomFilter>(path);
}
}
BFWrapper() = default; // NOLINT
bool contains(const uint64_t* hashes) const
{
return is_cbf ? cbf.get()->contains(hashes) > 0 : bf.get()->contains(hashes);
}
uint8_t get_count(const uint64_t* hashes) const
{
return is_cbf ? cbf.get()->contains(hashes) : 1;
}
bool is_counting() const { return is_cbf; }
unsigned get_k() const { return is_cbf ? cbf.get()->get_k() : bf.get()->get_k(); }
unsigned get_hash_num() const
{
return is_cbf ? cbf.get()->get_hash_num() : bf.get()->get_hash_num();
}
void
print_details() // NOLINT(readability-convert-member-functions-to-static,-warnings-as-errors)
{
const auto size = is_cbf ? cbf.get()->get_bytes() : bf.get()->get_bytes();
const auto fpr = is_cbf ? cbf.get()->get_fpr() : bf.get()->get_fpr();
std::cout << "BLOOM::\tcounting: " << (is_cbf ? "YES" : "NO") << "\tsize: " << size
<< "\tnumber hash functions: " << get_hash_num() << "\tkmer size: " << get_k()
<< "\tFPR: " << fpr << std::endl;
}
private:
bool is_cbf;
std::unique_ptr<btllib::KmerBloomFilter> bf;
std::unique_ptr<btllib::KmerCountingBloomFilter8> cbf;
};
inline void
NTMC64(
const char* kmerSeq,
const unsigned k,
const unsigned m,
uint64_t& fhVal,
uint64_t& rhVal,
uint64_t* hVal)
{
fhVal = btllib::hashing_internals::base_forward_hash(kmerSeq, k);
rhVal = btllib::hashing_internals::base_reverse_hash(kmerSeq, k);
uint64_t base_hash = btllib::hashing_internals::canonical(fhVal, rhVal);
btllib::hashing_internals::extend_hashes(base_hash, k, m, hVal);
}
inline void
NTMC64(
const unsigned char charOut,
const unsigned char charIn,
const unsigned k,
const unsigned m,
uint64_t& fhVal,
uint64_t& rhVal,
uint64_t* hVal)
{
fhVal = btllib::hashing_internals::next_forward_hash(fhVal, k, charOut, charIn);
rhVal = btllib::hashing_internals::next_reverse_hash(rhVal, k, charOut, charIn);
uint64_t base_hash = btllib::hashing_internals::canonical(fhVal, rhVal);
btllib::hashing_internals::extend_hashes(base_hash, k, m, hVal);
}
inline void
NTMC64_changelast(
const unsigned char charOut,
const unsigned char charIn,
const unsigned k,
const unsigned m,
uint64_t& fhVal,
uint64_t& rhVal,
uint64_t* hVal)
{
fhVal ^= btllib::hashing_internals::SEED_TAB[charOut];
fhVal ^= btllib::hashing_internals::SEED_TAB[charIn];
unsigned char out_rev = charOut & btllib::hashing_internals::CP_OFF;
unsigned char in_rev = charIn & btllib::hashing_internals::CP_OFF;
rhVal ^= btllib::hashing_internals::srol_table(out_rev, k - 1);
rhVal ^= btllib::hashing_internals::srol_table(in_rev, k - 1);
uint64_t base_hash = btllib::hashing_internals::canonical(fhVal, rhVal);
btllib::hashing_internals::extend_hashes(base_hash, k, m, hVal);
}
/* Returns the median of a vector of uint8_t. */
uint8_t
median(std::vector<uint8_t>& median_vec)
{
if (median_vec.size() > 0) {
std::sort(median_vec.begin(), median_vec.end());
return median_vec[median_vec.size() / 2];
}
return 0;
}
bool
is_kmer_solid(uint64_t* hVal, const BFWrapper& bloom, const BFWrapper& bloomrep)
{
bool solid_if_reg = !opt::secbf || !bloomrep.contains(hVal);
bool solid_if_count = !bloom.is_counting() || (bloom.get_count(hVal) <= opt::max_threshold &&
bloom.get_count(hVal) >= opt::min_threshold);
return solid_if_reg && solid_if_count;
}
/* Checks that the filepath is readable and exits if it is not. */
static inline void
assert_readable(const std::string& path)
{
if (access(path.c_str(), R_OK) == -1) {
std::cerr << PROGRAM ": error: `" << path << "': " << strerror(errno) << std::endl;
exit(EXIT_FAILURE);
}
}
/* Checks if the base is ATGC. */
bool
isATGCBase(unsigned char C)
{
return (C == 'A' || C == 'T' || C == 'G' || C == 'C');
}
/* Checks if the base is ATGC and IUPAC. */
bool
isAcceptedBase(unsigned char C)
{
return (
C == 'A' || C == 'T' || C == 'G' || C == 'C' || C == 'R' || C == 'Y' || C == 'S' ||
C == 'W' || C == 'K' || C == 'M' || C == 'B' || C == 'D' || C == 'H' || C == 'V');
}
char
RC(unsigned char C)
{
switch (C) {
case 'A':
case 'a':
return 'T';
case 'T':
case 't':
return 'A';
case 'G':
case 'g':
return 'C';
case 'C':
case 'c':
return 'G';
default:
return 'N';
}
}
/* Find the first only ATGC kmer starting at the beginning of a sequence.
* Assumption: no insertions or deletions. */
unsigned
findFirstAcceptedKmer(unsigned b_i, const std::string& contigSeq)
{
for (unsigned i = b_i; i + opt::k < contigSeq.size();) {
if (isAcceptedBase(toupper(contigSeq.at(i)))) {
bool good_kmer = true;
for (unsigned j = i + 1; j < i + opt::k; j++) {
if (!isAcceptedBase(toupper(contigSeq.at(j)))) {
good_kmer = false;
i = j + 1;
break;
}
}
if (good_kmer) {
return i;
}
} else {
i++;
}
}
return contigSeq.size() - 1;
}
/* Code to upper-case strings */
std::string
str_to_upper(const std::string& str)
{
std::string result = str;
for (char& c : result) {
c = std::toupper(c);
}
return result;
}
/* Helper for filling out the LPS array for detecting a low complexity repeat. */
void
computeLPSArray(std::string possible_repeat, int n, std::vector<int>& lps)
{
int len = 0;
int i;
lps[0] = 0;
i = 1;
while (i < n) {
if (possible_repeat[i] == possible_repeat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
/* Determines if a string is a low complexity repeat of a word. */
bool
isRepeatInsertion(const std::string& possible_repeat)
{
int n = possible_repeat.size();
std::vector<int> lps(n);
computeLPSArray(possible_repeat, n, lps);
int len = lps[n - 1];
return (len > 0 && n % (n - len) == 0);
}
/* Struct that keeps track of details for substitutions. */
struct sRec
{
unsigned pos = 0;
unsigned char draft_char = 0;
unsigned char sub_base = 0;
unsigned num_support = 0;
unsigned char altbase1 = 0;
unsigned altsupp1 = 0;
unsigned char altbase2 = 0;
unsigned altsupp2 = 0;
unsigned char altbase3 = 0;
unsigned altsupp3 = 0;
};
struct seqNode
{
int node_type = -1; // -1=unset; 0=position; 1=character
size_t s_pos = 0;
size_t e_pos = 0;
unsigned char c = 0;
unsigned num_support = 0;
};
/* Makes a character insertion RIGHT BEFORE <insert_pos> by creating a character node holding <c>
* with <num_support>
* - sets <node> to your insertion node (the node that holds the character <c>). */
void
makeInsertion(
unsigned& t_node_index,
unsigned insert_pos,
const std::string& insertion_bases,
unsigned num_support,
std::vector<seqNode>& newSeq)
{
seqNode orig_node = newSeq[t_node_index];
std::vector<seqNode> to_insert;
for (char insertion_base : insertion_bases) {
seqNode insertion_node;
insertion_node.node_type = 1;
insertion_node.c = insertion_base;
insertion_node.num_support = num_support;
to_insert.push_back(insertion_node);
}
if (orig_node.node_type == 0) {
if (insert_pos <= orig_node.s_pos) {
// gather nodes following this insertion
std::vector<seqNode> reappend;
unsigned i = t_node_index;
while (i < newSeq.size() && newSeq[i].node_type != -1) {
reappend.push_back(newSeq[i]);
newSeq[i].node_type = -1;
i++;
}
// make insertion
for (unsigned i = 0; i < to_insert.size(); i++) {
if (t_node_index + i < newSeq.size()) {
newSeq[t_node_index + i] = to_insert[i];
} else {
newSeq.push_back(to_insert[i]);
}
}
// reappend
for (unsigned i = 0; i < reappend.size(); i++) {
if (t_node_index + to_insert.size() + i < newSeq.size()) {
newSeq[t_node_index + to_insert.size() + i] = reappend[i];
} else {
newSeq.push_back(reappend[i]);
}
}
} else {
seqNode after_node;
after_node.node_type = 0;
after_node.s_pos = insert_pos;
after_node.e_pos = orig_node.e_pos;
newSeq[t_node_index].e_pos = insert_pos - 1;
for (unsigned i = 0; i < to_insert.size(); i++) {
if (t_node_index + i + 1 < newSeq.size()) {
newSeq[t_node_index + i + 1] = to_insert[i];
} else {
newSeq.push_back(to_insert[i]);
}
}
if (t_node_index + to_insert.size() + 1 < newSeq.size()) {
newSeq[t_node_index + to_insert.size() + 1] = after_node;
} else {
newSeq.push_back(after_node);
}
t_node_index++;
}
} else if (orig_node.node_type == 1) {
// gather the nodes following this insertion
unsigned i = t_node_index;
std::vector<seqNode> reappend;
while (i < newSeq.size() && newSeq[i].node_type != -1) {
reappend.push_back(newSeq[i]);
newSeq[i].node_type = -1;
i++;
}
// make the insertion
for (unsigned i = 0; i < to_insert.size(); i++) {
if (t_node_index + i < newSeq.size()) {
newSeq[t_node_index + i] = to_insert[i];
} else {
newSeq.push_back(to_insert[i]);
}
}
// push all of the insertions back
for (unsigned i = 0; i < reappend.size(); i++) {
if (t_node_index + to_insert.size() + i < newSeq.size()) {
newSeq[t_node_index + to_insert.size() + i] = reappend[i];
} else {
newSeq.push_back(reappend[i]);
}
}
}
}
/* Make a deletion starting and including <pos> of length <num_del> with support <num_support> in
* seqNode structure.
* - sets the t_node_index and pos to the position right after the deletion */
void
makeDeletion(
unsigned& t_node_index,
unsigned& pos,
unsigned num_del,
unsigned num_support,
std::vector<seqNode>& newSeq)
{
seqNode orig_node = newSeq[t_node_index];
if (orig_node.node_type == 0) {
unsigned leftover_del = 0;
if (pos <= orig_node.s_pos) {
if (pos + num_del <= orig_node.e_pos) {
// we are deleting off the beginning of a position node
newSeq[t_node_index].s_pos = pos + num_del;
newSeq[t_node_index].num_support = num_support;
pos = newSeq[t_node_index].s_pos;
return;
}
// we deleted the entire position node and are moving on
leftover_del = pos + num_del - orig_node.e_pos;
pos = orig_node.e_pos + 1;
// overwite the following onto this one
unsigned i = t_node_index + 1;
while (i < newSeq.size() && newSeq[i].node_type != -1) {
newSeq[i - 1] = newSeq[i];
newSeq[i].node_type = -1;
i++;
}
} else {
if (pos + num_del <= orig_node.e_pos) {
// we are deleting in the middle of a position node
seqNode split_node;
split_node.node_type = 0;
split_node.s_pos = pos + num_del;
split_node.e_pos = orig_node.e_pos;
split_node.num_support = num_support;
newSeq[t_node_index].e_pos = pos - 1;
pos = split_node.s_pos;
t_node_index++;
if (t_node_index < newSeq.size()) {
newSeq[t_node_index] = split_node;
} else {
newSeq.push_back(split_node);
}
return;
}
// deleted from the middle of a position node past the end of it
leftover_del = pos + num_del - orig_node.e_pos;
newSeq[t_node_index].e_pos = pos - 1;
pos = orig_node.e_pos + 1;
t_node_index++;
}
if (leftover_del > 0) {
// pass the deletion to the next seqNode
if (t_node_index < newSeq.size() && newSeq[t_node_index].node_type != -1) {
if (newSeq[t_node_index].node_type == 0) {
pos = newSeq[t_node_index].s_pos;
}
makeDeletion(t_node_index, pos, leftover_del, num_support, newSeq);
}
}
} else if (orig_node.node_type == 1) {
unsigned i = t_node_index;
unsigned leftover_del = num_del;
// delete all the characters as possible
while (i < newSeq.size() && newSeq[i].node_type == 1 && leftover_del > 0) {
newSeq[i].node_type = -1;
leftover_del--;
i++;
}
// overwrite what comes after the characters
unsigned j = t_node_index;
while (i < newSeq.size() && newSeq[i].node_type != -1) {
newSeq[j] = newSeq[i];
newSeq[i].node_type = -1;
i++;
j++;
}
// deal with whatever is left
if (leftover_del > 0) {
// pass the deletion to the next seqNode
if (t_node_index < newSeq.size() && newSeq[t_node_index].node_type != -1) {
if (newSeq[t_node_index].node_type == 0) {
pos = newSeq[t_node_index].s_pos;
}
makeDeletion(t_node_index, pos, leftover_del, num_support, newSeq);
}
}
}
}
/* Returns the character at pos based on the seqNode structure. */
unsigned char
getCharacter(unsigned& pos, seqNode node, const std::string& contigSeq)
{
if (node.node_type == 0) {
return contigSeq.at(pos);
}
if (node.node_type == 1) {
return node.c;
}
unsigned char c = 0;
return c;
}
/* Increments the position and adjusts the node accordingly based on seqNode structure. */
void
increment(unsigned& pos, unsigned& node_index, std::vector<seqNode>& newSeq)
{
seqNode node = newSeq[node_index];
if (node.node_type == 0) {
pos++;
if (pos > node.e_pos) {
node_index++;
if (node_index < newSeq.size() && newSeq[node_index].node_type == 0) {
pos = newSeq[node_index].s_pos;
}
}
} else if (node.node_type == 1) {
node_index++;
if (node_index < newSeq.size() && newSeq[node_index].node_type == 0) {
pos = newSeq[node_index].s_pos;
}
}
}
/* Find the first accepted kmer (contains only ATGC characters) starting anywhere, based on the
* RopeLink structure. */
std::string
findAcceptedKmer(
unsigned& h_seq_i,
unsigned& t_seq_i,
unsigned& h_node_index,
unsigned& t_node_index,
const std::string& contigSeq,
std::vector<seqNode>& newSeq)
{
// temporary values
std::string kmer_str;
seqNode curr_node = newSeq[t_node_index];
unsigned temp_t_node_index = t_node_index;
unsigned temp_h_node_index;
unsigned i = t_seq_i;
while (i < contigSeq.size() && temp_t_node_index < newSeq.size() &&
newSeq[temp_t_node_index].node_type != -1) {
unsigned char c;
c = getCharacter(i, curr_node, contigSeq);
if (isAcceptedBase(toupper(c))) {
std::string kmer_str;
kmer_str += c;
temp_h_node_index = temp_t_node_index;
unsigned j = i;
increment(j, temp_t_node_index, newSeq);
// continue until you cant
while (j < contigSeq.size() && temp_t_node_index < newSeq.size() &&
newSeq[temp_t_node_index].node_type != -1) {
curr_node = newSeq[temp_t_node_index];
c = getCharacter(j, curr_node, contigSeq);
if (!isAcceptedBase(toupper(c))) {
i = j;
break;
}
kmer_str += c;
if (kmer_str.size() == opt::k) {
break;
}
increment(j, temp_t_node_index, newSeq);
}
// you found a good kmer so return it and adjust
if (kmer_str.size() == opt::k) {
h_seq_i = i;
t_seq_i = j;
h_node_index = temp_h_node_index;
t_node_index = temp_t_node_index;
return kmer_str;
}
}
increment(i, temp_t_node_index, newSeq);
}
h_seq_i = contigSeq.length();
t_seq_i = contigSeq.length();
return "";
}
/* Get the previous insertion (aka continuous string of character nodes) starting at t_node_index.
*/
std::string
getPrevInsertion(unsigned t_seq_i, unsigned t_node_index, std::vector<seqNode>& newSeq)
{
std::string prev_insertion;
// if we just finished the insertion
if ((t_node_index < newSeq.size() && newSeq[t_node_index].node_type == 0 &&
t_seq_i == newSeq[t_node_index].s_pos) ||
newSeq[t_node_index].node_type == 1) {
t_node_index--;
}
while (t_node_index < newSeq.size() && newSeq[t_node_index].node_type == 1) {
prev_insertion += RC(newSeq[t_node_index].c);
t_node_index--;
}
return prev_insertion;
}
/* Write the edits and new draft contig into respective files. */
void
writeEditsToFile(
std::ofstream& dfout,
std::ofstream& rfout,
std::ofstream& vfout,
const std::string& contigHdr,
const std::string& contigSeq,
std::vector<seqNode>& newSeq,
std::queue<sRec>& substitution_record,
std::map<std::string, std::string>& clinvar)
{
dfout << ">" << contigHdr.c_str() << "\n"; // FASTA HEADER RLWYY
unsigned node_index = 0;
std::string insertion_bases;
int num_support = -1;
unsigned char draft_char;
unsigned pos = 0;
// track a deletion
std::string deleted_bases;
seqNode curr_node = newSeq[node_index];
while (node_index < newSeq.size() && curr_node.node_type != -1) {
if (curr_node.node_type == 0) {
// print out every information about contigSeq
draft_char = contigSeq.at(curr_node.s_pos);
// log an insertion if it occured before this
if (!insertion_bases.empty()) {
// 19APR2024RLW >>>
std::string clinvarinfo;
std::ostringstream altid;
std::string insert_str;
draft_char = contigSeq.at(curr_node.s_pos - insertion_bases.size());
insert_str = draft_char;
insert_str += insertion_bases;
altid << contigHdr.c_str() << ">" << char(toupper(draft_char)) << pos
<< str_to_upper(insert_str);
std::string altvarid = altid.str(); // RLW 21AUG2023
if (!clinvar[altvarid].empty()) {
clinvarinfo += "^";
clinvarinfo += clinvar[altvarid];
} else {
clinvarinfo += "^NA";
}
// 19APR2024RLW <<<
rfout << contigHdr.c_str() << "\t" << pos << "\t" << draft_char << "\t+"
<< insertion_bases.c_str() << "\t" << num_support << "\n";
vfout << contigHdr.c_str() << "\t" << pos << "\t.\t" << draft_char << "\t"
<< draft_char << insertion_bases.c_str() << "\t.\tPASS\tAD=" << num_support
<< clinvarinfo << "\tGT\t1/1\n";
insertion_bases = "";
num_support = -1;
}
// log all the substitutions up to this point
while (!substitution_record.empty() &&
substitution_record.front().pos <= curr_node.e_pos) {
std::vector<char> alt_base_vcf(0);
std::vector<unsigned> alt_supp_vcf(0);
bool snv_mode_no_edit =
!(opt::snv && substitution_record.front().draft_char ==
substitution_record.front().sub_base);
if (snv_mode_no_edit) {
rfout << contigHdr.c_str() << "\t" << substitution_record.front().pos + 1
<< "\t" << substitution_record.front().draft_char << "\t"
<< substitution_record.front().sub_base << "\t"
<< substitution_record.front().num_support;
}
std::string base(1, substitution_record.front().sub_base);
std::string support = std::to_string(substitution_record.front().num_support);
std::string clinvarinfo; // RLW 23AUG2023