<?
/*
    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");
//ci0100135421:ci0100133709:ci0100138251:ci0100151960:ci0100151353:ci0100131418:ci0100131500:ci0100148766:

/////VARIABLE GLOBALE POUR LE STOCKAGE DU LINEAGE
$table=(array) null;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function select_recursif_descendant($conn,$start,$res)
{

global $table;
if ($start!=''){
$requete="SELECT * FROM \"LINEAGE\" WHERE \"DAUGHTER_CELL\"=$start";
}
else return;

$stmt=pg_query($conn,$requete);
while ($row=pg_fetch_array($stmt))
	{
	//$IN2.=$row["DAUGHTER_CELL"].",";
	//$IN2.="(".$requeteIN2.") UNION ";
	$requete0="
	select \"DEV_PERCENT\" from \"DEV_STAGE\" where \"DEV_STAGE_ID\"=(
	SELECT \"DEV_STAGE_ID\" from \"ANAT_ENTITY\" where \"SPECIES\"='Ciona intestinalis' and \"ANAT_ID\"='".$row["DAUGHTER_CELL"]."')";
	$stmt0=pg_query($conn,$requete0);
	$row0=pg_fetch_array($stmt0);
	$table[$row["DAUGHTER_CELL"]]=$row0["DEV_PERCENT"];
	$res=select_recursif_descendant($conn,"'".$row["MOTHER_CELL"]."'",$res);
	}
return $res;
}

function select_recursif_ascendant($conn,$start,$res)
{
global $table;

if ($start!=''){
$requete="SELECT * FROM \"LINEAGE\" WHERE \"MOTHER_CELL\"=$start";
}
else return;

$stmt=pg_query($conn,$requete);
while ($row=pg_fetch_array($stmt))
	{
	//$IN2.=$row["DAUGHTER_CELL"].",";
	//$IN2.="(".$requeteIN2.") UNION ";
	$requete0="
	select \"DEV_PERCENT\" from \"DEV_STAGE\" where \"DEV_STAGE_ID\"=(
	SELECT \"DEV_STAGE_ID\" from \"ANAT_ENTITY\" where \"SPECIES\"='Ciona intestinalis' and \"ANAT_ID\"='".$row["MOTHER_CELL"]."')";
	$stmt0=pg_query($conn,$requete0);
	$row0=pg_fetch_array($stmt0);
	$table[$row["MOTHER_CELL"]]=$row0["DEV_PERCENT"];
	$res=select_recursif_ascendant($conn,"'".$row["DAUGHTER_CELL"]."'",$res);
	}
return $res;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




function stepblast_lineage($c,$anatname,$anatdevdepart,$devmin,$devmax,$species)
{

global $table;

$requete="SELECT \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='$anatname' and \"DEV_STAGE_ID\"=(select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='$anatdevdepart' and \"SPECIES\"='$species') and \"SPECIES\"='$species'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$anatid=$row["ANAT_ID"];

if (strlen($anatid)==0) {print "Error $anatname isn't exist at $anatdevdepart ! "; return;}
// print $anatid."<br><br>";


//// On regarde tout les target qui sont exprimé dans la cellule de depart
$target=(array) null;
$requete="SELECT distinct \"TARGET_ID\",\"IN_SITU_ID\" from \"TARGET\" where \"REGION_ID\"='$anatid' and \"IN_SITU_ID\" in (
select \"IN_SITU_ID\" from \"IN_SITU\" where \"PRIVATE\"='n' and \"CHECKED\"='y' and \"WILD_TYPE\"='y'
) and \"TARGET_ID\" in (select \"MOL_ID\" from \"MOL_ENTITY\" where \"TYPE\"<>'construction')";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
//print $row["TARGET_ID"]."<br>";
$target[$row["TARGET_ID"]]++;

}
  //print_r($target);
//  print "<br><br>";


///// On s'occupe du lignage
//$table=(array) null;

$res=select_recursif_descendant($c,$anatid,$res);
$res=select_recursif_ascendant($c,$anatid,$res);

//print_r($table);


////////// On fait le tri sur les stage de devel et sur les anat
$requete="SELECT \"DEV_STAGE_ID\",\"DEV_PERCENT\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' and \"NAME\"='$devmin'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$devidmin=$row["DEV_STAGE_ID"];
$devpermin=$row["DEV_PERCENT"];

$requete="SELECT \"DEV_STAGE_ID\",\"DEV_PERCENT\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' and \"NAME\"='$devmax'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$devidmax=$row["DEV_STAGE_ID"];
$devpermax=$row["DEV_PERCENT"];

asort($table);

///////// On elimine les ID qui ne sont pas dans la fenetre devpermin devpermax
$newtable=(array) null;
$tableIN="(";

foreach ($table as $key => $val)
{
if ($val<$devpermin) continue;
if ($val>$devpermax) continue;
$newtable[$key]=$val;

$tableIN.="'$key',";

}
$tableIN.=")";

$tableIN=str_replace(",)",")",$tableIN);

//  print "<br><bR>";
//  print_r($newtable);
//  print "<br><bR>";
//  print $tableIN;
//  print "<br><bR>";


$table=$newtable;

// print_r($table);
// print "<br><br>";

////////////////// on merge les deux donnees

$merge=(array) null;
$nametodev=(array) null;

foreach ($target as $key => $val)
{

$requete="
select anat.\"NAME\" as \"ANAT_NAME\",dev.\"DEV_PERCENT\" from \"ANAT_ENTITY\" anat,\"DEV_STAGE\" dev where anat.\"ANAT_ID\" in (
SELECT distinct \"REGION_ID\" from \"TARGET\" where \"TARGET_ID\"='$key' and \"REGION_ID\" in $tableIN
) and anat.\"DEV_STAGE_ID\"=dev.\"DEV_STAGE_ID\"";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{

$tmp="";

$requete0="SELECT \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\"=(select \"ANISEED_CLUSTER_ID\" from \"CLONE_SEQ\" where \"CLONE_SEQ_ID\"='$key')";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
// print $row0["NAME"]."\t";
if (strlen($row0["NAME"])>0) {$tmp="1";$merge[$row0["NAME"]].=$row["ANAT_NAME"]."@";$nametodev[$row0["NAME"]].=$row["DEV_PERCENT"]."@";}




$requete0="SELECT \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\" in (select \"ANISEED_CLUSTER_ID\" from \"CLONE_SEQ\" where \"CLONE_NAME\"=(select distinct \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\"='$key'))";
//print $requete0;
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
// print $row0["NAME"]."\t";
if (strlen($row0["NAME"])>0) {$tmp="1";$merge[$row0["NAME"]].=$row["ANAT_NAME"]."@";$nametodev[$row0["NAME"]].=$row["DEV_PERCENT"]."@";}


$requete0="SELECT \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\"='$key' and \"TYPE\"='gene'";

$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
//  print $row0["NAME"]."<br>";
if (strlen($row0["NAME"])>0) {$tmp="1";$merge[$row0["NAME"]].=$row["ANAT_NAME"]."@";$nametodev[$row0["NAME"]].=$row["DEV_PERCENT"]."@";}



}
}

//  print_r($merge);

// print "<br><br>";
// print_r($nametodev);


$devper=(array) null;

$requete="SELECT \"DEV_PERCENT\" from \"DEV_STAGE\" where \"DEV_PERCENT\" between '$devpermin' and '$devpermax'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt)) $devper[count($devper)]=$row["DEV_PERCENT"];


$final=(array) null;
foreach ($merge as $key => $val)
{

$exp=$nametodev[$key];


$tmp=explode("@",$exp);

$tmpprof=(array) null;
for ($i=0;$i<count($devper);$i++)
	{
	if (in_array($devper[$i],$tmp)) $tmpprof[$devper[$i]]="1";else $tmpprof[$devper[$i]]="0";
	}

$final[$key]=implode("",$tmpprof);
}

// print "<hr>";
// print_r($final);


////TEST/////////
arsort($final);
/////////////////


///////// final contient pour chaque gene le profil d'expression dans le lignage de la cellule choisis lignage ascendant 
print "<br>";

print "<font color=\"#D64242\" size=\"2\"><b>Gene expressed in $anatname line from $devmin to $devmax</b></font><br><br>";

print "<center>";

print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";

print "<tr>";

print "<td style=\"border-right:1px solid black;padding:10px;\">&nbsp;</td>";

$requete="SELECT \"NAME\" from \"DEV_STAGE\" where \"SPECIES\"='$species' and \"DEV_PERCENT\" between '$devpermin' and '$devpermax' order by \"DEV_PERCENT\"";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
if ($row["NAME"]==$anatdevdepart) print "<td style=\"border-right:1px solid black;padding:0px;\" align=\"center\"><font size=\"-3\" color=\"#D64242\"><i>".$row["NAME"]."</i></font></td>";
else print "<td style=\"border-right:1px solid black;padding:5px;\" align=\"center\"><font size=\"-3\"><i>".$row["NAME"]."</i></font></td>";
}

print "</tr>";



$kusa=(array) null;
/*$kusa[0]="ci0100152409";
$kusa[1]="ci0100137826";
$kusa[2]="ci0100134481";
$kusa[3]="ci0100154271";
$kusa[4]="ci0100138672";
$kusa[5]="ci0100146672";
$kusa[6]="ci0100144155";
$kusa[7]="ci0100141671";
$kusa[8]="ci0100140030";
$kusa[9]="ci0100146731";
$kusa[10]="ci0100146149";
$kusa[11]="ci0100146710";
$kusa[12]="ci0100146760";
$kusa[13]="ci0100130424";
$kusa[14]="ci0100135440";
$kusa[15]="ci0100143409";
$kusa[16]="ci0100143436";
$kusa[17]="ci0100146270";
$kusa[18]="ci0100146526";
$kusa[19]="ci0100143639";
$kusa[20]="ci0100138062";*/


foreach ($final as $key => $val)
{
//if (!in_array($key,$kusa)) continue;
print "<tr>";
//print $key." => ";

$requete="SELECT \"SYNO\" from \"SYNO\" where \"GENE_ID\"=(select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$key') and \"SYNO\" not like 'ci01001%'";
$stmt=pg_query($c,$requete);

if (pg_num_rows($stmt)==0) print "<td style=\"border-right:1px solid black;padding:0px;\"><font size=\"-3\"><a href=\"molecule-gene.php?name=$key\">$key</a></font>&nbsp;</td>";
else
{
print "<td style=\"border-right:1px solid black;padding:0px;\"><font size=\"-3\">";
if ($row=pg_fetch_array($stmt)) print "<a href=\"molecule-gene.php?name=$key\">".$row["SYNO"]."</a>"; // a remplacer par un while si on les veux tous
print "</font>&nbsp;</td>";
}


for ($i=0;$i<strlen($val);$i++)
	{
	$value=$val{$i};
	if ($value=="0") print "<td  style=\"border-right:1px solid black;padding:0px;\" align=\"center\"><img src=\"no2.gif\"></td>";
	if ($value=="1") print "<td  style=\"border-right:1px solid black;padding:0px;\" align=\"center\"><img src=\"yes2.gif\"></td>";
	}

print "</tr>";
}

print "</table>";

print "</center>";

return $final;
}
//////////////

function mergeme($big,$final)
{
foreach($final as $key => $val)
{
if (strlen($big[$key])==0) $big[$key]=$val;
else $big[$key].="@$val";
}
return $big;
}


include ("frames_header.php");


$big=(array) null;

$anat=$_GET["anat"];
$devanat=$_GET["devanat"];

$devmin=$_GET["devfrom"];
$devmax=$_GET["devto"];


//function stepblast_lineage($c,$anatname,$anatdevdepart,$devmin,$devmax,$species)


$anatname="B6.3 cell pair";
$anatdevdepart="Late 32-cell stage";
$devmin="24-cell stage";
$devmax="Mid gastrula";
$species="Ciona intestinalis";



$final=stepblast_lineage($c,$anat,$devanat,$devmin,$devmax,"Ciona intestinalis");
$big=mergeme($big,$final);


include ("frames_footer.php");


/*
$final=stepblast_lineage($c,"a6.5 cell pair","Late 32-cell stage","16-cell stage","Mid gastrula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B6.2 cell pair","Late 32-cell stage","16-cell stage","Mid gastrula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B6.4 cell pair","Late 32-cell stage","16-cell stage","Mid gastrula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"b6.5 cell pair","Late 32-cell stage","16-cell stage","Mid gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
*/


/*
$final=stepblast_lineage($c,"secondary muscle lineage (posterior)","Early tailbud","Early tailbud","Tadpole","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"primary muscle lineage","Mid tailbud","Early tailbud","Tadpole","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"secondary muscle lineage","Mid tailbud","Early tailbud","Tadpole","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"primary muscle lineage","Late tailbud","Early tailbud","Tadpole","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"secondary muscle lineage","Late tailbud","Early tailbud","Tadpole","Ciona intestinalis");
$big=mergeme($big,$final);
*/


/*
$final=stepblast_lineage($c,"B8.7 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"B8.8 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"B8.15 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"B8.16 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"A8.16 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"B7.5 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);
$final=stepblast_lineage($c,"b8.17 cell pair","110-cell stage","16-cell stage","Late gastrula","Ciona intestinalis");
$big=mergeme($big,$final);*/

/*
$final=stepblast_lineage($c,"A7.3 cell pair","64-cell stage","64-cell stage","110-cell stage","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A7.7 cell pair","64-cell stage","64-cell stage","110-cell stage","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A8.13 cell pair","110-cell stage","64-cell stage","110-cell stage","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A8.14 cell pair","110-cell stage","64-cell stage","110-cell stage","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A8.5 cell pair","110-cell stage","64-cell stage","110-cell stage","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A8.6 cell pair","110-cell stage","64-cell stage","110-cell stage","Ciona intestinalis");
$big=mergeme($big,$final);
*/

/*
$final=stepblast_lineage($c,"A7.2 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A7.5 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"A7.6 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B7.1 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B7.2 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B7.5 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B7.7 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);

$final=stepblast_lineage($c,"B8.5 cell pair","110-cell stage","64-cell stage","Late neurula","Ciona intestinalis");
$big=mergeme($big,$final);
*/


/*
foreach ($big as $key => $val)
{
$requete="SELECT \"SYNO\" from \"SYNO\" where \"GENE_ID\"=(select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$key') and \"SYNO\"  like 'ci01001%'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if (strlen($row["SYNO"])==0) print $key." => $val<br>";
else print $row["SYNO"]." => $val<br>";
}

*/

















?>