<?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
?>
<title>ANISEED v3.0 - ESTs cluster</title>
<?
include("frames_header.php");
?>



<SCRIPT>
function CheckALL() {
		for(a=0; a<document.form.length; ++a){
			document.form[a].checked=!(document.form[a].checked);
		}
}
</SCRIPT>



<?php

	//connection
	

    $geneID=$_GET["name"];	


print '<title>ANISEED - EST cluster : '.$geneID.'</title>';


	$base=$_GET["base"];
    //$requete="SELECT \"NAME\",\"SPECIES\" from \"MOL_ENTITY\" where \"MOL_ID\"='$geneID'";
if (strlen($base)>0)
{
	$requete="SELECT \"NAME\",\"SPECIES\" from \"MOL_ENTITY\" where \"MOL_ID\"='".$_GET["base"]."'";
    $stmt=pg_query($c,$requete);
	$row=pg_fetch_array($stmt);
$name=$row["NAME"];
$species=$row["SPECIES"];
}
$id=$geneID;
	
$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"];
//	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["ABSTRACT_ID"]."\"><font color=\"#000000\" size=\"-1\">".$ro["ABSTRACT_ID"]."</font></a></td></tr>";
	}


if (strlen($base)>0)
{
?>

<!-- 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 $base; ?>">Sequence</a></li>
    <li id="current"><a href="#">ESTs cluster</a></li>
    <li><a href="est-expression.php?name=<?print $geneID;?>&base=<? print $base; ?>&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><a href="FLATexport-molecule-gene.php?name=<? print $name; ?>">Export</a></li>

  </ul>
</div>
<!-- EOF Onglet -->

<?
} //Fin du if strlenbase



	//$requete2="select count(*) AS compte from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"='$geneID'";
	$requete2="select count(*) AS compte from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"='$geneID' or \"ANISEED_KYOTO_CLUSTER_ID\"='$geneID' or \"ANISEED_ENSEMBL_CLUSTER_ID\"='$geneID'";
	$stmt2 = pg_query($c,$requete2);
	
	if( $row2=pg_fetch_array($stmt2) ){
	    
    	print "<br><br><br><br><font color=\"#D64242\" size=\"3\"><b>Cluster &nbsp;&nbsp;&nbsp;".$geneID."</b></font><br>";
    
    	print "<br><font color=\"#456898\" size=\"3\"><b><u>Features</u></b></font><br><br>";
        print "<table width=\"100%\"><tr valign=\"top\" align=\"left\"><td width=\"20%\"><font color=\"#656565\" size=\"-1\"><b>Name: </b></font></td><td><font color=\"#656565\" size=\"-1\">".$geneID."</font></td></tr>";
        print "<tr valign=\"top\" align=\"left\"><td><font color=\"#656565\" size=\"-1\"><b>Composition: </b></font></td><td><font color=\"#656565\" size=\"-1\">".$row2["compte"]." sequences</font></td></tr>";        


		$requete="SELECT \"NAME\" FROM \"GENE\" WHERE \"GENE_ID\"='$geneID'";
		$stmt = pg_query($c,$requete);
		
		if( $row=pg_fetch_array($stmt) ){
			print "<tr valign=\"top\" align=\"left\"><td><font color=\"#656565\" size=\"-1\"><b>Corresponding gene: </b></font></td><td><a href=\"molecule-gene.php?name=".$row["NAME"]."\"><font color=\"#000000\" size=\"-1\">".$row["NAME"]."</font></a></td></tr>";
        }
		else
				{

			//KYOTO
			$requete="SELECT \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\"='$geneID'";
			$stmt=pg_query($c,$requete);
				if( $row=pg_fetch_array($stmt) ){
					$abstract=$row["NAME"];
					$requete2="SELECT \"NAME\" from \"MOL_ENTITY\" where \"NAME\" like '$abstract.%' and \"NAME\"<>'$abstract'";
					$stmt2=pg_query($c,$requete2);
					if (pg_num_rows($stmt2)>0)
					{
					while ($row2=pg_fetch_array($stmt2))
						{
						print "<tr valign=\"top\" align=\"left\"><td><font color=\"#656565\" size=\"-1\"><b>Corresponding gene: </b></font></td><td><a href=\"molecule-gene.php?name=".$row2["NAME"]."\"><font color=\"#000000\" size=\"-1\">".$row2["NAME"]."</font></a></td></tr>";
						}
					}
					else
					{
					//ENSEMBL
					
					$requete="select \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\" in (SELECT \"TRANSCRIPT_ID\" from \"TRANSCRIPT\" where \"ABSTRACT_ID\"='$geneID')";
					$stmt=pg_query($c,$requete);
					while ($row=pg_fetch_array($stmt))
						{
						print "<tr valign=\"top\" align=\"left\"><td><font color=\"#656565\" size=\"-1\"><b>Corresponding gene: </b></font></td><td><a href=\"molecule-gene.php?name=".$row["NAME"]."\"><font color=\"#000000\" size=\"-1\">".$row["NAME"]."</font></a></td></tr>";
						}



					}


				}
				

			}
		
		$requete="SELECT \"SPECIES\" FROM \"MOL_ENTITY\" WHERE \"MOL_ID\"='$geneID'";
		$stmt = pg_query($c,$requete);
		
		if( $row=pg_fetch_array($stmt) ){
		    $species=$row["SPECIES"];
			print "<tr valign=\"top\" align=\"left\"><td><font color=\"#656565\" size=\"-1\"><b>Species: </b></font></td><td><font color=\"#656565\" size=\"-1\">".$species."</font></td></tr>";
        }
		
		
		print "</table>";		
		
		
		//print "<br><center><a href=\"est-expression.php?name=$geneID&species=$species\"><img src=\"pictures/button-library.jpg\" border=\"0\"></a></center>";
        print "<br><br><font color=\"#456898\" size=\"3\"><b><u>Cluster composition</u></b></font><br><br>";
		
	    //$requete="select \"GENBANK_ID\", \"CLONE_NAME\", \"END\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"='$geneID' order by \"CLONE_NAME\"";
	$requete="select \"GENBANK_ID\", \"CLONE_NAME\", \"END\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"='$geneID' OR \"ANISEED_KYOTO_CLUSTER_ID\"='$geneID' or \"ANISEED_ENSEMBL_CLUSTER_ID\"='$geneID' order by \"CLONE_NAME\"";

	    $stmt = pg_query($c,$requete);
		
        print '<form name="form" action="AllClonesFASTA.php" target="_BLANK" method="POST"><table width="50%" border="0" cellspacing="0" cellpadding="0">';
        print '<tr align="center" valign="middle"><td>&nbsp;</td><td><font color="#456898" size="2"><b>Clone name</b></font></td><td><font color="#456898" size="2"><b>Genbank ID</b></font></td><td><font color="#456898" size="2"><b>End</b></font></td></tr>';
        $count=0;
		while($row=pg_fetch_array($stmt)){
		    //++$count;
	        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>";
			/*if($count>50){
			    //OCICancel($stmt);
				//pg_cancel_query($stmt);
		        break;
		    }*/
		}
	    print "</table></form>";
 	    
		if($count>50){
		    print "...<a href=\"AllClones.php?name=$geneID\">Get all clones</a><br><br>";
		}
		
		print "&nbsp;&nbsp;&nbsp;<img border=\"0\" src=\"pictures/button-selectALL.jpg\" onClick=\"CheckALL();\">";

		print "&nbsp;&nbsp;&nbsp;<img border=\"0\" src=\"pictures/button-download.jpg\" onClick=\"document.form.submit();\">";
		
		//print '<br><br>&nbsp;&nbsp;&nbsp;<a href="ClusterFLATexport-molecule-clone.php?name='.$geneID.'">Get all clones in raw text format</a>';
		

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

?>


<?php
   	include("frames_footer.php");
?> 