<!-- ANATOMY Module
 display lineage of an anatomical territory
 -->
<html>
<head>
<title>ANISEED - Lineage (Ciona intestinalis, Halocynthia roretzi)</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="This page shows Ciona intestinalis lineage information corresponding to the cell you have clicked. For more information, contact aniseed@ibdm.univ-mrs.fr.">
<meta name="keywords" content="Lineage information, lineage data, cells, Ciona intestinalis, Halocynthia roretzi, ascidian database, ciona intestinalis database.">
<meta name = "robots" content = "index, follow, all">
<META HTTP-EQUIV="Content-Language" CONTENT="EN">
<style type="text/css">
</style>
</head>

<?php
/*
    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");
    include("frames_header.php");
?>



<?php
    $id=$_GET["idanat"];

	$requete="SELECT \"DEV_STAGE_ID\",\"SPECIES\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='$id'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);

	$dev=$row["DEV_STAGE_ID"];
	$species=$row["SPECIES"];


?>

  <link rel="stylesheet" type="text/css" media="screen" href="ongletv1.css" title="Version 1" />
<!-- Header pour l'onglet -->
<div id="header">
  <ul>
    <li><a href="anatomy.php?species=<?print $species;?>&dev=<?print $dev;?>&region=<?print $id?>">Anatomical Ontology</a></li>
    <li><a href="short_hierarchy.php?region=<?print $id;?>&dev=<?print $dev;?>" href="">Anatomical Territory Summary Card</a></li>
<li id="current" ><a href="#">Lineage</a></li>    
<li><a onclick="this.href='insitu-result.php?anat_menu=1&BOOLanat=1&only=1&BOOLmut=0&BOOLmanip=2&idanat=<?print $id;?>'" href="">Molecular Markers</a></li> 
 				<li>
					<a onclick="this.href='anatomy-regulation-card.php?idanat=<?print $id;?>'" href="#">
						Regulatory Network
					</a>
				</li>
				  <!--<li><a href="fates.php?idanat=<?print $id;?>">Fate</a></li>-->

    
    <!--<li><a onclick="this.href='insitu-result.php?BOOLanat=1&xref=2&idanat=<?print $id;?>:'" href="#">InSitu data</a></li>-->

    <!--<li><a href="position.php?idanat=<?print $id;?>">Biometry</a></li>-->
    <!--<li><a href="induction.php?idanat=<?print $id;?>">Induction</a></li>-->
    <!--<li><a href="#">InSitu data</a></li>

    <li><a href="#">Induction</a></li>-->

  </ul>
</div>
<!-- EOF Onglet -->
<br><br>



<?
	
	if(! is_numeric($id)){
	    Print "Error: missing parameter";
		include("frames_footer.php");
		return;
	}
	
    $dev="";
	$lastdev="";
	print "</center>";
	
	$requete="select B.\"DEV_PERCENT\", B.\"NAME\" as \"DEV\", A.\"NAME\", A.\"DEV_STAGE_ID\" from \"ANAT_ENTITY\" A, \"DEV_STAGE\" B where A.\"ANAT_ID\"='$id' and A.\"DEV_STAGE_ID\"=B.\"DEV_STAGE_ID\"";
    $stmt = pg_query($c,$requete);
	if ($row=pg_fetch_array($stmt)){
	    $dev=$row["DEV_STAGE_ID"];
	    print "<br><br><font color=\"#D64242\" size=\"3\"><b>".$row["NAME"]." &nbsp;(".$row["DEV"].") lineage</b></font><br><br>";
	}

//Si on a moins de 8-Cell stage, on prend pas
$requete0="SELECT \"DEV_PERCENT\" from \"DEV_STAGE\" where \"SPECIES\"='$species' and \"NAME\"='8-cell stage'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
$seuil=$row0["DEV_PERCENT"];

if ($row["DEV_PERCENT"]>$seuil)
{
print "<a href=lineageTree.php?idanat=$id>";
print "<img src=\"pictures/button-graph.jpg\" border=\"0\">";
print "</a>";
print '<br><br>';
}	
else
{
print "<h1><em><font size=-1>Note: Graphical view is not available for this development stage.</font></em></h1><br>";
}


	


	$count=0;
	$doublons=(array) null;//when a structure has several parents -> avoid doublons
	//search for cell precursors



$term=(array) null;
$term2=(array) null;

function select_recursif_descendant_2($conn,$start,$res,$nivo,$doubl)
{
global $doublons;
global $lastdev;
global $count;
global $dev;
global $term;
global $term2;

/*ON ECRIT SA REQUETE*/

if ($start!='')
{
$requete="SELECT * FROM \"LINEAGE\" WHERE  \"DAUGHTER_CELL\"= $start ";

}
else return;

$stmt=pg_query($conn,$requete);

//Rajout
if (pg_num_rows($stmt)!=0) $nivo++;//print "Nz";
if (pg_num_rows($stmt)==0) $nivo--;//print "Ze";



while ($row=pg_fetch_array($stmt))
	{
	
	$res=select_recursif_descendant_2($conn,$row["MOTHER_CELL"],$res,$nivo,$doubl);
	

	if ($doubl=='yes')
	{

	    if(! in_array($row["DAUGHTER_CELL"],$doublons))
	    	{
		$doublons[count($doublons)]=$row["DAUGHTER_CELL"];
		
		//Rjout
		$mothercell=$row["MOTHER_CELL"];
		//print $mothercell."<br>";
		$requete3= "SELECT a.\"NAME\" FROM \"ANAT_ENTITY\" a where a.\"ANAT_ID\"='$mothercell' ";
		$stmt3 = pg_query($conn,$requete3);
		if ($row3=pg_fetch_array($stmt3)) {
		    $mothername=$row3["NAME"];
			
		}
        ++$count;
		$requete2= "SELECT a.\"NAME\", a.\"DEV_STAGE_ID\", b.\"NAME\" as \"DEV\" FROM \"ANAT_ENTITY\" a, \"DEV_STAGE\" b where a.\"ANAT_ID\"='".$row["DAUGHTER_CELL"]."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($conn,$requete2);
		
		if ($row2=pg_fetch_array($stmt2)){
		    $daughtername=$row2["NAME"];
			$dev2=$row2["DEV_STAGE_ID"];
			if($dev2==$dev)
			    $devname='<strong><font size="2" color="#FF9900">'.$row2["DEV"].'</font></strong>';
			else
			    $devname=$row2["DEV"];
		}
		}
		else continue;
		
		
	}
	else
	{
		//Rjout
		$mothercell=$row["MOTHER_CELL"];
		$requete3= "SELECT a.\"NAME\" FROM \"ANAT_ENTITY\" a where a.\"ANAT_ID\"='$mothercell' ";
		$stmt3 = pg_query($conn,$requete3);
		if ($row3=pg_fetch_array($stmt3)) {
		    $mothername=$row3["NAME"];
			
		}
        ++$count;
		$requete2= "SELECT a.\"NAME\", a.\"DEV_STAGE_ID\", b.\"NAME\" as \"DEV\" FROM \"ANAT_ENTITY\" a, \"DEV_STAGE\" b where a.\"ANAT_ID\"='".$row["DAUGHTER_CELL"]."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($conn,$requete2);
		
		if ($row2=pg_fetch_array($stmt2)){
		    $daughtername=$row2["NAME"];
			$dev2=$row2["DEV_STAGE_ID"];
			if($dev2==$dev)
			    $devname='<strong><font size="2" color="#FF9900">'.$row2["DEV"].'</font></strong>';
			else
			    $devname=$row2["DEV"];
		}

	}
	
	
//if ($doubl=='yes') $term[$nivo].=$daughtername."@";
$term[$nivo].=$row["DAUGHTER_CELL"]."@";
//print $term[$nivo]."<br>";
//
	}
return $res;
}


$res=select_recursif_descendant_2($c,$id,$res,0,'yes');




///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

	$lastdev="";
	//$lastmother="";

//On a $term dans l'ordre inverse, on va le remettre dans l'ordre:
$oterm=(array) null;
$oterm2=(array) null;

for ($a=count($term);$a>=0;$a--) $oterm[count($oterm)]=$term[$a];
for ($a=count($term2);$a>=0;$a--) $oterm2[count($oterm2)]=$term2[$a];






//Fin de la vignette des peres




	$couleur=array();
	
	$doublons=(array) null;//not sure it is usefull here
	//search for daughter cells

$nivo=0;

$recap=(array) null;
$recap2=(array) null;

$tree="";

function select_recursif_descendant_3($conn,$start,$res,$nivo,$doubl)
{
global $doublons;
global $lastdev;
global $count;
global $dev;

global $recap;
global $recap2;
global $tree;

/*ON ECRIT SA REQUETE*/

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

}
else return;


$stmt=pg_query($conn,$requete);

if (pg_num_rows($stmt)!=0) $nivo++;//print "Nz";
if (pg_num_rows($stmt)==0) $nivo--;//print "Ze";


while ($row=pg_fetch_array($stmt))
	{
	
	if ($doubl=='yes')
	{    if(! in_array($row["DAUGHTER_CELL"],$doublons))
	    	{
		$doublons[count($doublons)]=$row["DAUGHTER_CELL"];
		$mocell=$row["MOTHER_CELL"];
////////
		
		$requete2= "SELECT a.\"NAME\", a.\"DEV_STAGE_ID\", b.\"NAME\" as \"DEV\" FROM \"ANAT_ENTITY\" a, \"DEV_STAGE\" b where a.\"ANAT_ID\"='".$row["DAUGHTER_CELL"]."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($conn,$requete2);
		if ($row2=pg_fetch_array($stmt2)) {
		    $daughtername=$row2["NAME"];
			$dev2=$row2["DEV_STAGE_ID"];
			if($dev2==$dev)
			    $devname='<strong><font color="red">'.$row2["DEV"].'</font></strong>';
			else
			    $devname=$row2["DEV"];
		}
	
		$requete3= "SELECT a.\"NAME\" FROM \"ANAT_ENTITY\" a where a.\"ANAT_ID\"='$mocell' ";
		$stmt3 = pg_query($conn,$requete3);
		if ($row3=pg_fetch_array($stmt3)) {
		    $mothername=$row3["NAME"];
			
		}
		}
		else
		    continue;
	}
	else
	{	
	
        ++$count;
		$mocell=$row["MOTHER_CELL"];
////////
		
		$requete2= "SELECT a.\"NAME\", a.\"DEV_STAGE_ID\", b.\"NAME\" as \"DEV\" FROM \"ANAT_ENTITY\" a, \"DEV_STAGE\" b where a.\"ANAT_ID\"='".$row["DAUGHTER_CELL"]."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($conn,$requete2);
		if ($row2=pg_fetch_array($stmt2)) {
		    $daughtername=$row2["NAME"];
			$dev2=$row2["DEV_STAGE_ID"];
			if($dev2==$dev)
			    $devname='<strong><font color="red">'.$row2["DEV"].'</font></strong>';
			else
			    $devname=$row2["DEV"];
		}
	
		$requete3= "SELECT a.\"NAME\" FROM \"ANAT_ENTITY\" a where a.\"ANAT_ID\"='$mocell' ";
		$stmt3 = pg_query($conn,$requete3);
		if ($row3=pg_fetch_array($stmt3)) {
		    $mothername=$row3["NAME"];
			
		}		



	}

//if ($doubl=='yes') $recap[$nivo].=$daughtername."@";
if ($doubl=='yes') $recap[$nivo].=$row["DAUGHTER_CELL"]."@";


	$res=select_recursif_descendant_3($conn,$row["DAUGHTER_CELL"],$res,$nivo,$doubl);
	
	}
return $res;
}


$res=select_recursif_descendant_3($c,$id,$res,0,'yes'); //Avec doublon pour remplir recap

/*
for ($a=0;$a<count($oterm);$a++)
print $oterm[$a].'<br>';

for ($a=0;$a<count($recap);$a++)
print $recap[$a].'<br>';
*/

	print '<table border="1" cellspacing="0" cellpadding="0"><tr>';

//On recup tous les stades
//c pas génial, car ca fou des bugs
$requete="SELECT \"NAME\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' order by \"DEV_PERCENT\" ";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$devel[count($devel)]=$row["NAME"];
}


for ($a=0;$a<=count($oterm)-2;$a++)
{

print '<td valign="top" align="left">';
/*if ($a+2==$dev) 
print '&nbsp;<strong><font color="#FF9900">'.$devel[$a+1].'</font></strong>&nbsp;<br>____________________<br>';
else print '&nbsp;'.$devel[$a+1].'&nbsp;<br>____________________<br>';
*/
	$tmp=explode("@",$oterm[$a]);
	for ($b=0;$b<count($tmp)-1;$b++)
	{

//AJOUT
if ($b==0)
{
$r="SELECT a.\"NAME\",a.\"DEV_STAGE_ID\" from \"DEV_STAGE\" a,\"ANAT_ENTITY\" b where b.\"ANAT_ID\"='".$tmp[$b]."' and b.\"DEV_STAGE_ID\"=a.\"DEV_STAGE_ID\"";
$stmt=pg_query($c,$r);
$row=pg_fetch_array($stmt);
$stadereq=$row["NAME"];
$cur=$row["DEV_STAGE_ID"];

if ($cur==$dev) print '&nbsp;<font size="2" color="#FF9900"><b>'.$stadereq.'</b></font>&nbsp;<br>____________________<br>';
else print '&nbsp;<font color="#456898" size="2"><b>'.$stadereq.'</b></font>&nbsp;<br>____________________<br>';




}






	//$r="SELECT \"ANAT_ID\",\"DEV_STAGE_ID\" from \"ANAT_ENTITY\" where \"NAME\"='".$tmp[$b]."'";
	$r="SELECT \"NAME\",\"DEV_STAGE_ID\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='".$tmp[$b]."'";
	$stmt=pg_query($c,$r);
	$row=pg_fetch_array($stmt);

/*	
/////TRAITEMENT POUR LES EMBRYOS DANS LES CASE
	$r0="SELECT \"NAME\" from \"DEV_STAGE\" where \"DEV_STAGE_ID\"='".$row["DEV_STAGE_ID"]."'";
	$st0=pg_query($c,$r0);
	$row0=pg_fetch_array($st0);

$devname=$row0["NAME"];

$tmp0=explode("-",$devname);
if (strlen($tmp0[1])>0) 
	{
	//Stade inferieur ou egal a 110-cell stage
	$part=trim($tmp0[0]);
	$tmp2=explode(" ",$part);
	if (strlen($tmp2[1])>0) //Early ou Late32
		{
		if ($tmp2[0]=="Early") $STADE="early".$tmp2[1];
		if ($tmp2[0]=="Late") $STADE="late".$tmp2[1];
		}
	else
		{
			//Tout le reste
		$STADE=trim($tmp2[0]);


		}

	}
else
	{
	//Stade superieur a 110-cell stage ou Zygot ou Unfertilized egg
	;
	}

$aname=$row["NAME"];
if ($STADE=="late32") $aname="late$aname";
	
//////// FIN TRAITEMENT
*/
	print '<strong>.</strong><a href="short_hierarchy.php?region='.$tmp[$b].'&dev='.$row["DEV_STAGE_ID"].'"><font color="#000000" size="-1">'.$row["NAME"].'</font></a><br>';



/*
///////////SUITE TRAITEMENT ///////////////
if (is_file('embryos/'.$STADE.'c/'.$STADE.'anim.gif'))
{
print '<img width="100" src="embryos/'.$STADE.'c/'.$aname.'anim.jpg"><img width="100" src="embryos/'.$STADE.'c/'.$STADE.'vege.gif">';
}
//////////FIN SUITE TRAITEMENT ////////////
*/





	}

print '</td>';
}

for ($a=1;$a<=count($recap);$a++)
{
print '<td valign="top" align="left">';
//print '&nbsp;'.$devel[$dev+$a-1].'&nbsp;<br>____________________<br>';
	$tmp=explode("@",$recap[$a]);
	for ($b=0;$b<count($tmp)-1;$b++)
	{

//AJOUT
if ($b==0)
{
$r="SELECT a.\"NAME\",a.\"DEV_STAGE_ID\" from \"DEV_STAGE\" a,\"ANAT_ENTITY\" b where b.\"ANAT_ID\"='".$tmp[$b]."' and b.\"DEV_STAGE_ID\"=a.\"DEV_STAGE_ID\"";
$stmt=pg_query($c,$r);
$row=pg_fetch_array($stmt);
$stadereq=$row["NAME"];
$cur=$row["DEV_STAGE_ID"];

print '&nbsp;<font color="#456898" size="2"><b>'.$stadereq.'</b></font>&nbsp;<br>____________________<br>';

}







	$r="SELECT \"NAME\",\"DEV_STAGE_ID\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='".$tmp[$b]."'";
	$stmt=pg_query($c,$r);
	$row=pg_fetch_array($stmt);



	//print '<strong>.</strong>'.$tmp[$b].'<br>';
	print '<strong>.</strong><a href="short_hierarchy.php?region='.$tmp[$b].'&dev='.$row["DEV_STAGE_ID"].'"><font color="#000000" size="-1">'.$row["NAME"].'</font></a><br>';





	}





print '</td>';
}



print '</td></tr>';

print '</table>';







	if($count==0)
	    print "<strong>No match</strong>";
	print "</center>";



function rgb2html($r,$g,$b)
{
  // Le str_pad permet de remplir avec des 0
  // parce que sinon rgb2html(Array(0,255,255)) retournerait #0ffff<=manque un 0 !
  return "#" . str_pad(dechex( ($r<<16)|($g<<8)|$b ), 6, "0", STR_PAD_LEFT);
}

?>

<?php

print '<br><a href="FLATexport-lineage.php?idanat='.$id.'">Display Lineage in raw text format</a><br>';

    include("frames_footer.php");
?>