#!/usr/bin/perl # module for plotting array data and generating an imagemap # Chris Seidel Summer 2000 use GD; use CGI qw(:standard); use POSIX; require "../arraylib.pl"; #seed the randomnumber generator srand($$); $ln10 = log(10); $ln2 = log(2); #image size $width = 700; $height = 500; $arraypath = "/your/subdir/dbhttpd/html/amad/data"; $q = new CGI; if( $q->param() ){ &plotArray; }else{ &myForm; } exit; sub plotArray { print header; if($q->param('plot')){ $uID = $q->param('plot'); open( ARRAYS, "/your/subdir/dbhttpd/lib/amad/tools/arrays.txt" ); while(){ @array_info = split(/\t/); last if($array_info[0] eq $uID); } close(ARRAYS); $array = $array_info[4]; }else{ @array_info = split(/\t/, $q->param('array')); $array = $array_info[4]; } print "$array_info[0]\n"; &PrintNavBar(); print "

$array_info[1]

\n"; print "

Unique ID:  $array_info[0]\nProbe1: $array_info[2]\nProbe2: $array_info[3]
\n"; # $i = 0; # foreach $thing (@array_info){ # print "$i: $thing
"; # ++$i; # } open(DATA, "$arraypath/$array") || die("Couldn't open array $array"); $j=0; $line = ; #find which elements contain the patterns @d_bits = split(/\t/,$line); $i=0; foreach $thing (@d_bits){ if($thing eq "CH1D"){ $ch1d = $i; }elsif($thing eq "CH2D"){ $ch2d = $i; } #find ORF if($thing eq "NAME"){ $orf = $i; } ++$i; } # get data while( $line = ){ chop($line); @d_bits = split(/\t/,$line); # net counts for CH1 and CH2 unless( ($d_bits[$ch1d] =~ /[0-9]/) && ($d_bits[$ch2d] =~ /[0-9]/) && ($d_bits[$ch1d] > 0 ) && ($d_bits[$ch2d] > 0 ) ){ next; } # get orf $orfs[$j] = $d_bits[$orf]; $ch1[$j] = $d_bits[$ch1d]; $ch2[$j] = $d_bits[$ch2d]; $ch1Sum += $ch1[$j]; $ch2Sum += $ch2[$j]; ++$j; } close(DATA); #got the data, now do something with it $nmbr_points = $j; $avg_ch1 = $ch1Sum/$nmbr_points; $avg_ch2 = $ch2Sum/$nmbr_points; $norm = $avg_ch2/$avg_ch1; $log_of_2 = &logbase(2,2); #calculate this only once &MakeImage; if( $q->param('log') == 1 ){ &plotLogvsLog; &writeImage; &htmlLogvsLog; }else{ &CalcLog2RatvsRtG; &plotLog2RatvsRtG; &writeImage; &htmlLog2RatvsRtG; } } sub CalcLog2RatvsRtG { $highRtG = 0; $lowRtG = 10; $highRatio = 0; $lowRatio = 0; for($i=0; $i< $nmbr_points; ++$i){ $RtimesG[$i] = &log2( sqrt($ch1[$i] * $ch2[$i]) ); $ratio = ($ch1[$i]/$ch2[$i])*$norm; $log2[$i] = &log2($ratio); # for SD calc $ratioSum += $log2[$i]; # find highest and lowest values if($RtimesG[$i] > $highRtG){ $highRtG = $RtimesG[$i]; } if($RtimesG[$i] < $lowRtG){ $lowRtG = $RtimesG[$i]; } if( ($log2[$i] > 0) && ($log2[$i] > $highRatio) ){ $highRatio = $log2[$i]; }elsif($log2[$i] < 0 && $log2[$i] < $lowRatio){ $lowRatio = $log2[$i]; } # use absolute value of highest or lowest ratio if( $highRatio < abs($lowRatio) ){ $max_ratio = abs($lowRatio); }else{ $max_ratio = $highRatio; } } #end for $meanLog2Ratio = $ratioSum/$nmbr_points; for($i=0; $i< $nmbr_points; ++$i){ $devSum += ($log2[$i] - $meanLog2Ratio)*($log2[$i] - $meanLog2Ratio); } $StdDev = sqrt( $devSum/$nmbr_points ); } sub MakeImage { $im = new GD::Image($width, $height); &AllocateColors; } sub drawAxesLog2RatvsRtG { # y axis $im->line($left_margin,$top_margin,$left_margin,$height-$bottom_margin,$red); # x axis $im->line($tx, $ty,$tx+$x_span, $ty,$red); #labels $im->string(gdSmallFont, $width/2 - $left_margin, $height-25, "log2(sqrt(G * R))", $red); $im->stringUp(gdSmallFont, 5, int($height/2)+25, "log2 G/R", $red); #label y ranges $y_max_label = sprintf("%.2f",$max_ratio); $im->string(gdSmallFont, 1, $top_margin-2, $y_max_label, $blue); $im->string(gdSmallFont, 1, $height-$bottom_margin-10, "-".$y_max_label, $blue); # label x range $x_max_label = sprintf("%.2f",$highRtG); $im->string(gdSmallFont, $left_margin+$x_span-10, $top_margin+($y_span/2), $highRtG, $blue); $im->string(gdSmallFont, $left_margin+5, $top_margin+($y_span/2), $lowest_x, $blue); #upper right corner label $norm_label = sprintf("%.2f",$norm); $im->string(gdSmallFont, $width-($width/5), $top_margin, "N: $norm_label", $blue); $avgCh1_label = int($avg_ch1); $im->string(gdSmallFont, $width-($width/5), $top_margin+15, "Avg Ch1: $avgCh1_label", $blue); $avgCh2_label = int($avg_ch2); $im->string(gdSmallFont, $width-($width/5), $top_margin+30, "Avg Ch2: $avgCh2_label", $blue); } sub plotLog2RatvsRtG { $top_margin = 15; $bottom_margin = 15; $left_margin = 25; $right_margin = 15; $x_span = $width - $left_margin - $right_margin; $y_span = $height - $top_margin - $bottom_margin; $tx = $left_margin; $ty = int( $top_margin + (0.5 * $y_span) ); $lowest_x = 0; # scale the output for the x axis $lowest_x = floor($lowRtG) - 1 if($lowRtG >= 2); &drawAxesLog2RatvsRtG; print ""; $total_imap = 0; $sd_cutoff = 3; $sd_cutoff = $q->param('sd_cutoff') if ( defined( $q->param('sd_cutoff')) ); if( $q->param('clr') ){ $clr_red = $red; $clr_green = $drkgreen; }else{ $clr_red = $black; $clr_green = $black; } for($i=0; $i<$nmbr_points; ++$i){ # $x = int( ($RtimesG[$i]/$highRtG) * $x_span ); $x = int( ( ($RtimesG[$i]-$lowest_x)/($highRtG-$lowest_x) ) * $x_span ); $y = int( ($log2[$i]/$max_ratio) * ($y_span/2) ); if( ( (abs($log2[$i])-abs($meanLog2Ratio))/$StdDev) > $sd_cutoff ){ if($log2[$i] > 0 ){ $im->filledRectangle($tx+$x-1,$ty-$y-1,$tx+$x+1,$ty-$y+1,$clr_green); } if($log2[$i] < 0 ){ $im->filledRectangle($tx+$x-1,$ty-$y-1,$tx+$x+1,$ty-$y+1,$clr_red); } print "\"$orfs[$i]\"\n"; ++$total_imap; }else{ $im->filledRectangle($tx+$x-1,$ty-$y-1,$tx+$x+1,$ty-$y+1,$black); } } print ""; } sub plotLogvsLog { $top_margin = 15; $bottom_margin = 15; $left_margin = 25; $right_margin = 15; $x_span = $width - $left_margin - $right_margin; $y_span = $height - $top_margin - $bottom_margin; $tx = $left_margin; $ty = int( $top_margin + $y_span ); &drawAxesLogvsLog; for($i=0; $i<$nmbr_points; ++$i){ $x = int( (&log10($ch1[$i])/5) * $x_span ); $y = int( (&log10($ch2[$i])/5) * $y_span ); # printf("$ch1[$i]: %.3f\n", &logbase($ch1[$i],10)); $im->filledRectangle($tx+$x-1,$ty-$y-1,$tx+$x+1,$ty-$y+1,$black); } } sub drawAxesLogvsLog { # y axis $im->line($left_margin,$top_margin,$left_margin,$height-$bottom_margin,$red); # x axis $im->line($tx, $ty,$tx+$x_span, $ty,$red); #labels $im->string(gdSmallFont, $width/2 - $left_margin, $height-15, "log10 Ch1", $red); $im->stringUp(gdSmallFont, 5, int($height/2)+25, "log10 Ch2", $red); #label y ranges $im->string(gdSmallFont, $left_margin-10, $top_margin-2, "5", $blue); # label x range $x_max_label = sprintf("%.2f",$highRtG); $im->string(gdSmallFont, $left_margin+$x_span-5, $top_margin+$y_span+3, "5", $blue); #bottom right corner label $norm_label = sprintf("%.2f",$norm); $im->string(gdSmallFont, $width-($width/5), $top_margin+$y_span-($y_span/5), "N: $norm_label", $blue); $avgCh1_label = int($avg_ch1); $im->string(gdSmallFont, $width-($width/5), $top_margin+$y_span-($y_span/5)+15, "Avg Ch1: $avgCh1_label", $blue); $avgCh2_label = int($avg_ch2); $im->string(gdSmallFont, $width-($width/5), $top_margin+$y_span-($y_span/5)+30, "Avg Ch2: $avgCh2_label", $blue); } sub htmlLog2RatvsRtG { print "
\n"; print ""; print "

\n"; print "\n"; print "\n\n"; print "\n"; printf("\n", $highRtG, $lowRtG); printf("\n",$highRatio,$lowRatio); printf("\n",$meanLog2Ratio ); printf("\n", $StdDev); print "
Number of entries:$nmbr_points
Number greater than $sd_cutoff SD:$total_imap
High and Low log2(sqrt(G*R)):%.3f     %.3f
High and Low Log2(Ratio): %.3f     %.3f
mean Log2 Ratio is: %.4f
The Standard Deviation is: %.4f
\n"; print "Return\n"; } sub htmlLogvsLog { print "
\n"; print ""; print "

\n"; print "

";
    print "Number of entries: $nmbr_points\n";
#    print "Highest log2(sqrt(G*R)): $highRtG\n";
#    print "High and Low Log2(Ratio): $highRatio $lowRatio\n";
    print "Return\n";
}

sub writeImage {

    $rand_int = int( rand(999) );

$imagefilename = "$array".$rand_int.".gif";
#print "imagefile: $imagefilename\n";

open(GIF, ">tmp/$imagefilename");
binmode GIF;
print GIF $im->gif;
close(GIF);

}

sub AllocateColors {
$white = $im->colorAllocate(255,255,255);
$red   = $im->colorAllocate(255,0,0);
$blue  = $im->colorAllocate(0,0,255);
$green = $im->colorAllocate(0,255,0);
$drkgreen = $im->colorAllocate(0,153,0);
$black = $im->colorAllocate(0,0,0);
}
sub myForm {

    print header;
    print "Array Plotter\n";
    &PrintNavBar();
    print<Plotting Utility
This utility will show a plot of log(G/R,2) vs log(sqrt(G*R),2) or log(ch1,10) vs log(ch2,10) 
for any of the arrays in the database. Select the array, and push Plot. 
Things I'm planning to add
are:
  • Color coding data points via functional category, genelist, etc.
  • Proper scaling and tick marks
  • Better selection/display of current arrays
  • Other kinds of plots and analysis summaries
Any ideas or suggestions, let me know!. EOF open( ARRAYS, "/your/subdir/dbhttpd/lib/amad/tools/arrays.txt" ); print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n
Log2(G/R) vs Log2(sqrt(G*R))
\n"; print "Log10 vs Log10   (no imap yet)
\n"; print "Use color SD cutoff for imagemap

"; print ""; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; $count = 1; while(){ @array_list = split(/\t/); if( ($count%2) == 0 ){ print "\n"; }else{ print "\n"; } print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print "\n"; ++$count; } print "
#PlotUniqueIDDescriptionProbe 1Probe 2CategoryUserPrintlist
$count$array_list[0]$array_list[1]$array_list[2]$array_list[3]$array_list[7]$array_list[8]$array_list[10]
\n"; print "

\n"; close(ARRAYS); } sub logbase { my ($number,$base) = @_; return if $number <= 0 or $base <= 0 or $base == 1; return log($number)/log($base); } sub log2 { #$ln2 is defined above to keep from repeatedly calculating it my ($number) = @_; return if $number <= 0 or $number == 1; return log($number)/$ln2; } sub log10 { #$ln10 is defined above to keep from repeatedly calculating it my ($number) = @_; return if $number <= 0 or $number == 1; return log($number)/$ln10; }