Skip to content

Instantly share code, notes, and snippets.

@alexcritschristoph
Created May 21, 2021 06:43
Show Gist options
  • Save alexcritschristoph/979bb1b598652633af50d3b02c8dd546 to your computer and use it in GitHub Desktop.
Save alexcritschristoph/979bb1b598652633af50d3b02c8dd546 to your computer and use it in GitHub Desktop.
Percent Identity of reads from a BAM file
import pysam
bamfile = pysam.AlignmentFile('file.bam')
for read in bamfile.fetch():
number_of_mismatches = read.get_tag("NM")
read_length = read.infer_query_length()
read_percent_id = (1 - float(number_of_mismatches) / float(read_length)) * 100
# if you want a specific read
if read.query_name == 'my_read':
print(read_percent_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment