This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exter.py
230 lines (188 loc) · 7.12 KB
/
exter.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
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
"""
>>> exter = Exter()
>>> exter.add_exon('mygene', [10, 20])
>>> exter.add_exon('mygene', [12, 18])
>>> exter.add_exon('mygene', [30, 40])
>>> exter.add_exon('anothergene', [32, 40])
>>> exter.union()
>>> exter.localize(12, 13)
[{'exon': 0, 'start': 2, 'gene': 'mygene', 'stop': 3, 'strand': '+'}]
>>> exter.localize(32, 33)
[{'exon': 0, 'start': 0, 'gene': 'anothergene', 'stop': 1, 'strand': '+'}, {'exon': 1, 'start': 12, 'gene': 'mygene', 'stop': 13, 'strand': '+'}]
>>> exter.exons('mygene')
[(10, 20), (30, 40)]
>>> exter.globalize('mygene', (9, 11))
[(19, 20), (30, 31)]
>>> exter.globalize('mygene', (30, 41)) # should this raise an error?
[]
>>> exter = Exter()
>>> exter.add_exon('atm', [1000,1100])
>>> exter.add_exon('atm', [5000,5100])
>>> exter.add_exon('atm', [7000,7100])
>>> exter.exons('atm')
[(1000, 1100), (5000, 5100), (7000, 7100)]
>>> exter.localize(5050, 5051)
[{'exon': 1, 'start': 150, 'gene': 'atm', 'stop': 151, 'strand': '+'}]
>>> exter.localize(7050, 7051)
[{'exon': 2, 'start': 250, 'gene': 'atm', 'stop': 251, 'strand': '+'}]
>>> exter.localize(7099, 7100)
[{'exon': 2, 'start': 299, 'gene': 'atm', 'stop': 300, 'strand': '+'}]
>>> exter.localize(1000, 1001)
[{'exon': 0, 'start': 0, 'gene': 'atm', 'stop': 1, 'strand': '+'}]
>>> exter.add_exon('atmm', [1000,1100], reverse_strand=True)
>>> exter.add_exon('atmm', [5000,5100], reverse_strand=True)
>>> exter.localize(5050, 5051)
[{'exon': 0, 'start': 150, 'gene': 'atm', 'stop': 151, 'strand': '-'}]
"""
from __future__ import print_function
from collections import defaultdict
from interlap import InterLap
import gzip
import sys
def read_gff(gff_path, CDS="CDS", union=True):
"""
read a gff and return a dictionary of chrom -> exter
"""
D = {'gene': {}, CDS: {}, 'mRNA': {}, 'transcript': {}}
parents = {}
fh = gzip.open(gff_path) if gff_path.endswith(".gz") else open(gff_path)
for toks in (x.strip().split("\t") for x in fh if x[0] != '#'):
if len(toks) > 9: continue
info = dict([list(val.split('=')) for val in toks[8].split(';')])
ftype = toks[2]
if "ID" in info:
id = info["ID"].split(":", 1)[-1]
else:
id = None
if 'Parent' in info:
parent=info['Parent'].split(':', 1)[-1]
else:
parent = None
if id is not None and parent is not None:
parents[id] = parent
if not ftype in D:
continue
strand = toks[6]
if ftype == 'gene':
name = info["Name"]
if id in D['gene']:
assert toks[0] == D['gene'][id]["chrom"], toks
#print(info["Name"], toks, D['gene'][info["Name"]])
D[ftype][id]['start'] = min(D[ftype][id]['start'], int(toks[3]))
D[ftype][id]['stop'] = max(D[ftype][id]['stop'], int(toks[4]))
continue
D[ftype][id] = dict(chrom=toks[0], start=int(toks[3]),
stop=int(toks[4]), strand=strand, name=name)
else:
if ftype == 'mRNA': ftype = 'transcript'
if not id in D[ftype]:
D[ftype][id] = []
D[ftype][id].append(dict(chrom=toks[0], start=int(toks[3]),
stop=int(toks[4]), strand=strand, parent=parent))
tr = D['transcript']
result = {}
for id, exons in D[CDS].items():
for exon in exons:
try:
transr = tr[exon['parent']]
except:
# hack it into same structure as above.
transr = [{"parent": parents[exon['parent']]}]
try:
gene = D['gene'][transr[0]['parent']]
except:
print(transr[0], "parent not found. skipping")
continue
if not exon['chrom'] in result:
result[exon['chrom']] = Exter()
result[exon['chrom']].add_exon(gene['name'], (exon['start'], exon['stop']))
if union:
for chrom in result:
result[chrom].union()
return result
class Exter(object):
def __init__(self):
# TODO use gff
self._exons = defaultdict(list)
# this helps find names from positions
self._posns = InterLap()
self._dirty = False
self._reverse = set()
def add_exon(self, name, start_end, reverse_strand=False):
self._dirty = True
self._posns.add((start_end[0], start_end[1], name))
if reverse_strand:
if name in self._exons and not name in self._reverse:
raise Exception(name + " already in as + strand")
self._reverse.add(name)
elif name in self._reverse:
raise Exception(name + " already in as - strand")
self._exons[name].append((start_end[0], start_end[1]))
def exons(self, name):
return self._exons[name]
def _sort(self):
for name in self._exons:
self._exons[name].sort()
self._dirty = False
def union(self):
"""
collapse overlapping exons from the same gene to their union
"""
self._posns = InterLap()
for name in self._exons:
if self._dirty:
self._exons[name].sort()
B = self._exons[name]
before = list(self._exons[name])
reduced = [self._exons[name][0]]
for i, b in enumerate(self._exons[name][1:]):
a = reduced[-1]
if a[1] >= b[0]:
reduced[-1] = (reduced[-1][0], max(reduced[-1][1], b[1]))
else:
reduced.append(b)
self._exons[name] = reduced
for r in reduced:
self._posns.add((r[0], r[1], name))
self._dirty = False
def localize(self, start, end):
if self._dirty: self._sort()
names = set()
for p in self._posns.find((start, end)):
names.add(p[2])
result = []
for name in names:
exons = self._exons[name]
rev = name in self._reverse
if rev:
exons = exons[::-1]
off = exons[0][0]
for i, (estart, estop) in enumerate(exons):
if i > 0:
# add last intron
#print(off, estart, exons[i-1], estart - exons[i-1][1])
off += (estart - exons[i-1][1])
if estop < start:
continue
if estart > end:
continue
result.append({'gene': name, 'start': start - off, 'stop': end - off,
'strand': ('-' if rev else '+'), 'exon': i})
return result
def globalize(self, name, start_end):
"""
"""
if self._dirty: self._sort()
start, end = start_end
off = self._exons[name][0][0]
result = []
for estart, estop in self._exons[name]:
if end + off < estart: continue
if start + off > estop: break
result.append((max(estart, off + start), min(estop, off + end)))
off += estop - estart
return result
if __name__ == "__main__":
import doctest
print(doctest.testmod())
#read_gff(sys.argv[1])