-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgo.hpp
1198 lines (1046 loc) · 41.3 KB
/
algo.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
#pragma once
#include <cassert>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <map>
#include <omp.h>
#include <set>
#include <span>
#include <string>
#include <time.h>
#include <vector>
#include <seqan/align.h>
#include <seqan/align_parallel.h>
#include <seqan/arg_parse.h>
#include <seqan/bam_io.h>
#include <seqan/basic.h>
#include <seqan/seq_io.h>
#include <seqan/sequence.h>
#include <seqan/store.h>
#include <seqan/stream.h>
#include <seqan/vcf_io.h>
#include "misc.hpp"
#include "options.hpp"
// Sequence, alignment, and alignment row.
typedef seqan::String<seqan::Dna5> TSequence;
typedef seqan::Align<TSequence, seqan::ArrayGaps> TAlign;
typedef seqan::Row<TAlign>::Type TRow;
using ssize_t = std::make_signed_t<size_t>;
using TSeqInfix = seqan::Segment<TSequence const, seqan::InfixSegment>;
#define LL_THRESHOLD -25.5
#define LG10 3.322
#define NO_ALIGNMENT -10000
inline constexpr size_t NO_BEST = size_t(-1ull);
/* Stores information of how a read aligns across a variant */
class varAlignInfo
{
public:
size_t nD;
size_t nI;
size_t nAlleles;
std::vector<double> alignS;
bool softClipped;
bool alignsLeft;
bool alignsRight;
// int refAlignLen;
// Supports and rejects logic is not correct for very long variants
// Alternate allele is supported as judged from bam alignment record
bool supports(float const refLen, float const altLen, LRCOptions const & O) const
{
if (altLen > refLen)
{ // insertion (these are simplistic for insertion/deletion type variants)
// doesn't work properly if alt and ref are of similar size
return ((alignsLeft && alignsRight && ((float)nI > altLen * O.altThreshFraction) &&
((float)nI < altLen * O.altThreshFractionMax)) ||
softClipped);
}
else
{
return ((alignsLeft && alignsRight && ((float)nD > refLen * O.altThreshFraction) &&
((float)nD < refLen * O.altThreshFractionMax)) ||
softClipped);
}
}
// Alternate allele is rejected as judged from bam alignment record
bool rejects(float const refLen, float const altLen, LRCOptions const & O) const
{
if (altLen > refLen)
{ // insertion
return ((alignsLeft && alignsRight && ((float)nI < altLen * O.refThreshFraction)) && (!softClipped));
}
else
{
return ((alignsLeft && alignsRight && ((float)nD < refLen * O.refThreshFraction)) && (!softClipped));
}
}
bool present(LRCOptions const & O) const
{
return (nI >= O.minPresent || nD >= O.minPresent);
}
bool aligns() const
{
return (alignsLeft && alignsRight);
}
void reset()
{
nD = 0;
nI = 0;
for (size_t i = 0; i < alignS.size(); i++)
alignS[i] = NO_ALIGNMENT;
softClipped = false;
alignsLeft = false;
alignsRight = false;
}
// Likelihood of variant relative to the most likely,
// Updates pref values. A value x, represents that the allele is 2^-x less likely than the most likely allele
// Returns an index to the most likely allele, if one exists
size_t alignmentPreference(size_t const wSizeActual, LRCOptions const & O, std::vector<double> & pref) const
{
int maxScore = alignS[0];
size_t maxI = 0;
int minAlignScore = static_cast<double>(wSizeActual) * 1.2;
for (size_t i = 0; i < nAlleles; i++)
{
// pref[i] = 0;
if (alignS[i] > maxScore)
{
maxI = i;
maxScore = alignS[i];
}
}
if (maxScore == NO_ALIGNMENT || maxScore <= minAlignScore)
{
return NO_BEST;
}
else
{
for (size_t i = 0; i < nAlleles; i++)
{
float d = (maxScore - alignS[i]) / O.logScaleFactor;
if (alignS[i] == NO_ALIGNMENT || alignS[i] <= minAlignScore)
{
d = (maxScore - minAlignScore) / O.logScaleFactor;
}
if (d > O.maxAlignBits)
d = O.maxAlignBits;
if (d < 0 && O.verbose)
std::cerr << "WTF negative alignment score";
pref[i] += d;
}
return maxI;
}
}
// Likelihood of variant relative to the most likely,
// Increments pref values. A value x, represents that the allele is 2^-x less likely than the most likely allele
// Returns an index to the most likely allele, if one exists
size_t vaPreference(LRCOptions const & O,
size_t const refLen,
std::vector<size_t> const & altLens,
std::vector<double> & pref) const
{
if (softClipped)
{ // Softclipped, doesn't support the reference, all other alleles are equally likely
pref[0] += O.overlapBits;
return NO_BEST;
}
if (!alignsLeft || !alignsRight)
{
return NO_BEST;
}
// Count number of deletions && insertions, find the variant that is closest in size
ssize_t insDel = nI - nD;
ssize_t minD = std::abs(insDel);
size_t minDi = 0;
for (size_t i = 1; i < nAlleles; i++)
{
ssize_t cD = altLens[i - 1] - refLen;
if (std::abs(cD - insDel) < minD)
{
minDi = i;
minD = std::abs(cD - insDel);
}
}
for (size_t i = 0; i < nAlleles; i++)
{
if (i != minDi)
pref[i] += O.overlapBits;
}
return minDi;
}
// varAlignInfo(): nD(0),nI(0),
// refS(NO_ALIGNMENT),altS(NO_ALIGNMENT),softClipped(false),alignsLeft(false),alignsRight(false){};
varAlignInfo(size_t const nAllelesIn)
{
nAlleles = nAllelesIn;
nD = 0;
nI = 0;
alignS.resize(nAlleles);
for (size_t i = 0; i < nAlleles; i++)
{
alignS[i] = NO_ALIGNMENT;
}
softClipped = false;
alignsLeft = false;
alignsRight = false;
}
varAlignInfo() : varAlignInfo{2}
{}
};
/* Turns genotyping into std::string */
void getGtString(std::vector<double> & lls,
std::vector<size_t> const & ads,
std::vector<size_t> const & vas,
std::string & gtString)
{
size_t gtLen = lls.size();
for (size_t i = 0; i < gtLen; i++)
lls[i] = -lls[i]; // Really silly hack for historical reasons, would confuse the hell out of me to fix it
// int max = 0;
double maxP = lls[0];
size_t a1 = 0;
size_t a2 = 0;
size_t maxA1 = 0;
size_t maxA2 = 0;
for (size_t i = 0; i < gtLen; i++)
{
if (lls[i] > maxP)
{
maxP = lls[i];
// max = i;
maxA1 = a1;
maxA2 = a2;
}
if (a2 < a1)
{
a2++;
}
else
{
a1++;
a2 = 0;
}
}
std::ostringstream buff;
buff << maxA2 << "/" << maxA1 << ":";
for (size_t i = 0; i < ads.size() - 1; i++)
buff << ads[i] << ",";
buff << ads[ads.size() - 1] << ":";
for (size_t i = 0; i < vas.size() - 1; i++)
buff << vas[i] << ",";
buff << vas[vas.size() - 1] << ":";
for (size_t i = 0; i < gtLen; i++)
{
double lp = (lls[i] - maxP) / LG10;
if (lp < LL_THRESHOLD)
lp = LL_THRESHOLD;
buff << int(-10 * lp);
if (i != gtLen - 1)
buff << ",";
}
gtString = buff.str();
}
// Input: variant and seqan::VarAlignInfo records for each read overlapping variant
// Output: Relative genotype likelihoods in log_2 scale and read info counts
inline void multiUpdateVC(seqan::VcfRecord const & var,
std::vector<varAlignInfo> const & vais,
std::vector<double> & vC,
std::vector<size_t> & rI,
std::vector<size_t> & VAs,
size_t const wSizeActual,
LRCOptions const & O,
genotyping_model const gtm)
{
seqan::StringSet<seqan::CharString> altSet;
strSplit(altSet, var.alt, seqan::EqualsChar<','>());
size_t nAlts = length(altSet); // TODO: = var.nAlts
std::vector<size_t> altLens;
altLens.resize(nAlts);
for (size_t i = 0; i < nAlts; i++)
{
altLens[i] = length(altSet[i]);
}
// Loop over all reads in bam file(s) 1 (deletion biased calls)
for (auto & vai : vais)
{
std::vector<double> prefs;
prefs.resize(nAlts + 1);
// read does not occur in bam file(s) 2 (insertion biased calls)
if (gtm == genotyping_model::ad || gtm == genotyping_model::joint)
{
size_t bestI = vai.alignmentPreference(wSizeActual, O, prefs);
if (bestI != NO_BEST)
rI[bestI]++;
rI[rI.size() - 1]++;
}
if (gtm == genotyping_model::va || gtm == genotyping_model::joint)
{
size_t bestI = vai.vaPreference(O, length(var.ref), altLens, prefs);
if (bestI != NO_BEST)
VAs[bestI]++;
VAs[VAs.size() - 1]++;
if (O.verbose)
std::cerr << "va " << /*TODO vai.first <<*/ " " << vai.nD << " " << vai.nI << " " << prefs[0] << " "
<< prefs[1] << " " << bestI << '\n';
}
if (gtm == genotyping_model::va_old)
{
size_t bestI = 0;
double bestScore = 0; // std::numeric_limits< float>::max();
for (size_t iP = 0; iP < nAlts; iP++)
{
double cScore =
O.overlapBits * (-vai.supports(length(var.ref), altLens[iP], O) * 1.0 +
vai.rejects(length(var.ref), altLens[iP], O) * 1.0); // ACHTUNG: needs fixing
prefs[iP + 1] += cScore;
if (cScore < bestScore)
{
bestScore = cScore;
bestI = iP + 1;
}
}
VAs[bestI]++;
VAs[VAs.size() - 1]++;
if (O.verbose)
std::cerr << "va_old " << /*TODO vai.first <<*/ " " << vai.nD << " " << vai.nI << " " << prefs[0] << " "
<< prefs[1] << " " << bestI << '\n';
}
if (gtm == genotyping_model::presence)
{
if (vai.present(O))
{
prefs[0] += O.overlapBits;
}
else
{
prefs[1] += O.overlapBits;
}
for (size_t iP = 2; iP <= nAlts; iP++)
{
prefs[iP] += O.overlapBits;
}
}
// if( O.verbose ) std::cerr << "Update VC bam1 " << vai.first << " " << prefs[0] << " " << prefs[1] << " "
// <<
// vai.supports( (int) length( var.ref ), (int) length( altSet[0] ), O ) << " " <<
// vai.rejects( (int) length( var.ref), (int) length( altSet[0]), O ) << '\n';
float minPref = std::numeric_limits<float>::max();
float maxPref = std::numeric_limits<float>::lowest();
for (size_t iP = 0; iP < nAlts + 1; iP++)
{
if (prefs[iP] < minPref)
minPref = prefs[iP];
if (prefs[iP] > maxPref)
maxPref = prefs[iP];
}
for (size_t iP = 0; iP < nAlts + 1; iP++)
prefs[iP] -= minPref;
#define MINIMUM_PREF_DIFF 2.0
if (maxPref - minPref > MINIMUM_PREF_DIFF)
{
size_t vCI = 0;
for (size_t a1 = 0; a1 < nAlts + 1; a1++)
{
for (size_t a2 = 0; a2 <= a1; a2++)
{
if (a1 != a2)
{
if (prefs[a1] == prefs[a2])
{
vC[vCI] += prefs[a1];
}
else if (prefs[a1] > prefs[a2] + 2)
{
vC[vCI] += prefs[a2] + 1;
}
else if (prefs[a2] > prefs[a1] + 2)
{
vC[vCI] += prefs[a1] + 1;
}
else if (prefs[a1] > prefs[a2])
{
vC[vCI] += (prefs[a1] + prefs[a2]) / 2.0;
}
}
else
{
vC[vCI] += prefs[a1];
}
vCI++;
}
}
}
}
if (O.verbose)
std::cerr << "multiUpdateVC " << vC[0] << " " << vC[1] << " " << vC[2] << '\n';
}
// Input: seqan::VcfRecord, reference and alternate reference
// Output: Chrom and position of variant, ref sequence and alt sequence
// TODO: Change this for a library that does this
inline void getLocRefAlt(seqan::VcfRecord const & variant,
seqan::FaiIndex const & faiI,
seqan::CharString const & chrom,
TSequence & refSeq,
std::vector<TSequence> & altSeqs,
size_t const wSizeActual,
LRCOptions const & O)
{
TSequence ref = variant.ref;
seqan::StringSet<seqan::CharString> altSet;
strSplit(altSet, variant.alt, seqan::EqualsChar<','>());
size_t nAlts = length(altSet);
int32_t beginPos = variant.beginPos;
unsigned idx = 0;
if (!getIdByName(idx, faiI, chrom))
{
if (O.verbose)
std::cerr << "rID " << chrom << " " << beginPos
<< " WARNING: reference FAI index has no entry for rID in Ref std::mapped.\n";
}
if (O.genotypeRightBreakpoint)
readRegion(refSeq, faiI, idx, beginPos - wSizeActual + length(ref), beginPos + length(ref) + wSizeActual);
else
readRegion(refSeq, faiI, idx, beginPos - wSizeActual, beginPos + wSizeActual);
if (O.verbose)
std::cerr << "refSeq " << refSeq << " " << chrom << " " << beginPos << std::endl;
std::vector<size_t> altLens;
altLens.resize(nAlts);
for (size_t i = 0; i < nAlts; i++)
altLens[i] = length(altSet[i]);
size_t refLen = length(variant.ref);
for (size_t i = 0; i < nAlts; i++)
{
if (!O.genotypeRightBreakpoint)
{
readRegion(altSeqs[i],
faiI,
idx,
beginPos - wSizeActual,
beginPos); // beginPos is included in the alt sequence
TSequence post;
if (altLens[i] < (size_t)wSizeActual)
{
post = altSet[i];
TSequence post2;
readRegion(post2, faiI, idx, beginPos + refLen, beginPos + refLen + wSizeActual - altLens[i]);
append(post, post2);
}
else
{
post = infixWithLength(altSet[i], 0, wSizeActual);
}
append(altSeqs[i], post);
}
else
{
if (altLens[i] < (size_t)wSizeActual)
{
readRegion(altSeqs[i], faiI, idx, beginPos - wSizeActual + altLens[i], beginPos);
append(altSeqs[i], altSet[i]);
}
else
{
altSeqs[i] = infixWithLength(altSet[i], altLens[i] - wSizeActual, wSizeActual);
}
TSequence post;
readRegion(post, faiI, idx, beginPos + refLen, beginPos + refLen + wSizeActual);
append(altSeqs[i], post);
}
}
if (O.verbose)
{
std::cerr << "Printing altSeq " << '\n';
for (size_t i = 0; i < nAlts; i++)
std::cerr << "altSeq " << i << " " << altSeqs[i] << '\n';
std::cerr << "Done printing altSeq " << '\n';
}
}
// Crops a subsequence in a seqan::BamAlignmentRecord and rewrites into the seqan::BamAlignmentRecord
inline void cropSeq(seqan::BamAlignmentRecord const & bar,
seqan::VcfRecord const & var,
ssize_t const wSizeActual, // <- this is signed here!
LRCOptions const & O,
TSequence & croppedSeq)
{
auto & cigarString = bar.cigar;
ssize_t alignPos = bar.beginPos;
ssize_t readPos = 0;
ssize_t lReadPos = 0;
size_t cigarI = 0;
char cigarOperation = cigarString[cigarI].operation;
// Searches for the first position overlapping our window (right insert) or last position overlapping window (left
// insert)
// TODO the following can underflow :o
ssize_t searchPos = var.beginPos - wSizeActual; // Want to change the search intervals for TRs
if (O.genotypeRightBreakpoint)
searchPos = var.beginPos + length(var.ref) + wSizeActual;
searchPos = std::max<ssize_t>(searchPos, 0);
while (alignPos < searchPos && cigarI < length(cigarString))
{
// lAlignPos = alignPos;
lReadPos = readPos;
cigarOperation = cigarString[cigarI].operation;
switch (cigarOperation)
{
case 'D':
alignPos += cigarString[cigarI].count;
break;
case '=':
case 'X':
case 'M':
alignPos += cigarString[cigarI].count;
[[fallthrough]];
case 'S':
case 'I':
readPos += cigarString[cigarI].count;
break;
case 'H': // this is untested
break;
default:
std::cerr << "WARNING: cigar string case not accounted for \n";
}
if (O.verbose)
std::cerr << bar.qName << " readpos " << alignPos << " " << var.beginPos << " " << searchPos << " "
<< cigarI << " " << readPos << " " << cigarString[cigarI].count << " " << cigarOperation << '\n';
cigarI++;
}
if (alignPos < searchPos && O.verbose)
{
std::cerr << "Read clipped " << alignPos << " " << var.beginPos << " " << searchPos << " " << cigarI << " "
<< length(cigarString) << '\n';
}
if (cigarOperation == 'S' || cigarOperation == 'H')
readPos = lReadPos;
ssize_t rBeg = 0;
ssize_t rEnd = 0;
if (O.genotypeRightBreakpoint)
{
// if( alignPos >= searchPos ){
// rBeg = readPos - 2*wSizeActual;
// rEnd = readPos;
//}else
if (alignPos >= searchPos - 2 * wSizeActual)
{
ssize_t rShift = searchPos - alignPos;
rBeg = readPos - 2 * wSizeActual + rShift;
rEnd = readPos + rShift;
}
else
{
rBeg = readPos;
rEnd = readPos + wSizeActual;
if (O.verbose)
std::cerr << "Insensible case for read " << bar.qName << " " << alignPos << " " << var.beginPos << " "
<< searchPos << " " << cigarI << " " << length(cigarString) << '\n';
}
}
else
{
ssize_t rShift = alignPos - searchPos;
rBeg = readPos - rShift;
rEnd = readPos + 2 * wSizeActual - rShift;
if (rShift < 0 && O.verbose)
std::cerr << "Poorly formatted read, case not accounted for " << bar.qName << " " << alignPos << " "
<< var.beginPos << " " << searchPos << " " << cigarI << " " << length(cigarString) << '\n';
}
if (O.verbose)
std::cerr << "Cropped read " << bar.qName << " " << alignPos << " " << var.beginPos << " " << searchPos << " "
<< cigarI << " " << length(cigarString) << " " << rBeg << " " << rEnd << '\n';
if (rBeg < 0)
rBeg = 0;
if (rEnd < 2 * wSizeActual)
rEnd = 2 * wSizeActual;
if (rEnd > (ssize_t)length(bar.seq))
rEnd = (ssize_t)length(bar.seq);
if (O.verbose)
std::cerr << "ToInfix " << rBeg << " " << rEnd << " " << length(bar.seq) << '\n';
croppedSeq = infixWithLength(bar.seq, rBeg, rEnd - rBeg);
if (O.verbose)
std::cerr << "Successful crop " << bar.qName << " " << croppedSeq << '\n';
}
template <typename TSeq>
inline TSeq mask(TSeq const & in)
{
TSeq ret;
seqan::appendValue(ret, in[0]);
for (size_t i = 1; i < seqan::length(in); ++i)
if (in[i] != in[i - 1])
seqan::appendValue(ret, in[i]);
return ret;
}
/** Input: bamStream, a VCF entry, reference fasta and alt fasta file if required by VCF entry
Output: variant alignment info for each read near the VCF entry
*/
inline void LRprocessReads(seqan::VcfRecord const & variant,
seqan::CharString const & chrom,
seqan::FaiIndex const & faiI,
std::vector<seqan::BamAlignmentRecord const *> const & overlappingBars,
std::vector<varAlignInfo> & vais,
size_t const wSizeActual,
LRCOptions const & O)
{
TSequence refSeq;
std::vector<TSequence> altSeqs;
seqan::StringSet<seqan::CharString> altSet;
strSplit(altSet, variant.alt, seqan::EqualsChar<','>());
size_t const nAlts = length(altSet);
altSeqs.resize(nAlts);
if (O.verbose)
std::cerr << "nAlts " << nAlts << '\n';
// move outside of this function
getLocRefAlt(variant, faiI, chrom, refSeq, altSeqs, wSizeActual, O);
if (O.outputRefAlt)
{
std::cerr << chrom << " " << /*beginPos +*/ 1 << " " << variant.info << " " << refSeq;
for (size_t i = 0; i < nAlts; i++)
std::cerr << " " << altSeqs[i];
std::cerr << '\n';
return;
}
if (O.mask)
refSeq = mask(refSeq);
seqan::Score<int32_t, seqan::Simple> scoringScheme32(O.match, O.mismatch, O.gapExtend, O.gapOpen);
seqan::Score<int16_t, seqan::Simple> scoringScheme16(O.match, O.mismatch, O.gapExtend, O.gapOpen);
double const band_fac = std::min<double>(O.bandedAlignmentPercent, 100.0) / 100.0;
// The set of alleles is the same for all alignments
std::vector<TSeqInfix> seqsV;
seqsV.resize(nAlts + 1);
seqsV[0] = infix(refSeq, 0, seqan::length(refSeq));
for (size_t j = 0; j < nAlts; ++j)
seqsV[j + 1] = infix(altSeqs[j], 0, seqan::length(altSeqs[j]));
int32_t const vBand = static_cast<double>(seqan::length(refSeq)) * band_fac;
// This is a set that contains the respective read at every position [needs to be same size as other set]
std::vector<TSeqInfix> seqsH;
seqsH.resize(nAlts + 1);
TSequence seqToAlign;
seqan::ExecutionPolicy<seqan::Serial, seqan::Vectorial> execP;
for (size_t i = 0; i < overlappingBars.size(); ++i)
{
seqan::BamAlignmentRecord const & b = *overlappingBars[i];
varAlignInfo & vai = vais[i];
seqan::clear(seqToAlign);
if (O.cropRead)
cropSeq(b, variant, wSizeActual, O, seqToAlign);
else
seqToAlign = b.seq; // converts IUPAC to Dna5
for (auto & seqH : seqsH)
seqH = infix(seqToAlign, 0, seqan::length(seqToAlign));
int32_t const hBand = static_cast<double>(seqan::length(seqToAlign)) * band_fac;
if (seqan::length(refSeq) > std::numeric_limits<int16_t>::max() &&
seqan::length(seqToAlign) > std::numeric_limits<int16_t>::max())
{
auto scores = seqan::localAlignmentScore(execP, seqsH, seqsV, scoringScheme32, -vBand, +hBand);
for (size_t j = 0; j < seqan::length(vai.alignS); ++j)
vai.alignS[j] = scores[j];
}
else // this allows better vectorisation
{
auto scores = seqan::localAlignmentScore(execP, seqsH, seqsV, scoringScheme16, -vBand, +hBand);
for (size_t j = 0; j < seqan::length(vai.alignS); ++j)
vai.alignS[j] = scores[j];
}
}
}
inline void initializeBam(std::string fileName, seqan::BamIndex<seqan::Bai> & bamIndex, seqan::BamFileIn & bamStream)
{
if (!seqan::open(bamStream, fileName.data()))
throw error{"Could not open ", fileName, " for reading."};
fileName += ".bai";
if (!seqan::open(bamIndex, fileName.data()))
throw error{"Could not read BAI index file ", fileName};
seqan::BamHeader header;
readHeader(header, bamStream);
}
// Open a bam file or a set of bam files if the filename does not end with .bam
inline void parseBamFileName(std::filesystem::path const & bfN,
std::vector<seqan::BamFileIn> & bamStreamV,
std::vector<seqan::BamIndex<seqan::Bai>> & bamIndexV,
LRCOptions const & O)
{
std::vector<std::filesystem::path> paths;
if (bfN.native().ends_with(".bam") || bfN.native().ends_with(".sam.gz"))
{
paths.push_back(bfN);
}
else
{
std::ifstream fS;
fS.open(bfN);
std::string bf;
while (fS >> bf)
paths.push_back(bf);
}
if (O.verbose)
std::cerr << "Checking input files" << (O.cacheDataInTmp ? " and copying to cache dir..." : "...");
for (std::filesystem::path & p : paths)
{
if (!p.native().ends_with(".bam") && !p.native().ends_with(".sam.gz"))
throw error{"Input file '", p, "' has unrecognized extension."};
if (!std::filesystem::exists(p))
throw error{"Input file '", p, "' does not exist."};
std::filesystem::path p_bai = p;
p_bai += ".bai";
if (!std::filesystem::exists(p_bai))
throw error{"Input file '", p, "' has no corresponding '.bai' index."};
if (O.cacheDataInTmp) // copy to tmp
{
std::filesystem::path new_p = O.cacheDir / p.filename();
std::filesystem::path new_p_bai = O.cacheDir / p_bai.filename();
if (std::filesystem::exists(new_p) || std::filesystem::exists(new_p_bai))
throw error{"Cache file already exists. Does a filename appear twice in input?"};
std::filesystem::copy(p, new_p);
std::filesystem::copy(p_bai, new_p_bai);
p = new_p; // update path in-place
}
}
if (O.verbose)
std::cerr << " done.";
bamIndexV.resize(paths.size());
bamStreamV.resize(paths.size());
for (size_t i = 0; i < paths.size(); ++i)
initializeBam(paths[i], bamIndexV[i], bamStreamV[i]);
}
// Examines a seqan::BamAlignmentRecord for evidence of supporting a variant and writes evidence into varAlignInfo
// record
inline void examineBamAlignment(seqan::BamAlignmentRecord const & bar,
seqan::VcfRecord const & var,
varAlignInfo & vai,
LRCOptions const & O)
{
vai.reset();
auto & cigarString = bar.cigar;
int32_t alignPos = bar.beginPos;
size_t cigarI = 0;
char cigarOperation = cigarString[cigarI].operation;
int32_t regionBeg = var.beginPos - O.varWindow;
int32_t regionEnd = var.beginPos + (int32_t)length(var.ref) + O.varWindow;
seqan::StringSet<seqan::CharString> infos;
strSplit(infos, var.info, seqan::EqualsChar<';'>());
for (size_t i = 0; i < length(infos); i++)
{
seqan::StringSet<seqan::CharString> info2;
strSplit(info2, infos[i], seqan::EqualsChar<'='>());
int32_t cVal;
if (info2[0] == "TRRBEGIN")
{
if (info2[1] != ".")
{
cVal = std::atoi(toCString(info2[1])) - O.varWindow;
if (cVal < regionBeg)
regionBeg = cVal;
}
}
if (info2[0] == "TRREND")
{
if (info2[1] != ".")
{
cVal = std::atoi(toCString(info2[1])) + O.varWindow;
if (cVal > regionEnd)
regionEnd = cVal;
}
}
if (info2[0] == "REGBEGIN")
{
if (info2[1] != ".")
{
cVal = std::atoi(toCString(info2[1])) - O.varWindow;
if (cVal < regionBeg)
regionBeg = cVal;
}
}
if (info2[0] == "REGEND")
{
if (info2[1] != ".")
{
cVal = std::atoi(toCString(info2[1])) + O.varWindow;
if (cVal > regionEnd)
regionEnd = cVal;
}
}
}
/* if( infos.count( "TRRBEGIN" ) == 1 and infos.count( "TRREND" ) == 1 ){
regionBeg = std::atoi(var.infos["TRRBEGIN"] ) - O.varWindow;
seqan::CharString endStr = getValueById( var.genotypeInfos, trrEndID);
regionEnd = std::atoi( var.infos["TRREND"] ) + O.varWindow;
if( O.verbose ) std::cerr << "Found TRR " << regionBeg << " " << regionEnd << '\n';
}else{*/
if (O.verbose)
std::cerr << "TRR " << regionBeg << " " << regionEnd << '\n';
// }
if (alignPos < regionBeg)
vai.alignsLeft = true;
// Find the first position that overlaps the window we are interested in
while (alignPos < regionBeg && cigarI < length(cigarString))
{
cigarOperation = cigarString[cigarI].operation;
if (cigarOperation == 'M' || cigarOperation == 'D')
{
alignPos += cigarString[cigarI].count;
}
cigarI++;
}
// only counts the number of deleted bp in the window, probably fine to count a longer distance
if (alignPos > regionBeg && cigarOperation == 'D' && (alignPos - (regionBeg) >= (int)O.minDelIns))
{
vai.nD = alignPos - regionBeg;
}
while (alignPos < regionEnd && cigarI < length(cigarString))
{
cigarOperation = cigarString[cigarI].operation;
switch (cigarOperation)
{
case 'D':
if (cigarString[cigarI].count >= O.minDelIns)
vai.nD += cigarString[cigarI].count;
[[fallthrough]];
case '=':
case 'X':
case 'M':
alignPos += cigarString[cigarI].count;
break;
case 'I':
if (cigarString[cigarI].count >= O.minDelIns)
vai.nI += cigarString[cigarI].count;
break;
case 'S':
if (cigarString[cigarI].count > O.maxSoftClipped)
{
if (!O.genotypeRightBreakpoint)
{
if (cigarI == length(cigarString) - 1)
vai.softClipped = true;
}
else
{
if (cigarI == 0)
vai.softClipped = true;
}
}
break;
case 'H': // this is untested
break;
default:
std::cerr << "WARNING: cigar string case not accounted for \n";
}
if (O.verbose)
std::cerr << bar.qName << " " << cigarOperation << " " << cigarString[cigarI].count << " " << alignPos
<< " " << cigarI << " " << vai.nD << " " << vai.nI << '\n';
cigarI++;
}
if (alignPos > regionEnd)
vai.alignsRight = true;
if (O.verbose)
std::cerr << "examinSeq " << bar.qName << " " << vai.nD << " " << vai.nI << " " << vai.softClipped << '\n';
}
// Gets reads in the region overlapping the variant
inline void parseReads(std::vector<seqan::BamAlignmentRecord> const & bars,
seqan::VcfRecord const & var,
std::vector<seqan::BamAlignmentRecord const *> & overlappingBars,
std::vector<varAlignInfo> & align_infos,
size_t const wSizeActual,
LRCOptions const & O)
{
int32_t beg = var.beginPos - wSizeActual;
int32_t end = var.beginPos + wSizeActual;
if (O.genotypeRightBreakpoint)
{
beg = beg + length(var.ref);
end = end + length(var.ref);
}
seqan::StringSet<seqan::CharString> altSet;
strSplit(altSet, var.alt, seqan::EqualsChar<','>());
size_t nAlts = length(altSet);
varAlignInfo vai(nAlts + 1);
int32_t stopReading = beg;
if (O.genotypeRightBreakpoint)
stopReading = end;
// TODO evaluate unordered_map here
std::map<std::string_view, size_t> nameCache;
for (seqan::BamAlignmentRecord const & record : bars)
{
if (overlappingBars.size() >= O.maxBARcount || record.beginPos > stopReading) // || record.rID != rID)
return;
// Ignore the read if it does not stretch to the region we are interested in
if ((record.beginPos + (int32_t)length(record.seq) < beg) ||
(record.beginPos + (int32_t)getAlignmentLengthInRef(record) < beg) || record.mapQ < O.minMapQ)
continue;
examineBamAlignment(record, var, vai, O);
if (O.verbose)
std::cerr << "Read record " << record.qName << '\n';
// If we are on the next reference or at the end already then we stop.
if (/*record.rID == -1 || record.rID > rID || */ record.beginPos >= end)
break;