forked from petercombs/dicty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterToMonomappers.py
37 lines (33 loc) · 1.15 KB
/
FilterToMonomappers.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
37
import pysam
from sys import argv, stderr
if __name__ == "__main__":
input = pysam.AlignmentFile(argv[1])
output = pysam.AlignmentFile(argv[2], "wb", template=input)
current_read_1s = []
current_read_2s = []
current_read_id = ""
total_reads = 0
kept_reads = 0
for read in input:
total_reads += 1
if read.qname != current_read_id and len(current_read_1s) == 1:
output.write(current_read_1s[0])
if len(current_read_2s) == 1:
output.write(current_read_2s[0])
kept_reads += 2
else:
kept_reads += 1
current_read_1s = []
current_read_2s = []
current_read_id = read.qname
[current_read_1s, current_read_2s][read.is_read2].append(read)
if read.qname != current_read_id and len(current_read_1s) == 1:
output.write(current_read_1s[0])
if len(current_read_2s) == 1:
output.write(current_read_2s[0])
kept_reads += 2
else:
kept_reads += 1
output.close()
input.close()
print("Kept {} of {}".format(kept_reads, total_reads), file=stderr)