#!/usr/bin/perl

use CGI qw(param);
use GD;

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

open(RAW_DATA, "./MIPS_level2.txt");
while($buffer = <RAW_DATA>){
  chomp($buffer);
  ($in1, $others) = split(/\t/, $buffer);
  $func{$in1} = $others;
  }

close(RAW_DATA);

open(RAW_DATA, "./Uetz_orf.txt");
while($buffer = <RAW_DATA>){
  ($in1, $in2, $others) = split(/\t/, $buffer);
  $pair = $in1 . ":" . $in2;
  $inte{$pair} = 1;
  }

close(RAW_DATA);

open(RAW_DATA, "./ORF-gene.txt");
while($buffer = <RAW_DATA>){
  chomp($buffer);
  ($orf, $genes) = split(/\t/, $buffer);
  ($g1, @others) = split(/ +/, $genes);
  $gene_ORF{$orf} = $g1;
  }

close(RAW_DATA);


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



for($num_method = 0; $num_method < 3; $num_method++){

$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);


if($num_method == 0){
   print "<b><FONT FACE=\"sans-serif,helvetica,arial\">Using method of local pattern</font></b><br>";
   open(RAW_DATA, "pseu_dp_all_g16.out");
   }
elsif($num_method == 2){
   print "<br><br><b><FONT FACE=\"sans-serif,helvetica,arial\">Using method of spectral analysis</font></b><br>";
   open(RAW_DATA, "fft_compare_g0.98.out");
   }
else{
   print "<br><br><b><FONT FACE=\"sans-serif,helvetica,arial\">Using method of simultaneous correlation</font></b><br>";
   open(RAW_DATA, "ttt_compare_g0.95.out");
   }

$ii = 0;
while($buffer= <RAW_DATA>){
   chomp($buffer);
   if($num_method == 0){
      ($num1, $num2, $gene1, $gene2, $score, $x, $y, $len) = split(/ +/, $buffer);
      }
   elsif($num_method == 2){
      ($num1, $num2, $gene1, $dum1, $gene2, $dum2, $score) = split(/ +/, $buffer);
      }
   else{
      ($num1, $num2, $gene1, $dum1, $gene2, $dum2, $score) = split(/ +/, $buffer);
      }

   if($gene1 eq $id){
       $level1{$ii} = $gene2;
       $fun1 = $func{$gene1};
       $fun2 = $func{$gene2};
       $found = 0;
       $found_inter = 0;
       if(length($fun1) !=0 &&
          length($fun2) !=0 &&
          $fun1 ne "99." &&
          $fun2 ne "99."){
             (@funs1) = split(/:/, $fun1);
             (@funs2) = split(/:/, $fun2);
             $l1 = @funs1;
             $l2 = @funs2;
             for($i=0; $i<$l1; $i++){
                 for($j=0; $j<$l2; $j++){
                   if($funs1[$i] eq $funs2[$j]){
                     $found = 1;
                     break;
                     }
                   }
                 }
             }
       if($found == 1){
          $func{$ii} = 1;
          }
       else{
          $func{$ii} = 0;
          } 

#######interaction test
       
       $pair = $gene1 . ":" . $gene2;  
       if($inte{$pair} == 1){
          $found_inter =1;
          }
       else{
         $pair = $gene2 . ":" . $gene1;
         if($inte{$pair} == 1){
            $found_inter =1;
            }
         }

       if($found_inter == 1){
         $interaction{$ii} =1; 
         }
       else{
         $interaction{$ii} =0;
         }



       $ii++;
       }
   if($gene2 eq $id){
       $level1{$ii} = $gene1;
       $fun1 = $func{$gene1};
       $fun2 = $func{$gene2};
       $found = 0;
       $found_inter = 0;
       if(length($fun1) !=0 &&
          length($fun2) !=0 &&
          $fun1 ne "99." &&
          $fun2 ne "99."){
             (@funs1) = split(/:/, $fun1);
             (@funs2) = split(/:/, $fun2);
             $l1 = @funs1;
             $l2 = @funs2;
             for($i=0; $i<$l1; $i++){
                 for($j=0; $j<$l2; $j++){
                   if($funs1[$i] eq $funs2[$j]){
                     $found = 1;
                     break;
                     }
                   }
                 }
             }
       if($found == 1){
          $func{$ii} = 1;
          }
       else{
          $func{$ii} = 0;
          }


       $pair = $gene1 . ":" . $gene2;  
       if($inte{$pair} == 1){
          $found_inter =1;
          }
       else{
         $pair = $gene2 . ":" . $gene1;
         if($inte{$pair} == 1){
            $found_inter =1;
            }
         }

       if($found_inter == 1){
         $interaction{$ii} =1;
         }
       else{
         $interaction{$ii} =0;
         }


       $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;

if($num_method == 0 && $num_level1 > 0){
  print "<map name=img_map>\n";
  }
elsif($num_method == 2 && $num_level1 > 0){
  print "<map name=img_map2>\n";
  }
elsif($num_method == 1 && $num_level1 > 0){
  print "<map name=img_map3>\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($interaction{$i} == 1 && $interaction{$i} == 1){
             draw_node($posx, $posy, $name, $gn, $greenblue);
             }
         if($func{$i} == 1){
             draw_node($posx, $posy, $name, $gn, $blue);
             }
         elsif($func{$i} == 1){
             draw_node($posx, $posy, $name, $gn, $green);
             }
         else{
             draw_node($posx, $posy, $name, $gn, $black);
             }
         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);

   if($num_method == 0){
       print "<IMG USEMAP=\"#img_map\" src=\"./tmp/$imgname\" border=0 ISMAP>";
       }
   elsif($num_method == 2){
       print "<IMG USEMAP=\"#img_map2\" src=\"./tmp/$imgname\" border=0 ISMAP>";
       }
   else{
       print "<IMG USEMAP=\"#img_map3\" src=\"./tmp/$imgname\" border=0 ISMAP>";
       }

   }

#print "<br>";

}


print "<br><br><font color=green>green dots:</font> interact with the central gene<br>";
print "<font color=blue>blue dots:</font> have the same function as the central gene<br>";
print "black dots: none of the above<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.cgi?id=$nn">
imgarea


}

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

