<?
include ("connection.php");
set_time_limit(0);

$species="Ciona intestinalis";
//$devname="Early 32-cell stage";
$devname="16-cell stage";
//$devname="Early neurula";
//$devname="Zygote";
$maxdist=0.6;




$wordsone=(array) null;
$wordstwo=(array) null;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
function select_recursif_descendant_2_W2($conn,$start,$res)
{
global $word;global $wordstwo;global $wordsone;global $count;

if ($start!='') $requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"ANAT_ID\"= $start "; else return;
$stmt2=pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));
while($row2=pg_fetch_array($stmt2))
	{
	$res=select_recursif_descendant_2_W2($conn,$row2["CHILD_OF"],$res);
	$word=$row2["ANAT_ID"];
	if( (! in_array($word,$wordstwo)) ) $wordstwo[count($wordstwo)]=$word;
	}
return $res;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function select_recursif_descendant_2_W1($conn,$start,$res)
{
global $word;global $wordstwo;global $wordsone;global $count;

if ($start!='') $requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"ANAT_ID\"= $start "; else return;
$stmt2=pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));
while($row2=pg_fetch_array($stmt2))
	{
	$res=select_recursif_descendant_2_W1($conn,$row2["CHILD_OF"],$res);
	$word=$row2["ANAT_ID"];
	if( (! in_array($word,$wordsone)) ) $wordsone[count($wordsone)]=$word;
	}
return $res;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function Commun_Spe($c,$A,$B)
{
global $wordsone;global $wordstwo;
$wordsone=(array) null;
$wordstwo=(array) null;
$commun=0;
$wordsone[count($wordsone)]=$A;
select_recursif_descendant_2_W1($c,$A,$res);
//print_r($wordsone);
//print "<br>";
$count=0;
$wordstwo[count($wordstwo)]=$B;
select_recursif_descendant_2_W2($c,$B,$res);
//print_r($wordstwo);

for ($x=0;$x<count($wordsone);$x++) for ($y=0;$y<count($wordstwo);$y++) if ($wordsone[$x]==$wordstwo[$y]) $commun++;
$spe1=count($wordsone)-$commun;
$spe2=count($wordstwo)-$commun;


return array($commun,$spe1,$spe2);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function getCommun($tab) { return $tab[0]; }
function getSpe1($tab) { return $tab[1]; }
function getSpe2($tab) { return $tab[2]; }

function distanceNode($c,$A,$B)
{
$S1_S2_commun=Commun_Spe($c,$A,$B);
$C=getCommun($S1_S2_commun);
$S1=getSpe1($S1_S2_commun);
$S2=getSpe2($S1_S2_commun);

$dist=($S1/($S1+$C))+($S2/($S2+$C));
return $dist;

}


//print distanceNode($c,"2464805","2464804");


//////////////////////// OPTIMIZATION /////////////////////////

$tab=(array) null;

$requete="select \"IN_SITU_ID\" from \"IN_SITU\" where \"DEV_STAGE_ID\"=(
select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' and \"NAME\"='$devname'
) and \"PRIVATE\"='n' and \"CHECKED\"='y'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
//print $row["IN_SITU_ID"]."<br>";


$region="";
$requete0="SELECT distinct \"REGION_ID\" from \"TARGET\" where \"IN_SITU_ID\"='".$row["IN_SITU_ID"]."' order by \"REGION_ID\"";
$stmt0=pg_query($c,$requete0);
while ($row0=pg_fetch_array($stmt0))
{
if (strlen($region)==0) $region=$row0["REGION_ID"];
else $region.="@".$row0["REGION_ID"];
}

if (strlen($tab[$region])==0) $tab[$region]=$row["IN_SITU_ID"];
else $tab[$region].="@".$row["IN_SITU_ID"];

$region="";

}

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

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


$CACHE=(array) null;

$TSUMINA=(array) null;
$I=0;
foreach ($tab as $key => $val)
{
$J=0;
foreach ($tab as $key2 => $val2)
	{
	
	//if ($key==$key2) continue;
	//if ($I>=$J) {$J++;continue;}
	if ($I>$J) {$J++;continue;}

// 	print "$I=$J ($key) VS ($key2) <br>";

	$region=explode("@",$key);
	$region2=explode("@",$key2);

	$sumMIN=0;
	for ($i=0;$i<count($region);$i++)
		{
		$MIN=100;
		for ($j=0;$j<count($region2);$j++)
			{
			if (strlen($CACHE[$region[$i]."@".$region2[$j]])==0) 
				{
				$dist=distanceNode($c,$region[$i],$region2[$j]);
				$CACHE[$region[$i]."@".$region2[$j]]=$dist;
				}
			else $dist=$CACHE[$region[$i]."@".$region2[$j]];
//  			print $region[$i]." VS ".$region2[$j]." => ".$dist."<br>";
			if ($dist<$MIN) $MIN=$dist;
			}
// 		print "MIN=$MIN<br>";	
		$sumMIN+=$MIN;	



		}
	$sumMIN/=count($region);
// 	print "SUMMIN=$sumMIN";
// 	print "<hr>";
	$TSUMINA[count($TSUMINA)]=$sumMIN;
	$J++;
	}
$I++;
}


// print "<br>";
// print "==>";print_r($TSUMINA);
/////////////////// FAIRE PAREIL POUR L'AUTRE SENS ///////////////////////////


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

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

$TSUMINB=(array) null;
$I=0;
foreach ($tab as $key => $val)
{
$J=0;
foreach ($tab as $key2 => $val2)
	{
	
	//if ($key==$key2) continue;
	//if ($I>=$J) {$J++;continue;}
	if ($I>$J) {$J++;continue;}
	

	//print "$I=$J ($key) VS ($key2) <br>";

	$region=explode("@",$key);
	$region2=explode("@",$key2);

	$sumMIN=0;
	for ($i=0;$i<count($region2);$i++)
		{
		$MIN=100;
		for ($j=0;$j<count($region);$j++)
			{
			if (strlen($CACHE[$region2[$i]."@".$region[$j]])==0)
				{
				$dist=distanceNode($c,$region2[$i],$region[$j]);
				$CACHE[$region2[$i]."@".$region[$j]]=$dist;
				}
			else $dist=$CACHE[$region2[$i]."@".$region[$j]]=$dist;
//  			print $region2[$i]." VS ".$region[$j]." => ".$dist."<br>";
			if ($dist<$MIN) $MIN=$dist;
			}
// 		print "MIN=$MIN<br>";	
		$sumMIN+=$MIN;	



		}
	$sumMIN/=count($region);
// 	print "SUMMIN=$sumMIN";
// 	print "<hr>";
	$TSUMINB[count($TSUMINB)]=$sumMIN;
	$J++;
	}
$I++;
}

// print "<br>";
// print "==>";print_r($TSUMINB);


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////// CALCUL DICE


$dice=(array) null;
for ($i=0;$i<count($TSUMINA);$i++)
{
$a=$TSUMINA[$i];
$b=$TSUMINB[$i];

$dice[count($dice)]=($a+$b)/2;

}


//print "DICE ========+++>";print_r($dice);

//  print "<br><br><br>";


//$handlew=fopen("./resultDICE/resultDICE.txt","w") or die ("ERROR");
pg_query($c,"begin");
$K=0;
$I=0;
$TEST=(array) null;
$COUNT=0;
foreach ($tab as $key => $val)
{
$J=0;
foreach ($tab as $key2 => $val2)
	{

	if ($I>$J) {$J++;continue;}

//  	print "ISH : $val VS $val2 = ".$dice[$K]."<br>";
	$tmp=explode("@",$val);
	$tmp2=explode("@",$val2);

	for ($i=0;$i<count($tmp);$i++)
		{
		for ($j=0;$j<count($tmp2);$j++)
			{
			//if ($i>$j) continue;
			if ($tmp[$i]==$tmp2[$j]) continue;
			
			if ($TEST[$tmp[$i]."@".$tmp2[$j]]==1) continue;
			if ($TEST[$tmp2[$j]."@".$tmp[$i]]==1) continue;

			//print $tmp[$i]." VS ".$tmp2[$j]."<br>";
			//print $tmp2[$j]." VS ".$tmp[$i]."<br>";
			$TEST[$tmp[$i]."@".$tmp2[$j]]++;
			$TEST[$tmp2[$j]."@".$tmp[$i]]++;
			
			//print "INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\",\"IN_SITU_ID\",\"DISTANCE\",\"TYPE\") values ('".trim($tmp[$i])."','".trim($tmp2[$j])."','".$dice[$K]."','st');<br>";
			//fputs($handlew,"INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\",\"IN_SITU_ID_2\",\"DISTANCE\",\"TYPE\") values ('".trim($tmp[$i])."','".trim($tmp2[$j])."','".$dice[$K]."','st');\n");
			pg_query($c,"INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\",\"IN_SITU_ID_2\",\"DISTANCE\",\"TYPE\") values ('".trim($tmp[$i])."','".trim($tmp2[$j])."','".$dice[$K]."','st');");
			//print $tmp[$i]." ".$tmp2[$j]." ===>".$dice[$K]."<br>";


			//fputs($handlew,"INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\",\"IN_SITU_ID_2\",\"DISTANCE\",\"TYPE\") values ('".trim($tmp2[$j])."','".trim($tmp[$i])."','".$dice[$K]."','st');\n");
			//print "INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\",\"IN_SITU_ID_2\",\"DISTANCE\",\"TYPE\") values ('".trim($tmp2[$j])."','".trim($tmp[$i])."','".$dice[$K]."','st');<br>";
			pg_query($c,"INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\",\"IN_SITU_ID_2\",\"DISTANCE\",\"TYPE\") values ('".trim($tmp2[$j])."','".trim($tmp[$i])."','".$dice[$K]."','st');");
			$COUNT++;
			
			

			}
		}

	$J++;
	$K++;
	}
$I++;
}

pg_query($c,"commit");
fclose($handlew);

print "<br><br>COUNT=".($COUNT*2);





?>