dotplot.pl

Invoke on the command line using:


   % cat TB.mum.out | dotplot.pl thing.png


#!/usr/bin/perl use strict; use GD; my ($BLACK, $WHITE, $RED); my $BORDER = 50; my $WIDTH = 400; my $TOTAL_X = $BORDER + $BORDER + $WIDTH; my $TOTAL_Y = $BORDER + $BORDER + $WIDTH; my $file_name = $ARGV[0]; my ($max_x, $max_y, $x, $y, $l, @list); $max_x = 0; $max_y = 0; while(<STDIN>) { if (!/>/) { s/^\s*//; my($x, $y, $l) = split(/\s+/); push(@list, $x . " " . $y . " " . $l); $max_x = $x if ($x > $max_x); $max_y = $y if ($y > $max_y); } } my $max_length = $max_x; $max_length = $max_y if ($max_y > $max_x); my $transform_unit = $WIDTH / $max_length; my $image = init_image($TOTAL_X, $TOTAL_Y); place_axes($image, $transform_unit, $max_x, $max_y); place_data($image, $transform_unit, \@list); write_file($image, $file_name); exit(1); sub place_data { my($i, $t, $l) = @_; my($x1, $y1, $x2, $y2); for my $row (@$l) { my($x, $y, $length) = split(/\s/,$row); $x1 = x2pos($x, $t); $x2 = x2pos($x+$length, $t); $y1 = y2pos($y, $t); $y2 = y2pos($y+$length, $t); $i->line($x1, $y1, $x2, $y1,$BLACK); $i->line($x1, $y1, $x1, $y2,$BLACK); } } sub place_axes { my($i, $t, $x, $y) = @_; my($x1, $y1, $x2, $y2); $x1 = x2pos(0, $t); $x2 = x2pos($x, $t); $y1 = y2pos(0, $t); $y2 = y2pos($y, $t); $i->line($x1, $y1, $x2, $y1,$BLACK); $i->line($x1, $y1, $x1, $y2,$BLACK); } sub x2pos { my($n, $transform) = @_; return(int(($n * $transform) + $BORDER)); } sub y2pos { my($n, $transform) = @_; return(int($WIDTH - ($n * $transform)) + $BORDER); } sub write_file { my ($i, $f) = @_; open(F_OUT, ">$f") || die "I cant write $f"; binmode F_OUT; print F_OUT $image->png; close(F_OUT); } sub init_image { my($x,$y) = @_; my($image) = new GD::Image($x,$y); $BLACK = $image->colorAllocate(0,0,0); $RED = $image->colorAllocate(255,0,0); $WHITE = $image->colorAllocate(255,255,255); $image->fill(2,2,$WHITE); return($image); }
left