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


<?php

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

if (isset($_POST["pathgenemodels"]))
{
	$pathgenemodels=$_POST["pathgenemodels"];
  
}
else 
{
 echo "<h2>Error : Please select a Genes model file.</h2>";
 include("frames_footer.php");
 return;
}



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


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

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

//file containing the good ID in first position
$pathtowrite="./est_clustering/species-format.txt";

$os=getenv("OS");

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

if($path=="")
{
	echo"Error: Please select an est/cDNA file <BR>";
	include("frames_footer.php");
	return;
}
echo "The file to parse is : $path<br>";


if(!($fdread=fopen($path, "r")))
{
	echo " ERROR : Can't open '$path' <BR>";
	include("frames_footer.php");
	return;
}
else
{
	echo " The file '$path' is open <BR>";
}

if(!($fdwrite=fopen ($pathtowrite, "w")))
{
	echo " ERROR : Can't create the file :$pathtowrite <BR>";
	include("frames_footer.php");
	return;
}
else
{
	echo " The file : $pathtowrite is being created<BR>";
}
if ($launch=='y')
{
$buffer = fgets($fdread, 4096);
while(!feof($fdread))
{
	if(ereg("^>", $buffer))
	{
		if(ereg("gb=([a-zA-Z0-9]+)", $buffer, $tabgb))
		{	
			$gb=$tabgb[1];
			$ligne=">$gb\n";
		}
		else
			$ligne=$buffer;
				
		
	}
	elseif(ereg("^#",$buffer))
	{
			$ligne="";
	}elseif(ereg("^\n",$buffer))
	{
		$ligne="";
	}
	
	elseif(ereg("^[^>#]", $buffer))
	{
		$ligne=$buffer;
	}	
	
	
	
	fputs($fdwrite,$ligne);
	
	$buffer= fgets($fdread, 4096);
}
fclose($fdread);
fclose($fdwrite);
}

if ($format=='y')
{
	//formatdb -i cionaV1.trn.fasta -p F -o T -n Genemodels
	
	//$pathgenemodels="\"".$pathgenemodels."\"";
	
  
	if (strpos($os,"Win")!==false)
	{
	 if (!copy($pathgenemodels, "est_clustering\cpgenesmodels.fasta")) 
	 {
 	 	echo "The copy of $pathgenemodels has failed...<br />\n";
 	 }
		
		if(`blast\formatdb -i est_clustering\cpgenesmodels.fasta -p F -o T -n est_clustering\Genemodels`)
			echo "Formating the database...<br>";
		echo "The copy of $pathgenemodels has failed...<br />\n";
	}
	else
	{
	if (!copy($pathgenemodels, "./est_clustering/cpgenesmodels.fasta")) 
	{
 	 	echo "The copy of $pathgenemodels has failed...<br />\n";
 	}
	if(`./blast/formatdb -i ./est_clustering/cpgenesmodels.fasta -p F -o T -n ./est_clustering/Genemodels`)
		echo "Formating the database...<br>";
	else echo "formatdb ... ... ...<br>";
	}

}

if ($launch=='y')
{
	//	megablast -D 3 -W 10 -F F -d Genemodels -i species-format.txt -s 50 -o MEGABLASTresults.txt	
	if (strpos($os,"Win")!==false)
	{
	
	$exec="blast\megablast -D 3 -W 20 -F F -d est_clustering\Genemodels -i est_clustering\species-format.txt -s 50 -o est_clustering\MEGABLASTresults.txt";
		if(exec($exec))
			echo "Running Megablast<br>";
		else echo "Megablast ... ... ...<br>";
	}
	else
	{
	
	 $exec2="./blast/megablast -D 3 -W 20 -F F -d ./est_clustering/Genemodels -i ./est_clustering/species-format.txt -s 50 -o ./est_clustering/MEGABLASTresults.txt";
		if(exec($exec2))
			echo "Running Megablast<br>";
		else echo "Megablast ... ... ...<br>";
	}

}



$pathlog="./est_clustering/est_clustering_logs.txt";
if(!($fdlog=fopen ($pathlog, "w")))
{
	echo" ERROR : Can't create the file :$pathlog <BR>";
	include("frames_footer.php");
	return;
}
else
{
	echo" The file : $pathlog is being created<BR>";
}




$pathtoread="./est_clustering/MEGABLASTresults.txt";

if(!($fdread=fopen ($pathtoread, "r")))
{
	echo " ERROR : Can't open the file :$pathtoread <BR>";
	include("frames_footer.php");
	return;
}
else
{
	echo " The file to parse : $pathtoread is open <BR>";
}

$clust=array(null);
while(!feof($fdread))
{
	$buffer = fgets($fdread, 4096);
	$buffer=trim($buffer);
	if(ereg("^#", $buffer))
	{	continue;	}
	
	$data=split("\t", $buffer);
	$clone=$data[0];
	$score = $data[11];
    $gene = $data[1];
    $id = $data[2];
    $hsp = $data[3];
	
	//$align[$clone][$gene] += $hsp;
	
	if (!array_key_exists($clone, $clust)) 
	{	$clust[$clone]["score"]=0;	}

	if( ($clust[$clone]["score"]<$score)and($id>90) and ($hsp>40) )
	{
		$clust[$clone]["score"]=$score;
        $clust[$clone]["gene"]=$gene;
        $clust[$clone]["ok"]="y";
		//print "$clone      ".$clust[$clone]["gene"]."        ".$clust[$clone]["score"]."<br>";
		fputs($fdlog,$clone."       ".$clust[$clone]["gene"]."        ".$clust[$clone]["score"]."\n");
	}
	elseif(($clust[$clone]["score"]==$score)and($clust[$clone]["gene"] != $gene) )
	{
		$clust[$clone]["ok"]="n";
		
	}
}

pg_query($c,"begin");

foreach ($clust as $key => $value) 
	if($clust[$key]["ok"]=="y")
	{
		$valclone=$key;
		$valgene= $clust[$key]["gene"];
		//$valscore= $clust[$key]["score"];
		//$valhsp=$align[$key][$clust[$key]["gene"]];
		
//echo " \"$valclone \t $valgene \t $valscore \t $valhsp \" \n" ;
        echo " \"$valclone => \t $valgene<br>" ;
		
		$requete= "update \"CLONE_SEQ\" set \"ANISEED_CLUSTER_ID\"=( select \"GENE_ID\" from \"GENE\" where \"NAME\"='$valgene') where \"GENBANK_ID\"='$key'";
		$cpt++;
		if ($test=='n')
		{

			
		
			$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
				
			

			
			//ocifreestatement($stmt);
		}
		
		if ($test=='y')
		{
			echo "(fake) $requete <br>";
		}
	}

pg_query($c,"commit");

fclose($fdread);
fclose($fdlog);

echo "<BR>Update of $cpt clone_seq <br>";
echo "<BR>Update clones / genes : DONE<BR>";



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