<?
include ("connection.php");


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);
}

function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color,$dashed) {

   $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));

   $dx = $x2 + ($x1 - $x2) * $alength / $distance;
   $dy = $y2 + ($y1 - $y2) * $alength / $distance;

   $k = $awidth / $alength;

   $x2o = $x2 - $dx;
   $y2o = $dy - $y2;

   $x3 = $y2o * $k + $dx;
   $y3 = $x2o * $k + $dy;

   $x4 = $dx - $y2o * $k;
   $y4 = $dy - $x2o * $k;


if ($dashed=="dashed") 	MDashedLine($im, $x1, $y1, $dx, $dy, $color,"FFFFFF");
else  imageline($im, $x1, $y1, $dx, $dy, $color);

   	imageline($im, $x3, $y3, $x4, $y4, $color);
   	imageline($im, $x3, $y3, $x2, $y2, $color);
   	imageline($im, $x2, $y2, $x4, $y4, $color);
}


function displayMe($im,$R,$centreX,$centreY,$t,$rel,$gene,$level)
{
//global $im;
if ($level==0) imagestring($im,5,$centreX,$centreY,$gene,imagecolorallocate($im,0,0,0));


//// Traitement sur t pour enlever les doublon ///
$tempo=(array) null;
for ($i=0;$i<count($t);$i++) if (strlen($tempo[$t[$i]])==0) $tempo[$t[$i]]++;
//$t=(array) null;
foreach ($tempo as $key => $val) $tt[count($tt)]=$key;
//////////////////////////////////////////////////

if ($level==0) $alpha=round(180/(count($tt)-0));
if ($level>0)  $alpha=round(180/(count($tt)-0));


$unit=$alpha;
$alpha=0;
$pos=(array) null;
$lu=(array) null;
$deja=(array) null;
for ($i=0;$i<count($t);$i++)
{
if (strlen($lu[$t[$i]])!=0) {$deja[$t[$i]]++;continue; }
	
$X=$R*cos(deg2rad(90)-deg2rad($alpha))+$centreX;
$Y=$R*sin(deg2rad(90)-deg2rad($alpha))+$centreY;

$Xp=$X;
$Yp=$Y;

if ($X>$centreX) $Xp+=20;
if ($X<$centreX) $Xp-=50;
if ($Y>$centreY) $Yp+=10;
if ($Y<$centreY) $Yp-=10;

$colorT=imagecolorallocate($im,0,0,0);

imagestring($im,5,$Xp,$Yp,$t[$i],$colorT);

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

$lu[$t[$i]]++;
	
}

////////// ARETE ////////
$tour=0;
for ($i=0;$i<count($rel);$i++)
{
$tmp=explode("@",$rel[$i]);
$target=$tmp[0];
$type=$tmp[1];
$direct=$tmp[2];
$position=$pos[$target];
$tmp=explode("@",$position);
$X=$tmp[0];
$Y=$tmp[1];
$alpha=$tmp[2];

if (strlen($deja[$target])>0) {if ($tour==0) $tour++; else {$centreX+=0;$centreY+=0;$X+=5;$Y+=5;$tour=0;}}
if ($type=="U") $color=imagecolorallocate($im,0,180,0);
else if ($type=="D") $color=imagecolorallocate($im,255,0,0);
if ($direct=="direct") arrow($im,$centreX,$centreY,$X,$Y,10,3,$color,"notdashed");
if ($direct=="indirect") arrow($im,$centreX,$centreY,$X,$Y,10,3,$color,"dashed");
}
////////////////////////
return $pos;
}


function displayMe2($im,$R,$centreX,$centreY,$t,$rel,$gene,$level)
{
//global $im;
if ($level==0) imagestring($im,5,$centreX,$centreY,$gene,imagecolorallocate($im,0,0,0));

//// Traitement sur t pour enlever les doublon ///
$tempo=(array) null;
for ($i=0;$i<count($t);$i++) if (strlen($tempo[$t[$i]])==0) $tempo[$t[$i]]++;
//$t=(array) null;
foreach ($tempo as $key => $val) $tt[count($tt)]=$key;
//////////////////////////////////////////////////

if ($level==0) $alpha=round(-180/(count($tt)-0));
if ($level>0)  $alpha=round(-180/(count($tt)-0));
$unit=$alpha;
$alpha=-10; //// POUR NE PAS CHEVAUCHER AVEC L'AUTRE DIAGRAMME EN 180
$pos=(array) null;
$lu=(array) null;
$deja=(array) null;
for ($i=0;$i<count($t);$i++)
{
if (strlen($lu[$t[$i]])!=0) {$deja[$t[$i]]++;continue; }
	
$X=$R*cos(deg2rad(90)-deg2rad($alpha))+$centreX;
$Y=$R*sin(deg2rad(90)-deg2rad($alpha))+$centreY;

$Xp=$X;
$Yp=$Y;

if ($X>$centreX) $Xp+=20;
if ($X<$centreX) $Xp-=50;
if ($Y>$centreY) $Yp+=10;
if ($Y<$centreY) $Yp-=10;

$colorT=imagecolorallocate($im,0,0,0);

imagestring($im,5,$Xp,$Yp,$t[$i],$colorT);

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

$lu[$t[$i]]++;
	
}

////////// ARETE ////////
$tour=0;
for ($i=0;$i<count($rel);$i++)
{
$tmp=explode("@",$rel[$i]);
$target=$tmp[0];
$type=$tmp[1];
$direct=$tmp[2];
$position=$pos[$target];
$tmp=explode("@",$position);
$X=$tmp[0];
$Y=$tmp[1];
$alpha=$tmp[2];


if (strlen($deja[$target])>0) {if ($tour==0) $tour++; else {$centreX+=0;$centreY+=0;$X+=5;$Y+=5;$tour=0;}}
if ($type=="U") $color=imagecolorallocate($im,0,0,180);
else if ($type=="D") $color=imagecolorallocate($im,255,0,255);
if ($direct=="direct") arrow($im,$X,$Y,$centreX,$centreY,10,3,$color,"notdashed");
if ($direct=="indirect") arrow($im,$X,$Y,$centreX,$centreY,10,3,$color,"dashed");
}
////////////////////////
return $pos;
}

/*

function network($gene,$h,$w,$R0,$R1)
{ ////// FUNCTION ////
global $c;

$im=imagecreate($h,$w);
imagefilledrectangle($im,0,0,$h,$w,imagecolorallocate($im,220,220,220));
//$background=imagecolorallocate($im,220,220,220);

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


$GLOBAL=(array) null;
////////////////////////////////////////////////////////////////

////// TOUR 1 /////////

$requete="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$gene'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);

$genename=$row["GENE_CLUSTER_NAME"];
if (strlen($genename)==0) $genename=$gene;



$requete="SELECT distinct \"GENE_X\",\"GENE_Y\",\"TYPE\",\"DIRECT\" from \"REG_EVIDENCE\" where \"GENE_X\"='$gene'";// and \"DEV_STAGE_ID\"='567'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{

$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='".$row["GENE_Y"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);

$curname=$row0["GENE_CLUSTER_NAME"];
if (strlen($curname)==0) $curname=$row["GENE_Y"];

$t[count($t)]=$curname;
$type=strtoupper($row["TYPE"]);
$type=$type{0};
if ($row["DIRECT"]=="y") $direct="direct";
if ($row["DIRECT"]=="n") $direct="indirect";
//$rel[count($rel)]=$row["GENE_Y"]."@".$type."@".$direct;
$rel[count($rel)]=$curname."@".$type."@".$direct;
}


//  print_r($t);
//  print_r($rel);

//$R=2000;
$R=$R0;
if (count($t)>0) $pos0=displayMe($im, $R,$centreX,$centreY,$t,$rel,$genename,0);
////////////////////////////
//print_r($pos0);print "<br>";
$GLOBAL[count($GLOBAL)]=$pos0;


/////// TOUR 2 /////////////
$tour0=(array) null;
$tabpos=(array) null;
for ($i=0;$i<count($t);$i++)
{
$t0=(array) null;
$rel0=(array) null;
$genename=$t[$i];
$requete="SELECT \"GENE_CLUSTER_ID\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_NAME\"='$genename'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if (strlen($row["GENE_CLUSTER_ID"])>0) $gene=$row["GENE_CLUSTER_ID"];
else $gene=$genename;

//$R=300;
$R=$R1;


$requete="SELECT distinct \"GENE_X\",\"GENE_Y\",\"TYPE\",\"DIRECT\" from \"REG_EVIDENCE\" where \"GENE_X\"='$gene'";// and \"DEV_STAGE_ID\"='567'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='".$row["GENE_Y"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
$curname=$row0["GENE_CLUSTER_NAME"];
if (strlen($curname)==0) $curname=$row["GENE_Y"];
$t0[count($t0)]=$curname;

$tour0[count($tour0)]=$curname;
$type=strtoupper($row["TYPE"]);
$type=$type{0};
if ($row["DIRECT"]=="y") $direct="direct";
if ($row["DIRECT"]=="n") $direct="indirect";
//$rel0[count($rel0)]=$row["GENE_Y"]."@".$type."@".$direct;
$rel0[count($rel0)]=$curname."@".$type."@".$direct;
}
$point=$pos0[$genename];
$tmp=explode("@",$point);
$centreX=$tmp[0];
$centreY=$tmp[1];
if (count($t0)>0) 
	{
	$pos1=displayMe($im,$R,$centreX,$centreY,$t0,$rel0,$genename,1);
	$tabpos[count($tabpos)]=$pos1;
	$GLOBAL[count($GLOBAL)]=$pos1;
	}

}
///////////////////////////
/////////////////////////////////////////////////////////////////

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

//     imagepng($im);


return $GLOBAL;
} //// FUNCTION ////

*/


function networkGLOB($gene,$h,$w,$R0,$R1,$devid,$anat)
{ ////// FUNCTION ////
global $c;

$im=imagecreate($h,$w);
imagefilledrectangle($im,0,0,$h,$w,imagecolorallocate($im,220,220,220));
//$background=imagecolorallocate($im,220,220,220);

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

$GLOBcentreX=$centreX;
$GLOBcentreY=$centreY;
$GLOBgene=$gene;


$GLOBAL=(array) null;
////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////// CEUX QUI SONT REGULES PAR LE GENE /////////////////////////////////////////////////////////////

////// TOUR 1 /////////

$requete="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$gene'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);

$genename=$row["GENE_CLUSTER_NAME"];
if (strlen($genename)==0) $genename=$gene;


if (strlen($devid)>0) $DEVSTAGE=" AND \"DEV_STAGE_ID\"='$devid' ";
if (strlen($anat)>0) $ANAT="AND \"REG_ANAT\"='$anat' ";

$requete="SELECT distinct \"GENE_X\",\"GENE_Y\",\"TYPE\",\"DIRECT\" from \"REG_EVIDENCE\" where \"GENE_X\"='$gene' $DEVSTAGE $ANAT";// and \"DEV_STAGE_ID\"='567'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{

$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='".$row["GENE_Y"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);

$curname=$row0["GENE_CLUSTER_NAME"];
if (strlen($curname)==0) $curname=$row["GENE_Y"];

$t[count($t)]=$curname;
$type=strtoupper($row["TYPE"]);
$type=$type{0};
if ($row["DIRECT"]=="y") $direct="direct";
if ($row["DIRECT"]=="n") $direct="indirect";
//$rel[count($rel)]=$row["GENE_Y"]."@".$type."@".$direct;
$rel[count($rel)]=$curname."@".$type."@".$direct;
}


//  print_r($t);
//  print_r($rel);

//$R=2000;
$R=$R0;
if (count($t)>0) $pos0=displayMe($im, $R,$centreX,$centreY,$t,$rel,$genename,0);
////////////////////////////
//print_r($pos0);print "<br>";
$GLOBAL[count($GLOBAL)]=$pos0;


/////// TOUR 2 /////////////
$tour0=(array) null;
$tabpos=(array) null;
for ($i=0;$i<count($t);$i++)
{
$t0=(array) null;
$rel0=(array) null;
$genename=$t[$i];
$requete="SELECT \"GENE_CLUSTER_ID\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_NAME\"='$genename'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if (strlen($row["GENE_CLUSTER_ID"])>0) $gene=$row["GENE_CLUSTER_ID"];
else $gene=$genename;

//$R=300;
$R=$R1;


$requete="SELECT distinct \"GENE_X\",\"GENE_Y\",\"TYPE\",\"DIRECT\" from \"REG_EVIDENCE\" where \"GENE_X\"='$gene' $DEVSTAGE $ANAT";// and \"DEV_STAGE_ID\"='567'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='".$row["GENE_Y"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
$curname=$row0["GENE_CLUSTER_NAME"];
if (strlen($curname)==0) $curname=$row["GENE_Y"];
$t0[count($t0)]=$curname;

$tour0[count($tour0)]=$curname;
$type=strtoupper($row["TYPE"]);
$type=$type{0};
if ($row["DIRECT"]=="y") $direct="direct";
if ($row["DIRECT"]=="n") $direct="indirect";
//$rel0[count($rel0)]=$row["GENE_Y"]."@".$type."@".$direct;
$rel0[count($rel0)]=$curname."@".$type."@".$direct;
}
$point=$pos0[$genename];
$tmp=explode("@",$point);
$centreX=$tmp[0];
$centreY=$tmp[1];
if (count($t0)>0) 
	{
	$pos1=displayMe($im,$R,$centreX,$centreY,$t0,$rel0,$genename,1);
	$tabpos[count($tabpos)]=$pos1;
	$GLOBAL[count($GLOBAL)]=$pos1;
	}

}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
					///////////////// TRANSITION ///////////////////

$t=(array) null;
$rel=(array) null;
$pos1=(array) null;
$pos0=(array) null;
$t0=(array) null;
$rel0=(array) null;
$centreX=$GLOBcentreX;
$centreY=$GLOBcentreY;
$gene=$GLOBgene;



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

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// CEUX QUI REGULENT LE GENE //////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////// TOUR 1 /////////

$requete="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$gene'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);

$genename=$row["GENE_CLUSTER_NAME"];
if (strlen($genename)==0) $genename=$gene;



$requete="SELECT distinct \"GENE_X\",\"GENE_Y\",\"TYPE\",\"DIRECT\" from \"REG_EVIDENCE\" where \"GENE_Y\"='$gene' $DEVSTAGE $ANAT";// and \"DEV_STAGE_ID\"='567'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{

$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='".$row["GENE_X"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);

$curname=$row0["GENE_CLUSTER_NAME"];
if (strlen($curname)==0) $curname=$row["GENE_X"];

$t[count($t)]=$curname;
$type=strtoupper($row["TYPE"]);
$type=$type{0};
if ($row["DIRECT"]=="y") $direct="direct";
if ($row["DIRECT"]=="n") $direct="indirect";
//$rel[count($rel)]=$row["GENE_Y"]."@".$type."@".$direct;
$rel[count($rel)]=$curname."@".$type."@".$direct;
}


//  print_r($t);
//  print_r($rel);

//$R=2000;
$R=$R0;
if (count($t)>0) $pos0=displayMe2($im,$R,$centreX,$centreY,$t,$rel,$genename,0);
////////////////////////////
//print_r($pos0);print "<br>";
$GLOBAL[count($GLOBAL)]=$pos0;


/////// TOUR 2 /////////////
$tour0=(array) null;
$tabpos=(array) null;
for ($i=0;$i<count($t);$i++)
{
$t0=(array) null;
$rel0=(array) null;
$genename=$t[$i];
$requete="SELECT \"GENE_CLUSTER_ID\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_NAME\"='$genename'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if (strlen($row["GENE_CLUSTER_ID"])>0) $gene=$row["GENE_CLUSTER_ID"];
else $gene=$genename;

//$R=300;
$R=$R1;


$requete="SELECT distinct \"GENE_X\",\"GENE_Y\",\"TYPE\",\"DIRECT\" from \"REG_EVIDENCE\" where \"GENE_Y\"='$gene' $DEVSTAGE $ANAT";// and \"DEV_STAGE_ID\"='567'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='".$row["GENE_X"]."'";
$stmt0=pg_query($c,$requete0);
$row0=pg_fetch_array($stmt0);
$curname=$row0["GENE_CLUSTER_NAME"];
if (strlen($curname)==0) $curname=$row["GENE_X"];
$t0[count($t0)]=$curname;

$tour0[count($tour0)]=$curname;
$type=strtoupper($row["TYPE"]);
$type=$type{0};
if ($row["DIRECT"]=="y") $direct="direct";
if ($row["DIRECT"]=="n") $direct="indirect";
//$rel0[count($rel0)]=$row["GENE_Y"]."@".$type."@".$direct;
$rel0[count($rel0)]=$curname."@".$type."@".$direct;
}
$point=$pos0[$genename];
$tmp=explode("@",$point);
$centreX=$tmp[0];
$centreY=$tmp[1];
if (count($t0)>0) 
	{
	$pos1=displayMe2($im,$R,$centreX,$centreY,$t0,$rel0,$genename,1);
	$tabpos[count($tabpos)]=$pos1;
	$GLOBAL[count($GLOBAL)]=$pos1;
	}

}

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




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

//      imagepng($im);


return $GLOBAL;
} //// FUNCTION ////


// $h=4000;



// $GLOBAL=networkGLOB("cluster11245",$h,$h,$h/3,($h/3)/8);






?>