comparison mapping_quality_stats.py @ 0:f00479673d47 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
author artbio
date Wed, 15 Jun 2022 10:43:07 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f00479673d47
1 import argparse
2 from collections import defaultdict
3
4 import pysam
5
6
7 def Parser():
8 the_parser = argparse.ArgumentParser()
9 the_parser.add_argument('-bam', '--bam', dest='bam', required=True,
10 help='input BAM file')
11 the_parser.add_argument('-o', '--output', dest='distribution',
12 required=True,
13 help='tabular output for mapq distribution')
14 args = the_parser.parse_args()
15 return args
16
17
18 def collect_mapq(bam, out):
19 samfile = pysam.AlignmentFile(bam, "rb")
20 mapq_dict = defaultdict(int)
21 for read in samfile:
22 mapq_dict[read.mapping_quality] += 1
23 with open(out, 'w') as out:
24 out.write('mapq\tnumber_of_alignments\n')
25 for quality in sorted(mapq_dict):
26 out.write(f"{quality}\t{mapq_dict[quality]}\n")
27 return mapq_dict
28
29
30 def main(bam, out):
31 collect_mapq(bam, out)
32
33
34 if __name__ == "__main__":
35 args = Parser()
36 main(args.bam, args.distribution)