EnsEMBL/TopLevelAssemblyMapper.pm untested

filter: Statementstmt 0.0% 0 / 120 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 209 CC 28 SCAR 44.6
CC
28
Cov
0%
map
210.0
_list
72.0
new
30.0
CRAP 86.6
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::TopLevelAssemblyMapper -
34 Handles mapping between a given coordinate system and the toplevel
35 pseudo coordinate system.
36  
37 =head1 SYNOPSIS
38  
39 $db = Bio::EnsEMBL::DBSQL::DBAdaptor->new(...);
40 $asma = $db->get_AssemblyMapperAdaptor();
41 $csa = $db->get_CoordSystemAdaptor();
42  
43 my $toplevel = $cs_adaptor->fetch_by_name('toplevel');
44 my $ctg_cs = $cs_adaptor->fetch_by_name('contig');
45  
46 $asm_mapper = $map_adaptor->fetch_by_CoordSystems( $toplevel, $ctg_cs );
47  
48 # map to toplevel coord system for this region
49 @chr_coords =
50 $asm_mapper->map( 'AL30421.1.200.92341', 100, 10000, -1, $ctg_cs );
51  
52 # list toplevel seq_region_ids for this region
53 @chr_ids =
54 $asm_mapper->list_ids( 'AL30421.1.200.92341', 1, 1000, -1,
55 $ctg_cs );
56  
57 =head1 DESCRIPTION
58  
59 The TopLevelAssemblyMapper performs mapping between a provided
60 coordinate system and the toplevel pseudo cooordinate system. The
61 toplevel coordinate system is not a real coordinate system, but
62 represents the highest coordinate system that can be mapped to in a
63 given region. It is only possible to perform unidirectional mapping
64 using this mapper, because it does not make sense to map from the
65 toplevel coordinate system to another coordinate system.
66  
67 =head1 METHODS
68  
69 =cut
70  
71  
72 use strict;
73 use warnings;
74  
75 package Bio::EnsEMBL::TopLevelAssemblyMapper;
76 $Bio::EnsEMBL::TopLevelAssemblyMapper::VERSION = '114.0.0_50'; # TRIAL
77 $Bio::EnsEMBL::TopLevelAssemblyMapper::VERSION = '114.0.050';
78 use Bio::EnsEMBL::Utils::Exception qw(throw);
79 use Bio::EnsEMBL::Mapper;
80 use Bio::EnsEMBL::CoordSystem;
81 use Scalar::Util qw(weaken);
82  
83 =head2 new
84  
85 Arg [1] : Bio::EnsEMBL::DBAdaptor $dbadaptor the adaptor for
86 the database this mapper is using.
87 Arg [2] : Toplevel CoordSystem
88 Arg [3] : Other CoordSystem
89 Description: Creates a new TopLevelAssemblyMapper object
90 Returntype : Bio::EnsEMBL::DBSQL::TopLevelAssemblyMapper
91 Exceptions : throws if any of the 3 arguments are missing/ not
92 : of the correct type.
93 Caller : Bio::EnsEMBL::DBSQL::AssemblyMapperAdaptor
94 Status : Stable
95  
96 =cut
97  
98  
99 sub new {
100 my ($caller, $adaptor, $toplevel_cs, $other_cs) = @_;
101  
102 my $class = ref($caller) || $caller;
103  
104 if(!ref($toplevel_cs)) {
105 throw('Toplevel CoordSystem argument expected.');
106 }
107 if(!ref($other_cs)) {
108 throw('Other CoordSystem argument expected.');
109 }
110  
111 if(!$toplevel_cs->is_top_level()) {
112 throw($toplevel_cs->name() . " is not the toplevel CoordSystem.");
113 }
114 if($other_cs->is_top_level()) {
115 throw("Other CoordSystem argument should not be toplevel CoordSystem.");
116 }
117  
118 my $cs_adaptor = $adaptor->db()->get_CoordSystemAdaptor();
119 my $coord_systems = $cs_adaptor->fetch_all();
120  
121 my $self = bless {'coord_systems' => $coord_systems,
122 'toplevel_cs' => $toplevel_cs,
123 'other_cs' => $other_cs}, $class;
124  
125 $self->adaptor($adaptor);
126 return $self;
127 }
128  
129 sub adaptor {
130 my $self = shift;
131  
132 weaken($self->{'adaptor'} = shift) if(@_);
133  
134 return $self->{'adaptor'};
135 }
136  
137 =head2 map
138  
139 Arg [1] : string $frm_seq_region
140 The name of the sequence region to transform FROM
141 Arg [2] : int $frm_start
142 The start of the region to transform FROM
143 Arg [3] : int $frm_end
144 The end of the region to transform FROM
145 Arg [4] : int $strand
146 The strand of the region to transform FROM
147 Arg [5] : Bio::EnsEMBL::CoordSystem
148 The coordinate system to transform FROM
149 Arg [6] : if set will do a fastmap
150 Arg [7] : (optional) dummy placeholder to keep the interface
151 consistent across different mappers
152 Arg [8] : (optional) boolean
153 Whether or not to include the original coordinates
154 Example : @coords = $mapper->map('X', 1_000_000, 2_000_000,
155 1, $chr_cs);
156 Description: Transforms coordinates from one coordinate system
157 to another.
158 Returntype : List of Bio::EnsEMBL::Mapper::Coordinate and/or
159 Bio::EnsEMBL::Mapper:Gap objects
160 Exceptions : thrown if if the specified TO coordinate system is not one
161 of the coordinate systems associated with this mapper
162 Caller : general
163 Status : Stable
164  
165 =cut
166  
167  
168 sub map {
169 throw('Incorrect number of arguments.') if @_ < 6;
170  
171 my($self, $frm_seq_region_name, $frm_start, $frm_end, $frm_strand, $frm_cs,
172 $fastmap, $dummy, $include_org_coord) = @_;
173  
174 if($frm_cs->is_top_level()) {
175 throw("The toplevel CoordSystem can only be mapped TO, not FROM.");
176 }
177  
178 my @tmp;
179 push @tmp, $frm_seq_region_name;
180 my $seq_region_id = @{$self->adaptor()->seq_regions_to_ids($frm_cs, \@tmp)}[0];
181  
182 my $mapper = $self->{'mapper'};
183 my $toplevel_cs = $self->{'toplevel_cs'};
184 my $other_cs = $self->{'other_cs'};
185 my $adaptor = $self->adaptor;
186  
187 if($frm_cs != $other_cs && !$frm_cs->equals($other_cs)) {
188 throw("Coordinate system " . $frm_cs->name . " " . $frm_cs->version .
189 " is neither the assembled nor the component coordinate system " .
190 " of this AssemblyMapper");
191 }
192  
193 my $coord_systems = $self->{'coord_systems'};
194  
195 my $csa = $self->adaptor()->db()->get_CoordSystemAdaptor();
196  
197 #
198 # TBD try to make this more efficient
199 #
200 my $from_rank = $other_cs->rank();
201 foreach my $cs (@$coord_systems) {
202 last if($cs->rank >= $from_rank);
203  
204 #check if a mapping path even exists to this coordinate system
205 my @mapping_path = @{ $csa->get_mapping_path( $cs, $other_cs ) };
206  
207 if(@mapping_path) {
208  
209 # Try to map to this coord system. If we get back any coordinates then
210 # it is our 'toplevel' that we were looking for
211 my $mapper = $adaptor->fetch_by_CoordSystems($other_cs, $cs);
212  
213 if($fastmap) {
214 my @result = $mapper->fastmap($frm_seq_region_name, $frm_start, $frm_end,
215 $frm_strand, $frm_cs);
216 return @result if(@result);
217 } else {
218 my @coords = $mapper->map($frm_seq_region_name, $frm_start, $frm_end,
219 $frm_strand, $frm_cs, undef, undef, $include_org_coord);
220  
221 if(@coords > 1) {
222 return @coords;
223 } elsif ($include_org_coord) {
224 return @coords unless $coords[0]{mapped}->isa('Bio::EnsEMBL::Mapper::Gap');
225 } else {
226 return @coords unless $coords[0]->isa('Bio::EnsEMBL::Mapper::Gap');
227 }
228 }
229 }
230 }
231  
232 #
233 # the toplevel coordinate system for the region requested *is* the requested region.
234 #
235 if ($fastmap) {
236 return ($seq_region_id, $frm_start, $frm_end, $frm_strand, $other_cs);
237 }
238  
239 my $coord = Bio::EnsEMBL::Mapper::Coordinate->new($seq_region_id, $frm_start,$frm_end, $frm_strand, $other_cs);
240 if ($include_org_coord) {
241 return { 'original' => $coord, 'mapped' => $coord };
242 } else {
243 return $coord;
244 }
245 }
246  
247 =head2 flush
248  
249 Args : none
250 Example : none
251 Description: polymorphism with AssemblyMapper, does nothing
252 Returntype : none
253 Exceptions : none
254 Status : Stable
255  
256 =cut
257  
258 sub flush {}
259  
260 =head2 fastmap
261  
262 Arg [1] : string $frm_seq_region
263 The name of the sequence region to transform FROM
264 Arg [2] : int $frm_start
265 The start of the region to transform FROM
266 Arg [3] : int $frm_end
267 The end of the region to transform FROM
268 Arg [4] : int $strand
269 The strand of the region to transform FROM
270 Arg [5] : Bio::EnsEMBL::CoordSystem
271 The coordinate system to transform FROM
272 Example : @coords = $mapper->fastmap('X', 1_000_000, 2_000_000,
273 1, $chr_cs);
274 Description: Transforms coordinates from one coordinate system
275 to another.
276 Returntype : List of Bio::EnsEMBL::Mapper::Coordinate and/or
277 Bio::EnsEMBL::Mapper:Gap objects
278 Exceptions : thrown if if the specified TO coordinate system is not one
279 of the coordinate systems associated with this mapper
280 Caller : general
281 Status : Stable
282  
283 =cut
284  
285 sub fastmap {
286 my $self = shift;
287 return $self->map(@_,1);
288 }
289  
290 =head2 assembled_CoordSystem
291  
292 Arg [1] : none
293 Example : $cs = $mapper->assembled_CoordSystem
294 Description: Retrieves the assembled CoordSystem from this mapper
295 Returntype : Bio::EnsEMBL::CoordSystem
296 Exceptions : none
297 Caller : internal, AssemblyMapperAdaptor
298 Status : Stable
299  
300 =cut
301  
302 sub assembled_CoordSystem {
303 my $self = shift;
304 return $self->{'toplevel_cs'};
305 }
306  
307 =head2 component_CoordSystem
308  
309 Arg [1] : none
310 Example : $cs = $mapper->component_CoordSystem
311 Description: Retrieves the component CoordSystem from this mapper
312 Returntype : Bio::EnsEMBL::CoordSystem
313 Exceptions : none
314 Caller : internal, AssemblyMapperAdaptor
315 Status : Stable
316  
317 =cut
318  
319 sub component_CoordSystem {
320 my $self = shift;
321 return $self->{'other_cs'};
322 }
323  
324  
325 sub _list {
326 my($self, $frm_seq_region_name, $frm_start, $frm_end, $frm_cs, $seq_regions) = @_;
327  
328 my $mapper = $self->{'mapper'};
329 my $toplevel_cs = $self->{'toplevel_cs'};
330 my $other_cs = $self->{'other_cs'};
331 my $adaptor = $self->adaptor;
332  
333 if($frm_cs->is_top_level()) {
334 throw("The toplevel CoordSystem can only be mapped TO, not FROM.");
335 }
336 if($frm_cs != $other_cs && !$frm_cs->equals($other_cs)) {
337 throw("Coordinate system " . $frm_cs->name . " " . $frm_cs->version .
338 " is neither the assembled nor the component coordinate system " .
339 " of this AssemblyMapper");
340 }
341  
342 my $coord_systems = $self->{'coord_systems'};
343 my $csa = $self->adaptor()->db()->get_CoordSystemAdaptor();
344  
345 #
346 # TBD try to make this more efficient
347 #
348 my $from_rank = $other_cs->rank();
349 foreach my $cs (@$coord_systems) {
350 last if($cs->rank >= $from_rank);
351  
352 #check if a mapping path even exists to this coordinate system
353 my @mapping_path = @{ $csa->get_mapping_path( $cs, $other_cs ) };
354  
355 if(@mapping_path) {
356  
357 # Try to map to this coord system. If we get back any coordinates then
358 # it is our 'toplevel' that we were looking for
359 my $mapper = $adaptor->fetch_by_CoordSystems($other_cs, $cs);
360  
361 my @result;
362  
363 my @tmp;
364 push @tmp, $frm_seq_region_name;
365 my $seq_region_id = @{$self->adaptor()->seq_regions_to_ids($frm_cs, \@tmp)}[0];
366  
367 if($seq_regions) {
368 @result = $mapper->list_seq_regions($frm_seq_region_name, $frm_start,
369 $frm_end, $frm_cs);
370 } else {
371 @result = $mapper->list_ids($frm_seq_region_name, $frm_start,
372 $frm_end, $frm_cs);
373 }
374  
375 return @result if(@result);
376 }
377 }
378  
379 # the toplevel coordinate system for the region requested *is* the
380 return ($frm_seq_region_name);
381  
382  
383 # requested region.
384 if($seq_regions) {
385 return ($frm_seq_region_name);
386 }
387  
388 #this seems a bit silly and inefficient, but it is probably never
389 #called anyway.
390 my $slice_adaptor = $adaptor->db()->get_SliceAdaptor();
391 my $slice = $slice_adaptor->fetch_by_region($other_cs->name(),
392 $frm_seq_region_name,
393 undef,undef,undef,$other_cs);
394 return ($slice_adaptor->get_seq_region_id($slice));
395 }
396  
397  
398  
399 =head2 list_seq_regions
400  
401 Arg [1] : string $frm_seq_region
402 The name of the sequence region of interest
403 Arg [2] : int $frm_start
404 The start of the region of interest
405 Arg [3] : int $frm_end
406 The end of the region to transform of interest
407 Arg [5] : Bio::EnsEMBL::CoordSystem $frm_cs
408 The coordinate system to obtain overlapping ids of
409 Example : foreach $id ($asm_mapper->list_ids('X',1,1000,$ctg_cs)) {...}
410 Description: Retrieves a list of overlapping seq_region names
411 of another coordinate system. This is the same as the
412 list_ids method but uses seq_region names rather internal ids
413 Returntype : List of strings
414 Exceptions : none
415 Caller : general
416 Status : Stable
417  
418 =cut
419  
420 sub list_seq_regions {
421 throw('Incorrect number of arguments.') if(@_ != 5);
422 return _list(@_,1);
423 }
424  
425  
426 =head2 list_ids
427  
428 Arg [1] : string $frm_seq_region
429 The name of the sequence region of interest.
430 Arg [2] : int $frm_start
431 The start of the region of interest
432 Arg [3] : int $frm_end
433 The end of the region to transform of interest
434 Arg [5] : Bio::EnsEMBL::CoordSystem $frm_cs
435 The coordinate system to obtain overlapping ids of
436 Example : foreach $id ($asm_mapper->list_ids('X',1,1000,$chr_cs)) {...}
437 Description: Retrieves a list of overlapping seq_region internal identifiers
438 of another coordinate system. This is the same as the
439 list_seq_regions method but uses internal identfiers rather
440 than seq_region strings
441 Returntype : List of ints
442 Exceptions : thrown if the from CoordSystem is the toplevel coord system
443 thrown if the from CoordSystem is not the one used in the mapper
444 Caller : general
445 Status : Stable
446  
447 =cut
448  
449 sub list_ids {
450 throw('Incorrect number of arguments.') if(@_ != 5);
451 return _list(@_,0);
452 }
453  
454  
455  
456  
457  
458 1;