#!/usr/local/bin/perl

#sub to print gene intervals according to data provided
#by the geneloc project


use CGI qw(:standard);;

#require "sub_html.pl";  #needed for sub print_copyright_notice
require "../bin/geneloc/variables";
require "sub_print_html_top.pl";
my $query = new CGI;

#set some constants

my $gene_densities_dir = "data/densities";

$titles_font_color = "\"#990033\"";

my $interval = 1;  #in Megabases

#start HTML page

print header;
#print start_html(-title  =>  'GeneLoc Integrated Map',
#                 -bgcolor => 'white');
#print_copyright_notice ("html");
#print html top
print "<html><head><title>GeneLoc Integrated Map</title>";
print_html_top();

#print header

#my $header = 
#"
#<TABLE>
#<TR>
#<TD>
#<IMG SRC=\"http://$URL_STATIC_HTML_PAGES/$CURRENT/graphics/logo.jpg\" width=\"285\"alt=\"GeneLoc\">
#</TD>
#<TD ALIGN=\"CENTER\">
#<H2><FONT COLOR=\"GREEN\">GeneLoc</h2><h3>Formerly known as The Unified Database for Human Genome Mapping (UDB)</FONT></H3>
#<a href=\"http://bip.weizmann.ac.il\">Bioinformatics and Biological Computing</a>
#&
#<a href=\"http://bioinfo.weizmann.ac.il/genome_center\">Crown Human Genome Center</a>

#<BR>
#<a href=\"http://www.weizmann.ac.il\">Weizmann Institute of Science</a>
#</TD>
#</TR></TABLE>
#<HR>
#";

#print $header, p;

#print form

unless ($query->param()) {

   #print title
   
   my $title = "<H2><FONT COLOR=$titles_font_color>Show Gene Densities</FONT></H2>\n";
   print $title;

   print $query->startform,
      
   "&nbsp;&nbsp;Choose Chromosome: ",
   $query->popup_menu(-name=>'chr',
					  -values=>[1 .. 22, 'X', 'Y'],
					  -default=>'1'
							   ),
   "&nbsp", "\n",
   
   $query->submit(-value=>'Submit'),
   $query->endform;
   
}

#print results

if ($query->param('chr')) {

   my $chr = uc $query->param('chr');

   #print title
   
   my $title = "<H2><FONT COLOR=$titles_font_color>&nbsp;&nbsp;Gene Densities in Chromosome $chr</FONT></H2>\n";
   print $title;
   
   #print explanation
   
   my $text = 
   "&nbsp;&nbsp;&nbsp;The following table shows the number of genes in each $interval Mb interval along the chromosome.
   For this calculation, the midpoint location of each gene was taken.<br />\n";
   
#   print $text;
   
   #print table
   
   my $total_nr_genes = 0;
   
   my $source_file = $gene_densities_dir . "/$chr";
   
   open (SOURCE, $source_file) or cgi_error ("Canot open \"$source_file\": $!");

   $text .= "<TABLE BORDER CELLPADDING=3 CELLSPACING=3 style='margin-left:15px;'>\n";
   
   $text .= "<TR><TH>Interval (Mb)</TH><TH>No. of genes</TH></TR>\n";

   my ($pos, $nr_genes, $range);
   while (<SOURCE>) {
      chomp;
      ($pos, $nr_genes) = split (/\t/);
      $range = $pos - $interval . "-" . $pos;
      $text .= "<TR><TD>$range</TD><TD>$nr_genes</TD></TR>\n";
      $total_nr_genes += $nr_genes;
   }
   
   $text .= "</TABLE>\n";

   $text .= 
   "&nbsp;&nbsp;&nbsp;Total Number of genes in chromosome $chr: $total_nr_genes.<br /><br />\n";
   $text = "&nbsp;&nbsp;&nbsp;Total Number of genes in chromosome $chr: $total_nr_genes.<br /><br />\n" . $text;
   print $text;

   close SOURCE;

}

#end HTML page
print "&nbsp;&nbsp;";
print "<a href=\"$URL_STATIC_HTML_PAGES/$CURRENT/\">GeneLoc</a>&nbsp;&nbsp;\n",
      "<a href=\"$URL_GENECARDS\">GeneCards</a>\n";

#print end_html;
print_footer();
#####################################

sub cgi_error {

  my($string) = @_;
  print "\n<P><B><FONT SIZE =+1 COLOR=RED>Error:</FONT></B> $string<P>\n";
  
}

