Debian Codegraph @

src/align/LinearAlignment.cpp

raw
1 //
2 // This is the basic Hirschberg algo, not the affine variation by Myers and Miller.
3 // The tricky part of this code is that we use the a single array to store portions
4 // of two rows in the alignment matrices.
5 //
6 // Straightforward notes here: http://globin.cse.psu.edu/courses/fall2001/DP.pdf ;
7 // taken from "Recent Developments in Linear-Space Alignment Methods: A Survey".
8 // I follow them pretty closely except for the semiglobal alignment mode implemented
9 // here.
10 //
11 // For the affine (Gotoh) variation, not yet implemented, see Myers & Miller 1988
12 //
13
14 #include <pbcopper/align/LinearAlignment.h>
15
16 #include <cassert>
17
18 #include <algorithm>
19 #include <string>
20 #include <vector>
21
22 #include <boost/numeric/ublas/io.hpp>
23 #include <boost/numeric/ublas/matrix.hpp>
24 #include <boost/numeric/ublas/vector.hpp>
25
26 #include <pbcopper/align/PairwiseAlignment.h>
27 #include <pbcopper/utility/MinMax.h>
28
29 //#define DEBUG_LINEAR_ALIGNMENT
30
31 #ifdef DEBUG_LINEAR_ALIGNMENT
32 #include <iostream>
33 #endif // DEBUG_LINEAR_ALIGNMENT
34
35 namespace PacBio {
36 namespace Align {
37 namespace LinearAlign {
38
39 namespace ublas = boost::numeric::ublas;
40
41 constexpr int ALIGN_INSERT_SCORE = -2;
42 constexpr int ALIGN_DELETE_SCORE = -2;
43 constexpr int ALIGN_MISALIGN_MATCH_SCORE = -1;
44 constexpr int ALIGN_MATCH_SCORE = +2;
45
46 const AlignParams params{ALIGN_MATCH_SCORE, ALIGN_MISALIGN_MATCH_SCORE, ALIGN_INSERT_SCORE,
47 ALIGN_DELETE_SCORE};
48 const AlignConfig config{params, AlignMode::GLOBAL};
49
50 //
51 // Get transcript of NW alignment taking
52 // target[j1..j2] into query[i1..i2] (one-based indexing)
53 // used for trivial base cases.
54 //
55 std::string NWTranscript(const std::string& target, int j1, int j2, const std::string& query,
56 int i1, int i2, int* score)
57 {
58 // If j1 > j2 or i1 > i2, the respective subtarget or subquery is empty,
59 // ergo we have pure insertions or deletions.
60 assert((i2 - i1 >= -1) && (j2 - j1 >= -1));
61
62 // implement this inline later
63 std::string T = target.substr(j1 - 1, j2 - j1 + 1);
64 std::string Q = query.substr(i1 - 1, i2 - i1 + 1);
65 std::unique_ptr<PacBio::Align::PairwiseAlignment> aln{Align(T, Q, score, config)};
66 std::string X = aln->Transcript();
67 return X;
68 }
69
70 #ifndef NDEBUG
71 bool CheckTranscript(const std::string& transcript, const std::string& unalnTarget,
72 const std::string& unalnQuery)
73 {
74 std::unique_ptr<PairwiseAlignment> pa{
75 PairwiseAlignment::FromTranscript(transcript, unalnTarget, unalnQuery)};
76 return static_cast<bool>(pa);
77 }
78 #endif // NDEBUG
79
80 //
81 // Hirschberg recursion:
82 // Find optimal transcript taking target[j1..j2] into query[i1..i2] (one-based indices)
83 // Operates by divide-and-conquer, finding midpoint (m, j*) and recursing on halves, then joining.
84 // Notes:
85 //
86 // | Alignment | L | L_1 | L_2 |
87 // | Path | (0,0) ~> (I,J) | (0,0) ~> (m, j*) | (m, j*) ~> (I, J) |
88 // | T, Q | T[1..J], Q[1..I] | T[1..j*], Q[1..m] | T[j*+1..J], Q[m+1..I] |
89 // | Transcript | X | X_1 | X_2 |
90 //
91 // target on horizontal, query on vertical
92 // i refers to query; j refers to target
93 // this gives better balanced recursion in the (common) semiglobal case
94 //
95 std::string OptimalTranscript(const std::string& target, int j1, int j2, const std::string& query,
96 int i1, int i2, ublas::vector<int>& buf1, ublas::vector<int>& buf2,
97 int* score = nullptr)
98 {
99 #ifndef NDEBUG
100 std::string subtarget = target.substr(j1 - 1, j2 - j1 + 1);
101 std::string subquery = query.substr(i1 - 1, i2 - i1 + 1);
102 #endif
103
104 #ifdef DEBUG_LINEAR_ALIGNMENT
105 std::cout << "Called OptimalTranscript("
106 << "T[" << j1 << ".." << j2 << "], "
107 << "Q[" << i1 << ".." << i2 << "]) " << target << ' ' << query << " (" << subtarget
108 << ", " << subquery << ")\n";
109 #endif
110
111 std::string x;
112 std::string x1;
113 std::string x2;
114 int segmentScore;
115 const AlignParams& configParams = config.Params;
116
117 //
118 // Base case
119 //
120 if ((j2 - j1 < 1) || (i2 - i1 < 1)) {
121 x = NWTranscript(target, j1, j2, query, i1, i2, &segmentScore);
122 }
123
124 //
125 // Recursive case
126 //
127 else {
128 assert(buf1.size() == target.size() + 1);
129 assert(buf2.size() == target.size() + 1);
130
131 ublas::vector<int>& Sm = buf1; // S-
132 ublas::vector<int>& Sp = buf2; // S+
133
134 int mid = (i1 + i2) / 2;
135
136 //
137 // Score forward, i1 upto mid
138 // ( T[j1..j2] vs Q[i1..m] )
139 //
140 Sm(j1 - 1) = 0;
141 for (int j = j1; j <= j2; j++) {
142 Sm(j) = Sm(j - 1) + configParams.Delete;
143 }
144 for (int i = i1; i <= mid; i++) {
145 int s;
146 int c;
147 s = Sm(j1 - 1);
148 c = Sm(j1 - 1) + configParams.Insert;
149 Sm(j1 - 1) = c;
150 for (int j = j1; j <= j2; j++) {
151 char t = target[j - 1];
152 char q = query[i - 1];
153 c = Utility::Max(Sm(j) + configParams.Insert,
154 s + (t == q ? configParams.Match : configParams.Mismatch),
155 c + configParams.Delete);
156 s = Sm(j);
157 Sm(j) = c;
158 }
159 }
160
161 //
162 // Score backwards, i2 downto mid
163 // ( T[j1..j2] vs Q[m+1..i2] )
164 //
165 Sp(j2) = 0;
166 for (int j = j2 - 1; j >= j1 - 1; j--) {
167 Sp(j) = Sp(j + 1) + configParams.Delete;
168 }
169 for (int i = i2 - 1; i >= mid; i--) {
170 int s;
171 int c;
172 s = Sp(j2);
173 c = Sp(j2) + configParams.Delete;
174 Sp(j2) = c;
175 for (int j = j2 - 1; j >= j1 - 1; j--) {
176 char t = target[j]; // j + 1 - 1
177 char q = query[i]; // i + 1 - 1
178 c = Utility::Max(Sp(j) + configParams.Insert,
179 s + (t == q ? configParams.Match : configParams.Mismatch),
180 c + configParams.Delete);
181 s = Sp(j);
182 Sp(j) = c;
183 }
184 }
185
186 //
187 // Find where optimal path crosses the mid row
188 //
189 ublas::vector<int> sum = Sm + Sp;
190 int j = std::max_element(sum.begin() + j1 - 1, sum.begin() + j2 + 1) - sum.begin();
191 segmentScore = sum[j];
192
193 int segment1Score;
194 int segment2Score;
195 x1 = OptimalTranscript(target, j1, j, query, i1, mid, buf1, buf2, &segment1Score);
196 x2 = OptimalTranscript(target, j + 1, j2, query, mid + 1, i2, buf1, buf2, &segment2Score);
197 assert(segmentScore == segment1Score + segment2Score);
198 x = x1 + x2;
199 }
200
201 // Check 1: transcript has to take target[j1..j2] into query[i1..i2]
202 assert(CheckTranscript(x, subtarget, subquery));
203
204 // Check 2: same score as basic N/W?
205 #ifndef NDEBUG
206 int peerScore;
207 std::unique_ptr<PairwiseAlignment> peerAlignment{
208 Align(subtarget, subquery, &peerScore, config)};
209 assert(peerScore == segmentScore);
210 #endif
211
212 if (score != nullptr) {
213 *score = segmentScore;
214 }
215 return x;
216 }
217
218 } // namespace LinearAlign
219
220 PairwiseAlignment* AlignLinear(const std::string& target, const std::string& query, int* score,
221 AlignConfig /*unused*/)
222 {
223 using namespace LinearAlign;
224 int J = target.length();
225 ublas::vector<int> buf1(J + 1);
226 ublas::vector<int> buf2(J + 1);
227 std::string x =
228 OptimalTranscript(target, 1, target.length(), query, 1, query.length(), buf1, buf2, score);
229 return PairwiseAlignment::FromTranscript(x, target, query);
230 }
231
232 PairwiseAlignment* AlignLinear(const std::string& target, const std::string& query, AlignConfig cfg)
233 {
234 return AlignLinear(target, query, nullptr, cfg);
235 }
236
237 } // namespace Align
238 } // namespace PacBio