blib/lib/Bio/Graphics/Glyph/processed_transcript.pm untested

filter: Statementstmt 0.0% 0 / 161 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 303 CC 39 SCAR 36.6
CC
39
Cov
0%
create_implied_utrs
110.0
adjust_exons
56.0
guess_options
42.0
CRAP 39.0
1 package Bio::Graphics::Glyph::processed_transcript;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::transcript2);
5 use constant DEFAULT_UTR_COLOR => '#D0D0D0';
6  
7 sub new {
8 my $class = shift;
9 my $self = $class->SUPER::new(@_);
10 $self->guess_options if !defined $self->option('implied_utrs')
11 && !defined $self->option('adjust_exons');
12 $self;
13 }
14  
15 sub guess_options {
16 my $self = shift;
17 my ($exons,$utrs,$cds);
18 foreach ($self->parts) {
19 $exons++ if $_->feature->type =~ /exon/i;
20 $utrs++ if $_->feature->type =~ /utr$/i;
21 $cds++ if $_->feature->type =~ /^cds/i;
22 $self->configure(implied_utrs=>1) if $exons && $cds && !$utrs;
23 $self->configure(adjust_exons=>1) if $exons && $utrs;
24 }
25 }
26  
27 # this option will generate implied UTRs by subtracting the
28 # CDS features from the exons.
29 sub create_implied_utrs {
30 my $self = shift;
31 return if $self->{'.implied_utrs'}++;
32  
33 # parts should be ordered from left to right
34 my @features = sort {$a->start <=> $b->start} map {$_->feature} $self->parts;
35  
36 my @exons = grep {$_->type =~ /^exon/} @features;
37 my @cds = grep {$_->type =~ /^CDS/ } @features;
38 my @old_utr = grep {$_->type =~ /UTR/ } @features;
39  
40 # if there are already UTRs then we don't modify anything
41 return if @old_utr;
42  
43 # if exons or CDS features are missing, then we abandon ship
44 return unless @exons && @cds;
45  
46 my $first_cds = $cds[0];
47 my $last_cds = $cds[-1];
48 my $strand = $self->feature->strand;
49  
50 my $factory = $self->factory;
51  
52 # make the left-hand UTRs
53 for (my $i=0;$i<@exons;$i++) {
54 my $start = $exons[$i]->start;
55 last if $start >= $first_cds->start;
56 my $end = $first_cds->start > $exons[$i]->end ? $exons[$i]->end : $first_cds->start-1;
57 my $utr = Bio::Graphics::Feature->new(-start=>$start,
58 -end=>$end,
59 -strand=>$strand,
60 -type=>$strand >= 0 ? 'five_prime_UTR' : 'three_prime_UTR');
61 unshift @{$self->{parts}},$factory->make_glyph($self->{level}+1,$utr);
62 }
63 # make the right-hand UTRs
64 for (my $i=$#exons; $i>=0; $i--) {
65 my $end = $exons[$i]->end;
66 last if $end <= $last_cds->end;
67 my $start = $last_cds->end < $exons[$i]->start ? $exons[$i]->start : $last_cds->end+1;
68 my $utr = Bio::Graphics::Feature->new(-start=>$start,
69 -end=>$end,
70 -strand=>$strand,
71 -type=>$strand >= 0 ? 'three_prime_UTR' : 'five_prime_UTR');
72 push @{$self->{parts}},$factory->make_glyph($self->{level}+1,$utr);
73 }
74 }
75  
76 # Preprocess the glyph to remove overlaps between UTRs and
77 # exons. The exons are clipped so that UTRs have precedence
78 sub adjust_exons {
79 my $self = shift;
80  
81 return if $self->{'.adjust_exons'}++;
82  
83 # find everything that is not an exon (utrs and cds's)
84 my @parts = sort {$a->{left}<=>$b->{left}} $self->parts;
85 my @exon = grep {$_->feature->type =~ /exon/i} @parts;
86 my %seen = map {$_=>1} @exon;
87 my @other = grep {!$seen{$_}} @parts;
88  
89 my @clipped_parts;
90 my %positions = map {("$_->{left}:$_->{width}" =>1)} @other;
91 my @unique_exons = grep {!$positions{"$_->{left}:$_->{width}"}} @exon;
92  
93 # the first and last exons may need to be clipped if they overlap
94 # with another feature (CDS or UTR)
95 my $first_exon = $unique_exons[0];
96 my $last_exon = $unique_exons[-1];
97  
98 # deal with left hand side first
99 my $e_left = $first_exon->{left};
100 my $e_right = $e_left + $first_exon->{width};
101 for my $other (@other) {
102 my $o_left = $other->{left};
103 my $o_right = $o_left + $other->{width};
104 next if $e_left > $o_right;
105 last if $e_right < $o_left;
106 #dgg- need to skip 3prime/right utr for 1exon; end same as exon
107 last if (@unique_exons == 1 && $o_left > $e_left); #dgg- o_ is 3prime not 5
108 # clip left hand side; may get clipped into oblivion!
109 $first_exon->{left} = $o_right + 1;
110 $first_exon->{width} = $e_right - $first_exon->{left};
111 }
112  
113 # deal with right hand side
114 $e_left = $last_exon->{left};
115 $e_right = $e_left + $last_exon->{width};
116 for (my $i=$#other; $i>=0; $i--) {
117 my $o_left = $other[$i]->{left};
118 my $o_right = $o_left + $other[$i]->{width};
119 next if $e_right < $o_left;
120 last if $e_left > $o_right;
121 # clip right hand side; may get clipped into oblivion!
122 #dgg- !! this always clips to oblivion: $last_exon->{width} = ($e_left - 1) - $last_exon->{left};
123 $last_exon->{width} = $o_left - $last_exon->{left}; #dgg-
124 }
125  
126 $self->{parts} = [ grep {$_->width > 0} sort {$a->{left}<=>$b->{left}} (@other,@unique_exons)];
127 }
128  
129 sub fixup_glyph {
130 my $self = shift;
131 return unless $self->level == 0;
132 $self->create_implied_utrs if $self->option('implied_utrs');
133 $self->adjust_exons if $self->option('implied_utrs') || $self->option('adjust_exons');
134 }
135  
136 sub draw {
137 my $self = shift;
138 $self->fixup_glyph();
139 $self->SUPER::draw(@_);
140  
141 return unless $self->thin_utr;
142 my $gd = shift;
143 my ($dx,$dy) = @_;
144 my $bgcolor = $self->bgcolor;
145  
146 my @parts = $self->parts;
147 for (my $i = 0; $i < @parts; $i++) {
148 if ($i >= 1 && ($parts[$i-1]->is_utr != $parts[$i]->is_utr)) {
149 next unless $parts[$i-1]->end+1 == $parts[$i]->start;
150 my ($x1,$y1,$x2,$y2) = $parts[$i]->bounds($dx,$dy+$self->top+$self->pad_top);
151 my $height = $parts[$i-1]->is_utr ?
152 $parts[$i-1]->height
153 : $parts[$i]->height;
154 my $center = ($y1+$y2)/2;
155 $gd->line($x1,$center-$height/2,$x1,$center+$height/2,$bgcolor); # erase
156 }
157 }
158  
159 }
160  
161 sub boxes {
162 my $self = shift;
163 $self->fixup_glyph();
164 $self->SUPER::boxes(@_);
165 }
166  
167 sub is_utr {
168 my $self = shift;
169 return $self->feature->primary_tag =~ /UTR|untranslated_region/i;
170 }
171  
172 sub thin_utr {
173 my $self = shift;
174 $self->option('thin_utr');
175 }
176  
177 sub utr_color {
178 my $self = shift;
179 return $self->SUPER::bgcolor if $self->thin_utr;
180 return $self->color('utr_color') if $self->option('utr_color');
181 return $self->factory->translate_color(DEFAULT_UTR_COLOR);
182 }
183  
184 sub height {
185 my $self = shift;
186 my $height = $self->SUPER::height;
187 return $height unless $self->thin_utr;
188 return $self->is_utr ? int($height/1.5+0.5) : $height;
189 }
190  
191 sub pad_top {
192 my $self = shift;
193 my $pad_top = $self->SUPER::pad_top;
194 return $pad_top unless $self->thin_utr && $self->is_utr;
195 return $pad_top + int(0.167*$self->SUPER::height + 0.5);
196 }
197  
198 sub bgcolor {
199 my $self = shift;
200 return $self->SUPER::bgcolor unless $self->is_utr;
201 return $self->utr_color;
202 }
203  
204 sub connector {
205 my $self = shift;
206 return 'quill' if $self->option('decorate_introns');
207 return $self->SUPER::connector(@_);
208 }
209  
210  
211 sub _subfeat {
212 my $self = shift;
213 return $self->SUPER::_subfeat(@_) unless ref($self) && $self->{level} == 0 && $self->option('one_cds');
214 my $feature = shift;
215  
216 my @subparts = $feature->get_SeqFeatures(qw(CDS five_prime_UTR three_prime_UTR UTR));
217  
218 # The CDS and UTRs may be represented as a single feature with subparts or as several features
219 # that have different IDs. We handle both cases transparently.
220 my @result;
221 foreach (@subparts) {
222 if ($_->primary_tag =~ /CDS|UTR/i) {
223 my @cds_seg = $_->get_SeqFeatures;
224 if (@cds_seg > 0) { push @result,@cds_seg } else { push @result,$_ }
225 } else {
226 push @result,$_;
227 }
228 }
229 return @result;
230 }
231  
232 1;
233  
234 __END__