<?php
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
include("connection.php");
//Title before frames_header
header("Content-type:text/plain");
?>
<?php

	//connection
	

$clustid=$_GET["clusterid"];
$aniseed_gm=str_replace("cluster","aniseedV3_",$clustid);


//// agregation des modeles de genes
$agreg_gm="";
$requete="SELECT \"NAME\" from \"GENE\" where \"GENE_ID\" in (
select \"GENE_MODEL_ID\" from \"GENE_CONCEPT\" where \"GENE_CLUSTER_ID\"='$clustid'
)";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
if (strlen($agreg_gm)==0) $agreg_gm=$row["NAME"];
else $agreg_gm.=":".$row["NAME"];

}


$requete="
select \"NAME\" from \"GENE\" where \"GENE_ID\" in (
SELECT \"GENE_MODEL_ID\" from \"GENE_CONCEPT\" where \"GENE_CLUSTER_ID\"='$clustid')";

$stmt=pg_query($c,$requete);

$est=(array) null;
while ($row=pg_fetch_array($stmt))
{

///////////////////////////////////// KYOTO ////
if ($row["NAME"]{0}=="K") //KYOTO
{
$temp=explode(".",$row["NAME"]);
$abstract=$temp[0].".".$temp[1].".".$temp[2];
$qu="SELECT \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$abstract' and \"TYPE\"='abstract'";
$st=pg_query($c,$qu);
$ro=pg_fetch_array($st);

$kyotoid=$ro["MOL_ID"];

$requete0="select \"CLONE_SEQ_ID\" from \"CLONE_SEQ\" where \"ANISEED_KYOTO_CLUSTER_ID\"='$kyotoid'";
$stmt0=pg_query($c,$requete0);
while ($row0=pg_fetch_array($stmt0)) $est[$row0["CLONE_SEQ_ID"]]++;

}

///////////////////////////////////// ENSEMBL ////
if ($row["NAME"]{0}=="E")
	{
	$qu="SELECT \"ABSTRACT_ID\" from \"TRANSCRIPT\" where \"TRANSCRIPT_ID\"=(select \"GENE_ID\" from \"GENE\" where \"NAME\"='".$row["NAME"]."')";
	$st=pg_query($c,$qu);
	$ro=pg_fetch_array($st);

 	$ensid=$ro["ABSTRACT_ID"];

	$requete0="select \"CLONE_SEQ_ID\" from \"CLONE_SEQ\" where \"ANISEED_ENSEMBL_CLUSTER_ID\"='$ensid'";
	$stmt0=pg_query($c,$requete0);
	while ($row0=pg_fetch_array($stmt0)) $est[$row0["CLONE_SEQ_ID"]]++;

	}

////////////////////////////////////// JGI

if ($row["NAME"]{0}=="c")
	{
	$requete0="SELECT \"CLONE_SEQ_ID\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where \"NAME\"='".$row["NAME"]."')";
	$stmt0=pg_query($c,$requete0);
	while ($row0=pg_fetch_array($stmt0)) $est[$row0["CLONE_SEQ_ID"]]++;
	}



}



$requete="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$clustid'";
$stmt=pg_query($c,$requete);
$gm_name="";
while ($row=pg_fetch_array($stmt)) if (strlen($gm_name)==0) $gm_name=$row["GENE_CLUSTER_NAME"]; else $gm_name.=" / ".$row["GENE_CLUSTER_NAME"];

$requete="select distinct \"SPECIES\" from \"MOL_ENTITY\" where \"MOL_ID\" in (SELECT \"GENE_MODEL_ID\" from \"GENE_CONCEPT\" where \"GENE_CLUSTER_ID\"='$clustid')";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$species=$row["SPECIES"];


$count=0;

foreach ($est as $key => $val)
{
$requete="SELECT \"GENBANK_ID\",\"CLONE_NAME\",\"END\" from \"CLONE_SEQ\" where \"CLONE_SEQ_ID\"='$key'";
$stmt = pg_query($c,$requete);
$row=pg_fetch_array($stmt);

$gb=$row["GENBANK_ID"];
$cname=$row["CLONE_NAME"];
$end=$row["END"];

$requete="select \"SEQUENCE\", \"LENGTH\", \"COMMENTS\", \"MOL_ID\" from \"SEQUENCE\" where \"MOL_ID\"=$key";
$stmt = pg_query($c,$requete);
if($row=pg_fetch_array($stmt)){
$lob = $row["SEQUENCE"];
pg_query ($c, "begin");	/*Debut de la transaction*/
$handle_lo = pg_lo_open($c,$lob,"r");
$seq=pg_lo_read($handle_lo);
pg_query($c,"end"); /*fin de la Transaction*/
$seq=ereg_replace("<br>","",$seq);
$seq=ereg_replace("\r\n","",$seq);
$seq=ereg_replace("\n","",$seq);
$seq = chunk_split($seq, 70, "\n");
}

$header="$gb";
if (strlen($cname)>0) $header.=" | ".$cname;

print ">$header\n$seq\n\n";



/*
print "<tr align=\"center\"><!--<td><input name=\"id[]\" type=\"checkbox\" value=\"".$row["GENBANK_ID"]."\"></td>--><td width=\"33%\"><a href=\"molecule-clone.php?name=".$row["GENBANK_ID"]."\"><font color=\"#000000\" size=\"-1\">".$row["CLONE_NAME"]."</font></a></td><td width=\"33%\"><a href=\"molecule-clone.php?name=".$row["GENBANK_ID"]."\"><font color=\"#000000\" size=\"-1\">".$row["GENBANK_ID"]."</font></a></td><td width=\"33%\"><font color=\"#656565\" size=\"-1\">".$row["END"]."</font></td></tr>";
*/



}




// print "&nbsp;&nbsp;&nbsp;<img border=\"0\" src=\"pictures/button-selectALL.jpg\" onClick=\"CheckALL();\">";





?>
