-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.hpp
6274 lines (5661 loc) · 358 KB
/
main.hpp
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
#ifndef main_hpp_INCLUDED
#define main_hpp_INCLUDED
#include "bcf_formats.step1.hpp" // auto-generated
#include "CmdLineArgs.hpp"
#include "common.hpp"
#include "iohts.hpp"
#include "logging.hpp"
#include "main_consensus.hpp"
#include "main_conversion.hpp"
#include "MolecularID.hpp"
#include "htslib/faidx.h"
#include "htslib/hts.h"
#include "htslib/sam.h"
#include "htslib/synced_bcf_reader.h"
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
#include <vector>
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
class HapLink {
public:
std::basic_string<std::pair<uvc1_refgpos_t, AlignmentSymbol>> pos_symb_string;
std::array<uvc1_readnum_t, 2> fr_cnts;
std::array<uvc1_readnum_t, 2> other_hap_cnts;
HapLink(
std::basic_string<std::pair<uvc1_refgpos_t, AlignmentSymbol>> pos_symb_string_1,
std::array<uvc1_readnum_t, 2> fr_cnts_1,
std::array<uvc1_readnum_t, 2> other_hap_cnt_1) {
pos_symb_string = pos_symb_string_1;
fr_cnts = fr_cnts_1;
other_hap_cnts = other_hap_cnt_1;
};
};
// T=uvc1_readnum_t for deletion and T=string for insertion
template <class T>
std::pair<uvc1_readnum_t, T>
indelToData_getMajority(const std::map<T, uvc1_readnum_t> & indel2cnt) {
uvc1_readnum_t maxcnt = 0;
T argmaxcnt = T();
for (const auto ic : indel2cnt) {
if (ic.second > maxcnt ||
((ic.second == maxcnt) && (ic.first > argmaxcnt))) {
maxcnt = ic.second;
argmaxcnt = ic.first;
}
}
return std::make_pair(maxcnt, argmaxcnt);
}
template <class T>
uvc1_readnum_t
posToIndelToData_get(const std::map<uvc1_readnum_t, std::map<T, uvc1_readnum_t>> & pos2indel2data, const uvc1_readnum_t refpos, const T & indel, uvc1_readnum_t default1 = 0, uvc1_readnum_t default2 = 0) {
if (pos2indel2data.find(refpos) == pos2indel2data.end()) { return default1; }
if (pos2indel2data.at(refpos).find(indel) == pos2indel2data.at(refpos).end()) { return default2; }
return pos2indel2data.at(refpos).at(indel);
}
template <class T>
void
posToIndelToCount_inc(std::map<uvc1_refgpos_t, std::map<T, uvc1_readnum_t>> & pos2indel2count, uvc1_readpos_t pos, const T indel, uvc1_readnum_t incvalue = 1) {
assertUVC(incvalue > 0);
auto pos2indel2count4it = pos2indel2count.insert(std::make_pair(pos, std::map<T, uvc1_readnum_t>()));
auto indel2count4it = pos2indel2count4it.first->second.insert(std::make_pair(indel, 0));
indel2count4it.first->second += incvalue;
assertUVC (posToIndelToData_get(pos2indel2count, pos, indel) > 0);
}
template <class T>
T
posToIndelToCount_updateByConsensus(std::map<uvc1_refgpos_t, std::map<T, uvc1_readnum_t>> & dst, const std::map<uvc1_refgpos_t, std::map<T, uvc1_readnum_t>> & src, uvc1_readnum_t epos, uvc1_readnum_t incvalue = 1) {
const auto pos2indel2count4it = src.find(epos);
assertUVC(pos2indel2count4it != src.end());
auto indel2count = pos2indel2count4it->second;
const T & src_indel = (indel2count.size() > 1 ? (indelToData_getMajority(indel2count).second) : indel2count.begin()->first);
// The following code generates null-valued InDels if more than one InDel is found, which is not intended.
// const T & src_indel = (indel2count.size() > 1 ? T() : indel2count.begin()->first);
assertUVC (indel2count.begin()->second > 0);
posToIndelToCount_inc<T>(dst, epos, src_indel, incvalue);
return src_indel;
}
// For reason of commenting-out this method, please check the method updateByRepresentative that calls this method
/*
template <bool TIsIncVariable, class T>
uvc1_readnum_t
posToIndelToCount_updateByRepresentative(std::map<uvc1_readnum_t, std::map<T, uvc1_readnum_t>> & dst, const std::map<uvc1_readnum_t, std::map<T, uvc1_readnum_t>> & src, uvc1_readnum_t epos, uvc1_readnum_t incvalue = 1) {
const auto pos2indel2count4it = src.find(epos);
assertUVC(pos2indel2count4it != src.end());
T max_indel;
uvc1_readnum_t max_count = 0;
uvc1_readnum_t sum_count = 0;
for (auto indel2count : pos2indel2count4it->second) {
auto indel = indel2count.first;
auto count = indel2count.second;
if (count > max_count) {
max_indel = indel;
max_count = count;
}
if (TIsIncVariable) {sum_count += count; }
}
assertUVC(0 < max_count);
if (TIsIncVariable) {
posToIndelToCount_inc<T>(dst, epos, max_indel, sum_count);
} else {
posToIndelToCount_inc<T>(dst, epos, max_indel, incvalue);
}
return max_count;
}
*/
template <class T>
void
posToIndelToCount_updateBySummation(std::map<uvc1_readnum_t, std::map<T, uvc1_readnum_t>> & dst, const std::map<uvc1_readnum_t, std::map<T, uvc1_readnum_t>> & src) {
for (auto src_pos2indel2count4it : src) {
auto src_pos = src_pos2indel2count4it.first;
auto src_indel2count = src_pos2indel2count4it.second;
for (auto src_indel2count4it : src_indel2count) {
assertUVC(src_indel2count4it.second > 0 || !(
std::cerr << src_indel2count4it.second << " > 0 failed for the key " << src_indel2count4it.first << " at position " << src_pos << std::endl
));
posToIndelToCount_inc<T>(dst, src_pos, src_indel2count4it.first, src_indel2count4it.second);
}
}
}
void
posToIndelToCount_DlenToDseq(std::map<uvc1_readnum_t, std::map<std::string, uvc1_readnum_t>> & dst, const std::map<uvc1_readnum_t, std::map<uvc1_readnum_t, uvc1_readnum_t>> & src,
const std::string & refchars, uvc1_readnum_t incluBegPos) {
for (auto src_pos2indel2count4it : src) {
uvc1_readnum_t src_pos = src_pos2indel2count4it.first;
auto src_del2count = src_pos2indel2count4it.second;
for (auto src_del2count4it : src_del2count) {
std::string dseq = refchars.substr(src_pos - incluBegPos, src_del2count4it.first);
posToIndelToCount_inc(dst, src_pos, dseq, src_del2count4it.second);
}
}
}
#define INS_N_ANCHOR_BASES 1 // https://www.ncbi.nlm.nih.gov/pmc/articles/PMC149199/
#define TVN_MICRO_VQ_DELTA 3
#define TIN_CONTAM_MICRO_VQ_DELTA 0
#define BAM_PHREDI(b, i) (bam_get_qual((b))[(i)])
const bcfrec::BcfFormat FORMAT_UNCOV = bcfrec::BcfFormat();
const RevComplement THE_REV_COMPLEMENT;
enum ValueType {
SYMBOL_COUNT_SUM,
BASE_QUALITY_MAX, // https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6302405/
VALUE_TYPE_END,
};
// is not WGS (i.e., is hybrid-capture-WES or amplicon-PCR)
template <class T1, class T2>
inline bool
does_fmt_imply_short_frag(const T1 & fmt, const T2 wgs_min_avg_fragsize) {
return (fmt.APLRI[0] + fmt.APLRI[2]) < int64mul(fmt.APLRI[1] + fmt.APLRI[3], wgs_min_avg_fragsize);
}
std::pair<uvc1_readnum_t, std::string>
read_family_con_ampl_getMajority_clip(const auto &read_family_con_ampl, const uvc1_refgpos_t epos) {
std::map<std::string, uvc1_readnum_t> indel2readnum = { {"", 0} };
auto p2e2d = read_family_con_ampl.getPosToCseqToData();
if (p2e2d.find(epos) != p2e2d.end()) {
std::map<std::string, uvc1_readnum_t> indel2data = p2e2d.find(epos)->second;
indel2readnum.insert(indel2data.begin(), indel2data.end());
}
return indelToData_getMajority(indel2readnum);
}
std::pair<uvc1_readnum_t, std::string>
read_family_con_ampl_getMajority_ins(const auto &read_family_con_ampl, const uvc1_refgpos_t epos) {
std::map<std::string, uvc1_readnum_t> indel2readnum = { {"", 0} };
for (const AlignmentSymbol s : INS_SYMBOLS) {
auto p2e2d = read_family_con_ampl.getPosToIseqToData(s);
if (p2e2d.find(epos) != p2e2d.end()) {
std::map<std::string, uvc1_readnum_t> indel2data = p2e2d.find(epos)->second;
indel2readnum.insert(indel2data.begin(), indel2data.end());
}
}
return indelToData_getMajority(indel2readnum);
}
std::pair<uvc1_readnum_t, uvc1_refgpos_t>
read_family_con_ampl_getMajority_del(const auto &read_family_con_ampl, const uvc1_refgpos_t epos) {
std::map<uvc1_refgpos_t, uvc1_readnum_t> indel2readnum = { {0, 0} };
for (const AlignmentSymbol s : DEL_SYMBOLS) {
auto p2e2d = read_family_con_ampl.getPosToDlenToData(s);
if (p2e2d.find(epos) != p2e2d.end()) {
std::map<uvc1_refgpos_t, uvc1_readnum_t> indel2data = p2e2d.find(epos)->second;
indel2readnum.insert(indel2data.begin(), indel2data.end());
}
}
return indelToData_getMajority(indel2readnum);
}
struct PhredMutationTable {
const uvc1_qual_t transition_CG_TA;
const uvc1_qual_t transition_AT_CG;
const uvc1_qual_t transversion_CG_AT;
const uvc1_qual_t transversion_other;
const uvc1_qual_t indel_open;
const uvc1_qual_t indel_ext;
uvc1_qual_t all_mutation_inc;
PhredMutationTable(
const uvc1_qual_t transition_CG_TA,
const uvc1_qual_t transition_AT_CG,
const uvc1_qual_t transversion_CG_AT,
const uvc1_qual_t transversion_other,
const uvc1_qual_t indel_open,
const uvc1_qual_t indel_ext,
const bool is_rescued)
:
transition_CG_TA(transition_CG_TA),
transition_AT_CG(transition_AT_CG),
transversion_CG_AT(transversion_CG_AT),
transversion_other(transversion_other),
indel_open(indel_open),
indel_ext(indel_ext),
all_mutation_inc(is_rescued ? 3 : 0)
{ }
uvc1_qual_t toPhredErrRateRaw(const AlignmentSymbol con_symbol, const AlignmentSymbol alt_symbol) const {
if (isSymbolIns(con_symbol) || isSymbolDel(con_symbol)) {
return indel_open;
} else if (con_symbol == LINK_M) {
if (LINK_D1 == alt_symbol || LINK_I1 == alt_symbol) {
return indel_open;
} else if (LINK_D2== alt_symbol || LINK_I2 == alt_symbol) {
return indel_open + indel_ext * 1;
} else {
return indel_open + indel_ext * 2;
}
} else if ((con_symbol == BASE_C && alt_symbol == BASE_T) || (con_symbol == BASE_G && alt_symbol == BASE_A)) {
return transition_CG_TA;
} else if ((con_symbol == BASE_A && alt_symbol == BASE_G) || (con_symbol == BASE_T && alt_symbol == BASE_C)) {
return transition_AT_CG;
} else if ((con_symbol == BASE_C && alt_symbol == BASE_A) || (con_symbol == BASE_G && alt_symbol == BASE_T)) {
return transversion_CG_AT;
} else {
return transversion_other;
}
}
uvc1_qual_t toPhredErrRate(const AlignmentSymbol con_symbol, const AlignmentSymbol alt_symbol) const {
return toPhredErrRateRaw(con_symbol, alt_symbol) + all_mutation_inc;
}
};
constexpr bool
is_mut_transition(const AlignmentSymbol con_symbol, const AlignmentSymbol alt_symbol) {
return ((con_symbol == BASE_C && alt_symbol == BASE_T) || (con_symbol == BASE_G && alt_symbol == BASE_A)
|| (con_symbol == BASE_T && alt_symbol == BASE_C) || (con_symbol == BASE_A && alt_symbol == BASE_G)
);
}
const uvc1_refgpos_t SYMBOL_TO_INDEL_N_UNITS[] = {
[BASE_A] = 0, [BASE_C] = 0, [BASE_G] = 0, [BASE_T] = 0, [BASE_N] = 0,
[BASE_NN] = 0,
[LINK_M] = 0,
[LINK_D3P] = -3, [LINK_D2] = -2, [LINK_D1] = -1,
[LINK_I3P] = 3, [LINK_I2] = 2, [LINK_I1] = 1,
[LINK_NN] = 0,
[END_ALIGNMENT_SYMBOLS] = 0,
};
template <class T>
class TDistribution {
protected:
std::array<T, NUM_ALIGNMENT_SYMBOLS> symbol2data;
};
typedef uvc1_readnum_t molcount_t;
template <class TB2C>
class GenericSymbol2Bucket2Count : TDistribution<TB2C> {
public:
molcount_t
getSymbolBucketCount(AlignmentSymbol symbol, size_t bucket) const {
assertUVC(bucket < NUM_BUCKETS);
return this->symbol2data[symbol][bucket];
};
const TB2C &
getSymbolCounts(AlignmentSymbol symbol) const {
return this->symbol2data[symbol];
};
void
incSymbolBucketCount(AlignmentSymbol symbol, size_t bucket, uvc1_qual_t increment) {
assertUVC(bucket < NUM_BUCKETS || !fprintf(stderr, "%lu < %d failed!", bucket, NUM_BUCKETS));
this->symbol2data[symbol].at(bucket) += increment;
};
const TB2C
vectorsumBySymbolType(const SymbolType symboltype) const {
TB2C ret = {0};
for (AlignmentSymbol symbol : SYMBOL_TYPE_TO_SYMBOLS[symboltype]) {
for (size_t i = 0; i < this->symbol2data[symbol].size(); i++) {
ret[i] += this->getSymbolBucketCount(symbol, i);
}
}
return ret;
};
void
clearSymbolBucketCount() {
assertUVC(sizeof(TB2C) * NUM_ALIGNMENT_SYMBOLS == sizeof(this->symbol2data) || !fprintf(stderr, "%lu * %u != %lu\n", sizeof(TB2C), NUM_ALIGNMENT_SYMBOLS, sizeof(this->symbol2data)));
for (size_t i = 0; i < NUM_ALIGNMENT_SYMBOLS; i++) {
for (size_t j = 0; j < this->symbol2data[0].size(); j++) {
this->symbol2data[i][j] = 0;
}
}
};
};
template <class TInteger>
class GenericSymbol2Count : TDistribution<TInteger> {
public:
TInteger
getSymbolCount(AlignmentSymbol symbol) const {
return this->symbol2data[symbol];
};
template <ValueType TUpdateType = SYMBOL_COUNT_SUM>
int // update_max_inc : high GC : 3, even distribution of nucleotides : 6, conservative : 0
incSymbolCount(const AlignmentSymbol symbol, const TInteger increment, const TInteger update_max_inc = 0) {
STATIC_ASSERT_WITH_DEFAULT_MSG(BASE_QUALITY_MAX == TUpdateType || SYMBOL_COUNT_SUM == TUpdateType);
if (SYMBOL_COUNT_SUM == TUpdateType) {
this->symbol2data[symbol] += increment;
} else {
this->symbol2data[symbol] = MAX(this->symbol2data[symbol], increment) + (this->symbol2data[symbol] ? update_max_inc : 0);
}
return 0;
};
const TInteger
_sumBySymbolType(AlignmentSymbol beg, AlignmentSymbol end) const {
assertUVC (beg <= end);
TInteger alpha_sum = 0;
for (AlignmentSymbol symb = beg; symb <= end; symb = AlignmentSymbol(((uvc1_unsigned_int_t)symb) + 1)) {
alpha_sum += this->symbol2data[symb];
}
return alpha_sum;
};
const TInteger
sumBySymbolType(const SymbolType symboltype) const {
if (symboltype == BASE_SYMBOL) {
return this->_sumBySymbolType(BASE_A, BASE_NN);
} else if (symboltype == LINK_SYMBOL) {
return this->_sumBySymbolType(LINK_M, LINK_NN);
} else {
abort();
return -1;
}
};
template <bool TIsRefCountedOnlyOnce>
int
_fillConsensusCounts(
AlignmentSymbol & count_argmax, uvc1_qual_t & count_max, uvc1_qual_t & count_sum,
AlignmentSymbol incluBeg, AlignmentSymbol incluEnd) const {
assertUVC (incluBeg <= incluEnd);
count_argmax = incluEnd; // assign with the value AlignmentSymbol(NUM_ALIGNMENT_SYMBOLS) to flag for error
count_max = 0;
count_sum = 0;
for (AlignmentSymbol symb = incluBeg; symb <= incluEnd; symb = AlignmentSymbol(((uvc1_unsigned_int_t)symb) + 1)) {
if (TIsRefCountedOnlyOnce) {
if (count_max < this->symbol2data[symb] || (LINK_M == count_argmax && (0 < this->symbol2data[symb]))) {
count_argmax = symb;
count_max = this->symbol2data[symb];
count_sum = count_max;
}
} else {
if (count_max < this->symbol2data[symb]) {
count_argmax = symb;
count_max = this->symbol2data[symb];
}
count_sum += this->symbol2data[symb];
}
}
assertUVC((incluBeg <= count_argmax && count_argmax <= incluEnd) || !fprintf(stderr, "The value %u is not between %u and %u", count_argmax, incluBeg, incluEnd));
return 0;
};
template <bool TIsRefCountedOnceInLink = false, bool TIgnorePaddedDel = false>
int
fillConsensusCounts(
AlignmentSymbol & count_argmax, uvc1_qual_t & count_max, uvc1_qual_t & count_sum,
const SymbolType symboltype) const {
if (symboltype == BASE_SYMBOL) {
return this->template _fillConsensusCounts<false >(count_argmax, count_max, count_sum, BASE_A, (TIgnorePaddedDel ? BASE_T : BASE_NN));
} else if (symboltype == LINK_SYMBOL) {
return this->template _fillConsensusCounts<TIsRefCountedOnceInLink>(count_argmax, count_max, count_sum, LINK_M, LINK_NN);
} else {
abort();
return -1;
}
};
template<bool TIsRefCountedOnceInLink>
AlignmentSymbol
_updateByConsensus(const GenericSymbol2Count<TInteger> & thatSymbol2Count,
const SymbolType symboltype, const AlignmentSymbol ambig_pos, const uvc1_qual_t incvalue) {
AlignmentSymbol argmax_count = END_ALIGNMENT_SYMBOLS; // AlignmentSymbol(0) is not fully correct
uvc1_qual_t max_count = 0;
uvc1_qual_t sum_count = 0;
thatSymbol2Count.template fillConsensusCounts<TIsRefCountedOnceInLink>(argmax_count, max_count, sum_count, symboltype);
if (max_count > 0) {
if ((sum_count - max_count) == 0) {
this->symbol2data[argmax_count] += incvalue;
return argmax_count;
} else {
this->symbol2data[ambig_pos] += incvalue;
return ambig_pos;
}
} else {
return END_ALIGNMENT_SYMBOLS;
}
};
template<bool TIsRefCountedOnceInLink = true>
std::array<AlignmentSymbol, 2>
updateByConsensus(const GenericSymbol2Count<TInteger> & thatSymbol2Count, uvc1_qual_t incvalue = 1) {
AlignmentSymbol baseSymb = this->template _updateByConsensus<false >(thatSymbol2Count, BASE_SYMBOL, BASE_NN, incvalue);
AlignmentSymbol linkSymb = this->template _updateByConsensus<TIsRefCountedOnceInLink>(thatSymbol2Count, LINK_SYMBOL, LINK_NN, incvalue);
return {baseSymb, linkSymb};
};
// For reason of commenting-out this method, please check the identically named method that calls it
/*
template <bool TIsIncVariable = true>
AlignmentSymbol
updateByRepresentative(const GenericSymbol2Count<TInteger> & other, uvc1_qual_t incvalue = 1) {
AlignmentSymbol consalpha;
uvc1_qual_t countalpha, totalalpha;
for (SymbolType symboltype : SYMBOL_TYPE_ARR) {
other.fillConsensusCounts(consalpha, countalpha, totalalpha, symboltype);
if (countalpha > 0) {
this->symbol2data[consalpha] += (TIsIncVariable ? totalalpha : incvalue);
}
}
return consalpha;
};
*/
template <bool TIsRefCountedOnceInLink>
int
updateByFiltering(
std::array<AlignmentSymbol, NUM_SYMBOL_TYPES> & con_symbols,
const GenericSymbol2Count<TInteger> & other,
const bool is_padded_del_ignored,
const std::array<uvc1_qual_t, NUM_SYMBOL_TYPES> thres = {{0}},
const uvc1_qual_t incvalue = 1) {
int ret = 0;
AlignmentSymbol consalpha;
uvc1_qual_t countalpha, totalalpha;
for (SymbolType symboltype : SYMBOL_TYPE_ARR) {
if (LINK_SYMBOL == symboltype) {
other.template fillConsensusCounts<TIsRefCountedOnceInLink>(consalpha, countalpha, totalalpha, symboltype);
} else {
if (is_padded_del_ignored) {
other.template fillConsensusCounts<false, true>(consalpha, countalpha, totalalpha, symboltype);
} else {
other.template fillConsensusCounts<false, false>(consalpha, countalpha, totalalpha, symboltype);
}
}
auto adjcount = MAX(countalpha * 2, totalalpha) - totalalpha;
if (adjcount >= thres[symboltype] && adjcount > 0) {
this->symbol2data[consalpha] += incvalue;
ret++;
}
con_symbols[symboltype] = consalpha;
}
return ret;
};
int
updateByMajorMinusMinor(
std::array<AlignmentSymbol, NUM_SYMBOL_TYPES> & con_symbols,
std::array<uvc1_qual_t, NUM_SYMBOL_TYPES> & con_counts,
const GenericSymbol2Count<TInteger> & other) {
int ret = 0;
AlignmentSymbol consalpha;
uvc1_qual_t countalpha, totalalpha;
for (SymbolType symboltype : SYMBOL_TYPE_ARR) {
if (LINK_SYMBOL == symboltype) {
other.template fillConsensusCounts<true >(consalpha, countalpha, totalalpha, symboltype);
} else {
other.template fillConsensusCounts<false>(consalpha, countalpha, totalalpha, symboltype);
}
auto adjcount = MAX(countalpha * 2, totalalpha) - totalalpha;
if (adjcount > 0) {
this->symbol2data[consalpha] += adjcount;
ret++;
}
con_symbols[symboltype] = consalpha;
con_counts[symboltype] = adjcount;
}
return ret;
};
};
template<class T>
class CoveredRegion {
protected:
std::vector<T> idx2symbol2data;
std::array<std::map<uvc1_refgpos_t, std::map<uvc1_readpos_t , uvc1_readnum_t>>, NUM_INS_SYMBOLS> pos2dlen2data;
std::array<std::map<uvc1_refgpos_t, std::map<std::string, uvc1_readnum_t>>, NUM_DEL_SYMBOLS> pos2iseq2data;
std::array<ConsensusBlockSet, NUM_CONSENSUS_BLOCK_CIGAR_TYPES> conblocksets;
public:
const uvc1_refgpos_t tid;
const uvc1_refgpos_t incluBegPosition; // where end_pos = incluBegPosition + idx2symbol2data.size()
CoveredRegion() {};
CoveredRegion(uvc1_refgpos_t tid, uvc1_refgpos_t beg, uvc1_refgpos_t end): tid(tid), incluBegPosition(beg) {
assertUVC (beg < end || !fprintf(stderr, "assertion %d < %d failed!\n", beg, end));
this->idx2symbol2data = std::vector<T>(end-beg); // end should be real end position plus one
for (const auto conblock_cigartype : ALL_CONSENSUS_BLOCK_CIGAR_TYPES) {
if (is_ConsensusBlockCigarType_right2left(conblock_cigartype)) {
conblocksets[conblock_cigartype].setIsRightToLeft(true);
}
}
};
T &
getRefByPos(const uvc1_refgpos_t pos, const bam1_t *bam = NULL) {
assertUVC(pos >= this->incluBegPosition || !fprintf(stderr, "%d >= %d failed for qname %s !\n", pos, this->incluBegPosition, (NULL != bam ? bam_get_qname(bam) : "?")));
uvc1_refgpos_t pos2 = pos - this->incluBegPosition;
assertUVC(pos2 < UNSIGN2SIGN(idx2symbol2data.size()) || !fprintf(stderr, "%d < %d failed for qname %s !\n", pos, this->incluBegPosition + (uvc1_refgpos_t)(idx2symbol2data.size()), (NULL != bam ? bam_get_qname(bam) : "?")));
return this->idx2symbol2data[pos2];
};
const T &
getByPos(const uvc1_refgpos_t pos, const bam1_t *bam = NULL) const {
assertUVC(pos >= this->incluBegPosition || !fprintf(stderr, "%d >= %d failed for qname %s !\n", pos, this->incluBegPosition, (NULL != bam ? bam_get_qname(bam) : "?")));
uvc1_refgpos_t pos2 = pos - this->incluBegPosition;
assertUVC((pos2 < UNSIGN2SIGN(idx2symbol2data.size())) || !fprintf(stderr, "%d < %d failed for qname %s !\n", pos, this->incluBegPosition + (uvc1_refgpos_t)(idx2symbol2data.size()), (NULL != bam ? bam_get_qname(bam) : "?")));
return this->idx2symbol2data[pos2];
};
uvc1_refgpos_t // size_t
getIncluBegPosition() const {
return this->incluBegPosition;
};
uvc1_refgpos_t // size_t
getExcluEndPosition() const {
return this->incluBegPosition + UNSIGN2SIGN(idx2symbol2data.size());
};
const std::map<uvc1_refgpos_t, std::map<uvc1_refgpos_t , uvc1_readnum_t>> &
getPosToDlenToData(const AlignmentSymbol s) const {
int idx = (LINK_D1 == s ? 0 : ((LINK_D2 == s) ? 1: 2));
return pos2dlen2data[idx];
};
const std::map<uvc1_refgpos_t, std::map<std::string, uvc1_readnum_t>> &
getPosToIseqToData(const AlignmentSymbol s) const {
int idx = (LINK_I1 == s ? 0 : ((LINK_I2 == s) ? 1: 2));
return pos2iseq2data[idx];
};
std::map<uvc1_refgpos_t, std::map<uvc1_readpos_t , uvc1_readnum_t>> &
getRefPosToDlenToData(const AlignmentSymbol s) {
int idx = (LINK_D1 == s ? 0 : ((LINK_D2 == s) ? 1: 2));
return pos2dlen2data[idx];
};
std::map<uvc1_refgpos_t, std::map<std::string, uvc1_refgpos_t>> &
getRefPosToIseqToData(const AlignmentSymbol s) {
int idx = (LINK_I1 == s ? 0 : ((LINK_I2 == s) ? 1: 2));
return pos2iseq2data[idx];
};
const ConsensusBlockSet &
getConsensusBlockSet(const ConsensusBlockCigarType cigar_type) const {
return conblocksets[cigar_type];
};
ConsensusBlockSet &
getRefConsensusBlockSet(const ConsensusBlockCigarType cigar_type) {
return conblocksets[cigar_type];
};
};
template <class TSymbol2Bucket2Count>
class GenericSymbol2Bucket2CountCoverage : public CoveredRegion<TSymbol2Bucket2Count> {
public:
GenericSymbol2Bucket2CountCoverage() : CoveredRegion<TSymbol2Bucket2Count>(0, 0, 1) { }
template <class T1, class T2, class T3>
GenericSymbol2Bucket2CountCoverage(T1 tid, T2 beg, T3 end) : CoveredRegion<TSymbol2Bucket2Count>(tid, beg, end) {}
};
typedef std::array<molcount_t, NUM_BUCKETS> Bucket2Count;
typedef GenericSymbol2Bucket2Count<Bucket2Count> Symbol2Bucket2Count;
typedef GenericSymbol2Count<uvc1_readnum_t> Symbol2Count;
typedef GenericSymbol2Bucket2CountCoverage<Symbol2Bucket2Count> Symbol2Bucket2CountCoverage;
typedef CoveredRegion<SegFormatPrepSet> SegFormatPrepSets;
typedef CoveredRegion<SegFormatThresSet> SegFormatThresSets;
typedef CoveredRegion<std::array<SegFormatInfoSet, NUM_ALIGNMENT_SYMBOLS>> Symbol2SegFormatInfoSets;
typedef CoveredRegion<std::array<FamFormatInfoSet, NUM_ALIGNMENT_SYMBOLS>> Symbol2FamFormatInfoSets;
typedef CoveredRegion<std::array<std::array<molcount_t, NUM_FRAG_FORMAT_DEPTH_SETS>, NUM_ALIGNMENT_SYMBOLS>> Symbol2FragFormatDepthSets;
typedef CoveredRegion<std::array<std::array<molcount_t, NUM_FAM_FORMAT_DEPTH_SETS>, NUM_ALIGNMENT_SYMBOLS>> Symbol2FamFormatDepthSets;
typedef CoveredRegion<std::array<std::array<molcount_t, NUM_DUPLEX_FORMAT_DEPTH_SETS>, NUM_ALIGNMENT_SYMBOLS>> Symbol2DuplexFormatDepthSets;
typedef CoveredRegion<std::array<std::array<molcount_t, NUM_VQ_FORMAT_TAG_SETS>, NUM_ALIGNMENT_SYMBOLS>> Symbol2VQFormatTagSets;
const size_t SIZE_PER_GENOMIC_POS = sizeof(SegFormatPrepSet)
+ sizeof(SegFormatThresSet)
+ ((sizeof(SegFormatInfoSet) + 2 * sizeof(FamFormatInfoSet)) * NUM_ALIGNMENT_SYMBOLS)
+ sizeof(molcount_t) * (NUM_FRAG_FORMAT_DEPTH_SETS + NUM_FAM_FORMAT_DEPTH_SETS * 2 + NUM_DUPLEX_FORMAT_DEPTH_SETS + NUM_VQ_FORMAT_TAG_SETS) * NUM_ALIGNMENT_SYMBOLS
+ NUM_BUCKETS;
template <class T1, class T2, class T3>
int
formatSumBySymbolType(
const T1 & xFormatYSets,
const T2 symboltype,
const T3 formatSet) {
int ret = 0;
for (const auto symbol : SYMBOL_TYPE_TO_SYMBOLS[symboltype]) {
ret += xFormatYSets[symbol][formatSet];
}
return ret;
};
void
initTidBegEnd(uvc1_refgpos_t & tid, uvc1_refgpos_t & inc_beg, uvc1_refgpos_t & exc_end) {
tid = -1;
inc_beg = INT32_MAX;
exc_end = 0;
};
int
fillTidBegEndFromAlns1(uvc1_refgpos_t & tid, uvc1_refgpos_t & inc_beg, uvc1_refgpos_t & exc_end, const std::vector<bam1_t *> & alns1, bool initialized=false) {
assertUVC(alns1.size() > 0);
if (!initialized) {
initTidBegEnd(tid, inc_beg, exc_end);
}
for (const bam1_t *aln : alns1) {
assertUVC (tid == -1 || SIGN2UNSIGN(aln->core.tid) == tid);
tid = aln->core.tid;
inc_beg = MIN(inc_beg, SIGN2UNSIGN(aln->core.pos));
exc_end = MAX(exc_end, SIGN2UNSIGN(bam_endpos(aln))) + 1; // The plus one accounts for possible insertion and/or soft-clip at the end
}
assertUVC (tid != -1);
assertUVC (inc_beg < exc_end);
return 0;
};
int
fillTidBegEndFromAlns2(uvc1_refgpos_t & tid, uvc1_refgpos_t & inc_beg, uvc1_refgpos_t & exc_end, const std::vector<std::vector<bam1_t *>> &alns2, bool initialized=false) {
assertUVC(alns2.size() > 0);
if (!initialized) {
initTidBegEnd(tid, inc_beg, exc_end);
}
for (auto alns1 : alns2) {
fillTidBegEndFromAlns1(tid, inc_beg, exc_end, alns1, true);
}
return 0;
};
int
fillTidBegEndFromAlns3(uvc1_refgpos_t & tid, uvc1_refgpos_t & inc_beg, uvc1_refgpos_t & exc_end, const std::vector<std::vector<std::vector<bam1_t *>>> & alns3, bool initialized=false) {
assertUVC(alns3.size() > 0);
if (!initialized) {
initTidBegEnd(tid, inc_beg, exc_end);
}
for (auto alns2 : alns3) {
fillTidBegEndFromAlns2(tid, inc_beg, exc_end, alns2, true);
}
return 0;
};
template <class T>
bool
is_indel_context_more_STR(uvc1_refgpos_t rulen1, uvc1_refgpos_t rc1, uvc1_refgpos_t rulen2, uvc1_refgpos_t rc2, const T indel_str_repeatsize_max) {
if (rulen2 * rc2 == 0) {
return true;
}
if (rulen1 > indel_str_repeatsize_max || rulen2 > indel_str_repeatsize_max) {
return ((rulen1 < rulen2 || (rulen1 == rulen2 && rc1 > rc2)) ? true : false);
}
int rank1 = (rc1 <= 1 ? (-(int)rc1 * rulen1) : ((int)(rc1 - 1) * rulen1));
int rank2 = (rc2 <= 1 ? (-(int)rc2 * rulen1) : ((int)(rc2 - 1) * rulen2));
if (0 == rc1 || 0 == rulen1) {
rank1 = -100;
}
if (0 == rc2 || 0 == rulen2) {
rank2 = -100;
}
if (rank1 > rank2) {
return true;
} else {
return false;
}
}
template <class T1, class T2, class T3>
uvc1_refgpos_t
indelpos_repeatsize_to_repeatnum(const T1 & refstring, const T2 refpos, const T3 repeatsize) {
uvc1_refgpos_t qidx = refpos;
while ((qidx + repeatsize < UNSIGN2SIGN(refstring.size())) && refstring[qidx] == refstring[qidx+repeatsize]) {
qidx++;
}
return (qidx - refpos) / repeatsize + 1;
}
int
indelpos_to_context(
std::string & repeatunit,
uvc1_refgpos_t & max_repeatnum,
const std::string & refstring,
uvc1_refgpos_t refpos,
uvc1_refgpos_t indel_str_repeatsize_max) {
max_repeatnum = 0;
if (refpos >= UNSIGN2SIGN(refstring.size())) {
repeatunit = "";
return -1;
}
uvc1_refgpos_t repeatsize_at_max_repeatnum = 0;
for (uvc1_refgpos_t repeatsize = 1; repeatsize <= indel_str_repeatsize_max; repeatsize++) {
uvc1_refgpos_t repeatnum = indelpos_repeatsize_to_repeatnum(refstring, refpos, repeatsize);
if (is_indel_context_more_STR(repeatsize, repeatnum, repeatsize_at_max_repeatnum, max_repeatnum, indel_str_repeatsize_max)) {
max_repeatnum = repeatnum;
repeatsize_at_max_repeatnum = repeatsize;
}
}
repeatunit = refstring.substr(refpos, repeatsize_at_max_repeatnum);
return 0;
}
uvc1_qual_t
indel_len_rusize_phred(uvc1_refgpos_t indel_len, uvc1_refgpos_t repeatunit_size) {
assertUVC (indel_len > 0 && repeatunit_size > 0);
// https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2734402/#bib41 suggests power law with exponent of 1.5-1.6
// TODO: investigate this
// derived from the python code: for i in range(1, 20): print("{} {}".format( int( round(10.0/log(10.0)*log(i)) ) , i ))
const std::array<uvc1_qual_t, 18+1> n_units_to_phred = {{
0 ,
0 , // 1
3 , // 2
5 , // 3
6 , // 4
7 , // 5
8 , // 6
8 , // 7
9 , // 8
10 , // 9
10 , // 10
10 , // 11
11 , // 12
11 , // 13
11 , // 14
12 , // 15
12 , // 16
12 , // 17
13 , // 18
}};
if (0 == (indel_len % repeatunit_size)) {
auto n_units = indel_len / repeatunit_size;
return n_units_to_phred[MIN(n_units , UNSIGN2SIGN(n_units_to_phred.size()-1))];
} else {
return n_units_to_phred[MIN(indel_len, UNSIGN2SIGN(n_units_to_phred.size()-1))];
}
}
// https://www.ncbi.nlm.nih.gov/pmc/articles/PMC149199/ for artifactual errors
// https://www.cell.com/trends/genetics/fulltext/S0168-9525(13)00070-X, ref12, for germline variants
uvc1_qual_t
indel_phred(double ampfact, uvc1_refgpos_t repeatsize_at_max_repeatnum, uvc1_refgpos_t max_repeatnum) {
uvc1_refgpos_t region_size = repeatsize_at_max_repeatnum * max_repeatnum;
double num_slips = (region_size > 64 ? (double)(region_size - 8) : log1p(exp((double)region_size - (double)8)))
* ampfact / ((double)(repeatsize_at_max_repeatnum * repeatsize_at_max_repeatnum));
return prob2phred((1.0 - DBL_EPSILON) / (num_slips + 1.0));
// e.g. for the short tandem repeat ACACAC : repeatsize_at_max_repeatnum = 2, indel_n_units = 3, repeatunit = AC
}
std::vector<RegionalTandemRepeat>
refstring2repeatvec(
const std::string & refstring,
const uvc1_refgpos_t indel_str_repeatsize_max,
const uvc1_refgpos_t indel_minisattelite_repeatsize_max,
const uvc1_qual_t indel_BQ_max,
const double indel_polymerase_slip_rate,
const double indel_del_to_ins_err_ratio,
const uvc1_flag_t specialflag IGNORE_UNUSED_PARAM) {
std::vector<RegionalTandemRepeat> region_repeatvec(refstring.size());
for (auto & rtr : region_repeatvec) {
rtr.indelphred = indel_BQ_max;
}
for (uvc1_refgpos_t refpos = 0; refpos < UNSIGN2SIGN(refstring.size());) {
uvc1_refgpos_t repeatsize_at_max_repeatnum = 0;
uvc1_refgpos_t max_repeatnum = 0;
uvc1_refgpos_t repeat_endpos = refpos;
uvc1_refgpos_t anyTR_repeatsize_at_max_repeatnum = 0;
uvc1_refgpos_t anyTR_max_repeatnum = 0;
uvc1_refgpos_t anyTR_repeat_endpos = refpos;
for (uvc1_refgpos_t repeatsize = 1; repeatsize <= indel_minisattelite_repeatsize_max; repeatsize++) {
uvc1_refgpos_t qidx = refpos;
while (qidx + repeatsize < UNSIGN2SIGN(refstring.size()) && refstring[qidx] == refstring[qidx+repeatsize]) {
qidx++;
}
uvc1_refgpos_t repeatnum = (qidx - refpos) / repeatsize + 1;
if (repeatsize <= indel_str_repeatsize_max
&& is_indel_context_more_STR(repeatsize, repeatnum, repeatsize_at_max_repeatnum, max_repeatnum, indel_str_repeatsize_max)) {
repeatsize_at_max_repeatnum = repeatsize;
max_repeatnum = repeatnum;
repeat_endpos = qidx + repeatsize;
}
if (is_indel_context_more_STR(repeatsize, repeatnum, anyTR_repeatsize_at_max_repeatnum, anyTR_max_repeatnum, indel_minisattelite_repeatsize_max)) {
anyTR_repeatsize_at_max_repeatnum = repeatsize;
anyTR_max_repeatnum = repeatnum;
anyTR_repeat_endpos = qidx + repeatsize;
}
}
assertUVC(repeat_endpos > refpos);
{
uvc1_refgpos_t tl = MIN(repeat_endpos, UNSIGN2SIGN(refstring.size())) - refpos;
const uvc1_qual_t decphred = indel_phred(indel_polymerase_slip_rate * (indel_del_to_ins_err_ratio),
repeatsize_at_max_repeatnum, tl / repeatsize_at_max_repeatnum);
for (uvc1_refgpos_t i = refpos; i != MIN(repeat_endpos, UNSIGN2SIGN(refstring.size())); i++) {
if (tl > region_repeatvec[i].tracklen) {
region_repeatvec[i].begpos = refpos;
region_repeatvec[i].tracklen = tl;
region_repeatvec[i].unitlen = repeatsize_at_max_repeatnum;
region_repeatvec[i].indelphred = indel_BQ_max - MIN(indel_BQ_max - 1, decphred);
}
}
}
{
uvc1_refgpos_t anyTR_tl = MIN(anyTR_repeat_endpos, UNSIGN2SIGN(refstring.size())) - refpos;
for (uvc1_refgpos_t i = refpos; i != MIN(anyTR_repeat_endpos, UNSIGN2SIGN(refstring.size())); i++) {
if (anyTR_tl > region_repeatvec[i].anyTR_tracklen) {
region_repeatvec[i].anyTR_begpos = refpos;
region_repeatvec[i].anyTR_tracklen = anyTR_tl;
region_repeatvec[i].anyTR_unitlen = anyTR_repeatsize_at_max_repeatnum;
}
}
}
const auto nbases_to_next = indel_str_repeatsize_max + repeatsize_at_max_repeatnum;
refpos += MAX(repeatsize_at_max_repeatnum * max_repeatnum, nbases_to_next + 1) - (nbases_to_next);
}
region_repeatvec.push_back(LAST(region_repeatvec));
return region_repeatvec;
}
template <class T1, class T2>
uvc1_qual_t
ref_to_phredvalue(uvc1_refgpos_t & n_units,
uvc1_refgpos_t & max_repeatnum,
uvc1_refgpos_t & repeatsize_at_max_repeatnum,
const T1 & refstring,
const uvc1_refgpos_t refpos,
const uvc1_qual_t max_phred,
double ampfact,
const uvc1_refgpos_t cigar_oplen,
const T2 cigar_op,
const uvc1_refgpos_t indel_str_repeatsize_max,
const double indel_del_to_ins_err_ratio,
const uvc1_flag_t specialflag IGNORE_UNUSED_PARAM) {
max_repeatnum = 0;
repeatsize_at_max_repeatnum = 0;
for (uvc1_refgpos_t repeatsize = 1; repeatsize <= indel_str_repeatsize_max; repeatsize++) {
uvc1_refgpos_t qidx = refpos;
while (qidx + repeatsize < UNSIGN2SIGN(refstring.size()) && refstring[qidx] == refstring[qidx+repeatsize]) {
qidx++;
}
uvc1_refgpos_t repeatnum = (qidx - refpos) / repeatsize + 1;
if (is_indel_context_more_STR(repeatsize, repeatnum, repeatsize_at_max_repeatnum, max_repeatnum, indel_str_repeatsize_max)) {
max_repeatnum = repeatnum;
repeatsize_at_max_repeatnum = repeatsize;
}
}
if (cigar_oplen == repeatsize_at_max_repeatnum && cigar_op == BAM_CDEL) {
ampfact *= indel_del_to_ins_err_ratio;
}
// Because of a higher number of PCR cycles, it is higher than the one set in Fig. 3 at https://www.ncbi.nlm.nih.gov/pmc/articles/PMC149199/
uvc1_qual_t decphred = indel_phred(ampfact, repeatsize_at_max_repeatnum, max_repeatnum);
if (0x1 == (specialflag & 0x1)) {
LOG(logINFO) << "indel_phred("
<< ampfact << ", "
<< repeatsize_at_max_repeatnum << ", "
<< max_repeatnum << ") = " << decphred;
}
// The number of units should minimize InDel collision.
if (repeatsize_at_max_repeatnum * (max_repeatnum - 1) >= 6 - 1) {
n_units = ((0 == cigar_oplen % repeatsize_at_max_repeatnum) ? (cigar_oplen / repeatsize_at_max_repeatnum) : ((1 == cigar_oplen) ? 1 : 0));
} else {
n_units = 1 + (cigar_oplen / 6);
}
return max_phred - MIN(max_phred, decphred) + indel_len_rusize_phred(cigar_oplen, repeatsize_at_max_repeatnum);
}
int
update_seg_format_prep_sets_by_aln(
SegFormatPrepSets & seg_format_prep_sets,
const bam1_t *aln,
const std::vector<RegionalTandemRepeat> & rtr_vec,
const CoveredRegion<uvc1_qual_big_t> & baq_offsetarr,
const uvc1_refgpos_t region_offset,
const uvc1_flag_t dflag,
const std::basic_string<AlignmentSymbol> & region_symbolvec,
const CommandLineArgs & paramset,
const uvc1_flag_t specialflag IGNORE_UNUSED_PARAM) {
const uvc1_refgpos_t rend = bam_endpos(aln);
const auto cigar = bam_get_cigar(aln);
uvc1_refgpos_t nge_cnt = 0; // number of gap extensions.
uvc1_refgpos_t ngo_cnt = 0; // number of gap openings.
uvc1_qual_t insbaq_sum = 0;
uvc1_qual_t delbaq_sum = 0;
uvc1_readpos_t inslen_sum = 0;
uvc1_readpos_t dellen_sum = 0;
uvc1_refgpos_t qpos = 0;
uvc1_refgpos_t rpos = aln->core.pos;
uvc1_readpos_t max_clip_len = 0;
uvc1_readpos_t max_indel_len = 0;
for (uint32_t i = 0; i < aln->core.n_cigar; i++) {
const auto c = cigar[i];
const auto cigar_op = bam_cigar_op(c);
const auto cigar_oplen = bam_cigar_oplen(c);
if (BAM_CINS == cigar_op || BAM_CDEL == cigar_op) {
nge_cnt += bam_cigar_oplen(c);
ngo_cnt++;
if (BAM_CINS == cigar_op) {
insbaq_sum += baq_offsetarr.getByPos(MIN(rpos + UNSIGN2SIGN(cigar_oplen), baq_offsetarr.getExcluEndPosition() - 1)) - baq_offsetarr.getByPos(rpos);
inslen_sum += bam_cigar_oplen(c);
qpos += cigar_oplen;
} else if (BAM_CDEL == cigar_op) {
delbaq_sum += baq_offsetarr.getByPos(MIN(rpos + UNSIGN2SIGN(cigar_oplen), baq_offsetarr.getExcluEndPosition() - 1)) - baq_offsetarr.getByPos(rpos);
dellen_sum += bam_cigar_oplen(c);
rpos += cigar_oplen;
}
} else if (cigar_op == BAM_CMATCH || cigar_op == BAM_CEQUAL || cigar_op == BAM_CDIFF) {
UPDATE_MAX(max_indel_len, UNSIGN2SIGN(cigar_oplen));
qpos += cigar_oplen;
rpos += cigar_oplen;
} else {
if (cigar_op == BAM_CSOFT_CLIP || cigar_op == BAM_CHARD_CLIP) {
UPDATE_MAX(max_clip_len, UNSIGN2SIGN(cigar_oplen));
}
process_cigar(qpos, rpos, cigar_op, cigar_oplen);
}
}
const auto *bam_aux_data = bam_aux_get(aln, "NM");
const uvc1_refgpos_t nm_cnt = ((bam_aux_data != NULL) ? bam_aux2i(bam_aux_data) : nge_cnt);
assertUVC (nm_cnt >= nge_cnt);
const uvc1_base1500x_t xm_cnt = nm_cnt - nge_cnt;
const uvc1_base1500x_t xm1500 = xm_cnt * 1500 / (rend - aln->core.pos);
const uvc1_base1500x_t go1500 = ngo_cnt * 1500 / (rend - aln->core.pos);
const uvc1_base1500x_t avg_gaplen = nge_cnt / MAX(1, ngo_cnt);
const uvc1_refgpos_t frag_pos_L = MIN(aln->core.pos, aln->core.mpos);
const uvc1_refgpos_t frag_pos_R = (frag_pos_L + abs(aln->core.isize));
const bool isrc = ((aln->core.flag & 0x10) == 0x10);
// strand does not seem to be useful here?
/*
const bool isr2 = ((aln->core.flag & 0x80) == 0x80 && (aln->core.flag & 0x1) == 0x1);
const bool strand = bam_get_strand(aln); //(isrc ^ isr2);
*/
const auto pcr_dp_inc = ((dflag & 0x4) ? 1 : 0);
const auto umi_dp_inc = ((dflag & 0x1) ? 1 : 0);
qpos = 0;
rpos = aln->core.pos;