<?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");
?>
<?php
	//connection

	
		//$origin="";
		$species="";
		$words="";
		$genelist="";
	
	  
		if(isset($_POST["species"]))
			$species=$_POST["species"];
		if(isset($_POST["go"]))
			$words=explode("@",$_POST["go"]);
		if(isset($_POST["genelist"]))
			$genelist=$_POST["genelist"];

		
		if (isset($_POST["notin"]))
			$notin=$_POST["notin"];

		    

for ($a=0;$a<count($words);$a++) print "GO\t".$words[$a]."\n";

/* SIMULATION D'UNE REQUETE SUR HIERARCHIE PAR UNE PROCEDURE DE REQUETES RECURSIVES */


function select_recursif_descendant($conn,$start,$res)
{
$res.=$start.",";
/*ON ECRIT SA REQUETE*/
$requete="SELECT distinct * FROM \"GO_CHILD_OF\" WHERE \"CHILD_OF\"=".$start." ";


$stmt=pg_query($conn,$requete);
while ($row=pg_fetch_array($stmt))
	{
	$res=select_recursif_descendant($conn,"'".$row["GO_ID"]."'",$res);
	}
return $res;
}

$result="";
for ($a=0;$a<count($words);$a++)
{
$words[$a]="'".$words[$a]."'";
$result=select_recursif_descendant($c,$words[$a],$result);
$query.=$result;
}
$query=substr($query,0,strlen($query)-1);

/* FIN DE LA SIMULATION */

		//search genes corresponding to the GO annotation
if ($notin!='y')
{		
		$requete="SELECT distinct B.\"NAME\", B.\"GENE_ID\", C.\"SPECIES\" FROM \"GO_DESCRIPTION\" A, \"GENE\" B, \"MOL_ENTITY\" C WHERE (A.\"MOL_ID\"=B.\"GENE_ID\") and (A.\"MOL_ID\"=C.\"MOL_ID\") and (A.\"GO_ID\" in (".$query.") )";
		
		if( strlen($species)>1 )
		    $requete.=" AND (C.\"SPECIES\" ='$species')";
		
		
		$pp=0;
		$tt=0 + $_POST["originortho"] + $_POST["originblast"] + $_POST["originip"];
		if($tt>0){
		    $requete.="AND (";
			
			if($_POST["originortho"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.="  (A.\"PHYLOGENY_ORIGIN\"='y') ";
				++$pp;
			}
			if($_POST["originblast"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.=" (A.\"BLAST_ORIGIN\"='y') ";
				++$pp;
			}
			if($_POST["originip"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.=" (A.\"IP_ORIGIN\"='y') ";
				++$pp;
			}	
		    $requete.=")";
        }


		if( strlen($genelist)>0){
		    $genelist = substr($genelist, 0, strlen($genelist)-1);
		    $genelist = ereg_replace(":","','",$genelist);

			$requete.=" AND (B.\"NAME\" in('".$genelist."'))";
		}
		
}
else
{
//Le cas du non

if( strlen($genelist)>0){
		    $genelist = substr($genelist, 0, strlen($genelist)-1);
		    $genelist = ereg_replace(":","','",$genelist);
}

$requete="
SELECT distinct B.\"NAME\", B.\"GENE_ID\", C.\"SPECIES\" FROM \"GO_DESCRIPTION\" A, \"GENE\" B, \"MOL_ENTITY\" C WHERE (A.\"MOL_ID\"=B.\"GENE_ID\") and (A.\"MOL_ID\"=C.\"MOL_ID\") and (A.\"GO_ID\" not in (".$query.") )";

if( strlen($species)>1 )
		    $requete.=" AND (C.\"SPECIES\" ='$species')";
$pp=0;
		$tt=0 + $_POST["originortho"] + $_POST["originblast"] + $_POST["originip"];
		if($tt>0){
		    $requete.="AND (";
			
			if($_POST["originortho"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.="  (A.\"PHYLOGENY_ORIGIN\"='y') ";
				++$pp;
			}
			if($_POST["originblast"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.=" (A.\"BLAST_ORIGIN\"='y') ";
				++$pp;
			}
			if($_POST["originip"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.=" (A.\"IP_ORIGIN\"='y') ";
				++$pp;
			}	
		    $requete.=")";
        }


		if( strlen($genelist)>0){
		    $genelist = substr($genelist, 0, strlen($genelist)-1);
		    $genelist = ereg_replace(":","','",$genelist);

			$requete.=" AND (B.\"NAME\" in('".$genelist."'))";
		}

$requete.=" EXCEPT SELECT distinct B.\"NAME\", B.\"GENE_ID\", C.\"SPECIES\" FROM \"GO_DESCRIPTION\" A, \"GENE\" B, \"MOL_ENTITY\" C WHERE (A.\"MOL_ID\"=B.\"GENE_ID\") and (A.\"MOL_ID\"=C.\"MOL_ID\") and (A.\"GO_ID\" in (".$query.") )";

if( strlen($species)>1 )
		    $requete.=" AND (C.\"SPECIES\" ='$species')";
$pp=0;
		$tt=0 + $_POST["originortho"] + $_POST["originblast"] + $_POST["originip"];
		if($tt>0){
		    $requete.="AND (";
			
			if($_POST["originortho"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.="  (A.\"PHYLOGENY_ORIGIN\"='y') ";
				++$pp;
			}
			if($_POST["originblast"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.=" (A.\"BLAST_ORIGIN\"='y') ";
				++$pp;
			}
			if($_POST["originip"]==1){
			    if($pp>0)
				    $requete.=" OR ";
				$requete.=" (A.\"IP_ORIGIN\"='y') ";
				++$pp;
			}	
		    $requete.=")";
        }


		if( strlen($genelist)>0){
		 	$requete.=" AND (B.\"NAME\" in('".$genelist."'))";
		}

}





	
		$stmt =pg_query($c,$requete);
		
        
		$count=0;
		$genelist="";
		while( $row=pg_fetch_array($stmt) ){
		
		    //recherche des noms du gene
			
			$requete="SELECT \"SYNO\", \"ORIGIN\" FROM \"SYNO\" WHERE \"GENE_ID\"=".$row["GENE_ID"]." order by \"ORIGIN\" DESC";
			$stmt2 = pg_query($c,$requete);
			
			$names=""; $a=0;
		    while( $row2=pg_fetch_array($stmt2) ){
			    
			    if($row2["SYNO"]==$row["NAME"])
				    continue;
				
				if(++$a>1)
				    $names.='/';
			    $names.=$row2["SYNO"];
				if($row2["ORIGIN"]=="user-JGI")
				    $names.="*";
					
				
                //la chaine affichee ne doit pas depasser 100 char.
                //on ne prend en compte que l'affichage et pas le code html dans la mesure
				
				if( strlen($names)-(($a-1)*48) > 100 ){
				    $names.="/...";
					break;
				}
				
			}
			if($a==0){
				$requete="select \"DESCRIPTION\", \"EVALUE\" from \"BLAST\" where \"MOL_ID\"='".$row["GENE_ID"]."' and \"SCORE\"=(select max(\"SCORE\") from \"BLAST\" where \"MOL_ID\"='".$row["GENE_ID"]."')";
				$stmt2 = pg_query($c,$requete);
				if($row2=pg_fetch_array($stmt2)){
					
					$eval = $row2["EVALUE"];
					if( $eval <= 1e-10)
						$names.= "HIGHLY ";
					else if( ($eval >= 1e-4) and ($eval <= 1e-3) )
						$names.= "WEAKLY ";
					if($eval <= 1e-3)
						$names.= "SIMILAR TO ".$row2["DESCRIPTION"]." (Eval: $eval)";
				}
			}
			
		    ++$count;
			
			/*
if ($names=="") print "GENE_ID\t".$row["NAME"]."\tSPECIES\t".$row["SPECIES"]."\n";
	else	print "GENE_ID\t".$row["NAME"]."\tPOSSIBLE_NAME\t$names\tSPECIES\t".$row["SPECIES"]."\n";
			*/

////// CONVERSION POUR LE ANISEED GENEMODEL //////////////////////////////////
	$idname=$row["NAME"];
	
	$requete0="SELECT \"GENE_CLUSTER_ID\" from \"GENE_CONCEPT\" where \"GENE_MODEL_ID\"=(
	select \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$idname' and \"TYPE\"='gene'
	)";
	$stmt0=pg_query($c,$requete0);
	$row0=pg_fetch_array($stmt0);
	$geneclusterid=$row0["GENE_CLUSTER_ID"];
	$idname=str_replace("cluster","aniseedV3_",$row0["GENE_CLUSTER_ID"]);

	if (strlen($idname)==0) continue;


	$requete0="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$geneclusterid'";
	$stmt0=pg_query($c,$requete0);
	$row0=pg_fetch_array($stmt0);
	$NAME=$row0["GENE_CLUSTER_NAME"];
	if (strlen($NAME)==0)
		{
		print "+======>";
		$requete0="SELECT \"GENE_CLUSTER_NAME_BLAST\" from \"GENE_CONCEPT_NAME_BLAST\" where \"GENE_CLUSTER_ID\"='$geneclusterid'";
		$stmt0=pg_query($c,$requete0);
		$row0=pg_fetch_array($stmt0);
		$NAME=$row0["GENE_CLUSTER_NAME_BLAST"];
		print $NAME.">>>>>>>>";
		}

	if (strlen($NAME)==0) print "GENE_ID\t$idname\tSPECIES\t".$row["SPECIES"]."\n";
	else print "GENE_ID\t$idname\tPOSSIBLE_NAME\t$NAME\tSPECIES\t".$row["SPECIES"]."\n";

////////////////////////////////////////////////////////////////////////////////

			
			
		    $genelist.=$row["NAME"].":";  //liste les gene id pour rechercher les in situs.
		}
		
		
		if($count==0)
		    print "NO_MATCH_FOUND\n//\n";
		
		
        



?>