Microarray/Analysis/CGH.pm untested

filter: Statementstmt 0.0% 0 / 362 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 42 totaltotal 0.0% 0 / 654 CC 68 SCAR 38.1
CC
68
Cov
0%
smooth_data_by_location
182.0
sort_chromosome_data
110.0
sort_genome_data
90.0
CRAP 45.0
1 package Microarray::Analysis::CGH;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6  
7 our $VERSION = '1.9';
8  
9 use Microarray::Analysis;
10  
11 { package cgh_analysis;
12  
13 our @ISA = qw( analysis );
14  
15 sub new {
16 my $class = shift;
17 my $self = { };
18 bless $self, $class;
19 if (@_){
20 $self->set_data(@_);
21 }
22 return $self;
23 }
24 sub set_data {
25 my $self = shift;
26 while (my $oData = shift){
27 $self->data_object($oData) if ($oData->isa('data_file') || $oData->isa('processed_data'));
28 $self->clone_locns_file($oData) if $oData->isa('clone_locn_file');
29 $self->cgh_call_object($oData) if ($oData->isa('processed_data') && $oData->cgh_call_data);
30 }
31 }
32 sub cgh_call_data {
33 $ENV{ CGH_CALL_DATA };
34 }
35 sub cgh_call_object {
36 my $self = shift;
37 @_ ? $self->{ _cgh_call_object } = shift
38 : $self->{ _cgh_call_object };
39 }
40 # pass a feature_id to return its call value
41 sub cgh_call {
42 my $self = shift;
43 if ($self->cgh_call_data){
44 my $oCGH_Call = $self->cgh_call_object;
45 return $oCGH_Call->cgh_call(shift);
46 } else {
47 return;
48 }
49 }
50 # pass a feature_id to return its segment level
51 sub seg_level {
52 my $self = shift;
53 if ($self->cgh_call_data){
54 my $oCGH_Call = $self->cgh_call_object;
55 return $oCGH_Call->seg_level(shift);
56 } else {
57 return;
58 }
59 }
60 # the call data in chromosome chunks
61 sub chr_cgh_calls {
62 my $self = shift;
63 if ($self->cgh_call_data){
64 my $oCGH_Call = $self->cgh_call_object;
65 return $oCGH_Call->chr_cgh_calls;
66 } else {
67 return;
68 }
69 }
70 sub chr_seg_levels {
71 my $self = shift;
72 if ($self->cgh_call_data){
73 my $oCGH_Call = $self->cgh_call_object;
74 return $oCGH_Call->chr_seg_levels;
75 } else {
76 return;
77 }
78 }
79 sub flip_flop {
80 my $self = shift;
81 if (defined $self->{ _flip_flop }){
82 $self->{ _flip_flop };
83 } else {
84 return 1;
85 }
86 }
87 sub flip {
88 my $self = shift;
89 $self->{ _flip_flop } = -1;
90 }
91 sub flop {
92 my $self = shift;
93 $self->{ _flip_flop } = 1;
94 }
95 sub reporter_chrs {
96 my $self = shift;
97 $self->{ _reporter_chrs };
98 }
99 sub get_reporter_data {
100 my $self = shift;
101 my $reporter = shift;
102 my $hReporters = $self->reporters;
103 unless (defined $hReporters->{ $reporter }){
104 $hReporters->{ $reporter } = { ratios => [] };
105 }
106 $hReporters->{ $reporter };
107 }
108 # pass reporter,log2,locn
109 sub set_reporter_data {
110 my $self = shift;
111 my $hReporter_data = $self->get_reporter_data(shift);
112 my $aRatios = $hReporter_data->{ ratios };
113 push(@$aRatios,shift);
114 $hReporter_data->{ locn } = shift;
115 }
116 sub reporter_locn {
117 my $self = shift;
118 my $hReporter = $self->get_reporter_data(shift,shift); # second shift for genome plot - chromosome name
119 return $hReporter->{locn};
120 }
121 sub reporter_log {
122 my $self = shift;
123 my $hReporter = $self->get_reporter_data(shift,shift); # second shift for genome plot - chromosome name
124 my $aFeat_Ratios = $hReporter->{ratios};
125 if (@$aFeat_Ratios == 1){
126 return $aFeat_Ratios->[0];
127 } else {
128 my $log_ratio;
129 for my $ratio (@$aFeat_Ratios){
130 $log_ratio += $ratio;
131 }
132 return ($log_ratio/scalar @$aFeat_Ratios);
133 }
134 }
135 sub parse_embedded_locn {
136 my $self = shift;
137 my $location = shift;
138 my ($chr,$start,$end) = split(/:|\.\./,$location);
139 $chr =~ s/chr//;
140 return ($chr,int(($start+$end)/2));
141 }
142 sub clone_locns_file {
143 my $self = shift;
144 @_ ? $self->{ _clone_locns_file } = shift
145 : $self->{ _clone_locns_file };
146 }
147 sub embedded_locns {
148 my $self = shift;
149 @_ ? $self->{ _embedded_locns } = shift
150 : $self->{ _embedded_locns };
151 }
152 sub do_smoothing {
153 my $self = shift;
154 @_ ? $self->{ _do_smoothing } = shift
155 : $self->{ _do_smoothing };
156 }
157 sub smoothing {
158 my $self = shift;
159 if (@_){
160 $self->smooth_window(shift);
161 $self->smooth_step(shift);
162 $self->{ _do_smoothing }++;
163 } else {
164 $self->{ _do_smoothing };
165 }
166 }
167 sub smooth_window {
168 my $self = shift;
169 if (@_){
170 $self->{ _smooth_window } = shift;
171 $self->{ _do_smoothing }++;
172 } else {
173 if (defined $self->{ _smooth_window }){
174 $self->{ _smooth_window };
175 } else {
176 $self->default_smooth_window;
177 }
178 }
179 }
180 sub default_smooth_window {
181 500000
182 }
183 sub smooth_step {
184 my $self = shift;
185 if(@_){
186 $self->{ _smooth_step } = shift;
187 $self->{ _do_smoothing }++;
188 } else {
189 if (defined $self->{ _smooth_step }){
190 $self->{ _smooth_step };
191 } else {
192 $self->default_smooth_step;
193 }
194 }
195 }
196 sub default_smooth_step {
197 150000
198 }
199 sub is_smoothed {
200 my $self = shift;
201 $self->{ _smoothed };
202 }
203 # this method uses a moving window of a specific genomic length,
204 # that moves along the genome in steps of a defined length,
205 # to smooth our CGH profiles.
206 sub smooth_data_by_location {
207 my $self = shift;
208
209 # our smoothing parameters
210 my $window = $self->smooth_window;
211 my $step = $self->smooth_step;
212
213 # all of the sorted data
214 my $aAll_Locns = $self->x_values;
215 my $aAll_Logs = $self->y_values;
216 my $aAll_CGH_Calls = $self->z_values; # returns null if no CGHcall/DNAcopy data
217  
218 $self->starting_data($aAll_Locns,$aAll_Logs);
219  
220 # arrays to hold the final smoothed data
221 my @aAll_Smooth_Locns = ();
222 my @aAll_Smooth_Logs = ();
223 my @aAll_Smooth_Calls = ();
224  
225 # set the chromosome we are working with - single or whole genome?
226 my @aPlot_Chromosomes;
227 if (my $chr = $self->plot_chromosome){
228 @aPlot_Chromosomes = ($chr);
229 } else {
230 @aPlot_Chromosomes = (1..22,'X','Y');
231 }
232
233 # scroll through the individual chromosomes...
234 for (my $j=0; $j<@aPlot_Chromosomes; $j++){
235
236 # ...and get the sorted data for this chromosome
237 my $aLocns = $aAll_Locns->[$j];
238 my $aLogs = $aAll_Logs->[$j];
239 my $aCGH_Calls = $aAll_CGH_Calls->[$j] if ($aAll_CGH_Calls && @$aAll_CGH_Calls);
240  
241 # reset the window start and end location
242 my $start = $aLocns->[0];
243 my $end = $start + $window;
244
245 # arrays to hold the smoothed data for this chromosome
246 my @aSmooth_Chr_Locns = ();
247 my @aSmooth_Chr_Logs = ();
248 my @aSmooth_Chr_Calls = ();
249
250 #Êarrays to hold data in the moving window
251 #Êmust be empty 'real' lists so that moving_average() works as expected
252 my @aWindow_Logs = ();
253 my @aWindow_Locns = ();
254 my @aWindow_Calls = ();
255
256 # scroll through the sorted data
257 for (my $i=0; $i<@$aLocns; $i++){
258 my $genomic_locn = $aLocns->[$i];
259 my $log_value = $aLogs->[$i];
260 my $cgh_call = $aCGH_Calls->[$i] if ($aCGH_Calls && @$aCGH_Calls);
261
262 # are we past the end of the window?
263 if ($genomic_locn > $end){
264 # if so, average up what's in the window...
265 #Êmust pass referenced 'real' lists here, so that moving average works as expected
266 my ($av_locn, $av_log, $av_call) = $self->moving_average(\@aWindow_Locns, \@aWindow_Logs, \@aWindow_Calls);
267 # ...add these values to the smoothed data arrays...
268 push(@aSmooth_Chr_Locns, $av_locn);
269 push(@aSmooth_Chr_Logs, $av_log);
270 push(@aSmooth_Chr_Calls, $av_call) if ($aCGH_Calls && @$aCGH_Calls);
271  
272 # ...move the end of the window to include the next location...
273 $end = (int($genomic_locn/100000) * 100000) + $step;
274 # ...move the start up as well...
275 $start = $end - $window;
276  
277 # ...and remove any data that is no longer in the window
278 if ((@aWindow_Locns) && ($aWindow_Locns[-1] < $start)){
279 @aWindow_Locns = ();
280 @aWindow_Logs = ();
281 @aWindow_Calls = () if ($aCGH_Calls && @$aCGH_Calls);
282 } else {
283 while ((@aWindow_Locns) && ($aWindow_Locns[0] < $start)){ # get rid of any values now out of the region
284 my $shifted1 = shift @aWindow_Locns;
285 my $shifted2 = shift @aWindow_Logs;
286 my $shifted3 = shift @aWindow_Calls if ($aCGH_Calls && @$aCGH_Calls);
287 }
288 }
289 }
290
291 # either this location fell in the window to start with,
292 # or the window has now been moved to include it
293 # so we add it to the window array and continue to the next location
294 push (@aWindow_Locns, $genomic_locn);
295 push (@aWindow_Logs, $log_value);
296 push (@aWindow_Calls, $cgh_call) if ($aCGH_Calls && @$aCGH_Calls);
297 }
298 # we've finished with this chromosome, but have some values left in the last window
299 my ($av_locn, $av_log, $av_call) = $self->moving_average(\@aWindow_Locns, \@aWindow_Logs, \@aWindow_Calls);
300 push(@aSmooth_Chr_Locns, $av_locn);
301 push(@aSmooth_Chr_Logs, $av_log);
302 push(@aSmooth_Chr_Calls, $av_call) if ($aCGH_Calls && @$aCGH_Calls);
303
304 # add the smoothed data for this chromosome to our array
305 # and then continue to the next chromosome
306 push(@aAll_Smooth_Locns,\@aSmooth_Chr_Locns);
307 push(@aAll_Smooth_Logs,\@aSmooth_Chr_Logs);
308 push(@aAll_Smooth_Calls,\@aSmooth_Chr_Calls) if (@aSmooth_Chr_Calls);
309 }
310  
311 # finally, set all the smoothed data to our plotting values
312 $self->{ _x_values } = \@aAll_Smooth_Locns;
313 $self->{ _y_values } = \@aAll_Smooth_Logs;
314 $self->{ _z_values } = \@aAll_Smooth_Calls if (@aAll_Smooth_Calls);
315 $self->{ _smoothed } = 1;
316 }
317 sub starting_data {
318 my $self = shift;
319 if (@_){
320 $self->start_x_values(shift);
321 $self->start_y_values(shift);
322 } else {
323 return ($self->start_x_values,$self->start_y_values);
324 }
325 }
326 sub start_x_values {
327 my $self = shift;
328 @_ ? $self->{ _start_x_values } = shift
329 : $self->{ _start_x_values };
330 }
331 sub start_y_values {
332 my $self = shift;
333 @_ ? $self->{ _start_y_values } = shift
334 : $self->{ _start_y_values };
335 }
336 sub moving_average {
337 my $self = shift;
338 my ($aLocns,$aLogs,$aCalls) = @_;
339 my ($av_locn, $av_log2, $av_call);
340 if (@$aLocns > 1){
341 $av_locn = calc_average($aLocns);
342 $av_log2 = calc_average($aLogs);
343 $av_call = calc_mode($aCalls) if ($aCalls && @$aCalls);
344 } elsif (@$aLocns == 1){
345 $av_locn = $aLocns->[0];
346 $av_log2 = $aLogs->[0];
347 $av_call = $aCalls->[0] if ($aCalls && @$aCalls);
348 }
349 return(int $av_locn, $av_log2, $av_call);
350 }
351 # the next two methods avoid using the horribly slow Statistics::Descriptive module
352 sub calc_mode { #Êor the first value, if no mode
353 my $aList = shift;
354 my %hFreq = ();
355 for my $num (@$aList){
356 $hFreq{$num}++;
357 }
358 my ($max_count,$most_common) = ($hFreq{$$aList[0]},$$aList[0]);
359 while (my ($num,$count) = each %hFreq){
360 $most_common = $num if ($count > $max_count);
361 }
362 return $most_common;
363 }
364 sub calc_average {
365 my $aList = shift;
366 my $total;
367 for my $val (@$aList){
368 $total += $val;
369 }
370 return ($total/scalar @$aList);
371 }
372 }
373  
374 { package chromosome_cgh;
375  
376 our @ISA = qw( cgh_analysis );
377  
378 sub sort_genome_data {
379 my $self = shift;
380 $self->sort_chromosome_data;
381 }
382 sub all_cgh_smooth {
383 my $self = shift;
384 if ($self->cgh_call_data){
385 my $oCGH_Call = $self->cgh_call_object;
386 my $aahAll_CGH_Smooth = $oCGH_Call->all_cgh_smooth;
387 return $aahAll_CGH_Smooth->[0];
388 } else {
389 return;
390 }
391 }
392 sub sort_chromosome_data {
393 my $self = shift;
394 my $plot_chr = $self->plot_chromosome;
395 my $oData_File = $self->data_object;
396 die "Microarray::Analysis::CGH ERROR: No data object provided\n" unless $oData_File;
397
398 $oData_File->flip if ($self->flip_flop == -1);
399 my $spot_count = $oData_File->spot_count;
400 if ($self->embedded_locns){
401 for (my $i=0; $i<$spot_count; $i++){
402 if (my $embedded_locn = $oData_File->feature_id($i)){
403 my ($chr,$locn) = $self->parse_embedded_locn($embedded_locn);
404 next unless ($plot_chr eq $chr);
405 if (my $log = $oData_File->log2_ratio($i)){
406 $self->set_reporter_data($oData_File->synonym_id($i),$log,$locn);
407 }
408 }
409 }
410 } elsif (my $oClone_Positions = $self->clone_locns_file) {
411 my $hClones = $oClone_Positions->clone_hash;
412 for (my $i=0; $i<$spot_count; $i++){
413 my $feature = $oData_File->feature_id($i);
414 next unless ( (defined $$hClones{$feature}) &&
415 ($plot_chr eq $$hClones{$feature}{_chr}) );
416 if (my $log = $oData_File->log2_ratio($i)){
417 $self->set_reporter_data($feature,$log,$oClone_Positions->location($feature));
418 }
419 }
420 } else {
421 die "Microarray::Analysis::CGH ERROR; No clone positions to work with\n";
422 }
423 $self->order_data;
424 }
425 sub order_data {
426 my $self = shift;
427 my $hReporters = $self->reporters;
428 my @aReporters = keys %$hReporters;
429 my $aSorted_Reporters = [];
430 if ($self->smoothing){
431 @$aSorted_Reporters = sort { $$hReporters{ $a }{locn} <=> $$hReporters{ $b }{locn} } @aReporters;
432 } else {
433 $aSorted_Reporters = \@aReporters;
434 }
435 my $call_data = $self->cgh_call_data; #Êto save 32,000 calls later
436 my @aLog_Ratios = ();
437 my @aLocns = ();
438 my @aZ_Values = ();
439 for my $reporter (@$aSorted_Reporters){
440 push(@aLocns,$self->reporter_locn($reporter));
441 push(@aLog_Ratios,$self->reporter_log($reporter));
442 push(@aZ_Values,$self->z_value($reporter)) if $call_data;
443 }
444 $self->{ _x_values } = [\@aLocns];
445 $self->{ _y_values } = [\@aLog_Ratios];
446 $self->{ _reporter_names } = [$aSorted_Reporters];
447 $self->{ _z_values } = [\@aZ_Values] if @aZ_Values;
448 }
449 }
450  
451 { package genome_cgh;
452  
453 our @ISA = qw( chromosome_cgh );
454  
455 sub sort_genome_data {
456 my $self = shift;
457 my $oData_File = $self->data_object;
458 die "Microarray::Analysis::CGH ERROR: No data object provided\n" unless $oData_File;
459  
460 $oData_File->flip if ($self->flip_flop == -1);
461 $self->{ _reporters } = {}; # reset the reporters for this chromosome
462 my $spot_count = $oData_File->spot_count;
463 if ($self->embedded_locns){
464 for (my $i=0; $i<$spot_count; $i++){
465 if (my $embedded_locn = $oData_File->feature_id($i)){
466 my ($chr,$locn) = $self->parse_embedded_locn($embedded_locn);
467 if (my $log = $oData_File->log2_ratio($i)){
468 $self->set_reporter_data($oData_File->synonym_id($i),$log,$locn,$chr);
469 }
470 }
471 }
472 } elsif (my $oClone_Positions = $self->clone_locns_file) {
473 my $hClones = $oClone_Positions->clone_hash;
474 for (my $i=0; $i<$spot_count; $i++){
475 my $feature = $oData_File->feature_id($i);
476 next unless (defined $$hClones{$feature});
477 if (my $log = $oData_File->log2_ratio($i)){
478 $self->set_reporter_data($feature,$log,$oClone_Positions->location($feature),$$hClones{$feature}{_chr});
479 }
480 }
481 } else {
482 die "Microarray::Analysis::CGH ERROR; No clone positions to work with\n";
483 }
484 $self->order_genome_data;
485 }
486 sub all_cgh_smooth {
487 my $self = shift;
488 if (my $oCGH_Call = $self->cgh_call_object){
489 return $oCGH_Call->all_cgh_smooth;
490 } else {
491 return;
492 }
493 }
494 sub order_genome_data {
495 my $self = shift;
496 my $hReporters = $self->reporters;
497 my (@aReporters,@aLocns,@aLog_Ratios,@aZ_Values);
498 my $call_data = $self->cgh_call_data; #Êto save 32,000 calls later
499
500 for my $chr ((1..22,'X','Y')){
501
502 my $hChr_Reporters = $hReporters->{ $chr };
503 my @aChr_Reporters = keys %$hChr_Reporters;
504
505 my $aSorted_Chr_Reporters = [];
506 my $aSorted_Chr_Logs = [];
507 my $aSorted_Chr_Locns = [];
508 my $aSorted_Chr_Zvals = [];
509
510 if ($self->smoothing){
511 @$aSorted_Chr_Reporters = sort { $$hChr_Reporters{ $a }{locn} <=> $$hChr_Reporters{ $b }{locn} } @aChr_Reporters;
512 } else {
513 $aSorted_Chr_Reporters = \@aChr_Reporters;
514 }
515 for my $reporter (@$aSorted_Chr_Reporters){
516 push(@$aSorted_Chr_Locns,$$hChr_Reporters{$reporter}{ locn });
517 push(@$aSorted_Chr_Logs,$self->reporter_log($reporter,$chr));
518 push(@$aSorted_Chr_Zvals,$self->z_value($reporter)) if $call_data;
519 }
520 push (@aReporters,$aSorted_Chr_Reporters);
521 push (@aLocns,$aSorted_Chr_Locns);
522 push (@aLog_Ratios,$aSorted_Chr_Logs);
523 push (@aZ_Values,$aSorted_Chr_Zvals) if @$aSorted_Chr_Zvals;
524 }
525 $self->{ _x_values } = \@aLocns;
526 $self->{ _y_values } = \@aLog_Ratios;
527 $self->{ _reporter_names } = \@aReporters;
528 $self->{ _z_values } = \@aZ_Values if @aZ_Values;
529 }
530 sub set_reporter_data {
531 my $self = shift;
532 my $hReporter_data = $self->get_reporter_data(shift,pop); # pop chromosome name
533 my $aRatios = $hReporter_data->{ ratios };
534 push(@$aRatios,shift);
535 $hReporter_data->{ locn } = shift;
536 }
537 sub get_reporter_data {
538 my $self = shift;
539 my $reporter = shift;
540 my $chr = shift;
541 my $hReporters = $self->reporters;
542 unless (defined $hReporters->{ $chr }{ $reporter }){
543 $hReporters->{ $chr }{ $reporter } = { ratios => [] };
544 }
545 $hReporters->{ $chr }{ $reporter };
546 }
547 }
548 1;
549  
550 __END__