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

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 178 CC 12 SCAR 25.9
CC
12
Cov
0%
draw_kd_plot
30.0
height
12.0
draw_component
12.0
CRAP 13.3
1 package Bio::Graphics::Glyph::protein;
2  
3 use strict;
4 use base qw(Bio::Graphics::Glyph::cds);
5  
6  
7 # turn off description
8 sub description { 0 }
9  
10 # turn off label
11 # sub label { 1 }
12  
13 sub height {
14 my $self = shift;
15 my $font = $self->mono_font;
16 return $self->dna_fits ? 2 * $font->height
17 : $self->do_kd ? $self->SUPER::height
18 : 0;
19 }
20  
21 sub do_kd {
22 my $self = shift;
23 my $do_kd = $self->option('do_kd');
24 return if defined($do_kd) && !$do_kd;
25 return 1;
26 }
27  
28 sub draw_component {
29 my $self = shift;
30 my $gd = shift;
31 my ($x1,$y1,$x2,$y2) = $self->bounds(@_);
32
33 if ($self->protein_fits && $self->{cds_translation}) {
34 return $self->SUPER::draw_component($gd,@_);
35 }
36 elsif ($self->{cds_translation}) {
37 $self->draw_kd_plot($gd,$self->{cds_translation},$x1,$y1,$x2,$y2);
38 }
39 else {
40 $self->Bio::Graphics::Glyph::generic::draw_component($gd,@_);
41 }
42 }
43  
44 sub do_cds_translation { return 1 }
45  
46 sub draw_protein {
47 my $self = shift;
48  
49 my ($gd,$protein,$x1,$y1,$x2,$y2) = @_;
50 my $pixels_per_base = $self->scale;
51
52 my $feature = $self->feature;
53  
54 my @bases = split '', $protein;
55 my $color = $self->fgcolor;
56 my $font = $self->mono_font;
57 my $lineheight = $font->height;
58 $y1 -= $lineheight/2 - 3;
59  
60 my $start = $self->panel->left + $self->map_pt($feature->start);
61 my $end = $self->panel->left + $self->map_pt($feature->end);
62  
63 my $offset = int(($x1-$start-1)/$pixels_per_base);
64  
65 for (my $i=$offset;$i<@bases;$i++) {
66 my $x = $start + $i * $pixels_per_base;
67 next if $x+1 < $x1;
68 last if $x > $x2;
69 $gd->char($font,$x+2,$y1,$bases[$i],$color);
70 }
71  
72 }
73  
74 sub draw_kd_plot {
75 my $self = shift;
76 my $gd = shift;
77 my $protein = shift;
78 my ($x1,$y1,$x2,$y2) = @_;
79  
80 my $kd_window = $self->option('kd_window') || 9;
81  
82 # Calculate the KD plot ...
83  
84 my %scores = ( I => 4.5,
85 V => 4.2,
86 L => 3.8,
87 F => 2.8,
88 C => 2.5,
89 M => 1.9,
90 A => 1.8,
91 G => -0.4,
92 T => -0.7,
93 W => -0.9,
94 S => -0.8,
95 Y => -1.3,
96 P => -1.6,
97 H => -3.2,
98 E => -3.5,
99 Q => -3.5,
100 D => -3.5,
101 N => -3.5,
102 K => -3.9,
103 R => -4.5,
104 );
105
106 my @datapoints;
107 my @seq = split('', uc($protein));
108  
109 $kd_window = $kd_window < scalar(@seq) ? $kd_window : scalar(@seq);
110  
111 my $maxkd = 4.5;
112 my $minkd = -4.5;
113  
114 my $kd = 0;
115 for (my $i = 0 ; $i < @seq && $i < $kd_window ; $i++) {
116 $kd += $scores{$seq[$i]} || 0;
117 }
118  
119 my $content = $kd / $kd_window;
120 push @datapoints, $content;
121  
122 for (my $i = $kd_window; $i < @seq; $i++) {
123 $kd -= $scores{$seq[$i-$kd_window]} || 0;
124 $kd += $scores{$seq[$i]} || 0;
125 $content = $kd / $kd_window;
126 push @datapoints, $content;
127 }
128  
129 my $scale = $maxkd - $minkd;
130 foreach (my $i = 0; $i < @datapoints; $i++) {
131 $datapoints[$i] = ($datapoints[$i] - $minkd) / $scale;
132 }
133  
134 # Calculate values that will be used in the layout
135
136 my $bin_height = $y2-$y1;
137 my $fgcolor = $self->fgcolor;
138 my $bgcolor = $self->factory->translate_color($self->panel->gridcolor);
139 my $axiscolor = $self->color('axis_color') || $fgcolor;
140  
141 # Draw the axes
142
143 $gd->line($x1, $y1, $x1, $y2, $axiscolor);
144 $gd->line($x2-2,$y1, $x2-2,$y2, $axiscolor);
145 $gd->line($x1, $y1, $x1+3,$y1, $axiscolor);
146 $gd->line($x1, $y2, $x1+3,$y2, $axiscolor);
147 $gd->line($x1, ($y2+$y1)/2,$x1+3,($y2+$y1)/2,$axiscolor);
148 $gd->line($x2-4,$y1, $x2-1, $y1, $axiscolor);
149 $gd->line($x2-4,$y2, $x2-1, $y2, $axiscolor);
150 $gd->line($x2-4,($y2+$y1)/2,$x2-1,($y2+$y1)/2,$axiscolor);
151 $gd->line($x1+5,$y2, $x2-5,$y2, $bgcolor);
152 $gd->line($x1+5,($y2+$y1)/2,$x2-5,($y2+$y1)/2,$bgcolor);
153 $gd->line($x1+5,$y1, $x2-5,$y1, $bgcolor);
154 my $label = 'Kyte-Doolittle hydropathy plot';
155 $gd->string($self->mono_font,$x1+5,$y1,$label,$axiscolor)
156 if $bin_height > $self->mono_font->height*2 &&
157 $self->width > $self->mono_font->width*length($label);
158  
159 $gd->string($self->mono_font,$x2-20,$y1,$maxkd,$axiscolor)
160 if $bin_height > $self->mono_font->height*2.5;
161 $gd->string($self->mono_font,$x2-20,$y2-$self->mono_font->height,$minkd,$axiscolor)
162 if $bin_height > $self->mono_font->height*2.5;
163  
164 my $graphwidth = $x2 - $x1;
165 $scale = $graphwidth / (@datapoints + $kd_window - 1);
166 for (my $i = 1; $i < @datapoints; $i++) {
167 my $x = $i + $kd_window / 2;
168 my $xlo = $x1 + ($x - 1) * $scale;
169 my $xhi = $x1 + $x * $scale;
170 my $y = $y2 - ($bin_height*$datapoints[$i]);
171 $gd->line($xlo, $y2 - ($bin_height*$datapoints[$i-1]),
172 $xhi, $y,
173 $fgcolor);
174 }
175 }
176  
177 sub make_key_feature {
178 my $self = shift;
179 my @gatc = qw(A C D E F G H I K L M N P Q R S T V W Y);
180 my $offset = $self->panel->offset;
181 my $scale = 1/$self->scale; # base pairs/pixel
182  
183 my $start = $offset+1;
184 my $stop = $offset+100*$scale;
185 my $feature =
186 Bio::Graphics::Feature->new(-start=> $start,
187 -stop => $stop,
188 -seq => join('',map{$gatc[rand 4]} (1..500)),
189 -name => $self->option('key'),
190 -strand => '+1',
191 );
192 $feature;
193 }
194  
195 1;
196  
197 __END__