1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/*
* Copyright 2011, Ben Langmead <langmea@cs.jhu.edu>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bowtie 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* unique.h
*
* Encapsulates objects and routines for determining whether and to
* what extend the best alignment for a read is "unique." In the
* simplest scenario, uniqueness is determined by whether we found only
* one alignment. More complex scenarios might assign a uniqueness
* score based that is a function of (of a summarized version of): all
* the alignments found and their scores.
*
* Since mapping quality is related to uniqueness, objects and
* routings for calculating mapping quality are also included here.
*/
#ifndef UNIQUE_H_
#define UNIQUE_H_
#include <string>
#include "aligner_result.h"
#include "simple_func.h"
#include "util.h"
#include "scoring.h"
typedef int64_t TMapq;
/**
* Class that returns yes-or-no answers to the question of whether a
*/
class Uniqueness {
public:
/**
* Given an AlnSetSumm, determine if the best alignment is "unique"
* according to some definition.
*/
static bool bestIsUnique(
const AlnSetSumm& s,
const AlnFlags& flags,
bool mate1,
size_t rdlen,
size_t ordlen,
char *inps)
{
assert(!s.empty());
return !VALID_AL_SCORE(s.secbest(mate1));
}
};
/**
* Collection of routines for calculating mapping quality.
*/
class Mapq {
public:
virtual ~Mapq() { }
virtual TMapq mapq(
const AlnSetSumm& s,
const AlnFlags& flags,
bool mate1,
size_t rdlen,
size_t ordlen,
char *inps) const = 0;
};
extern const TMapq unp_nosec_perf;
extern const TMapq unp_nosec[11];
extern const TMapq unp_sec_perf[11];
extern const TMapq unp_sec[11][11];
extern const TMapq pair_nosec_perf;
/**
* V3 of the MAPQ calculator
*/
class BowtieMapq3 : public Mapq {
public:
BowtieMapq3(
const SimpleFunc& scoreMin,
const Scoring& sc) :
scoreMin_(scoreMin),
sc_(sc)
{ }
virtual ~BowtieMapq3() { }
/**
* Given an AlnSetSumm, return a mapping quality calculated.
*/
virtual TMapq mapq(
const AlnSetSumm& s,
const AlnFlags& flags,
bool mate1,
size_t rdlen,
size_t ordlen,
char *inps) // put string representation of inputs here
const
{
if(s.paired()) {
return pair_nosec_perf;
} else {
bool hasSecbest = VALID_AL_SCORE(s.secbest(mate1));
if(!flags.canMax() && !s.exhausted(mate1) && !hasSecbest) {
return 255;
}
TAlScore scMax = (TAlScore)sc_.perfectScore(rdlen);
TAlScore scMin = scoreMin_.f<TAlScore>((float)rdlen);
assert_geq(scMax, scMin);
TAlScore best = scMax - s.best(mate1).score(); // best score (lower=better)
size_t best_bin = (size_t)((double)best * (10.0 / (double)(scMax - scMin)) + 0.5);
assert_geq(best_bin, 0);
assert_lt(best_bin, 11);
if(hasSecbest) {
assert_geq(s.best(mate1).score(), s.secbest(mate1).score());
size_t diff = s.best(mate1).score() - s.secbest(mate1).score();
size_t diff_bin = (size_t)((double)diff * (10.0 / (double)(scMax - scMin)) + 0.5);
assert_geq(diff_bin, 0);
assert_lt(diff_bin, 11);
// A valid second-best alignment was found
if(best == scMax) {
// Best alignment has perfect score
return unp_sec_perf[best_bin];
} else {
// Best alignment has less than perfect score
return unp_sec[diff_bin][best_bin];
}
} else {
// No valid second-best alignment was found
if(best == scMax) {
// Best alignment has perfect score
return unp_nosec_perf;
} else {
// Best alignment has less than perfect score
return unp_nosec[best_bin];
}
}
}
}
protected:
SimpleFunc scoreMin_;
const Scoring& sc_;
};
/**
* V2 of the MAPQ calculator
*/
class BowtieMapq2 : public Mapq {
public:
BowtieMapq2(
const SimpleFunc& scoreMin,
const Scoring& sc) :
scoreMin_(scoreMin),
sc_(sc)
{ }
virtual ~BowtieMapq2() { }
/**
* Given an AlnSetSumm, return a mapping quality calculated.
*/
virtual TMapq mapq(
const AlnSetSumm& s,
const AlnFlags& flags,
bool mate1,
size_t rdlen,
size_t ordlen,
char *inps) // put string representation of inputs here
const
{
// Did the read have a second-best alignment?
bool hasSecbest = s.paired() ?
VALID_AL_SCORE(s.secbestPaired()) :
VALID_AL_SCORE(s.secbest(mate1));
// for hisat
bool equalSecbest = false;
if(hasSecbest) {
if(s.paired()) {
assert_geq(s.bestPaired(), s.secbestPaired());
equalSecbest = s.bestPaired() == s.secbestPaired();
} else {
assert_geq(s.best(mate1), s.secbest(mate1));
equalSecbest = s.best(mate1) == s.secbest(mate1);
}
}
// This corresponds to a scenario where we found one and only one
// alignment but didn't really look for a second one
if(!flags.canMax() &&
!s.exhausted(mate1) &&
(!hasSecbest || !equalSecbest)) {
return 60;
}
// scPer = score of a perfect match
TAlScore scPer = (TAlScore)sc_.perfectScore(rdlen);
if(s.paired()) {
scPer += (TAlScore)sc_.perfectScore(ordlen);
}
// scMin = score of a just barely valid match
TAlScore scMin = scoreMin_.f<TAlScore>((float)rdlen);
if(s.paired()) {
scMin += scoreMin_.f<TAlScore>((float)ordlen);
}
TAlScore secbest = scMin-1;
TAlScore diff = (scPer - scMin); // scores can vary by up to this much
TMapq ret = 0;
TAlScore best = s.paired() ?
s.bestPaired().score() : s.best(mate1).score();
// best score but normalized so that 0 = worst valid score
TAlScore bestOver = best - scMin;
if(sc_.monotone) {
// End-to-end alignment
if(!hasSecbest) {
if (bestOver >= diff * (double)0.8f) ret = 42;
else if(bestOver >= diff * (double)0.7f) ret = 40;
else if(bestOver >= diff * (double)0.6f) ret = 24;
else if(bestOver >= diff * (double)0.5f) ret = 23;
else if(bestOver >= diff * (double)0.4f) ret = 8;
else if(bestOver >= diff * (double)0.3f) ret = 3;
else ret = 0;
} else {
secbest = s.paired() ?
s.secbestPaired().score() : s.secbest(mate1).score();
TAlScore bestdiff = abs(abs(static_cast<long>(best))-abs(static_cast<long>(secbest)));
if(bestdiff >= diff * (double)0.9f) {
if(bestOver == diff) {
ret = 39;
} else {
ret = 33;
}
} else if(bestdiff >= diff * (double)0.8f) {
if(bestOver == diff) {
ret = 38;
} else {
ret = 27;
}
} else if(bestdiff >= diff * (double)0.7f) {
if(bestOver == diff) {
ret = 37;
} else {
ret = 26;
}
} else if(bestdiff >= diff * (double)0.6f) {
if(bestOver == diff) {
ret = 36;
} else {
ret = 22;
}
} else if(bestdiff >= diff * (double)0.5f) {
// Top third is still pretty good
if (bestOver == diff) {
ret = 35;
} else if(bestOver >= diff * (double)0.84f) {
ret = 25;
} else if(bestOver >= diff * (double)0.68f) {
ret = 16;
} else {
ret = 5;
}
} else if(bestdiff >= diff * (double)0.4f) {
// Top third is still pretty good
if (bestOver == diff) {
ret = 34;
} else if(bestOver >= diff * (double)0.84f) {
ret = 21;
} else if(bestOver >= diff * (double)0.68f) {
ret = 14;
} else {
ret = 4;
}
} else if(bestdiff >= diff * (double)0.3f) {
// Top third is still pretty good
if (bestOver == diff) {
ret = 32;
} else if(bestOver >= diff * (double)0.88f) {
ret = 18;
} else if(bestOver >= diff * (double)0.67f) {
ret = 15;
} else {
ret = 3;
}
} else if(bestdiff >= diff * (double)0.2f) {
// Top third is still pretty good
if (bestOver == diff) {
ret = 31;
} else if(bestOver >= diff * (double)0.88f) {
ret = 17;
} else if(bestOver >= diff * (double)0.67f) {
ret = 11;
} else {
ret = 0;
}
} else if(bestdiff >= diff * (double)0.1f) {
// Top third is still pretty good
if (bestOver == diff) {
ret = 30;
} else if(bestOver >= diff * (double)0.88f) {
ret = 12;
} else if(bestOver >= diff * (double)0.67f) {
ret = 7;
} else {
ret = 0;
}
} else if(bestdiff > 0) {
// Top third is still pretty good
if(bestOver >= diff * (double)0.67f) {
ret = 6;
} else {
ret = 2;
}
} else {
assert_eq(bestdiff, 0);
// Top third is still pretty good
if(bestOver >= diff * (double)0.67f) {
ret = 1;
} else {
ret = 0;
}
}
}
} else {
// Local alignment
if(!hasSecbest) {
if (bestOver >= diff * (double)0.8f) ret = 44;
else if(bestOver >= diff * (double)0.7f) ret = 42;
else if(bestOver >= diff * (double)0.6f) ret = 41;
else if(bestOver >= diff * (double)0.5f) ret = 36;
else if(bestOver >= diff * (double)0.4f) ret = 28;
else if(bestOver >= diff * (double)0.3f) ret = 24;
else ret = 22;
} else {
secbest = s.paired() ?
s.secbestPaired().score() : s.secbest(mate1).score();
TAlScore bestdiff = abs(abs(static_cast<long>(best))-abs(static_cast<long>(secbest)));
if (bestdiff >= diff * (double)0.9f) ret = 40;
else if(bestdiff >= diff * (double)0.8f) ret = 39;
else if(bestdiff >= diff * (double)0.7f) ret = 38;
else if(bestdiff >= diff * (double)0.6f) ret = 37;
else if(bestdiff >= diff * (double)0.5f) {
if (bestOver == diff) ret = 35;
else if(bestOver >= diff * (double)0.50f) ret = 25;
else ret = 20;
} else if(bestdiff >= diff * (double)0.4f) {
if (bestOver == diff) ret = 34;
else if(bestOver >= diff * (double)0.50f) ret = 21;
else ret = 19;
} else if(bestdiff >= diff * (double)0.3f) {
if (bestOver == diff) ret = 33;
else if(bestOver >= diff * (double)0.5f) ret = 18;
else ret = 16;
} else if(bestdiff >= diff * (double)0.2f) {
if (bestOver == diff) ret = 32;
else if(bestOver >= diff * (double)0.5f) ret = 17;
else ret = 12;
} else if(bestdiff >= diff * (double)0.1f) {
if (bestOver == diff) ret = 31;
else if(bestOver >= diff * (double)0.5f) ret = 14;
else ret = 9;
} else if(bestdiff > 0) {
if(bestOver >= diff * (double)0.5f) ret = 11;
else ret = 2;
} else {
assert_eq(bestdiff, 0);
if(bestOver >= diff * (double)0.5f) ret = 1;
else ret = 0;
}
}
}
// Note: modifications to inps must be synchronized
//if(inps != NULL) {
// inps = itoa10<TAlScore>(best, inps);
// *inps++ = ',';
// inps = itoa10<TAlScore>(secbest, inps);
// *inps++ = ',';
// inps = itoa10<TMapq>(ret, inps);
//}
return ret;
}
protected:
SimpleFunc scoreMin_;
const Scoring& sc_;
};
/**
* TODO: Do BowtieMapq on a per-thread basis prior to the mutex'ed output
* function.
*
* topCoeff :: top_coeff
* botCoeff :: bot_coeff
* mx :: mapqMax
* horiz :: mapqHorizon (sort of)
*
* sc1 <- tab$sc1
* sc2 <- tab$sc2
* mapq <- rep(mx, length(sc1))
* diff_top <- ifelse(sc1 != best & sc2 != best, abs(best - abs(pmax(sc1, sc2))), 0)
* mapq <- mapq - diff_top * top_coeff
* diff_bot <- ifelse(sc2 != horiz, abs(abs(sc2) - abs(horiz)), 0)
* mapq <- mapq - diff_bot * bot_coeff
* mapq <- round(pmax(0, pmin(mx, mapq)))
* tab$mapq <- mapq
*/
class BowtieMapq : public Mapq {
public:
BowtieMapq(
const SimpleFunc& scoreMin,
const Scoring& sc) :
scoreMin_(scoreMin),
sc_(sc)
{ }
virtual ~BowtieMapq() { }
/**
* Given an AlnSetSumm, return a mapping quality calculated.
*/
virtual TMapq mapq(
const AlnSetSumm& s,
const AlnFlags& flags,
bool mate1,
size_t rdlen,
size_t ordlen,
char *inps) // put string representation of inputs here
const
{
bool hasSecbest = VALID_AL_SCORE(s.secbest(mate1));
if(!flags.canMax() && !s.exhausted(mate1) && !hasSecbest) {
return 255;
}
TAlScore scPer = (TAlScore)sc_.perfectScore(rdlen);
TAlScore scMin = scoreMin_.f<TAlScore>((float)rdlen);
TAlScore secbest = scMin-1;
TAlScore diff = (scPer - scMin);
float sixth_2 = (float)(scPer - diff * (double)0.1666f * 2);
float sixth_3 = (float)(scPer - diff * (double)0.1666f * 3);
TMapq ret = 0;
TAlScore best = s.best(mate1).score();
if(!hasSecbest) {
// Top third?
if(best >= sixth_2) {
ret = 37;
}
// Otherwise in top half?
else if(best >= sixth_3) {
ret = 25;
}
// Otherwise has no second-best?
else {
ret = 10;
}
} else {
secbest = s.secbest(mate1).score();
TAlScore bestdiff = abs(abs(static_cast<long>(best))-abs(static_cast<long>(secbest)));
if(bestdiff >= diff * 0.1666 * 5) {
ret = 6;
} else if(bestdiff >= diff * 0.1666 * 4) {
ret = 5;
} else if(bestdiff >= diff * 0.1666 * 3) {
ret = 4;
} else if(bestdiff >= diff * 0.1666 * 2) {
ret = 3;
} else if(bestdiff >= diff * 0.1666 * 1) {
ret = 2;
} else {
ret = 1;
}
}
// Note: modifications to inps must be synchronized
//if(inps != NULL) {
// inps = itoa10<TAlScore>(best, inps);
// *inps++ = ',';
// inps = itoa10<TAlScore>(secbest, inps);
// *inps++ = ',';
// inps = itoa10<TMapq>(ret, inps);
//}
return ret;
}
protected:
SimpleFunc scoreMin_;
const Scoring& sc_;
};
/**
* Create and return new MAPQ calculating object.
*/
static inline Mapq *new_mapq(
int version,
const SimpleFunc& scoreMin,
const Scoring& sc)
{
if(version == 3) {
return new BowtieMapq3(scoreMin, sc);
} else if(version == 2) {
return new BowtieMapq2(scoreMin, sc);
} else {
return new BowtieMapq(scoreMin, sc);
}
}
#endif /*ndef UNIQUE_H_*/
|