| 1 |
#ifndef SEQLIB_BAM_RECORD_H |
| 2 |
#define SEQLIB_BAM_RECORD_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
#include <vector> |
| 7 |
#include <iostream> |
| 8 |
#include <sstream> |
| 9 |
#include <cassert> |
| 10 |
#include <algorithm> |
| 11 |
|
| 12 |
extern "C" { |
| •13 |
#include <htslib/hts.h> |
| 14 |
#include <htslib/sam.h> |
| 15 |
#include <htslib/bgzf.h> |
| 16 |
#include <htslib/kstring.h> |
| 17 |
#include <htslib/faidx.h> |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
#include "SeqLib/SeqLibUtils.h" |
| 22 |
#include "SeqLib/GenomicRegion.h" |
| 23 |
#include "SeqLib/UnalignedSequence.h" |
| 24 |
|
| 25 |
static const char BASES[16] = {' ', 'A', 'C', ' ', |
| 26 |
'G', ' ', ' ', ' ', |
| 27 |
'T', ' ', ' ', ' ', |
| 28 |
' ', ' ', ' ', 'N'}; |
| 29 |
|
| 30 |
static std::string cigar_delimiters = "MIDNSHPX"; |
| 31 |
|
| 32 |
static const uint8_t CIGTAB[255] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 33 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 34 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 35 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 36 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 37 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 38 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 39 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 40 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
| 41 |
#define FRORIENTATION 0 |
| 42 |
#define FFORIENTATION 1 |
| 43 |
#define RFORIENTATION 2 |
| 44 |
#define RRORIENTATION 3 |
| 45 |
#define UDORIENTATION 4 |
| 46 |
|
| 47 |
namespace SeqLib { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class CigarField { |
| 54 |
|
| 55 |
friend class Cigar; |
| 56 |
|
| 57 |
public: |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
CigarField(char t, uint32_t l); |
| 65 |
|
| 66 |
|
| 67 |
CigarField(uint32_t f) : data(f) {} |
| 68 |
|
| 69 |
|
| 70 |
inline uint32_t raw() const { return data; } |
| 71 |
|
| 72 |
|
| 73 |
friend std::ostream& operator<<(std::ostream& out, const CigarField& c); |
| 74 |
|
| 75 |
|
| 76 |
inline char Type() const { return bam_cigar_opchr(data); } |
| 77 |
|
| 78 |
|
| 79 |
inline uint8_t RawType() const { return bam_cigar_op(data); } |
| 80 |
|
| 81 |
|
| 82 |
inline uint32_t Length() const { return bam_cigar_oplen(data); } |
| 83 |
|
| 84 |
|
| 85 |
inline bool ConsumesReference() const { return bam_cigar_type(bam_cigar_op(data))&2; } |
| 86 |
|
| 87 |
|
| 88 |
inline bool ConsumesQuery() const { return bam_cigar_type(bam_cigar_op(data))&1; } |
| 89 |
|
| 90 |
|
| 91 |
inline bool operator==(const CigarField& c) const { return c.Type() == Type() && c.Length() == Length(); } |
| 92 |
|
| 93 |
|
| 94 |
inline bool operator!=(const CigarField& c) const { return !(c == *this); } |
| 95 |
|
| 96 |
private: |
| 97 |
|
| 98 |
|
| 99 |
uint32_t data; |
| 100 |
|
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
class Cigar { |
| 108 |
|
| 109 |
public: |
| 110 |
|
| 111 |
|
| 112 |
Cigar() {} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
Cigar(const std::string& cig); |
| 118 |
|
| 119 |
typedef std::vector<CigarField>::iterator iterator; |
| 120 |
typedef std::vector<CigarField>::const_iterator const_iterator; |
| 121 |
iterator begin() { return m_data.begin(); } |
| 122 |
iterator end() { return m_data.end(); } |
| 123 |
const_iterator begin() const { return m_data.begin(); } |
| 124 |
const_iterator end() const { return m_data.end(); } |
| 125 |
|
| 126 |
|
| 127 |
inline const CigarField& back() const { return m_data.back(); } |
| 128 |
|
| 129 |
|
| 130 |
inline CigarField& back() { return m_data.back(); } |
| 131 |
|
| 132 |
|
| 133 |
inline const CigarField& front() const { return m_data.front(); } |
| 134 |
|
| 135 |
|
| 136 |
inline CigarField& front() { return m_data.front(); } |
| 137 |
|
| 138 |
|
| 139 |
inline size_t size() const { return m_data.size(); } |
| 140 |
|
| 141 |
|
| 142 |
inline CigarField& operator[](size_t i) { return m_data[i]; } |
| 143 |
|
| 144 |
|
| 145 |
const CigarField& operator[](size_t i) const { return m_data[i]; } |
| 146 |
|
| 147 |
|
| 148 |
inline int TotalLength() const { |
| 149 |
int t = 0; |
| 150 |
for (Cigar::const_iterator c = m_data.begin(); c != m_data.end(); ++c) |
| 151 |
|
| 152 |
t += c->Length(); |
| 153 |
return t; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
inline int NumQueryConsumed() const { |
| 158 |
int out = 0; |
| 159 |
for (Cigar::const_iterator c = m_data.begin(); c != m_data.end(); ++c) |
| 160 |
if (c->ConsumesQuery()) |
| 161 |
out += c->Length(); |
| 162 |
return out; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
inline int NumReferenceConsumed() const { |
| 167 |
int out = 0; |
| 168 |
|
| 169 |
for (Cigar::const_iterator c = m_data.begin(); c != m_data.end(); ++c) |
| 170 |
if (c->ConsumesReference()) |
| 171 |
out += c->Length(); |
| 172 |
return out; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
inline void add(const CigarField& c) { |
| 177 |
m_data.push_back(c); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
bool operator==(const Cigar& c) const; |
| 182 |
|
| 183 |
|
| 184 |
bool operator!=(const Cigar& c) const { return !(c == *this); } |
| 185 |
|
| 186 |
|
| 187 |
friend std::ostream& operator<<(std::ostream& out, const Cigar& c); |
| 188 |
|
| 189 |
|
| 190 |
private: |
| 191 |
|
| 192 |
std::vector<CigarField> m_data; |
| 193 |
|
| 194 |
}; |
| 195 |
|
| 196 |
typedef SeqHashMap<std::string, size_t> CigarMap; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
class BamRecord { |
| 207 |
|
| 208 |
friend class BLATWraper; |
| 209 |
friend class BWAWrapper; |
| 210 |
|
| 211 |
public: |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
BamRecord(const std::string& name, const std::string& seq, const GenomicRegion * gr, const Cigar& cig); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
void init(); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
bool isEmpty() const { return !b; } |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
void assign(bam1_t* a); |
| 240 |
|
| 241 |
|
| 242 |
BamRecord() {} |
| 243 |
|
| 244 |
|
| 245 |
inline bool ReverseFlag() const { return b ? ((b->core.flag&BAM_FREVERSE) != 0) : false; } |
| 246 |
|
| 247 |
|
| 248 |
inline bool MateReverseFlag() const { return b ? ((b->core.flag&BAM_FMREVERSE) != 0) : false; } |
| 249 |
|
| 250 |
|
| 251 |
inline bool Interchromosomal() const { return b ? b->core.tid != b->core.mtid && PairMappedFlag() : false; } |
| 252 |
|
| 253 |
|
| 254 |
inline bool DuplicateFlag() const { return b ? ((b->core.flag&BAM_FDUP) != 0) : false; } |
| 255 |
|
| 256 |
|
| 257 |
inline bool SecondaryFlag() const { return b ? ((b->core.flag&BAM_FSECONDARY) != 0) : false; } |
| 258 |
|
| 259 |
|
| 260 |
inline bool PairedFlag() const { return b ? ((b->core.flag&BAM_FPAIRED) != 0) : false; } |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
inline int PairOrientation() const { |
| 271 |
if (!PairMappedFlag()) |
| 272 |
return UDORIENTATION; |
| 273 |
else if ( (!ReverseFlag() && Position() <= MatePosition() && MateReverseFlag() ) || |
| 274 |
(ReverseFlag() && Position() >= MatePosition() && !MateReverseFlag() ) ) |
| 275 |
return FRORIENTATION; |
| 276 |
else if (!ReverseFlag() && !MateReverseFlag()) |
| 277 |
return FFORIENTATION; |
| 278 |
else if (ReverseFlag() && MateReverseFlag()) |
| 279 |
return RRORIENTATION; |
| 280 |
else if ( ( ReverseFlag() && Position() < MatePosition() && !MateReverseFlag()) || |
| 281 |
(!ReverseFlag() && Position() > MatePosition() && MateReverseFlag())) |
| 282 |
return RFORIENTATION; |
| 283 |
assert(false); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
inline bool QCFailFlag() const { return b ? ((b->core.flag&BAM_FQCFAIL) != 0) : false; } |
| 288 |
|
| 289 |
|
| 290 |
inline bool SupplementaryFlag() const { return b ? ((b->core.flag&BAM_FSUPPLEMENTARY) != 0) : false; } |
| 291 |
|
| 292 |
|
| 293 |
inline bool MappedFlag() const { return b ? ((b->core.flag&BAM_FUNMAP) == 0) : false; } |
| 294 |
|
| 295 |
|
| 296 |
inline bool MateMappedFlag() const { return b ? ((b->core.flag&BAM_FMUNMAP) == 0) : false; } |
| 297 |
|
| 298 |
|
| 299 |
inline bool PairMappedFlag() const { return b ? (!(b->core.flag&BAM_FMUNMAP) && !(b->core.flag&BAM_FUNMAP) && (b->core.flag&BAM_FPAIRED) ) : false; } |
| 300 |
|
| 301 |
|
| 302 |
inline bool ProperPair() const { return b ? (b->core.flag&BAM_FPROPER_PAIR) : false;} |
| 303 |
|
| 304 |
|
| 305 |
inline bool ProperOrientation() const { |
| 306 |
if (!b) |
| 307 |
return false; |
| 308 |
|
| 309 |
|
| 310 |
if (b->core.tid != b->core.mtid) |
| 311 |
return false; |
| 312 |
|
| 313 |
|
| 314 |
if (b->core.pos < b->core.mpos) { |
| 315 |
return (b->core.flag&BAM_FREVERSE) == 0 && (b->core.flag&BAM_FMREVERSE) != 0 ? true : false; |
| 316 |
} else { |
| 317 |
return (b->core.flag&BAM_FREVERSE) == 0 && (b->core.flag&BAM_FMREVERSE) != 0 ? false : true; |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
int32_t CountNBases() const; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
void QualityTrimmedSequence(int32_t qualTrim, int32_t& startpoint, int32_t& endpoint) const; |
| 332 |
|
| 333 |
|
| 334 |
std::string QualitySequence() const; |
| 335 |
|
| 336 |
|
| 337 |
inline int32_t Position() const { return b ? b->core.pos : -1; } |
| 338 |
|
| 339 |
|
| 340 |
int32_t PositionWithSClips() const; |
| 341 |
|
| 342 |
|
| 343 |
inline int32_t MatePosition() const { return b ? b->core.mpos: -1; } |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
int32_t CountBWASecondaryAlignments() const; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
int32_t CountBWAChimericAlignments() const; |
| 357 |
|
| 358 |
|
| 359 |
int32_t PositionEnd() const; |
| 360 |
|
| 361 |
|
| 362 |
int32_t PositionEndWithSClips() const; |
| 363 |
|
| 364 |
|
| 365 |
int32_t PositionEndMate() const; |
| 366 |
|
| 367 |
|
| 368 |
inline int32_t ChrID() const { return b ? b->core.tid : -1; } |
| 369 |
|
| 370 |
|
| 371 |
inline int32_t MateChrID() const { return b ? b->core.mtid : -1; } |
| 372 |
|
| 373 |
|
| 374 |
inline int32_t MapQuality() const { return b ? b->core.qual : -1; } |
| 375 |
|
| 376 |
|
| 377 |
inline void SetQCFail(bool f) { |
| 378 |
if (f) |
| 379 |
b->core.flag |= BAM_FQCFAIL; |
| 380 |
else |
| 381 |
b->core.flag &= ~BAM_FQCFAIL; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
inline void SetMapQuality(int32_t m) { if (b) b->core.qual = m; } |
| 386 |
|
| 387 |
|
| 388 |
inline void SetChrID(int32_t i) { b->core.tid = i; } |
| 389 |
|
| 390 |
|
| 391 |
inline void SetChrIDMate(int32_t i) { b->core.mtid = i; } |
| 392 |
|
| 393 |
|
| 394 |
inline void SetPositionMate(int32_t i) { b->core.mpos = i; } |
| 395 |
|
| 396 |
|
| 397 |
inline void SetPairMappedFlag(bool f) { |
| 398 |
if (f) |
| 399 |
b->core.flag |= BAM_FPAIRED; |
| 400 |
else |
| 401 |
b->core.flag &= ~BAM_FPAIRED; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
inline void SetMateReverseFlag(bool f) { |
| 406 |
if (f) |
| 407 |
b->core.flag |= BAM_FMREVERSE; |
| 408 |
else |
| 409 |
b->core.flag &= ~BAM_FMREVERSE; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
inline int32_t CigarSize() const { return b ? b->core.n_cigar : -1; } |
| 414 |
|
| 415 |
|
| 416 |
inline bool FirstFlag() const { return (b->core.flag&BAM_FREAD1); } |
| 417 |
|
| 418 |
|
| 419 |
inline std::string Qname() const { return std::string(bam_get_qname(b)); } |
| 420 |
|
| 421 |
|
| 422 |
inline char* QnameChar() const { return bam_get_qname(b); } |
| 423 |
|
| 424 |
|
| 425 |
inline uint32_t AlignmentFlag() const { return b->core.flag; } |
| 426 |
|
| 427 |
|
| 428 |
inline int32_t InsertSize() const { return b->core.isize; } |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
inline std::string ParseReadGroup() const { |
| 434 |
|
| 435 |
|
| 436 |
std::string RG; |
| 437 |
if (GetZTag("RG", RG)) |
| 438 |
return RG; |
| 439 |
|
| 440 |
|
| 441 |
std::string qn = Qname(); |
| 442 |
size_t posr = qn.find(":", 0); |
| 443 |
return (posr != std::string::npos) ? qn.substr(0, posr) : "NA"; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
inline int32_t FullInsertSize() const { |
| 448 |
|
| 449 |
if (b->core.tid != b->core.mtid || !PairMappedFlag()) |
| 450 |
return 0; |
| 451 |
|
| 452 |
return std::abs(b->core.pos - b->core.mpos) + GetCigar().NumQueryConsumed(); |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
inline int32_t Length() const { return b->core.l_qseq; } |
| 458 |
|
| 459 |
|
| 460 |
void SmartAddTag(const std::string& tag, const std::string& val); |
| 461 |
|
| 462 |
|
| 463 |
void SetQname(const std::string& n); |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
void SetQualities(const std::string& n, int offset); |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
void SetSequence(const std::string& seq); |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
void SetCigar(const Cigar& c); |
| 484 |
|
| 485 |
|
| 486 |
friend std::ostream& operator<<(std::ostream& out, const BamRecord &r); |
| 487 |
|
| 488 |
|
| 489 |
GenomicRegion AsGenomicRegion() const; |
| 490 |
|
| 491 |
|
| 492 |
GenomicRegion AsGenomicRegionMate() const; |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
inline int NumAlignedBases() const { |
| 505 |
int out = 0; |
| 506 |
uint32_t* c = bam_get_cigar(b); |
| 507 |
for (size_t i = 0; i < b->core.n_cigar; i++) |
| 508 |
if (bam_cigar_opchr(c[i]) == 'M' || |
| 509 |
bam_cigar_opchr(c[i]) == 'I' || |
| 510 |
bam_cigar_opchr(c[i]) == '=' || |
| 511 |
bam_cigar_opchr(c[i]) == 'X' || |
| 512 |
bam_cigar_opchr(c[i]) == 'D') |
| 513 |
out += bam_cigar_oplen(c[i]); |
| 514 |
return out; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
inline uint32_t MaxInsertionBases() const { |
| 520 |
uint32_t* c = bam_get_cigar(b); |
| 521 |
uint32_t imax = 0; |
| 522 |
for (size_t i = 0; i < b->core.n_cigar; i++) |
| 523 |
if (bam_cigar_opchr(c[i]) == 'I') |
| 524 |
imax = std::max(bam_cigar_oplen(c[i]), imax); |
| 525 |
return imax; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
inline uint32_t MaxDeletionBases() const { |
| 530 |
uint32_t* c = bam_get_cigar(b); |
| 531 |
uint32_t dmax = 0; |
| 532 |
for (size_t i = 0; i < b->core.n_cigar; i++) |
| 533 |
if (bam_cigar_opchr(c[i]) == 'D') |
| 534 |
dmax = std::max(bam_cigar_oplen(c[i]), dmax); |
| 535 |
return dmax; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
inline uint32_t NumMatchBases() const { |
| 540 |
uint32_t* c = bam_get_cigar(b); |
| 541 |
uint32_t dmax = 0; |
| 542 |
for (size_t i = 0; i < b->core.n_cigar; i++) |
| 543 |
if (bam_cigar_opchr(c[i]) == 'M') |
| 544 |
dmax += bam_cigar_oplen(c[i]); |
| 545 |
return dmax; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
Cigar GetCigar() const { |
| 551 |
uint32_t* c = bam_get_cigar(b); |
| 552 |
Cigar cig; |
| 553 |
for (size_t k = 0; k < b->core.n_cigar; ++k) { |
| 554 |
cig.add(CigarField(c[k])); |
| 555 |
} |
| 556 |
return cig; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
Cigar GetReverseCigar() const { |
| 561 |
uint32_t* c = bam_get_cigar(b); |
| 562 |
Cigar cig; |
| 563 |
for (int k = b->core.n_cigar - 1; k >= 0; --k) |
| 564 |
cig.add(CigarField(c[k])); |
| 565 |
return cig; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
void ClearSeqQualAndTags(); |
| 572 |
|
| 573 |
|
| 574 |
std::string Sequence() const; |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
double MeanPhred() const; |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
BamRecord(const std::string& name, const std::string& seq, const std::string& ref, const GenomicRegion * gr); |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
inline std::string Qualities(int offset = 33) const { |
| 594 |
uint8_t * p = bam_get_qual(b); |
| 595 |
if (!p) |
| 596 |
return std::string(); |
| 597 |
|
| 598 |
|
| 599 |
std::string out(b->core.l_qseq, ' '); |
| 600 |
for (int32_t i = 0; i < b->core.l_qseq; ++i) |
| 601 |
out[i] = (char)(p[i] + offset); |
| 602 |
return out; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
inline int32_t AlignmentPositionReverse() const { |
| 609 |
uint32_t* c = bam_get_cigar(b); |
| 610 |
int32_t p = 0; |
| 611 |
for (int32_t i = b->core.n_cigar - 1; i >= 0; --i) { |
| 612 |
if ( (bam_cigar_opchr(c[i]) == 'S') || (bam_cigar_opchr(c[i]) == 'H')) |
| 613 |
p += bam_cigar_oplen(c[i]); |
| 614 |
else |
| 615 |
break; |
| 616 |
} |
| 617 |
return p; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
inline int32_t AlignmentEndPositionReverse() const { |
| 624 |
uint32_t* c = bam_get_cigar(b); |
| 625 |
int32_t p = 0; |
| 626 |
for (size_t i = 0; i < b->core.n_cigar; ++i) { |
| 627 |
if ( (bam_cigar_opchr(c[i]) == 'S') || (bam_cigar_opchr(c[i]) == 'H')) |
| 628 |
p += bam_cigar_oplen(c[i]); |
| 629 |
else |
| 630 |
break; |
| 631 |
} |
| 632 |
return (b->core.l_qseq - p); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
inline int32_t AlignmentPosition() const { |
| 639 |
uint32_t* c = bam_get_cigar(b); |
| 640 |
int32_t p = 0; |
| 641 |
for (size_t i = 0; i < b->core.n_cigar; ++i) { |
| 642 |
if (bam_cigar_opchr(c[i]) == 'S') |
| 643 |
p += bam_cigar_oplen(c[i]); |
| 644 |
else if (bam_cigar_opchr(c[i]) != 'H') |
| 645 |
break; |
| 646 |
} |
| 647 |
return p; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
inline int32_t AlignmentEndPosition() const { |
| 653 |
uint32_t* c = bam_get_cigar(b); |
| 654 |
int32_t p = 0; |
| 655 |
for (int32_t i = b->core.n_cigar - 1; i >= 0; --i) { |
| 656 |
if ( (bam_cigar_opchr(c[i]) == 'S') || (bam_cigar_opchr(c[i]) == 'H')) |
| 657 |
p += bam_cigar_oplen(c[i]); |
| 658 |
else |
| 659 |
break; |
| 660 |
} |
| 661 |
return (b->core.l_qseq - p); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
inline int32_t NumSoftClip() const { |
| 666 |
int32_t p = 0; |
| 667 |
uint32_t* c = bam_get_cigar(b); |
| 668 |
for (size_t i = 0; i < b->core.n_cigar; ++i) |
| 669 |
if (bam_cigar_opchr(c[i]) == 'S') |
| 670 |
p += bam_cigar_oplen(c[i]); |
| 671 |
return p; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
inline int32_t NumHardClip() const { |
| 676 |
int32_t p = 0; |
| 677 |
uint32_t* c = bam_get_cigar(b); |
| 678 |
for (size_t i = 0; i < b->core.n_cigar; ++i) |
| 679 |
if (bam_cigar_opchr(c[i]) == 'H') |
| 680 |
p += bam_cigar_oplen(c[i]); |
| 681 |
return p; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
inline int32_t NumClip() const { |
| 687 |
int32_t p = 0; |
| 688 |
uint32_t* c = bam_get_cigar(b); |
| 689 |
for (size_t i = 0; i < b->core.n_cigar; ++i) |
| 690 |
if ( (bam_cigar_opchr(c[i]) == 'S') || (bam_cigar_opchr(c[i]) == 'H') ) |
| 691 |
p += bam_cigar_oplen(c[i]); |
| 692 |
return p; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
bool GetZTag(const std::string& tag, std::string& s) const; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
bool GetTag(const std::string& tag, std::string& s) const; |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
std::vector<int> GetSmartIntTag(const std::string& tag) const; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
std::vector<double> GetSmartDoubleTag(const std::string& tag) const; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
std::vector<std::string> GetSmartStringTag(const std::string& tag) const; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
inline bool GetIntTag(const std::string& tag, int32_t& t) const { |
| 741 |
uint8_t* p = bam_aux_get(b.get(),tag.c_str()); |
| 742 |
if (!p) |
| 743 |
return false; |
| 744 |
t = bam_aux2i(p); |
| 745 |
int type = *p++; |
| 746 |
if (!(type == 'i' || type == 'C' || type=='S' || type=='s' || type =='I' || type=='c')) |
| 747 |
return false; |
| 748 |
|
| 749 |
return true; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
inline bool GetFloatTag(const std::string& tag, float& t) const { |
| 758 |
uint8_t* p = bam_aux_get(b.get(),tag.c_str()); |
| 759 |
if (!p) |
| 760 |
return false; |
| 761 |
|
| 762 |
t = bam_aux2f(p); |
| 763 |
int type = *p; |
| 764 |
type = *p++; |
| 765 |
if (!(type == 'f' || type == 'd')) |
| 766 |
return false; |
| 767 |
|
| 768 |
return true; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
void AddZTag(std::string tag, std::string val); |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
inline void AddIntTag(const std::string& tag, int32_t val) { |
| 782 |
bam_aux_append(b.get(), tag.data(), 'i', 4, (uint8_t*)&val); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
inline void SetID(int32_t id) { |
| 789 |
b->core.tid = id; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
inline void SetPosition(int32_t pos) { |
| 796 |
b->core.pos = pos; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
inline std::string CigarString() const { |
| 802 |
std::stringstream cig; |
| 803 |
uint32_t* c = bam_get_cigar(b); |
| 804 |
for (size_t k = 0; k < b->core.n_cigar; ++k) |
| 805 |
cig << bam_cigar_oplen(c[k]) << "MIDNSHP=XB"[c[k]&BAM_CIGAR_MASK]; |
| 806 |
return cig.str(); |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
inline std::string ChrName() const { |
| 816 |
std::stringstream ss; |
| 817 |
ss << (b->core.tid + 1); |
| 818 |
|
| 819 |
return ss.str(); |
| 820 |
|
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
inline std::string ChrName(const SeqLib::BamHeader& h) const { |
| 829 |
if (b->core.tid < 0) |
| 830 |
return std::string(); |
| 831 |
|
| 832 |
if (!h.isEmpty()) |
| 833 |
return h.IDtoName(b->core.tid); |
| 834 |
|
| 835 |
|
| 836 |
std::stringstream ss; |
| 837 |
ss << b->core.tid; |
| 838 |
|
| 839 |
|
| 840 |
return ss.str(); |
| 841 |
|
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
inline std::string Brief() const { |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
std::stringstream ss; |
| 851 |
ss << (b->core.tid + 1) << ":" << AddCommas(b->core.pos) << "(" << ((b->core.flag&BAM_FREVERSE) != 0 ? "+" : "-") << ")"; |
| 852 |
return ss.str(); |
| 853 |
|
| 854 |
|
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
inline std::string BriefMate() const { |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
std::stringstream ss; |
| 863 |
ss << (b->core.mtid + 1) << ":" << AddCommas(b->core.mpos) << "(" << ((b->core.flag&BAM_FMREVERSE) != 0 ? "+" : "-") << ")"; |
| 864 |
return ss.str(); |
| 865 |
|
| 866 |
|
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
inline void RemoveTag(const char* tag) { |
| 873 |
uint8_t* p = bam_aux_get(b.get(), tag); |
| 874 |
if (p) |
| 875 |
bam_aux_del(b.get(), p); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
inline void RemoveAllTags() { |
| 880 |
size_t keep = (b->core.n_cigar<<2) + b->core.l_qname + ((b->core.l_qseq + 1)>>1) + b->core.l_qseq; |
| 881 |
b->data = (uint8_t*)realloc(b->data, keep); |
| 882 |
b->l_data = keep; |
| 883 |
b->m_data = b->l_data; |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
inline bam1_t* raw() const { return b.get(); } |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
int OverlappingCoverage(const BamRecord& r) const; |
| 895 |
|
| 896 |
|
| 897 |
SeqPointer<bam1_t> shared_pointer() const { return b; } |
| 898 |
|
| 899 |
protected: |
| 900 |
|
| 901 |
SeqPointer<bam1_t> b; |
| 902 |
|
| 903 |
}; |
| 904 |
|
| 905 |
typedef std::vector<BamRecord> BamRecordVector; |
| 906 |
|
| 907 |
typedef std::vector<BamRecordVector> BamRecordClusterVector; |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
namespace BamRecordSort { |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
struct ByReadPosition |
| 916 |
{ |
| 917 |
bool operator()( const BamRecord& lx, const BamRecord& rx ) const { |
| 918 |
return (lx.ChrID() < rx.ChrID()) || (lx.ChrID() == rx.ChrID() && lx.Position() < rx.Position()); |
| 919 |
} |
| 920 |
}; |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
struct ByMatePosition |
| 925 |
{ |
| 926 |
bool operator()( const BamRecord& lx, const BamRecord& rx ) const { |
| 927 |
return (lx.MateChrID() < rx.MateChrID()) || (lx.MateChrID() == rx.MateChrID() && lx.MatePosition() < rx.MatePosition()); |
| 928 |
} |
| 929 |
}; |
| 930 |
|
| 931 |
} |
| 932 |
|
| 933 |
} |
| 934 |
#endif |