forked from Ensembl/ensembl-vep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranscriptTree.pm
339 lines (246 loc) · 8.38 KB
/
TranscriptTree.pm
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
=head1 LICENSE
Copyright [2016-2017] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=cut
=head1 CONTACT
Please email comments or questions to the public Ensembl
developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
Questions may also be sent to the Ensembl help desk at
<http://www.ensembl.org/Help/Contact>.
=cut
# EnsEMBL module for Bio::EnsEMBL::VEP::TranscriptTree
#
#
=head1 NAME
Bio::EnsEMBL::VEP::TranscriptTree - class containing IntervalTree of transcript locations
=head1 SYNOPSIS
my $tree = Bio::EnsEMBL::VEP::TranscriptTree->new({
config => $config,
annotation_source => $as
});
my $overlaps = $tree->fetch($chr, $start, $end);
=head1 DESCRIPTION
The TranscriptTree class uses a series of Set::IntervalTree objects
to retrieve references to overlapping features.
It requires an attached AnnotationSource that implements a
populate_tree() method that will populate the tree with features.
=head1 METHODS
=cut
use strict;
use warnings;
package Bio::EnsEMBL::VEP::TranscriptTree;
use base qw(Bio::EnsEMBL::VEP::BaseVEP);
use Bio::EnsEMBL::Utils::Scalar qw(assert_ref);
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Set::IntervalTree;
=head2 new
Arg 1 : hashref $args
{
config => Bio::EnsEMBL::VEP::Config,
annotation_source => Bio::EnsEMBL::VEP::AnnotationSource,
}
Example : $tree = Bio::EnsEMBL::VEP::TranscriptTree->new({
config => $config,
annotation_source => $as
});
Description: Create a new Bio::EnsEMBL::VEP::TranscriptTree object. Requires
annotation_source arg to be set to a reference to a compatible
AnnotationSource i.e. one that implements a populate_tree()
method.
Returntype : Bio::EnsEMBL::VEP::TranscriptTree
Exceptions : throws if no AnnotationSource given or AnnotationSource has no populate_tree() method
Caller : Runner
Status : Stable
=cut
sub new {
my $caller = shift;
my $class = ref($caller) || $caller;
my $self = $class->SUPER::new(@_);
my $hashref = $_[0];
my $as = $hashref->{annotation_source};
assert_ref($as, 'Bio::EnsEMBL::VEP::AnnotationType::Transcript');
throw("ERROR: Unable to populate tree from annotation source type ".ref($as)) unless $as->can('populate_tree');
$as->populate_tree($self);
$self->valid_chromosomes($as->valid_chromosomes);
return $self;
}
=head2 get_chr_tree
Example : $chr_tree = $tree->get_chr_tree($chr);
Description: Get the Set::IntervalTree object for this chromosome.
Returntype : Set::IntervalTree
Exceptions : none
Caller : insert(), fetch()
Status : Stable
=cut
sub get_chr_tree {
my $self = shift;
my $chr = shift;
return $self->{trees}->{$chr} ||= Set::IntervalTree->new();
}
=head2 insert
Arg 1 : string $chromosome
Arg 2 : int $start
Arg 3 : int $end
Arg 4 : scalar $obj
Example : [$min, $max] = $tree->insert($chr, $start, $end);
$tree->insert($chr, $start, $end, $obj);
Description: Insert given chromosome range into tree. If no object $obj
is provided, a fetch is first performed, and any overlapping
ranges are merged with this one, removed, and a single
expanded range re-inserted into the tree. The expanded range
is then returned as an arrayref [$min, $max].
If an object $obj (can be a reference or any scalar) then this
is inserted instead, with the provided [$start, $end] returned
Returntype : arrayref [$min, $max]
Exceptions : none
Caller : general
Status : Stable
=cut
sub insert {
my ($self, $c, $s, $e, $obj) = @_;
my $tree = $self->get_chr_tree($c);
# obj given, just insert it and return
if($obj) {
$obj->{s} ||= $s;
$obj->{e} ||= $e;
$tree->insert($obj, $s - 1, $e);
return [$s, $e];
}
# otherwise do a merge if any overlap and insert an arrayref representing the range
my ($min, $max) = ($s, $e);
my $fetched = $tree->fetch($s - 1, $e);
if(@$fetched) {
foreach my $f(@$fetched) {
$min = $f->[0] if $f->[0] < $min;
$max = $f->[1] if $f->[1] > $max;
}
$tree->remove($s - 1, $e);
}
$tree->insert([$min, $max], $min - 1, $max);
return [$min, $max];
}
=head2 fetch
Arg 1 : string $chromosome
Arg 2 : int $start
Arg 3 : int $end
Example : my $overlapping = $tree->fetch($chr, $start, $end)
Description: Fetch features overlapping the given coordinate range
Returntype : arrayref
Exceptions : none
Caller : general
Status : Stable
=cut
sub fetch {
my ($self, $c, $s, $e) = @_;
return $self->get_chr_tree($self->get_source_chr_name($c))->fetch($s - 1, $e);
}
=head2 nearest
Arg 1 : string $chromosome
Arg 2 : int $start
Arg 3 : int $end
Example : my $nearest = $tree->nearest($chr, $start, $end)
Description: Fetch nearest features to given coordinate range
Returntype : whatever reference type was inserted by insert()
Exceptions : none
Caller : general
Status : Stable
=cut
sub nearest {
my ($self, $c, $s, $e) = @_;
my $tree = $self->get_chr_tree($self->get_source_chr_name($c));
# invert if $s > $e
($e, $s) = ($s, $e) if $s > $e;
# search up and down - note "up" in Set::IntervalTree means a higher numerical value coordinate
# confusing if you're used to talking in terms of up/downstream on a genome
# so we search "up" from $s and "down" from $e as this will capture overlaps too
my @search = grep {$_} (
$tree->fetch_nearest_down($e),
$tree->fetch_nearest_up($s),
);
# bail out here if <=1 result (empty chr or 1 result)
return \@search unless scalar @search > 1;
# get dists as hash indexed on array pos in @search
my %dists =
map {$_ => $self->_get_dist(
@{$self->_get_obj_start_end($search[$_])},
$s, $e
)}
0..$#search;
# find minimum distance
my $min_dist = (sort {$a <=> $b} values %dists)[0];
# return all objects that have that dist
return [map {$search[$_]} grep {$dists{$_} == $min_dist} 0..$#search];
}
=head2 _get_obj_start_end
Arg 1 : hashref $obj or arrayref [$start, $end]
Example : [$s, $e] = $tree->_get_obj_start_end($obj)
Description: Gets start and end given either an object hashref
or arrayref [$s, $e]
Returntype : arrayref [$start, $end]
Exceptions : none
Caller : nearest()
Status : Stable
=cut
sub _get_obj_start_end {
my ($self, $obj) = @_;
my ($s, $e);
if(ref($obj) eq 'ARRAY') {
($s, $e) = @$obj;
}
else {
$s = $obj->{s} || $obj->{start} or throw("ERROR: No start field \"s\" defined on object\n");
$e = $obj->{e} || $obj->{end} || $s;
}
return [$s, $e];
}
=head2 _get_dist
Arg 1 : int $start1
Arg 2 : int $end1
Arg 3 : int $start2
Arg 4 : int $end2
Example : $dist = $tree->_get_dist(1, 5, 8, 12)
Description: Finds shortest distance between any two ends of two given
coordinate pairs.
Returntype : int
Exceptions : none
Caller : nearest()
Status : Stable
=cut
sub _get_dist {
my ($self, $o_s, $o_e, $s, $e) = @_;
# calculate all possible distances
# sort to find lowest
return (
sort {$a <=> $b}
(
abs($s - $o_s),
abs($s - $o_e),
abs($e - $o_s),
abs($e - $o_e)
)
)[0];
}
=head2 valid_chromosomes
Arg 1 : (optional) arrayref $valid_chromosomes
Example : $valids = $tree->valid_chromosomes();
Description: Getter/setter for the list of valid chromosomes as found
in the configured AnnotationSource.
Returntype : arrayref of strings
Exceptions : none
Caller : new(), BaseVEP
Status : Stable
=cut
sub valid_chromosomes {
my $self = shift;
$self->{valid_chromosomes} = shift if @_;
return $self->{valid_chromosomes};
}
1;