| 1 | package Microarray::Image::CGH_Plot; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | our $VERSION = '1.9'; | ||
| 7 | |||
| 8 | use Microarray::Image; | ||
| 9 | require Microarray::File; | ||
| 10 | use Microarray::Analysis::CGH; | ||
| 11 | |||
| 12 | { package cgh_plot; | ||
| 13 | |||
| 14 | our @ISA = qw( chromosome_cgh plot ); # will use the Microarray::Analysis::CGH new() | ||
| 15 | |||
| 16 | sub sort_data { | ||
| 17 | my $self = shift; | ||
| 18 | my $oData_File = $self->data_object; | ||
| 19 | die "Microarray::Image ERROR: No data object provided\n" unless $oData_File; | ||
| 20 | |||
| 21 | if ($oData_File->comes_sorted ){ # already sorted by seq_start, already flip_flopped - i.e. from pipeline_data | ||
| 22 | $self->{ _x_values } = $oData_File->all_locns; | ||
| 23 | $self->{ _y_values } = $oData_File->all_log2_ratio; | ||
| 24 | $self->{ _reporter_names } = $oData_File->all_feature_names; | ||
| 25 | if ($self->cgh_call_data){ | ||
| 26 | if ($self->plot_call_colours){ | ||
| 27 | $self->{ _z_values } = $self->chr_cgh_calls; | ||
| 28 | } elsif ($self->plot_segment_colours){ | ||
| 29 | $self->{ _z_values } = $self->chr_seg_levels; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } else { | ||
| 33 | $self->sort_genome_data; | ||
| 34 | } | ||
| 35 | $self->{ _data_sorted } = 1; | ||
| 36 | } | ||
| 37 | sub plot_chromosome { | ||
| 38 | my $self = shift; | ||
| 39 | if (@_){ | ||
| 40 | $self->{ _plot_chromosome } = shift; | ||
| 41 | } else { | ||
| 42 | if (defined $self->{ _plot_chromosome }){ | ||
| 43 | $self->{ _plot_chromosome }; | ||
| 44 | } else { | ||
| 45 | die "Microarray::Image ERROR; No plot chromosome was specified\n"; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | sub data_sorted { | ||
| 50 | my $self = shift; | ||
| 51 | $self->{ _data_sorted }; | ||
| 52 | } | ||
| 53 | sub plot_centromere { | ||
| 54 | my $self = shift; | ||
| 55 | if (@_){ | ||
| 56 | $self->{ _plot_centromere } = shift; | ||
| 57 | } else { | ||
| 58 | if (defined $self->{ _plot_centromere }){ | ||
| 59 | return $self->{ _plot_centromere }; | ||
| 60 | } else { | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | # this is a hashref of the x/y plot values | ||
| 66 | #Êused to avoid plotting the same values twice | ||
| 67 | sub plotted_values { | ||
| 68 | my $self = shift; | ||
| 69 | @_ ? $self->{ _plotted_values } = shift | ||
| 70 | : $self->{ _plotted_values }; | ||
| 71 | } | ||
| 72 | sub shift_zero { | ||
| 73 | my $self = shift; | ||
| 74 | @_ ? $self->{ _shift_zero } = shift | ||
| 75 | : $self->{ _shift_zero }; | ||
| 76 | } | ||
| 77 | sub plot_cgh_call { | ||
| 78 | my $self = shift; | ||
| 79 | $self->segment_levels; | ||
| 80 | $self->breakpoints; | ||
| 81 | $self->call_colours; | ||
| 82 | } | ||
| 83 | sub segment_levels { | ||
| 84 | my $self = shift; | ||
| 85 | $self->{ _segment_levels }++; | ||
| 86 | } | ||
| 87 | sub plot_segment_levels { | ||
| 88 | my $self = shift; | ||
| 89 | $self->{ _segment_levels }; | ||
| 90 | } | ||
| 91 | sub breakpoints { | ||
| 92 | my $self = shift; | ||
| 93 | $self->{ _breakpoints }++; | ||
| 94 | } | ||
| 95 | sub plot_breakpoints { | ||
| 96 | my $self = shift; | ||
| 97 | $self->{ _breakpoints }; | ||
| 98 | } | ||
| 99 | sub plot_colours { | ||
| 100 | my $self = shift; | ||
| 101 | my $method = shift; | ||
| 102 | $self->$method; | ||
| 103 | } | ||
| 104 | sub rainbow_colours { | ||
| 105 | my $self = shift; | ||
| 106 | delete $self->{ _segment_colours } if (defined $self->{ _segment_colours }); | ||
| 107 | delete $self->{ _call_colours } if (defined $self->{ _call_colours }); | ||
| 108 | } | ||
| 109 | sub call_colours { | ||
| 110 | my $self = shift; | ||
| 111 | # can't plot call and segment colours, so last one in wins! | ||
| 112 | delete $self->{ _segment_colours } if (defined $self->{ _segment_colours }); | ||
| 113 | $self->{ _call_colours }++; | ||
| 114 | } | ||
| 115 | sub plot_call_colours { | ||
| 116 | my $self = shift; | ||
| 117 | $self->{ _call_colours }; | ||
| 118 | } | ||
| 119 | sub segment_colours { | ||
| 120 | my $self = shift; | ||
| 121 | # can't plot call and segment colours, so last one in wins! | ||
| 122 | delete $self->{ _call_colours } if (defined $self->{ _call_colours }); | ||
| 123 | $self->{ _segment_colours }++; | ||
| 124 | } | ||
| 125 | sub plot_segment_colours { | ||
| 126 | my $self = shift; | ||
| 127 | $self->{ _segment_colours }; | ||
| 128 | } | ||
| 129 | sub plot_gene_locn { | ||
| 130 | my $self = shift; | ||
| 131 | if (@_){ | ||
| 132 | unless (defined $self->{ _plot_gene_info }){ | ||
| 133 | $self->{ _plot_gene_info } = {}; | ||
| 134 | } | ||
| 135 | my %hGene_Info = @_; | ||
| 136 | my $hGene_Info = $self->{ _plot_gene_info }; | ||
| 137 | %$hGene_Info = (%$hGene_Info, %hGene_Info); | ||
| 138 | } else { | ||
| 139 | $self->{ _plot_gene_info }; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | sub plot_gene_names { | ||
| 143 | my $self = shift; | ||
| 144 | my $hGene_Info = $self->{ _plot_gene_info }; | ||
| 145 | return (keys %$hGene_Info); | ||
| 146 | } | ||
| 147 | sub plot_gene_chr { | ||
| 148 | my $self = shift; | ||
| 149 | my $gene = shift; | ||
| 150 | my $hGene_Info = $self->{ _plot_gene_info }; | ||
| 151 | return $hGene_Info->{ $gene }{ chr }; | ||
| 152 | } | ||
| 153 | sub plot_gene_start { | ||
| 154 | my $self = shift; | ||
| 155 | my $gene = shift; | ||
| 156 | my $hGene_Info = $self->{ _plot_gene_info }; | ||
| 157 | return $hGene_Info->{ $gene }{ start }; | ||
| 158 | } | ||
| 159 | sub plot_gene_end { | ||
| 160 | my $self = shift; | ||
| 161 | my $gene = shift; | ||
| 162 | my $hGene_Info = $self->{ _plot_gene_info }; | ||
| 163 | return $hGene_Info->{ $gene }{ end }; | ||
| 164 | } | ||
| 165 | # the call data (either CGHcall calls, or DNAcopy segments) | ||
| 166 | # the original chr chunks for either call or segment level data | ||
| 167 | sub z_values { | ||
| 168 | my $self = shift; | ||
| 169 | if (defined $self->{ _z_values }){ | ||
| 170 | return $self->{ _z_values }; | ||
| 171 | } else { | ||
| 172 | if ($self->plot_call_colours){ | ||
| 173 | $self->chr_cgh_calls; | ||
| 174 | } elsif ($self->plot_segment_colours){ | ||
| 175 | $self->chr_seg_levels; | ||
| 176 | } else { | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | # the ordered linear plottable data | ||
| 182 | sub plotz_values { | ||
| 183 | my $self = shift; | ||
| 184 | $self->{ _plotz_values }; | ||
| 185 | } | ||
| 186 | # the z_value for a specific feature_id | ||
| 187 | sub z_value { | ||
| 188 | my $self = shift; | ||
| 189 | if ($self->plot_call_colours){ | ||
| 190 | $self->cgh_call(shift); | ||
| 191 | } elsif ($self->plot_segment_colours){ | ||
| 192 | $self->seg_level(shift); | ||
| 193 | } else { | ||
| 194 | return; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | sub make_plot { | ||
| 198 | my $self = shift; | ||
| 199 | if (@_){ | ||
| 200 | $self->parse_args(@_); | ||
| 201 | } | ||
| 202 | $self->sort_data; | ||
| 203 | my ($x, $y) = $self->plot_dimensions; | ||
| 204 | ## set the y value the same as the genome plot | ||
| 205 | $self->{ _gd_object } = GD::Image->new($x,$y); | ||
| 206 | $self->set_plot_background; | ||
| 207 | $self->make_colour_grads; | ||
| 208 | $self->make_outline($x,$y); | ||
| 209 | if ($self->smoothing) { | ||
| 210 | $self->smooth_data_by_location; | ||
| 211 | } | ||
| 212 | $self->plot_spots; | ||
| 213 | if ($self->plot_segment_levels || $self->plot_breakpoints){ | ||
| 214 | if ($self->cgh_call_data){ | ||
| 215 | $self->plot_cgh_smooth_data; | ||
| 216 | } else { | ||
| 217 | warn "Microarray::Image::CGH_Plot Could not plot DNAcopy segment levels because there was no DNAcopy data available\n"; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | $self->return_image; | ||
| 221 | } | ||
| 222 | sub default_scale { | ||
| 223 | 500000; | ||
| 224 | } | ||
| 225 | sub y_scale { | ||
| 226 | 1; | ||
| 227 | } | ||
| 228 | sub plot_cgh_smooth_data { | ||
| 229 | my $self = shift; | ||
| 230 | |||
| 231 | my $ahCGH_Smooth = $self->all_cgh_smooth; | ||
| 232 | my $scale = $self->scale; | ||
| 233 | my $y_scale = $self->y_scale; | ||
| 234 | my $zero_shift = $self->shift_zero; | ||
| 235 | |||
| 236 | my $image = $self->gd_object; | ||
| 237 | |||
| 238 | for my $hSegment (@$ahCGH_Smooth){ | ||
| 239 | my $start = $hSegment->{ start }; | ||
| 240 | my $end = $hSegment->{ end }; | ||
| 241 | my $ratio = $hSegment->{ level }; | ||
| 242 | my $call = $hSegment->{ call }; | ||
| 243 | |||
| 244 | my $plot_start = int($start/$scale); | ||
| 245 | my $plot_end = int($end/$scale); | ||
| 246 | $ratio += $zero_shift if ($zero_shift); | ||
| 247 | my $plot_log = int((225 - ($ratio * 150))*$y_scale); | ||
| 248 | my $colour = $self->get_call_colour($call); # call, or 'smoothed' call | ||
| 249 | $image->filledRectangle($plot_start,($plot_log-1),$plot_end,($plot_log+1),$colour); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | # normalise the processed data relative to the image ready to be plotted | ||
| 253 | sub plot_data { | ||
| 254 | my $self = shift; | ||
| 255 | my $aaX = $self->x_values; | ||
| 256 | my $aaY = $self->y_values; | ||
| 257 | my $aaZ = $self->z_values; | ||
| 258 | |||
| 259 | my $aX = $$aaX[0]; | ||
| 260 | my $aY = $$aaY[0]; | ||
| 261 | my $aZ = $$aaZ[0] if ($aaZ && @$aaZ); | ||
| 262 | my $scale = $self->scale; | ||
| 263 | my $y_scale = $self->y_scale; | ||
| 264 | my $zero_shift = $self->shift_zero; | ||
| 265 | my $aLocns = []; | ||
| 266 | my $aLog2 = []; | ||
| 267 | my $aCall_Data = []; | ||
| 268 | |||
| 269 | my %hPlot_Values = (); | ||
| 270 | |||
| 271 | for (my $i=0; $i<@$aX; $i++ ){ | ||
| 272 | my $locn = $aX->[$i]; | ||
| 273 | my $log2 = $aY->[$i]; | ||
| 274 | next unless($locn && $log2); | ||
| 275 | |||
| 276 | $locn = int($locn/$scale); | ||
| 277 | push(@$aLocns, $locn); | ||
| 278 | |||
| 279 | my $call = $aZ->[$i] if ($aZ && @$aZ); | ||
| 280 | if ($zero_shift){ | ||
| 281 | $log2 += $zero_shift; | ||
| 282 | $call += $zero_shift if ($aZ && @$aZ); # can have null seg values | ||
| 283 | } | ||
| 284 | ## divide the y axis by 3 for a +1.5/-1.5 plot | ||
| 285 | $log2 = int((225 - ($log2 * 150))*$y_scale); | ||
| 286 | push(@$aLog2, $log2); | ||
| 287 | |||
| 288 | $call = int((225 - ($call * 150))*$y_scale) if ($aZ && @$aZ); | ||
| 289 | push(@$aCall_Data, $call) if ($aaZ && @$aaZ); | ||
| 290 | |||
| 291 | $hPlot_Values{"$locn,$log2"}++; # to avoid plotting the same values twice | ||
| 292 | } | ||
| 293 | $self->{ _plotx_values } = $aLocns; | ||
| 294 | $self->{ _ploty_values } = $aLog2; | ||
| 295 | $self->{ _plotz_values } = $aCall_Data if (@$aCall_Data && ($self->plot_call_colours || $self->plot_segment_colours)); | ||
| 296 | $self->plotted_values(\%hPlot_Values); | ||
| 297 | return($aLocns,$aLog2); | ||
| 298 | } | ||
| 299 | sub plot_dimensions { | ||
| 300 | my $self = shift; | ||
| 301 | my $scale = $self->scale; | ||
| 302 | my $y_scale = $self->y_scale; | ||
| 303 | |||
| 304 | my $chr_length = $self->chr_length($self->plot_chromosome); | ||
| 305 | my $x = int(($chr_length/$scale) + 1); | ||
| 306 | my $y = 450 * $y_scale; | ||
| 307 | my $x_margin = 0; | ||
| 308 | my $y_margin = 0; | ||
| 309 | $self->{ _x_length } = $x; | ||
| 310 | $self->{ _y_length } = $y; | ||
| 311 | $self->{ _x_margin } = 0; | ||
| 312 | $self->{ _y_margin } = 0; | ||
| 313 | $self->{ _middle } = $y/2; | ||
| 314 | |||
| 315 | return ($x,$y); | ||
| 316 | } | ||
| 317 | sub pixel_size { | ||
| 318 | my $self = shift; | ||
| 319 | if (defined $self->{ _pixel_size }){ | ||
| 320 | return $self->{ _pixel_size }; | ||
| 321 | } else { | ||
| 322 | return 3; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | sub plot_spots { | ||
| 326 | my $self = shift; | ||
| 327 | |||
| 328 | my $image = $self->gd_object; | ||
| 329 | my ($aX,$aY) = $self->plot_data; | ||
| 330 | my $hPlot_Values = $self->plotted_values; # hash record of "x,y" values to plot | ||
| 331 | my $pixel_size = $self->pixel_size; | ||
| 332 | |||
| 333 | my $aCGH_Colours = $self->plotz_values; | ||
| 334 | if (($self->plot_call_colours || $self->plot_segment_colours) && !$aCGH_Colours) { | ||
| 335 | warn "Microarray::Image::CGH_Plot Could not plot CGHcall or segment colours because there was no CGHcall/DNAcopy data available\n"; | ||
| 336 | } | ||
| 337 | |||
| 338 | for (my $i=0; $i<@$aX; $i++){ | ||
| 339 | my $x = $aX->[$i]; | ||
| 340 | my $y = $aY->[$i]; | ||
| 341 | next unless ($x && $y); | ||
| 342 | if (defined $hPlot_Values->{"$x,$y"}){ | ||
| 343 | delete $hPlot_Values->{"$x,$y"}; # first time we've seen this - only plot once, then throw away | ||
| 344 | } else { | ||
| 345 | next; # already plotted/key deleted - no point plotting the same values twice | ||
| 346 | } | ||
| 347 | my $colour; | ||
| 348 | if ($aCGH_Colours && @$aCGH_Colours){ | ||
| 349 | $colour = $self->get_colour($aCGH_Colours->[$i]); # call, or 'smoothed' call | ||
| 350 | } else { | ||
| 351 | $colour = $self->get_colour($y); | ||
| 352 | } | ||
| 353 | if ($pixel_size == 1){ | ||
| 354 | $image->setPixel($x,$y,$colour); | ||
| 355 | } else { | ||
| 356 | $image->filledEllipse($x,$y,3,3,$colour); | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | # plot the outline of the diagram, ready for spots to be added | ||
| 361 | sub make_outline { | ||
| 362 | use GD; | ||
| 363 | my $self = shift; | ||
| 364 | my $x = shift; | ||
| 365 | my $y = shift; | ||
| 366 | |||
| 367 | my $image = $self->gd_object; | ||
| 368 | my $scale = $self->scale; | ||
| 369 | my $y_scale = $self->y_scale; | ||
| 370 | my $chr = $self->plot_chromosome; | ||
| 371 | |||
| 372 | # get colours from the GD colour table | ||
| 373 | my $black = $image->colorExact(0,0,0); | ||
| 374 | my $red = $image->colorExact(255,0,0); | ||
| 375 | my $green = $image->colorExact(0,255,0); | ||
| 376 | |||
| 377 | # plot a gene location, if required | ||
| 378 | if ($self->plot_gene_locn){ | ||
| 379 | my @aGenes = $self->plot_gene_names; | ||
| 380 | for my $gene_name (@aGenes){ | ||
| 381 | next unless ($self->plot_gene_chr($gene_name) eq $chr); | ||
| 382 | my $gene_start = int($self->plot_gene_start($gene_name)/$scale); | ||
| 383 | my $gene_end = int($self->plot_gene_end($gene_name)/$scale); | ||
| 384 | my $name_length = ((length($gene_name))*6); # pixel length of gene_name | ||
| 385 | my $royal_blue = $image->colorExact(51,0,255); | ||
| 386 | $image->filledRectangle($gene_start,0,$gene_end,$y,$royal_blue); | ||
| 387 | my $name_start = $gene_end + 5; | ||
| 388 | # make sure the name is withing the plot boundary | ||
| 389 | if (($name_start + $name_length) > $x){ | ||
| 390 | $name_start = $gene_start - $name_length - 5; | ||
| 391 | } | ||
| 392 | $image->string(GD::Font->Small,$name_start,($y-15),$gene_name,$royal_blue); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | # 3px wide log2 ratio lines | ||
| 397 | $image->filledRectangle(0,(150*$y_scale),$x,(150*$y_scale),$red); # +0.5 | ||
| 398 | $image->filledRectangle(0,(225*$y_scale),$x,(225*$y_scale),$green); # 0.0 | ||
| 399 | $image->filledRectangle(0,(300*$y_scale),$x,(300*$y_scale),$red); # -0.5 | ||
| 400 | # axis labels | ||
| 401 | $image->string(GD::Font->Giant,10,(150*$y_scale),'0.5',$black); | ||
| 402 | $image->string(GD::Font->Giant,10,(225*$y_scale),'0',$black); | ||
| 403 | $image->string(GD::Font->Giant,10,(300*$y_scale),'-0.5',$black); | ||
| 404 | |||
| 405 | if ($self->plot_centromere){ | ||
| 406 | my $blue = $image->colorExact(125,125,255); | ||
| 407 | # dashed style for centromere lines | ||
| 408 | $image->setStyle($blue,$blue,$blue,$blue,gdTransparent,gdTransparent); | ||
| 409 | my $cen = int($self->chr_centromere($chr)/$scale); | ||
| 410 | $image->line($cen,0,$cen,$y,gdStyled); | ||
| 411 | } | ||
| 412 | |||
| 413 | } | ||
| 414 | # set a rainbow of graduated colours in the GD colour table, for use in the plot | ||
| 415 | sub set_plot_background { | ||
| 416 | my $self = shift; | ||
| 417 | my $image = $self->gd_object; | ||
| 418 | ## first allocated colour is set to background | ||
| 419 | my $grey = $image->colorAllocate(180,180,180); | ||
| 420 | } | ||
| 421 | sub make_colour_grads { | ||
| 422 | my $self = shift; | ||
| 423 | my $image = $self->gd_object; | ||
| 424 | $image->colorAllocate(0,0,0); | ||
| 425 | $image->colorAllocate(125,125,255); # centromere blue | ||
| 426 | |||
| 427 | for (my $i = 0; $i<=255; $i+=3){ | ||
| 428 | $image->colorAllocate($i,255,0); ## Add red -> green = yellow | ||
| 429 | $image->colorAllocate(255,$i,0); ## Add green -> red = yellow | ||
| 430 | } | ||
| 431 | $image->colorAllocate(150,150,0); # dull yellow | ||
| 432 | $image->colorAllocate(0,150,0); # dull green | ||
| 433 | $image->colorAllocate(150,0,0); # dull red | ||
| 434 | $image->colorAllocate(0,0,150); # dull blue | ||
| 435 | $image->colorAllocate(51,0,255); # royal blue | ||
| 436 | } | ||
| 437 | # the colour of the segment level (average) line | ||
| 438 | # uses the CGHcall value | ||
| 439 | sub get_call_colour { | ||
| 440 | my $self = shift; | ||
| 441 | my $call = shift; | ||
| 442 | |||
| 443 | my $image = $self->gd_object; | ||
| 444 | |||
| 445 | if ($call eq '0'){ | ||
| 446 | return $image->colorExact(150,150,0); | ||
| 447 | } elsif ($call eq '1'){ | ||
| 448 | return $image->colorExact(0,150,0); | ||
| 449 | } elsif (($call eq '-1') || ($call eq '-2')){ | ||
| 450 | return $image->colorExact(150,0,0); | ||
| 451 | } else { | ||
| 452 | return $image->colorExact(0,0,150); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | # the (default) rainbow colour pattern for spot colouring | ||
| 457 | # wide yellow region across log2=0 | ||
| 458 | # red below log2 =~ -0.77 | ||
| 459 | # green above log2 =~ +0.77 | ||
| 460 | # with a gradient in between | ||
| 461 | sub get_colour { | ||
| 462 | my $self = shift; | ||
| 463 | my $ratio = shift; | ||
| 464 | |||
| 465 | my $image = $self->gd_object; | ||
| 466 | my $y_scale = $self->y_scale; | ||
| 467 | # get colours from the GD colour table | ||
| 468 | my $red = $image->colorExact(255,0,0); | ||
| 469 | my $green = $image->colorExact(0,255,0); | ||
| 470 | my $yellow = $image->colorExact(255,255,0); | ||
| 471 | my $colour; | ||
| 472 | |||
| 473 | # DEFAULT SCALE = 2500000 = 450 pixels in Y axis | ||
| 474 | # plot is 450 pixels * $y_scale | ||
| 475 | # +0.5 is at 150 | ||
| 476 | # 0 is at 225 | ||
| 477 | # -0.5 is at 300 | ||
| 478 | # green below 110 | ||
| 479 | # red above 340 | ||
| 480 | # yellow between 190 & 260 | ||
| 481 | # have a 70 pixel yellow region across 0 | ||
| 482 | # making a colour gradient across an 80 pixel region, 40 pixels either side of +/-0.5 | ||
| 483 | # have created 85 colours for each gradient | ||
| 484 | |||
| 485 | # 255 hues / 80 pixels = 3.1875 hues per pixel for the default | ||
| 486 | my $factor = 255/(80 * $y_scale); | ||
| 487 | |||
| 488 | if ($ratio <= (110*$y_scale)){ | ||
| 489 | $colour = $green; | ||
| 490 | } elsif ($ratio >= (340*$y_scale)){ | ||
| 491 | $colour = $red; | ||
| 492 | } elsif (((260*$y_scale) > $ratio)&&($ratio > (190*$y_scale))) { | ||
| 493 | $colour = $yellow; | ||
| 494 | } elsif ($ratio >= (260*$y_scale)) { | ||
| 495 | # calculate how much green to remove from yellow, to create red | ||
| 496 | my $green_hue = int(255-($factor*($ratio-(260*$y_scale)))); # factorial = 255/(low_yellow - green) | ||
| 497 | $colour = $image->colorClosest(255,$green_hue,0); # reducing green, closer to red | ||
| 498 | } else { | ||
| 499 | # calculate how much red to remove from yellow, to create green | ||
| 500 | my $red_hue = int(255-($factor*((190*$y_scale)-$ratio))); # factorial = 255/(high_yellow - red) | ||
| 501 | $colour = $image->colorClosest($red_hue,255,0); # reducing red, closer to green | ||
| 502 | } | ||
| 503 | return($colour); | ||
| 504 | } | ||
| 505 | sub image_map { | ||
| 506 | my $self = shift; | ||
| 507 | my ($aX,$aY) = $self->plot_data; | ||
| 508 | |||
| 509 | my ($aReporters,$reporter); | ||
| 510 | if ($self->smoothing) { | ||
| 511 | $aReporters = $self->x_values; # genomic location of window centre | ||
| 512 | $reporter = 'window'; | ||
| 513 | } else { | ||
| 514 | $aReporters = $self->reporter_names; # bac names | ||
| 515 | $reporter = 'clone'; | ||
| 516 | } | ||
| 517 | |||
| 518 | my $map_name = 'cgh_plot'; | ||
| 519 | if (@_){ | ||
| 520 | $map_name = shift; | ||
| 521 | } | ||
| 522 | my $map_string = "<MAP NAME=\"$map_name\">\n"; | ||
| 523 | |||
| 524 | for (my $i=0; $i<@$aX; $i++){ | ||
| 525 | my $x = $aX->[$i]; | ||
| 526 | my $y = int($aY->[$i]) + 225; | ||
| 527 | next unless (($x && $y)&&($y>0)&&($x>0)); | ||
| 528 | my $element = $aReporters->[0][$i]; | ||
| 529 | $map_string .= "<area \"[$reporter]=[$element]\" shape=\"circle\" coords=\"$x,$y,3\" />\n"; | ||
| 530 | } | ||
| 531 | $map_string .= "</MAP>\n"; | ||
| 532 | return $map_string; | ||
| 533 | } | ||
| 534 | sub chr_centromere { | ||
| 535 | my $self = shift; | ||
| 536 | my $chr = shift; | ||
| 537 | my %hCentromere = ( | ||
| 538 | 1 => 124200000, | ||
| 539 | 2 => 93400000, | ||
| 540 | 3 => 91700000, | ||
| 541 | 4 => 50900000, | ||
| 542 | 5 => 47700000, | ||
| 543 | 6 => 60500000, | ||
| 544 | 7 => 58900000, | ||
| 545 | 8 => 45200000, | ||
| 546 | 9 => 50600000, | ||
| 547 | 10 => 40300000, | ||
| 548 | 11 => 52900000, | ||
| 549 | 12 => 35400000, | ||
| 550 | 13 => 16000000, | ||
| 551 | 14 => 15600000, | ||
| 552 | 15 => 17000000, | ||
| 553 | 16 => 38200000, | ||
| 554 | 17 => 22200000, | ||
| 555 | 18 => 16100000, | ||
| 556 | 19 => 28500000, | ||
| 557 | 20 => 27100000, | ||
| 558 | 21 => 12300000, | ||
| 559 | 22 => 11800000, | ||
| 560 | 23 => 59400000, | ||
| 561 | 'X' => 59400000, | ||
| 562 | 24 => 11500000, | ||
| 563 | 'Y' => 11500000 | ||
| 564 | ); | ||
| 565 | return ($hCentromere{$chr}); | ||
| 566 | } | ||
| 567 | sub chr_length { | ||
| 568 | my $self = shift; | ||
| 569 | my $chr = shift; | ||
| 570 | my %hChromosome = ( | ||
| 571 | # chr length | ||
| 572 | 1 => 247249719, | ||
| 573 | 2 => 242951149, | ||
| 574 | 3 => 199501827, | ||
| 575 | 4 => 191273063, | ||
| 576 | 5 => 180857866, | ||
| 577 | 6 => 170899992, | ||
| 578 | 7 => 158821424, | ||
| 579 | 8 => 146274826, | ||
| 580 | 9 => 140273252, | ||
| 581 | 10 => 135374737, | ||
| 582 | 11 => 134452384, | ||
| 583 | 12 => 132349534, | ||
| 584 | 13 => 114142980, | ||
| 585 | 14 => 106368585, | ||
| 586 | 15 => 100338915, | ||
| 587 | 16 => 88827254, | ||
| 588 | 17 => 78774742, | ||
| 589 | 18 => 76117153, | ||
| 590 | 19 => 63811651, | ||
| 591 | 20 => 62435964, | ||
| 592 | 21 => 46944323, | ||
| 593 | 22 => 49691432, | ||
| 594 | 23 => 154913754, | ||
| 595 | 'X' => 154913754, | ||
| 596 | 24 => 57772954, | ||
| 597 | 'Y' => 57772954, | ||
| 598 | 25 => 3080419480 # END | ||
| 599 | ); | ||
| 600 | return ($hChromosome{$chr}); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | { package genome_cgh_plot; | ||
| 604 | |||
| 605 | # must try genome_cgh first, otherwise will move through cgh_plot to chromosome_cgh | ||
| 606 | our @ISA = qw( genome_cgh cgh_plot ); | ||
| 607 | |||
| 608 | sub plot_dimensions { | ||
| 609 | my $self = shift; | ||
| 610 | my $scale = $self->scale; | ||
| 611 | my $y_scale = $self->y_scale; | ||
| 612 | my $genome_length = $self->chr_length(25); # end | ||
| 613 | my $x = ($genome_length/$scale)+25; | ||
| 614 | my $y = 450 * $y_scale; | ||
| 615 | my $x_margin = 0; | ||
| 616 | my $y_margin = 0; | ||
| 617 | $self->{ _x_length } = $x; | ||
| 618 | $self->{ _y_length } = $y; | ||
| 619 | $self->{ _x_margin } = 0; | ||
| 620 | $self->{ _y_margin } = 0; | ||
| 621 | $self->{ _middle } = $y/2; | ||
| 622 | |||
| 623 | return ($x,$y); | ||
| 624 | } | ||
| 625 | sub default_scale { | ||
| 626 | ## set scale for the genome plot to 2.5Mb per pixel | ||
| 627 | 2500000; | ||
| 628 | } | ||
| 629 | sub y_scale { | ||
| 630 | my $self = shift; | ||
| 631 | my $scale = $self->scale; | ||
| 632 | my $default_scale = $self->default_scale; | ||
| 633 | return $default_scale/$scale; | ||
| 634 | } | ||
| 635 | sub mini_plot { | ||
| 636 | my $self = shift; | ||
| 637 | $self->scale(10000000); | ||
| 638 | $self->{ _pixel_size } = 1; | ||
| 639 | } | ||
| 640 | sub font_size { | ||
| 641 | my $self = shift; | ||
| 642 | if ($self->scale <= 2000000 ){ | ||
| 643 | return 'Giant'; | ||
| 644 | } elsif ($self->scale < 3000000 ){ | ||
| 645 | return 'Small'; | ||
| 646 | } else { | ||
| 647 | return 'Tiny'; | ||
| 648 | } | ||
| 649 | } | ||
| 650 | sub plot_chromosome { | ||
| 651 | return; | ||
| 652 | } | ||
| 653 | sub plot_cgh_smooth_data { | ||
| 654 | my $self = shift; | ||
| 655 | |||
| 656 | my $aahAll_CGH_Smooth = $self->all_cgh_smooth; | ||
| 657 | my $scale = $self->scale; | ||
| 658 | my $y_scale = $self->y_scale; | ||
| 659 | my $zero_shift = $self->shift_zero; | ||
| 660 | my $pixel_size = $self->pixel_size; | ||
| 661 | |||
| 662 | my $image = $self->gd_object; | ||
| 663 | |||
| 664 | for my $chr ((0..23)){ | ||
| 665 | my $ahCGH_Smooth = $aahAll_CGH_Smooth->[$chr]; | ||
| 666 | my $chr_offset = $self->chr_offset($chr+1); | ||
| 667 | for my $hSegment (@$ahCGH_Smooth){ | ||
| 668 | my $start = $hSegment->{ start }; | ||
| 669 | my $end = $hSegment->{ end }; | ||
| 670 | my $ratio = $hSegment->{ level }; | ||
| 671 | my $call = $hSegment->{ call }; | ||
| 672 | |||
| 673 | my $plot_start = int(($start/$scale) + ($chr_offset/$scale)) + 25; | ||
| 674 | my $plot_end = int(($end/$scale) + ($chr_offset/$scale)) + 25; | ||
| 675 | $ratio += $zero_shift if ($zero_shift); | ||
| 676 | my $plot_log = int((225 - ($ratio * 150))*$y_scale); | ||
| 677 | my $colour = $self->get_call_colour($call); # call, or 'smoothed' call | ||
| 678 | if ($pixel_size == 1){ | ||
| 679 | $image->line($plot_start,$plot_log,$plot_end,$plot_log,$colour); | ||
| 680 | } else { | ||
| 681 | $image->filledRectangle($plot_start,($plot_log-1),$plot_end,($plot_log+1),$colour); | ||
| 682 | } | ||
| 683 | } | ||
| 684 | } | ||
| 685 | } | ||
| 686 | sub plot_data { | ||
| 687 | my $self = shift; | ||
| 688 | my $scale = $self->scale; | ||
| 689 | my $y_scale = $self->y_scale; | ||
| 690 | my $aaX = $self->x_values; | ||
| 691 | my $aaY = $self->y_values; | ||
| 692 | my $aaZ = $self->z_values; | ||
| 693 | |||
| 694 | my $zero_shift = $self->shift_zero; | ||
| 695 | my $aLocns = []; | ||
| 696 | my $aLog2 = []; | ||
| 697 | my $aCall_Data = []; | ||
| 698 | my %hPlot_Values = (); | ||
| 699 | |||
| 700 | for my $chr ((0..23)){ | ||
| 701 | my $aX = $$aaX[$chr]; | ||
| 702 | my $aY = $$aaY[$chr]; | ||
| 703 | my $aZ = $$aaZ[$chr] if ($aaZ && @$aaZ); | ||
| 704 | my $chr_offset = $self->chr_offset($chr+1); | ||
| 705 | for (my $i=0; $i<@$aX; $i++ ){ | ||
| 706 | my $locn = $aX->[$i]; | ||
| 707 | my $log2 = $aY->[$i]; | ||
| 708 | next unless($locn && $log2); | ||
| 709 | |||
| 710 | $locn = int(($locn/$scale) + ($chr_offset/$scale) + 25); | ||
| 711 | push(@$aLocns, $locn); | ||
| 712 | |||
| 713 | my $call = $aZ->[$i] if ($aZ && @$aZ); | ||
| 714 | if ($zero_shift){ | ||
| 715 | $log2 += $zero_shift; | ||
| 716 | $call += $zero_shift if ($aZ && @$aZ); # can have null seg values | ||
| 717 | } | ||
| 718 | |||
| 719 | ## multiply the log value by a quarter of the y axis to get a +2/-2 plot | ||
| 720 | $log2 = int((225 - ($log2 * 150))*$y_scale); | ||
| 721 | push(@$aLog2, $log2); | ||
| 722 | |||
| 723 | $call = int((225 - ($call * 150))*$y_scale) if ($aZ && @$aZ); | ||
| 724 | push(@$aCall_Data, $call) if ($aaZ && @$aaZ); | ||
| 725 | |||
| 726 | $hPlot_Values{"$locn,$log2"}++; # to avoid plotting the same values twice | ||
| 727 | } | ||
| 728 | } | ||
| 729 | $self->{ _plotx_values } = $aLocns; | ||
| 730 | $self->{ _ploty_values } = $aLog2; | ||
| 731 | $self->{ _plotz_values } = $aCall_Data if (@$aCall_Data && ($self->plot_call_colours || $self->plot_segment_colours)); | ||
| 732 | $self->plotted_values(\%hPlot_Values); | ||
| 733 | return($aLocns,$aLog2); | ||
| 734 | } | ||
| 735 | # Harcode the plot outline for the genome plot as dimensions do not change | ||
| 736 | sub make_outline { | ||
| 737 | use GD; | ||
| 738 | my $self = shift; | ||
| 739 | my $x = shift; | ||
| 740 | my $y = shift; | ||
| 741 | my $image = $self->gd_object; | ||
| 742 | my $scale = $self->scale; | ||
| 743 | my $y_scale = $self->y_scale; | ||
| 744 | my $font = $self->font_size; | ||
| 745 | |||
| 746 | # get colours from the GD colour table | ||
| 747 | my $black = $image->colorExact(0,0,0); | ||
| 748 | my $red = $image->colorExact(255,0,0); | ||
| 749 | my $green = $image->colorExact(0,255,0); | ||
| 750 | my $blue = $image->colorExact(125,125,255); | ||
| 751 | my $royal_blue = $image->colorExact(51,0,255); | ||
| 752 | |||
| 753 | # 3px wide log2 ratio lines | ||
| 754 | $image->filledRectangle(0,(150*$y_scale),$x,(150*$y_scale),$red); # +0.5 | ||
| 755 | $image->filledRectangle(0,(225*$y_scale),$x,(225*$y_scale),$green); # 0.0 | ||
| 756 | $image->filledRectangle(0,(300*$y_scale),$x,(300*$y_scale),$red); # -0.5 | ||
| 757 | # axis labels | ||
| 758 | $image->string(GD::Font->$font,0,(150*$y_scale),'0.5',$black); | ||
| 759 | $image->string(GD::Font->$font,0,(225*$y_scale),'0',$black); | ||
| 760 | $image->string(GD::Font->$font,0,(300*$y_scale),'-0.5',$black); | ||
| 761 | $image->setStyle($blue,$blue,$blue,$blue,gdTransparent,gdTransparent); | ||
| 762 | |||
| 763 | # plot chr separator lines and chr names for each chromosome | ||
| 764 | for my $chr ((1..24)){ | ||
| 765 | my $start = int($self->chr_offset($chr)/$scale); | ||
| 766 | my $end = int($self->chr_offset($chr+1)/$scale); | ||
| 767 | my $middle = int(($start+$end)/2); | ||
| 768 | if ($self->plot_centromere){ | ||
| 769 | # centromere | ||
| 770 | my $cen = int(($self->chr_offset($chr)+$self->chr_centromere($chr))/$scale); | ||
| 771 | # dashed style for centromere lines | ||
| 772 | $image->line($cen+25,0,$cen+25,(450*$y_scale),gdStyled); | ||
| 773 | } | ||
| 774 | # chr buffer | ||
| 775 | $image->filledRectangle($start+25,0,$start+25,(450*$y_scale),$black); | ||
| 776 | # set chr names | ||
| 777 | my $chr_name; | ||
| 778 | if ($chr == 23){ | ||
| 779 | $chr_name = 'X'; | ||
| 780 | } elsif ($chr == 24){ | ||
| 781 | $chr_name = 'Y'; | ||
| 782 | # end line | ||
| 783 | $image->line($end+25,0,$end+25,(450*$y_scale),$black); | ||
| 784 | } else { | ||
| 785 | $chr_name = $chr; | ||
| 786 | } | ||
| 787 | # print chr name at bottom of plot | ||
| 788 | $image->string(GD::Font->$font,$middle+20,(420*$y_scale),$chr_name,$black); | ||
| 789 | |||
| 790 | if ($self->plot_gene_locn){ | ||
| 791 | my @aGenes = $self->plot_gene_names; | ||
| 792 | for my $gene_name (@aGenes){ | ||
| 793 | next unless ($self->plot_gene_chr($gene_name) eq $chr_name); | ||
| 794 | my $gene_start = int(($self->chr_offset($chr) + $self->plot_gene_start($gene_name))/$scale); | ||
| 795 | my $gene_end = int(($self->chr_offset($chr) + $self->plot_gene_end($gene_name))/$scale); | ||
| 796 | $image->filledRectangle($gene_start+25,0,$gene_end+25,(450*$y_scale),$royal_blue); | ||
| 797 | } | ||
| 798 | } | ||
| 799 | } | ||
| 800 | } | ||
| 801 | sub chr_offset { | ||
| 802 | my $self = shift; | ||
| 803 | my $chr = shift; | ||
| 804 | my %hChromosome = ( | ||
| 805 | # start bp # chr length | ||
| 806 | 1 => 0, # 247249719 | ||
| 807 | 2 => 247249720, # 242951149 | ||
| 808 | 3 => 490200869, # 199501827 | ||
| 809 | 4 => 689702696, # 191273063 | ||
| 810 | 5 => 880975759, # 180857866 | ||
| 811 | 6 => 1061833625, # 170899992 | ||
| 812 | 7 => 1232733617, # 158821424 | ||
| 813 | 8 => 1391555041, # 146274826 | ||
| 814 | 9 => 1537829867, # 140273252 | ||
| 815 | 10 => 1678103119, # 135374737 | ||
| 816 | 11 => 1813477856, # 134452384 | ||
| 817 | 12 => 1947930240, # 132349534 | ||
| 818 | 13 => 2080279774, # 114142980 | ||
| 819 | 14 => 2194422754, # 106368585 | ||
| 820 | 15 => 2300791339, # 100338915 | ||
| 821 | 16 => 2401130254, # 88827254 | ||
| 822 | 17 => 2489957508, # 78774742 | ||
| 823 | 18 => 2568732250, # 76117153 | ||
| 824 | 19 => 2644849403, # 63811651 | ||
| 825 | 20 => 2708661054, # 62435964 | ||
| 826 | 21 => 2771097018, # 46944323 | ||
| 827 | 22 => 2818041341, # 49691432 | ||
| 828 | 23 => 2867732773, # 154913754 | ||
| 829 | 'X' => 2867732773, # 154913754 | ||
| 830 | 24 => 3022646527, # 57772954 | ||
| 831 | 'Y' => 3022646527, # 57772954 | ||
| 832 | 25 => 3080419480 # END | ||
| 833 | ); | ||
| 834 | return ($hChromosome{$chr}); | ||
| 835 | } | ||
| 836 | } | ||
| 837 | |||
| 838 | 1; | ||
| 839 | |||
| 840 | __END__ |