EnsEMBL/TranscriptMapper.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 39 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 286 CC 31 SCAR 39.1
CC
31
Cov
0%
_load_mapper
110.0
genomic2cds
90.0
cds2genomic
20.0
CRAP 50.0
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 TranscriptMapper - A utility class used to perform coordinate conversions
34 between a number of coordinate systems relating to transcripts
35  
36 =head1 SYNOPSIS
37  
38 my $trmapper = Bio::EnsEMBL::TranscriptMapper->new($transcript);
39  
40 @coords = $trmapper->cdna2genomic( 123, 554 );
41  
42 @coords = $trmapper->genomic2cdna( 141, 500, -1 );
43  
44 @coords = $trmapper->genomic2cds( 141, 500, -1 );
45  
46 @coords = $trmapper->pep2genomic( 10, 60 );
47  
48 @coords = $trmapper->genomic2pep( 123, 400, 1 );
49  
50 =head1 DESCRIPTION
51  
52 This is a utility class which can be used to perform coordinate conversions
53 between a number of coordinate systems relating to transcripts.
54  
55 Any transcript object given to TranscriptMapper should have a proper Slice
56 object attached. The Slice provides vital information for the mapping process.
57 After TranscriptMapper has been instantiated, changes to the supplied
58 Transcript will not affect the Mapper's results.
59  
60 =head1 METHODS
61  
62 =cut
63  
64 package Bio::EnsEMBL::TranscriptMapper;
65 $Bio::EnsEMBL::TranscriptMapper::VERSION = '114.0.0_50'; # TRIAL
66 $Bio::EnsEMBL::TranscriptMapper::VERSION = '114.0.050';
67 use strict;
68 use warnings;
69  
70 use Bio::EnsEMBL::Utils::Exception qw(throw);
71  
72 use Bio::EnsEMBL::Mapper;
73 use Bio::EnsEMBL::Mapper::Gap;
74 use Bio::EnsEMBL::Mapper::Coordinate;
75  
76  
77  
78 =head2 new
79  
80 Arg [1] : Bio::EnsEMBL::Transcript $transcript
81 The transcript for which a TranscriptMapper should be created.
82 Example : $trans_mapper = Bio::EnsEMBL::TranscriptMapper->new($transcript)
83 Description: Creates a TranscriptMapper object which can be used to perform
84 various coordinate transformations relating to transcripts.
85 Note that the TranscriptMapper uses the transcript state at the
86 time of creation to perform the conversions, and that a new
87 TranscriptMapper must be created if the Transcript is altered.
88 'Genomic' coordinates are coordinates which are relative to the
89 slice that the Transcript is on.
90 Returntype : Bio::EnsEMBL::TranscriptMapper
91 Exceptions : throws if a transcript is not an argument
92 Caller : Transcript::get_TranscriptMapper
93 Status : Stable
94  
95 =cut
96  
97 sub new {
98 my $caller = shift;
99 my $transcript = shift;
100  
101 my $class = ref($caller) || $caller;
102  
103 if(!ref($transcript) || !$transcript->isa('Bio::EnsEMBL::Transcript')) {
104 throw("Transcript argument is required.");
105 }
106  
107  
108 my $exons = $transcript->get_all_Exons();
109 my $start_phase;
110 if(@$exons) {
111 $start_phase = $exons->[0]->phase;
112 } else {
113 $start_phase = -1;
114 }
115  
116 # Create a cdna <-> genomic mapper and load it with exon coords
117 my $mapper = _load_mapper($transcript);
118  
119 my $self = bless({'exon_coord_mapper' => $mapper,
120 'start_phase' => $start_phase,
121 'cdna_coding_start' => $transcript->cdna_coding_start(),
122 'cdna_coding_end' => $transcript->cdna_coding_end()},
123 $class);
124 }
125  
126  
127 =head2 _load_mapper
128  
129 Arg [1] : Bio::EnsEMBL::Transcript $transcript
130 The transcript for which a mapper should be created.
131 Example : my $mapper = _load_mapper($transcript);
132 Description: loads the mapper
133 Returntype : Bio::EnsEMBL::Mapper
134 Exceptions : none
135 Caller : Internal
136 Status : Stable
137  
138 =cut
139  
140 sub _load_mapper {
141 my $transcript = shift;
142  
143 my $mapper = Bio::EnsEMBL::Mapper->new( 'cdna', 'genomic');
144  
145 my $edits_on = $transcript->edits_enabled();
146 my @edits;
147  
148 if($edits_on) {
149 @edits = @{$transcript->get_all_SeqEdits()};
150 @edits = sort {$a->start() <=> $b->start()} @edits;
151 }
152  
153 my $edit_shift = 0;
154  
155 my $cdna_start = undef;
156  
157 my $cdna_end = 0;
158  
159  
160 foreach my $ex (@{$transcript->get_all_Exons}) {
161 my $gen_start = $ex->seq_region_start();
162 my $gen_end = $ex->seq_region_end();
163  
164 $cdna_start = $cdna_end + 1;
165 $cdna_end = $cdna_start + $ex->length() - 1;
166  
167 my $strand = $ex->strand();
168  
169 # add deletions and insertions into pairs when SeqEdits turned on
170 # ignore mismatches (i.e. treat as matches)
171 if($edits_on) {
172 while(@edits && $edits[0]->start() + $edit_shift <= $cdna_end) {
173  
174 my $edit = shift(@edits);
175 my $len_diff = $edit->length_diff();
176  
177 if($len_diff) {
178 # break pair into two parts, finish first pair just before edit
179  
180 my $prev_cdna_end = $edit->start() + $edit_shift - 1;
181 my $prev_cdna_start = $cdna_start;
182 my $prev_len = $prev_cdna_end - $prev_cdna_start + 1;
183  
184 my $prev_gen_end;
185 my $prev_gen_start;
186 if($strand == 1) {
187 $prev_gen_start = $gen_start;
188 $prev_gen_end = $gen_start + $prev_len - 1;
189 } else {
190 $prev_gen_start = $gen_end - $prev_len + 1;
191 $prev_gen_end = $gen_end;
192 }
193  
194 if($prev_len > 0) { # only create map pair if not boundary case
195 $mapper->add_map_coordinates
196 ('cdna', $prev_cdna_start, $prev_cdna_end, $strand,
197 'genome', $prev_gen_start,$prev_gen_end);
198 }
199  
200 $cdna_start = $prev_cdna_end + 1;
201  
202 if($strand == 1) {
203 $gen_start = $prev_gen_end + 1;
204 } else {
205 $gen_end = $prev_gen_start - 1;
206 }
207  
208 $cdna_end += $len_diff;
209  
210 if($len_diff > 0) {
211 $cdna_start += $len_diff;
212 }
213 else {
214 if($strand == 1) {
215 $gen_start -= $len_diff;
216 } else {
217 $gen_end += $len_diff;
218 }
219 }
220  
221 $edit_shift += $len_diff;
222 }
223 }
224 }
225  
226 my $pair_len = $cdna_end - $cdna_start + 1;
227  
228 if($pair_len > 0) {
229 $mapper->add_map_coordinates('cdna', $cdna_start, $cdna_end, $strand,
230 'genome', $gen_start, $gen_end);
231 }
232 }
233  
234 return $mapper;
235 }
236  
237  
238 =head2 cdna2genomic
239  
240 Arg [1] : $start
241 The start position in cdna coordinates
242 Arg [2] : $end
243 The end position in cdna coordinates
244 Example : @cdna_coords = $transcript_mapper->cdna2genomic($start, $end);
245 Description: Converts cdna coordinates to genomic coordinates. The
246 return value is a list of coordinates and gaps, where the coordinates
247 are relative to the sequence region that the transcript used to construct
248 this mapper was on.
249 Returntype : list of Bio::EnsEMBL::Mapper::Coordinate and
250 Bio::EnsEMBL::Mapper::Gap objects
251 Exceptions : throws if no start or end
252 Caller : general
253 Status : Stable
254  
255 =cut
256  
257  
258 sub cdna2genomic {
259 my ($self, $start, $end, $include_original_region, $cdna_coding_start) = @_;
260  
261 if( !defined $end ) {
262 throw("Must call with start/end");
263 }
264  
265 $cdna_coding_start = defined $cdna_coding_start ? $cdna_coding_start : 1;
266 my $mapper = $self->{'exon_coord_mapper'};
267  
268 return $mapper->map_coordinates('cdna', $start, $end, 1, "cdna", $include_original_region, $cdna_coding_start);
269  
270 }
271  
272  
273 =head2 genomic2cdna
274  
275 Arg [1] : $start
276 The start position in genomic coordinates
277 Arg [2] : $end
278 The end position in genomic coordinates
279 Arg [3] : $strand
280 The strand of the genomic coordinates (default value 1)
281 Example : @coords = $trans_mapper->genomic2cdna($start, $end, $strnd);
282 Description: Converts genomic coordinates to cdna coordinates. The
283 return value is a list of coordinates and gaps. Gaps
284 represent intronic or upstream/downstream regions which do
285 not comprise this transcripts cdna. Coordinate objects
286 represent genomic regions which map to exons (utrs included).
287 Note: A poorly formed Transcript will cause this method to
288 malfunction.
289 Returntype : list of Bio::EnsEMBL::Mapper::Coordinate and
290 Bio::EnsEMBL::Mapper::Gap objects
291 Exceptions : throws if start, end or strand not defined
292 Caller : general
293 Status : Stable
294  
295 =cut
296  
297 sub genomic2cdna {
298 my ($self, $start, $end, $strand) = @_;
299  
300 unless(defined $start && defined $end && defined $strand) {
301 throw("start, end and strand arguments are required\n");
302 }
303  
304 my $mapper = $self->{'exon_coord_mapper'};
305  
306 return $mapper->map_coordinates("genome", $start, $end, $strand,"genomic");
307  
308 }
309  
310  
311 =head2 cds2genomic
312  
313 Arg [1] : int $start
314 start position in cds coords
315 Arg [2] : int $end
316 end position in cds coords
317 Arg [3] boolean (0 or 1) $include_original_region
318 option to include original input coordinate region mappings in the result
319 Example : @genomic_coords = $transcript_mapper->cds2genomic(69, 306);
320 Description: Converts cds coordinates into genomic coordinates. Coordinates returned
321 are relative to sequence region that the transcript used to construct
322 this mapper was on.
323 Returntype : list of Bio::EnsEMBL::Mapper::Gap and
324 Bio::EnsEMBL::Mapper::Coordinate objects
325 Exceptions : throws if no end
326 Caller : general
327 Status : at risk
328  
329 =cut
330  
331 sub cds2genomic {
332 my ( $self, $start, $end, $include_original_region ) = @_;
333  
334 if ( !( defined($start) && defined($end) ) ) {
335 throw("Must call with start and end");
336 }
337  
338 # Move start end into translate cDNA coordinates now.
339 $start = $start +( $self->{'cdna_coding_start'} - 1 ) ;
340 $end = $end + ( $self->{'cdna_coding_start'} - 1 );
341
342 #Check if the start exceeds the cdna_coding_end, if yes, return
343 if($start > $self->{'cdna_coding_end'}){
344 return undef;
345 }
346
347 #Check if the end exceeds the cdna_coding_end, if yes, truncate it otherwise we will be including gaps
348 if($end > $self->{'cdna_coding_end'}){
349 $end = $self->{'cdna_coding_end'};
350 }
351
352 return $self->cdna2genomic( $start, $end, $include_original_region, $self->{'cdna_coding_start'} );
353 }
354  
355 =head2 pep2genomic
356  
357 Arg [1] : int $start
358 start position in peptide coords
359 Arg [2] : int $end
360 end position in peptide coords
361 Example : @genomic_coords = $transcript_mapper->pep2genomic(23, 102);
362 Description: Converts peptide coordinates into genomic coordinates. The
363 coordinates returned are relative to the sequence region that the
364 transcript used to construct this TranscriptMapper was on.
365 Returntype : list of Bio::EnsEMBL::Mapper::Gap and
366 Bio::EnsEMBL::Mapper::Coordinate objects
367 Exceptions : throws if no end
368 Caller : general
369 Status : Stable
370  
371 =cut
372  
373 sub pep2genomic {
374 my ( $self, $start, $end ) = @_;
375  
376 if ( !( defined($start) && defined($end) ) ) {
377 throw("Must call with start and end");
378 }
379  
380 # Take possible N-padding at beginning of CDS into account.
381 my $start_phase = $self->{'start_phase'};
382 my $shift = ( $start_phase > 0 ) ? $start_phase : 0;
383  
384 # Move start end into translate cDNA coordinates now.
385 $start = 3*$start - 2 + ( $self->{'cdna_coding_start'} - 1 ) - $shift;
386 $end = 3*$end + ( $self->{'cdna_coding_start'} - 1 ) - $shift;
387  
388 return $self->cdna2genomic( $start, $end );
389 }
390  
391  
392 =head2 genomic2cds
393  
394 Arg [1] : int $start
395 The genomic start position
396 Arg [2] : int $end
397 The genomic end position
398 Arg [3] : int $strand
399 The genomic strand
400 Example : @cds_coords = $trans_mapper->genomic2cds($start, $end, $strand);
401 Description: Converts genomic coordinates into CDS coordinates of the
402 transcript that was used to create this transcript mapper.
403 Returntype : list of Bio::EnsEMBL::Mapper::Coordinate and
404 Bio::EnsEMBL::Mapper::Gap objects
405 Exceptions : throw if start, end or strand not defined
406 Caller : general
407 Status : Stable
408  
409 =cut
410  
411 sub genomic2cds {
412 my ($self, $start, $end, $strand) = @_;
413  
414 if(!defined($start) || !defined($end) || !defined($strand)) {
415 throw("start, end and strand arguments are required");
416 }
417  
418 if($start > $end + 1) {
419 throw("start arg must be less than or equal to end arg + 1");
420 }
421  
422 my $cdna_cstart = $self->{'cdna_coding_start'};
423 my $cdna_cend = $self->{'cdna_coding_end'};
424  
425 #this is a pseudogene if there is no coding region
426 if(!defined($cdna_cstart)) {
427 #return a gap of the entire requested region, there is no CDS
428 return Bio::EnsEMBL::Mapper::Gap->new($start,$end);
429 }
430  
431 my @coords = $self->genomic2cdna($start, $end, $strand);
432  
433 my @out;
434  
435 foreach my $coord (@coords) {
436 if($coord->isa('Bio::EnsEMBL::Mapper::Gap')) {
437 push @out, $coord;
438 } else {
439 my $start = $coord->start;
440 my $end = $coord->end;
441  
442 if($coord->strand == -1 || $end < $cdna_cstart || $start > $cdna_cend) {
443 #is all gap - does not map to peptide
444 push @out, Bio::EnsEMBL::Mapper::Gap->new($start,$end);
445 } else {
446 #we know area is at least partially overlapping CDS
447
448 my $cds_start = $start - $cdna_cstart + 1;
449 my $cds_end = $end - $cdna_cstart + 1;
450  
451 if($start < $cdna_cstart) {
452 #start of coordinates are in the 5prime UTR
453 push @out, Bio::EnsEMBL::Mapper::Gap->new($start, $cdna_cstart-1);
454  
455 #start is now relative to start of CDS
456 $cds_start = 1;
457 }
458
459 my $end_gap = undef;
460 if($end > $cdna_cend) {
461 #end of coordinates are in the 3prime UTR
462 $end_gap = Bio::EnsEMBL::Mapper::Gap->new($cdna_cend + 1, $end);
463 #adjust end to relative to CDS start
464 $cds_end = $cdna_cend - $cdna_cstart + 1;
465 }
466  
467 #start and end are now entirely in CDS and relative to CDS start
468 $coord->start($cds_start);
469 $coord->end($cds_end);
470  
471 push @out, $coord;
472  
473 if($end_gap) {
474 #push out the region which was in the 3prime utr
475 push @out, $end_gap;
476 }
477 }
478 }
479 }
480  
481 return @out;
482  
483 }
484  
485  
486 =head2 genomic2pep
487  
488 Arg [1] : $start
489 The start position in genomic coordinates
490 Arg [2] : $end
491 The end position in genomic coordinates
492 Arg [3] : $strand
493 The strand of the genomic coordinates
494 Example : @pep_coords = $transcript->genomic2pep($start, $end, $strand);
495 Description: Converts genomic coordinates to peptide coordinates. The
496 return value is a list of coordinates and gaps.
497 Returntype : list of Bio::EnsEMBL::Mapper::Coordinate and
498 Bio::EnsEMBL::Mapper::Gap objects
499 Exceptions : throw if start, end or strand not defined
500 Caller : general
501 Status : Stable
502  
503 =cut
504  
505 sub genomic2pep {
506 my ($self, $start, $end, $strand) = @_;
507  
508 unless(defined $start && defined $end && defined $strand) {
509 throw("start, end and strand arguments are required");
510 }
511  
512 my @coords = $self->genomic2cds($start, $end, $strand);
513  
514 my @out;
515  
516 my $start_phase = $self->{'start_phase'};
517  
518 #take into account possible N padding at beginning of CDS
519 my $shift = ($start_phase > 0) ? $start_phase : 0;
520  
521 foreach my $coord (@coords) {
522 if($coord->isa('Bio::EnsEMBL::Mapper::Gap')) {
523 push @out, $coord;
524 } else {
525  
526 #start and end are now entirely in CDS and relative to CDS start
527  
528 #convert to peptide coordinates
529 my $pep_start = int(($coord->start + $shift + 2) / 3);
530 my $pep_end = int(($coord->end + $shift + 2) / 3);
531 $coord->start($pep_start);
532 $coord->end($pep_end);
533  
534 push @out, $coord;
535 }
536 }
537  
538 return @out;
539 }
540  
541  
542 1;