<?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');

//On definit le header comme un fichier XML, le flux sera du XML
//donc ne pas mettre de html
?>
<title>ANISEED v3.0 - Export Gene model card</title>
<?
include ("frames_header.php");


include("connection.php");

?>

<?php

 $name=$_GET["name"];
print "<title>ANISEED - Export $name</title>";

?>

<?

	$requete="select A.*, B.\"SPECIES\" from \"GENE\" A, \"MOL_ENTITY\" B where (A.\"NAME\"='$name')and(A.\"GENE_ID\"=B.\"MOL_ID\")";
	$stmt = pg_query($c,$requete);

	if($row=pg_fetch_array($stmt)){
	
	    $id=$row["GENE_ID"];
	    $species=$row["SPECIES"];
		
$clustid=$id;

if ($row["NAME"]{0}=="K") //KYOTO
{
//AJOUT POUR L'ABSTRACT de KYOTO

$qu="SELECT \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\"='$id'";
$st=pg_query($c,$qu);
$ro=pg_fetch_array($st);

$temp=explode(".",$ro["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);

	$clustid=$ro["MOL_ID"];
		//print "<tr valign=\"top\" align=\"left\"><td><font color=\"#656565\" size=\"-1\"><b>Corresponding cluster: </b></font></td><td>";
		//print "<a href=\"ESTcluster.php?name=".$ro["MOL_ID"]."\"><font color=\"#000000\" size=\"-1\">".$ro["MOL_ID"]."</font></a></td></tr>";
//FIN AJOUT
}

if ($row["NAME"]{0}=="E")
	{
	//POUR ENSEMBL

	$qu="SELECT \"ABSTRACT_ID\" from \"TRANSCRIPT\" where \"TRANSCRIPT_ID\"='$id'";
	$st=pg_query($c,$qu);
	$ro=pg_fetch_array($st);

 	$clustid=$ro["ABSTRACT_ID"];
	}


?>

<!-- Header pour l'onglet -->
  <link rel="stylesheet" type="text/css" media="screen" href="ongletv1.css" title="Version 1" />

<div id="header">
  <ul>
    <li><a href="molecule-gene.php?name=<?print $name?>">Transcript model card</a></li>
    <li><a href="molecule-sequence.php?id=<? print $id; ?>">Sequence</a></li>
    <li><a href="ESTcluster.php?name=<? print $clustid; ?>&base=<?print $id;?>">ESTs cluster</a></li>
    <li><a href="est-expression.php?name=<?print $clustid;?>&base=<? print $id; ?>&species=<?print $species;?>">EST count</a></li>
<!--    <li><a href="expression-curve.php?name=<? print $name;?>">Expression profile</a></li>-->
    <li><a href="insitu-result.php?target=<? print $name;?>&MOLtype=2&Order=DEV_STAGE_ID">InSitu data</a></li>
    <!--<li><a href="moltool.php?target=<? print $name; ?>">Molecular tool</a></li>-->
    <!--<li><a href="#" style="color:white;">Microarray probe</a></li>-->

    <li id="current"><a href="FLATexport-molecule-gene.php?name=<? print $name; ?>">Export</a></li>
  </ul>
</div>
<!-- EOF Onglet -->
<br><br>
<div style="border:1px solid black;padding:10px;margin:10px;background-color:lightyellow;color:black;">

<?


print "<br><br><br>#$name<br>";
	

		
	    $requete2="select \"SYNO\" from \"SYNO\" where \"GENE_ID\"=$id and \"ORIGIN\"='user-JGI'";
	    $stmt2 = pg_query($c,$requete2);
		
       
		$a=0;
		while($row2=pg_fetch_array($stmt2)){
			   if(++$a>0)
	
		      // print $row2["SYNO"];
			print "FEATURE\tPOSSIBLE_BIO_NAME_MANUAL\t".$row2["SYNO"]."<br>";
	
		}
	
		
		
	    $requete2="select \"SYNO\" from \"SYNO\" where \"GENE_ID\"=$id and \"ORIGIN\"='phylogeny'";
	    $stmt2 = pg_query($c,$requete2);
		
		$a=0;
		while($row2=pg_fetch_array($stmt2)){
		   
				if(++$a>0)
print "FEATURE\tPOSSIBLE_BIO_NAME\t".$row2["SYNO"]."<br>";
		
		}
		if($a==0){
			$requete2="select \"DESCRIPTION\", \"EVALUE\" from \"BLAST\" where \"MOL_ID\"=$id and \"SCORE\"=(select max(\"SCORE\") from \"BLAST\" where \"MOL_ID\"=$id)";
		
			$stmt2 = pg_query($c,$requete2);
			if($row2=pg_fetch_array($stmt2)){
			    $eval = $row2["EVALUE"];

			$b=0;
			  if( $eval <= 1e-10)
				{
				print "FEATURE\tPOSSIBLE_BIO_NAME\t";
				    print "Highly ";
				$b++;
				}
				else if( ($eval >= 1e-4) and ($eval <= 1e-3) )
				{
				print "FEATURE\tPOSSIBLE_BIO_NAME\t";
				    print "Weakly ";
				$b++;
				}
				if($eval <= 1e-3)
				{
				if ($b==0) print "FEATURE\tPOSSIBLE_BIO_NAME\t";
				print "Similar to ".$row2["DESCRIPTION"]."\tEVAL\t$eval<br>";
				

				}
			}
		}
		
	
	

	
		
if ($row["COMMENTS"]!="") print "FEATURE\tDESCRIPTION\t".$row["COMMENTS"]."<br>";
		
		
if ($row["SPECIES"]!="") print "FEATURE\tSPECIES\t".$row["SPECIES"]."<br>";
		
        
if ($row["SCAFFOLD_ID"]!="") print "FEATURE\tSCAFFOLD\t".$row["SCAFFOLD_ID"]."<br>";
		
	
if ($row["CHR_NUM"]!="") print "FEATURE\tCHR-NUMBER\t".$row["CHR_NUM"]."<br>";
		

if ($row["STRAND"]!="") print "FEATURE\tSTRAND\t".$row["STRAND"]."<br>";
		
if ($row["BEGIN"]!="") print "FEATURE\tBEGIN\t".$row["BEGIN"]."<br>";

if ($row["END"]!="") print "FEATURE\tEND\t".$row["END"]."<br>";

if ($row["INTRON_EXON"]!="") print "FEATURE\tEXON\t".$row["INTRON_EXON"]."<br>";

		
print "FEATURE\tCLUSTER_ID\t$id<br>";
		
		
	
	   
	 $requete2="select a.\"NAME\" from \"GENE_REGULATION\" b, \"PROMOTER\" a where a.\"PROMOTER_ID\"=b.\"PROMOTER_ID\" and b.\"GENE_ID\"=$id";
		$stmt2 = pg_query($c,$requete2);	
        while($row2=pg_fetch_array($stmt2)){
	
print "FEATURE\tPROMOTER_REGION\t".$row2["NAME"]."<br>";
		
		}
		
		if($row["SPECIES"] == "Ciona intestinalis"){
			print "FEATURE\tGBROWSE_LINK\thttp://crfb.univ-mrs.fr/ciona-bin/gbrowse/intestinalis?name=$name<br>";
							
	    
		}
		

		
		
///////////////////////////////////////////INTERPRO////////////////////////////////////////////

		

		$requete="select \"LENGTH\" from \"SEQUENCE\" where \"MOL_ID\"=$id";
		$stmt = pg_query($c,$requete);
		
		$row=pg_fetch_array($stmt);
		
		$length=$row["LENGTH"];


		$requete="select A.*, B.\"NAME\" from \"IP_DESCRIPTION\" A, \"INTERPRO\" B where A.\"MOL_ID\"='$id' and A.\"IP_ID\"=B.\"IP_ID\"";
	    $stmt = pg_query($c,$requete);
		
        $count=0;
		while($row=pg_fetch_array($stmt)){
		
		    $tab[$row["METHOD"]][$row["IP_ID"]]["location"]=$row["LOCATION"];
		    $tab[$row["METHOD"]][$row["IP_ID"]]["IPID"]=$row["IP_ID"];
			$tab[$row["METHOD"]][$row["IP_ID"]]["DBID"]=$row["DB_ID"];
			$tabname[$row["IP_ID"]]=$row["NAME"];
			
			++$count;
		
		}
		
		if($count>0){
		
			
			foreach ($tab as $method => $dbid){		
				
			
		

			    $a=0;
				foreach ($dbid as  $data){
				    
					++$a;
		
print "IPRSCAN\tMETHOD\t$method\tNAME\t".$tabname[$data["IPID"]]."\tID\t".$data["IPID"]."\tPOSITION_SCORE\t".$data["location"]."<br>";

	
				}

		
			}
			
		
		
		}

		

/////////////////////////////////////////////////ORTHOLOG/////////////////////////////////////////////////



	 $requete="select * from \"PHYLOGENY\" where \"MOL_ID\"=$id and \"ORTHOLOG\" is not NULL order by \"SPECIES\", \"ORTHOLOG\", \"SCORE2\" DESC";
    	$stmt = pg_query($c,$requete);
		$count=0;
    	while($row=pg_fetch_array($stmt)){
		    ++$count;
			
print "ORTHOLOG\tSPECIES\t".$row["SPECIES"]."\tGENE\t".$row["ORTHOLOG"]."\tID\t".$row["ID"]."\tSCORE\t".$row["SCORE"]."\tFAMILLY\t".$row["FAMILLY"]."<br>";
				
		}
		
	
/////////////////////////////////////////////////PARALOG/////////////////////////////////////////////////
 $requete="select * from \"PHYLOGENY\" where \"MOL_ID\"=$id and \"PARALOG\" is not NULL order by \"SPECIES\", \"SCORE1\" DESC";
    	$stmt = pg_query($c,$requete);
		$count=0;
    	while($row=pg_fetch_array($stmt)){
		    ++$count;
		    print "PARALOG\tNAME\t".$row["PARALOG"]."\tSCORE\t".$row["SCORE2"]."%<br>";
		   
		}
		if ($count!=0) print "PARALOG\tNAME\t$name\tSCORE\t".$row["SCORE1"]."%<br>";	
		



	
//////////////////////////////////////////Best BLAST hits///////////////////////////////////////////


	    
        $requete="select \"SCORE\", \"PROTEIN\", \"EVALUE\", \"BLOCKS\", \"DESCRIPTION\", \"ID\" from \"BLAST\" where \"MOL_ID\"=$id and \"DESCRIPTION\" is not NULL order by \"SCORE\" DESC";        	
		
		$stmt = pg_query($c,$requete);
		$count=0;
    	while($row=pg_fetch_array($stmt)){
		    ++$count;
		
print "BBH\tPROTEIN\t".$row["PROTEIN"]."\tSCORE\t".$row["SCORE"]."\tEVALUE\t".$row["EVALUE"]."\tDESCRIPTION\t".$row["DESCRIPTION"]."<br>";
	

		
		}
	
	

////////////////////////////////////////////////GO//////////////////////////////////////////////////
	   
	


		$requete2="select B.\"SPECIES_ORIGIN\", A.\"GO_ID\", A.\"GO_TERM\", A.\"GO_TYPE\" from \"GO\" A, \"GO_DESCRIPTION\" B where (B.\"MOL_ID\"=$id) and (B.\"GO_ID\"=A.\"GO_ID\") order by A.\"GO_TYPE\", \"GO_ID\"";
	    $stmt2 = pg_query($c,$requete2);
	
		$count=0;
		while($row2=pg_fetch_array($stmt2)){
		    ++$count;
		   
			print "GO\tID\t".$row2["GO_ID"]."\tORIGIN\t".$row2["SPECIES_ORIGIN"]."\tTYPE\t".$row2["GO_TYPE"]."\tTERM\t".$row2["GO_TERM"]."<br>";
			

				
		}
		
//////////////////////////////////////////SEQUENCE/////////////////////////////////////////////
		$requete="select \"SEQUENCE\", \"LENGTH\", \"COMMENTS\", \"MOL_ID\" 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*/

		/* FIN DE L'AFFICHAGE DU LOB */



			$seq=ereg_replace("<br>","",$seq);
			$seq=ereg_replace("\n","",$seq);
			$seq = chunk_split($seq, 70, "<br>");
			

		   print "SEQUENCE\tLENGHT\t".$row["LENGTH"]."<br>";
		print "SEQUENCE\tCOMMENTS\t".$row["COMMENTS"]."";
		print "SEQ<br>";
		print $seq;
		}
	
//////////////////////////////////////////PROT_SEQUENCE/////////////////////////////////////////////
		$requete="select \"SEQUENCE\", \"LENGTH\", \"MOL_ID\" from \"PROT_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*/

		/* FIN DE L'AFFICHAGE DU LOB */



			$seq=ereg_replace("<br>","",$seq);
			$seq=ereg_replace("\n","",$seq);
			$seq = chunk_split($seq, 70, "<br>");
			

		print "PROT_SEQUENCE\tLENGHT\t".$row["LENGTH"]."<br>";
		//print "SEQUENCE\tCOMMENTS\t".$row["COMMENTS"]."";
		print "PROT_SEQ<br>";
		print $seq;
		}
	


	
	
print "//";
	
?>
</div>
	
<?
	}else{ print "NO_MATCH_FOUND<br>//";  }


include ("frames_footer.php");
?>
