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 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
#################################################################################
# Copyright (c) 2011-2015, Pacific Biosciences of California, Inc.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Pacific Biosciences nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY PACIFIC BIOSCIENCES AND ITS
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PACIFIC BIOSCIENCES OR
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#################################################################################
# Author: David Alexander
from functools import wraps
from bisect import bisect_right, bisect_left
from pbcore.sequence import reverseComplement
from ._BamSupport import *
from ._AlignmentMixin import AlignmentRecordMixin
import os
__all__ = [ "BamAlignment" ]
def _unrollCigar(cigar, exciseSoftClips=False):
"""
Run-length decode the cigar (input is BAM packed CIGAR, not a cigar string)
Removes hard clip ops from the output. Remove all?
"""
cigarArray = np.array(cigar, dtype=int)
hasHardClipAtLeft = cigarArray[0,0] == BAM_CHARD_CLIP
hasHardClipAtRight = cigarArray[-1,0] == BAM_CHARD_CLIP
ncigar = len(cigarArray)
x = np.s_[int(hasHardClipAtLeft) : ncigar - int(hasHardClipAtRight)]
ops = np.repeat(cigarArray[x,0], cigarArray[x,1])
if exciseSoftClips:
return ops[ops != BAM_CSOFT_CLIP]
else:
return ops
def _makeBaseFeatureAccessor(featureName):
def f(self, aligned=True, orientation="native"):
return self.baseFeature(featureName, aligned, orientation)
return f
def requiresReference(method):
@wraps(method)
def f(bamAln, *args, **kwargs):
if not bamAln.bam.isReferenceLoaded:
raise UnavailableFeature, "this feature requires loaded reference sequence"
else:
return method(bamAln, *args, **kwargs)
return f
def requiresPbi(method):
@wraps(method)
def f(bamAln, *args, **kwargs):
if not bamAln.hasPbi:
raise UnavailableFeature, "this feature requires a PacBio BAM index"
else:
return method(bamAln, *args, **kwargs)
return f
def requiresMapping(method):
@wraps(method)
def f(bamAln, *args, **kwargs):
if bamAln.isUnmapped:
raise UnavailableFeature, "this feature requires a *mapped* BAM record"
else:
return method(bamAln, *args, **kwargs)
return f
class BamAlignment(AlignmentRecordMixin):
def __init__(self, bamReader, pysamAlignedRead, rowNumber=None):
#TODO: make these __slot__
self.peer = pysamAlignedRead
self.bam = bamReader
self.rowNumber = rowNumber
self.tStart = self.peer.pos
self.tEnd = self.peer.aend
# Our terminology doesn't agree with pysam's terminology for
# "query", "read". This makes this code confusing.
if self.peer.is_reverse:
clipLeft = self.peer.rlen - self.peer.qend
clipRight = self.peer.qstart
else:
clipLeft = self.peer.qstart
clipRight = self.peer.rlen - self.peer.qend
# handle virtual qStart/qEnd for CCS READTYPE
if self.isCCS:
qs, qe = 0, self.qLen
else:
qs, qe = self.qStart, self.qEnd
# alignment start/end (aStart/aEnd)
self.aStart = qs + clipLeft
self.aEnd = qe - clipRight
# Cache of unrolled cigar, in genomic orientation
self._unrolledCigar = None
@property
def reader(self):
return self.bam
@property
def hasPbi(self):
return self.rowNumber is not None
@property
def qId(self):
return self.readGroupInfo.ID
@property
def qName(self):
return self.peer.qname
@property
def qStart(self):
if self.isCCS:
raise UnavailableFeature("No qStart for CCS READTYPE")
return self.peer.opt("qs")
@property
def qEnd(self):
if self.isCCS:
raise UnavailableFeature("No qEnd for CCS READTYPE")
return self.peer.opt("qe")
@property
def qLen(self):
return self.peer.query_length
@property
def tId(self):
return self.peer.tid
@property
def isCCS(self):
return self.readType == "CCS"
@property
def isMapped(self):
return not self.isUnmapped
@property
def isUnmapped(self):
return self.peer.is_unmapped
@property
def isReverseStrand(self):
return self.peer.is_reverse
@property
def isForwardStrand(self):
return not self.peer.is_reverse
@property
def HoleNumber(self):
return self.peer.opt("zm")
@property
def MapQV(self):
return self.peer.mapq
@requiresMapping
def clippedTo(self, refStart, refEnd):
"""
Return a new `BamAlignment` that refers to a subalignment of
this alignment, as induced by clipping to reference
coordinates `refStart` to `refEnd`.
.. warning::
This function takes time linear in the length of the alignment.
"""
assert type(self) is BamAlignment
if (refStart >= refEnd or
refStart >= self.tEnd or
refEnd <= self.tStart):
raise IndexError, "Clipping query does not overlap alignment"
# The clipping region must intersect the alignment, though it
# does not have to be contained wholly within it.
refStart = max(self.referenceStart, refStart)
refEnd = min(self.referenceEnd, refEnd)
refPositions = self.referencePositions(orientation="genomic")
readPositions = self.readPositions(orientation="genomic")
uc = self.unrolledCigar(orientation="genomic")
# Clipping positions within the alignment array
clipStart = bisect_right(refPositions, refStart) - 1
clipEnd = bisect_left(refPositions, refEnd)
tStart = refStart
tEnd = refEnd
cUc = uc[clipStart:clipEnd]
readLength = np.count_nonzero(cUc != BAM_CDEL)
if self.isForwardStrand:
aStart = readPositions[clipStart]
aEnd = aStart + readLength
else:
aEnd = readPositions[clipStart] + 1
aStart = aEnd - readLength
return ClippedBamAlignment(self, tStart, tEnd, aStart, aEnd, cUc)
@property
@requiresMapping
def referenceInfo(self):
return self.bam.referenceInfo(self.referenceId)
@property
@requiresMapping
def referenceName(self):
return self.referenceInfo.FullName
@property
def movieName(self):
return self.readGroupInfo.MovieName
@property
def readGroupInfo(self):
return self.bam.readGroupInfo(rgAsInt(self.peer.opt("RG")))
@property
def readScore(self):
"""
Return the "read score", a de novo prediction (not using any
alignment) of the accuracy (between 0 and 1) of this read.
.. note::
This capability was not available in `cmp.h5` files, so
use of this property can result in code that won't work on
legacy data.
"""
return self.peer.opt("rq")
@property
def readType(self):
return self.readGroupInfo.ReadType
@property
def scrapType(self):
if self.readType != "SCRAP":
raise ValueError, "scrapType not meaningful for non-scrap reads"
else:
return self.peer.opt("sc")
@property
def sequencingChemistry(self):
return self.readGroupInfo.SequencingChemistry
@property
def hqRegionSnr(self):
"""
Return the per-channel SNR averaged over the HQ region.
.. note::
This capability was not available in `cmp.h5` files, so
use of this property can result in code that won't work on
legacy data.
"""
return self.peer.opt("sn")
@property
def referenceId(self):
return self.tId
@property
def queryStart(self):
return self.qStart
@property
def queryEnd(self):
return self.qEnd
#TODO: provide this in cmp.h5 but throw "unsupported"
@property
def queryName(self):
return self.peer.qname
@property
@requiresMapping
def identity(self):
if self.hasPbi:
# Fast (has pbi)
if self.readLength == 0:
return 0.
else:
return 1. - float(self.nMM + self.nIns + self.nDel)/self.readLength
else:
# Slow (no pbi);
if self.readLength == 0:
return 0.
else:
x = self.transcript()
nMM = x.count("R")
nIns = x.count("I")
nDel = x.count("D")
return 1. - float(nMM + nIns + nDel)/self.readLength
@property
def mapQV(self):
return self.peer.mapq
@property
def numPasses(self):
return self.peer.opt("np")
@property
def zScore(self):
raise UnavailableFeature("No ZScore in BAM")
@property
def barcode(self):
raise Unimplemented()
@property
def barcodeName(self):
raise Unimplemented()
def transcript(self, orientation="native", style="gusfield"):
"""
A text representation of the alignment moves (see Gusfield).
This can be useful in pretty-printing an alignment.
"""
uc = self.unrolledCigar(orientation)
# MIDNSHP=X
_exoneratePlusTrans = np.fromstring("Z ZZZZ|*", dtype=np.int8)
_exonerateTrans = np.fromstring("Z ZZZZ| ", dtype=np.int8)
_cigarTrans = np.fromstring("ZIDZZZZMM", dtype=np.int8)
_gusfieldTrans = np.fromstring("ZIDZZZZMR", dtype=np.int8)
if style == "exonerate+": return _exoneratePlusTrans [uc].tostring()
elif style == "exonerate": return _exonerateTrans [uc].tostring()
elif style == "cigar": return _cigarTrans [uc].tostring()
else: return _gusfieldTrans [uc].tostring()
@requiresReference
def reference(self, aligned=True, orientation="native"):
if not (orientation == "native" or orientation == "genomic"):
raise ValueError, "Bad `orientation` value"
tSeq = self.bam.referenceFasta[self.referenceName].sequence[self.tStart:self.tEnd]
shouldRC = orientation == "native" and self.isReverseStrand
tSeqOriented = reverseComplement(tSeq) if shouldRC else tSeq
if aligned:
x = np.fromstring(tSeqOriented, dtype=np.int8)
y = self._gapifyRef(x, orientation)
return y.tostring()
else:
return tSeqOriented
@requiresMapping
def unrolledCigar(self, orientation="native"):
"""
Run-length decode the CIGAR encoding, and orient. Clipping ops are removed.
"""
if self.isUnmapped: return None
if self._unrolledCigar is None:
self._unrolledCigar = _unrollCigar(self.peer.cigar, exciseSoftClips=True)
if BAM_CMATCH in self._unrolledCigar:
raise IncompatibleFile("CIGAR op 'M' illegal in PacBio BAM files")
if (orientation == "native" and self.isReverseStrand):
return self._unrolledCigar[::-1]
else:
return self._unrolledCigar
@requiresMapping
def referencePositions(self, aligned=True, orientation="native"):
"""
Returns an array of reference positions.
If aligned is True, the array has the same length as the
alignment and referencePositions[i] = reference position of
the i'th column in the oriented alignment.
If aligned is False, the array has the same length as the read
and referencePositions[i] = reference position of the i'th
base in the oriented read.
"""
assert (aligned in (True, False) and
orientation in ("native", "genomic"))
ucOriented = self.unrolledCigar(orientation)
refNonGapMask = (ucOriented != BAM_CINS)
if self.isReverseStrand and orientation == "native":
pos = self.tEnd - 1 - np.hstack([0, np.cumsum(refNonGapMask[:-1])])
else:
pos = self.tStart + np.hstack([0, np.cumsum(refNonGapMask[:-1])])
if aligned:
return pos
else:
return pos[ucOriented != BAM_CDEL]
def readPositions(self, aligned=True, orientation="native"):
"""
Returns an array of read positions.
If aligned is True, the array has the same length as the
alignment and readPositions[i] = read position of the i'th
column in the oriented alignment.
If aligned is False, the array has the same length as the
mapped reference segment and readPositions[i] = read position
of the i'th base in the oriented reference segment.
"""
assert (aligned in (True, False) and
orientation in ("native", "genomic"))
ucOriented = self.unrolledCigar(orientation)
readNonGapMask = (ucOriented != BAM_CDEL)
if self.isReverseStrand and orientation == "genomic":
pos = self.aEnd - 1 - np.hstack([0, np.cumsum(readNonGapMask[:-1])])
else:
pos = self.aStart + np.hstack([0, np.cumsum(readNonGapMask[:-1])])
if aligned:
return pos
else:
return pos[ucOriented != BAM_CINS]
def baseFeature(self, featureName, aligned=True, orientation="native"):
"""
Retrieve the base feature as indicated.
- `aligned` : whether gaps should be inserted to reflect the alignment
- `orientation`: "native" or "genomic"
Note that this function assumes the the feature is stored in
native orientation in the file, so it is not appropriate to
use this method to fetch the read or the qual, which are
oriented genomically in the file.
"""
if not (orientation == "native" or orientation == "genomic"):
raise ValueError, "Bad `orientation` value"
if self.isUnmapped and (orientation != "native" or aligned == True):
raise UnavailableFeature, \
"Cannot get genome oriented/aligned features from unmapped BAM record"
# 0. Get the "concrete" feature name. (Example: Ipd could be
# Ipd:Frames or Ipd:CodecV1)
concreteFeatureName = self.bam._baseFeatureNameMappings[self.qId][featureName]
# 1. Extract in native orientation
tag, kind_, dtype_ = BASE_FEATURE_TAGS[concreteFeatureName]
data_ = self.peer.opt(tag)
if isinstance(data_, str):
data = np.fromstring(data_, dtype=dtype_)
else:
# This is about 300x slower than the fromstring above.
# Unless pysam exposes buffer or numpy interface,
# is is going to be very slow.
data = np.fromiter(data_, dtype=dtype_)
del data_
assert len(data) == self.peer.rlen
# 2. Decode
if kind_ == "qv":
data -= 33
elif kind_ == "codecV1":
data = codeToFrames(data)
# 3. Clip
# [s, e) delimits the range, within the query, that is in the aligned read.
# This will be determined by the soft clips actually in the file as well as those
# imposed by the clipping API here.
if self.isCCS:
s = self.aStart
e = self.aEnd
else:
s = self.aStart - self.qStart
e = self.aEnd - self.qStart
assert s >= 0 and e <= len(data)
clipped = data[s:e]
# 4. Orient
shouldReverse = self.isReverseStrand and orientation == "genomic"
if kind_ == "base":
ungapped = reverseComplementAscii(clipped) if shouldReverse else clipped
else:
ungapped = clipped[::-1] if shouldReverse else clipped
# 5. Gapify if requested
if aligned == False:
return ungapped
else:
return self._gapifyRead(ungapped, orientation)
def _gapifyRead(self, data, orientation):
return self._gapify(data, orientation, BAM_CDEL)
def _gapifyRef(self, data, orientation):
return self._gapify(data, orientation, BAM_CINS)
def _gapify(self, data, orientation, gapOp):
if self.isUnmapped: return data
# Precondition: data must already be *in* the specified orientation
if data.dtype == np.int8:
gapCode = ord("-")
else:
gapCode = data.dtype.type(-1)
uc = self.unrolledCigar(orientation=orientation)
alnData = np.repeat(np.array(gapCode, dtype=data.dtype), len(uc))
gapMask = (uc == gapOp)
alnData[~gapMask] = data
return alnData
IPD = _makeBaseFeatureAccessor("Ipd")
PulseWidth = _makeBaseFeatureAccessor("PulseWidth")
#QualityValue = _makeBaseFeatureAccessor("QualityValue")
InsertionQV = _makeBaseFeatureAccessor("InsertionQV")
DeletionQV = _makeBaseFeatureAccessor("DeletionQV")
DeletionTag = _makeBaseFeatureAccessor("DeletionTag")
MergeQV = _makeBaseFeatureAccessor("MergeQV")
SubstitutionQV = _makeBaseFeatureAccessor("SubstitutionQV")
def read(self, aligned=True, orientation="native"):
if not (orientation == "native" or orientation == "genomic"):
raise ValueError, "Bad `orientation` value"
if self.isUnmapped and (orientation != "native" or aligned == True):
raise UnavailableFeature, \
"Cannot get genome oriented/aligned features from unmapped BAM record"
data = np.fromstring(self.peer.seq, dtype=np.int8)
if self.isCCS:
s = self.aStart
e = self.aEnd
else:
s = self.aStart - self.qStart
e = self.aEnd - self.qStart
l = self.qLen
# clip
assert l == len(data) and s >= 0 and e <= l
if self.isForwardStrand: clipped = data[s:e]
else: clipped = data[(l-e):(l-s)]
# orient
shouldReverse = self.isReverseStrand and orientation == "native"
ungapped = reverseComplementAscii(clipped) if shouldReverse else clipped
# gapify
if aligned: r = self._gapifyRead(ungapped, orientation)
else: r = ungapped
return r.tostring()
def __repr__(self):
if self.isUnmapped:
return "Unmapped BAM record: " + self.queryName
else:
return "BAM alignment: %s @ %s %3d %9d %9d" \
% (self.queryName, ("+" if self.isForwardStrand else "-"),
self.referenceId, self.tStart, self.tEnd)
def __str__(self):
if self.bam.isReferenceLoaded:
COLUMNS = 80
val = ""
val += repr(self) + "\n\n"
val += "Read: " + self.readName + "\n"
val += "Reference: " + self.referenceName + "\n\n"
val += "Read length: " + str(self.readLength) + "\n"
val += "Identity: " + "%0.3f" % self.identity + "\n"
alignedRead = self.read()
alignedRef = self.reference()
transcript = self.transcript(style="exonerate+")
refPos = self.referencePositions()
refPosString = "".join([str(pos % 10) for pos in refPos])
for i in xrange(0, len(alignedRef), COLUMNS):
val += "\n"
val += " " + refPosString[i:i+COLUMNS] + "\n"
val += " " + alignedRef [i:i+COLUMNS] + "\n"
val += " " + transcript [i:i+COLUMNS] + "\n"
val += " " + alignedRead [i:i+COLUMNS] + "\n"
val += "\n"
return val
else:
return repr(self)
def __cmp__(self, other):
return cmp((self.referenceId, self.tStart, self.tEnd),
(other.referenceId, other.tStart, other.tEnd))
@requiresPbi
def __getattr__(self, key):
if key in self.bam.pbi.columnNames:
return self.bam.pbi[self.rowNumber][key]
else:
raise AttributeError("no such column '%s' in pbi index" % key)
def __dir__(self):
basicDir = dir(self.__class__)
if self.hasPbi:
return basicDir + self.bam.pbi.columnNames
else:
return basicDir
class ClippedBamAlignment(BamAlignment):
def __init__(self, aln, tStart, tEnd, aStart, aEnd, unrolledCigar):
# Self-consistency checks
assert aln.isMapped
assert tStart <= tEnd
assert aStart <= aEnd
assert np.count_nonzero(unrolledCigar != BAM_CDEL) == (aEnd - aStart)
# Assigment
self.peer = aln.peer
self.bam = aln.bam
self.rowNumber = aln.rowNumber
self.tStart = tStart
self.tEnd = tEnd
self.aStart = aStart
self.aEnd = aEnd
self._unrolledCigar = unrolledCigar # genomic orientation
def unrolledCigar(self, orientation="native"):
if orientation=="native" and self.isReverseStrand:
return self._unrolledCigar[::-1]
else:
return self._unrolledCigar
|