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

<?php
$speciesaniseed="";
if (isset($_POST["speciesaniseed"]))
{
	$speciesaniseed=$_POST["speciesaniseed"];
}
if(strlen($speciesaniseed)<=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>";
}//else print "No test mode selected. Data will be recorded.<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 \"IP_DESCRIPTION\" where \"MOL_ID\" in (select \"MOL_ID\" from \"MOL_ENTITY\" where \"SPECIES\"='$speciesaniseed')";
	pg_query($c,"begin");
	$stmt0 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	
	
		echo "Previous IP results deleted<br>"; 
		$requete="DELETE from \"GO_DESCRIPTION\" where (\"IP_ORIGIN\" is not null) and (\"PHYLOGENY_ORIGIN\" is null) and (\"BLAST_ORIGIN\" is null) and \"MOL_ID\" in (select \"MOL_ID\" from \"MOL_ENTITY\" where \"SPECIES\"='$speciesaniseed')";
		
		pg_query($c,"begin");
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			
		echo "Previous GO results deleted<br>"; 
			$requete="UPDATE \"GO_DESCRIPTION\" set \"IP_ORIGIN\"=''";
			pg_query($c,"begin");
			$stmt1 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
			
		pg_query($c,"commit");
	
	// Test whether commit was successful. If error occurred, return error message
		
		//ocifreestatement($stmt0);
		//ocifreestatement($stmt);
		//ocifreestatement($stmt1);
	
	pg_query($c,"commit");
	//echo "Commit of the 1st transaction : Done <br>"; 

}


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

//read the file
while(!feof($fd))
{
	$buffer = fgets($fd, 4096);
	if(ereg("Sequence \"([^\"]+)", $buffer, $genetab))
	{	
		$nbgenes++;
		$gene=$genetab[1];
		$requete= "SELECT \"GENE_ID\" FROM \"GENE\" where \"NAME\"='$gene'and \"GENE_ID\" in (select \"MOL_ID\" from \"MOL_ENTITY\" where \"SPECIES\"='$speciesaniseed')";
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		if($row=pg_fetch_array($stmt))
		{
			$geneID=$row["GENE_ID"];
			//echo"gene : $gene : trouve dans la base <br>"; 
		}
		else 
		{
			echo"<br> *********ERROR: $gene is missing from the database********** <br>" ;
			include("frames_footer.php");
			return;
		}
		//echo "geneID :::$geneID";
		//ocifreestatement($stmt);
	}
	
	if(ereg("InterPro +(IPR[^ ]+) +([^\n]+)", $buffer, $itpro))
	{	
		$interpro=$itpro[1];
        $term=$itpro[2];
	 	//echo "$interpro ($term)<br>";
	 }
	
	
	if(ereg("\(GO\:",$buffer))
	{
		$tmp=split("GO\:",$buffer);
		$taille=count($tmp);
		$go="";
		echo "gene: $gene ($geneID)<br>";
		for($i=0;$i<$taille; ++$i)
		{
			if (ereg("^([0-9]+)",$tmp[$i],$gonum)) 
			{
				$go="GO:".$gonum[1];
				echo "&nbsp; $go &nbsp;";
				if ($test=='n')
				{
					//echo "geneID ::$geneID   ";
					//echo "go ::$go   ";
					$requete="SELECT \"MOL_ID\" FROM \"GO_DESCRIPTION\" where (\"MOL_ID\"=$geneID) and (\"GO_ID\"='$go')";
					$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					if(!$row=pg_fetch_array($stmt))
					{

						//Test sur l'existence de ce terme GO
						$requete2="SELECT \"GO_ID\" from \"GO\" where \"GO_ID\"='$go'";
						$stmt2=pg_query($c,$requete2);
						if ($row2=pg_fetch_array($stmt2))
						{
						$requete= "INSERT INTO \"GO_DESCRIPTION\" (\"MOL_ID\", \"GO_ID\", \"ORIGIN\", \"SPECIES_ORIGIN\", \"IP_ORIGIN\") values ( $geneID, '$go', 'Interpro', 'Interpro', 'y' )";
						pg_query($c,"begin");
						$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						pg_query($c,"commit");						
						}
						else
						{
						print "<br>Warning ! This GO: ($go) is not present in the table !<br>";
						}
					
					}
					else
					{
						$requete= "UPDATE \"GO_DESCRIPTION\" set \"IP_ORIGIN\"='y' where (\"MOL_ID\"=$geneID) and (\"GO_ID\"='$go')";
						pg_query($c,"begin");
						$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						pg_query($c,"commit");
                        //ocifreestatement($stmt3);
					}
                    //ocifreestatement($stmt);
									
				}

			}
		}
		echo"<br>";
	}
	
	if(ereg("method", $buffer))
	{
		
		while( ($buffer = fgets($fd, 4096)) && ($buffer!= "\n"))
		{
			//if(ereg("^NA\n$",$buffer))	
			//	continue;
			if(ereg("^([^ ]+) +([^ ]+) +([^ ]+)[^T|?]+([^\n]+)",$buffer,$tab))
			{
				$method=trim($tab[1]);
				$mid=trim($tab[2]);
                $short=trim($tab[3]);
                $location=trim($tab[4]);
				echo" $method / $mid / $short<br>$location<br><br>";
				
				if(strlen($location)>$maxpos)
				{
					$maxpos=strlen($location);
				}
				if ($test=='n')
				{
					if ($interpro!="")
					{
                        $requete="SELECT \"IP_ID\" from \"INTERPRO\" where \"IP_ID\"='$interpro'" ;
                        $stmta = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if(!$rowa=pg_fetch_array($stmta))
			{
                               //ocifreestatement($stmta);
                               continue;
                        }
                        //ocifreestatement($stmta);
                        
						$requete="SELECT \"MOL_ID\", \"IP_ID\", \"METHOD\", \"LOCATION\" FROM \"IP_DESCRIPTION\" where (\"MOL_ID\"=$geneID) and (\"IP_ID\"='$interpro') and ( \"METHOD\"='$method') and (\"LOCATION\"= '$location')";
						$stmtbis = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						if(!$rowbis=pg_fetch_array($stmtbis))
						{
							$requete="INSERT INTO \"IP_DESCRIPTION\" (\"MOL_ID\", \"IP_ID\", \"METHOD\", \"DB_ID\", \"LOCATION\") values ( $geneID, '$interpro', '$method', '$mid', '$location' )";
							//echo"<br>$requete<br>";
							pg_query($c,"begin");
							$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
							pg_query($c,"commit");
							//ocifreestatement($stmt);
						}
						//ocifreestatement($stmtbis);
					}
				}
					
				if ($test=='y')
				{
					if (($interpro!="")or($interpro!=NULL))
					{
						echo "$geneID, '$interpro', '$method', '$mid', '$location' into \"IP_DESCRIPTION\" ";
					}
				}
			}
			else 
			{
				echo"ERROR: Missing parameter <br>";
				include("frames_footer.php");
				return;
			}
		}
	}

}
fclose($fd);
print "<br><br>DONE";
?>


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