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


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

	gzwrite($handle,"#gene model // species // interpro // method // position & score\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 \"IP_ID\", \"METHOD\", \"LOCATION\" from \"IP_DESCRIPTION\" where \"MOL_ID\"=".$row["GENE_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["IP_ID"]."\t".$row2["METHOD"]."\t".$row2["LOCATION"]."\n") === FALSE) {
				echo "Error";
				exit;
			}
		
		}
	}


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