lib/Microarray/DatasetImageMaker.pm untested

filter: Statementstmt 0.0% 0 / 217 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 362 CC 26 SCAR 28.8
CC
26
Cov
0%
_makeMatrixImage
56.0
_get_log_color
42.0
makeImage
12.0
CRAP 17.8
1 package Microarray::DatasetImageMaker;
2  
3 # License information (the MIT license)
4  
5 # Copyright (c) 2003 Christian Rees, Janos Demeter, John Matese, Gavin
6 # Sherlock; Stanford University
7  
8 # Permission is hereby granted, free of charge, to any person
9 # obtaining a copy of this software and associated documentation files
10 # (the "Software"), to deal in the Software without restriction,
11 # including without limitation the rights to use, copy, modify, merge,
12 # publish, distribute, sublicense, and/or sell copies of the Software,
13 # and to permit persons to whom the Software is furnished to do so,
14 # subject to the following conditions:
15  
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18  
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
27  
28  
29 # Class: Microarray::DatasetImageMaker
30 #
31 # Microarray::DatasetImageMaker is a class that accepts a
32 # Microarray::Dataset object and produces the images based on the meta
33 # data contained in the dataset object. It is meant to separate the
34 # image production code into something a little more manageable.
35 #
36 # Usage:
37 #
38 # $imageMaker = Microarray::DatasetImageMaker(dataset=>$dataSetObj);
39 #
40 # where $dataset is an CdtDataset object class or another concrete
41 # Dataset class..
42 #
43 # Future Plans: This class should definitely be augmented to make
44 # images based on the various tree files
45  
46  
47 use strict;
48 use GD;
49 use File::Basename;
50 use vars qw($VERSION $dbg);
51  
52 use Microarray::Config;
53  
54 $VERSION = "0.1";
55  
56 my $PACKAGE = 'Microarray::DatasetImageMaker';
57 my $kImgType = $PACKAGE.'::__imgType';
58 my $kDataset = $PACKAGE.'::__dataset';
59 my $kContrast = $PACKAGE.'::__contrast';
60 my $kColorScheme = $PACKAGE.'::__colorScheme';
61 my $kName = $PACKAGE.'::__name';
62 my $kImagePath = $PACKAGE.'::__imagePath';
63 my $kDataPath = $PACKAGE.'::__dataPath';
64 my $kHeight = $PACKAGE.'::__height';
65 my $kWidth = $PACKAGE.'::__width';
66  
67 my %kColorSchemeTranslation = Microarray::Config->ColorSchemeTranslationHash;
68  
69  
70 # debug
71 $dbg = 0;
72  
73  
74 #####################################################################
75 sub new {
76 #####################################################################
77 # this is jsut a simple constructor, returning a DatasetImageMaker
78  
79 my $class = shift;
80 my $self = { };
81 bless ($self, $class);
82  
83 $self->__setImageType();
84  
85 return $self;
86  
87 }
88  
89  
90 #####################################################################
91 sub __setImageType {
92 #####################################################################
93 #
94  
95 my $self = shift;
96  
97 $self->{$kImgType} = Microarray::Config->ImageType;
98  
99 }
100  
101 #####################################################################
102 sub imageType {
103 #####################################################################
104 # returns the suffix for the image type being used for a newly
105 # constructed dataset
106  
107 return $_[0]->{$kImgType};
108 }
109  
110 #####################################################################
111 sub _dataset {
112 #####################################################################
113 # returns the dataset object used to initialize the imageMaker, if any
114  
115 return $_[0]->{$kDataset};
116 }
117  
118  
119 #####################################################################
120 sub _contrast {
121 #####################################################################
122 # returns contrast used initialize the imageMaker, either belonging to
123 # the dataset or other optional argument
124  
125 my $self = shift;
126  
127 if (!$self->{$kContrast} && ($self->_dataset)) {
128  
129 $self->{$kContrast} = $self->_dataset->contrast;
130  
131 }
132  
133 return $self->{$kContrast};
134 }
135  
136  
137 #####################################################################
138 sub _name {
139 #####################################################################
140 # returns file base name of the dataset used initialize the
141 # imageMaker, either belonging to the dataset or other optional
142 # argument
143  
144 my $self = shift;
145 if (!$self->{$kName} && ($self->_dataset)) {
146 $self->{$kName} = $self->_dataset->fileBaseName;
147 }
148 return $self->{$kName};
149 }
150  
151  
152 #####################################################################
153 sub _imagePath {
154 #####################################################################
155 # returns image outpath of the dataset used initialize the imageMaker,
156 # either belonging to the dataset or other optional argument
157  
158 my $self = shift;
159 if (!$self->{$kImagePath} && ($self->_dataset)) {
160  
161 $self->{$kImagePath} = $self->_dataset->imagepath;
162 }
163 return $self->{$kImagePath};
164 }
165  
166  
167 #####################################################################
168 sub _dataPath {
169 #####################################################################
170 # returns data outpath of the dataset used initialize the imageMaker,
171 # either belonging to the dataset or other optional argument
172  
173 my $self = shift;
174 if (!$self->{$kDataPath} && ($self->_dataset)) {
175  
176 $self->{$kDataPath} = $self->_dataset->datapath;
177 }
178 return $self->{$kDataPath};
179 }
180  
181  
182 #####################################################################
183 sub _height {
184 #####################################################################
185 # returns height of the dataset used initialize the imageMaker, either
186 # belonging to the dataset or other optional argument
187  
188 my $self = shift;
189 if (!$self->{$kHeight} && ($self->_dataset)) {
190 $self->{$kHeight} = $self->_dataset->height;
191 }
192 return $self->{$kHeight};
193 }
194  
195  
196  
197 #####################################################################
198 sub _width {
199 #####################################################################
200 # returns width used initialize the imageMaker, either belonging to
201 # the dataset or other optional argument
202  
203 my $self = shift;
204  
205 if (!$self->{$kWidth} && ($self->_dataset)) {
206  
207 $self->{$kWidth} = $self->_dataset->width;
208  
209 }
210  
211 return $self->{$kWidth};
212 }
213  
214  
215 #####################################################################
216 sub _colorscheme {
217 #####################################################################
218 # returns colorscheme used initialize the imageMaker, either belonging
219 # to the dataset or other optional argument
220  
221 my $self = shift;
222  
223 if (!$self->{$kColorScheme} && ($self->_dataset)) {
224  
225 $self->{$kColorScheme} = $self->_dataset->colorScheme;
226  
227 }
228  
229 return $self->{$kColorScheme};
230 }
231  
232  
233  
234 #####################################################################
235 sub _load_image {
236 #####################################################################
237 #
238 my $self = shift;
239 my $filename = shift;
240  
241 open(IN, $filename) || die "cannot open $filename! $!\n";
242  
243 my $funcname = "newFrom".ucfirst($self->imageType);
244  
245 my $image = GD::Image->$funcname(\*IN);
246  
247 close(IN);
248  
249 return $image;
250  
251 }
252  
253 #####################################################################
254 sub __checkAndSetDataset {
255 #####################################################################
256 # does some quick check on the dataset object being passed and sets it
257 # as a known attribute
258  
259 my $self = shift;
260 my %args = @_;
261  
262 if (ref($args{'dataset'}) =~ m/Dataset/) {
263  
264 # they must pass a dataset argument and it must be some kind
265 # of dataset (attempting a little flexibility, here (MAGE-ML
266 # dataset, cdt dataset, generic dataset, etc...))
267  
268 $self->{$kDataset} = $args{'dataset'};
269  
270 }else{
271  
272 die "$PACKAGE requires a valid Dataset object, passed in by named argumnt";
273 }
274  
275 return;
276  
277 }
278  
279  
280 #####################################################################
281 sub makeImage {
282 #####################################################################
283 # this is the major method of DatasetImageMaker which produces the
284 # images. It takes two named arguments, a CdtDataset arguement
285 # ('dataset') and an image type ('type'). Current known types are
286 # 'matrix' and 'header'
287 #
288 # Usage:
289 # $imageMaker->makeImage(dataset=>$ds,
290 # type=>'matrix');
291  
292 my $self = shift;
293 my %args = @_;
294  
295 $self->__checkAndSetDataset(%args);
296  
297 my $type = $args{'type'};
298  
299 # make a matrix image
300 if ($type eq "matrix") {
301 # load data matrix
302 $self->_load_matrix();
303  
304 print "Updating Image data with contrast of ".$self->_contrast." and colorscheme ".$self->_colorscheme." ...\n";
305  
306 my $contrast = $self->_contrast;
307 my $colscheme = $self->_colorscheme;
308  
309 # generate matrix gif
310 $self->_makeMatrixImage( $contrast, $colscheme );
311  
312 }elsif($type eq "header") {
313  
314 # make experiment names image
315 $self->_makeExptImage();
316  
317 }else{
318  
319 print "Unknown image type passed to $PACKAGE->makeImage. Known types are matrix and header.\n\n"; }
320  
321 return;
322 }
323  
324  
325 #####################################################################
326 sub _load_matrix {
327 #####################################################################
328  
329 my $self = shift;
330  
331 my ($matrix, $index);
332  
333 my $filename = $self->_dataPath.$self->_name.".data_matrix";
334  
335 $matrix = [ ];
336  
337 open(IN, $filename) || die "cannot open _matrix_ $filename $!\n";
338  
339 while (<IN>) {
340  
341 chomp;
342 my @row = split("\t", $_, -1);
343 $index = shift @row;
344  
345 $$matrix[$index] = \@row;
346
347 }
348  
349 $self->{MATRIX} = $matrix;
350  
351 }
352  
353  
354  
355 #####################################################################
356 sub _makeMatrixImage { # Perl version of GIF generation
357 #####################################################################
358 #
359
360 my $self = shift;
361  
362 my $contrast = shift; # the contrast value to use
363 my $color = shift; # the color scheme to use
364  
365 printf "cols: %s\n", $color;
366  
367 my $outdir = $self->_imagePath; # where to write image file
368 my $name = $self->_name; # name of the image file
369 my $aref = $self->{MATRIX}; # reference to an array of tab delimited rows of the data matrix
370  
371 my $num_rows = $self->_height;
372 my $num_columns = $self->_width; # subtract one, because the first value in row is index number, not data
373  
374 my $bm_width = $num_columns;
375 my $bm_height = $num_rows;
376  
377 # my $color = 0;
378 my $missing_value = 80;
379
380 my %colors;
381
382 my $image = new GD::Image($bm_width,$bm_height); # create a new bitmap the size of the matrix
383
384 $colors{grey} = $image->colorAllocate($missing_value, $missing_value, $missing_value);
385 $colors{black} = $image->colorAllocate(0,0,0); # first color allocated becomes background
386 $colors{white} = $image->colorAllocate(255,255,255);
387  
388 # translating to a controlled parameter names
389 $color = $kColorSchemeTranslation{$color};
390  
391 if ($color eq "rg") { # red green
392 $colors{red} = $image->colorAllocate(255,0,0);
393 $colors{green} = $image->colorAllocate(0,255,0);
394 } elsif ($color eq "yb") { # yellow blue
395 $colors{red} = $image->colorAllocate(255,255,0);
396 $colors{green} = $image->colorAllocate(0,0,255);
397 }
398  
399 &_shadeAllocate($image, \%colors, $color);
400
401 for(my $y=0; $y<=$num_rows; $y++) {
402
403 for (my $x=0; $x<=$num_columns; $x++) {
404  
405 # my $color = _get_log_color( $self->getMatrixValue($x,$y), $contrast, \%colors );
406 my $color = _get_log_color( $self->{MATRIX}[$y][$x], $contrast, \%colors );
407 # modified for direct access to save extra method call for each pixel
408  
409 $image->setPixel($x, $y, $color);
410 }
411
412 if ( !( $y % 100 ) )
413 {
414 print "row $y\n" if ($dbg) ;
415 }
416  
417 }
418
419 # print the MATRIX image
420
421 open (MATRIX, ">$outdir$name.data_matrix.".$self->imageType) || die($!);
422 binmode MATRIX;
423 print MATRIX ($self->imageType eq "gif" ? $image->gif : $image->png);
424 close MATRIX;
425  
426 print "saving image as ".$self->imageType."\n" if ($dbg) ;
427  
428 return;
429
430 }
431  
432  
433 #####################################################################
434 sub _makeExptImage {
435 #####################################################################
436 #
437  
438 my $self = shift;
439  
440 my $outdir = $self->_imagePath();
441 my $name = $self->_name();
442  
443 my $line_number = $self->_width; # = number of items in list
444 my $line_maxlength = 0;
445 my $font_height = 10; # tiny
446 my $font_width = 5;
447 ## my $font_height = 13; # small
448 ## my $font_width = 7;
449 ## my $font = "gdTinyFont"; # not used, see below
450  
451 my $bm_width = 0;
452 my $bm_height = 0;
453 my $white;
454 my $black;
455 my $i = 0;
456  
457 $line_number = 0;
458
459 my @expts;
460  
461 # Should definitely investigate the use of something public, or
462 # write a new accessor for experiment names instead of being
463 # forced to use private method of the CdtDataset, like so...
464 # my $exptNamesRef = $self->_dataset->__cdtFileObject->columnNamesArrayRef;
465
466 for (my $i=0;$i<$self->_width();$i++) {
467 # trying it here, but this seems a little cludgy...
468 push (@expts, $self->_dataset->experiment($i, 'NAME'));
469 # push @expts, $$exptNamesRef[$i]; # above line replaces this
470 }
471
472 foreach my $line ( @expts ) {
473 if (length($line) > $line_maxlength) {
474 $line_maxlength = length($line);
475 }
476 $line_number++;
477 }
478
479 $bm_width = ($font_height * ($line_number) );
480 $bm_height = ($font_width * ($line_maxlength+1) );
481  
482 my $nameGif = new GD::Image($bm_width, $bm_height);
483
484 $white = $nameGif->colorAllocate(255,255,255);
485 $black = $nameGif->colorAllocate(0,0,0);
486  
487 foreach $name ( @expts ) {
488 $nameGif->stringUp(gdTinyFont, ($i * $font_height)+1, $bm_height - 3, $name, $black);
489 $i++;
490 }
491  
492 open (HEADER, ">$outdir$name.expt_info.".$self->imageType) || die($!);
493 binmode HEADER;
494 print HEADER ($self->imageType eq "gif" ? $nameGif->gif : $nameGif->png);
495 close(HEADER);
496
497 }
498  
499  
500 #####################################################################
501 sub _get_log_color {
502 #####################################################################
503 #
504  
505 my $logratio = shift;
506 my $contrast = shift;
507 my $colors = shift; # shade container
508  
509 if ( !defined($logratio) || $logratio eq "" ) {
510 return $$colors{grey};
511 }
512  
513 my $converted_logratio = int(($logratio/$contrast)*124);
514
515 if ( $converted_logratio == 0) {
516 return $$colors{black};
517 }
518
519 if ($logratio > 0) { # red hue
520
521 if ($logratio >= $contrast) { # bigger then contrast, return brightest red
522 return $$colors{red};
523 } else {
524 return $$colors{reds}[ $converted_logratio ];
525 }
526  
527 } else { # green hue
528  
529 if (abs($logratio) > $contrast) { # bigger then contrast, return brightest green
530 return $$colors{green};
531 } else {
532 return $$colors{greens}[ $converted_logratio ];
533 }
534 }
535  
536 } # end get_log_color
537  
538  
539 #####################################################################
540 sub _shadeAllocate {
541 #####################################################################
542 #
543  
544 my $image = shift;
545 my $colors = shift; # hashref
546  
547 my $colscheme = shift;
548  
549 my (@shade_green, @shade_red);
550  
551 if ($colscheme eq "rg") {
552 for(my $shade=0;$shade<124;$shade++)
553 {
554 $shade_green[124-$shade] = $image->colorAllocate(0,($shade*2),0);
555 $shade_red[$shade] = $image->colorAllocate(($shade*2),0,0);
556 }
557 } elsif ($colscheme eq "yb") {
558 for(my $shade=0;$shade<124;$shade++)
559 {
560 $shade_green[124-$shade] = $image->colorAllocate(0,0,($shade*2));
561 $shade_red[$shade] = $image->colorAllocate(($shade*2),($shade*2),0);
562 }
563 }
564  
565 $$colors{reds} = \@shade_red;
566 $$colors{greens} = \@shade_green;
567  
568 }
569  
570  
571  
572 1;
573  
574  
575 __END__