#!/usr/bin/env python #usage: python filter_out_reads_with_less_than_80QC.py bwa_mem_ont2d_CB_2D_1D_to_GRCm38_cdna_all.sorted.no_secondary.bam bwa_mem_ont2d_CB_2D_1D_to_GRCm38_cdna_all.sorted.no_secondary.no_read_less_than_80QC.bam import pysam import sys inputFile = pysam.AlignmentFile(sys.argv[1], "rb") outputFile = pysam.AlignmentFile(sys.argv[2], "wb", template=inputFile) def analyzeRead(x): if (x.is_secondary): pass elif (x.is_supplementary): pass elif (x.is_unmapped): pass else: lengthRead = x.infer_query_length() lengthAlignment = x.query_alignment_length percentAlignment = lengthAlignment / float(lengthRead) if percentAlignment>=0.8: outputFile.write(x) def main(): for read in inputFile: analyzeRead(read) inputFile.close() outputFile.close() if __name__ == "__main__": main()