<?php
    include("connection.php");
    include("frames_header.php");
?>



<?php

function select_recursif_descendant($conn,$start,$res){ //transformed to get parent terms
	$res.=$start.",";
	
	//$requete="SELECT distinct * FROM \"GO_CHILD_OF\" WHERE \"CHILD_OF\"=".$start." ";
	$requete="SELECT distinct * FROM \"GO_CHILD_OF\" WHERE \"GO_ID\"=".$start." ";

	$stmt=pg_query($conn,$requete);
	while ($row=pg_fetch_array($stmt))
	{
		//$res=select_recursif_descendant($conn,"'".$row["GO_ID"]."'",$res);
		$res=select_recursif_descendant($conn,"'".$row["CHILD_OF"]."'",$res);
	}
	return $res;
}


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

$test=$_POST["test"];
if($test!='y'){
	$test='n';
}else print "test mode<br>";


$species=$_POST["species"];
if(strlen($species)<=0){
	print "Error: Please select a species.<br>";
	return;
}
print $species."<br><br>";


$allgo=array();;

$requete='select distinct("MOL_ID") from "MOL_ENTITY" where "TYPE"=\'gene\' and "SPECIES"=\''.$species.'\'';
$stmtGG = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while($rowGG=pg_fetch_array($stmtGG)){
	$genename=$rowGG["MOL_ID"];;
	
	$query="";
	$requete='select "GO_ID" from "GO_DESCRIPTION" where "MOL_ID"=\''.$genename.'\'';
	$stmtG = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	while($rowG=pg_fetch_array($stmtG)){
		$go=$rowG["GO_ID"];
		$result="";
		$go2="'".$go."'";
		$result=select_recursif_descendant($c,$go2,$result);
		$query.=$result;
		
		$go ="";
		$go2="";
		
	}
	
	$query=ereg_replace("'","",$query);
	$query=ereg_replace(" ","",$query);
	$query=explode(",",$query);
	$query=array_unique($query);
	
	foreach($query as $key=>$value){
		if(strlen($value)<3)
			continue;
		$allgo[$value]++;
	}
	
}



$count=0;
foreach ($allgo as $go => $occ) {
	if(strlen($go)<=0)
		continue;
	
	++$count;
	print "$go => $occ<br>";
	
	if($test=="n"){
		$requete='select * from "GO_STAT" where "SPECIES"=\''.$species.'\' and "GO_ID"=\''.$go.'\'';
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		if($row=pg_fetch_array($stmt)){
			//update
			$requete="update \"GO_STAT\" set \"OCCURRENCE\"=".$occ." where \"SPECIES\"='$species' and \"GO_ID\"='".$go."'";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
			++$count;
		}else{
		    //recording
			$requete="insert into \"GO_STAT\" (\"SPECIES\",\"GO_ID\",\"OCCURRENCE\")" .
					" values('".$species."','".$go."',".$occ.")";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
			++$count;
		}
	}
}


/*
$count=0;
$requete='select "GO_ID", count("GO_ID") as "COUNT" from "GO_DESCRIPTION" where "MOL_ID" in (select "MOL_ID" from "MOL_ENTITY" where "TYPE"=\'gene\' and "SPECIES"=\''.$species.'\') GROUP BY "GO_ID"';
$stmtG = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while($rowG=pg_fetch_array($stmtG)){

	if($test=="n"){
		$requete='select * from "GO_STAT" where "SPECIES"=\''.$species.'\' and "GO_ID"=\''.$rowG["GO_ID"].'\'';
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		if($row=pg_fetch_array($stmt)){
			//update
			$requete="update \"GO_STAT\" set \"OCCURRENCE\"=".$rowG["COUNT"]." where \"SPECIES\"=$species and \"GO_ID\"='".$rowG["GO_ID"]."'";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
			++$count;
		}else{
		    //recording
			$requete="insert into \"GO_STAT\" (\"SPECIES\",\"GO_ID\",\"OCCURRENCE\")" .
					" values('".$species."','".$rowG["GO_ID"]."',".$rowG["COUNT"].")";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
			++$count;
		}
	}
	
}
*/

print "<br><br>Done: $count records<br>";
?>


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