| 1 |
// Author: David Alexander |
| 2 |
|
| 3 |
#include <ConsensusCore/Quiver/detail/RecursorBase.hpp> |
| 4 |
|
| 5 |
#include <ConsensusCore/Align/PairwiseAlignment.hpp> |
| 6 |
#include <ConsensusCore/Edna/EdnaEvaluator.hpp> |
| 7 |
#include <ConsensusCore/LFloat.hpp> |
| 8 |
#include <ConsensusCore/Logging.hpp> |
| 9 |
#include <ConsensusCore/Matrix/DenseMatrix.hpp> |
| 10 |
#include <ConsensusCore/Matrix/SparseMatrix.hpp> |
| 11 |
#include <ConsensusCore/Quiver/QuiverConfig.hpp> |
| 12 |
#include <ConsensusCore/Quiver/QvEvaluator.hpp> |
| 13 |
#include <ConsensusCore/Quiver/detail/Combiner.hpp> |
| 14 |
#include <ConsensusCore/Types.hpp> |
| 15 |
#include <ConsensusCore/Utils.hpp> |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <boost/type_traits.hpp> |
| 19 |
#include <string> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
// TODO(dalexander): put these into a RecursorConfig struct |
| 23 |
#define MAX_FLIP_FLOPS 5 |
| 24 |
#define ALPHA_BETA_MISMATCH_TOLERANCE 0.2f |
| 25 |
#define REBANDING_THRESHOLD 0.04 |
| 26 |
|
| 27 |
using std::max; |
| 28 |
using std::min; |
| 29 |
|
| 30 |
namespace ConsensusCore { |
| 31 |
namespace detail { |
| 32 |
|
| 33 |
template <typename M, typename E, typename C> |
| 34 |
int RecursorBase<M, E, C>::FillAlphaBeta(const E& e, M& a, M& b) const |
| 35 |
{ |
| 36 |
FillAlpha(e, M::Null(), a); |
| 37 |
FillBeta(e, a, b); |
| 38 |
|
| 39 |
int I = e.ReadLength(); |
| 40 |
int J = e.TemplateLength(); |
| 41 |
int flipflops = 0; |
| 42 |
int maxSize = static_cast<int>(0.5 + REBANDING_THRESHOLD * (I + 1) * (J + 1)); |
| 43 |
|
| 44 |
// if we use too much space, do at least one more round |
| 45 |
// to take advantage of rebanding |
| 46 |
if (a.UsedEntries() >= maxSize || b.UsedEntries() >= maxSize) { |
| 47 |
FillAlpha(e, b, a); |
| 48 |
FillBeta(e, a, b); |
| 49 |
FillAlpha(e, b, a); |
| 50 |
flipflops += 3; |
| 51 |
} |
| 52 |
|
| 53 |
while (std::fabs(a(I, J) - b(0, 0)) > ALPHA_BETA_MISMATCH_TOLERANCE && |
| 54 |
flipflops <= MAX_FLIP_FLOPS) { |
| 55 |
if (flipflops % 2 == 0) { |
| 56 |
FillAlpha(e, b, a); |
| 57 |
} else { |
| 58 |
FillBeta(e, a, b); |
| 59 |
} |
| 60 |
flipflops++; |
| 61 |
} |
| 62 |
|
| 63 |
if (std::fabs(a(I, J) - b(0, 0)) > ALPHA_BETA_MISMATCH_TOLERANCE) { |
| 64 |
LDEBUG << "Could not mate alpha, beta. Read: " << e.ReadName() << " Tpl: " << e.Template(); |
| 65 |
throw AlphaBetaMismatchException(); |
| 66 |
} |
| 67 |
|
| 68 |
return flipflops; |
| 69 |
} |
| 70 |
|
| 71 |
struct MoveSpec |
| 72 |
{ |
| 73 |
Move MoveType; |
| 74 |
int ReadDelta; |
| 75 |
int ReferenceDelta; |
| 76 |
}; |
| 77 |
|
| 78 |
template <typename M, typename E, typename C> |
| 79 |
const PairwiseAlignment* RecursorBase<M, E, C>::Alignment(const E& e, const M& a) const |
| 80 |
{ |
| 81 |
if (!boost::is_same<C, ViterbiCombiner>::value) { |
| 82 |
ShouldNotReachHere(); |
| 83 |
} |
| 84 |
|
| 85 |
int I, J; |
| 86 |
I = e.ReadLength(); |
| 87 |
J = e.TemplateLength(); |
| 88 |
|
| 89 |
// Matrix must be filled in before requesting traceback |
| 90 |
assert(a(I, J) != lfloat()); |
| 91 |
|
| 92 |
int i = I; |
| 93 |
int j = J; |
| 94 |
float pathScore = 0; |
| 95 |
|
| 96 |
MoveSpec incMove = {INCORPORATE, 1, 1}; |
| 97 |
MoveSpec delMove = {DELETE, 0, 1}; |
| 98 |
MoveSpec extraMove = {EXTRA, 1, 0}; |
| 99 |
MoveSpec mergeMove = {MERGE, 1, 2}; |
| 100 |
std::vector<MoveSpec> moves; |
| 101 |
|
| 102 |
while (i > 0 || j > 0) { |
| 103 |
MoveSpec bestMove = {INVALID_MOVE, 0, 0}; |
| 104 |
float prevScore, moveScore, totalScore; |
| 105 |
float bestScore = lfloat(); |
| 106 |
float bestMoveScore = lfloat(); |
| 107 |
|
| 108 |
if (i > 0 && j > 0) { |
| 109 |
prevScore = a(i - 1, j - 1); |
| 110 |
moveScore = e.Inc(i - 1, j - 1); |
| 111 |
totalScore = prevScore + moveScore; |
| 112 |
if (totalScore > bestScore) { |
| 113 |
// Incorporate (match or mismatch) |
| 114 |
bestMove = incMove; |
| 115 |
bestScore = totalScore; |
| 116 |
bestMoveScore = moveScore; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (j > 0) { |
| 121 |
// Delete |
| 122 |
prevScore = a(i, j - 1); |
| 123 |
bool freeDelete = (!e.PinEnd() && i == I) || (!e.PinStart() && i == 0); |
| 124 |
moveScore = freeDelete ? 0 : e.Del(i, j - 1); |
| 125 |
totalScore = prevScore + moveScore; |
| 126 |
if (totalScore > bestScore) { |
| 127 |
bestMove = delMove; |
| 128 |
bestScore = totalScore; |
| 129 |
bestMoveScore = moveScore; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (i > 0) { |
| 134 |
// Extra |
| 135 |
prevScore = a(i - 1, j); |
| 136 |
moveScore = e.Extra(i - 1, j); |
| 137 |
totalScore = prevScore + moveScore; |
| 138 |
if (totalScore > bestScore) { |
| 139 |
bestMove = extraMove; |
| 140 |
bestScore = totalScore; |
| 141 |
bestMoveScore = moveScore; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if ((movesAvailable_ & MERGE) && i > 0 && j > 1) { |
| 146 |
// Merge |
| 147 |
prevScore = a(i - 1, j - 2); |
| 148 |
moveScore = e.Merge(i - 1, j - 2); |
| 149 |
totalScore = prevScore + moveScore; |
| 150 |
if (totalScore > bestScore) { |
| 151 |
bestMove = mergeMove; |
| 152 |
bestScore = totalScore; |
| 153 |
bestMoveScore = moveScore; |
| 154 |
} |
| 155 |
} |
| 156 |
assert(AlmostEqual(a(i, j), bestScore)); |
| 157 |
assert(bestMove.MoveType != INVALID_MOVE); |
| 158 |
assert(bestMoveScore != lfloat()); |
| 159 |
|
| 160 |
moves.push_back(bestMove); |
| 161 |
i -= bestMove.ReadDelta; |
| 162 |
j -= bestMove.ReferenceDelta; |
| 163 |
pathScore += bestMoveScore; |
| 164 |
} |
| 165 |
assert(i == 0 && j == 0); |
| 166 |
|
| 167 |
// Reverse moves |
| 168 |
std::reverse(moves.begin(), moves.end()); |
| 169 |
|
| 170 |
// Replay moves and stringify. |
| 171 |
std::string target; |
| 172 |
std::string query; |
| 173 |
i = 0; |
| 174 |
j = 0; |
| 175 |
foreach (const MoveSpec& move, moves) { |
| 176 |
switch (move.MoveType) { |
| 177 |
case INCORPORATE: |
| 178 |
target += e.Template()[j]; |
| 179 |
query += e.Basecalls()[i]; |
| 180 |
break; |
| 181 |
case EXTRA: |
| 182 |
target += '-'; |
| 183 |
query += e.Basecalls()[i]; |
| 184 |
break; |
| 185 |
case DELETE: |
| 186 |
target += e.Template()[j]; |
| 187 |
query += '-'; |
| 188 |
break; |
| 189 |
case MERGE: |
| 190 |
target += e.Template()[j]; |
| 191 |
target += e.Template()[j + 1]; |
| 192 |
query += '-'; |
| 193 |
query += e.Basecalls()[i]; |
| 194 |
break; |
| 195 |
case INVALID_MOVE: |
| 196 |
ShouldNotReachHere(); |
| 197 |
break; |
| 198 |
default: |
| 199 |
ShouldNotReachHere(); |
| 200 |
} |
| 201 |
i += move.ReadDelta; |
| 202 |
j += move.ReferenceDelta; |
| 203 |
assert(target.length() == query.length()); |
| 204 |
} |
| 205 |
|
| 206 |
return new PairwiseAlignment(target, query); |
| 207 |
} |
| 208 |
|
| 209 |
template <typename M, typename E, typename C> |
| 210 |
RecursorBase<M, E, C>::RecursorBase(int movesAvailable, const BandingOptions& bandingOptions) |
| 211 |
: movesAvailable_(movesAvailable), bandingOptions_(bandingOptions) |
| 212 |
{ |
| 213 |
} |
| 214 |
|
| 215 |
template <typename M, typename E, typename C> |
| 216 |
RecursorBase<M, E, C>::~RecursorBase() |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
// template instantiation |
| 221 |
template class RecursorBase<DenseMatrix, QvEvaluator, ViterbiCombiner>; |
| 222 |
template class RecursorBase<SparseMatrix, QvEvaluator, ViterbiCombiner>; |
| 223 |
template class RecursorBase<SparseMatrix, QvEvaluator, SumProductCombiner>; |
| 224 |
template class RecursorBase<SparseMatrix, EdnaEvaluator, SumProductCombiner>; |
| 225 |
} |
| 226 |
} |