Created
October 21, 2011 14:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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