<?
include ("connection.php");

$H=$_GET["h"];
$ish=$_GET["ish"];

//$dev="64-cell stage";
//$dev="8-cell stage";
//$dev="Late 32-cell stage";

//$dev="44-cell stage";

//$ish="2759093";

$requete="select \"NAME\" from \"DEV_STAGE\" where \"DEV_STAGE_ID\"=(select distinct \"DEV_STAGE_ID\" from \"ANAT_ENTITY\" where \"ANAT_ID\" in(SELECT \"REGION_ID\" from \"TARGET\" where \"IN_SITU_ID\"='$ish'))";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$dev=$row["NAME"];



//$BIOM="FLATNESS";

$m=(array) null;
$t0=(array) null;

$requete="SELECT \"CELL_A\",\"CELL_B\" from \"NEIGHBOR\" where \"CELL_A\" in (
select \"ANAT_ID\" from \"ANAT_ENTITY\" where \"DEV_STAGE_ID\"=(
select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' and \"NAME\"='$dev'
)
) AND \"CONTACT\">0";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{

$requete0="SELECT \"NAME\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='".$row["CELL_A"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);

$A=$row0["NAME"];

 if (strlen($t0[$A])==0) $t0[$A]++;

$requete0="SELECT \"NAME\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='".$row["CELL_B"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);

$B=$row0["NAME"];

 if (strlen($t0[$B])==0) $t0[$B]++;

$m[$A][$B]=1;


}

arsort($t0);

// print_r($t0);return;


$t=(array) null;
foreach ($t0 as $key => $val)  $t[count($t)]=$key;


//print_r($m);
////// PARCOURS DE MATRICE ////////
$rel=(array) null;
foreach ($m as $key => $val) foreach ($val as $key2 => $val2) $rel[count($rel)]=$key."@".$key2;








display_network($t,$rel,$H,$H,$ish);






function display_network($t,$rel,$h,$w,$ish)
{
global $c;   //// VARIABLE GLOBALE
global $BIOM;//// A MODIFIER SI EXPLOITATION DU SCRIPT
global $dev; ////

if (count($t)==0) return;

$im=imagecreate($h,$w);
$background=imagecolorallocate($im,255,255,255);


$centreX=$w/2;
$centreY=$h/2;

// imageellipse($im,$centreX,$centreY,$w-100,$h-100,imagecolorallocate($im,0,0,0));


imagestring($im,5,$centreX,$centreY,$gene,imagecolorallocate($im,255,0,0));

//$R=200; si h=500 et $w=500

$R=($h*2)/5;


$alpha=round(360/(count($t)-0));
$unit=$alpha;
$alpha=0;


$pos=(array) null;


////////// AJOUT //////////
//$ish="2752366"; //16 cell attention a changé en haut
//$ish="2759176"; //16cell
//$ish="2751964"; //L32cell OTX

//$ish="2759271"; //16c Eph
//$ish="2758750"; //L32c Eph
//$ish="2752063"; //L32c Nodal
//$ish="2762737"; //64c Nodal
//$ish="2759554"; //16c FoxAa
//$ish="2763736"; //8c FoxAa
//$ish="2759093";


$requete="
select \"NAME\" from \"ANAT_ENTITY\" where \"ANAT_ID\" in (
SELECT distinct \"REGION_ID\" from \"TARGET\" where \"IN_SITU_ID\"='$ish')";
$stmt=pg_query($c,$requete);
$tanat=(array) null;
while ($row=pg_fetch_array($stmt)) 
	{
	$tanat[str_replace(" cell pair","",$row["NAME"])]++;
	$tanat[str_replace(" cell pair","",$row["NAME"])."*"]++;
	}
//////////////////////////

//print_r($tanat);return;

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

$X=$R*cos(deg2rad(90)-deg2rad($alpha))+$centreX;
$Y=$R*sin(deg2rad(90)-deg2rad($alpha))+$centreY;

/*
$requete="SELECT \"$BIOM\" from \"BIOMETRY_SHAPE\" where \"CELL\"=(
select \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='".$t[$i]."' and \"DEV_STAGE_ID\"=(
select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='$dev' and \"SPECIES\"='Ciona intestinalis'))";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);

$biom=$row["$BIOM"];*/


///////AJOUT/////
if (strlen($tanat[$t[$i]])>0) 
	{
	$Xp=$X;
	$Yp=$Y;
	if ($X>$centreX) $Xp+=20;
	if ($X<$centreX) $Xp-=50;
	if ($Y>$centreY) $Yp+=10;
	if ($Y<$centreY) $Yp-=10;

	imagestring($im,5,$Xp,$Yp,$t[$i],imagecolorallocate($im,0,0,255));
	
	//imagestring($im,5,$X,$Y,$t[$i],imagecolorallocate($im,0,0,255));
	}
else 
	{	
	$Xp=$X;
	$Yp=$Y;

	if ($X>$centreX) $Xp+=20;
	if ($X<$centreX) $Xp-=50;
	if ($Y>$centreY) $Yp+=10;
	if ($Y<$centreY) $Yp-=10;
	
	imagestring($im,5,$Xp,$Yp,$t[$i],imagecolorallocate($im,125,125,125));
	}
/////////////////


//imagestring($im,5,$X,$Y,$t[$i],imagecolorallocate($im,0,125,125));



$pos[$t[$i]]="$X@$Y@$alpha";

$alpha+=$unit;

}


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

$tmp=explode("@",$rel[$i]);

$target=$tmp[0];
$target2=$tmp[1];


//$position=$pos[$t[$target]];
$position=$pos[$target];

$position2=$pos[$target2];

$tmp=explode("@",$position);
$tmp2=explode("@",$position2);

$X=$tmp[0];
$Y=$tmp[1];

$X2=$tmp2[0];
$Y2=$tmp2[1];


$alpha=$tmp[2];
$alpha2=$tmp[2];


/////// AJOUT /////////////

if ((strlen($tanat[$target])>0) && (strlen($tanat[$target2])>0)) 
	{
	$color=imagecolorallocate($im,255,0,0);
	imageline($im,$X,$Y,$X2,$Y2,$color);
	}
else 
	{
	$color=imagecolorallocate($im,200,200,200);
	imageline($im,$X,$Y,$X2,$Y2,$color);
	//MDashedLine($im,$X,$Y,$X2,$Y2,$color,imagecolorallocate($im,255,255,255));
	}

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


/*$color=imagecolorallocate($im,255,0,0);
imageline($im,$X,$Y,$X2,$Y2,$color);*/



}  

header("Content-type:image/png");

  imagepng($im);




}




function MDashedLine($image, $x0, $y0, $x1, $y1, $fg, $bg)
{
        $st = array($fg, $fg, $fg, $fg, $bg, $bg, $bg, $bg);
        ImageSetStyle($image, $st);
        ImageLine($image, $x0, $y0, $x1, $y1, IMG_COLOR_STYLED);
}


?>