#!/usr/bin/perl -w # # use strict; use Image::Magick; my $usage = "Usage: $0 \n"; #my $infile = 'Gene_Corr.out'; #my $infile = 'GxG_test.out'; my $lastCoord = 0; my $infile = shift or die $usage; my $outfile = shift or die $usage; #my %matrix = &readData($infile); my @dataArray = &readData($infile); my $size = 0; foreach my $rec (@dataArray) { $size = $rec->{X} if ($rec->{X} > $size); $size = $rec->{Y} if ($rec->{Y} > $size); } my $mult = 1; #my $size = $mult * ((keys %matrix) + 0); $size = $mult * $size; print("Drawing...\n"); my $image = &initGraphics($size,$size); #my $maxVal = do {($^O eq 'MSWin32') ? 255 : 65535}; #$maxVal = 255; #my $y = 0; #foreach my $row (sort keys %matrix) { # my $x = $y + $mult; # foreach my $col (sort keys %{ $matrix{$row} }) { # my $hexColor = &selectColor($matrix{$row}{$col}); # &drawSquare($image, $x, $y, $hexColor); # &drawSquare($image, $y, $x, $hexColor); # $x += $mult; # } # $y += $mult; # } foreach my $rec (@dataArray) { my $hexColor = &selectColor($rec->{VALUE}); &drawSquare($image, $rec->{X}-1, $rec->{Y}-1, $hexColor); &drawSquare($image, $rec->{Y}-1, $rec->{X}-1, $hexColor); } # Symbolic names #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); #&drawLines($image, $size, 4); # Symbolic names #&drawLines($image, $size, 100); #&drawLines($image, $size, 10); #&drawLines($image, $size, 85); #&drawLines($image, $size, 5); #&drawLines($image, $size, 126); #&drawLines($image, $size, 12); #&drawLines($image, $size, 114); #&drawLines($image, $size, 47); # Chromosome location #&drawLines($image, $size, 25); #&drawLines($image, $size, 83); #&drawLines($image, $size, 48); #&drawLines($image, $size, 38); #&drawLines($image, $size, 12); #&drawLines($image, $size, 29); #&drawLines($image, $size, 27); #&drawLines($image, $size, 26); #&drawLines($image, $size, 27); #&drawLines($image, $size, 21); #&drawLines($image, $size, 25); #&drawLines($image, $size, 46); #&drawLines($image, $size, 40); #&drawLines($image, $size, 7); #&drawLines($image, $size, 17); #&drawLines($image, $size, 18); #&drawLines($image, $size, 23); #&drawLines($image, $size, 42); #&drawLines($image, $size, 7); #&drawLines($image, $size, 33); #&drawLines($image, $size, 10); #&drawLines($image, $size, 6); $image->Write($outfile, compression=>'BZip'); if ($outfile =~ /\.pnggggg$/) { $image->Write(filename=>"png8:$outfile"); } else { $image->Write(filename=>$outfile); } undef $image; sub readData { my ($infile) = @_; print("Reading...\n"); open(IN, $infile) or die "Cannot open $infile\n"; my @dataArray = (); while () { chomp; my ($this, $x, $that, $y, $value1) = split(/\s+/); my $rec = { X => $x, Y => $y, VALUE => $value1, }; push(@dataArray, $rec); } close(IN); return @dataArray; } sub readDataOld { my ($infile) = @_; my %HoH = (); open(IN, $infile) or die "Cannot open $infile\n"; while () { chomp; my ($this, $that, $value1, $value2) = split(/\s+/); $HoH{$this}{$that} = $value1; } close(IN); return %HoH; } sub initGraphics { my ($width, $height) = @_; my $image = Image::Magick->new(); $image->Set(size=>"${width}x${height}"); $image->ReadImage('xc:#ff0000'); return $image; } sub drawSquare { my ($image, $x, $y, $hexColor) = @_; # my $hex = sprintf("%x", $value); # lower case a-f if ($mult == 1) { $image->Set("pixel[$x,$y]"=>$hexColor); #$image->Set("pixel[$x,$y]"=>"$value,$value,$value"); } else { for (my $ym=0; $ym<$mult; $ym++) { for (my $xm=0; $xm<$mult; $xm++) { my $x1 = $x + $xm; my $y1 = $y + $ym; $image->Set("pixel[$x1,$y1]"=>$hexColor); } } } #my $x2 = $x + $mult - 1; #my $y2 = $y + $mult - 1; #$image->Draw(primitive=>'rectangle', fill=>"#$hex$hex$hex", stroke=>'', points=>"$x,$y $x2,$y2"); } sub selectColor { my ($value) = @_; my $maxVal = 255; my $gray = 0; my $hexColor; if ($gray) { my $gray = int(0.5*($value + 1)*$maxVal); my $hex = sprintf("%x", $gray); # lower case a-f $hexColor = "#$hex$hex$hex"; } else { if ($value <= -0.8) { $hexColor = '#0000ff'; } elsif ($value <= -0.6) { $hexColor = '#3399ff'; } elsif ($value <= -0.4) { $hexColor = '#66ccff'; } elsif ($value <= -0.2) { $hexColor = '#99ccff'; } elsif ($value <= -0.05) { $hexColor = '#ccccff'; } elsif ($value <= 0.05) { $hexColor = '#ffffff'; } elsif ($value <= 0.2) { $hexColor = '#ffccff'; } elsif ($value <= 0.4) { $hexColor = '#ff99ff'; } elsif ($value <= 0.6) { $hexColor = '#ff66cc'; } elsif ($value <= 0.8) { $hexColor = '#ff6666'; } else { $hexColor = '#ff0000'; } } return $hexColor; } sub drawLines { my ($image, $size, $coord) = @_; my $max = $size - 1; $coord += $lastCoord; $lastCoord = $coord; $coord--; print("$coord\n"); $image->Draw(primitive=>'line', stroke=>'#000000', points=>"$coord,0 $coord,$max"); $image->Draw(primitive=>'line', stroke=>'#000000', points=>"0,$coord $max,$coord"); return; }