Last active
April 14, 2017 12:38
-
-
Save wdecoster/886e9b013c08523cf45cea89b6855950 to your computer and use it in GitHub Desktop.
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
import pysam | |
import re | |
def extractMDFromBam(bam): | |
''' | |
loop over a bam file and get the edit distance to the reference genome | |
mismatches are stored in the MD tag | |
scale by aligned read length | |
''' | |
samfile = pysam.AlignmentFile(bam, "rb") | |
return [(read.query_alignment_length - sum([int(item) for item in re.split('[ACTG^]', read.get_tag("MD")) if not item == '']))/read.query_alignment_length for read in samfile.fetch()] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment