| 1 | package Bio::Graphics::Util; | ||
| 2 | |||
| 3 | # Non object-oriented utilities used here-and-there in Bio::Graphics modules | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::Graphics::Util - non-object-oriented utilities used in Bio::Graphics modules | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | require Exporter; | ||
| 13 | use base qw(Exporter); | ||
| 14 | use vars '@EXPORT','@EXPORT_OK'; | ||
| 15 | @EXPORT = 'frame_and_offset'; | ||
| 16 | use Bio::Root::Version; | ||
| 17 | |||
| 18 | =over 4 | ||
| 19 | |||
| 20 | =item ($frame,$offset) = frame_and_offset($pos,$strand,$phase) | ||
| 21 | |||
| 22 | Calculate the reading frame for a given genomic position, strand and | ||
| 23 | phase. The offset is the offset from $pos to the first nucleotide | ||
| 24 | of the reading frame. | ||
| 25 | |||
| 26 | In a scalar context, returns the frame only. | ||
| 27 | |||
| 28 | =back | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub frame_and_offset { | ||
| 33 | my ($pos,$strand,$phase) = @_; | ||
| 34 | $strand ||= +1; | ||
| 35 | $phase ||= 0; | ||
| 36 | my $codon_start = $strand >= 0 | ||
| 37 | ? $pos + $phase | ||
| 38 | : $pos - $phase; # probably wrong | ||
| 39 | my $frame = ($codon_start-1) % 3; | ||
| 40 | my $offset = $strand >= 0 ? $phase : -$phase; | ||
| 41 | return wantarray ? ($frame,$offset) : $frame; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | 1; |