<?
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
include ("connection.php");
//print '<title>ANISEED - STEP-BLAST superpose</title>';
//print '<body bgcolor="#505F70" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">';
//include ("progressbar.php");
include ("frames_header.php");
set_time_limit(0);


//MOL1
$mol=$_GET["ref"];

print '<center>';
print "<br><br>Expression profile comparison between <u><strong>";
//Recherche du nom de la molecule
$R="select \"SYNO\" from \"SYNO\" where \"ORIGIN\" like '%JGI%' and \"GENE_ID\" in (
select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$mol' )";
$ST=pg_query($c,$R);
$ROW=pg_fetch_array($ST);
if (pg_num_rows($ST)>0) print $ROW["SYNO"];
else
{
$R="select \"SYNO\" from \"SYNO\" where \"ORIGIN\" not like '%JGI%' and \"GENE_ID\" in (
select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$mol' )";
$ST=pg_query($c,$R);
$ROW=pg_fetch_array($ST);
if (pg_num_rows($ST)>0) print $ROW["SYNO"];
else print $_GET["ref"];
}
///////////
print "</strong></u> <small><i>(left column)</i></small> and ";

//$handle=fopen("sb.txt","w");

function torevFate($fate)
{
if (trim($fate)=="C") return "CEN";
if (trim($fate)=="EN") return "endodermal strand";
if (trim($fate)=="EP") return "epidermis";
if (trim($fate)=="H") return "head endoderm";
if (trim($fate)=="ME") return "mesenchyme";
if (trim($fate)=="MU") return "muscle";
if (trim($fate)=="NE") return "neck";
if (trim($fate)=="NS") return "nervous system";
if (trim($fate)=="NP") return "neurohypophysis primordium";
if (trim($fate)=="NO") return "notochord";
if (trim($fate)=="P") return "palps";
if (trim($fate)=="S") return "sensory vesicle";
if (trim($fate)=="T") return "tail nerve cord";
if (trim($fate)=="TEN") return "TEN";
if (trim($fate)=="TLC") return "TLC";
if (trim($fate)=="TVC") return "TVC";
if (trim($fate)=="VI") return "visceral ganglion";
//return "V"; //On a une expression mais le FATE n'est pas defini
}


//$mol="ci0100151552";
//$mol="ci0100133709";

$molGLOB=$mol;

//Recuperation des destins present dans la base de donnees
$fate=(array) null;
$requete="SELECT \"KEYWORDS\" from \"FATE_KW\"";
$stmt=pg_query($c,$requete);
$k=0;
while ($row=pg_fetch_array($stmt))
{
//$fate[count($fate)]=$row["KEYWORDS"];
$fate[$row["KEYWORDS"]]=$k++;
}

//Recuperation des stades de developpement
$dev=(array) null;
$requete="SELECT \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' order by \"DEV_PERCENT\"";
$stmt=pg_query($c,$requete);
$k=0;
while ($row=pg_fetch_array($stmt))
{
//$dev[count($dev)]=$row["DEV_STAGE_ID"];
$dev[$row["DEV_STAGE_ID"]]=$k++;
}


//Initialisation de la table
foreach($fate as $key => $val)
{
foreach ($dev as $key2 => $val2)
	{
	$table[$key][$key2]="0";
	}
}



//Recuperation du profil d'expression
$prof=(array) null;
$requete="SELECT A.* from \"ISH_CLUSTERING\" A,\"DEV_STAGE\" B where A.\"MOL_ID\"=(
select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$mol'
) order by A.\"DEV_STAGE_ID\"";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$dev=$row["DEV_STAGE_ID"];
$spot=$row["SPOT"];

$tmp=explode("@",$spot);
//print $spot;
$bool=false;
for ($i=0;$i<count($tmp);$i++)
{
if (trim($tmp[$i])=="R") foreach ($table as $key => $val) $table[$key][$dev]="R";
else if (trim($tmp[$i])=="V") foreach ($table as $key => $val) $table[$key][$dev]="V";
else if (trim($tmp[$i])=="U") foreach ($table as $key => $val) $table[$key][$dev]="U";
else  {
	if (!$bool) {
	foreach ($table as $key => $val) $table[$key][$dev]="RR";
	$bool=true;
	}

	$table[torevFate(trim($tmp[$i]))][$dev]=trim($tmp[$i]);
}

}

}

/*
print "<table border=\"0\">";
print "<tr>";

foreach ($table as $key => $val)
{
print "<td><small><b>$key</b></small></td>";
//print "<small><b>$key</b></small>";
	foreach ($table[$key] as $key2 => $val2)
		{
		print "<td>";
		if ($table[$key][$key2]=="0") print '<img src="gray.gif">';
		else if ($table[$key][$key2]=="R") print '<img src="no.gif">';
		else if ($table[$key][$key2]=="RR") print '<img src="nerv.gif">';
		else if ($table[$key][$key2]=="U") print '<img src="ubi.gif">';
		else print '<img src="yes.gif">';
		print "</td>";
		}
//print "<br>";
print "</tr>";
}
print "</table>";

print "</center>";

/////////////////////////////////////////////////////////////////
print "<hr>";
//Initialize(0,0,200,30,'#000000','lightblue','red');
//ProgressBar(10);
return;
/////////////////////////////////////////////////////////////////
*/

$count++;

$mol=$_GET["cur"];

print " <strong><u>";
//Recherche du nom de la molecule
$R="select \"SYNO\" from \"SYNO\" where \"ORIGIN\" like '%JGI%' and \"GENE_ID\" in (
select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$mol' )";
$ST=pg_query($c,$R);
$ROW=pg_fetch_array($ST);
if (pg_num_rows($ST)>0) print $ROW["SYNO"];
else
{
$R="select \"SYNO\" from \"SYNO\" where \"ORIGIN\" not like '%JGI%' and \"GENE_ID\" in (
select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$mol' )";
$ST=pg_query($c,$R);
$ROW=pg_fetch_array($ST);
if (pg_num_rows($ST)>0) print $ROW["SYNO"];
else print $_GET["cur"];
}
///////////
print "</u></strong> <small><i>(right column)</i></small><br><br>";

//Recuperation des stades de developpement
$dev=(array) null;
$requete="SELECT \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' order by \"DEV_PERCENT\"";
$stmt=pg_query($c,$requete);
$k=0;
while ($row=pg_fetch_array($stmt))
{
$dev[$row["DEV_STAGE_ID"]]=$k++;
}

//Initialisation de la table
$table2=(array) null;
foreach($fate as $key => $val)
{
foreach ($dev as $key2 => $val2)
	{
	$table2[$key][$key2]="0";
	}
}


//Recuperation du profil d'expression
$prof=(array) null;
$requete="SELECT * from \"ISH_CLUSTERING\" where \"MOL_ID\"=(
select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$mol'
) order by \"DEV_STAGE_ID\"";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$dev=$row["DEV_STAGE_ID"];
$spot=$row["SPOT"];

$tmp=explode("@",$spot);
$bool=false;

for ($i=0;$i<count($tmp);$i++)
{
if (trim($tmp[$i])=="R") foreach ($table2 as $key => $val) $table2[$key][$dev]="R";
else if (trim($tmp[$i])=="V") foreach ($table2 as $key => $val) $table2[$key][$dev]="V";
else if (trim($tmp[$i])=="U") foreach ($table2 as $key => $val) $table2[$key][$dev]="U";
else  {
	if (!$bool) {
	foreach ($table2 as $key => $val) $table2[$key][$dev]="RR";
	$bool=true;
	}

	$table2[torevFate(trim($tmp[$i]))][$dev]=trim($tmp[$i]);
}
}

}

print '
<style type="text/css">

#cadreendo {
color: white;
background-color:black;
}

#cadremeso {
color: white;
background-color:green;
}
#cadreendo {
color: white;
background-color:blue;
}
#cadrecton {
color: white;
background-color:red;
}
#cadrectoe {
color: white;
background-color:gray;
}
#bord {
border:0px solid black;
background-color:white;
}
#bordcleav {
border:1px solid black;
background-color:lightyellow;
color: black;
}

#bordgast {
border:1px solid black;
background-color:lightgreen;
color: black;
}

#bordneur {
border:1px solid black;
background-color:lightblue;
color: black;
}

#bordtail {
border:1px solid black;
background-color:lightgray;
color: black;
}

#bordlarva{
border:1px solid black;
background-color:white;
color: black;
}

#bordadult {
border:1px solid black;
background-color:lightcoral;
color: black;
}



#bord2 {
border:2px solid black;
background-color:white;
}
#bordT {
border:1px solid black;
background-color:white;
}

</style>
';

print "<table id=\"bordT\">";
print "<tr>";
print "<td >&nbsp;</td>";
print "<td id=\"bordcleav\"><small><b>Egg</b></small></td>";
print "<td id=\"bordcleav\"><small><b>Zyg</b></small></td>";
print "<td id=\"bordcleav\"><small><b>2-c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>4-c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>8-c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>16c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>24c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>e32</b></small></td>";
print "<td id=\"bordcleav\"><small><b>l32</b></small></td>";
print "<td id=\"bordcleav\"><small><b>44c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>64c</b></small></td>";
print "<td id=\"bordcleav\"><small><b>76c</b></small></td>";
print "<td id=\"bordgast\"><small><b>eGa</b></small></td>";
print "<td id=\"bordgast\"><small><b>mGa</b></small></td>";
print "<td id=\"bordgast\"><small><b>lGa</b></small></td>";
print "<td id=\"bordneur\"><small><b>eNe</b></small></td>";
print "<td id=\"bordneur\"><small><b>mNe</b></small></td>";
print "<td id=\"bordneur\"><small><b>lNe</b></small></td>";
print "<td id=\"bordtail\"><small><b>iTb</b></small></td>";
print "<td id=\"bordtail\"><small><b>eTb</b></small></td>";
print "<td id=\"bordtail\"><small><b>mTb</b></small></td>";
print "<td id=\"bordtail\"><small><b>lTb</b></small></td>";
print "<td id=\"bordlarva\"><small><b>Tad</b></small></td>";
print "<td id=\"bordadult\"><small><b>Y.A</b></small></td>";

print "</tr>";
print "<tr>";

foreach ($table as $key => $val)
{
if ($key=="endodermal strand") print "<td><small><b><div id=\"cadreendo\">$key</div></b></small></td>";
if ($key=="head endoderm") print "<td><small><b><div id=\"cadreendo\">$key</div></b></small></td>";
if ($key=="mesenchyme") print "<td><small><b><div id=\"cadremeso\">$key</div></b></small></td>";
if ($key=="muscle") print "<td><small><b><div id=\"cadremeso\">$key</div></b></small></td>";
if ($key=="notochord") print "<td><small><b><div id=\"cadremeso\">$key</div></b></small></td>";
if ($key=="TLC") print "<td><small><b><div id=\"cadremeso\">$key</div></b></small></td>";
if ($key=="TVC") print "<td><small><b><div id=\"cadremeso\">$key</div></b></small></td>";
if ($key=="CEN") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="neck") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="nervous system") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="neurohypophysis primordium") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="palps") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="sensory vesicle") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="tail nerve cord") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="TEN") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="visceral ganglion") print "<td><small><b><div id=\"cadrecton\">$key</div></b></small></td>";
if ($key=="epidermis") print "<td><small><b><div id=\"cadrectoe\">$key</div></b></small></td>";
//print "<td><small><b><div id=\"cadremeso\">$key</div></b></small></td>";

	foreach ($table[$key] as $key2 => $val2)
		{
		if ($table[$key][$key2]==$table2[$key][$key2]) print "<td id=\"bord\">";
		else if ($table[$key][$key2]=="0") print "<td id=\"bord\">";
		else if ($table2[$key][$key2]=="0") print "<td id=\"bord\">";
		else print "<td id=\"bord2\">";
		if ($table[$key][$key2]=="0") print '<img title="No data" src="pictures/gray.gif">';
		else if ($table[$key][$key2]=="R") print '<img title="No expression" src="pictures/no.gif">';
		else if ($table[$key][$key2]=="RR") print '<img title="No expression in this territory" src="pictures/no.gif">';
		else if ($table[$key][$key2]=="U") print '<img title="Expressed in whole embryo" src="pictures/yes.gif">';
		else print '<img title="Expressed in this territory" src="pictures/yes.gif">';

		/*if (($table[$key][$key2]=="0")&&($table2[$key][$key2]=="0")) print '<img src="gray.gif">';
		else if ($table[$key][$key2]==$table2[$key][$key2]) print '<img src="endo.gif">';
		else print '<img src="gray.gif">';*/
		if ($table2[$key][$key2]=="0") print '<img title="No data" src="pictures/gray.gif">';
		else if ($table2[$key][$key2]=="R") print '<img title="No expression" src="pictures/no.gif">';
		else if ($table2[$key][$key2]=="RR") print '<img title="No expression in this territory" src="pictures/no.gif">';
		else if ($table2[$key][$key2]=="U") print '<img title="Expressed in whole embryo" src="pictures/yes.gif">';
		else print '<img title="Expressed in this territory" src="pictures/yes.gif">';
		
		print "</td>";
		}
print "</tr>";
}
print '</table>';
print '</center>';


include ("frames_footer.php");
?>