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


	if (!$handle = gzopen("ortholog.gz", "wb9")) {
	   echo "Impossible d'ouvrir le fichier";
	   exit;
	}

	gzwrite($handle,"#gene model // species // ortholog name // ortholog id // score // protein familly // species\n");

    $count=0;
	$count2=0;
    $requete= "select a.\"NAME\", a.\"GENE_ID\", b.\"SPECIES\" from \"GENE\" a, \"MOL_ENTITY\" b where b.\"MOL_ID\"=a.\"GENE_ID\" order by b.\"SPECIES\"";
	$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	while( $row=pg_fetch_array($stmt) ){
	    ++$count;
		
	    $requete="select * from \"PHYLOGENY\" where \"MOL_ID\"=".$row["GENE_ID"]." and \"ORTHOLOG\" is not null";
    	$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
    	while($row2=pg_fetch_array($stmt2)){
		    ++$count2;
		    if (gzwrite($handle,$row["NAME"]."\t".$row["SPECIES"]."\t".$row2["ID"]."\t".$row2["ORTHOLOG"]."\t".$row2["SCORE2"]."%\t".$row2["FAMILLY"]."\t".$row2["SPECIES"]."\n") === FALSE) {
				echo "Error: Impossible d'�rire dans le fichier!";
				exit;
			}
		}
		
	}


	gzclose($handle);
	print "Ok. $count genes. $count2 orthologs written on ortholog.gz";
	
	include("frames_footer.php");
?>