-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathannotate.py
340 lines (301 loc) · 11.3 KB
/
annotate.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import gzip
import itertools
import operator
import functools
import urllib.parse
import numpy as np
import ncls
class Intervals:
"""
A class for manipulating GFF3-like intervals.
This is a wrapper around a numpy record array, with some convenience
functions for doing set operations on closed integer intervals.
"""
def __init__(self, data):
if not (
isinstance(data, np.recarray)
and hasattr(data, "start")
and hasattr(data, "end")
):
raise ValueError(
"data must be a numpy recarray with start and end attributes"
)
self.data = data
self.data_fields = set(data.dtype.names)
self._ncls = None
def __len__(self):
return len(self.data)
def __getattr__(self, key):
# Use attributes of self.data as if they were attributes of this class.
return getattr(self.data, key)
def __iter__(self):
# Iterate over self.data.
return iter(self.data)
def __getitem__(self, key):
# Use indexes of self.data as if they were indexes of this class.
return self.data[key]
@property
def ncls(self):
"""
Nested Containment List for the intervals.
"""
if self._ncls is None:
start = self.start
end = self.end
# arrays must be c-contiguous for ncls, so copy if they're not
if not start.flags["C_CONTIGUOUS"]:
start = np.array(start)
if not end.flags["C_CONTIGUOUS"]:
end = np.array(end)
self._ncls = ncls.NCLS(start, end, np.arange(len(start)))
return self._ncls
def overlap(self, start, end, yield_all=False):
"""
Find all intervals that overlap the query interval(s) ``[start, end]``.
``start`` and ``end`` may be single numbers, or numpy arrays.
Returns a generator yielding a tuple (qi, record_idx), where qi is the
index of the query interval and record_idx is a list of indexes record
indexes that overlaps the query interval. If ``yield_all=True``, empty
lists will also be generated for query intervals with no overlaps.
"""
try:
len(start)
except TypeError:
start = np.array([start])
end = np.array([end])
assert len(start) == len(end)
# arrays must be c-contiguous for ncls, so copy if they're not
if not start.flags["C_CONTIGUOUS"]:
start = np.array(start)
if not end.flags["C_CONTIGUOUS"]:
end = np.array(end)
indexes = np.arange(len(start))
query_idx, self_idx = self.ncls.all_overlaps_both(start, end, indexes)
last_qi = 0
for qi, groups in itertools.groupby(
zip(query_idx, self_idx), operator.itemgetter(0)
):
if yield_all:
while last_qi < qi:
yield last_qi, []
last_qi += 1
yield qi, [idx[1] for idx in groups]
last_qi += 1
if yield_all:
qi = len(start)
while last_qi < qi:
yield last_qi, []
last_qi += 1
def _iter_merge(self):
"""
Generator of ``[start, end]`` coordinates for merged intervals.
This only does the correct thing for closed intervals.
"""
if len(self) == 0:
return
start = None
end = -np.inf
pending = True
for record in sorted(self, key=operator.itemgetter("start")):
if record.start - 1 <= end:
# intersects (or is contiguous with) the previous record(s)
end = max(end, record.end)
pending = True
else:
# this record does not intersect with the previous record(s)
if end > 0:
yield start, end
pending = False
start, end = record.start, record.end
if pending:
yield start, end
def merge(self):
"""
Returns a new instance containing merged intervals.
This only does the correct thing for closed intervals.
"""
records = list(self._iter_merge())
recarray = np.rec.fromrecords(records, dtype=[("start", int), ("end", int)])
return self.__class__(recarray)
def subset(self, start=None, end=None, **kwargs):
"""
Returns a new instance containing a subset of intervals.
If the ``start`` and/or ``end``
coordinates are specified, only intervals in the ``[start, end]``
genomic window are generated. Intevals overlapping this window
are clipped to the window boundaries.
Additional keyword arguments can be specified to obtain subsets
by filtering for exact matches on other data fields. E.g.
>>> gff.subset(source="ensembl", type="exon")
"""
if start is None:
start = 1
if end is None:
end = np.inf
records = self.data
if len(kwargs) > 0:
inc_filters = []
for key, val in kwargs.items():
if key not in self.data_fields:
raise ValueError(f"unknown record field '{key}'")
inc_filters.append(self.data[key] == val)
indices = np.where(np.all(inc_filters, axis=0))
records = self.data[indices]
indices = np.where(np.logical_and(records.end >= start, records.start <= end))
records = records[indices]
indices = np.where(np.logical_or(records.start < start, records.end > end))
records[indices] = records[indices].copy()
records[indices].start = np.maximum(records[indices].start, start)
records[indices].end = np.minimum(records[indices].end, end)
return self.__class__(records)
def dump(self, file):
"""
Print tab-separated fields to the file object ``file``.
"""
for record in self:
print(*record, sep="\t", file=file)
@classmethod
def fromrecords(cls, records):
"""
Returns a new instance from ``records``, which is a list of 2-tuples.
"""
recarray = np.rec.fromrecords(records, dtype=[("start", int), ("end", int)])
return cls(recarray)
def parse_gff3_attributes(attr):
"""
Split semicolon delimited attributes into a dictionary.
"""
d = dict()
fields = attr.split(";")
for field in fields:
subfields = field.split("=")
if len(subfields) == 1:
d[field] = True
elif len(subfields) == 2:
k, v = subfields
d[k] = urllib.parse.unquote(v)
else:
raise ValueError(f"unknown field {field} in annotation {attr}")
return d
def load_gff3(filename, **kwargs):
"""
Load a GFF3 file, filtering on equality of provided keyword args (if any).
E.g. to load only ensembl_havana annotations for chromosome 1:
>>> load_gff3("/path/to/file.gff", source="ensembl_havana", seqid="1")
GFF3 format references:
https://www.ensembl.org/info/website/upload/gff.html
http://gmod.org/wiki/GFF3
"""
field_types = [
# field name, numpy dtype
("seqid", object),
("source", object),
("type", object),
("start", int),
("end", int),
("score", object),
("strand", object),
("phase", object),
("attributes", object),
]
field_names = set([name for name, _ in field_types])
field_names -= set(["start", "end"]) # TODO support these? via tabix?
keep = lambda record: True # noqa:E731
if len(kwargs) > 0:
for key in kwargs:
if key not in field_names:
raise ValueError(f"unsupported filter key '{key}'")
indices = [i for i, (key, _) in enumerate(field_types) if key in kwargs]
getter = operator.itemgetter(*indices)
values = tuple(kwargs[field_types[i][0]] for i in indices)
if len(values) == 1:
values = values[0]
keep = lambda record: getter(record) == values # noqa:E731
xopen = functools.partial(gzip.open, mode="rt")
try:
xopen(filename).read(1)
except OSError:
xopen = open
records = []
with xopen(filename) as f:
for lineno, line in enumerate(f, 1):
line = line.rstrip()
if line[0] == "#":
continue
record = line.split("\t")
if len(record) != len(field_types):
raise RuntimeError(
f"{filename}: {lineno}: expected {len(field_types)} "
f"tab-separated columns, but found {len(record)}"
)
record[3] = int(record[3])
record[4] = int(record[4])
if keep(record):
records.append(tuple(record))
records = np.rec.fromrecords(records, dtype=field_types)
return Intervals(records)
def parse_predictions(pred_file, max_chr_name_len=128):
chr_dtype = f"U{max_chr_name_len}"
with open(pred_file) as f:
# header is: chrom, start, end, Pr(X), ...
header = next(f).split()
dtype = [(header[0], chr_dtype), (header[1], int), (header[2], int)] + [
(h, float) for h in header[3:]
]
preds = np.loadtxt(f, dtype=dtype)
records = np.rec.array(preds)
return header, records
def annotate(*, gff_file, pred_file, file, x=None, n=None, pad=100000):
if x is None and n is None:
raise ValueError("Specify either top x proportion of hits or top n hits")
genes = load_gff3(gff_file, source="ensembl_havana", type="gene")
preds_header, preds = parse_predictions(pred_file)
# sort predictions by p-value
p = preds_header[3] # Pr(X) label
idx = np.argsort(preds[p])[::-1]
# take only top predictions
if x is not None:
# annotate top x proportion of hits
n = int(len(idx) * x)
top_preds = preds[idx[:n]]
# extend in both directions by `pad`
top_preds.start -= pad
top_preds.end += pad
chroms = np.unique(top_preds.chrom).tolist()
for chrom in sorted(chroms, key=int):
chr_preds = top_preds[np.where(top_preds.chrom == chrom)]
records = Intervals(chr_preds).merge()
chr_genes = genes.subset(seqid=chrom)
for j, gene_idx in chr_genes.overlap(records.start, records.end, yield_all=True):
record = records[j]
gene_names = []
for j in gene_idx:
attr = parse_gff3_attributes(chr_genes[j].attributes)
gene_names.append(attr.get("Name"))
print(
chrom,
# report the region without padding
record.start + pad,
record.end - pad,
"; ".join(gene_names),
sep="\t",
file=file,
)
if __name__ == "__main__":
import sys
if len(sys.argv) != 4:
print(f"usage: {sys.argv[0]} file.gff3 predictions.txt x")
exit(1)
gff_file = sys.argv[1]
pred_file = sys.argv[2]
x = float(sys.argv[3])
if x <= 0:
raise ValueError("x must be greater than zero")
if x < 1:
# annotate top x proportion of hits
n = None
else:
# annotate top n hits
n = round(x)
x = None
annotate(gff_file=gff_file, pred_file=pred_file, file=sys.stdout, n=n, x=x)