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


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

	gzwrite($handle,"#gene models // species // clone count // expression level (/oo)// library size // library type\n");


$requete= "select \"COMMENTS\", sum(\"LIB_SIZE\") as \"SUM\" from \"LIBRARY\" where \"SPECIES\"='Ciona intestinalis' group by \"COMMENTS\"";
$stmt66 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while( $row66=pg_fetch_array($stmt66) ){
	$type=$row66["COMMENTS"];
	$libsize=$row66["SUM"];
	print $type."  ".$libsize."<br>";

    $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\" and \"SPECIES\"='Ciona intestinalis' order by b.\"SPECIES\"";
	$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	while( $row=pg_fetch_array($c,$requete) ){
	    ++$count;
		$requete= "select a.* from \"DDD\" a, \"LIBRARY\" b where a.\"GENE_ID\"=".$row["GENE_ID"]." and a.\"LIBRARY_ID\"=b.\"LIBRARY_ID\" and b.\"COMMENTS\"='$type'";
		$stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		//$libsize=0;
		$clonecount=0;
		//$alllibid="-";
		while( $row2=pg_fetch_array($stmt2) ){
		    //$libsize += OCIResult($stmt2, "LIB_SIZE");
			$clonecount += $row2["CLONE_COUNT"];
			//$alllibid.=OCIResult($stmt2, "LIBRARY_ID")."-";
		}
		
		$explev=0;
		if($libsize>0)
			$explev=round((($clonecount/$libsize)*1000),4);
			
		++$count2;
		if (gzwrite($handle, $row["NAME"]."\t".$row["SPECIES"]."\t".$clonecount."\t".$explev."\t".$libsize."\t".$type."\n") === FALSE) {
			echo "Error";
			exit;
		}

		
		
	}
}

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