Debian Codegraph @

src/dagcon/Alignment.cpp

raw
1 #include <pbcopper/dagcon/Alignment.h>
2
3 #include <stdexcept>
4 #include <string>
5
6 #include <cassert>
7
8 namespace PacBio {
9 namespace Dagcon {
10
11 void NormalizeGaps(Alignment& alignment, bool push)
12 {
13 assert(alignment.Query.length() == alignment.Target.length());
14 const size_t queryLength = alignment.Query.length();
15 if (queryLength == 0) {
16 return;
17 }
18
19 std::string qNorm;
20 qNorm.reserve(queryLength + 100);
21 std::string tNorm;
22 tNorm.reserve(queryLength + 100);
23
24 // convert mismatches to indels
25 for (size_t i = 0; i < queryLength; i++) {
26 const char queryBase = alignment.Query[i];
27 const char targetBase = alignment.Target[i];
28 if (queryBase != targetBase && queryBase != '-' && targetBase != '-') {
29 qNorm += '-';
30 qNorm += queryBase;
31 tNorm += targetBase;
32 tNorm += '-';
33 } else {
34 qNorm += queryBase;
35 tNorm += targetBase;
36 }
37 }
38
39 // update length
40 assert(qNorm.length() == tNorm.length());
41 const size_t qNormLength = qNorm.length();
42
43 if (push) {
44 // push gaps to the right, but not past the end
45 for (size_t i = 0; i < qNormLength - 1; i++) {
46 // pushing target gaps
47 if (tNorm[i] == '-') {
48 size_t j = i;
49 while (++j < qNormLength) {
50 const char c = tNorm[j];
51 if (c != '-') {
52 if (c == qNorm[i]) {
53 tNorm[i] = c;
54 tNorm[j] = '-';
55 }
56 break;
57 }
58 }
59 }
60
61 // pushing query gaps
62 if (qNorm[i] == '-') {
63 size_t j = i;
64 while (++j < qNormLength) {
65 const char c = qNorm[j];
66 if (c != '-') {
67 if (c == tNorm[i]) {
68 qNorm[i] = c;
69 qNorm[j] = '-';
70 }
71 break;
72 }
73 }
74 }
75 }
76 }
77 assert(qNorm.length() == tNorm.length());
78 assert(qNormLength == tNorm.length());
79
80 // update alignment sequence
81 alignment.Query.clear();
82 alignment.Target.clear();
83 for (size_t i = 0; i < qNormLength; ++i) {
84 if (qNorm[i] != '-' || tNorm[i] != '-') {
85 alignment.Query += qNorm[i];
86 alignment.Target += tNorm[i];
87 }
88 }
89 }
90
91 void TrimAlignment(Alignment& alignment, int trimLength)
92 {
93 const auto targetLength = alignment.Target.length();
94
95 int lbases = 0;
96 size_t lOffset = 0U;
97 while (lbases < trimLength && lOffset < targetLength) {
98 if (alignment.Target[lOffset] != '-') {
99 ++lbases;
100 }
101 ++lOffset;
102 }
103
104 int rbases = 0;
105 size_t rOffset = targetLength;
106 while (rbases < trimLength && rOffset > lOffset) {
107 --rOffset;
108 if (alignment.Target[rOffset] != '-') {
109 ++rbases;
110 }
111 }
112
113 alignment.Start += lbases;
114 alignment.Query = alignment.Query.substr(lOffset, rOffset - lOffset);
115 alignment.Target = alignment.Target.substr(lOffset, rOffset - lOffset);
116 }
117
118 } // namespace Dagcon
119 } // namespace PacBio