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

<?php
$species="";
if (isset($_POST["species"]))
{
	$species=$_POST["species"];
}
if(strlen($species)<=0)
{
 echo "<h2>Error : Please select a species.</h2>";
 include("frames_footer.php");
 return;
}


if (isset($_POST["test"]))
	$test=$_POST["test"];
else $test='n';
if($test=='y'){
	echo "<h2>Test mode selected. No data will be recorded.</h2><br>";
}


if (isset($_POST["filepath"]))
	$path=$_POST["filepath"];
else $path="";

if (isset($_POST["del"]))
	$del=$_POST["del"];
else $del='n';


if(($test=='n')&&($del=='y'))
{
	//$requete="DELETE from ciona_design.go_description where mol_id in (select gene_id from ciona_design.gene where species='$species' and origin='blast')";
    $requete= "DELETE from \"GO_DESCRIPTION\" where \"BLAST_ORIGIN\"='y' AND \"IP_ORIGIN\" is NULL AND \"PHYLOGENY_ORIGIN\" is NULL and \"MOL_ID\" in (select \"MOL_ID\" from \"MOL_ENTITY\" where \"SPECIES\"='$species')";
	pg_query($c,"begin");
	$stmt0 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	pg_query($c,"commit");

	echo "Previous results deleted<br>"; 

	$requete="UPDATE \"GO_DESCRIPTION\" set \"BLAST_ORIGIN\"=''";
	pg_query($c,"begin");
	$stmt1 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	pg_query($c,"commit");	


	//ocifreestatement($stmt0);
	//ocifreestatement($stmt1);
	
	
	
}
//////////////////////////////////////////////////////
if (!($fd=fopen ($path, "r")))
{
	echo "$path cannot be read<br>";
	include("frames_footer.php");
	return;
}
else{
	echo "$path is open <br>";
}


//read the file
while(!feof($fd))
{	
	//$pointeurgoa1=ftell ($fd);
	$buffergoa = fgets($fd, 4096);
	$tmpgoa=split("\t",$buffergoa);
	
	$prot=$tmpgoa[2];
	$ori=$tmpgoa[5];
	
	ereg("GO\:([0-9]+)",$buffergoa,$gotab);
	$go=$gotab[1];
	
//	print "$prot -- $ori -- GO:$go<br>";
	
	if( (strlen($go)>0)and(strlen($prot)>0)and(strlen($ori)>0)and($ori != "GOA:interpro") ){
	    if (isset($tabgoaid1[$prot])) 
		    $tabgoaid1[$prot]=$tabgoaid1[$prot].":".$go;
		else
		    $tabgoaid1[$prot]=$go;
			
//		print $prot."->".$tabgoaid1[$prot]."<br><br>";
	}
	$prot=""; $ori="";	$go=""; $tmpgoa="";
}
fclose($fd);



$requete="select \"GENE_ID\" from \"GENE\", \"MOL_ENTITY\" where \"GENE_ID\"=\"MOL_ID\" and \"SPECIES\"='$species'";
$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while($row=pg_fetch_array($stmt)){
    
	$geneid=$row["GENE_ID"];
	
	$requete="select \"PROTEIN\", \"EVALUE\" from \"BLAST\" where \"MOL_ID\"=$geneid and \"DB\"='swissprot'";
	$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	while($row2=pg_fetch_array($stmt2)){
	    $eval = $row2["EVALUE"];
		$bestprot = $row2["PROTEIN"];
	    if($eval <= 1e-10){
		    if(array_key_exists($bestprot,$tabgoaid1)){
			    $golist=$tabgoaid1[$bestprot];
				$golist=split(":",$golist);
				for($a=0; $a<count($golist); ++$a){
				    $goid="GO:".$golist[$a];
					
					print "-gene#$geneid  ->$bestprot ->$goid<br>";
					
					if(strlen($goid)<=0)
					    continue;
					
					$requete="select * from \"GO\" where \"GO_ID\"='$goid'";
					$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					if(! $row3=pg_fetch_array($stmt3)){
					    print "$goid is missing<br>";
						continue;
					}
					
					
					if($test=='n'){
						$requete="select * from \"GO_DESCRIPTION\" where \"MOL_ID\"=$geneid and \"GO_ID\"='$goid'";
						$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						if($row3=pg_fetch_array($stmt3)){
							$requete= "update \"GO_DESCRIPTION\" set \"BLAST_ORIGIN\"='y' where \"MOL_ID\"=$geneid and \"GO_ID\"='$goid'";
						//print $requete;
							pg_query($c,"begin");
							$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
							pg_query($c,"commit");
							
						}else{
							$requete= "INSERT INTO \"GO_DESCRIPTION\" (\"MOL_ID\", \"GO_ID\", \"ORIGIN\", \"BLAST_ORIGIN\", \"SPECIES_ORIGIN\") values ($geneid, '$goid', 'blast', 'y', 'blast')";
						//print $requete;
							pg_query($c,"begin");
							$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
							pg_query($c,"commit");
						}
						//OCIFreeStatement($stmt3);
					}
					$goid="";
				}
			}
		}
	}//OCIFreeStatement($stmt2);

}
//OCIFreeStatement($stmt);


print "<br><br>DONE";
?>


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