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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
/*
* Copyright 2011, Ben Langmead <blangmea@jhsph.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/>.
*/
#include <string>
#include <sys/time.h>
#include "sam.h"
#include "filebuf.h"
using namespace std;
/**
* Print a reference name in a way that doesn't violate SAM's character
* constraints. \*|[!-()+-<>-~][!-~]* (i.e. [33, 63], [65, 126])
*/
void SamConfig::printRefName(
BTString& o,
const std::string& name) const
{
size_t namelen = name.length();
for(size_t i = 0; i < namelen; i++) {
if(isspace(name[i])) {
return;
}
o.append(name[i]);
}
}
/**
* Print a reference name given a reference index.
*/
void SamConfig::printRefNameFromIndex(BTString& o, size_t i) const {
printRefName(o, refnames_[i]);
}
/**
* Print SAM header to given output buffer.
*/
void SamConfig::printHeader(
BTString& o,
const string& rgid,
const string& rgs,
bool printHd,
bool printSq,
bool printPg) const
{
if(printHd) printHdLine(o, "1.0");
if(printSq) printSqLines(o);
if(!rgid.empty()) {
o.append("@RG");
o.append(rgid.c_str());
o.append(rgs.c_str());
o.append('\n');
}
if(printPg) printPgLine(o);
}
/**
* Print the @HD header line to the given string.
*/
void SamConfig::printHdLine(BTString& o, const char *samver) const {
o.append("@HD\tVN:");
o.append(samver);
o.append("\tSO:unsorted\n");
}
/**
* Print the @SQ header lines to the given string.
*/
void SamConfig::printSqLines(BTString& o) const {
char buf[1024];
for(size_t i = 0; i < refnames_.size(); i++) {
o.append("@SQ\tSN:");
printRefName(o, refnames_[i]);
o.append("\tLN:");
itoa10<size_t>(reflens_[i], buf);
o.append(buf);
o.append('\n');
}
}
/**
* Print the @PG header line to the given string.
*/
void SamConfig::printPgLine(BTString& o) const {
o.append("@PG\tID:");
o.append(pg_id_.c_str());
o.append("\tPN:");
o.append(pg_pn_.c_str());
o.append("\tVN:");
o.append(pg_vn_.c_str());
o.append('\n');
}
#define WRITE_SEP() { \
if(!first) o.append('\t'); \
first = false; \
}
/**
* Print the optional flags to the given string.
*/
void SamConfig::printAlignedOptFlags(
BTString& o, // output buffer
bool first, // first opt flag printed is first overall?
const Read& rd, // the read
AlnRes& res, // individual alignment result
const AlnFlags& flags, // alignment flags
const AlnSetSumm& summ, // summary of alignments for this read
const SeedAlSumm& ssm, // seed alignment summary
const PerReadMetrics& prm, // per-read metrics
const char *mapqInp) // inputs to MAPQ calculation
const
{
char buf[1024];
if(print_as_) {
// AS:i: Alignment score generated by aligner
itoa10<TAlScore>(res.score().score(), buf);
WRITE_SEP();
o.append("AS:i:");
o.append(buf);
}
if(print_xs_) {
// XS:i: Suboptimal alignment score
AlnScore sc = summ.secbestMate(rd.mate < 2);
if(sc.valid()) {
itoa10<TAlScore>(sc.score(), buf);
WRITE_SEP();
o.append("XS:i:");
o.append(buf);
}
}
if(print_xn_) {
// XN:i: Number of ambiguous bases in the referenece
itoa10<size_t>(res.refNs(), buf);
WRITE_SEP();
o.append("XN:i:");
o.append(buf);
}
if(print_x0_) {
// X0:i: Number of best hits
}
if(print_x1_) {
// X1:i: Number of sub-optimal best hits
}
size_t num_mm = 0;
size_t num_go = 0;
size_t num_gx = 0;
for(size_t i = 0; i < res.ned().size(); i++) {
if(res.ned()[i].isMismatch()) {
num_mm++;
} else if(res.ned()[i].isReadGap()) {
num_go++;
num_gx++;
while(i < res.ned().size()-1 &&
res.ned()[i+1].pos == res.ned()[i].pos &&
res.ned()[i+1].isReadGap())
{
i++;
num_gx++;
}
} else if(res.ned()[i].isRefGap()) {
num_go++;
num_gx++;
while(i < res.ned().size()-1 &&
res.ned()[i+1].pos == res.ned()[i].pos+1 &&
res.ned()[i+1].isRefGap())
{
i++;
num_gx++;
}
}
}
if(print_xm_) {
// XM:i: Number of mismatches in the alignment
itoa10<size_t>(num_mm, buf);
WRITE_SEP();
o.append("XM:i:");
o.append(buf);
}
if(print_xo_) {
// XO:i: Number of gap opens
itoa10<size_t>(num_go, buf);
WRITE_SEP();
o.append("XO:i:");
o.append(buf);
}
if(print_xg_) {
// XG:i: Number of gap extensions (incl. opens)
itoa10<size_t>(num_gx, buf);
WRITE_SEP();
o.append("XG:i:");
o.append(buf);
}
if(print_nm_) {
// NM:i: Edit dist. to the ref, Ns count, clipping doesn't
itoa10<size_t>(res.ned().size(), buf);
WRITE_SEP();
o.append("NM:i:");
o.append(buf);
}
if(print_md_) {
// MD:Z: String for mms. [0-9]+(([A-Z]|\^[A-Z]+)[0-9]+)*2
WRITE_SEP();
o.append("MD:Z:");
res.printMD(
res.mdop, // MD operations
res.mdch, // MD chars
res.mdrun, // MD run lengths
&o, // output buffer
NULL); // no char buffer
}
if(print_ys_ && summ.paired()) {
// AS:i: Alignment score generated by aligner
assert(res.oscore().valid());
itoa10<TAlScore>(res.oscore().score(), buf);
WRITE_SEP();
o.append("YS:i:");
o.append(buf);
}
if(print_zs_) {
// ZS:i: Pseudo-random seed for read
itoa10<uint32_t>(rd.seed, buf);
WRITE_SEP();
o.append("ZS:i:");
o.append(buf);
}
if(print_yt_) {
// YT:Z: String representing alignment type
WRITE_SEP();
flags.printYT(o);
}
if(print_yp_ && flags.partOfPair() && flags.canMax()) {
// YP:i: Read was repetitive when aligned paired?
WRITE_SEP();
flags.printYP(o);
}
if(print_ym_ && flags.canMax() && (flags.isMixedMode() || !flags.partOfPair())) {
// YM:i: Read was repetitive when aligned unpaired?
WRITE_SEP();
flags.printYM(o);
}
if(print_yf_ && flags.filtered()) {
// YF:i: Read was filtered?
first = flags.printYF(o, first) && first;
}
if(print_yi_) {
// Print MAPQ calibration info
if(mapqInp[0] != '\0') {
// YI:i: Suboptimal alignment score
WRITE_SEP();
o.append("YI:Z:");
o.append(mapqInp);
}
}
if(flags.partOfPair() && print_zp_) {
// ZP:i: Score of best concordant paired-end alignment
WRITE_SEP();
o.append("ZP:Z:");
if(summ.bestPaired().valid()) {
itoa10<TAlScore>(summ.bestPaired().score(), buf);
o.append(buf);
} else {
o.append("NA");
}
// Zp:i: Second-best concordant paired-end alignment score
WRITE_SEP();
o.append("Zp:Z:");
if(summ.secbestPaired().valid()) {
itoa10<TAlScore>(summ.secbestPaired().score(), buf);
o.append(buf);
} else {
o.append("NA");
}
}
if(print_zu_) {
// ZU:i: Score of best unpaired alignment
AlnScore best = (rd.mate <= 1 ? summ.best1() : summ.best2());
AlnScore secbest = (rd.mate <= 1 ? summ.secbest1() : summ.secbest2());
WRITE_SEP();
o.append("ZU:Z:");
if(best.valid()) {
itoa10<TAlScore>(best.score(), buf);
o.append(buf);
} else {
o.append("NA");
}
// Zu:i: Score of second-best unpaired alignment
WRITE_SEP();
o.append("Zu:Z:");
if(secbest.valid()) {
itoa10<TAlScore>(secbest.score(), buf);
o.append(buf);
} else {
o.append("NA");
}
}
if(!rgs_.empty()) {
WRITE_SEP();
o.append(rgs_.c_str());
}
if(print_xt_) {
// XT:i: Timing
WRITE_SEP();
struct timeval tv_end;
struct timezone tz_end;
gettimeofday(&tv_end, &tz_end);
size_t total_usecs =
(tv_end.tv_sec - prm.tv_beg.tv_sec) * 1000000 +
(tv_end.tv_usec - prm.tv_beg.tv_usec);
itoa10<size_t>(total_usecs, buf);
o.append("XT:i:");
o.append(buf);
}
if(print_xd_) {
// XD:i: Extend DPs
WRITE_SEP();
itoa10<uint64_t>(prm.nExDps, buf);
o.append("XD:i:");
o.append(buf);
// Xd:i: Mate DPs
WRITE_SEP();
itoa10<uint64_t>(prm.nMateDps, buf);
o.append("Xd:i:");
o.append(buf);
}
if(print_xu_) {
// XU:i: Extend ungapped tries
WRITE_SEP();
itoa10<uint64_t>(prm.nExUgs, buf);
o.append("XU:i:");
o.append(buf);
// Xu:i: Mate ungapped tries
WRITE_SEP();
itoa10<uint64_t>(prm.nMateUgs, buf);
o.append("Xu:i:");
o.append(buf);
}
if(print_ye_) {
// YE:i: Streak of failed DPs at end
WRITE_SEP();
itoa10<uint64_t>(prm.nDpFail, buf);
o.append("YE:i:");
o.append(buf);
// Ye:i: Streak of failed ungaps at end
WRITE_SEP();
itoa10<uint64_t>(prm.nUgFail, buf);
o.append("Ye:i:");
o.append(buf);
}
if(print_yl_) {
// YL:i: Longest streak of failed DPs
WRITE_SEP();
itoa10<uint64_t>(prm.nDpFailStreak, buf);
o.append("YL:i:");
o.append(buf);
// Yl:i: Longest streak of failed ungaps
WRITE_SEP();
itoa10<uint64_t>(prm.nUgFailStreak, buf);
o.append("Yl:i:");
o.append(buf);
}
if(print_yu_) {
// YU:i: Index of last succesful DP
WRITE_SEP();
itoa10<uint64_t>(prm.nDpLastSucc, buf);
o.append("YU:i:");
o.append(buf);
// Yu:i: Index of last succesful DP
WRITE_SEP();
itoa10<uint64_t>(prm.nUgLastSucc, buf);
o.append("Yu:i:");
o.append(buf);
}
if(print_yr_) {
// YR:i: Redundant seed hits
WRITE_SEP();
itoa10<uint64_t>(prm.nRedundants, buf);
o.append("YR:i:");
o.append(buf);
}
if(print_zf_) {
// ZF:i: FM Index ops for seed alignment
WRITE_SEP();
itoa10<uint64_t>(prm.nSdFmops, buf);
o.append("ZF:i:");
o.append(buf);
// Zf:i: FM Index ops for offset resolution
WRITE_SEP();
itoa10<uint64_t>(prm.nExFmops, buf);
o.append("Zf:i:");
o.append(buf);
}
if(print_zi_) {
// ZI:i: Seed extend loop iterations
WRITE_SEP();
itoa10<uint64_t>(prm.nExIters, buf);
o.append("ZI:i:");
o.append(buf);
}
if(print_xr_) {
// XR:Z: Original read string
WRITE_SEP();
o.append("XR:Z:");
printOptFieldEscapedZ(o, rd.readOrigBuf);
if(!rd.qualOrigBuf.empty()) {
// Xr:Z: Original quality string
WRITE_SEP();
o.append("Xr:Z:");
printOptFieldEscapedZ(o, rd.qualOrigBuf);
}
}
}
/**
* Print the optional flags to the given string.
*/
void SamConfig::printEmptyOptFlags(
BTString& o, // output buffer
bool first, // first opt flag printed is first overall?
const Read& rd, // read
const AlnFlags& flags, // alignment flags
const AlnSetSumm& summ, // summary of alignments for this read
const SeedAlSumm& ssm, // seed alignment summary
const PerReadMetrics& prm) // per-read metrics
const
{
char buf[1024];
if(print_zs_) {
// ZS:i: Pseudo-random seed for read
itoa10<uint32_t>(rd.seed, buf);
WRITE_SEP();
o.append("ZS:i:");
o.append(buf);
}
if(print_yt_) {
// YT:Z: String representing alignment type
WRITE_SEP();
flags.printYT(o);
}
if(print_yp_ && flags.partOfPair() && flags.canMax()) {
// YP:i: Read was repetitive when aligned paired?
WRITE_SEP();
flags.printYP(o);
}
if(print_ym_ && flags.canMax() && (flags.isMixedMode() || !flags.partOfPair())) {
// YM:i: Read was repetitive when aligned unpaired?
WRITE_SEP();
flags.printYM(o);
}
if(print_yf_ && flags.filtered()) {
// YM:i: Read was repetitive when aligned unpaired?
first = flags.printYF(o, first) && first;
}
if(!rgs_.empty()) {
WRITE_SEP();
o.append(rgs_.c_str());
}
if(print_xt_) {
// XT:i: Timing
WRITE_SEP();
struct timeval tv_end;
struct timezone tz_end;
gettimeofday(&tv_end, &tz_end);
size_t total_usecs =
(tv_end.tv_sec - prm.tv_beg.tv_sec) * 1000000 +
(tv_end.tv_usec - prm.tv_beg.tv_usec);
itoa10<size_t>(total_usecs, buf);
o.append("XT:i:");
o.append(buf);
}
if(print_xd_) {
// XD:i: Extend DPs
WRITE_SEP();
itoa10<uint64_t>(prm.nExDps, buf);
o.append("XD:i:");
o.append(buf);
// Xd:i: Mate DPs
WRITE_SEP();
itoa10<uint64_t>(prm.nMateDps, buf);
o.append("Xd:i:");
o.append(buf);
}
if(print_xu_) {
// XU:i: Extend ungapped tries
WRITE_SEP();
itoa10<uint64_t>(prm.nExUgs, buf);
o.append("XU:i:");
o.append(buf);
// Xu:i: Mate ungapped tries
WRITE_SEP();
itoa10<uint64_t>(prm.nMateUgs, buf);
o.append("Xu:i:");
o.append(buf);
}
if(print_ye_) {
// YE:i: Streak of failed DPs at end
WRITE_SEP();
itoa10<uint64_t>(prm.nDpFail, buf);
o.append("YE:i:");
o.append(buf);
// Ye:i: Streak of failed ungaps at end
WRITE_SEP();
itoa10<uint64_t>(prm.nUgFail, buf);
o.append("Ye:i:");
o.append(buf);
}
if(print_yl_) {
// YL:i: Longest streak of failed DPs
WRITE_SEP();
itoa10<uint64_t>(prm.nDpFailStreak, buf);
o.append("YL:i:");
o.append(buf);
// Yl:i: Longest streak of failed ungaps
WRITE_SEP();
itoa10<uint64_t>(prm.nUgFailStreak, buf);
o.append("Yl:i:");
o.append(buf);
}
if(print_yu_) {
// YU:i: Index of last succesful DP
WRITE_SEP();
itoa10<uint64_t>(prm.nDpLastSucc, buf);
o.append("YU:i:");
o.append(buf);
// Yu:i: Index of last succesful DP
WRITE_SEP();
itoa10<uint64_t>(prm.nUgLastSucc, buf);
o.append("Yu:i:");
o.append(buf);
}
if(print_yr_) {
// YR:i: Redundant seed hits
WRITE_SEP();
itoa10<uint64_t>(prm.nRedundants, buf);
o.append("YR:i:");
o.append(buf);
}
if(print_zf_) {
// ZF:i: FM Index ops for seed alignment
WRITE_SEP();
itoa10<uint64_t>(prm.nSdFmops, buf);
o.append("ZF:i:");
o.append(buf);
// Zf:i: FM Index ops for offset resolution
WRITE_SEP();
itoa10<uint64_t>(prm.nExFmops, buf);
o.append("Zf:i:");
o.append(buf);
}
if(print_zi_) {
// ZI:i: Seed extend loop iterations
WRITE_SEP();
itoa10<uint64_t>(prm.nExIters, buf);
o.append("ZI:i:");
o.append(buf);
}
if(print_xr_) {
// XR:Z: Original read string
WRITE_SEP();
o.append("XR:Z:");
printOptFieldEscapedZ(o, rd.readOrigBuf);
if(!rd.qualOrigBuf.empty()) {
// Xr:Z: Original quality string
WRITE_SEP();
o.append("Xr:Z:");
printOptFieldEscapedZ(o, rd.qualOrigBuf);
}
}
}
|