Microarray.pm untested

filter: Statementstmt 0.0% 0 / 314 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 68 Podpod 0.0% 0 / 64 totaltotal 0.0% 0 / 589 CC 58 SCAR 21.4
CC
58
Cov
0%
set_reporters
20.0
normalisation
20.0
signal_normalisation
20.0
CRAP 8.5
1 package Microarray;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 our $VERSION = '0.45';
7  
8  
9 sub BEGIN {
10 use Module::List qw(list_modules);
11 my $hModules = list_modules('Microarray::',{list_modules=>1,list_prefixes=>1, recurse => 1});
12 for my $module (keys %$hModules){
13 eval "require $module";
14 }
15 }
16  
17 { package microarray;
18  
19 # Constructor
20 sub new {
21 my $class = shift;
22 my $self = { };
23 bless $self, $class;
24 $self->barcode(shift); # barcode first
25  
26 if (@_){ # data file passed
27 my $data_file = shift; # data file
28 $self->data_file($data_file); # then load the data_file object
29 }
30
31 # set qc defaults
32 $ENV{ _LOW_SIGNAL_ } = 5000;
33 $ENV{ _HIGH_SIGNAL_ } = 60000;
34 $ENV{ _PERCEN_SAT_ } = 10;
35 $ENV{ _MIN_SNR_ } = 10;
36 $ENV{ _SIGNAL_QUALITY_ } = 95;
37 $ENV{ _MIN_DIAMETER_ } = 80;
38 $ENV{ _MAX_DIAMETER_ } = 150;
39 $ENV{ _TARGET_DIAMETER_ } = 100;
40 $ENV{ _MAX_DIAMETER_DEVIATION_ } = 10;
41
42 return $self;
43 }
44
45 ############################
46 # general getter setters #
47 ############################
48
49 sub barcode {
50 my $self = shift;
51 @_ ? $self->{ _barcode } = shift
52 : $self->{ _barcode };
53 }
54 sub data_file {
55 my $self = shift;
56 if (@_) {
57 my $data_file = shift;
58 my $oData_File;
59 if ((ref $data_file) && ($data_file->isa('data_file')) ){ # if data_file object, load directly
60 $oData_File = $data_file;
61 } else { # otherwise, if passed a file name
62 $oData_File = data_file->new($data_file); # create the data_file object - let it guess the file format
63 }
64 $self->{ _data_file } = $oData_File; # then load the data_file object
65 $ENV{ _BAD_FLAGS_ } = $oData_File->bad_flags;
66 } else {
67 $self->{ _data_file };
68 }
69 }
70 # how are missing samples described in the GAL file?
71 sub blank_feature {
72 my $self = shift;
73 if (@_) {
74 $self->{ _blank_feature } = shift;
75 } else {
76 if (defined $self->{ _blank_feature }) {
77 $self->{ _blank_feature };
78 } else {
79 $self->default_blank_feature;
80 }
81 }
82 }
83 sub default_blank_feature {
84 'n/a';
85 }
86 # is there an experimental prefix to feature id?
87 # anything beginning with a 'y' will be classed as 'yes', all others 'no'
88 sub prefix {
89 my $self = shift;
90 if (@_) {
91 $self->{ _prefix } = shift;
92 } else {
93 if (defined $self->{ _prefix }) {
94 $self->{ _prefix };
95 } else {
96 $self->default_prefix;
97 }
98 }
99 }
100 sub default_prefix {
101 'no'
102 }
103 sub channel1_dye_name {
104 my $self = shift;
105 my $data_file = $self->data_file;
106 $data_file->channel1_name;
107 }
108 sub channel2_dye_name {
109 my $self = shift;
110 my $data_file = $self->data_file;
111 $data_file->channel2_name;
112 }
113 sub long_ch1_name {
114 my $self = shift;
115 'ch1 ('.$self->channel1_dye_name.')';
116 }
117 sub long_ch2_name {
118 my $self = shift;
119 'ch2 ('.$self->channel2_dye_name.')';
120 }
121 sub which_channel {
122 my $self = shift;
123 my $dye_name = shift;
124 # assumes channels are different dyes!
125 if ($self->channel1_dye_name eq $dye_name) {
126 return 'ch1';
127 } elsif ($self->channel2_dye_name eq $dye_name) {
128 return 'ch2';
129 } else {
130 return undef;
131 }
132 }
133 # setter for data_file parameters used in feature selection
134 sub set_param {
135 my $self = shift;
136 my %hArgs = @_;
137 while(my($arg,$val) = each %hArgs){
138 if ($self->can($arg)){
139 $self->$arg($val);
140 } else {
141 die "Microrray ERROR; No parameter '$arg' is defined\n";
142 }
143 }
144 }
145
146 ##############################
147 # Microarray reporter methods #
148 ##############################
149
150 # set reporters scrolls through the spot data
151 # and fills a reporter object with all corresponding spot objects
152 sub set_reporters {
153 my $self = shift;
154 $self->{ _reporters } = { };
155 my $blank_feature = $self->blank_feature; # missing samples
156 my $data_file = $self->data_file;
157 $data_file->set_spot_objects;
158 my $aSpots = $data_file->get_spots;
159 SPOT: for (my $i=1; $i<@$aSpots; $i++) {
160 my $oSpot = $$aSpots[$i];
161 next SPOT unless $oSpot;
162 next SPOT unless ($oSpot->feature_id);
163 next SPOT if ($oSpot->feature_id =~ /$blank_feature/i);
164 $self->add_spot_to_reporter($oSpot);
165 }
166 }
167 # uses the spot feature_id to determine the array reporter_id
168 sub add_spot_to_reporter {
169 my $self = shift;
170 my $oSpot = shift;
171 if (my $oReporter = $self->get_reporter($oSpot->feature_id)) { # i.e. reporter already defined
172 $oReporter->add_reporter_spot($oSpot);
173 } else { # create a new reporter containing this spot
174 my $oReporter = array_reporter->new($oSpot->feature_id);
175 $oReporter->add_reporter_spot($oSpot);
176 $self->add_reporter($oReporter);
177 }
178 }
179 sub add_reporter {
180 my $self = shift;
181 my $oReporter = shift;
182 my $hReporters = $self->get_all_reporters;
183 $hReporters->{ $oReporter->reporter_id } = $oReporter;
184 }
185 sub get_reporter {
186 my $self = shift;
187 my $reporter_id = shift;
188 my $hReporters = $self->get_all_reporters;
189 return unless (defined $hReporters->{ $reporter_id });
190 $hReporters->{ $reporter_id };
191 }
192 # returns a hash of reporters; key=reporter_id, value=reporter object
193 sub get_all_reporters {
194 my $self = shift;
195 unless (defined $self->{ _reporters }){
196 $self->set_reporter_data;
197 }
198 $self->{ _reporters };
199 }
200 # returns an arrayref of spot objects for a given reporter_id
201 sub get_reporter_spots {
202 my $self = shift;
203 my $oReporter = $self->get_reporter(shift);
204 $oReporter->get_reporter_spots;
205 }
206 # returns an array of all reporter objects
207 sub get_reporter_objects {
208 my $self = shift;
209 my $hReporters = $self->get_all_reporters;
210 my @aValues = values %$hReporters;
211 return \@aValues;
212 }
213 # returns an array of all reporter ids
214 sub get_reporter_ids {
215 my $self = shift;
216 my $hReporters = $self->get_all_reporters;
217 my @aKeys = keys %$hReporters;
218 return \@aKeys;
219 }
220 sub set_reporter_data {
221 my $self = shift;
222 unless (defined $self->{ _reporters }){
223 $self->set_reporters;
224 }
225 my $aReporters = $self->get_reporter_objects;
226 for my $oReporter (@$aReporters) {
227 $self->sort_reporter_data($oReporter);
228 #$self->set_genetic_data($oReporter);
229 }
230 }
231 sub sort_reporter_data {
232 my $self = shift;
233 my $oReporter = shift;
234  
235 $oReporter->do_spot_qc;
236  
237 if ($oReporter->spots_passed_qc){
238 return if ($ENV{ REJECT_UNIQUE } && ($oReporter->spots_passed_qc == 1));
239 # for calculation of modal signal ratios
240 $self->all_ch1($oReporter->all_ch1);
241 $self->all_ch2($oReporter->all_ch2);
242 # for some plots
243 $self->x_pos($oReporter->x_pos);
244 $self->y_pos($oReporter->y_pos);
245 $self->all_ratios($oReporter->all_ratios);
246 } else {
247 return;
248 }
249 }
250 sub reject_unique {
251 $ENV{ REJECT_UNIQUE }++;
252 }
253 sub should_reject_unique {
254 $ENV{ REJECT_UNIQUE };
255 }
256 sub ignore_signal_qa {
257 $ENV{ _ignore_signal_qa }++;
258 }
259 sub should_ignore_signal_qa {
260 $ENV{ _ignore_signal_qa };
261 }
262 sub ignore_spot_qa {
263 $ENV{ _ignore_spot_qa }++;
264 }
265 sub should_ignore_spot_qa {
266 $ENV{ _ignore_spot_qa };
267 }
268
269 # the methods all_ch1/ch2/ratios create an array ref
270 # containing all the relevant values from the array
271 # shifting the relevant array ref from a Reporter object
272 sub all_ch1 {
273 my $self = shift;
274 unless (defined $self->{ _all_ch1 }){
275 $self->{ _all_ch1 } = [];
276 }
277 if (@_){
278 my $aCh1_Signals = $self->{ _all_ch1 };
279 my $aShifted = shift;
280 push(@$aCh1_Signals,@$aShifted);
281 } else {
282 $self->{ _all_ch1 };
283 }
284 }
285 sub all_ch2 {
286 my $self = shift;
287 unless (defined $self->{ _all_ch2 }){
288 $self->{ _all_ch2 } = [];
289 }
290 if (@_){
291 my $aCh2_Signals = $self->{ _all_ch2 };
292 my $aShifted = shift;
293 push(@$aCh2_Signals,@$aShifted);
294 } else {
295 $self->{ _all_ch2 };
296 }
297 }
298 sub x_pos {
299 my $self = shift;
300 unless (defined $self->{ _x_pos }){
301 $self->{ _x_pos } = [];
302 }
303 if (@_){
304 my $aX_Pos = $self->{ _x_pos };
305 my $aShifted = shift;
306 push(@$aX_Pos,@$aShifted);
307 } else {
308 $self->{ _x_pos };
309 }
310 }
311 sub y_pos {
312 my $self = shift;
313 unless (defined $self->{ _y_pos }){
314 $self->{ _y_pos } = [];
315 }
316 if (@_){
317 my $aY_Pos = $self->{ _y_pos };
318 my $aShifted = shift;
319 push(@$aY_Pos,@$aShifted);
320 } else {
321 $self->{ _y_pos };
322 }
323 }
324 sub all_ratios {
325 my $self = shift;
326 unless (defined $self->{ _all_ratios }){
327 $self->{ _all_ratios } = [];
328 }
329 if (@_){
330 my $aRatios = $self->{ _all_ratios };
331 my $aShifted = shift;
332 push(@$aRatios,@$aShifted);
333 } else {
334 $self->{ _all_ratios };
335 }
336 }
337 ###########
338
339 # summary of why spots were rejected
340 sub error_report {
341 my $self = shift;
342 if (defined $self->{ _error_report }) {
343 $self->{ _error_report };
344 } else {
345 $self->{ _error_report } = { };
346 }
347 }
348  
349 #################################
350 # Getter setters for spot #
351 # quality assessment criteria #
352 #################################
353  
354 # signal levels; set to linear range of the scanner
355 sub low_signal {
356 my $self = shift;
357 @_ ? $ENV{ _LOW_SIGNAL_ } = shift
358 : $ENV{ _LOW_SIGNAL_ };
359 }
360 sub high_signal {
361 my $self = shift;
362 @_ ? $ENV{ _HIGH_SIGNAL_ } = shift
363 : $ENV{ _HIGH_SIGNAL_ };
364 }
365 # % of pixels that are saturated
366 # provides check that signals are within the linear range
367 # and also helps to flag 'dirty' spots
368 sub percen_sat {
369 my $self = shift;
370 @_ ? $ENV{ _PERCEN_SAT_ } = shift
371 : $ENV{ _PERCEN_SAT_ };
372 }
373 # minimum acceptable spot signal:noise ratio
374 sub min_snr {
375 my $self = shift;
376 @_ ? $ENV{ _MIN_SNR_ } = shift
377 : $ENV{ _MIN_SNR_ };
378 }
379 # subjective assessment of signal quality, using (% signal > B + 2SD) or bluefuse's confidence value
380 sub signal_quality {
381 my $self = shift;
382 @_ ? $ENV{ _SIGNAL_QUALITY_ } = shift
383 : $ENV{ _SIGNAL_QUALITY_ };
384 }
385 # spot size
386 #Êby combining stringent diameter checking with
387 #Êexpected pixel number, we can check the
388 # 'circularity' of a spot
389 sub min_diameter {
390 my $self = shift;
391 @_ ? $ENV{ _MIN_DIAMETER_ } = shift
392 : $ENV{ _MIN_DIAMETER_ };
393 }
394 sub max_diameter {
395 my $self = shift;
396 @_ ? $ENV{ _MAX_DIAMETER_ } = shift
397 : $ENV{ _MAX_DIAMETER_ };
398 }
399 sub target_diameter {
400 my $self = shift;
401 @_ ? $ENV{ _TARGET_DIAMETER_ } = shift
402 : $ENV{ _TARGET_DIAMETER_ };
403 }
404 sub max_diameter_deviation {
405 my $self = shift;
406 @_ ? $ENV{ _MAX_DIAMETER_DEVIATION_ } = shift
407 : $ENV{ _MAX_DIAMETER_DEVIATION_ };
408 }
409 sub min_pixels {
410 my $self = shift;
411 if (@_) {
412 $self->{ _min_pixels } = shift;
413 } else {
414 unless (defined $self->{ _min_pixels }) {
415 $self->{ _min_pixels } = $self->pixel_area($self->min_diameter);
416 }
417 $self->{ _min_pixels };
418 }
419 }
420 sub max_pixels {
421 my $self = shift;
422 if (@_) {
423 $self->{ _max_pixels } = shift;
424 } else {
425 unless (defined $self->{ _max_pixels }) {
426 $self->{ _max_pixels } = $self->pixel_area($self->max_diameter);
427 }
428 $self->{ _max_pixels };
429 }
430 }
431 sub pixel_area {
432 my $self = shift;
433 my $micron_diameter = shift; # spot diameter in microns
434 my $data_file = $self->data_file;
435 my $pixel_size = $data_file->pixel_size; # each channel, in microns
436 my $pixel_radius = ($micron_diameter/$pixel_size)/2; # radius as number of pixels
437 my $pixel_area = 3.14159 * ($pixel_radius * $pixel_radius); # area as number of pixels
438 return int($pixel_area + 0.49999); # ensure correct rounding of a positive value
439 }
440 sub target_pixels {
441 my $self = shift;
442 if (@_) {
443 $self->{ _target_pixels } = shift;
444 } else {
445 if (defined $self->{ _target_pixels }) {
446 $self->{ _target_pixels };
447 } else {
448 $self->pixel_area($self->default_target_diameter);
449 }
450 }
451 }
452 sub normalisation { # modal log2 ratio normalisation
453 my $self = shift;
454 if (@_){
455 $self->{ _normalisation } = shift;
456 } else {
457 unless (defined $self->{ _normalisation }){
458 return 'yes';
459 }
460 if ($self->{ _normalisation } =~ /^n/i){
461 return undef;
462 } else {
463 return 'yes';
464 }
465 }
466 }
467 sub signal_normalisation { # from scanner output
468 my $self = shift;
469 if (@_){
470 $self->{ _signal_normalisation } = shift;
471 } else {
472 unless (defined $self->{ _signal_normalisation }){
473 return 'yes';
474 }
475 if ($self->{ _signal_normalisation } =~ /^n/i){
476 return undef;
477 } else {
478 return 'yes';
479 }
480 }
481 }
482 #Êgenetic_data_source defines whether we get the genetic data from file or database
483 # currently, 'data_file' means from the results file (ie what was in the GAL file)
484 # 'database' from array_pipeline_v4.chori_bac_clone_info
485 # can expand to fetch it from separate file, and also to specify database table if different
486 sub genetic_data_source {
487 my $self = shift;
488 if (@_) {
489 $self->{ _genetic_data_source } = shift;
490 } else {
491 if (defined $self->{ _genetic_data_source }) {
492 $self->{ _genetic_data_source };
493 } else {
494 $self->default_gendata_source;
495 }
496 }
497 }
498 sub default_gendata_source {
499 'data_file';
500 }
501 # user defined headers for data output
502 sub format_headers {
503 my $self = shift;
504 if (@_){
505 my @aHeaders = @_;
506 $self->{ _format_headers } = \@aHeaders;
507 } else {
508 if (defined $self->{ _format_headers }){
509 $self->{ _format_headers };
510 } else {
511 $self->default_format_headers;
512 }
513 }
514 }
515
516 ### image output ###
517 sub set_image_data {
518 my $self = shift;
519 my $oImage = shift;
520 $oImage->{ _ch1_values } = $self->all_ch1;
521 $oImage->{ _ch2_values } = $self->all_ch2;
522 $oImage->{ _x_coords } = $self->x_pos;
523 $oImage->{ _y_coords } = $self->y_pos;
524  
525 # for heatmaps
526 my $oData = $self->data_file;
527 if ($oData->can('array_columns') && $oData->can('spot_columns') && $oData->can('array_rows') && $oData->can('spot_rows')){
528 $oImage->{ _x_spots } = $oData->array_columns * ($oData->spot_columns + 1);
529 $oImage->{ _y_spots } = $oData->array_rows * ($oData->spot_rows + 1);
530 }
531
532 $oImage->parse_args(@_);
533 $oImage->process_data; # by-pass $oImage->set_data
534
535 # ma/ri/scatter use _ch1_values/_ch2_values
536 # heatmaps use ch1_values and x/y_coords
537 #
538 # currently no support for direct plotting of cgh_plots
539 # $self->{ _x_values } = $oData_File->all_locns;
540 # this is the problem - don't yet have genomic locations being returned directly
541 # should make a Microarray::CGH object and add this function
542 # but how to easily integrate database support?
543 # what about have a database object, which would be ensembl by default, but which will handle LIMS database
544 # $self->{ _y_values } = $oData_File->all_log2_ratio;
545 # $self->all_ratios;
546 # $self->{ _reporter_names } = $oData_File->all_feature_names;
547 # $self->get_reporter_ids;
548 # $self->{ _cgh_calls } = $oData_File->cgh_calls if $oData_File->isa('cgh_call_output');
549
550 }
551 sub plot_ma {
552 my $self = shift;
553 my $oImage = ma_plot->new();
554 $self->set_image_data($oImage,@_);
555 $oImage->make_plot;
556 }
557 sub print_ma_plot {
558 my $self = shift;
559 $self->print_plot(shift,$self->plot_ma(@_)); # params
560 }
561 sub plot_intensity_scatter {
562 my $self = shift;
563 my $oImage = intensity_scatter->new();
564 $self->set_image_data($oImage,@_);
565 $oImage->make_plot;
566 }
567 sub print_intensity_scatter {
568 my $self = shift;
569 $self->print_plot(shift,$self->plot_intensity_scatter(@_)); # params
570 }
571 sub plot_log2_heatmap {
572 my $self = shift;
573 my $oImage = log2_heatmap->new();
574 $self->set_image_data($oImage,@_);
575 $oImage->make_plot;
576 }
577 sub print_log2_heatmap {
578 my $self = shift;
579 $self->print_plot(shift,$self->plot_log2_heatmap(@_)); # params
580 }
581 sub plot_intensity_heatmap {
582 my $self = shift;
583 my $oImage = intensity_heatmap->new();
584 $self->set_image_data($oImage,@_);
585 $oImage->make_plot;
586 }
587 sub print_intensity_heatmap {
588 my $self = shift;
589 $self->print_plot(shift,$self->plot_intensity_heatmap(@_)); # params
590 }
591 sub print_plot {
592 my $self = shift;
593 my $path = shift;
594 my $plot_png = shift;
595 open (PLOT,">$path") or warn "Could not open filehandle '$path'\n$!";
596 print PLOT $plot_png;
597 close PLOT or warn "Could not close filehandle '$path'\n$!";
598 }
599 }
600  
601 1;
602  
603 __END__