Microarray/Image.pm untested

filter: Statementstmt 0.0% 0 / 228 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 349 CC 15 SCAR 15.3
CC
15
Cov
0%
set_data
12.0
print_location
12.0
scale
12.0
CRAP 4.6
1 package Microarray::Image;
2  
3 use 5.008;
4 use strict;
5 use warnings;
6 our $VERSION = '1.27';
7  
8 use GD;
9 use GD::Image;
10 use Microarray::File;
11  
12 { package plot;
13
14 sub new {
15 my $class = shift;
16 my $self = { };
17 if (@_){
18 $self->{ _data_object } = shift;
19 bless $self, $class;
20 $self->set_data;
21 } else {
22 bless $self, $class;
23 }
24 return $self;
25 }
26 sub gd_object {
27 my $self = shift;
28 $self->{ _gd_object };
29 }
30 sub parse_args {
31 my $self = shift;
32 my %hArgs = @_;
33 while(my($arg,$val) = each %hArgs){
34 if ($self->can($arg)){
35 $self->$arg($val);
36 } else {
37 die "Microrray::Image ERROR; No parameter '$arg' is defined\n";
38 }
39 }
40 }
41 sub set_data {
42 my $self = shift;
43 if (@_) {
44 $self->{ _data_object } = shift;
45 }
46 die "Microarray::Image ERROR: No data object provided\n" unless $self->data_object;
47 $self->sort_data;
48 ## polymorphic method to process data such as MA RI log2 etc
49 $self->process_data;
50 }
51 ## from data object the background adjusted intensity of ch1 and ch2
52 ## are set in the plot image object
53 sub sort_data {
54 my $self = shift;
55 my $oData = $self->data_object;
56 my $spot_count = $oData->spot_count;
57 my $aCh1 = [];
58 my $aCh2 = [];
59 for (my $i=0; $i<$spot_count; $i++){
60 my $ch1 = $oData->channel1_signal($i);
61 my $ch2 = $oData->channel2_signal($i);
62 next if (($ch1 <= 0)||($ch2<=0));
63 push(@$aCh1, $ch1);
64 push(@$aCh2, $ch2);
65 }
66 $self->{ _ch1_values } = $aCh1;
67 $self->{ _ch2_values } = $aCh2;
68 }
69 sub process_data {
70 my $self = shift;
71 my $aCh1 = $self->ch1_values;
72 my $aCh2 = $self->ch2_values;
73 ## if a process_data method is not set in plot class, simply use the raw intensity data
74 $self->{ _x_values } = $aCh1;
75 $self->{ _y_values } = $aCh2;
76 }
77 ## create a GD image and draw on it the desired plot
78 sub make_plot {
79 my $self = shift;
80 if (@_) {
81 $self->parse_args(@_);
82 }
83 ## Get the x and y coordiantes of the plot, dynamicaly set by the size of the dataset
84 my ($x, $y) = $self->plot_dimensions;
85 ## normalise the plot data according to the plot dimensions
86 $self->plot_values;
87 $self->{ _gd_object } = GD::Image->new($x,$y);
88  
89 $self->plot_outline;
90 $self->plot_spots;
91 $self->return_image;
92 }
93 sub return_image {
94 my $self = shift;
95 my $image = $self->gd_object;
96 $image->png;
97 }
98 sub plot_dimensions {
99 my $self = shift;
100 my $scale = $self->scale;
101 my $aY = $self->y_values;
102 my $aX = $self->x_values;
103 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
104 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
105 my $x = ($x_range * $scale);
106 my $y = ($y_range * $scale);
107 my $x_margin = 100;
108 my $y_margin = 100;
109 $self->{ _x_length } = $x;
110 $self->{ _y_length } = $y;
111 $self->{ _x_margin } = $x_margin;
112 $self->{ _y_margin } = $y_margin;
113 $self->{ _middle } = ($y + $y_margin)/2 ;
114 return(($x + $x_margin), ($y + $y_margin));
115 }
116 sub plot_values {
117 my $self = shift;
118 my $aX = $self->x_values;
119 my $aY = $self->y_values;
120 ## if a plot_values method is not set in plot class, simply plot the raw data
121 $self->{ _plotx_values } = $aX;
122 $self->{ _ploty_values } = $aY;
123 }
124 sub set_plot_background {
125 my $self = shift;
126 my $image = $self->gd_object;
127 ## first allocated colour is set to background
128 my $grey = $image->colorAllocate(180,180,180);
129 }
130 # plot the outline of the diagram, ready for spots to be added
131 sub plot_outline {
132 my $self = shift;
133 my $image = $self->gd_object;
134 my $scale = $self->scale;
135 $self->set_plot_background;
136 my $aY = $self->ploty_values;
137 my $aX = $self->plotx_values;
138 my ($x_min,$x_max,$x_range) = $self->data_range($aX);
139 my ($y_min,$y_max,$y_range) = $self->data_range($aY);
140 my $middle = $self->middle;
141 my $x_length = $self->x_length;
142 my $y_length = $self->y_length;
143 my $x_margin = $self->x_margin;
144 my $y_margin = $self->y_margin;
145 # get colours from the GD colour table
146 my $black = $image->colorExact(0,0,0);
147 ## x axis
148 $image->filledRectangle(50,$y_length+50,$x_length+50,$y_length+50,$black);
149 ## y axis
150 $image->filledRectangle(50,50,50,$y_length+50,$black);
151 ## x axis label
152 my $max_label = int($x_max);
153 my $min_label = int($x_min);
154 $image->string(GD::Font->Giant,($x_length+50)/2,$y_length+80,"(0.5)*Log2(R*G)",$black);
155 $image->string(GD::Font->Giant,(($x_min-$x_min)*$scale)+50,$y_length+70,"$min_label",$black);
156 $image->string(GD::Font->Giant,(($x_max-$x_min)*$scale)+50,$y_length+70,"$max_label",$black);
157 ## y axis label
158 $image->stringUp(GD::Font->Giant,10,$middle,"Log2(R/G)",$black);
159 $image->stringUp(GD::Font->Giant,30,$middle - 25,"0",$black);
160 }
161 ## given a GD image object and X and Y data arrays this method will plot on the
162 ## image each X,Y data point in black
163 sub plot_spots {
164 my $self = shift;
165 my $image = $self->gd_object;
166 my $aX = $self->plotx_values;
167 my $aY = $self->ploty_values;
168 my $black = $image->colorExact(0,0,0);
169 for (my $i=0; $i<@$aX; $i++){
170 my $x = $aX->[$i];
171 my $y = $aY->[$i];
172 next unless ($x && $y);
173 $image->filledEllipse($x,$y,3,3,$black);
174 }
175 }
176 ## finds the minimum, maximum and range of an array of data
177 sub data_range {
178 use Statistics::Descriptive;
179 my $self = shift;
180 my $aData = shift;
181 my $stat = Statistics::Descriptive::Full->new();
182 $stat->add_data(@$aData);
183 my $min = $stat->min();
184 my $max = $stat->max();
185 my $range = $stat->sample_range();
186 return($min,$max,$range);
187 }
188 ## set graduated colours in the GD colour table, for use in the plot
189 ## from red-yellow-green
190 sub make_colour_grads {
191 my $self = shift;
192 my $image = $self->gd_object;
193 my $count = 0;
194 for (my $i = 0; $i<=255; $i+=5){
195 $image->colorAllocate($i,255,0);
196 $image->colorAllocate(255,$i,0);
197 }
198 $image->colorAllocate(0,0,0);
199 }
200 sub print_image {
201 my $self = shift;
202 my $image = $self->gd_object;
203 if (@_) {
204 $self->{ _print_location} = shift;
205 $self->{ _image_name} = shift;
206 }
207 my $name = $self->get_image_name;
208 my $print_location = $self->print_location;
209 open FH, $print_location.$name.".png";
210 binmode(FH);
211 print FH $image->png;
212 close(FH);
213 }
214 sub get_image_name {
215 my $self = shift;
216 unless (defined $self->{ _image_name }){
217 $self->set_image_name;
218 }
219 $self->{ _image_name };
220 }
221 sub set_image_name {
222 my $self = shift;
223 ## need to concat plot type and data source(file name/db id)
224 my $image_name = 'image';
225 $self->{ _image_name } = $image_name;
226 }
227 sub print_location {
228 my $self = shift;
229 if (@_) {
230 $self->{ _print_location } = shift;
231 } else {
232 if (defined $self->{ _print_location }){
233 $self->{ _print_location };
234 } else {
235 die "Microarray::Image ERROR; No print destination defined\n";
236 }
237 }
238 }
239 sub file_path {
240 my $self = shift;
241 $self->{ _file_path };
242 }
243 sub microarray_file {
244 my $self = shift;
245 $self->{ _microarray_file };
246 }
247 sub ch1_values {
248 my $self = shift;
249 $self->{ _ch1_values };
250 }
251 sub ch2_values {
252 my $self = shift;
253 $self->{ _ch2_values };
254 }
255 sub middle {
256 my $self = shift;
257 $self->{ _middle };
258 }
259 sub scale {
260 my $self = shift;
261 if (@_){
262 $self->{ _scale } = shift;
263 } else {
264 if (defined $self->{ _scale }){
265 $self->{ _scale };
266 } else {
267 $self->default_scale;
268 }
269 }
270 }
271 sub x_length {
272 my $self = shift;
273 $self->{ _x_length };
274 }
275 sub y_length {
276 my $self = shift;
277 $self->{ _y_length };
278 }
279 sub x_margin {
280 my $self = shift;
281 $self->{ _x_margin };
282 }
283 sub y_margin {
284 my $self = shift;
285 $self->{ _y_margin };
286 }
287 sub x_values {
288 my $self = shift;
289 $self->{ _x_values };
290 }
291 sub y_values {
292 my $self = shift;
293 $self->{ _y_values };
294 }
295 sub plotx_values {
296 my $self = shift;
297 $self->{ _plotx_values };
298 }
299 sub ploty_values {
300 my $self = shift;
301 $self->{ _ploty_values };
302 }
303 sub ratio_values {
304 my $self = shift;
305 $self->{ _ratio_values };
306 }
307 sub x_coords {
308 my $self = shift;
309 $self->{ _x_coords };
310 }
311 sub y_coords {
312 my $self = shift;
313 $self->{ _y_coords };
314 }
315 sub data_object {
316 my $self = shift;
317 @_ ? $self->{ _data_object } = shift
318 : $self->{ _data_object };
319 }
320 sub data {
321 my $self = shift;
322 $self->{ _data };
323 }
324 }
325  
326  
327 1;
328  
329 __END__