Skip to content

Instantly share code, notes, and snippets.

@arq5x
Created October 21, 2011 14:47
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 arq5x/1304024 to your computer and use it in GitHub Desktop.
Save arq5x/1304024 to your computer and use it in GitHub Desktop.
Using pybedtools to report a BAM read's allele when it overlaps a SNP
#!/usr/bin/env python
"""
get_bam_allele.py
Usage: python get_bam_allele.py [BAM] [BED]
"""
from pybedtools import BedTool
import sys
# Create "BedTools" for the BAM and BED file
# that are passed in from the command line.
reads = BedTool(sys.argv[1])
snps = BedTool(sys.argv[2])
# Loop through each read in the BAM file and
# report the base for each SNP that it overlaps
for read in reads:
for snp in snps.all_hits(read):
# print the read name (idx=0), snp name and the allele
# in the read at the SNP site (idx=9 is the BAM sequence)
print read[0], snp.name, read[9][snp.start - read.start]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment