forked from BenLangmead/bowtie2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aligner_result.h
1982 lines (1766 loc) · 52 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 <langmea@cs.jhu.edu>
*
* 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"
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, int basesAligned, int edits, TAlScore ns, TAlScore gaps) {
score_ = score;
basesAligned_ = basesAligned;
edits_ = edits;
ns_ = ns;
gaps_ = gaps;
assert(valid());
}
/**
* Reset the score.
*/
void reset() {
score_ = basesAligned_ = edits_ = ns_ = gaps_ = 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;
edits_ = basesAligned_ = std::numeric_limits<int>::min();
ns_ = gaps_ = 0;
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_;
}
/**
* 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_;
basesAligned_ = o.basesAligned_;
ns_ = o.ns_;
edits_ = o.edits_;
score_ = o.score_;
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_;
}
/**
* 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_;
}
/**
* Return true iff this score is < score o.
*/
inline bool operator<(const AlnScore& o) const {
return !operator>=(o);
}
/**
* 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.basesAligned_ = basesAligned_ + o.basesAligned_;
s.ns_ = ns_ + o.ns_;
s.edits_ = edits_ + o.edits_;
s.score_ = score_ + o.score_;
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_;
basesAligned_ += o.basesAligned_;
score_ += o.score_;
edits_ += o.edits_;
ns_ += o.ns_;
}
return (*this);
}
TAlScore score() const { return score_; }
TAlScore penalty() const { return -score_; }
TAlScore gaps() const { return gaps_; }
TAlScore ns() const { return ns_; }
int basesAligned() const { return basesAligned_; }
int nedit() const { return edits_; }
// Score accumulated so far (penalties are subtracted starting at 0)
TAlScore score_;
// Number of bases matching between the read and reference
int basesAligned_;
// Edit distance
int edits_;
// 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_;
};
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
false, // scUnMapped
false); // xeq
}
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?
bool scUnMapped,
bool xeq)
{
init(pairing, canMax, maxed, maxedPair, nfilt, scfilt,
lenfilt, qcfilt, mixedMode, primary, oppAligned,
oppFw, scUnMapped, xeq);
}
/**
* 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,
bool scUnMapped,
bool xeq)
{
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;
oppFw_ = oppFw;
scUnMapped_ = scUnMapped;
xeq_ = xeq;
}
/**
* 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_;
}
bool isOppFw() const {
return oppFw_;
}
bool scUnMapped() const {
return scUnMapped_;
}
bool xeq() const {
return xeq_;
}
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_;
// True if opposite mate aligned in the forward direction
bool oppFw_;
// True if soft clipped bases are considered unmapped w/r/t TLEN
bool scUnMapped_;
bool xeq_;
};
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),
stackRead_(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();
stackRead_.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<char> stackRead_; // read characters
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)
{
reset();
}
/**
* Clear all contents.
*/
void reset();
/**
* Reverse all edit lists.
*/
void reverseEdits() {
ned_.reverse();
aed_.reverse();
}
/**
* Invert positions of edits so that they're with respect to the other end
* of the alignment. The assumption is that the .pos fields of the edits
* in the ned_/aed_/ced_ structures are offsets with respect to the first
* aligned character (i.e. after all trimming).
*/
void invertEdits() {
assert(shapeSet_);
assert_gt(rdlen_, 0);
assert_gt(rdrows_, 0);
Edit::invertPoss(ned_, rdexrows_, false);
Edit::invertPoss(aed_, rdexrows_, false);
}
/**
* Return true iff no result has been installed.
*/
bool empty() const {
if(!VALID_AL_SCORE(score_)) {
assert(ned_.empty());
assert(aed_.empty());
assert(!refcoord_.inited());
assert(!refival_.inited());
return true;
} else {
return false;
}
}
/**
* Return the identifier for the reference that the alignment
* occurred in.
*/
inline TRefId refid() const {
assert(shapeSet_);
return refcoord_.ref();
}
/**
* Return the orientation that the alignment occurred in.
*/
inline int orient() const {
assert(shapeSet_);
return refcoord_.orient();
}
/**
* Return the 0-based offset of the alignment into the reference
* sequence it aligned to.
*/
inline TRefOff refoff() const {
assert(shapeSet_);
return refcoord_.off();
}
/**
* Set arguments to coordinates for the upstream-most and downstream-most
* reference positions involved in the alignment.
*/
inline void getCoords(
Coord& st, // out: install starting coordinate here
Coord& en) // out: install ending coordinate here
const
{
assert(shapeSet_);
st.init(refcoord_);
en.init(refcoord_);
en.adjustOff(refExtent() - 1);
}
/**
* Set arguments to coordinates for the upstream-most and downstream-most
* reference positions covered by the read taking any read trimming into
* account. I.e. if the upstream-most offset involved in an alignment is
* 40 but the read was hard-trimmed by 5 on that end, the inferred
* upstream-most covered position is 35.
*/
inline void getExtendedCoords(
Coord& st, // out: install starting coordinate here
Coord& en, // out: install ending coordinate here
const AlnFlags& flags)
const
{
getCoords(st, en);
// Take trimming into account
if (!flags.scUnMapped()) {
int64_t trim_st = (fw() ? trim5p_ : trim3p_);
int64_t trim_en = (fw() ? trim3p_ : trim5p_);
trim_st += (fw() ? pretrim5p_ : pretrim3p_);
trim_en += (fw() ? pretrim3p_ : pretrim5p_);
st.adjustOff(-trim_st);
en.adjustOff( trim_en);
}
}
/**
* Set the upstream-most reference offset involved in the alignment, and
* the extent of the alignment (w/r/t the reference)
*/
void setShape(
TRefId id, // id of reference aligned to
TRefOff off, // offset of first aligned char into ref seq
TRefOff reflen, // length of reference sequence aligned to
bool fw, // aligned to Watson strand?
size_t rdlen, // length of read after hard trimming, before soft
bool pretrimSoft, // whether trimming prior to alignment was soft
size_t pretrim5p, // # poss trimmed form 5p end before alignment
size_t pretrim3p, // # poss trimmed form 3p end before alignment
bool trimSoft, // whether local-alignment trimming was soft
size_t trim5p, // # poss trimmed form 5p end during alignment
size_t trim3p); // # poss trimmed form 3p end during alignment
/**
* Return true iff the reference chars involved in this alignment result
* are entirely within with given bounds.
*/
bool within(
TRefId id,
TRefOff off,
bool fw,
size_t extent) const
{
if(refcoord_.ref() == id &&
refcoord_.off() >= off &&
refcoord_.off() + refExtent() <= off + extent &&
refcoord_.fw() == fw)
{
return true;
}
return false;
}
/**
* Set alignment score for this alignment.
*/
void setScore(AlnScore score) {
score_ = score;
}
/**
* Set the upstream-most and downstream-most nucleotides.
*/
void setNucs(bool fw, int nup, int ndn) {
nuc5p_ = fw ? nup : ndn;
nuc3p_ = fw ? ndn : nup;
}
/**
* Return the 0-based offset of the leftmost reference position involved in
* the alignment.
*/
const Coord& refcoord() const {
return refcoord_;
}
/**
* Return the 0-based offset of the leftmost reference position involved in
* the alignment.
*/
const Interval& refival() const {
return refival_;
}
/**
* Return the 0-based offset of the leftmost reference position involved in
* the alignment.
*/
Coord& refcoord() {
return refcoord_;
}
/**
* Return true if this alignment is to the Watson strand.
*/
inline bool fw() const {
return refcoord_.fw();
}
AlnScore score() const { return score_; }
AlnScore oscore() const { return oscore_; }
EList<Edit>& ned() { return ned_; }
EList<Edit>& aed() { return aed_; }
const EList<Edit>& ned() const { return ned_; }
const EList<Edit>& aed() const { return aed_; }
size_t readExtent() const { return rdextent_; }