<?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/>.

*/
header('Content-Type: text/plain');
include("connection.php");
//Title before frames_header

?>

<?php

    $geneID=$_GET["name"];	
	
	$requete2="select count(*) AS compte from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"='$geneID'";
	$stmt2 = pg_query($c,$requete2);
	if( $row2=pg_fetch_array($stmt2) ){
	    
    	
		
	    $requete2="select \"GENBANK_ID\", \"CLONE_NAME\", \"END\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"='$geneID' order by \"CLONE_NAME\"";
	    $stmt2 = pg_query($c,$requete2);
		
        
        $count=0;
		while($row2=pg_fetch_array($stmt2)){
		    ++$count;
			$name=$row2["GENBANK_ID"];

//Insertion du calcul des clones a partir de Flatexport-molecule-clone.php
	$requete="select B.\"SPECIES\", A.* from \"CLONE_SEQ\" A, \"MOL_ENTITY\" B where (A.\"NAME\"='$name' or A.\"GENBANK_ID\"='$name') and (B.\"MOL_ID\"=A.\"CLONE_SEQ_ID\")";
	$stmt = pg_query($c,$requete);
	if( $row=pg_fetch_array($stmt) ){
		
		$id=$row["CLONE_SEQ_ID"];
		$nameClone=$row["CLONE_NAME"];

print '#'.$row["GENBANK_ID"]."\n";		
if ($nameClone!=null) print "FEATURE\tCLONE_NAME\t$nameClone\n";
print "FEATURE\tSPECIES\t".$row["SPECIES"]."\n";	
print "FEATURE\tDESCRIPTION\t".$row["COMMENTS"]."\n";	
print "FEATURE\tTYPE\t".$row["TYPE"]."\n";
print "FEATURE\tORIENTATION\t".$row["END"]."\n";		
print "FEATURE\tCLUSTER\t".$row["ANISEED_CLUSTER_ID"]."\n";



		
		
//UNIGENE//		print "<tr valign=\"top\" align=\"left\"><td><strong>Unigene cluster: </strong></td><td><a href=\"ESTcluster.php?name=".OCIresult($stmt,"CLUSTER_ID")."\">".OCIresult($stmt,"CLUSTER_ID")."</a></td></tr>";

    //UNIGENE//	$clust=OCIresult($stmt,"CLUSTER_ID");
		$clust=$row["ANISEED_CLUSTER_ID"];
		$clonename=$row["CLONE_NAME"];
	

	
		//if(! ocicolumnisnull($stmt,"CLONE_NAME")){
			if ($row["CLONE_NAME"]!=null)
			{
		

		    //requete pour les librairies
			$requete="select \"LIBRARY_ID\" from \"CLONE\" where \"CLONE_NAME\"='".$row["CLONE_NAME"]."'";
			$stmt = pg_query($c,$requete);                                                      //on commence par voir s'il existe 1 enregistrement, ensuite on l'affiche
			if($row=pg_fetch_array($stmt)){                                                                  //en passant par 2 requetes separees
			//if(! ocicolumnisnull($stmt,"LIBRARY_ID")){                                        //
			    $requete="select b.\"LIBRARY_ID\", a.\"TITLE\", a.\"TISSUE\", a.\"ORGAN\", a.\"VECTOR\" from \"LIBRARY\" a, \"CLONE\" b where (a.\"LIBRARY_ID\"=b.\"LIBRARY_ID\")and(b.\"CLONE_NAME\"='".$clonename."')";
				$stmt = pg_query($c,$requete);
				//OCIFetch($stmt);
				//if(! ocicolumnisnull($stmt,"LIBRARY_ID")){
				if($row=pg_fetch_array($stmt)){
if ($row["TITLE"]!='') print "FEATURE\tLIBRARY\t".$row["TITLE"]."\n";
if ($row["TISSUE"]!='')print "FEATURE\tTISSUE\t".$row["TISSUE"]."\n";
if ($row["ORGAN"]!='') print "FEATURE\tORGAN\t".$row["ORGAN"]."\n";
if ($row["VECTOR"]!='') print "FEATURE\tVECTOR\t".$row["VECTOR"]."\n";
				    
					
				}
			}
		}

        ////nouvelle version pour utiliser le clustering maison
	    //requete pour gene affect�a ce clone
		$requete="SELECT \"NAME\" FROM \"GENE\" WHERE \"GENE_ID\"='$clust'";
		$stmt = pg_query($c,$requete);
		if( $row=pg_fetch_array($stmt) ){
			print "FEATURE\tCORRESPONDING_GENE\t".$row["NAME"]."\n";
			
    		}
		
        

//////////////////////////////////////////SEQUENCE/////////////////////////////////////////////
		$requete="select \"SEQUENCE\", \"LENGTH\", \"COMMENTS\" from \"SEQUENCE\" where \"MOL_ID\"=$id";
		$stmt = pg_query($c,$requete);
								
    	if( $row=pg_fetch_array($stmt) ){
	    	$lob = $row["SEQUENCE"];

		/* DEBUT DE L'AFFICHAGE DU CLOB */

		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("\n","",$seq);
			$seq = chunk_split($seq, 70, "\n");
			

		
		print "SEQUENCE\tLENGHT\t".$row["LENGTH"]."\n";
		print "SEQUENCE\tCOMMENTS\t".$row["COMMENTS"]."";
		print "SEQ\n";
		print $seq;
		
	    	
		}

	
	print '//'."\n\n";
	
}

		}


	}else{ print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>"; }

?>


<?php
   	
?> 