#!/usr/bin/perl

use CGI qw(param);
use GD;

$id = param("id");
$id =~ tr/a-z/A-Z/;


print "Content-type:text/html\n\n";
print "<html><title></title><body>\n";
print "<h1>The correlated genes with $id</h1>\n";




$im = new GD::Image(300, 300);
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$grey = $im->colorAllocate(200,200,200);
$red = $im->colorAllocate(250,0,0);
$green = $im->colorAllocate(0,255,0);
$blue = $im->colorAllocate(0,0,255);
$greenblue = $im->colorAllocate(0,255,255);


print "<b><FONT FACE=\"sans-serif,helvetica,arial\">Using method of local pattern</font></b><br>";
open(RAW_DATA, "pseu_dp_worm_6.txt");
   
$ii = 0;
while($buffer= <RAW_DATA>){
   chomp($buffer);
   ($gene1, $gene2, $score, $x, $y, $neg_pos) = split(/ +/, $buffer);

   if($gene1 eq $id){
       $level1{$ii} = $gene2;
       if($x != $y){
          $anno{$ii} = "t";
          }
       else{
          $anno{$ii} = "i";
          }

       $ii++;
       }

   if($gene2 eq $id){
       $level1{$ii} = $gene1;
       if($x != $y){
          $anno{$ii} = "t";
          }
       else{
          $anno{$ii} = "i";
          }

       $ii++;
       }

    }
close(RAW_DATA);
$num_level1 = $ii;

if($num_level1 == 0){
   print "No strong correlated genes found<br>";
   }
if($num_level1 == 1){
   $centerx = 120;
   $centery = 150;
   $im->arc($centerx,$centery,12,12,0,360,$red);
   $im->fill($centerx, $centery, $red);
#   $gn = $gene_ORF{$id};
   $gn = $id;
   if(length($gn) == 0){
       $gn = $id;
       }
   $im->string(gdMediumBoldFont,$centerx-16,$centery+8,$gn, $red);
   }
else{
   $centerx = 150;
   $centery = 150;
   $im->arc($centerx,$centery,12,12,0,360,$red);
   $im->fill($centerx, $centery, $red);
#   $gn = $gene_ORF{$id};
   $gn = $id;
   if(length($gn) == 0){
       $gn = $id;
       }
   $im->string(gdMediumBoldFont,$centerx-16,$centery+8,$gn, $red);
   }

$dis = 80;

  print "<map name=img_map>\n";

if($num_level1 > 0){
   for($i = 0; $i < $num_level1; $i++){
         $angle = $i * 2*3.1415926/$num_level1;
         $posx = $centerx + $dis * cos($angle);
         $posy = $centery + $dis * sin($angle);
         $name = $level1{$i};
#         $gn = $gene_ORF{$name};
         $gn = $name;
         if(length($gn) == 0){
             $gn = $name;
             }

         if($anno{$i} eq "t"){    
            draw_node($posx, $posy, $name, $gn, $black);
            }
         else{
            draw_node($posx, $posy, $name, $gn, $blue);
            }
 
         draw_edge($posx, $posy, $centerx, $centery); 
         }
   print "</map>";

   $imgname = $id . $num_method . ".png";

   open(DISPLAY,">./tmp/$imgname");
   flock(DISPLAY, LOCK_EX);
   binmode DISPLAY;

   print DISPLAY $im->png;

   close(DISPLAY);

   print "<IMG USEMAP=\"#img_map\" src=\"./tmp/$imgname\" border=0 ISMAP>";

   }

#print "<br>";


print "<br><br><font color=black>black dots:</font> time-shifted relationship<br>";
print "<font color=blue>blue dots:</font> inverted relationship<br>";

print "</body></html>";


sub draw_node
{
  my($xx, $yy, $nn, $gn, $cc) = @_;

  $im->arc($xx,$yy,12,12,0,360,$cc);
  $im->fill($xx, $yy, $cc);
  $im->string(gdMediumBoldFont,$xx-16,$yy+8,$gn, $black);
  $x1 = $xx-4;
  $y1 = $yy-4;
  $x2 = $xx+4;
  $y2 = $yy+4;
  print <<imgarea;
      <area shape="rect" coords="$x1, $y1, $x2, $y2" href="./search_worm.cgi?id=$nn">
imgarea


}

sub draw_edge
{
 my($x1, $y1, $x2, $y2) = @_;
 
 $im->line($x1, $y1, $x2, $y2, $black);
}

