| 1 |
// Author: Patrick Marks and David Alexander |
| 2 |
|
| 3 |
#include <ConsensusCore/Quiver/MutationScorer.hpp> |
| 4 |
|
| 5 |
#include <ConsensusCore/Edna/EdnaEvaluator.hpp> |
| 6 |
#include <ConsensusCore/Matrix/DenseMatrix.hpp> |
| 7 |
#include <ConsensusCore/Matrix/SparseMatrix.hpp> |
| 8 |
#include <ConsensusCore/Mutation.hpp> |
| 9 |
#include <ConsensusCore/Quiver/QvEvaluator.hpp> |
| 10 |
#include <ConsensusCore/Quiver/SimpleRecursor.hpp> |
| 11 |
#include <ConsensusCore/Quiver/SseRecursor.hpp> |
| 12 |
|
| 13 |
#include <string> |
| 14 |
|
| 15 |
#define EXTEND_BUFFER_COLUMNS 8 |
| 16 |
|
| 17 |
namespace ConsensusCore { |
| 18 |
template <typename R> |
| 19 |
MutationScorer<R>::MutationScorer(const EvaluatorType& evaluator, const R& recursor) |
| 20 |
: evaluator_(new EvaluatorType(evaluator)), recursor_(new R(recursor)) |
| 21 |
{ |
| 22 |
try { |
| 23 |
// Allocate alpha and beta |
| 24 |
alpha_ = new MatrixType(evaluator.ReadLength() + 1, evaluator.TemplateLength() + 1); |
| 25 |
beta_ = new MatrixType(evaluator.ReadLength() + 1, evaluator.TemplateLength() + 1); |
| 26 |
// Buffer where we extend into |
| 27 |
extendBuffer_ = new MatrixType(evaluator.ReadLength() + 1, EXTEND_BUFFER_COLUMNS); |
| 28 |
// Initial alpha and beta |
| 29 |
numFlipFlops_ = recursor.FillAlphaBeta(*evaluator_, *alpha_, *beta_); |
| 30 |
} catch (AlphaBetaMismatchException e) { |
| 31 |
delete alpha_; |
| 32 |
delete beta_; |
| 33 |
delete extendBuffer_; |
| 34 |
delete recursor_; |
| 35 |
throw; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
template <typename R> |
| 40 |
MutationScorer<R>::MutationScorer(const MutationScorer<R>& other) |
| 41 |
{ |
| 42 |
evaluator_ = new EvaluatorType(*other.evaluator_); |
| 43 |
recursor_ = new R(*other.recursor_); |
| 44 |
|
| 45 |
// Copy alpha and beta |
| 46 |
alpha_ = new MatrixType(*other.alpha_); |
| 47 |
beta_ = new MatrixType(*other.beta_); |
| 48 |
// Buffer where we extend into |
| 49 |
extendBuffer_ = new MatrixType(*other.extendBuffer_); |
| 50 |
numFlipFlops_ = other.numFlipFlops_; |
| 51 |
} |
| 52 |
|
| 53 |
template <typename R> |
| 54 |
float MutationScorer<R>::Score() const |
| 55 |
{ |
| 56 |
return (*beta_)(0, 0); |
| 57 |
} |
| 58 |
|
| 59 |
template <typename R> |
| 60 |
std::string MutationScorer<R>::Template() const |
| 61 |
{ |
| 62 |
return evaluator_->Template(); |
| 63 |
} |
| 64 |
|
| 65 |
template <typename R> |
| 66 |
void MutationScorer<R>::Template(std::string tpl) |
| 67 |
{ |
| 68 |
delete alpha_; |
| 69 |
delete beta_; |
| 70 |
evaluator_->Template(tpl); |
| 71 |
alpha_ = new MatrixType(evaluator_->ReadLength() + 1, evaluator_->TemplateLength() + 1); |
| 72 |
beta_ = new MatrixType(evaluator_->ReadLength() + 1, evaluator_->TemplateLength() + 1); |
| 73 |
recursor_->FillAlphaBeta(*evaluator_, *alpha_, *beta_); |
| 74 |
} |
| 75 |
|
| 76 |
template <typename R> |
| 77 |
const typename R::MatrixType* MutationScorer<R>::Alpha() const |
| 78 |
{ |
| 79 |
return alpha_; |
| 80 |
} |
| 81 |
|
| 82 |
template <typename R> |
| 83 |
const typename R::MatrixType* MutationScorer<R>::Beta() const |
| 84 |
{ |
| 85 |
return beta_; |
| 86 |
} |
| 87 |
|
| 88 |
template <typename R> |
| 89 |
const typename R::EvaluatorType* MutationScorer<R>::Evaluator() const |
| 90 |
{ |
| 91 |
return evaluator_; |
| 92 |
} |
| 93 |
|
| 94 |
template <typename R> |
| 95 |
const PairwiseAlignment* MutationScorer<R>::Alignment() const |
| 96 |
{ |
| 97 |
return recursor_->Alignment(*evaluator_, *alpha_); |
| 98 |
} |
| 99 |
|
| 100 |
template <typename R> |
| 101 |
float MutationScorer<R>::ScoreMutation(const Mutation& m) const |
| 102 |
{ |
| 103 |
int betaLinkCol = 1 + m.End(); |
| 104 |
int absoluteLinkColumn = 1 + m.End() + m.LengthDiff(); |
| 105 |
std::string oldTpl = evaluator_->Template(); |
| 106 |
std::string newTpl = ApplyMutation(m, oldTpl); |
| 107 |
float score; |
| 108 |
|
| 109 |
bool atBegin = (m.Start() < 3); |
| 110 |
bool atEnd = (m.End() > static_cast<int>(oldTpl.length()) - 2); |
| 111 |
|
| 112 |
if (!atBegin && !atEnd) { |
| 113 |
// Install mutated template |
| 114 |
evaluator_->Template(newTpl); |
| 115 |
|
| 116 |
int extendStartCol, extendLength; |
| 117 |
|
| 118 |
if (m.Type() == DELETION) { |
| 119 |
// Future thought: If we revise the semantic of Extra, |
| 120 |
// we can remove the extend and just link alpha and |
| 121 |
// beta directly. |
| 122 |
extendStartCol = m.Start() - 1; |
| 123 |
extendLength = 2; |
| 124 |
} else { |
| 125 |
extendStartCol = m.Start(); |
| 126 |
extendLength = 1 + m.NewBases().length(); |
| 127 |
assert(extendLength <= EXTEND_BUFFER_COLUMNS); |
| 128 |
} |
| 129 |
|
| 130 |
recursor_->ExtendAlpha(*evaluator_, *alpha_, extendStartCol, *extendBuffer_, extendLength); |
| 131 |
score = recursor_->LinkAlphaBeta(*evaluator_, *extendBuffer_, extendLength, *beta_, |
| 132 |
betaLinkCol, absoluteLinkColumn); |
| 133 |
} else if (!atBegin && atEnd) { |
| 134 |
// |
| 135 |
// Extend alpha to end |
| 136 |
// |
| 137 |
evaluator_->Template(newTpl); |
| 138 |
|
| 139 |
int extendStartCol = m.Start() - 1; |
| 140 |
int extendLength = newTpl.length() - extendStartCol + 1; |
| 141 |
|
| 142 |
recursor_->ExtendAlpha(*evaluator_, *alpha_, extendStartCol, *extendBuffer_, extendLength); |
| 143 |
score = (*extendBuffer_)(evaluator_->ReadLength(), extendLength - 1); |
| 144 |
|
| 145 |
// if (fabs(score - Score()) > 50) { |
| 146 |
// // FIXME! This happens on fluidigm amplicons, figure out why |
| 147 |
// Breakpoint(); |
| 148 |
// } |
| 149 |
} else if (atBegin && !atEnd) { |
| 150 |
// |
| 151 |
// Extend beta back |
| 152 |
// |
| 153 |
evaluator_->Template(newTpl); |
| 154 |
|
| 155 |
int extendLastCol = m.End(); |
| 156 |
int extendLength = m.End() + m.LengthDiff() + 1; |
| 157 |
|
| 158 |
recursor_->ExtendBeta(*evaluator_, *beta_, extendLastCol, *extendBuffer_, extendLength, |
| 159 |
m.LengthDiff()); |
| 160 |
score = (*extendBuffer_)(0, 0); |
| 161 |
} else { |
| 162 |
assert(atBegin && atEnd); |
| 163 |
// |
| 164 |
// Just do the whole fill |
| 165 |
// |
| 166 |
MatrixType alphaP(evaluator_->ReadLength() + 1, newTpl.length() + 1); |
| 167 |
evaluator_->Template(newTpl); |
| 168 |
recursor_->FillAlpha(*evaluator_, MatrixType::Null(), alphaP); |
| 169 |
score = alphaP(evaluator_->ReadLength(), newTpl.length()); |
| 170 |
} |
| 171 |
|
| 172 |
// Restore the original template. |
| 173 |
evaluator_->Template(oldTpl); |
| 174 |
|
| 175 |
// if (fabs(score - Score()) > 50) { Breakpoint(); } |
| 176 |
|
| 177 |
return score; |
| 178 |
} |
| 179 |
|
| 180 |
template <typename R> |
| 181 |
MutationScorer<R>::~MutationScorer() |
| 182 |
{ |
| 183 |
delete extendBuffer_; |
| 184 |
delete beta_; |
| 185 |
delete alpha_; |
| 186 |
delete recursor_; |
| 187 |
delete evaluator_; |
| 188 |
} |
| 189 |
|
| 190 |
template class MutationScorer<SimpleQvRecursor>; |
| 191 |
template class MutationScorer<SseQvRecursor>; |
| 192 |
template class MutationScorer<SparseSimpleQvRecursor>; |
| 193 |
template class MutationScorer<SparseSimpleQvSumProductRecursor>; |
| 194 |
template class MutationScorer<SparseSseQvRecursor>; |
| 195 |
template class MutationScorer<SparseSseQvSumProductRecursor>; |
| 196 |
template class MutationScorer<SparseSseEdnaRecursor>; |
| 197 |
} |