EnsEMBL/Upstream.pm untested

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 18 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 294 CC 46 SCAR 47.0
CC
46
Cov
0%
_derive_coords
272.0
_bases_to_trim
182.0
transcript
20.0
CRAP 109.4
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::Upstream - Object that defines an upstream region
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::Upstream;
38  
39 my $upstream = Bio::EnsEMBL::Upstream->new(
40 -transcript => $transcript,
41 -length => 2000 # bp
42 );
43  
44 # Retrieve coordinates of upstream region
45 my $upstream_region_start = $upstream->upstart;
46 my $upstream_region_end = $upstream->upend;
47  
48 # Retrieve coordinates in 'downstream' first intron
49 my $intron_region_start = $upstream->downstart;
50 my $intron_region_end = $upstream->downend;
51  
52 # Coordinates are returned in the same scheme as the input transcript.
53 # However, the coordinates of an upstream region can be transformed to
54 # any other scheme using a slice
55  
56 $upstream->transform($slice);
57  
58 # Coordinates can be retrieved in scheme in the same manner as the
59 # above.
60  
61 =head1 DESCRIPTION
62  
63 An object that determines the upstream region of a transcript. Such a
64 region is non-coding and ensures that other genes or transcripts are
65 not present. Ultimately, these objects can be used to looking for
66 promoter elements. To this end, it is also possible to derive a region
67 downstream of the first exon, within the first intron and where promoter
68 elements sometimes are found.
69  
70 =head1 METHODS
71  
72 =cut
73  
74 package Bio::EnsEMBL::Upstream;
75 $Bio::EnsEMBL::Upstream::VERSION = '114.0.0_50'; # TRIAL
76 $Bio::EnsEMBL::Upstream::VERSION = '114.0.050';
77 use strict;
78 use vars qw(@ISA);
79 use Bio::EnsEMBL::DBSQL::DBAdaptor;
80 use Bio::EnsEMBL::DBSQL::SimpleFeatureAdaptor;
81  
82 use Bio::EnsEMBL::Utils::Exception qw(throw);
83 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
84  
85 @ISA = qw(Bio::EnsEMBL::Feature);
86  
87  
88 =head2 new
89  
90 Arg [transcript] : (optional) Bio::EnsEMBL::Transcript
91 Arg [length] : (optional) int $length
92 Example : $upstream = Bio::EnsEMBL::Upstream->new(-transcript => $transcript,
93 -length => 2000);
94 Description: Creates a new upstream object
95 Returntype : Bio::EnsEMBL::Upstream
96 Exceptions : none
97 Caller : Bio::EnsEMBL::Transcript, general
98 Status : Stable
99  
100 =cut
101  
102 sub new {
103 my ($class, @args) = @_;
104 my $self = {};
105
106 bless $self, $class;
107
108 my ($transcript,
109 $length) = rearrange([qw(TRANSCRIPT
110 LENGTH
111 )],@args);
112
113 $self->transcript($transcript) if defined $transcript;
114 $self->length($length) if $length;
115
116 return $self
117 }
118  
119 =head2 transcript
120  
121 Arg : (optional) Bio::EnsEMBL::Transcript
122 Example : $self->transcript($transcript);
123 Description: Getter/setter for transcript object
124 Returntype : Bio::EnsEMBL::Transcript
125 Exceptions : Throws if argument is not undefined
126 or a Bio::EnsEMBL::Transcript
127 Caller : $self->new, $self->_derive_coords,
128 $self->_first_coding_Exon
129 Status : Stable
130  
131 =cut
132  
133  
134 sub transcript {
135 my $self = shift;
136
137 if (@_){
138 $self->{_transcript} = shift;
139
140 if (defined $self->{_transcript}) {
141 throw("Transcript is not a Bio::EnsEMBL::Transcript")
142 if (! $self->{_transcript}->isa("Bio::EnsEMBL::Transcript"));
143 $self->_flush_cache;
144 }
145 }
146
147 return $self->{_transcript}
148 }
149  
150 =head2 length
151  
152 Arg : (optional) int $length
153 Example : $self->length(2000); # bp
154 Description: Getter/setter for upstream region length.
155 Returntype : int
156 Exceptions : Throws if length is requested before it has been set.
157 Caller : $self->new, $self->_derive_coords
158 Status : Stable
159  
160 =cut
161  
162 sub length {
163 my $self = shift;
164
165 if (@_){
166 $self->{_length} = shift;
167 $self->_flush_cache;
168 }
169
170 throw("Region length has not been set.")
171 unless $self->{_length};
172
173 return $self->{_length}
174 }
175  
176 =head2 _flush_cache
177  
178 Arg : none
179 Example : $self->_flush_cache;
180 Description: Empties cached coordinates (called when
181 coordinate scheme or region length has changed).
182 Returntype : none
183 Exceptions : none
184 Caller : $self->length, $self->transform
185 Status : Stable
186  
187 =cut
188  
189 sub _flush_cache {
190 my $self = shift;
191
192 $self->upstart(undef);
193 $self->upend(undef);
194 $self->downstart(undef);
195 $self->downend(undef);
196 }
197  
198 =head2 upstart
199  
200 Arg : none
201 Example : $self->upstart;
202 Description: Returns the start coordinate of the region
203 upstream of the transcript. This coordinate
204 is always the furthest from the translation
205 initiation codon, whereas upend always abutts
206 the translation initiation codon.
207 Returntype : int
208 Exceptions : none
209 Caller : general
210 Status : Stable
211  
212 =cut
213  
214 sub upstart {
215 my $self = shift;
216
217 if (@_) {
218 $self->{_upstart} = shift @_;
219 return
220 }
221  
222 if (! defined $self->{_upstart}) {
223 $self->_derive_coords('up');
224 }
225  
226 return $self->{_upstart}
227 }
228  
229 =head2 upend
230  
231 Arg : none
232 Example : $self->upend;
233 Description: Returns the end coordinate of the region
234 upstream of the transcript. This coordinate
235 always always abutts the translation
236 initiation codon, whereas upstart always
237 returns the coorindate furthest from the
238 translation initiation codon.
239 Returntype : int
240 Exceptions : none
241 Caller : general
242 Status : Stable
243  
244 =cut
245  
246 sub upend {
247 my $self = shift;
248
249 if (@_) {
250 $self->{_upend} = shift @_;
251 return
252 }
253  
254 if (! defined $self->{_upend}) {
255 $self->_derive_coords('up');
256 }
257  
258 return $self->{_upend}
259 }
260  
261 =head2 downstart
262  
263 Arg : none
264 Example : $self->downstart;
265 Description: Returns the start coordinate of the region
266 in the first intron of the transcript. This
267 coordinate is always closest to the first
268 exon (irregardless of strand).
269 Returntype : int
270 Exceptions : none
271 Caller : general
272 Status : Stable
273  
274 =cut
275  
276 sub downstart {
277 my $self = shift;
278
279 if (@_) {
280 $self->{_downstart} = shift @_;
281 return
282 }
283  
284 if (! defined $self->{_downstart}) {
285 $self->_derive_coords('down');
286 }
287  
288 return $self->{_downstart}
289 }
290  
291 =head2 downend
292  
293 Arg : none
294 Example : $self->downend;
295 Description: Returns the end coordinate of the region
296 in the first intron of the transcript. This
297 coordinate is always furthest from the first
298 exon (irregardless of strand).
299 Returntype : int
300 Exceptions : none
301 Caller : general
302 Status : Stable
303  
304 =cut
305  
306 sub downend {
307 my $self = shift;
308  
309 if (@_) {
310 $self->{_downend} = shift @_;
311 return
312 }
313  
314 if (! defined $self->{_downend}) {
315 $self->_derive_coords('down');
316 }
317  
318 return $self->{_downend}
319 }
320  
321 =head2 transform
322  
323 Arg :
324 Example :
325 Description: Not yet implemented
326 Returntype :
327 Exceptions :
328 Caller :
329 Status : At Risk
330  
331 =cut
332  
333  
334 # Over-riding inherited class. As yet unimplemented.
335  
336 sub transform {
337 my $self = shift;
338  
339 throw("No transform method implemented for " . $self);
340 }
341  
342 =head2 derive_upstream_coords
343  
344 Arg : none
345 Example : my ($upstart, $upend)
346 = $self->derive_upstream_coords;
347 Description: Derives upstream coordinates (for
348 compatability with older scripts).
349 Returntype : arrayref
350 Exceptions : none
351 Caller : general
352 Status : Stable
353  
354 =cut
355  
356 sub derive_upstream_coords {
357 my $self = shift;
358  
359 return [$self->upstart, $self->upend]
360 }
361  
362 =head2 derive_downstream_coords
363  
364 Arg : none
365 Example : my ($downstart, $downend)
366 = $self->derive_downstream_coords;
367 Description: Derives downstream coordinates (for
368 compatability with older scripts).
369 Returntype : arrayref
370 Exceptions : none
371 Caller : general
372 Status : Stable
373  
374 =cut
375  
376 sub derive_downstream_coords {
377 my $self = shift;
378  
379 return [$self->downstart, $self->downend]
380 }
381  
382 =head2 _derive_coords
383  
384 Arg : string $direction (either 'up' or 'down').
385 Example : $self->_derive_coords('up');
386 Description: Determines the coordinates of either upstream
387 or downstream region.
388 Returntype : none
389 Exceptions : Throws if argument is not either 'up' or 'down'
390 Caller : $self->upstart, $self->upend, $self->downstart,
391 $self->downend
392 Status : Stable
393  
394 =cut
395  
396 sub _derive_coords {
397 my ($self, $direction) = @_;
398  
399 # Check direction
400 throw("Must specify either \'up\' of \'down\'-stream direction to derive coords.")
401 unless (($direction eq 'up')||($direction eq 'down'));
402  
403 # Put things in easily accessible places.
404 my $core_db_slice_adaptor = $self->transcript->slice->adaptor;
405 my $region_length = $self->length;
406  
407 # Whatever coord system the gene is currently is, transform to the toplevel.
408 my $transcript = $self->transcript->transform('toplevel');
409  
410 # Use our transformed transcript to determine the upstream region coords.
411 # End should always be just before the coding start (like ATG), including 3' UTR.
412 # Start is the outer limit of the region upstream (furthest from ATG).
413  
414 my $region_start;
415 my $region_end;
416  
417 if ($transcript->strand == 1){
418 if ($direction eq 'up'){
419 $region_end = $transcript->coding_region_start - 1;
420 $region_start = $region_end - $region_length;
421 } elsif ($direction eq 'down'){
422 $region_end = $self->_first_coding_Exon->end + 1;
423 $region_start = $region_end + $region_length;
424 }
425 } elsif ($transcript->strand == -1) {
426 if ($direction eq 'up'){
427 $region_end = $transcript->coding_region_end + 1;
428 $region_start = $region_end + $region_length;
429  
430 } elsif ($direction eq 'down'){
431 $region_end = $self->_first_coding_Exon->start - 1;
432 $region_start = $region_end - $region_length;
433 }
434 }
435  
436 # Trim the upstream/downstream region to remove extraneous coding sequences
437 # from other genes and/or transcripts.
438
439 my ($slice_low_coord, $slice_high_coord) = sort {$a <=> $b} ($region_start, $region_end);
440  
441 my $region_slice
442 = $core_db_slice_adaptor->fetch_by_region($transcript->slice->coord_system->name,
443 $transcript->slice->seq_region_name,
444 $slice_low_coord,
445 $slice_high_coord);
446  
447 if ($transcript->strand == 1) {
448 if ($direction eq 'up') {
449 $region_start += $self->_bases_to_trim('left_end', $region_slice);
450 } elsif ($direction eq 'down') {
451 $region_start -= $self->_bases_to_trim('right_end', $region_slice);
452 }
453 } elsif ($transcript->strand == -1) {
454 if ($direction eq 'up') {
455 $region_start -= $self->_bases_to_trim('right_end', $region_slice);
456 } elsif ($direction eq 'down') {
457 $region_start += $self->_bases_to_trim('left_end', $region_slice);
458 }
459 }
460  
461 # Always return start < end
462  
463 ($region_start, $region_end) = sort {$a <=> $b} ($region_start, $region_end);
464  
465 if ($direction eq 'up') {
466 $self->upstart($region_start);
467 $self->upend($region_end);
468 } elsif ($direction eq 'down') {
469 $self->downstart($region_start);
470 $self->downend($region_end);
471 }
472 }
473  
474 =head2 _bases_to_trim
475  
476 Arg : string $end_to_trim (either 'right_end' or
477 'left_end').
478 Arg : Bio::EnsEMBL::Slice
479 Example : $self->_derive_coords('right_end', $slice);
480 Description: Finds exons from other genes/transcripts that
481 invade our upstream/downstream slice and
482 returns the number of bases that should be
483 truncated from the appropriate end of the
484 upstream/downstream region.
485 Returntype : in
486 Exceptions : Throws if argument is not either 'right_end'
487 or 'left_end'
488 Caller : $self->_derive_coords
489 Status : Stable
490  
491 =cut
492  
493 # Method to look for coding regions that invade the upstream region. For
494 # now, this method returns the number of bases to trim. I doesn't yet
495 # do anything special if an exon is completely swallowed (truncates at
496 # the end of the overlapping exon and discards any non-coding sequence
497 # further upstream) or overlaps the 'wrong' end of the region (cases where
498 # two alternate exons share one end of sequence - does this happen?).
499  
500 # The input argument 'end' defines the end of the slice that should be
501 # truncated.
502  
503 sub _bases_to_trim {
504 my ($self, $end_to_trim, $slice) = @_;
505  
506 throw "Slice end argument must be either left_end or right_end"
507 unless ($end_to_trim eq 'right_end' || $end_to_trim eq 'left_end');
508  
509 my @overlap_coords;
510 my $slice_length = $slice->length;
511 my $right_trim = 0;
512 my $left_trim = 0;
513  
514 foreach my $exon (@{$slice->get_all_Exons}){
515 next if $exon->stable_id eq $self->_first_coding_Exon->stable_id;
516  
517 my $start = $exon->start;
518 my $end = $exon->end;
519  
520 # Choose from four possible exon arrangements
521  
522 # -----|********************|----- Slice
523 # --|=========================|--- Exon arrangement 1
524 # ----------|======|-------------- Exon arrangement 2
525 # --|=======|--------------------- Exon arrangement 3
526 # -------------------|=========|-- Exon arrangement 4
527  
528  
529 if ($start <= 0 && $end >= $slice_length) { # exon arrangement 1
530 $right_trim = $slice_length - 1;
531 $left_trim = $slice_length - 1;
532 last;
533  
534 } elsif ($start >= 0 && $end <= $slice_length) { # exon arrangement 2
535 my $this_right_trim = ($slice_length - $start) + 1;
536  
537 $right_trim = $this_right_trim
538 if $this_right_trim > $right_trim;
539  
540 $left_trim = $end
541 if $end > $left_trim;
542  
543 } elsif ($start <= 0 && $end < $slice_length) { # exon arrangement 3
544 $right_trim = $slice_length; # a bit draconian
545 $left_trim = $end
546 if $end > $left_trim;
547  
548 } elsif ($start > 0 && $end >= $slice_length) { # exon arrangement 4
549 my $this_right_trim = ($slice_length - $start) + 1;
550  
551 $right_trim = $this_right_trim
552 if $this_right_trim > $right_trim;
553  
554 $left_trim = $slice_length; # also a bit draconian
555 }
556  
557 }
558  
559 return $right_trim if $end_to_trim eq 'right_end';
560 return $left_trim if $end_to_trim eq 'left_end';
561 }
562  
563 =head2 _first_coding_Exon
564  
565 Arg : none
566 Example : $self->_first_coding_Exon;
567 Description: Finds the first exon of our transcript that
568 contains coding bases.
569 Returntype : Bio::EnsEMBL::Exon
570 Exceptions : none
571 Caller : $self->_derive_coords, $self->_bases_to_trim
572 Status : Stable
573  
574 =cut
575  
576 sub _first_coding_Exon {
577 my $self = shift;
578  
579 unless ($self->{_first_coding_exon}){
580  
581 my $exons = $self->transcript->get_all_translateable_Exons;
582  
583 $self->{_first_coding_exon} = $exons->[0]
584 if $self->transcript->strand == 1;
585 $self->{_first_coding_exon} = $exons->[-1]
586 if $self->transcript->strand == -1;
587 }
588  
589 return $self->{_first_coding_exon}
590 }
591  
592  
593 return 1;