view mapping_quality_stats.py @ 2:ce2d8f611a51 draft default tip

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/main/tools/mapping_quality_stats commit 2661225fdd0f533d4dc8e2561ea559fcc00b9128
author artbio
date Sat, 10 Feb 2024 01:32:48 +0000
parents f00479673d47
children
line wrap: on
line source

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)