Created
May 21, 2021 06:43
-
-
Save alexcritschristoph/979bb1b598652633af50d3b02c8dd546 to your computer and use it in GitHub Desktop.
Percent Identity of reads from a BAM file
This file contains hidden or 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 | |
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