-
Notifications
You must be signed in to change notification settings - Fork 1
/
sam-filter.py
executable file
·36 lines (29 loc) · 1.11 KB
/
sam-filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
import sys
import os
import argparse
DEFAULT_MIN_QUALITY = 30
DEFAULT_PHRED = 33
def get_args():
parser = argparse.ArgumentParser(
description="Filter SAM for average read quality.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-q", "--min_quality", type=int, default=DEFAULT_MIN_QUALITY,
help="Minimum average read quality.")
parser.add_argument("-p", "--phred", type=int, default=DEFAULT_PHRED,
help="Phred quality score type.")
return parser.parse_args()
def main(min_quality=DEFAULT_MIN_QUALITY,
phred=DEFAULT_PHRED):
for line in sys.stdin:
if line[0] == "@":
sys.stdout.write(line)
continue
qname,flag,rname,pos,mapq,cigar,rnext,pnext,tlen,seq,qual,rest = line.split(maxsplit=11)
base_quals = [ord(qchar) - phred for qchar in qual]
avg_quality = sum(base_quals)/len(base_quals)
if avg_quality > min_quality:
sys.stdout.write(line)
if __name__ == '__main__':
args = get_args()
main(**vars(args))