<?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/>.

*/
//connection
include("connection.php");
//Title before frames_header
print '<title>ANISEED v3.0 - Cleavage inequality result</title>';

   include("frames_header.php");
?>

<center>
<?php
	if($_POST["mvol"]==0)
		$mvol=">=";
	else
		$mvol="<=";
	$vol=$_POST["vol"];

	
	
	if(  (! is_numeric($vol))or($vol<0)  ){
	    print '<center><h2><strong>Wrong parameter.</strong></h2></center>';
		include("frames_footer.php");
		return;
	}
	
	
	$embryo=(array) null;
	if (isset($_POST["embryo"])) $embryo=$_POST["embryo"];

// print_r($_POST);



	$devstage=array();
	if(isset($_POST["devstage"]))
	    $devstage=$_POST["devstage"];

	$requete="SELECT \"NAME\",\"DEV_STAGE_ID\"  from \"DEV_STAGE\" where \"DEV_STAGE_ID\"=(select \"DEV_STAGE_ID\" from \"EMBRYO\" where \"EMBRYO_ID\"='".$embryo[0]."')";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$devid=$row["DEV_STAGE_ID"];
	$devname=$row["NAME"];
	


	$species="";
	if(isset($_POST["species"]))
	    $species=$_POST["species"];
	
	$celllist="";
	if(isset($_POST["celllist"]))
		$celllist=$_POST["celllist"];
	
	if(strlen($celllist)>0)
		print "<center><H2><font color=\"#FF6600\">Refining subset mode</font></H2></center>";
			

$t=(array) null;

print "<font color=\"#D64242\" size=\"3\"><b>Asymmetric Division</b></font><br>";

print "<font color=\"#D64242\" size=\"3\"><b>$devname ($species)</b></font><br><br>";

$volume=(array) null;

for ($i=0;$i<count($embryo);$i++)
{

	$requete="SELECT \"NAME\" from \"EMBRYO\" where \"EMBRYO_ID\"='".$embryo[$i]."'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$ename=$row["NAME"];
	$ename=str_replace("@"," ",$ename);
	print "<font color=\"green\" size=\"-1\"><b>$ename</b></font><br>";



	$requete="SELECT distinct a.\"CELL1\", a.\"CELL2\", a.\"RATIO\", b.\"NAME\", b.\"SPECIES\", b.\"DEV_STAGE_ID\", c.\"NAME\" as \"DEV\",a.\"EMBRYO_ID\" FROM \"EMBRYO_DIVISION\" a, \"ANAT_ENTITY\" b, \"DEV_STAGE\" c, \"EMBRYO_BIOMETRY\" d WHERE a.\"EMBRYO_ID\"='".$embryo[$i]."' and (a.\"CELL1\"=b.\"ANAT_ID\") and (b.\"DEV_STAGE_ID\"=c.\"DEV_STAGE_ID\")";
// 	print $requete;
	if( $species>0 ) $requete.=" AND (c.\"SPECIES\" ='$species') ";
	if( count($devstage)>0 ){ /*$devstage=implode("','",$devstage);*/$requete.=" AND (b.\"DEV_STAGE_ID\"='$devid') ";}
	if( $vol>0 ) $requete.=" AND (a.\"RATIO\"".$mvol.$vol.") ";
	if( strlen($celllist)>0)
	{
	$celllist = substr($celllist, 0, strlen($celllist)-1);
	$celllist = ereg_replace(":","','",$celllist);
	$requete.=" AND ( (a.\"CELL1\" in('".$celllist."'))OR(a.\"CELL2\" in('".$celllist."')) )";
	}
	$requete.="order by b.\"DEV_STAGE_ID\"";

	//$requete.="order by a.\"CELL1\",ascii(substring(b.\"NAME\" from 1 for 1)),b.\"NAME\"";


//   	print $requete."<br>";
	
	$stmt = pg_query($c,$requete);
	while ($row=pg_fetch_array($stmt))
	{
	if (strlen($t[$row["CELL1"]."@".$row["CELL2"]])==0) $t[$row["CELL1"]."@".$row["CELL2"]]=$row["RATIO"];
	else $t[$row["CELL1"]."@".$row["CELL2"]]+=$row["RATIO"];

	$requete0="SELECT \"VOLUME\" from \"EMBRYO_BIOMETRY\" where \"CELL\"='".$row["CELL1"]."' and \"EMBRYO_ID\"='".$embryo[$i]."'";
	$stmt0=pg_query($c,$requete0);
	$row0=pg_fetch_array($stmt0);

//  	print "VOL=".$row0["VOLUME"]."<br>";
	
	if (strlen($row["CELL1"])==0) $volume[$row["CELL1"]]=$row0["VOLUME"]; else $volume[$row["CELL1"]]+=$row0["VOLUME"];

	$requete0="SELECT \"VOLUME\" from \"EMBRYO_BIOMETRY\" where \"CELL\"='".$row["CELL2"]."' and \"EMBRYO_ID\"='".$embryo[$i]."'";
	$stmt0=pg_query($c,$requete0);
	$row0=pg_fetch_array($stmt0);

	if (strlen($row["CELL2"])==0) $volume[$row["CELL2"]]=$row0["VOLUME"]; else $volume[$row["CELL2"]]+=$row0["VOLUME"];

//  	print "VOL=".$row0["VOLUME"]."<br>";

	}
// print "<hr>";
}

print "<br><br>";

foreach ($t as $key => $val) 
	{
	$t[$key]/=count($embryo);
	$t[$key]=round($t[$key],2);
	}

foreach ($volume as $key => $val)
	{
	$volume[$key]/=count($embryo);
	$volume[$key]=round($volume[$key],2);
	}
	

	


	print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
	print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Anatomical entity 1</b></font></td><td><font color="#456898" size="2"><b>Anatomical entity1 Volume</b></font></td><td><font color="#456898" size="2"><b>Anatomical entity 2</b></font></td><td><font color="#456898" size="2"><b>Anatomical entity 2 Volume</b></font></td><td><font color="#456898" size="2"><b>Asymmetry (%)</b></font></td></tr>';

	$count=0;
	$celllist="";

	foreach ($t as $key => $val)
	{
	$tmp=explode("@",$key);
	$cell1=trim($tmp[0]);
	$cell2=trim($tmp[1]);

	$requete="SELECT \"NAME\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='$cell1'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$name1=$row["NAME"];

	$requete="SELECT \"NAME\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='$cell2'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$name2=$row["NAME"];

	$ratio=$val;

	$vol1=$volume[$cell1];
	$vol2=$volume[$cell2];
	
	if ($vol1>$vol2) 
		{
		$vol1isbold="<b>";
		$finvol1isbold="</b>";
		$vol2isbold="";
		$finvol2isbold="";
		}
	else
		{
		$vol2isbold="<b>";
		$finvol2isbold="</b>";
		$vol1isbold="";
		$finvol1isbold="";
		}


/*	$requete="SELECT avg(\"VOLUME\") as \"VOLUME\" from \"EMBRYO_BIOMETRY\" where \"CELL\"='$cell1'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$vol1=round($row["VOLUME"]);

	$requete="SELECT avg(\"VOLUME\") as \"VOLUME\" from \"EMBRYO_BIOMETRY\" where \"CELL\"='$cell2'";
	$stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
	$vol2=round($row["VOLUME"]);*/





	++$count;
		print '<tr align="center" valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
		
		print '<tr align="center" valign="top">
<td><a href="short_hierarchy.php?region='.$cell1.'&dev='.$devid.'"><font color="#000000" size="-1">'.$vol1isbold.$name1.$finvol1isbold.'</font></a></td> 
<td><font color="#656565" size="-1">'.$vol1isbold.$vol1.$finvol1isbold.'</font></td>
<td><a href="short_hierarchy.php?region='.$cell2.'&dev='.$devid.'"><font color="#000000" size="-1">'.$vol2isbold.$name2.$finvol2isbold.'</font></a></td> <td><font color="#656565" size="-1">'.$vol2isbold.$vol2.$finvol2isbold.'</font></td> 
<td><font color="#656565" size="-1">'.$ratio.'</font></td> 
</tr>';
		
		if($count>500){
			//OCICancel($stmt);
			pg_cancel_query($c);
			break;

	}

	}


	/*
	//search for cells		
	$requete="SELECT distinct a.\"CELL1\", a.\"CELL2\", a.\"RATIO\", b.\"NAME\", b.\"SPECIES\", b.\"DEV_STAGE_ID\", c.\"NAME\" as \"DEV\" FROM \"EMBRYO_DIVISION\" a, \"ANAT_ENTITY\" b, \"DEV_STAGE\" c WHERE (a.\"CELL1\"=b.\"ANAT_ID\") and (b.\"DEV_STAGE_ID\"=c.\"DEV_STAGE_ID\") and a.\"EMBRYO_ID\"='".$embryo[$i]."'";
	
	if( $species>0 ) $requete.=" AND (c.\"SPECIES\" ='$species') ";
	if( count($devstage)>0 ){ $devstage=join("','",$devstage);$requete.=" AND (b.\"DEV_STAGE_ID\"='$devid') ";}
	if( $vol>0 ) $requete.=" AND (a.\"RATIO\"".$mvol.$vol.") ";
	if( strlen($celllist)>0)
	{
	$celllist = substr($celllist, 0, strlen($celllist)-1);
	$celllist = ereg_replace(":","','",$celllist);
	$requete.=" AND ( (a.\"CELL1\" in('".$celllist."'))OR(a.\"CELL2\" in('".$celllist."')) )";
	}
	$requete.="order by b.\"DEV_STAGE_ID\"";
	print "$requete";
	$stmt = pg_query($c,$requete);

	print "<font color=\"#D64242\" size=\"3\"><b>Matching records</b></font><br><br>";
	print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
	print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Anatomical entity 1</b></font></td><td><font color="#456898" size="2"><b>Anatomical entity 2</b></font></td><td><font color="#456898" size="2"><b>Asymmetry (%)</b></font></td><td><font color="#456898" size="2"><b>Developmental stage</b></font></td><td><font color="#456898" size="2"><b>Species</b></font></td></tr>';
	$count=0;
	$celllist="";
	while( $row=pg_fetch_array($stmt) ){
	
	    $requete="SELECT \"NAME\" from \"ANAT_ENTITY\" WHERE \"ANAT_ID\"=".$row["CELL2"];
		$stmt2 = pg_query($c,$requete);
		if( $row2=pg_fetch_array($stmt2) )
		    $cell2=$row2["NAME"];
		
		
		++$count;
		print '<tr align="center" valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
		
		print '<tr align="center" valign="top"> <td><a href="short_hierarchy.php?region='.$row["CELL1"].'&dev='.$row["DEV_STAGE_ID"].'"><font color="#000000" size="-1">'.$row["NAME"].'</font></a></td> <td>'.'<a href="short_hierarchy.php?region='.$row["CELL2"].'&dev='.$row["DEV_STAGE_ID"].'"><font color="#000000" size="-1">'.$cell2.'</font></a></td> <td><font color="#656565" size="-1">'.$row["RATIO"].'</font></td> <td><font color="#656565" size="-1">'.$row["DEV"].'</font></td> <td><font color="#656565" size="-1">'.$row["SPECIES"].'</font></td> </tr>';
		
		if($count>500){
			//OCICancel($stmt);
			pg_cancel_query($c);
			break;
		}
		
	    $celllist.= $row["CELL1"].":".$row["CELL2"].":";  //liste les cell id pour le rafinage.
		
	}*/



	print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
	print '</table>';
	
	if($count==0)
		print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
	else
		print "<br><center>$count result(s)</center><br><br>";

	
	if($count>500)
		print "...<br>More than 500 results has been returned.<br>Please refine your query";




	if(substr_count($celllist,":")<1000){
		print '<form name="formulaire3" action="" method="post">';
		print '<input type="hidden" value="'.$celllist.'" name="celllist">';
		print'&nbsp;&nbsp;&nbsp;<input type="image" src="pictures/button-subset.jpg" onClick="javascript: window.document.formulaire3.action= window.document.formulaire3.redirect.value; submit;">';
		
		/*print '<br>
			   <select name="redirect" size="1">
				   <option value="biometry-search.php">Biometry</option>
				   <option value="neighbour-search.php">Neighbours</option>
				   <option value="fate-search.php">Fate</option>
				   <option value="lineage-search.php">Lineage</option>
				   <option value="division-search.php">Division</option>
				   <option value="insitu-search.php">In situ</option>
				   <option value="ID.php">Export ID list</option>
			   </select>';*/
		include("refine_anatomy.php");
			   
		print '</form>';
    }else{print '<font size="-1" color="#999999">Refine mode not available for set > 1000</font>';}

?>
</center>

<?php
    include("frames_footer.php");
?>