diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mapping_quality_stats.py	Wed Jun 15 10:43:07 2022 +0000
@@ -0,0 +1,36 @@
+import argparse
+from collections import defaultdict
+
+import pysam
+
+
+def Parser():
+    the_parser = argparse.ArgumentParser()
+    the_parser.add_argument('-bam', '--bam', dest='bam', required=True,
+                            help='input BAM file')
+    the_parser.add_argument('-o', '--output', dest='distribution',
+                            required=True,
+                            help='tabular output for mapq distribution')
+    args = the_parser.parse_args()
+    return args
+
+
+def collect_mapq(bam, out):
+    samfile = pysam.AlignmentFile(bam, "rb")
+    mapq_dict = defaultdict(int)
+    for read in samfile:
+        mapq_dict[read.mapping_quality] += 1
+    with open(out, 'w') as out:
+        out.write('mapq\tnumber_of_alignments\n')
+        for quality in sorted(mapq_dict):
+            out.write(f"{quality}\t{mapq_dict[quality]}\n")
+    return mapq_dict
+
+
+def main(bam, out):
+    collect_mapq(bam, out)
+
+
+if __name__ == "__main__":
+    args = Parser()
+    main(args.bam, args.distribution)