Skip to content

Instantly share code, notes, and snippets.

@mdshw5
Created June 5, 2014 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdshw5/73c6591237c7a9f88518 to your computer and use it in GitHub Desktop.
Save mdshw5/73c6591237c7a9f88518 to your computer and use it in GitHub Desktop.
python bam read counts
from subprocess import Popen PIPE
def bam_read_count(bamfile):
""" Return a tuple of the number of mapped and unmapped reads in a bam file """
p = Popen(['samtools', 'idxstats', bamfile], stdout=PIPE)
mapped = 0
unmapped = 0
for line in p.stdout:
rname, rlen, nm, nu = line.rstrip().split()
mapped += int(nm)
unmapped += int(nu)
return (mapped, unmapped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment