-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
290 lines (275 loc) · 11.4 KB
/
utils.py
File metadata and controls
290 lines (275 loc) · 11.4 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
"""
utils.py
Helper functions for morna; many are taken from Rail-RNA.
"""
from argparse import HelpFormatter
import sys
import re
def help_formatter(prog):
""" So formatter_class's max_help_position can be changed. """
return HelpFormatter(prog, max_help_position=40)
def parsed_md(md):
""" Divides an MD string up by boundaries between ^, letters, and numbers
md: an MD string (example: 33A^CC).
Return value: MD string split by boundaries described above.
"""
md_to_parse = []
md_group = [md[0]]
for i, char in enumerate(md):
if i == 0: continue
if (re.match('[A-Za-z]', char) is not None) \
!= (re.match('[A-Za-z]', md[i-1]) is not None) or \
(re.match('[0-9]', char) is not None) \
!= (re.match('[0-9]', md[i-1]) is not None):
if md_group:
md_to_parse.append(''.join(md_group))
md_group = [char]
else:
md_group.append(char)
if md_group:
md_to_parse.append(''.join(md_group))
return [char for char in md_to_parse if char != '0']
def indels_junctions_exons_mismatches(
cigar, md, pos, seq, drop_deletions=False, junctions_only=False
):
""" Finds indels, junctions, exons, mismatches from CIGAR, MD string, POS
cigar: CIGAR string
md: MD:Z string
pos: position of first aligned base
seq: read sequence
drop_deletions: drops deletions from coverage vectors iff True
junctions_only: does not populate mismatch list
Return value: tuple
(insertions, deletions, junctions, exons, mismatches).
Insertions is a list of tuples (last genomic position before insertion,
string of inserted bases). Deletions
is a list of tuples (first genomic position of deletion,
string of deleted bases). Junctions is a list
of tuples (junction start position (inclusive),
junction end position (exclusive),
left_diplacement, right_displacement). Exons is a list
of tuples (exon start position (inclusive),
exon end position (exclusive)). Mismatches is a list
of tuples (genomic position of mismatch, read base)
"""
insertions, deletions, junctions, exons, mismatches = [], [], [], [], []
cigar = re.split(r'([MINDS])', cigar)[:-1]
md = parsed_md(md)
seq_size = len(seq)
cigar_chars, cigar_sizes = [], []
cigar_index, md_index, seq_index = 0, 0, 0
max_cigar_index = len(cigar)
while cigar_index != max_cigar_index:
if cigar[cigar_index] == 0:
cigar_index += 2
continue
if cigar[cigar_index+1] == 'M':
aligned_base_cap = int(cigar[cigar_index])
aligned_bases = 0
while True:
try:
aligned_bases += int(md[md_index])
if aligned_bases <= aligned_base_cap:
md_index += 1
except ValueError:
# Not an int, but should not have reached a deletion
assert md[md_index] != '^', '\n'.join(
['cigar and md:',
''.join(cigar), ''.join(md)]
)
if not junctions_only:
mismatches.append(
(pos + aligned_bases,
seq[seq_index + aligned_bases])
)
correction_length = len(md[md_index])
m_length = aligned_base_cap - aligned_bases
if correction_length > m_length:
md[md_index] = md[md_index][:m_length]
aligned_bases = aligned_base_cap
else:
aligned_bases += correction_length
md_index += 1
if aligned_bases > aligned_base_cap:
md[md_index] = aligned_bases - aligned_base_cap
break
elif aligned_bases == aligned_base_cap:
break
# Add exon
exons.append((pos, pos + aligned_base_cap))
pos += aligned_base_cap
seq_index += aligned_base_cap
elif cigar[cigar_index+1] == 'N':
skip_increment = int(cigar[cigar_index])
# Add junction
junctions.append((pos, pos + skip_increment,
seq_index, seq_size - seq_index))
# Skip region of reference
pos += skip_increment
elif cigar[cigar_index+1] == 'I':
# Insertion
insert_size = int(cigar[cigar_index])
insertions.append(
(pos - 1, seq[seq_index:seq_index+insert_size])
)
seq_index += insert_size
elif cigar[cigar_index+1] == 'D':
assert md[md_index] == '^', '\n'.join(
['cigar and md:',
''.join(cigar), ''.join(md)]
)
# Deletion
delete_size = int(cigar[cigar_index])
md_delete_size = len(md[md_index+1])
assert md_delete_size >= delete_size
deletions.append((pos, md[md_index+1][:delete_size]))
if not drop_deletions: exons.append((pos, pos + delete_size))
if md_delete_size > delete_size:
# Deletion contains a junction
md[md_index+1] = md[md_index+1][delete_size:]
else:
md_index += 2
# Skip deleted part of reference
pos += delete_size
else:
# Soft clip
assert cigar[cigar_index+1] == 'S'
# Advance seq_index
seq_index += int(cigar[cigar_index])
cigar_index += 2
'''Merge exonic chunks/deletions; insertions/junctions could have chopped
them up.'''
new_exons = []
last_exon = exons[0]
for exon in exons[1:]:
if exon[0] == last_exon[1]:
# Merge ECs
last_exon = (last_exon[0], exon[1])
else:
# Push last exon to new exon list
new_exons.append(last_exon)
last_exon = exon
new_exons.append(last_exon)
return insertions, deletions, junctions, new_exons, mismatches
def dummy_md_index(cigar):
""" Creates dummy MD string from CIGAR in case of missing MD.
cigar: cigar string
Return value: dummy MD string
"""
cigar = re.split(r'([MINDS])', cigar)[:-1]
cigar_index = 0
max_cigar_index = len(cigar)
md = []
while cigar_index != max_cigar_index:
if cigar[cigar_index] == 0:
cigar_index += 2
continue
if cigar[cigar_index+1] == 'M':
try:
if type(md[-1]) is int:
md[-1] += int(cigar[cigar_index])
else:
md.append(int(cigar[cigar_index]))
except IndexError:
md.append(int(cigar[cigar_index]))
cigar_index += 2
elif cigar[cigar_index+1] in 'SIN':
cigar_index += 2
elif cigar[cigar_index+1] == 'D':
md.extend(['^', 'A'*int(cigar[cigar_index])])
cigar_index += 2
else:
raise RuntimeError(
'Accepted CIGAR characters are only in [MINDS].'
)
return ''.join(str(el) for el in md)
def junctions_from_raw_stream(raw_stream):
""" Generates junctions from raw stream
Raw format has four fields: junction's (chromosome, start (1-based,
inclusive), end (1-based, inclusive)), coverage
Yield value: tuple (chrom, start position, end position, coverage)
"""
for line in raw_stream:
tokens = line.strip().split('\t')
yield (tokens[0], int(tokens[1]), int(tokens[2]), int(tokens[3]))
def junctions_from_bed_stream(bed_stream):
""" Generates junctions from BED stream
bed_stream: input stream containing lines of a BED file characterizing
splice junctions
Yield value: tuple (chrom, start position, end position, coverage)
representing a junction. Start position is 1-based and inclusive;
end position is 1-based and inclusive.
"""
for line in bed_stream:
tokens = line.rstrip().split('\t')
if len(tokens) < 12:
continue
chrom = tokens[0]
chrom_start = int(tokens[1])
chrom_end = int(tokens[2])
coverage = int(tokens[4])
block_sizes = tokens[10].split(',')
block_starts = tokens[11].split(',')
# Handle trailing commas
try:
int(block_sizes[-1])
except ValueError:
block_sizes = block_sizes[:-1]
try:
int(block_starts[-1])
except ValueError:
block_starts = block_starts[:-1]
block_count = len(block_sizes)
if block_count < 2:
# No junctions
continue
assert block_count == len(block_starts)
junctions = []
# First block characterizes junction on left side of junction
junctions.append(chrom_start + int(block_starts[0])
+ int(block_sizes[0]))
for i in xrange(1, block_count - 1):
# Any intervening blocks characterize two junctions
junction_start = chrom_start + int(block_starts[i])
junctions.append(junction_start)
junctions.append(junction_start + int(block_sizes[i]))
# Final block characterizes junction on right side of junction
junctions.append(chrom_start + int(block_starts[-1]))
for i in xrange(len(junctions)/2):
yield (chrom, junctions[2*i]+1, junctions[2*i+1], coverage)
def junctions_from_sam_stream(sam_stream):
""" Generates junctions from SAM stream
sam_stream: input stream containing lines of a sam file characterizing
splice junctions
Yield value: tuple (chrom, start position, end position, 1)
representing a junction. Start position is 1-based and inclusive;
end position is 1-based and inclusive.
"""
for line in sam_stream:
if line[0] == '@': continue
try:
tokens = line.strip().split('\t')
flag = int(tokens[1])
if flag & 4:
continue
name = tokens[0]
rname = tokens[2]
cigar = tokens[5]
pos = int(tokens[3])
seq = tokens[9]
flag = int(tokens[1])
if 'N' not in cigar or flag & 256:
continue
#md = [token[5:] for token in tokens if token[:5] == 'MD:Z:'][0]
_, _, junctions_to_add, _, _ = indels_junctions_exons_mismatches(
cigar, dummy_md_index(cigar), pos, seq
)
for junction in junctions_to_add:
#strip displacements from junction before using them,
#and compensate for end position off-by-one
corrected_junction = (junction[0], junction[1] - 1,)
yield (rname,) + corrected_junction + (1,)
except IndexError:
print >>sys.stderr, ('Error found on line: ' + line)
raise