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

<?php

if (isset($_POST["speciesaniseed"]))
{
	$speciesaniseed=$_POST["speciesaniseed"];
  //echo "species aniseed : $speciesaniseed";
}
else 
{
 echo "<h2>Error : Problem with the species.</h2>";
 include("frames_footer.php");
 return;
}


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

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

$filetowrite="./updates/inparanoid/Results_Inparanoid_loader.txt";


if($species=="")
{
	echo"ERROR: Please select a species ";
	include("frames_footer.php");
	return;
}

if($path=="")
{
	echo"ERROR: Please select a file";
	include("frames_footer.php");
	return;
}

if(!($fd=fopen ($path, "r")))
{
	echo" ERROR : Cannot open the file :$path ";
	include("frames_footer.php");
	return;
}


/////////////////////////////////////////////////////////
if(!($fd2=fopen ($filetowrite, "w")))
{
	echo" ERROR : Cannot create the file :$filetowrite";
	include("frames_footer.php");
	return;
}

////////////////////////////////////////////////////////////
$c=0;
$cptligne=1;
$sizeci=0;
$sizeor=0;
$sizetotci=0;
$sizetotor=0;


// to get all the id of the orthologs

//split of the inparanoid file : to get first all the id of the species in aniseed

$buffer = fgets($fd, 4096);
while(!feof($fd))
{
	$cptligne++;
	$buffer = fgets($fd, 4096);
	$tab=split("\t",$buffer);
	$score=$tab[1];
	$ci=split(" ",trim($tab[2]));
	$or=split(" ",trim($tab[3]));
	
	$sizeci=count($ci); //size of the tab containing all the first id
	$sizeor=count($or); //size of the tab containing all the second id
	
	// to put all the Ensembl results of the file in an other file GenesEnsembl 

	for ($i=0;$i<$sizeor;$i+=2)
	{			
			fputs($fd2,$or[$i].", ");		
	}
	$sizetotci=$sizetotci+$sizeci;
	$sizetotor=$sizetotor+$sizeor;
}

echo"$cptligne lines<br>";
echo (($sizetotci-1)/2)." genes <br>";
echo (($sizetotor-1)/2)." orthologs <br>";

fclose($fd);
fclose($fd2);


?>


<table>
<TR>
	<td><p><font size=5 >Go to <a href="http://www.ensembl.org/Multi/martview" target="_blank">Ensembl EnsMart web page</a></font></td>
</TR><TR>
	<td>&nbsp;</td>
</TR><TR>

<?php
//switch($species)
//{
//	case "Homo sapiens" :
		echo" <br>
		<td><hr size=\"1\"></td>
		</TR><TR>
		<td>
			<font size=4 face=\"arial, sans-serif\">For Homo sapiens species:</font>
		</td>
		</TR><TR>
			<td> To get a compatible results file you must select the following options:<br>
				<DL>
				<DT>1/ \"START\"</DT>
					<DD>Select in the Ensembl database.<br>
					and $species for the dataset. <br>
					Click next.</DD>	
				<DT>2/ \"FILTER\"</DT>	
					<DD>
					In the \"GENE\" area check the \"Entries with following IDs\" option and select \"Ensembl peptide ID(s)\" from the combo box. Then select the \"ANISEED-manager\updates\inparanoid\Results_Inparanoid_Loader.txt\" file for the filepath.<br>
					Then click next.</DD>
				<DT>3/ \"OUTPUT\"</DT>
					<DD> Check \"Ensembl peptide ID\" and \"Description\" from the \"Ensembl Attributes\" panel<br>
					 Check \"HUGO ID\", \"Flybase Gene ID\" and \"FlyBase Symbol ID\" (when available), \"SPTrEMBL ID\", \"SWISSPROT ID\" from the \"External References\" panel<br>
					 Check \"Family Description\" from \"PROTEIN\" in the \"Family Attributes area\"<br>
					 
					 Check \"Text\", \"tab separated\" and \"gzip (.gz)\" for the file output format.<br>
					 Finally choose a name for the output file (eg: \"HSEnsMartResults\").<br>
					 Click export.</DD>						 
				</DL>
			</td>
		</TR><TR>
			<td>Select the path of the dowloaded file in the field above.</td>
		</TR><TR>
			<td><hr size=\"1\"></td>
		</TR><TR>
		 ";
/*
			break;
	case "Mus musculus" :
		echo" <br>
		<td><hr size=\"1\"></td>
		</TR><TR>
		<td>
			<font size=4 face=\"arial, sans-serif\">For Mus musculus species:</font>
		</td>
		</TR><TR>
			<td> EnsMart is composed into 3 stages. To get a compatible results file you must check or uncheck in each stages all the following options:<br>
				<DL>
				<DT>First stage : \"START\"</DT>
					<DD>Select in the \"FOCUS\" area Ensembl Genes.<br>
					Select Mus musculus for the species. <br>
					Click next.</DD>	
				<DT>Second stage : \"FILTER\"</DT>	
					<DD>In the \"REGION\" area uncheck \"Limit to (uncheck for entire genome)\" option.<br>
					In \"GENE\" area choose Ensembl peptide ID(s) from the combo box and give the filepath of the Inparanoid Loader script (Results_Inparanoid_Loader.txt).<br>
					Then click next.</DD>
				<DT>Third stage : \"OUTPUT\"</DT>
					<DD> Ensembl peptide ID, Description in the \"GENE\" area from \"Ensembl Attributes\"<br>
					 SWISSPROT ID, SPTrEMBL ID from the \"External Reference Attributes\"<br>
					 and from \"PROTEIN\" in the Family Attributes area , Family Description<br>
					 must be checked.<br>
					 Next for the output format file \"Text, tab separated\", \"gzip (.gz)\" need to be checked.<br>
					 Enter \"MMEnsMartResults\" to provide a name for file download.<br>
					 Then click export.</DD>						 
				</DL>
			</td>
		</TR><TR>
			<td>Next download the file and give the path of this file under.</td>
		</TR><TR>
			<td><hr size=\"1\"></td>
		</TR><TR>
		 ";
			break;
	case "Drosophila melanogaster" :
		echo" <br>
		<td><hr size=\"1\"></td>
		</TR><TR>
		<td>
			<font size=4 face=\"arial, sans-serif\">For Drosophila melanogaster species:</font>
		</td>
		</TR><TR>
			<td> EnsMart is composed into 3 stages. To get a compatible results file you must check or uncheck in each stages all the following options:<br>
				<DL>
				<DT>First stage : \"START\"</DT>
					<DD>Select in the \"FOCUS\" area Ensembl Genes.<br>
					Select Drosophila melanogaster for the species. <br>
					Click next.</DD>	
				<DT>Second stage : \"FILTER\"</DT>	
					<DD>In the \"REGION\" area uncheck \"Limit to (uncheck for entire genome)\" option.<br>
					In \"GENE\" area choose Ensembl peptide ID(s) from the combo box and give the filepath of the Inparanoid Loader script (Results_Inparanoid_Loader.txt).<br>
					Then click next.</DD>
				<DT>Third stage : \"OUTPUT\"</DT>
					<DD> Ensembl peptide ID in the \"GENE\" area from \"Ensembl Attributes\"<br>
					 SWISSPROT ID, Flybase Gene ID, FlyBase Symbol ID from the \"External Reference Attributes\"<br>
					 and from \"PROTEIN\" in the Family Attributes area , Family Description<br>
					 must be checked.<br>
					 Next for the output format file \"Text, tab separated\", \"gzip (.gz)\" need to be checked.<br>
					 Enter \"DMEnsMartResults\" to provide a name for file download.<br>
					 Then click export.</DD>						 
				</DL>
			</td>
		</TR><TR>
			<td>Next download the file and give the path of this file under.</td>
		</TR><TR>
			<td><hr size=\"1\"></td>
		</TR><TR>
		 ";
			break;
	

}
*/
?>
	
</TR><TR>
	<form target="_blank" action="inparanoid-loader3.php" method="post" name="formulaire" onsubmit="this.fileEnsmart.value=this._fileEnsmart.value" >
	<?php 
		print'<input type="hidden" name="species" value="'.$_POST["species"].'">'; 
		print'<input type="hidden" name="speciesaniseed" value="'.$_POST["speciesaniseed"].'">'; 
		print'<input type="hidden" name="filepath" value="'.$_POST["filepath"].'">'; 
	?>
	<table>
	<TR>
		<td><input type="checkbox" name="test" value="y" checked >Perform read test whithout recording data.</td>
	</TR><TR>
		<td>&nbsp;</td>
	</TR><TR>
		<td>Phylogeny Update:</td>
	</TR><TR>
		<td><input type="checkbox" name="updateorto" value="y" checked >Update orthologues.</td>
	</TR><TR>
		<td><input type="checkbox" name="updatepara" value="y" checked >Update paralogues.</td>
	</TR><TR>
		<td>&nbsp;</td>
	</TR><TR>
		<td><input type="checkbox" name="updatebioname" value="y" checked >Update biological name.</td>
	</TR><TR>
		<td><input type="checkbox" name="updatego" value="y" checked >Update GO.</td>
	</TR><TR>
		<td>&nbsp;</td>
	</TR><TR>
		<td><hr size=\"1\"></td>
	</TR><TR>
		<td>&nbsp;</td>
	</TR><TR>
		<td>Ensmart results file :&nbsp;
			<input type="file" name="_fileEnsmart">
			<input type="hidden" name="fileEnsmart">
			
		</td>
	</TR><TR>
		<td><input name="submit" type="image" src="pictures/button-submit.jpg" onClick="submit"></td>
	</TR>
	</table>
	</form>
</TR>
</table>







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