annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
1 import argparse
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
2 from collections import defaultdict
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
3
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
4 import pysam
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
5
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
6
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
7 def Parser():
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
8 the_parser = argparse.ArgumentParser()
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
9 the_parser.add_argument('-bam', '--bam', dest='bam', required=True,
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
10 help='input BAM file')
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
11 the_parser.add_argument('-o', '--output', dest='distribution',
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
12 required=True,
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
13 help='tabular output for mapq distribution')
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
14 args = the_parser.parse_args()
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
15 return args
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
16
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
17
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
18 def collect_mapq(bam, out):
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
19 samfile = pysam.AlignmentFile(bam, "rb")
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
20 mapq_dict = defaultdict(int)
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
21 for read in samfile:
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
22 mapq_dict[read.mapping_quality] += 1
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
23 with open(out, 'w') as out:
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
24 out.write('mapq\tnumber_of_alignments\n')
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
25 for quality in sorted(mapq_dict):
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
26 out.write(f"{quality}\t{mapq_dict[quality]}\n")
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
27 return mapq_dict
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
28
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
29
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
30 def main(bam, out):
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
31 collect_mapq(bam, out)
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
32
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
33
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
34 if __name__ == "__main__":
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
35 args = Parser()
f00479673d47 planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mapping_quality_stats commit e4b37874b820a2ac48732667128a08e5755b7c4b
artbio
parents:
diff changeset
36 main(args.bam, args.distribution)