| 1 |
// Author: David Alexander |
| 2 |
|
| 3 |
#include <ConsensusCore/Checksum.hpp> |
| 4 |
#include <ConsensusCore/Mutation.hpp> |
| 5 |
#include <ConsensusCore/Quiver/MultiReadMutationScorer.hpp> |
| 6 |
#include <ConsensusCore/Quiver/MutationScorer.hpp> |
| 7 |
#include <ConsensusCore/Sequence.hpp> |
| 8 |
#include <ConsensusCore/Utils.hpp> |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
#include <boost/format.hpp> |
| 12 |
#include <cfloat> |
| 13 |
#include <map> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#define MIN_FAVORABLE_SCOREDIFF 0.04f // Chosen such that 0.49 = 1 / (1 + exp(minScoreDiff)) |
| 18 |
|
| 19 |
namespace ConsensusCore { |
| 20 |
// |
| 21 |
// Could the mutation change the contents of the portion of the |
| 22 |
// template that is mapped to the read? |
| 23 |
// |
| 24 |
bool ReadScoresMutation(const MappedRead& read, const Mutation& mut) |
| 25 |
{ |
| 26 |
int ts = read.TemplateStart; |
| 27 |
int te = read.TemplateEnd; |
| 28 |
int ms = mut.Start(); |
| 29 |
int me = mut.End(); |
| 30 |
if (mut.IsInsertion()) { |
| 31 |
return (ts < ms && me <= te); // Insertion starts within? |
| 32 |
} else { |
| 33 |
return (ts < te && ts < me && ms < te); // Intervals intersect? |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// |
| 38 |
// Logic for turning a mutation to the global template space to |
| 39 |
// one in the coordinates understood by each individual mutation |
| 40 |
// scorer. This involves translation, complementation, and also |
| 41 |
// possible clipping, if the mutation is not wholly within the |
| 42 |
// mapped read. |
| 43 |
// |
| 44 |
Mutation OrientedMutation(const MappedRead& mr, const Mutation& mut) |
| 45 |
{ |
| 46 |
using std::min; |
| 47 |
using std::max; |
| 48 |
|
| 49 |
// Clip mutation to bounds of mapped read, so that overhanging |
| 50 |
// multibase changes are handled correctly |
| 51 |
Mutation cmut(INSERTION, 0, 0, "N"); |
| 52 |
if (mut.End() - mut.Start() > 1) { |
| 53 |
int cs, ce; |
| 54 |
cs = max(mut.Start(), mr.TemplateStart); |
| 55 |
ce = min(mut.End(), mr.TemplateEnd); |
| 56 |
if (mut.IsSubstitution()) { |
| 57 |
std::string cNewBases = mut.NewBases().substr(cs - mut.Start(), ce - cs); |
| 58 |
cmut = Mutation(mut.Type(), cs, ce, cNewBases); |
| 59 |
} else { |
| 60 |
cmut = Mutation(mut.Type(), cs, ce, mut.NewBases()); |
| 61 |
} |
| 62 |
} else { |
| 63 |
cmut = mut; |
| 64 |
} |
| 65 |
|
| 66 |
// Now orient |
| 67 |
if (mr.Strand == FORWARD_STRAND) { |
| 68 |
return Mutation(cmut.Type(), cmut.Start() - mr.TemplateStart, cmut.End() - mr.TemplateStart, |
| 69 |
cmut.NewBases()); |
| 70 |
} else { |
| 71 |
// This is tricky business |
| 72 |
int end = mr.TemplateEnd - cmut.Start(); |
| 73 |
int start = mr.TemplateEnd - cmut.End(); |
| 74 |
return Mutation(cmut.Type(), start, end, ReverseComplement(cmut.NewBases())); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
template <typename R> |
| 79 |
MultiReadMutationScorer<R>::MultiReadMutationScorer( |
| 80 |
const QuiverConfigTable& quiverConfigByChemistry, std::string tpl) |
| 81 |
: quiverConfigByChemistry_(quiverConfigByChemistry) |
| 82 |
, fwdTemplate_(tpl) |
| 83 |
, revTemplate_(ReverseComplement(tpl)) |
| 84 |
, reads_() |
| 85 |
{ |
| 86 |
DEBUG_ONLY(CheckInvariants()); |
| 87 |
fastScoreThreshold_ = 0; |
| 88 |
QuiverConfigTable::const_iterator it; |
| 89 |
for (it = quiverConfigByChemistry_.begin(); it != quiverConfigByChemistry_.end(); it++) { |
| 90 |
fastScoreThreshold_ = std::min(fastScoreThreshold_, it->second.FastScoreThreshold); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
template <typename R> |
| 95 |
MultiReadMutationScorer<R>::MultiReadMutationScorer(const MultiReadMutationScorer<R>& other) |
| 96 |
: quiverConfigByChemistry_(other.quiverConfigByChemistry_) |
| 97 |
, fastScoreThreshold_(other.fastScoreThreshold_) |
| 98 |
, fwdTemplate_(other.fwdTemplate_) |
| 99 |
, revTemplate_(other.revTemplate_) |
| 100 |
, reads_() |
| 101 |
{ |
| 102 |
// Make a deep copy of the readsAndScorers |
| 103 |
foreach (const ReadStateType& read, reads_) { |
| 104 |
reads_.push_back(ReadStateType(read)); |
| 105 |
} |
| 106 |
|
| 107 |
DEBUG_ONLY(CheckInvariants()); |
| 108 |
} |
| 109 |
|
| 110 |
template <typename R> |
| 111 |
MultiReadMutationScorer<R>::~MultiReadMutationScorer() |
| 112 |
{ |
| 113 |
} |
| 114 |
|
| 115 |
template <typename R> |
| 116 |
int MultiReadMutationScorer<R>::TemplateLength() const |
| 117 |
{ |
| 118 |
return fwdTemplate_.length(); |
| 119 |
} |
| 120 |
|
| 121 |
template <typename R> |
| 122 |
int MultiReadMutationScorer<R>::NumReads() const |
| 123 |
{ |
| 124 |
return reads_.size(); |
| 125 |
} |
| 126 |
|
| 127 |
template <typename R> |
| 128 |
const MappedRead* MultiReadMutationScorer<R>::Read(int readIdx) const |
| 129 |
{ |
| 130 |
return reads_[readIdx].IsActive ? reads_[readIdx].Read : NULL; |
| 131 |
} |
| 132 |
|
| 133 |
template <typename R> |
| 134 |
std::string MultiReadMutationScorer<R>::Template(StrandEnum strand) const |
| 135 |
{ |
| 136 |
return (strand == FORWARD_STRAND ? fwdTemplate_ : revTemplate_); |
| 137 |
} |
| 138 |
|
| 139 |
template <typename R> |
| 140 |
std::string MultiReadMutationScorer<R>::Template(StrandEnum strand, int templateStart, |
| 141 |
int templateEnd) const |
| 142 |
{ |
| 143 |
int len = templateEnd - templateStart; |
| 144 |
if (strand == FORWARD_STRAND) { |
| 145 |
return fwdTemplate_.substr(templateStart, len); |
| 146 |
} else { |
| 147 |
return revTemplate_.substr(TemplateLength() - templateEnd, len); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
template <typename R> |
| 152 |
void MultiReadMutationScorer<R>::ApplyMutations(const std::vector<Mutation>& mutations) |
| 153 |
{ |
| 154 |
DEBUG_ONLY(CheckInvariants()); |
| 155 |
std::vector<int> mtp = TargetToQueryPositions(mutations, fwdTemplate_); |
| 156 |
fwdTemplate_ = ConsensusCore::ApplyMutations(mutations, fwdTemplate_); |
| 157 |
revTemplate_ = ReverseComplement(fwdTemplate_); |
| 158 |
|
| 159 |
foreach (ReadStateType& rs, reads_) { |
| 160 |
try { |
| 161 |
int newTemplateStart = mtp[rs.Read->TemplateStart]; |
| 162 |
int newTemplateEnd = mtp[rs.Read->TemplateEnd]; |
| 163 |
|
| 164 |
// reads (even inactive reads) will have their mapping coords updated |
| 165 |
rs.Read->TemplateStart = newTemplateStart; |
| 166 |
rs.Read->TemplateEnd = newTemplateEnd; |
| 167 |
|
| 168 |
if (rs.IsActive) { |
| 169 |
rs.Scorer->Template(Template(rs.Read->Strand, newTemplateStart, newTemplateEnd)); |
| 170 |
} |
| 171 |
} catch (AlphaBetaMismatchException& e) { |
| 172 |
rs.IsActive = false; |
| 173 |
} |
| 174 |
} |
| 175 |
DEBUG_ONLY(CheckInvariants()); |
| 176 |
} |
| 177 |
|
| 178 |
template <typename R> |
| 179 |
bool MultiReadMutationScorer<R>::AddRead(const MappedRead& mr, float threshold) |
| 180 |
{ |
| 181 |
DEBUG_ONLY(CheckInvariants()); |
| 182 |
const QuiverConfig* config = &quiverConfigByChemistry_.At(mr.Chemistry); |
| 183 |
EvaluatorType ev(mr, Template(mr.Strand, mr.TemplateStart, mr.TemplateEnd), config->QvParams); |
| 184 |
RecursorType recursor(config->MovesAvailable, config->Banding); |
| 185 |
|
| 186 |
ScorerType* scorer; |
| 187 |
try { |
| 188 |
scorer = new MutationScorer<R>(ev, recursor); |
| 189 |
} catch (AlphaBetaMismatchException& e) { |
| 190 |
scorer = NULL; |
| 191 |
} |
| 192 |
|
| 193 |
if (scorer != NULL && threshold < 1.0f) { |
| 194 |
int I = ev.ReadLength(); |
| 195 |
int J = ev.TemplateLength(); |
| 196 |
int maxSize = static_cast<int>(0.5f + threshold * (I + 1) * (J + 1)); |
| 197 |
|
| 198 |
if (scorer->Alpha()->AllocatedEntries() >= maxSize || |
| 199 |
scorer->Beta()->AllocatedEntries() >= maxSize) { |
| 200 |
delete scorer; |
| 201 |
scorer = NULL; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
bool isActive = scorer != NULL; |
| 206 |
reads_.push_back(ReadStateType(new MappedRead(mr), scorer, isActive)); |
| 207 |
DEBUG_ONLY(CheckInvariants()); |
| 208 |
return isActive; |
| 209 |
} |
| 210 |
|
| 211 |
template <typename R> |
| 212 |
bool MultiReadMutationScorer<R>::AddRead(const MappedRead& mr) |
| 213 |
{ |
| 214 |
DEBUG_ONLY(CheckInvariants()); |
| 215 |
const QuiverConfig* config = &quiverConfigByChemistry_.At(mr.Chemistry); |
| 216 |
return AddRead(mr, config->AddThreshold); |
| 217 |
} |
| 218 |
|
| 219 |
template <typename R> |
| 220 |
float MultiReadMutationScorer<R>::Score(const Mutation& m) const |
| 221 |
{ |
| 222 |
float sum = 0; |
| 223 |
foreach (const ReadStateType& rs, reads_) { |
| 224 |
if (rs.IsActive && ReadScoresMutation(*rs.Read, m)) { |
| 225 |
Mutation orientedMut = OrientedMutation(*rs.Read, m); |
| 226 |
sum += (rs.Scorer->ScoreMutation(orientedMut) - rs.Scorer->Score()); |
| 227 |
} |
| 228 |
} |
| 229 |
return sum; |
| 230 |
} |
| 231 |
|
| 232 |
template <typename R> |
| 233 |
float MultiReadMutationScorer<R>::Score(MutationType mutationType, int position, char base) const |
| 234 |
{ |
| 235 |
Mutation m(mutationType, position, base); |
| 236 |
return Score(m); |
| 237 |
} |
| 238 |
|
| 239 |
template <typename R> |
| 240 |
float MultiReadMutationScorer<R>::FastScore(const Mutation& m) const |
| 241 |
{ |
| 242 |
float sum = 0; |
| 243 |
foreach (const ReadStateType& rs, reads_) { |
| 244 |
if (rs.IsActive && ReadScoresMutation(*rs.Read, m)) { |
| 245 |
Mutation orientedMut = OrientedMutation(*rs.Read, m); |
| 246 |
sum += (rs.Scorer->ScoreMutation(orientedMut) - rs.Scorer->Score()); |
| 247 |
if (sum < fastScoreThreshold_) { |
| 248 |
return sum; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
return sum; |
| 253 |
} |
| 254 |
|
| 255 |
template <typename R> |
| 256 |
std::vector<float> MultiReadMutationScorer<R>::Scores(const Mutation& m, float unscoredValue) const |
| 257 |
{ |
| 258 |
std::vector<float> scoreByRead; |
| 259 |
foreach (const ReadStateType& rs, reads_) { |
| 260 |
if (rs.IsActive && ReadScoresMutation(*rs.Read, m)) { |
| 261 |
Mutation orientedMut = OrientedMutation(*rs.Read, m); |
| 262 |
scoreByRead.push_back(rs.Scorer->ScoreMutation(orientedMut) - rs.Scorer->Score()); |
| 263 |
} else { |
| 264 |
scoreByRead.push_back(unscoredValue); |
| 265 |
} |
| 266 |
} |
| 267 |
return scoreByRead; |
| 268 |
} |
| 269 |
|
| 270 |
template <typename R> |
| 271 |
std::vector<float> MultiReadMutationScorer<R>::Scores(MutationType mutationType, int position, |
| 272 |
char base, float unscoredValue) const |
| 273 |
{ |
| 274 |
Mutation m(mutationType, position, base); |
| 275 |
return Scores(m, unscoredValue); |
| 276 |
} |
| 277 |
|
| 278 |
template <typename R> |
| 279 |
bool MultiReadMutationScorer<R>::IsFavorable(const Mutation& m) const |
| 280 |
{ |
| 281 |
float sum = 0; |
| 282 |
foreach (const ReadStateType& rs, reads_) { |
| 283 |
if (rs.IsActive && ReadScoresMutation(*rs.Read, m)) { |
| 284 |
Mutation orientedMut = OrientedMutation(*rs.Read, m); |
| 285 |
sum += (rs.Scorer->ScoreMutation(orientedMut) - rs.Scorer->Score()); |
| 286 |
} |
| 287 |
} |
| 288 |
return (sum > MIN_FAVORABLE_SCOREDIFF); |
| 289 |
} |
| 290 |
|
| 291 |
template <typename R> |
| 292 |
bool MultiReadMutationScorer<R>::FastIsFavorable(const Mutation& m) const |
| 293 |
{ |
| 294 |
float sum = 0; |
| 295 |
foreach (const ReadStateType& rs, reads_) { |
| 296 |
if (rs.IsActive && ReadScoresMutation(*rs.Read, m)) { |
| 297 |
Mutation orientedMut = OrientedMutation(*rs.Read, m); |
| 298 |
sum += (rs.Scorer->ScoreMutation(orientedMut) - rs.Scorer->Score()); |
| 299 |
if (sum < fastScoreThreshold_) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
return (sum > MIN_FAVORABLE_SCOREDIFF); |
| 305 |
} |
| 306 |
|
| 307 |
template <typename R> |
| 308 |
std::vector<int> MultiReadMutationScorer<R>::AllocatedMatrixEntries() const |
| 309 |
{ |
| 310 |
std::vector<int> allocatedCounts; |
| 311 |
for (int i = 0; i < static_cast<int>(reads_.size()); i++) { |
| 312 |
int n = AlphaMatrix(i)->AllocatedEntries() + BetaMatrix(i)->AllocatedEntries(); |
| 313 |
allocatedCounts.push_back(n); |
| 314 |
} |
| 315 |
return allocatedCounts; |
| 316 |
} |
| 317 |
|
| 318 |
template <typename R> |
| 319 |
std::vector<int> MultiReadMutationScorer<R>::UsedMatrixEntries() const |
| 320 |
{ |
| 321 |
std::vector<int> usedCounts; |
| 322 |
for (int i = 0; i < static_cast<int>(reads_.size()); i++) { |
| 323 |
int n = AlphaMatrix(i)->UsedEntries() + BetaMatrix(i)->UsedEntries(); |
| 324 |
usedCounts.push_back(n); |
| 325 |
} |
| 326 |
return usedCounts; |
| 327 |
} |
| 328 |
|
| 329 |
template <typename R> |
| 330 |
const AbstractMatrix* MultiReadMutationScorer<R>::AlphaMatrix(int i) const |
| 331 |
{ |
| 332 |
return reads_[i].Scorer->Alpha(); |
| 333 |
} |
| 334 |
|
| 335 |
template <typename R> |
| 336 |
const AbstractMatrix* MultiReadMutationScorer<R>::BetaMatrix(int i) const |
| 337 |
{ |
| 338 |
return reads_[i].Scorer->Beta(); |
| 339 |
} |
| 340 |
|
| 341 |
template <typename R> |
| 342 |
std::vector<int> MultiReadMutationScorer<R>::NumFlipFlops() const |
| 343 |
{ |
| 344 |
std::vector<int> nFlipFlops; |
| 345 |
foreach (const ReadStateType& rs, reads_) { |
| 346 |
nFlipFlops.push_back(rs.Scorer->NumFlipFlops()); |
| 347 |
} |
| 348 |
return nFlipFlops; |
| 349 |
} |
| 350 |
|
| 351 |
template <typename R> |
| 352 |
float MultiReadMutationScorer<R>::BaselineScore() const |
| 353 |
{ |
| 354 |
float sum = 0; |
| 355 |
foreach (const ReadStateType& rs, reads_) { |
| 356 |
if (rs.IsActive) sum += rs.Scorer->Score(); |
| 357 |
} |
| 358 |
return sum; |
| 359 |
} |
| 360 |
|
| 361 |
template <typename R> |
| 362 |
std::vector<float> MultiReadMutationScorer<R>::BaselineScores() const |
| 363 |
{ |
| 364 |
std::vector<float> scoreByRead; |
| 365 |
foreach (const ReadStateType& rs, reads_) { |
| 366 |
if (rs.IsActive) scoreByRead.push_back(rs.Scorer->Score()); |
| 367 |
} |
| 368 |
return scoreByRead; |
| 369 |
} |
| 370 |
|
| 371 |
template <typename R> |
| 372 |
void MultiReadMutationScorer<R>::CheckInvariants() const |
| 373 |
{ |
| 374 |
#ifndef NDEBUG |
| 375 |
assert(revTemplate_ == ReverseComplement(fwdTemplate_)); |
| 376 |
foreach (const ReadStateType& rs, reads_) { |
| 377 |
rs.CheckInvariants(); |
| 378 |
if (rs.IsActive) { |
| 379 |
assert(rs.Scorer->Template() == |
| 380 |
Template(rs.Read->Strand, rs.Read->TemplateStart, rs.Read->TemplateEnd)); |
| 381 |
assert(0 <= rs.Read->TemplateStart && |
| 382 |
rs.Read->TemplateStart <= static_cast<int>(fwdTemplate_.size())); |
| 383 |
assert(0 <= rs.Read->TemplateEnd && |
| 384 |
rs.Read->TemplateEnd <= static_cast<int>(fwdTemplate_.size())); |
| 385 |
assert(rs.Read->TemplateStart <= rs.Read->TemplateEnd); |
| 386 |
} |
| 387 |
} |
| 388 |
#endif // !NDEBUG |
| 389 |
} |
| 390 |
|
| 391 |
template <typename R> |
| 392 |
std::string MultiReadMutationScorer<R>::ToString() const |
| 393 |
{ |
| 394 |
std::stringstream ss; |
| 395 |
|
| 396 |
ss << "Template: " << Template() << std::endl; |
| 397 |
ss << "Score: " << BaselineScore() << std::endl; |
| 398 |
|
| 399 |
ss << "Reads:" << std::endl; |
| 400 |
foreach (const ReadStateType& rs, reads_) { |
| 401 |
ss << "\t" << rs.ToString() << std::endl; |
| 402 |
} |
| 403 |
return ss.str(); |
| 404 |
} |
| 405 |
|
| 406 |
namespace detail { |
| 407 |
|
| 408 |
template <typename ScorerType> |
| 409 |
ReadState<ScorerType>::ReadState(MappedRead* read, ScorerType* scorer, bool isActive) |
| 410 |
: Read(read), Scorer(scorer), IsActive(isActive) |
| 411 |
{ |
| 412 |
CheckInvariants(); |
| 413 |
} |
| 414 |
|
| 415 |
template <typename ScorerType> |
| 416 |
ReadState<ScorerType>::ReadState(const ReadState& other) |
| 417 |
: Read(NULL), Scorer(NULL), IsActive(other.IsActive) |
| 418 |
{ |
| 419 |
if (other.Read != NULL) Read = new MappedRead(*other.Read); |
| 420 |
if (other.Scorer != NULL) Scorer = new ScorerType(*other.Scorer); |
| 421 |
CheckInvariants(); |
| 422 |
} |
| 423 |
|
| 424 |
template <typename ScorerType> |
| 425 |
ReadState<ScorerType>::~ReadState() |
| 426 |
{ |
| 427 |
if (Read != NULL) delete Read; |
| 428 |
if (Scorer != NULL) delete Scorer; |
| 429 |
} |
| 430 |
|
| 431 |
template <typename ScorerType> |
| 432 |
void ReadState<ScorerType>::CheckInvariants() const |
| 433 |
{ |
| 434 |
#ifndef NDEBUG |
| 435 |
if (IsActive) { |
| 436 |
assert(Read != NULL && Scorer != NULL); |
| 437 |
assert(static_cast<int>(Scorer->Template().length()) == |
| 438 |
Read->TemplateEnd - Read->TemplateStart); |
| 439 |
} |
| 440 |
#endif // !NDEBUG |
| 441 |
} |
| 442 |
|
| 443 |
template <typename ScorerType> |
| 444 |
std::string ReadState<ScorerType>::ToString() const |
| 445 |
{ |
| 446 |
std::string score; |
| 447 |
if (IsActive) { |
| 448 |
score = (boost::format(" (Score= %0.2f)") % Scorer->Score()).str(); |
| 449 |
} else { |
| 450 |
score = "*INACTIVE*"; |
| 451 |
} |
| 452 |
return Read->ToString() + score; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
template class MultiReadMutationScorer<SparseSseQvRecursor>; |
| 457 |
template class MultiReadMutationScorer<SparseSseQvSumProductRecursor>; |
| 458 |
} |