Debian Codegraph @

src/C++/Quiver/SimpleRecursor.cpp

raw
1 // Author: David Alexander
2
3 #include <ConsensusCore/Quiver/SimpleRecursor.hpp>
4
5 #include <ConsensusCore/Edna/EdnaEvaluator.hpp>
6 #include <ConsensusCore/Interval.hpp>
7 #include <ConsensusCore/Matrix/DenseMatrix.hpp>
8 #include <ConsensusCore/Matrix/SparseMatrix.hpp>
9 #include <ConsensusCore/Quiver/QvEvaluator.hpp>
10 #include <ConsensusCore/Quiver/detail/Combiner.hpp>
11 #include <ConsensusCore/Quiver/detail/RecursorBase.hpp>
12 #include <ConsensusCore/Utils.hpp>
13
14 #include <algorithm>
15 #include <boost/tuple/tuple.hpp>
16 #include <climits>
17 #include <utility>
18
19 using std::min;
20 using std::max;
21
22 #define NEG_INF -FLT_MAX
23
24 namespace ConsensusCore {
25
26 template <typename M, typename E, typename C>
27 void SimpleRecursor<M, E, C>::FillAlpha(const E& e, const M& guide, M& alpha) const
28 {
29 int I = e.ReadLength();
30 int J = e.TemplateLength();
31
32 assert(alpha.Rows() == I + 1 && alpha.Columns() == J + 1);
33 assert(guide.IsNull() || (guide.Rows() == alpha.Rows() && guide.Columns() == alpha.Columns()));
34
35 int hintBeginRow = 0, hintEndRow = 0;
36
37 for (int j = 0; j <= J; ++j) {
38 this->RangeGuide(j, guide, alpha, &hintBeginRow, &hintEndRow);
39
40 int requiredEndRow = min(I + 1, hintEndRow);
41
42 int i;
43 float score = NEG_INF;
44 float thresholdScore = NEG_INF;
45 float maxScore = NEG_INF;
46
47 alpha.StartEditingColumn(j, hintBeginRow, hintEndRow);
48
49 int beginRow = hintBeginRow, endRow;
50 for (i = beginRow; i < I + 1 && (score >= thresholdScore || i < requiredEndRow); ++i) {
51 float thisMoveScore;
52 score = NEG_INF;
53
54 // Start:
55 if (i == 0 && j == 0) {
56 score = 0.0f;
57 }
58
59 // Incorporation:
60 if (i > 0 && j > 0) {
61 thisMoveScore = alpha(i - 1, j - 1) + e.Inc(i - 1, j - 1);
62 score = C::Combine(score, thisMoveScore);
63 }
64
65 // Extra:
66 if (i > 0) {
67 thisMoveScore = alpha(i - 1, j) + e.Extra(i - 1, j);
68 score = C::Combine(score, thisMoveScore);
69 }
70
71 // Delete:
72 if (j > 0) {
73 thisMoveScore = alpha(i, j - 1) + e.Del(i, j - 1);
74 score = C::Combine(score, thisMoveScore);
75 }
76
77 // Merge:
78 if ((this->movesAvailable_ & MERGE) && j > 1 && i > 0) {
79 thisMoveScore = alpha(i - 1, j - 2) + e.Merge(i - 1, j - 2);
80 score = C::Combine(score, thisMoveScore);
81 }
82
83 // Save score
84 alpha.Set(i, j, score);
85
86 if (score > maxScore) {
87 maxScore = score;
88 thresholdScore = maxScore - this->bandingOptions_.ScoreDiff;
89 }
90 }
91
92 endRow = i;
93 alpha.FinishEditingColumn(j, beginRow, endRow);
94
95 // Now, revise the hints to tell the caller where the mass of the
96 // distribution really lived in this column.
97 hintEndRow = endRow;
98 for (i = beginRow; i < endRow && alpha(i, j) < thresholdScore; ++i)
99 ;
100 hintBeginRow = i;
101 }
102 }
103
104 template <typename M, typename E, typename C>
105 void SimpleRecursor<M, E, C>::FillBeta(const E& e, const M& guide, M& beta) const
106 {
107 int I = e.ReadLength();
108 int J = e.TemplateLength();
109
110 assert(beta.Rows() == I + 1 && beta.Columns() == J + 1);
111 assert(guide.IsNull() || (guide.Rows() == beta.Rows() && guide.Columns() == beta.Columns()));
112
113 int hintBeginRow = I + 1, hintEndRow = I + 1;
114
115 for (int j = J; j >= 0; --j) {
116 this->RangeGuide(j, guide, beta, &hintBeginRow, &hintEndRow);
117
118 int requiredBeginRow = max(0, hintBeginRow);
119
120 beta.StartEditingColumn(j, hintBeginRow, hintEndRow);
121
122 int i;
123 float score = NEG_INF;
124 float thresholdScore = NEG_INF;
125 float maxScore = NEG_INF;
126
127 int beginRow, endRow = hintEndRow;
128 for (i = endRow - 1; i >= 0 && (score >= thresholdScore || i >= requiredBeginRow); --i) {
129 float thisMoveScore;
130 score = NEG_INF;
131
132 // Start:
133 if (i == I && j == J) {
134 score = 0.0f;
135 }
136
137 // Incorporation:
138 if (i < I && j < J) {
139 thisMoveScore = beta(i + 1, j + 1) + e.Inc(i, j);
140 score = C::Combine(score, thisMoveScore);
141 }
142
143 // Extra:
144 if (i < I) {
145 thisMoveScore = beta(i + 1, j) + e.Extra(i, j);
146 score = C::Combine(score, thisMoveScore);
147 }
148
149 // Delete:
150 if (j < J) {
151 thisMoveScore = beta(i, j + 1) + e.Del(i, j);
152 score = C::Combine(score, thisMoveScore);
153 }
154
155 // Merge:
156 if ((this->movesAvailable_ & MERGE) && j < J - 1 && i < I) {
157 thisMoveScore = beta(i + 1, j + 2) + e.Merge(i, j);
158 score = C::Combine(score, thisMoveScore);
159 }
160
161 // Save score
162 beta.Set(i, j, score);
163
164 if (score > maxScore) {
165 maxScore = score;
166 thresholdScore = maxScore - this->bandingOptions_.ScoreDiff;
167 }
168 }
169
170 beginRow = i + 1;
171 beta.FinishEditingColumn(j, beginRow, endRow);
172
173 // Now, revise the hints to tell the caller where the mass of the
174 // distribution really lived in this column.
175 hintBeginRow = beginRow;
176 for (i = endRow; i > beginRow && beta(i - 1, j) < thresholdScore; --i)
177 ;
178 hintEndRow = i;
179 }
180 }
181
182 /// Calculate the recursion score by "stitching" together partial
183 /// alpha and beta matrices. alphaColumn, betaColumn, and
184 /// absoluteColumn all refer to the same logical position in the
185 /// template, but may have different values if, for instance,
186 /// alpha here is a sub-range of the columns of the full alpha
187 /// matrix. Columns betaColumn and betaColumn + 1 of beta will be
188 /// read; columns alphaColumn - 1 and alphaColumn - 2 of alpha
189 /// will be read.
190 template <typename M, typename E, typename C>
191 float SimpleRecursor<M, E, C>::LinkAlphaBeta(const E& e, const M& alpha, int alphaColumn,
192 const M& beta, int betaColumn,
193 int absoluteColumn) const
194 {
195 const int I = e.ReadLength();
196
197 assert(alphaColumn > 1 && absoluteColumn > 1);
198 assert(absoluteColumn < e.TemplateLength());
199
200 int usedBegin, usedEnd;
201 boost::tie(usedBegin, usedEnd) =
202 RangeUnion(alpha.UsedRowRange(alphaColumn - 2), alpha.UsedRowRange(alphaColumn - 1),
203 beta.UsedRowRange(betaColumn), beta.UsedRowRange(betaColumn + 1));
204
205 float v = NEG_INF, thisMoveScore;
206
207 for (int i = usedBegin; i < usedEnd; i++) {
208 if (i < I) {
209 // Incorporate
210 thisMoveScore =
211 alpha(i, alphaColumn - 1) + e.Inc(i, absoluteColumn - 1) + beta(i + 1, betaColumn);
212 v = C::Combine(v, thisMoveScore);
213
214 // Merge (2 possible ways):
215 thisMoveScore = alpha(i, alphaColumn - 2) + e.Merge(i, absoluteColumn - 2) +
216 beta(i + 1, betaColumn);
217 v = C::Combine(v, thisMoveScore);
218
219 thisMoveScore = alpha(i, alphaColumn - 1) + e.Merge(i, absoluteColumn - 1) +
220 beta(i + 1, betaColumn + 1);
221 v = C::Combine(v, thisMoveScore);
222 }
223
224 // Delete:
225 thisMoveScore =
226 alpha(i, alphaColumn - 1) + e.Del(i, absoluteColumn - 1) + beta(i, betaColumn);
227 v = C::Combine(v, thisMoveScore);
228 }
229
230 return v;
231 }
232
233 //
234 // Reads: alpha(:, (beginColumn-2)..)
235 //
236 template <typename M, typename E, typename C>
237 void SimpleRecursor<M, E, C>::ExtendAlpha(const E& e, const M& alpha, int beginColumn, M& ext,
238 int numExtColumns) const
239 {
240 assert(numExtColumns >= 2);
241 assert(alpha.Rows() == e.ReadLength() + 1 && ext.Rows() == e.ReadLength() + 1);
242
243 // The new template may not be the same length as the old template.
244 // Just make sure that we have anough room to fill out the extend buffer
245 assert(beginColumn + 1 < e.TemplateLength() + 1);
246 assert(ext.Columns() >= numExtColumns);
247 assert(beginColumn >= 2);
248
249 for (int extCol = 0; extCol < numExtColumns; extCol++) {
250 int j = beginColumn + extCol;
251 int beginRow, endRow;
252
253 //
254 // If this extend is contained within the column bounds of
255 // the original alpha, we use the row range that was
256 // previously determined. Otherwise start at alpha's last
257 // UsedRow beginRow and go to the end.
258 //
259 if (j < alpha.Columns()) {
260 boost::tie(beginRow, endRow) = alpha.UsedRowRange(j);
261 } else {
262 beginRow = alpha.UsedRowRange(alpha.Columns() - 1).Begin;
263 endRow = alpha.Rows();
264 }
265
266 ext.StartEditingColumn(extCol, beginRow, endRow);
267
268 int i;
269 float score;
270
271 for (i = beginRow; i < endRow; i++) {
272 float thisMoveScore;
273 score = NEG_INF;
274
275 // Incorporation:
276 if (i > 0 && j > 0) {
277 float prev = extCol == 0 ? alpha(i - 1, j - 1) : ext(i - 1, extCol - 1);
278 thisMoveScore = prev + e.Inc(i - 1, j - 1);
279 score = C::Combine(score, thisMoveScore);
280 }
281
282 // Extra:
283 if (i > 0) {
284 thisMoveScore = ext(i - 1, extCol) + e.Extra(i - 1, j);
285 score = C::Combine(score, thisMoveScore);
286 }
287
288 // Delete:
289 if (j > 0) {
290 float prev = extCol == 0 ? alpha(i, j - 1) : ext(i, extCol - 1);
291 thisMoveScore = prev + e.Del(i, j - 1);
292 score = C::Combine(score, thisMoveScore);
293 }
294
295 // FIXME: is the merge code below incorrect for numExtColumns > 2?
296 // Merge:
297 if ((this->movesAvailable_ & MERGE) && j > 1 && i > 0) {
298 float prev = alpha(i - 1, j - 2);
299 thisMoveScore = prev + e.Merge(i - 1, j - 2);
300 score = C::Combine(score, thisMoveScore);
301 }
302
303 ext.Set(i, extCol, score);
304 }
305 assert(i == endRow);
306 ext.FinishEditingColumn(extCol, beginRow, endRow);
307 }
308 }
309
310 // Semantic: After ExtendBeta(B, j), we have
311 // ext(:, numExtColumns-1) = B'(:,j)
312 // ext(:, numExtColumns-2) = B'(:,j-1) ...
313 //
314 // Note: lastColumn is the numerically largest column number that
315 // will be filled, but it is filled first since beta fill is done
316 // backwards.
317 //
318 // Accesses B(:, ..(j+2))
319 template <typename M, typename E, typename C>
320 void SimpleRecursor<M, E, C>::ExtendBeta(const E& e, const M& beta, int lastColumn, M& ext,
321 int numExtColumns, int lengthDiff) const
322 {
323 int I = beta.Rows() - 1;
324 int J = beta.Columns() - 1;
325
326 int lastExtColumn = numExtColumns - 1;
327
328 assert(beta.Rows() == I + 1 && ext.Rows() == I + 1);
329
330 // The new template may not be the same length as the old template.
331 // Just make sure that we have anough room to fill out the extend buffer
332 assert(lastColumn + 2 <= J);
333 assert(lastColumn >= 0);
334 assert(ext.Columns() >= numExtColumns);
335
336 for (int j = lastColumn; j > lastColumn - numExtColumns; j--) {
337 int jp = j + lengthDiff;
338 int extCol = lastExtColumn - (lastColumn - j);
339 int beginRow, endRow;
340
341 if (j < 0) {
342 beginRow = 0;
343 endRow = beta.UsedRowRange(0).End;
344 } else {
345 boost::tie(beginRow, endRow) = beta.UsedRowRange(j);
346 }
347
348 ext.StartEditingColumn(extCol, beginRow, endRow);
349
350 int i;
351 float score;
352
353 for (i = endRow - 1; i >= beginRow; i--) {
354 float thisMoveScore;
355 score = NEG_INF;
356
357 // Incorporation:
358 if (i < I && j < J) {
359 float prev =
360 (extCol == lastExtColumn) ? beta(i + 1, j + 1) : ext(i + 1, extCol + 1);
361 thisMoveScore = prev + e.Inc(i, jp);
362 score = C::Combine(score, thisMoveScore);
363 }
364
365 // Extra:
366 if (i < I) {
367 thisMoveScore = ext(i + 1, extCol) + e.Extra(i, jp);
368 score = C::Combine(score, thisMoveScore);
369 }
370
371 // Delete:
372 if (j < J) {
373 float prev = (extCol == lastExtColumn) ? beta(i, j + 1) : ext(i, extCol + 1);
374 thisMoveScore = prev + e.Del(i, jp);
375 score = C::Combine(score, thisMoveScore);
376 }
377
378 // FIXME: is the merge code below incorrect for numExtColumns > 2?
379 // Merge:
380 if ((this->movesAvailable_ & MERGE) && j < J - 1 && i < I) {
381 thisMoveScore = beta(i + 1, j + 2) + e.Merge(i, jp);
382 score = C::Combine(score, thisMoveScore);
383 }
384
385 ext.Set(i, extCol, score);
386 }
387 ext.FinishEditingColumn(extCol, beginRow, endRow);
388 }
389 }
390
391 template <typename M, typename E, typename C>
392 SimpleRecursor<M, E, C>::SimpleRecursor(int movesAvailable, const BandingOptions& banding)
393 : detail::RecursorBase<M, E, C>(movesAvailable, banding)
394 {
395 }
396
397 template class SimpleRecursor<DenseMatrix, QvEvaluator, detail::ViterbiCombiner>;
398 template class SimpleRecursor<SparseMatrix, QvEvaluator, detail::ViterbiCombiner>;
399 template class SimpleRecursor<SparseMatrix, QvEvaluator, detail::SumProductCombiner>;
400 template class SimpleRecursor<SparseMatrix, EdnaEvaluator, detail::SumProductCombiner>;
401 }