<?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 - EST count</title>
<?
    include("frames_header.php");

if (empty($_GET["display"])) $_GET["display"]="no";

?>


<center>


<?php

    $cluster=$_GET["name"];

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


	$species=$_GET["species"];
$base=$_GET["base"];

$id=$cluster;
$requete="SELECT \"NAME\" from \"MOL_ENTITY\" where \"MOL_ID\"='$base'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$name=$row["NAME"];

$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>";
	}



?>


<!-- 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><a href="ESTcluster.php?name=<? print $cluster; ?>&base=<? print $base;?>">ESTs cluster</a></li>
    <li id="current" ><a href="#">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 -->
<br><br>
<br><br>
<?


    print "<font color=\"#D64242\" size=\"3\"><b>Cluster: $cluster</b></font><br><br>";

   print '<table width="99%" border="1" cellspacing="0" cellpadding="0">';
	print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Library type</b></strong></td><td><font color="#456898" size="2"><b>Tissue</b></font></td><td width="25%"><font color="#456898" size="2"><b>Library</b></font></td><td><font color="#456898" size="2"><b>Expression</b></font></td>  </tr>';
	$requete="select * from \"LIBRARY\" where \"SPECIES\"='$species' order by \"ORDRE\" ASC";
    $stmt = pg_query($c,$requete);
	
	$count=0;
	$data = array();
	while($row=pg_fetch_array($stmt)){
	
	    if(! isset($data[ $row["COMMENTS"] ][1])){//avoid notice messages
		    $data[ $row["COMMENTS"] ][0]="";
			$data[ $row["COMMENTS"] ][1]="";
		}
		
		$data[ $row["COMMENTS"] ][1] += $row["LIB_SIZE"];//calcule le cumul des tailles des lib de meme type
		
		$type[$count]=$row["COMMENTS"];
		++$count;
        
		//$requete2="select count(distinct B.\"CLONE_ID\") AS res from \"CLONE_SEQ\" A, \"CLONE\" B where A.\"ANISEED_CLUSTER_ID\"='$cluster' and (A.\"CLONE_NAME\"=B.\"CLONE_NAME\") and B.\"LIBRARY_ID\"=".$row["LIBRARY_ID"];
		$requete2="select count(distinct B.\"CLONE_ID\") AS res from \"CLONE_SEQ\" A, \"CLONE\" B where (A.\"ANISEED_CLUSTER_ID\"='$cluster' or A.\"ANISEED_KYOTO_CLUSTER_ID\"='$cluster' or A.\"ANISEED_ENSEMBL_CLUSTER_ID\"='$cluster') and (A.\"CLONE_NAME\"=B.\"CLONE_NAME\") and B.\"LIBRARY_ID\"=".$row["LIBRARY_ID"];
		$stmt2 = pg_query($c,$requete2);
	    if($row2=pg_fetch_array($stmt2)){
	        $value=$row2["res"];
		    if($value > 0){
			    //////////////////////4000 au lieu de 1000 pour y voir qq chose
			
		       print "<tr align=\"center\"><td><font color=\"#656565\" size=\"-1\">".$row["COMMENTS"]."&nbsp;</font></td><td><font color=\"#656565\" size=\"-1\">".$row["TISSUE"]."&nbsp;</font></td><td><font color=\"#656565\" size=\"-1\">".$row["TITLE"]."&nbsp;</font></td><td valign=\"middle\"><center><img src=\"picture-est.php?value=".round( 4000*($value/$row["LIB_SIZE"]), 4)."\"><br><font color=\"#656565\" size=\"-1\">$value clones/".$row["LIB_SIZE"]."</font></center>&nbsp;</td></tr>";
		        //print '<tr valign="top"><td><hr size="2"><td><hr size="2"><td><hr size="2"></td><td><hr size="2"></td></tr>';
				
				$data[ $row["COMMENTS"] ][0] += $value;//calcule le cumul des clones trouves dans des lib de meme type
				
			//print "ICICI:".round(4000*($value/$row["LIB_SIZE"]));

			}
		}
	}
	print '</table>';

//DEBUT	
	if($count==0){
	    print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
	}else{
	///////////////////////////////////////tableau fusionne de l'expression//////////////////////////
	    print "<br><br><br><strong>Resume:</strong><br><br>";
		print '<table width="80%" border="1" cellspacing="0" bordercolor="#333333">';
		
		$ll=0;
		$final=array();
		for($f=0; $f<count($type); ++$f){
		    if(! in_array($type[$f], $final) ){
			    $final[$ll++]=$type[$f];
	    	}
		}
	    for($a=0;$a<count($final);++$a){
		    if($data[$final[$a]][1]>0)
		        print '<tr valign="middle"><td><font color="#656565" size="-1">'.$final[$a].":  </font></td><td><center><img src=\"picture-est.php?value=".round( 4000*($data[$final[$a]][0]/$data[$final[$a]][1]), 4)."\"></center></td><td><font color=\"#656565\" size=\"-1\">".(0+$data[$final[$a]][0])." clones/".$data[$final[$a]][1]."</font></td></tr>";
			else
			    print '<tr valign="middle"><td><font color="#656565" size="-1">'.$final[$a].":  </font></td><td><center><img src=\"picture-est.php?value=0\"></center></td><td>&nbsp;</td></tr>";
		}
		
	    print '</table>';
	}
//FIN	
?>


</center>


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