Microarray/Image/QC_Plots.pm untested

filter: Statementstmt 0.0% 0 / 411 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 659 CC 40 SCAR 28.2
CC
40
Cov
0%
get_colour
72.0
get_colour
30.0
x_spots
20.0
CRAP 16.7
1 package Microarray::Image::QC_Plots;
2  
3 use 5.008;
4 use strict;
5 use warnings;
6 our $VERSION = '1.5';
7  
8 use Microarray::Image;
9 require Microarray::File;
10  
11  
12 { package ma_plot;
13
14 our @ISA = qw( plot );
15  
16 sub process_data {
17 my $self = shift;
18 my $aCh1 = $self->ch1_values;
19 my $aCh2 = $self->ch2_values;
20 my $aMvalues = [];
21 my $aAvalues = [];
22 for (my $i=0; $i<@$aCh1; $i++){
23 my $ch1 = $aCh1->[$i];
24 my $ch2 = $aCh2->[$i];
25 next unless ($ch1 && $ch2);
26 my $m = $self->calc_m($ch1, $ch2);
27 my $a = $self->calc_a($ch1, $ch2);
28 next unless ($m && $a);
29 push(@$aMvalues, $m);
30 push(@$aAvalues, $a);
31 }
32 $self->{ _y_values } = $aMvalues;
33 $self->{ _x_values } = $aAvalues;
34 }
35 sub plot_values {
36 my $self = shift;
37 my $aX = $self->x_values;
38 my $aY = $self->y_values;
39 my $scale = $self->scale;
40 my $middle = $self->middle;
41 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
42 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
43 my $aXadjusted = [];
44 my $aYadjusted = [];
45 for (my $i=0; $i<@$aX; $i++){
46 my $x = $aX->[$i];
47 my $y = $aY->[$i];
48 next unless ($x && $y);
49 $x = (($x - $x_min) * $scale) + 50;
50 $y = ($middle - ($y * $scale));
51 push(@$aXadjusted, $x);
52 push(@$aYadjusted, $y);
53 }
54 $self->{ _plotx_values } = $aXadjusted;
55 $self->{ _ploty_values } = $aYadjusted;
56 }
57 sub calc_m {
58 my $self = shift;
59 my $ch1 = shift;
60 my $ch2 = shift;
61 if (($ch1 == 0 )||($ch2 == 0)){
62 return();
63 }
64 return(log($ch1/$ch2)/log(2));
65 }
66 sub calc_a {
67 my $self = shift;
68 my $ch1 = shift;
69 my $ch2 = shift;
70 if (($ch1 == 0 )||($ch2 == 0)){
71 return();
72 }
73 return(log(0.5*($ch1*$ch2))/log(2));
74 }
75 sub default_scale {
76 25;
77 }
78 }
79  
80 { package ri_plot;
81  
82 our @ISA = qw( ma_plot );
83  
84 sub process_data {
85 my $self = shift;
86 my $aCh1 = $self->ch1_values;
87 my $aCh2 = $self->ch2_values;
88 my $aRvalues = [];
89 my $aIvalues = [];
90 for (my $i=0; $i<@$aCh1; $i++){
91 my $ch1 = $aCh1->[$i];
92 my $ch2 = $aCh2->[$i];
93 next unless ($ch1 && $ch2);
94 my $r_val = $self->calc_r($ch1, $ch2);
95 my $i_val = $self->calc_i($ch1, $ch2);
96 next unless ($r_val && $i_val);
97 push(@$aRvalues, $r_val);
98 push(@$aIvalues, $i_val);
99 }
100 $self->{ _y_values } = $aRvalues;
101 $self->{ _x_values } = $aIvalues;
102 }
103 sub calc_r {
104 my $self = shift;
105 my $ch1 = shift;
106 my $ch2 = shift;
107 if (($ch1 == 0 )||($ch2 == 0)){
108 return();
109 }
110 return(log($ch1/$ch2)/log(2));
111 }
112 sub calc_i {
113 my $self = shift;
114 my $ch1 = shift;
115 my $ch2 = shift;
116 if (($ch1 == 0 )||($ch2 == 0)){
117 return();
118 }
119 return(log($ch1*$ch2)/log(2));
120 }
121 # plot the outline of the diagram, ready for spots to be added
122 sub plot_outline {
123 my $self = shift;
124 my $image = $self->gd_object;
125 my $scale = $self->scale;
126 ## first allocated colour is set to background
127 my $grey = $image->colorAllocate(180,180,180);
128 my $aM = $self->ploty_values;
129 my $aA = $self->plotx_values;
130 my ($m_min,$m_max,$m_range) = $self->data_range($aM);
131 my ($a_min,$a_max,$a_range) = $self->data_range($aA);
132 my $middle = $self->middle;
133 my $x_length = $self->x_length;
134 my $y_length = $self->y_length;
135 my $x_margin = $self->x_margin;
136 my $y_margin = $self->y_margin;
137 # get colours from the GD colour table
138 my $black = $image->colorExact(0,0,0);
139 ## x axis
140 $image->filledRectangle(50,$y_length+50,$x_length+50,$y_length+50,$black);
141 ## y axis
142 $image->filledRectangle(50,50,50,$y_length+50,$black);
143 ## x axis label
144 my $max_label = int($a_max);
145 my $min_label = int($a_min);
146 $image->string(GD::Font->Giant,($x_length+50)/2,$y_length+80,"Log2(R*G)",$black);
147 $image->string(GD::Font->Giant,(($a_min-$a_min)*$scale)+50,$y_length+70,"$min_label",$black);
148 $image->string(GD::Font->Giant,(($a_max-$a_min)*$scale)+50,$y_length+70,"$max_label",$black);
149 ## y axis label
150 $image->stringUp(GD::Font->Giant,10,$middle,"Log2(R/G)",$black);
151 $image->stringUp(GD::Font->Giant,30,$middle - 25,"0",$black);
152 }
153 sub default_scale {
154 25;
155 }
156 }
157 { package intensity_scatter;
158  
159 our @ISA = qw( plot );
160  
161 sub plot_dimensions {
162 my $self = shift;
163 my $scale = $self->scale;
164 my $aX = $self->ch1_values;
165 my $aY = $self->ch2_values;
166 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
167 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
168 my ($x,$y) = (int((65536/$scale)+1),int((65536/$scale)+1));
169 if ($x_max > 65536){
170 $x = int(($x_max/$scale)+1);
171 }
172 if ($y_max > 65536){
173 $y = int(($y_max/$scale)+1);
174 }
175 my $x_margin = 100;
176 my $y_margin = 100;
177 $self->{ _x_length } = $x;
178 $self->{ _y_length } = $y;
179 $self->{ _x_margin } = $x_margin;
180 $self->{ _y_margin } = $y_margin;
181 $self->{ _middle } = ($y + $y_margin)/2 ;
182 return(($x + $x_margin), ($y + $y_margin));
183 }
184 sub plot_values {
185 my $self = shift;
186 my $aX = $self->x_values;
187 my $aY = $self->y_values;
188 my $scale = $self->scale;
189 my $x_length = $self->x_length;
190 my $y_length = $self->y_length;
191 my $x_margin = $self->x_margin;
192 my $y_margin = $self->y_margin;
193 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
194 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
195 my $aXadjusted = [];
196 my $aYadjusted = [];
197 for (my $i=0; $i<@$aX; $i++){
198 my $x = $aX->[$i];
199 my $y = $aY->[$i];
200 next unless ($x && $y);
201 $x = (($x/$scale))+50;
202 $y = ($y_length-($y/$scale))+50;
203 push(@$aXadjusted, $x);
204 push(@$aYadjusted, $y);
205 }
206 $self->{ _plotx_values } = $aXadjusted;
207 $self->{ _ploty_values } = $aYadjusted;
208 }
209 # plot the outline of the diagram, ready for spots to be added
210 sub plot_outline {
211 my $self = shift;
212 my $image = $self->gd_object;
213 my $scale = $self->scale;
214 ## first allocated colour is set to background
215 my $grey = $image->colorAllocate(180,180,180);
216 my $aX = $self->ch1_values;
217 my $aY = $self->ch2_values;
218 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
219 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
220 my $middle = $self->middle;
221 my $x_length = $self->x_length;
222 my $y_length = $self->y_length;
223 my $x_margin = $self->x_margin;
224 my $y_margin = $self->y_margin;
225 # get colours from the GD colour table
226 my $black = $image->colorExact(0,0,0);
227 ## x axis
228 $image->line(50,$y_length+50,$x_length+50,$y_length+50,$black);
229 ## y axis
230 $image->line(50,50,50,$y_length+50,$black);
231 ## x axis label
232 my ($x_max_label,$y_max_label) = (65536,65536);
233 if ($x_max > 65536){
234 $x_max_label = int($x_max+1);
235 }
236 if ($y_max > 65536){
237 $y_max_label = int($y_max+1);
238 }
239 $image->string(GD::Font->Giant,($x_length+50)/2,$y_length+80,"Channel 1 Intensity",$black);
240 $image->string(GD::Font->Giant,50,$y_length+50,"0",$black);
241 $image->string(GD::Font->Giant,$x_length+50,$y_length+50,"$x_max_label",$black);
242 ## y axis label
243 $image->stringUp(GD::Font->Giant,10,$middle,"Channel 2 Intensity",$black);
244 $image->stringUp(GD::Font->Giant,30,$y_length+50,"0",$black);
245 $image->stringUp(GD::Font->Giant,30,50,"$y_max_label",$black);
246 }
247 sub default_scale {
248 200;
249 }
250 }
251  
252 { package heatmap;
253
254 our @ISA = qw( plot );
255  
256 sub sort_data {
257 my $self = shift;
258 my $oData = $self->data_object;
259 my $spot_count = $oData->spot_count;
260 my $aCh1 = [];
261 my $aCh2 = [];
262 my $aXcoords = [];
263 my $aYcoords = [];
264 for (my $i=0; $i<$spot_count; $i++){
265 my $ch1 = $oData->channel1_signal($i);
266 my $ch2 = $oData->channel2_signal($i);
267 my $x_pos = $oData->x_pos($i);
268 my $y_pos = $oData->y_pos($i);
269 next if (($ch1 <= 0)||($ch2<=0));
270 push(@$aCh1, $ch1);
271 push(@$aCh2, $ch2);
272 push(@$aXcoords, $x_pos);
273 push(@$aYcoords, $y_pos);
274 }
275 $self->{ _ch1_values } = $aCh1;
276 $self->{ _ch2_values } = $aCh2;
277 $self->{ _x_coords } = $aXcoords;
278 $self->{ _y_coords } = $aYcoords;
279 }
280 sub plot_dimensions {
281 my $self = shift;
282 my $aY = $self->y_coords;
283 my $aX = $self->x_coords;
284 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
285 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
286 my $scale = $self->calculate_scale($x_range,$y_range);
287 my $x = int((($x_max + $x_min) / $scale) +1); # include margin, and round up
288 my $y = int((($y_max + $y_min) / $scale) +1);
289 $self->{ _x_length } = $x;
290 $self->{ _y_length } = $y;
291 return($x, $y);
292 }
293 sub plot_values {
294 return;
295 }
296 # we dynamically set the scale to best match the array layout.
297 #Êcan get the spot layout from the data file, if available
298 # or alternatively, the user can set the number of x/y spots
299 # or alternatively, the default scale will be used
300 sub calculate_scale {
301 my $self = shift;
302 my ($x_range,$y_range) = @_;
303 my $x_spots = $self->x_spots;
304 my $y_spots = $self->y_spots;
305 if ($x_spots && $y_spots){
306 my $x_scale = $x_range/$x_spots;
307 my $y_scale = $y_range/$y_spots;
308 if ($x_scale < $y_scale){
309 $self->scale(int($x_scale));
310 } else {
311 $self->scale(int($y_scale));
312 }
313 }
314 return $self->scale;
315 }
316 sub x_spots {
317 my $self = shift;
318 if (@_){
319 $self->{ _x_spots } = shift;
320 } else {
321 unless (defined $self->{ _x_spots }){
322 my $oData = $self->data_object;
323 if ($oData->can('array_columns') && $oData->can('spot_columns')){
324 $self->{ _x_spots } = $oData->array_columns * ($oData->spot_columns + 1);
325 }
326 }
327 $self->{ _x_spots };
328 }
329 }
330 sub y_spots {
331 my $self = shift;
332 if (@_){
333 $self->{ _y_spots } = shift;
334 } else {
335 unless (defined $self->{ _y_spots }){
336 my $oData = $self->data_object;
337 if ($oData->can('array_rows') && $oData->can('spot_rows')){
338 $self->{ _y_spots } = $oData->array_rows * ($oData->spot_rows + 1);
339 }
340 }
341 $self->{ _y_spots };
342 }
343 }
344 sub default_scale {
345 140;
346 }
347 }
348  
349 { package log2_heatmap;
350  
351 our @ISA = qw( heatmap );
352
353 sub process_data {
354 my $self = shift;
355 my $aCh1 = $self->ch1_values;
356 my $aCh2 = $self->ch2_values;
357 my $aRatio = [];
358 for (my $i=0; $i<@$aCh1; $i++){
359 my $ch1 = $aCh1->[$i];
360 my $ch2 = $aCh2->[$i];
361 next unless ($ch1 && $ch2);
362 push(@$aRatio, log($ch1/$ch2)/log(2));
363 }
364 $self->{ _x_values } = $aCh1;
365 $self->{ _y_values } = $aCh2;
366 $self->{ _ratio_values } = $aRatio;
367 }
368 sub plot_outline {
369 my $self = shift;
370 my $image = $self->gd_object;
371 ## first allocated colour is set to background
372 my $grey = $image->colorAllocate(180,180,180);
373 }
374 sub plot_spots {
375 my $self = shift;
376 my $image = $self->gd_object;
377 my $aXcoords = $self->x_coords;
378 my $aYcoords = $self->y_coords;
379 my $aRatio = $self->ratio_values;
380 my $scale = $self->scale;
381 $self->make_colour_grads;
382 for (my $i=0; $i<@$aXcoords; $i++){
383 my $x = $aXcoords->[$i];
384 my $y = $aYcoords->[$i];
385 my $ratio = $aRatio->[$i];
386 next unless ($x && $y && $ratio);
387 $x = int(($x / $scale) + 1);
388 $y = int(($y / $scale) + 1);
389 my $colour = $self->get_colour($ratio);
390 $image->setPixel($x,$y,$colour);
391 }
392 }
393 sub get_colour {
394 my $self = shift;
395 my $ratio = shift;
396 my $image = $self->gd_object;
397 my $colour;
398 if ($ratio <= -1.1){
399 $colour = $image->colorExact(255,0,0);
400 } elsif ($ratio >= 1.1){
401 $colour = $image->colorExact(0,255,0);
402 } elsif ((0.1 > $ratio)&&($ratio > -0.1)) {
403 $colour = $image->colorExact(255,255,0);
404 } elsif ($ratio >= 0.1) {
405 my $red_hue = 255 - (255 * ($ratio - 0.1));
406 $colour = $image->colorClosest($red_hue,255,0); # reducing red, closer to green
407 } else {
408 my $green_hue = 255 + (255 * ($ratio + 0.1));
409 $colour = $image->colorClosest(255,$green_hue,0); # reducing green, closer to red
410 }
411 return($colour);
412 }
413 sub make_colour_grads {
414 my $self = shift;
415 my $image = $self->gd_object;
416 $image->colorAllocate(255,255,0);
417 for (my $i = 0; $i<255; $i+=2){
418 $image->colorAllocate($i,255,0); ## Add red -> green = yellow
419 $image->colorAllocate(255,$i,0); ## Add green -> red = yellow
420 }
421 }
422 }
423  
424 { package intensity_heatmap;
425  
426 our @ISA = qw( heatmap );
427  
428 sub plot_outline {
429 my $self = shift;
430 my $image = $self->gd_object;
431 ## first allocated colour is set to background
432 my $black = $image->colorAllocate(0,0,0);
433 }
434 sub plot_channel {
435 my $self = shift;
436 @_ ? $self->{ _plot_channel } = shift
437 : $self->{ _plot_channel };
438 }
439 sub plot_spots {
440 my $self = shift;
441 my $image = $self->gd_object;
442 my $aXcoords = $self->x_coords;
443 my $aYcoords = $self->y_coords;
444 my $scale = $self->scale;
445 my $plot_channel;
446 if ($self->plot_channel && ($self->plot_channel == 2)){
447 $plot_channel = 'ch2_values';
448 } else {
449 $plot_channel = 'ch1_values';
450 }
451 my $aValues = $self->$plot_channel;
452 $self->make_colour_grads;
453 for (my $i=0; $i<@$aXcoords; $i++){
454 my $x = $aXcoords->[$i];
455 my $y = $aYcoords->[$i];
456 my $value = $aValues->[$i];
457 next unless ($x && $y && $value);
458 $x = int(($x / $scale) + 1);
459 $y = int(($y / $scale) + 1);
460 my $colour = $self->get_colour($value);
461 $image->setPixel($x,$y,$colour);
462 }
463 }
464 sub get_colour {
465 my $self = shift;
466 my $value = shift;
467 my $image = $self->gd_object;
468 my $colour;
469 if ($value == 0) { ## if the value is 0 colour black
470 $colour = $image->colorExact(0,0,0);
471 } elsif ($value <= 13000) { ## colour towards blue
472 my $blue_hue = $value / 50.9;
473 $colour = $image->colorClosest(0,0,$blue_hue);
474 } elsif ($value <= 26000) { ## colour towards turquoise
475 my $turquoise_hue = ($value - 13000) / 50.9;
476 $colour = $image->colorClosest(0,$turquoise_hue,255);
477 } elsif ($value <= 39000) { ## colour towards green
478 my $green_hue = ($value - 26000) / 50.9;
479 $colour = $image->colorClosest(0,255,255-$green_hue);
480 } elsif ($value <= 52000) { ## colour towards yellow
481 my $yellow_hue = ($value - 39000) / 50.9;
482 $colour = $image->colorClosest($yellow_hue,255,0);
483 } elsif ($value < 65000) { ## colour towards red
484 my $red_hue = ($value - 52000) / 50.9;
485 $colour = $image->colorClosest(255,255-$red_hue,0);
486 } elsif ($value >= 65000) { ## if value is saturated colour white
487 $colour = $image->colorExact(255,255,255);
488 }
489 return($colour);
490 }
491 # set a rainbow of graduated colours in the GD colour table, for use in the plot
492 sub make_colour_grads {
493 my $self = shift;
494 my $image = $self->gd_object;
495 my $count = 0;
496 $image->colorAllocate(0,0,0);
497 for (my $i = 5; $i<=255; $i+=5){
498 $image->colorAllocate(0,0,$i); ## Add blue up to 255 -> blue
499 $image->colorAllocate(0,$i,255); ## Add green up to 255 -> turquise
500 $image->colorAllocate(0,255,255-$i); ## Reduce blue -> green
501 $image->colorAllocate($i,255,0); ## Add red up to 255 -> yellow
502 $image->colorAllocate(255,255-$i,0); ## Reduce green -> red
503 }
504 }
505 }
506  
507  
508 1;
509  
510 __END__