-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathassign_multimappers.py
executable file
·73 lines (58 loc) · 2.12 KB
/
assign_multimappers.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# piped script to take multimappers and randomly assign
# requires a qname sorted file!!
import sys
import random
import argparse
def parse_args():
'''
Gives options
'''
parser = argparse.ArgumentParser(
description='Saves reads below a alignment threshold and discards all others')
parser.add_argument('-k', help='Alignment number cutoff')
parser.add_argument('--paired-end', dest='paired_ended',
action='store_true', help='Data is paired-end')
args = parser.parse_args()
alignment_cutoff = int(args.k)
paired_ended = args.paired_ended
return alignment_cutoff, paired_ended
if __name__ == "__main__":
'''
Runs the filtering step of choosing multimapped reads
'''
[alignment_cutoff, paired_ended] = parse_args()
if paired_ended:
alignment_cutoff = int(alignment_cutoff) * 2
# Store each line in sam file as a list of reads,
# where each read is a list of elements to easily
# modify or grab things
current_reads = []
current_qname = ''
for line in sys.stdin:
read_elems = line.strip().split('\t')
if read_elems[0].startswith('@'):
sys.stdout.write(line)
continue
# Keep taking lines that have the same qname
if read_elems[0] == current_qname:
# Add line to current reads
current_reads.append(line)
pass
else:
# Discard if there are more than the alignment cutoff
if len(current_reads) > alignment_cutoff:
current_reads = [line]
current_qname = read_elems[0]
elif len(current_reads) > 0:
# Just output all reads, which are then filtered with
# samtools
for read in current_reads:
sys.stdout.write(str(read))
# And then discard
current_reads = [line]
current_qname = read_elems[0]
else:
# First read in file
current_reads.append(line)
current_qname = read_elems[0]