-
Notifications
You must be signed in to change notification settings - Fork 119
/
aligner_result.h
2318 lines (2086 loc) · 64.8 KB
/
aligner_result.h
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
/*
* Copyright 2011, Ben Langmead <[email protected]>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALIGNER_RESULT_H_
#define ALIGNER_RESULT_H_
#include <utility>
#include <limits>
#include "mem_ids.h"
#include "ref_coord.h"
#include "read.h"
#include "filebuf.h"
#include "ds.h"
#include "edit.h"
#include "limit.h"
#include "splice_site.h"
typedef int64_t TAlScore;
#define VALID_AL_SCORE(x) ((x).score_ > MIN_I64)
#define VALID_SCORE(x) ((x) > MIN_I64)
#define INVALIDATE_SCORE(x) ((x) = MIN_I64)
/**
* A generic score object for an alignment. Used for accounting during
* SW and elsewhere. Encapsulates the score, the number of N positions
* and the number gaps in the alignment.
*
* The scale for 'score' is such that a perfect alignment score is 0
* and a score with non-zero penalty is less than 0. So differences
* between scores work as expected, but interpreting an individual
* score (larger is better) as a penalty (smaller is better) requires
* taking the absolute value.
*/
class AlnScore {
public:
/**
* Gapped scores are invalid until proven valid.
*/
inline AlnScore() {
reset();
invalidate();
assert(!valid());
}
/**
* Gapped scores are invalid until proven valid.
*/
inline AlnScore(
TAlScore score,
TAlScore ns,
TAlScore gaps,
bool repeat = false,
TAlScore splicescore = 0,
bool knownTranscripts = false,
bool nearSpliceSites = false,
int leftTrim = 0,
int rightTrim = 0) {
score_ = score;
ns_ = ns;
gaps_ = gaps;
repeat_ = repeat;
splicescore_ = splicescore;
knownTranscripts_ = knownTranscripts;
nearSpliceSites_ = nearSpliceSites;
leftTrim_ = leftTrim;
rightTrim_ = rightTrim;
hisat2_score_ = calculate_hisat2_score();
assert(valid());
}
/**
* Reset the score.
*/
void reset() {
score_ = hisat2_score_ = ns_ = gaps_ = 0;
repeat_ = false;
splicescore_ = 0;
knownTranscripts_ = false;
nearSpliceSites_ = false;
leftTrim_ = 0;
rightTrim_ = 0;
}
/**
* Return an invalid SwScore.
*/
inline static AlnScore INVALID() {
AlnScore s;
s.invalidate();
assert(!s.valid());
return s;
}
/**
* Return true iff this score has a valid value.
*/
inline bool valid() const {
return score_ != MIN_I64;
}
/**
* Make this score invalid (and therefore <= all other scores).
*/
inline void invalidate() {
score_ = MIN_I64;
assert(!valid());
}
/**
* Increment the number of gaps. If currently invalid, this makes
* the score valid with gaps == 1.
*/
inline void incNs(int nceil) {
if(++ns_ > nceil) {
invalidate();
}
assert_lt(ns_, 0x7fffffff);
}
/**
* Return true iff this score is > score o.
* Note: An "invalid" score is <= all other scores.
*/
inline bool operator>(const AlnScore& o) const {
if(!VALID_AL_SCORE(o)) {
if(!VALID_AL_SCORE(*this)) {
// both invalid
return false;
} else {
// I'm valid, other is invalid
return true;
}
} else if(!VALID_AL_SCORE(*this)) {
// I'm invalid, other is valid
return false;
}
return score_ > o.score_ || (score_ == o.score_ && hisat2_score_ > o.hisat2_score_);
}
/**
* Scores are equal iff they're bitwise equal.
*/
inline AlnScore& operator=(const AlnScore& o) {
// Profiling shows many cache misses on following lines
gaps_ = o.gaps_;
ns_ = o.ns_;
score_ = o.score_;
repeat_ = o.repeat_;
hisat2_score_ = o.hisat2_score_;
splicescore_ = o.splicescore_;
knownTranscripts_ = o.knownTranscripts_;
nearSpliceSites_ = o.nearSpliceSites_;
leftTrim_ = o.leftTrim_;
rightTrim_ = o.rightTrim_;
assert_lt(ns_, 0x7fffffff);
return *this;
}
/**
* Scores are equal iff they're bitwise equal.
*/
inline bool operator==(const AlnScore& o) const {
// Profiling shows cache misses on following line
return VALID_AL_SCORE(*this) && VALID_AL_SCORE(o) && score_ == o.score_ && hisat2_score_ == o.hisat2_score_;
}
/**
* Return true iff the two scores are unequal.
*/
inline bool operator!=(const AlnScore& o) const {
return !(*this == o);
}
/**
* Return true iff this score is >= score o.
*/
inline bool operator>=(const AlnScore& o) const {
if(!VALID_AL_SCORE(o)) {
if(!VALID_AL_SCORE(*this)) {
// both invalid
return false;
} else {
// I'm valid, other is invalid
return true;
}
} else if(!VALID_AL_SCORE(*this)) {
// I'm invalid, other is valid
return false;
}
return score_ > o.score_ || (score_ == o.score_ && hisat2_score_ >= o.hisat2_score_);
}
/**
* Return true iff this score is < score o.
*/
inline bool operator<(const AlnScore& o) const {
return !operator>=(o);
}
/**
* Return true iff this score is <= score o.
*/
inline bool operator<=(const AlnScore& o) const {
return !operator>(o);
}
/**
* Calculate difference between two SwScores.
*/
inline AlnScore operator-(const AlnScore& o) const {
if(!VALID_AL_SCORE(*this)) return *this;
AlnScore s;
s.gaps_ = gaps_ - o.gaps_;
s.ns_ = ns_;
s.score_ = score_ - o.score_;
s.splicescore_ = splicescore_ - o.splicescore_;
assert_lt(s.ns_, 0x7fffffff);
return s;
}
/**
* Calculate sum of two SwScores.
*/
inline AlnScore operator+(const AlnScore& o) const {
if(!VALID_AL_SCORE(*this)) return *this;
AlnScore s;
s.gaps_ = gaps_ + o.gaps_;
s.ns_ = ns_;
s.score_ = score_ + o.score_;
s.repeat_ = repeat_ | o.repeat_;
s.splicescore_ = splicescore_ + o.splicescore_;
s.hisat2_score_ = hisat2_score_ + o.hisat2_score_;
s.knownTranscripts_ = knownTranscripts_ | o.knownTranscripts_;
s.nearSpliceSites_ = nearSpliceSites_ | o.nearSpliceSites_;
s.leftTrim_ = leftTrim_ + o.leftTrim_;
s.rightTrim_ = rightTrim_ + o.rightTrim_;
assert_lt(s.ns_, 0x7fffffff);
return s;
}
/**
* Add given SwScore into this one.
*/
inline AlnScore operator+=(const AlnScore& o) {
if(VALID_AL_SCORE(*this)) {
gaps_ += o.gaps_;
score_ += o.score_;
repeat_ |= o.repeat_;
splicescore_ += o.splicescore_;
hisat2_score_ += o.hisat2_score_;
knownTranscripts_ |= o.knownTranscripts_;
nearSpliceSites_ |= o.nearSpliceSites_;
leftTrim_ += o.leftTrim_;
rightTrim_ += o.rightTrim_;
}
return (*this);
}
/**
* Subtract given SwScore from this one.
*/
inline AlnScore operator-=(const AlnScore& o) {
if(VALID_AL_SCORE(*this)) {
gaps_ -= o.gaps_;
score_ -= o.score_;
// splicescore_ -= o.splicescore_;
}
return (*this);
}
/**
* Calculate difference between two SwScores.
*/
inline AlnScore operator-(int o) const {
return (*this) + -o;
}
/**
* Calculate sum of a SwScore and an integer.
*/
inline AlnScore operator+(int o) const {
if(!VALID_AL_SCORE(*this)) return *this;
AlnScore s;
s.gaps_ = gaps_;
s.ns_ = ns_;
s.score_ = score_ + o;
// s.splicescore_ = splicescore_;
assert_lt(s.ns_, 0x7fffffff);
return s;
}
TAlScore score() const { return score_; }
TAlScore hisat2_score() const { return hisat2_score_; }
TAlScore penalty() const { return -score_; }
TAlScore gaps() const { return gaps_; }
TAlScore ns() const { return ns_; }
bool repeat() const { return repeat_;}
TAlScore splicescore() const { return splicescore_; }
bool knownTranscripts() const { return knownTranscripts_; }
bool nearSpliceSites() const { return nearSpliceSites_; }
bool trimed() const { return leftTrim_ > 0 || rightTrim_ > 0; }
TAlScore calculate_hisat2_score() const
{
// TAlScore 32 bits used for score_
TAlScore score = score_;
if(score > MAX_I32) score = MAX_I32;
else if(score < MIN_I32) score = MIN_I32;
// Next 4 bits for repeat score
TAlScore repeat_score = 0;
if(repeat_) repeat_score = 1;
// Next 4 bits for alignments against transcripts
TAlScore transcript_score = 0;
if(knownTranscripts_) transcript_score = 2;
else if(nearSpliceSites_) transcript_score = 1;
// Next 8 bits for splice site score
TAlScore splicescore = splicescore_ / 100;
if(splicescore > MAX_U8) splicescore = 0;
else splicescore = MAX_U8 - splicescore;
// Remaining 16 bits (rightmost 16 bits) for sum of left and right trim lengths
TAlScore trim = leftTrim_ + rightTrim_;
if(trim > MAX_U16) trim = 0;
else trim = MAX_U16 - trim;
return (score << 32) | (repeat_score << 28) | (transcript_score << 24) | (splicescore << 16) | trim;
}
// Score accumulated so far (penalties are subtracted starting at 0)
TAlScore score_;
// HISAT2 score, which is used internally to distinguish the alignments of RNA-seq reads
TAlScore hisat2_score_;
// Ns accumulated so far. An N opposite a non-gap counts as 1 N
// (even if it's N-to-N)
TAlScore ns_;
// # gaps encountered so far, unless that number exceeds the
// target, in which case the score becomes invalid and therefore <=
// all other scores
TAlScore gaps_;
bool repeat_;
// splice scores
TAlScore splicescore_;
// mapped to known transcripts?
bool knownTranscripts_;
// continuous alignment near (known) splice sites?
bool nearSpliceSites_;
int leftTrim_;
int rightTrim_;
};
enum {
// This alignment is one of a pair of alignments that form a concordant
// alignment for a read
ALN_FLAG_PAIR_CONCORD_MATE1 = 1,
ALN_FLAG_PAIR_CONCORD_MATE2,
// This alignment is one of a pair of alignments that form a discordant
// alignment for a read
ALN_FLAG_PAIR_DISCORD_MATE1,
ALN_FLAG_PAIR_DISCORD_MATE2,
// This is an unpaired alignment but the read in question is a pair;
// usually, this happens because the read had no reportable paired-end
// alignments
ALN_FLAG_PAIR_UNPAIRED_MATE1,
ALN_FLAG_PAIR_UNPAIRED_MATE2,
// This is an unpaired alignment of an unpaired read
ALN_FLAG_PAIR_UNPAIRED
};
/**
* Encapsulates some general information about an alignment that doesn't belong
* in AlnRes. Specifically:
*
* 1. Whether the alignment is paired
* 2. If it's paried, whether it's concordant or discordant
* 3. Whether this alignment was found after the paired-end categories were
* maxed out
* 4. Whether the relevant unpaired category was maxed out
*/
class AlnFlags {
public:
AlnFlags() {
init(
ALN_FLAG_PAIR_UNPAIRED,
false, // canMax
false, // maxed
false, // maxedPair
false, // nfilt
false, // scfilt
false, // lenfilt
false, // qcfilt
false, // mixedMode
false, // primary
false, // oppAligned
false); // oppFw
}
AlnFlags(
int pairing,
bool canMax,
bool maxed,
bool maxedPair,
bool nfilt,
bool scfilt,
bool lenfilt,
bool qcfilt,
bool mixedMode,
bool primary,
bool oppAligned, // opposite mate aligned?
bool oppFw) // opposite mate aligned forward?
{
init(pairing, canMax, maxed, maxedPair, nfilt, scfilt,
lenfilt, qcfilt, mixedMode, primary, oppAligned, oppFw);
}
/**
* Initialize given values for all settings.
*/
void init(
int pairing,
bool canMax,
bool maxed,
bool maxedPair,
bool nfilt,
bool scfilt,
bool lenfilt,
bool qcfilt,
bool mixedMode,
bool primary,
bool oppAligned,
bool oppFw)
{
assert_gt(pairing, 0);
assert_leq(pairing, ALN_FLAG_PAIR_UNPAIRED);
pairing_ = pairing;
canMax_ = canMax;
maxed_ = maxed;
maxedPair_ = maxedPair;
nfilt_ = nfilt;
scfilt_ = scfilt;
lenfilt_ = lenfilt;
qcfilt_ = qcfilt;
mixedMode_ = mixedMode;
primary_ = primary;
oppAligned_ = oppAligned;
}
/**
* Return true iff this alignment is from a paired-end read.
*/
bool partOfPair() const {
assert_gt(pairing_, 0);
return pairing_ < ALN_FLAG_PAIR_UNPAIRED;
}
#ifndef NDEBUG
/**
* Check that the flags are internally consistent.
*/
bool repOk() const {
assert(partOfPair() || !maxedPair_);
return true;
}
#endif
/**
* Print out string representation of YF:i flag for indicating whether and
* why the mate was filtered.
*/
bool printYF(BTString& o, bool first) const;
/**
* Print out string representation of YM:i flag for indicating with the
* mate per se aligned repetitively.
*/
void printYM(BTString& o) const;
/**
* Print out string representation of YM:i flag for indicating with the
* pair containing the mate aligned repetitively.
*/
void printYP(BTString& o) const;
/**
* Print out string representation of these flags.
*/
void printYT(BTString& o) const;
inline int pairing() const { return pairing_; }
inline bool maxed() const { return maxed_; }
inline bool maxedPair() const { return maxedPair_; }
/**
* Return true iff the alignment is not the primary alignment; i.e. not the
* first reported alignment for the fragment.
*/
inline bool isPrimary() const {
return primary_;
}
/**
* Set the primary flag.
*/
void setPrimary(bool primary) {
primary_ = primary;
}
/**
* Return whether both paired and unpaired alignments are considered for
* pairs & their constituent mates
*/
inline bool isMixedMode() const {
return mixedMode_;
}
/**
* Return true iff the alignment params are such that it's possible for a
* read to be suppressed for being repetitive.
*/
inline bool canMax() const {
return canMax_;
}
/**
* Return true iff the alignment was filtered out.
*/
bool filtered() const {
return !nfilt_ || !scfilt_ || !lenfilt_ || !qcfilt_;
}
/**
* Return true iff the read is mate #1 of a pair, regardless of whether it
* aligned as a pair.
*/
bool readMate1() const {
return pairing_ == ALN_FLAG_PAIR_CONCORD_MATE1 ||
pairing_ == ALN_FLAG_PAIR_DISCORD_MATE1 ||
pairing_ == ALN_FLAG_PAIR_UNPAIRED_MATE1;
}
/**
* Return true iff the read is mate #2 of a pair, regardless of whether it
* aligned as a pair.
*/
bool readMate2() const {
return pairing_ == ALN_FLAG_PAIR_CONCORD_MATE2 ||
pairing_ == ALN_FLAG_PAIR_DISCORD_MATE2 ||
pairing_ == ALN_FLAG_PAIR_UNPAIRED_MATE2;
}
/**
* Return true iff the read aligned as either mate of a concordant pair.
*/
bool alignedConcordant() const {
return pairing_ == ALN_FLAG_PAIR_CONCORD_MATE1 ||
pairing_ == ALN_FLAG_PAIR_CONCORD_MATE2;
}
/**
* Return true iff the read aligned as either mate of a discordant pair.
*/
bool alignedDiscordant() const {
return pairing_ == ALN_FLAG_PAIR_DISCORD_MATE1 ||
pairing_ == ALN_FLAG_PAIR_DISCORD_MATE2;
}
/**
* Return true iff the read aligned as either mate of a pair, concordant or
* discordant.
*/
bool alignedPaired() const {
return alignedConcordant() || alignedDiscordant();
}
/**
* Return true iff the read aligned as an unpaired read.
*/
bool alignedUnpaired() const {
return pairing_ == ALN_FLAG_PAIR_UNPAIRED;
}
/**
* Return true iff the read aligned as an unpaired mate from a paired read.
*/
bool alignedUnpairedMate() const {
return pairing_ == ALN_FLAG_PAIR_UNPAIRED_MATE1 ||
pairing_ == ALN_FLAG_PAIR_UNPAIRED_MATE2;
}
bool mateAligned() const {
return oppAligned_;
}
protected:
// See ALN_FLAG_PAIR_* above
int pairing_;
// True iff the alignment params are such that it's possible for a read to
// be suppressed for being repetitive
bool canMax_;
// This alignment is sampled from among many alignments that, taken
// together, cause this mate to align non-uniquely
bool maxed_;
// The paired-end read of which this mate is part has repetitive concordant
// alignments
bool maxedPair_;
bool nfilt_; // read/mate filtered b/c proportion of Ns exceeded ceil
bool scfilt_; // read/mate filtered b/c length can't provide min score
bool lenfilt_; // read/mate filtered b/c less than or equal to seed mms
bool qcfilt_; // read/mate filtered by upstream qc
// Whether both paired and unpaired alignments are considered for pairs &
// their constituent mates
bool mixedMode_;
// The read is the primary read
bool primary_;
// True iff the opposite mate aligned
bool oppAligned_;
};
static inline ostream& operator<<(ostream& os, const AlnScore& o) {
os << o.score();
return os;
}
// Forward declaration
class BitPairReference;
// A given AlnRes can be one of these three types
enum {
ALN_RES_TYPE_UNPAIRED = 1, // unpaired alignment
ALN_RES_TYPE_UNPAIRED_MATE1, // mate #1 in pair, aligned unpaired
ALN_RES_TYPE_UNPAIRED_MATE2, // mate #2 in pair, aligned unpaired
ALN_RES_TYPE_MATE1, // mate #1 in paired-end alignment
ALN_RES_TYPE_MATE2 // mate #2 in paired-end alignment
};
/**
* Seed alignment summary
*/
struct SeedAlSumm {
SeedAlSumm() { reset(); }
void reset() {
nonzTot = nonzFw = nonzRc = 0;
nrangeTot = nrangeFw = nrangeRc = 0;
neltTot = neltFw = neltRc = 0;
minNonzRangeFw = minNonzRangeRc = 0;
maxNonzRangeFw = maxNonzRangeRc = 0;
minNonzEltFw = minNonzEltRc = 0;
maxNonzEltFw = maxNonzEltRc = 0;
}
size_t nonzTot;
size_t nonzFw;
size_t nonzRc;
size_t nrangeTot;
size_t nrangeFw;
size_t nrangeRc;
size_t neltTot;
size_t neltFw;
size_t neltRc;
size_t minNonzRangeFw;
size_t minNonzRangeRc;
size_t maxNonzRangeFw;
size_t maxNonzRangeRc;
size_t minNonzEltFw;
size_t minNonzEltRc;
size_t maxNonzEltFw;
size_t maxNonzEltRc;
};
/**
* Encapsulates a stacked alignment, a nice intermediate format for alignments
* from which to left-align gaps, print CIGAR strings, and print MD:Z strings.
*/
class StackedAln {
public:
StackedAln() :
stackRef_(RES_CAT),
stackRel_(RES_CAT),
stackSNP_(RES_CAT),
stackRead_(RES_CAT),
stackSkip_(RES_CAT),
cigOp_(RES_CAT),
cigRun_(RES_CAT),
mdzOp_(RES_CAT),
mdzChr_(RES_CAT),
mdzRun_(RES_CAT)
{
reset();
}
/**
* Reset to an uninitialized state.
*/
void reset() {
inited_ = false;
trimLS_ = trimLH_ = trimRS_ = trimRH_ = 0;
stackRef_.clear();
stackRel_.clear();
stackSNP_.clear();
stackRead_.clear();
stackSkip_.clear();
cigDistMm_ = cigCalc_ = false;
cigOp_.clear();
cigRun_.clear();
mdzCalc_ = false;
mdzOp_.clear();
mdzChr_.clear();
mdzRun_.clear();
}
/**
* Return true iff the stacked alignment has been initialized.
*/
bool inited() const { return inited_; }
/**
* Initialized the stacked alignment with respect to a read string, a list of
* edits (expressed left-to-right), and integers indicating how much hard and
* soft trimming has occurred on either end of the read.
*
* s: read sequence
* ed: all relevant edits, including ambiguous nucleotides
* trimLS: # bases soft-trimmed from LHS
* trimLH: # bases hard-trimmed from LHS
* trimRS: # bases soft-trimmed from RHS
* trimRH: # bases hard-trimmed from RHS
*/
void init(
const BTDnaString& s,
const EList<Edit>& ed,
size_t trimLS,
size_t trimLH,
size_t trimRS,
size_t trimRH);
/**
* Left-align all the gaps. If this changes the alignment and the CIGAR or
* MD:Z strings have already been calculated, this renders them invalid.
*
* We left-align gaps with in the following way: for each gap, we check
* whether the character opposite the rightmost gap character is the same
* as the character opposite the character just to the left of the gap. If
* this is the case, we can slide the gap to the left and make the
* rightmost position previously covered by the gap into a non-gap.
*
* This scheme allows us to push the gap past a mismatch. BWA does seem to
* allow this. It's not clear that Bowtie 2 should, since moving the
* mismatch could cause a mismatch with one base quality to be replaced
* with a mismatch with a different base quality.
*/
void leftAlign(bool pastMms);
/**
* Build the CIGAR list, if it hasn't already built. Returns true iff it
* was built for the first time.
*/
bool buildCigar(bool xeq);
/**
* Build the MD:Z list, if it hasn't already built. Returns true iff it
* was built for the first time.
*/
bool buildMdz();
/**
* Write a CIGAR representation of the alignment to the given string and/or
* char buffer.
*/
void writeCigar(BTString* o, char* oc) const;
/**
* Write an MD:Z representation of the alignment to the given string and/or
* char buffer.
*/
void writeMdz(BTString* o, char* oc) const;
/**
* Check internal consistency.
*/
#ifndef NDEBUG
bool repOk() const {
if(inited_) {
assert_eq(stackRef_.size(), stackRead_.size());
assert_eq(stackRef_.size(), stackRel_.size());
}
return true;
}
#endif
protected:
bool inited_; // true iff stacked alignment is initialized
size_t trimLS_; // amount soft-trimmed from the LHS
size_t trimLH_; // amount hard-trimmed from the LHS
size_t trimRS_; // amount soft-trimmed from the RHS
size_t trimRH_; // amount hard-trimmed from the RHS
EList<char> stackRef_; // reference characters
EList<char> stackRel_; // bars relating reference to read characters
EList<bool> stackSNP_; // known SNP?
EList<char> stackRead_; // read characters
EList<uint32_t> stackSkip_;
bool cigDistMm_; // distinguish between =/X, rather than just M
bool cigCalc_; // whether we've calculated CIGAR ops/runs
EList<char> cigOp_; // CIGAR operations
EList<size_t> cigRun_; // CIGAR run lengths
bool mdzCalc_; // whether we've calculated MD:Z ops/runs
EList<char> mdzOp_; // MD:Z operations
EList<char> mdzChr_; // MD:Z operations
EList<size_t> mdzRun_; // MD:Z run lengths
};
/**
* Encapsulates an alignment result. The result comprises:
*
* 1. All the nucleotide edits for both mates ('ned').
* 2. All "edits" where an ambiguous reference char is resolved to an
* unambiguous char ('aed').
* 3. The score for the alginment, including summary information about the
* number of gaps and Ns involved.
* 4. The reference id, strand, and 0-based offset of the leftmost character
* involved in the alignment.
* 5. Information about trimming prior to alignment and whether it was hard or
* soft.
* 6. Information about trimming during alignment and whether it was hard or
* soft. Local-alignment trimming is usually soft when aligning nucleotide
* reads.
*
* Note that the AlnRes, together with the Read and an AlnSetSumm (*and* the
* opposite mate's AlnRes and Read in the case of a paired-end alignment),
* should contain enough information to print an entire alignment record.
*
* TRIMMING
*
* Accounting for trimming is tricky. Trimming affects:
*
* 1. The values of the trim* and pretrim* fields.
* 2. The offsets of the Edits in the EList<Edit>s.
* 3. The read extent, if the trimming is soft.
* 4. The read extent and the read sequence and length, if trimming is hard.
*
* Handling 1. is not too difficult. 2., 3., and 4. are handled in setShape().
*/
class AlnRes {
public:
AlnRes() :
// ned_(RES_CAT),
// aed_(RES_CAT)
ned_(NULL),
aed_(NULL),
ned_node_(NULL),
aed_node_(NULL),
raw_edits_(NULL)
{
reset();
}
AlnRes(const AlnRes& other) :
ned_(NULL),
aed_(NULL),
ned_node_(NULL),
aed_node_(NULL),
raw_edits_(NULL)
{
shapeSet_ = other.shapeSet_;
rdlen_ = other.rdlen_;
rdid_ = other.rdid_;
rdrows_ = other.rdrows_;
score_ = other.score_;
oscore_ = other.oscore_;
refcoord_ = other.refcoord_;
reflen_ = other.reflen_;
refival_ = other.refival_;
rdextent_ = other.rdextent_;
rdexrows_ = other.rdexrows_;
rfextent_ = other.rfextent_;
seedmms_ = other.seedmms_;
seedlen_ = other.seedlen_;
minsc_ = other.minsc_;
nuc5p_ = other.nuc5p_;
nuc3p_ = other.nuc3p_;
refns_ = other.refns_;
type_ = other.type_;
fraglenSet_ = other.fraglenSet_;
fraglen_ = other.fraglen_;
pretrimSoft_ = other.pretrimSoft_;
pretrim5p_ = other.pretrim5p_;
pretrim3p_ = other.pretrim3p_;
trimSoft_ = other.trimSoft_;
trim5p_ = other.trim5p_;
trim3p_ = other.trim3p_;
repeat_ = other.repeat_;
num_spliced_ = other.num_spliced_;
raw_edits_ = other.raw_edits_;
if(raw_edits_ != NULL) {
assert(ned_ == NULL && aed_ == NULL);
assert(ned_node_ == NULL && aed_node_ == NULL);
ned_node_ = raw_edits_->new_node();
aed_node_ = raw_edits_->new_node();
assert(ned_node_ != NULL && aed_node_ != NULL);
ned_ = &(ned_node_->payload);
aed_ = &(aed_node_->payload);
assert(other.ned_ != NULL && other.aed_ != NULL);
*ned_ = *(other.ned_);
*aed_ = *(other.aed_);
}
}
AlnRes& operator=(const AlnRes& other) {
if(this == &other) return *this;
shapeSet_ = other.shapeSet_;
rdlen_ = other.rdlen_;
rdid_ = other.rdid_;
rdrows_ = other.rdrows_;
score_ = other.score_;
oscore_ = other.oscore_;
refcoord_ = other.refcoord_;
reflen_ = other.reflen_;
refival_ = other.refival_;
rdextent_ = other.rdextent_;
rdexrows_ = other.rdexrows_;
rfextent_ = other.rfextent_;
seedmms_ = other.seedmms_;
seedlen_ = other.seedlen_;
minsc_ = other.minsc_;
nuc5p_ = other.nuc5p_;
nuc3p_ = other.nuc3p_;
refns_ = other.refns_;
type_ = other.type_;
fraglenSet_ = other.fraglenSet_;
fraglen_ = other.fraglen_;
pretrimSoft_ = other.pretrimSoft_;
pretrim5p_ = other.pretrim5p_;
pretrim3p_ = other.pretrim3p_;
trimSoft_ = other.trimSoft_;
trim5p_ = other.trim5p_;
trim3p_ = other.trim3p_;
repeat_ = other.repeat_;
num_spliced_ = other.num_spliced_;
assert(raw_edits_ == NULL || raw_edits_ == other.raw_edits_);
raw_edits_ = other.raw_edits_;
if(ned_ != NULL) {