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


	if (!$handle = gzopen("GO-annotations.gz", "wb9")) {
	   echo "Error";
	   exit;
	}

	gzwrite($handle,"#gene models // species // GO // GO name\n");


    $count=0;
	$count2=0;
	$requete= "select a.\"NAME\", b.\"SPECIES\", a.\"GENE_ID\" 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 A.\"GO_ID\", B.\"GO_TERM\" from \"GO_DESCRIPTION\" A, \"GO\" B where A.\"MOL_ID\"=".$row["GENE_ID"]." and A.\"GO_ID\"=B.\"GO_ID\"";
		$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["GO_ID"]."\t".$row2["GO_TERM"]."\n") === FALSE) {
				echo "Error";
				exit;
			}
		
		}
	}


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