#!/usr/local/bin/perl

#exon_struct.pl  
######################

require "../bin/geneloc/variables";
require "sub_print_html_top.pl";
require "../bin/geneloc/sub_db.pl";
use DBI;
use DBD::mysql;
use CGI qw(:standard);
use GL;
$| = 1;  #show results on the Web output as they come out
my $db_name = "geneloc_" . $DATA_VERSION;
my $dbh = SET_DBH($db_name, $PWORD);
#$test_db = "DBI:mysql:$db_name";
#my $dbh =  DBI->connect ($test_db, "swidler", undef) || die "can't connect:DBI::errstr";
#colors in display

$page_bg_color =           "\"#FFFFFF\"";         #bg means background
$table_headings_bg_color = "\"#BEDBF7\"";
$titles_font_color =       "\"black\"";
$labels_font_color =       "\"#117C7C\"";
$yellow_font_color =        "\"orange\"";

$this_script = "exon_struct.pl";
$this_url = "$URL_CGI_SCRIPTS/$CURRENT/$this_script";
$homepage_url = "$URL_STATIC_HTML_PAGES/$CURRENT/";
my $query = new CGI;
my @names = $query->param;
foreach $name (@names) {
    $params{$name} = param("$name");
}

my ($disp_name, $select, $ll_id, $ens_id, $gene_id, @rows, $row, %genes, @ll, @ens, $chrom, $contig, $strand, $start, $end, $ll_exons, $ens_exons, $chr_nr, @starts, @ends, @chroms, @contigs, @strands, @ids, $id);

$disp_name = $params{"disp_name"};
$chr_nr = $params{"chr_nr"};
$select = "select distinct ll_id, ens_id from geneloc where disp_name = \"$disp_name\"";

@rows = parse ($select, $dbh);
foreach $row (@rows) {
    ($ll_id, $ens_id) = split (/;/, $row);
    unless ($genes{$ll_id}) {
	push (@ll, $ll_id);
	$genes{$ll_id} = 1;
    }
    unless ($genes{$ens_id}) {
	push (@ens, $ens_id);
	$genes{$ens_id} = 1;
    }
}

foreach $ll_id (@ll) {
    $select = "select distinct chrom, contig, strand, start, end from exon_map where gene_id = \"$ll_id\" and contig = \"\" order by start";
    @rows = parse ($select, $dbh);
    foreach $row (@rows) {
	($chrom, $contig, $strand, $start, $end) = split (/;/, $row);
	push (@{$ll_exons -> {$ll_id} -> {"chrom"}}, $chrom);
#	push (@{$ll_exons -> {$ll_id} -> {"contig"}}, $contig);
	push (@{$ll_exons -> {$ll_id} -> {"strand"}}, $strand);
	push (@{$ll_exons -> {$ll_id} -> {"start"}}, $start);
	push (@{$ll_exons -> {$ll_id} -> {"end"}}, $end);
    }
}
foreach $ens_id (@ens) {
    $select = "select distinct chrom, contig, strand, start, end, id from exon_map where gene_id = \"$ens_id\" order by start";
    @rows = parse ($select, $dbh);
    foreach $row (@rows) {
	($chrom, $contig, $strand, $start, $end, $id) = split (/;/, $row);
	push (@{$ens_exons -> {$ens_id} -> {"chrom"}}, $chrom);
#	push (@{$ens_exons -> {$ens_id} -> {"contig"}}, $contig);
	push (@{$ens_exons -> {$ens_id} -> {"strand"}}, $strand);
	push (@{$ens_exons -> {$ens_id} -> {"start"}}, $start);
	push (@{$ens_exons -> {$ens_id} -> {"end"}}, $end);
	push (@{$ens_exons -> {$ens_id} -> {"exon_id"}}, $id);
    }
}

print "Content-type: text/html\n\n";
print "<HEAD><TITLE>GeneLoc Integrated Map for Chromosome $chr_nr: Exon structure for $disp_name</TITLE>\n";
#print html top

print_html_top();
print "<TABLE>  <!-- <TR><TD>
<a href=\"$homepage_url\">
<IMG SRC=\"$URL_STATIC_HTML_PAGES/$CURRENT/graphics/logo.jpg\" width=\"285\" alt=\"GeneLoc\"></a></TD></tr>-->
<tr><td><H2>&nbsp;&nbsp;Exon Structure for <A HREF=\"$URL_CARDDISP$disp_name\">$disp_name</a></h2></td></TR></TABLE>";
#print "<H2>Exon Structure for <A HREF=\"http://bioinformatics.weizmann.ac.il/cards-bin/carddisp?$disp_name\">$disp_name</a></h2><br>\n";
#print "[ <a href=\"$homepage_url\"> GeneLoc Home Page</a> ]\n";
unless ($ll[0] eq "" || !@ll){
    print "<h3>&nbsp;&nbsp;&nbsp;From <a href=\"$URL_ENTREZGENE$ll[0]\">Entrez Gene</a>:</h3>\n";
    print "<table border=1 style='margin-left:15px;'><tr BGCOLOR=$table_headings_bg_color><th><font color=$titles_font_color>Id</font></th><th><font color=$titles_font_color>Chromosome</font></th><th><font color=$titles_font_color>Strand</font></th><th><font color=$titles_font_color>Exon Start</font></th><th><font color=$titles_font_color>Exon End</font></th></tr>\n";
    my $index = 0;
    foreach (@ll) {
	@chrom = @{$ll_exons -> {$_} -> {"chrom"}};
#	@contig = @{$ll_exons -> {$_} -> {"contig"}};
	@strand = @{$ll_exons -> {$_} -> {"strand"}};
	@starts = @{$ll_exons -> {$_} -> {"start"}};
	@ends = @{$ll_exons -> {$_} -> {"end"}};
	my $num = $#starts;
	for ($index = 0; $index <= $num; $index++) {
	    unless ($contig[$index]) {
		$contig[$index] = "<br>";
	    }
	    print "<tr><td>$_</td><td>$chrom[$index]</td><td>$strand[$index]</td><td>$starts[$index]</td><td>$ends[$index]</td></tr>\n";
	}
    }
    print "</table><br>\n";
}

unless ($ens[0] eq "" || !@ens) {
    print "<h3>&nbsp;&nbsp;&nbsp;From <a href=\"$URL_ENSEMBL$ens[0]\">Ensembl</a>:</h3>\n";
    print "<table border=1 style='margin-left:15px;'><tr BGCOLOR=$table_headings_bg_color><th><font color=$titles_font_color>Gene Id</font></th><th><font color=$titles_font_color>Exon Id</font></th><th><font color=$titles_font_color>Chromosome</font></th><th><font color=$titles_font_color>Strand</font></th><th><font color=$titles_font_color>Exon Start</font></th><th><font color=$titles_font_color>Exon End</font></th></tr>\n";
    my $index = 0;
    foreach (@ens) {
	@chrom = @{$ens_exons -> {$_} -> {"chrom"}};
#	@contig = @{$ens_exons -> {$_} -> {"contig"}};
	@strand = @{$ens_exons -> {$_} -> {"strand"}};
	@starts = @{$ens_exons -> {$_} -> {"start"}};
	@ends = @{$ens_exons -> {$_} -> {"end"}};
	@ids = @{$ens_exons -> {$_} -> {"exon_id"}};
	my $num = $#starts;
	for ($index = 0; $index <= $num; $index++) {
	    unless ($contig[$index]) {
		$contig[$index] = "<br>";
	    }
	    print "<tr><td>$_</td><td>$ids[$index]</td><td>$chrom[$index]</td><td>$strand[$index]</td><td>$starts[$index]</td><td>$ends[$index]</td></tr>\n";
	}
    }
    print "</table><br>\n";
}
print_footer();
