-
Notifications
You must be signed in to change notification settings - Fork 14
/
helper.py
178 lines (154 loc) · 6.93 KB
/
helper.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python2.7
"""
--------------------------------------------------------------------------------
Read Origin Protocol: Helper Script
For more information, please visit: <https://github.com/smangul1/rop/wiki>
--------------------------------------------------------------------------------
"""
import argparse
import csv
import pysam
from Bio import SeqIO
import sys
# ------------------------------------------------------------------------------
# PARSE ARGUMENTS
# ------------------------------------------------------------------------------
ap = argparse.ArgumentParser('Read Origin Protocol: Helper Script')
req_args = ap.add_argument_group('Required Arguments')
req_args.add_argument('step')
opt_args = ap.add_argument_group('Optional Arguments')
req_args.add_argument('--input_file', '-i', action='store')
opt_args.add_argument('--output_file', '-o', action='store')
opt_args.add_argument('--pre', action='store')
opt_args.add_argument('--post', action='store')
opt_args.add_argument('--max', action='store_true')
opt_args.add_argument('--pe', action='store_true')
ARGS = ap.parse_args()
# ------------------------------------------------------------------------------
# UTILITY
# ------------------------------------------------------------------------------
# Excludes reads from the pre fasta and outputs the result to the post fasta.
def exclude(pre_file, reads, post_file):
with open(pre_file) as pre, open(post_file, 'w') as post:
for seq in SeqIO.parse(pre, 'fasta'):
if seq.name not in reads:
SeqIO.write([seq], post, 'fasta')
def writeset(set_o, output_file):
with open(output_file, 'w') as output_fo:
for el in sorted(set_o):
output_fo.write(el + '\n')
# ------------------------------------------------------------------------------
# 1. LOW QUALITY READ MARKING
# ------------------------------------------------------------------------------
if ARGS.step == 'lowq':
count = 0
with open(ARGS.post, 'w') as post:
for record in SeqIO.parse(ARGS.pre, 'fastq'):
j = record.letter_annotations['phred_quality']
prc = sum(1 for i in j if i >= 20) / float(len(j))
read_length = len(str(record.seq))
if prc > 0.75 and all(i in 'ACTGN' for i in record.seq):
post.write(str('>' +\
record.name.replace('/', '---') + '_length_' +\
str(read_length)) + '\n')
post.write(str(record.seq) + '\n')
else:
post.write(str('>lowq_' +\
record.name.replace('/', '---') + '_length_' +\
str(read_length)) + '\n')
post.write(str(record.seq) + '\n')
count += 1
print(count)
# ------------------------------------------------------------------------------
# 2. rDNA PROFILING
# ------------------------------------------------------------------------------
elif ARGS.step == 'rdna':
reads = set()
with pysam.AlignmentFile(ARGS.input_file, 'rb', check_sq=False) as input_fo:
for read in input_fo.fetch():
number_mismatches = int(read.get_tag('NM'))
read_length = int(read.infer_read_length())
identity = 1 - number_mismatches/read_length
if identity > 0.94:
reads.add(read.query_name)
exclude(ARGS.pre, reads, ARGS.post)
writeset(reads, ARGS.output_file)
print(len(reads))
# ------------------------------------------------------------------------------
# 3. REMAPPING TO REFERENCE
# ------------------------------------------------------------------------------
elif ARGS.step == 'reference':
ed_human = 10 if ARGS.max else 6
reads = set()
for input_file in ARGS.input_file.split(','):
with pysam.AlignmentFile(input_file, 'rb', check_sq=False) as input_fo:
for read in input_fo.fetch():
number_mismatches = int(read.get_tag('NM'))
read_length = int(read.infer_read_length())
alignment_length = int(read.query_alignment_length)
soft = read_length - alignment_length
number_mismatches += soft
if number_mismatches <= ed_human:
reads.add(read.query_name)
exclude(ARGS.pre, reads, ARGS.post)
writeset(reads, ARGS.output_file)
print(len(reads))
# ------------------------------------------------------------------------------
# 4. REPEAT PROFILING
# ------------------------------------------------------------------------------
elif ARGS.step == 'repeats':
reads = set()
with open(ARGS.input_file, 'r') as input_fo:
for line in csv.reader(input_fo, delimiter='\t'):
element, identity = line[0], float(line[2])
alignment_len = float(line[3])
start, end = int(line[6]), int(line[7])
read_len = end - start + 1
e = float(line[10])
if e < 1e-05 and alignment_len >= 0.8 * read_len and \
identity >= 0.9 * read_len:
reads.add(element)
exclude(ARGS.pre, reads, ARGS.post)
writeset(reads, ARGS.output_file)
print(len(reads))
# ------------------------------------------------------------------------------
# 5. CIRCULAR RNA PROFILING
# ------------------------------------------------------------------------------
elif ARGS.step == 'circrna':
reads = set()
for seq in SeqIO.parse(ARGS.input_file, 'fastq'):
reads.add(seq.id)
exclude(ARGS.pre, reads, ARGS.post)
writeset(reads, ARGS.output_file)
print(len(reads))
# ------------------------------------------------------------------------------
# 6. IMMUNE PROFILING
# ------------------------------------------------------------------------------
elif ARGS.step == 'immune':
reads = set()
for input_file in ARGS.input_file.split(','):
with open(input_file, 'r') as input_fo:
reader = csv.reader(input_fo)
reader.next()
for line in reader:
reads.add(line[0])
exclude(ARGS.pre, reads, ARGS.post)
writeset(reads, ARGS.output_file)
print(len(reads))
# ------------------------------------------------------------------------------
# 7. MICROBIOME
# ------------------------------------------------------------------------------
elif ARGS.step == 'microbiome':
reads = set()
for input_file in ARGS.input_file.split(','):
with pysam.AlignmentFile(input_file, 'rb', check_sq=False) as input_fo:
for read in input_fo.fetch():
number_mismatches = int(read.get_tag('NM'))
read_length = int(read.infer_read_length())
alignment_length = int(read.query_alignment_length)
identity = 1 - number_mismatches/float(alignment_length)
if alignment_length >= 0.8 * read_length and identity >= 0.9:
reads.add(read.query_name)
exclude(ARGS.pre, reads, ARGS.post)
writeset(reads, ARGS.output_file)
print(len(reads))