<?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");
header('Content-Type: text/plain');
?>
<?php
	


	//connection
	//include("connection.php");
	
	
	//variables pour le systeme page suiv/prec
	if( isset($_POST["option"]) ) $option=$_POST["option"];
	if( isset($_POST["offset"]) ) $offset=$_POST["offset"];
	if( isset($_POST["nbMAX"])  ) $nbMAX=$_POST["nbMAX"];
	
	if(! isset($option)) $option= 1;
	if(! isset($offset)) $offset= 0;
	if(! isset($nbMAX)) $nbMAX= 20;
	
	/////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////requete//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	

	//species
	if(isset($_POST["species"]))
	$species = $_POST["species"];
	
	//regions
	$sql_region='';	

$boool=0;
//if (strlen($_POST["notidanat"])>1) {$boool=1;}
if ((strlen($_POST["notidanat"])>1)&&($_POST["fatemode"]==0)) {$boool=1;}



	if( ((strlen($_POST["idanat"])>1)&&($_POST["fatemode"]==0) ) || ($boool==1)  ){
		
		$tabanat=split(":",$_POST["idanat"]);
		array_pop($tabanat);



		
		if($_POST["BOOLanat"]==1){//and
		
			//signifie que l'on cherche aussi les parents des anat fournis			if($_POST["xref"]==1){





				/* SIMULATION D'UNE REQUETE SUR HIERARCHIE PAR UNE PROCEDURE DE REQUETES RECURSIVES */
$IN1="";
function select_recursif_descendant_2($conn,$start,$res)
{
global $IN1;

if ($start!='')
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE \"ANAT_ID\"= $start ";
else return;

$stmt2=pg_query($conn,$requete2);
while($row2=pg_fetch_array($stmt2))
	{
	$IN1.=$row2["ANAT_ID"].",";
	$res=select_recursif_descendant_2($conn,$row2["CHILD_OF"],$res);
	}
return $res;
}
/* FIN DE LA SIMULATION */

				for($a=0;$a<count($tabanat);++$a)
					{
					$IN1="";
					$res=select_recursif_descendant_2($c,$tabanat[$a],$res);
					//$IN1=substr($IN1,0,strlen($IN1)-1);
					$IN1.=$tabanat[$a];
					if ($IN1=="") $IN1="0";	/*Cas du IN vide*/
					$sql_region.="INTERSECT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" IN(".$IN1.") ";
					}





			}
			//signifie que l'on cherche aussi les enfants des anat fournis			else if($_POST["xref"]==2){


				/* SIMULATION D'UNE REQUETE SUR HIERARCHIE PAR UNE PROCEDURE DE REQUETES RECURSIVES */
$IN2="";
function select_recursif_descendant($conn,$start,$res)
{
global $IN2;

if ($start!='')
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE \"CHILD_OF\"= $start ";
else return;

$stmt2=pg_query($conn,$requete2);
while($row2=pg_fetch_array($stmt2))
	{
	$IN2.=$row2["ANAT_ID"].",";
	$res=select_recursif_descendant($conn,$row2["ANAT_ID"],$res);
	}
return $res;
}
/* FIN DE LA SIMULATION */





				for($a=0;$a<count($tabanat);++$a)
					{
					$IN2="";
					$res=select_recursif_descendant($c,$tabanat[$a],$res);
					//$IN2=substr($IN2,0,strlen($IN2)-1);
					$IN2.=$tabanat[$a];
					if ($IN2=="") $IN2="0"; /*Cas du IN vide*/
					$sql_region.="INTERSECT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" IN(".$IN2.") ";
					}
				






			}
			//signifie que l'on cherche le terme exact			else{
				for($a=0;$a<count($tabanat);++$a){
					$sql_region.=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" = $tabanat[$a]";
				}
			}
			
			if($_POST["only"]==1){  //gestion du only  //avec le and on veut autant de trucs marques que de trucs selectionné mais pas plus.
			   /* $sql_region.=" intersect select IN_SITU_ID from (select CIONA_DESIGN.TARGET.*, count(*) over (partition by in_situ_id) count from CIONA_DESIGN.TARGET) where count = ".(count($tabanat));*/
			}
			
		}else{//or
	

			$zz=join(",",$tabanat);

			

			//if($_POST["only"]==2)  //gestion de l'option "not stained"
			//    $sql_region="INTERSECT SELECT IN_SITU_ID FROM CIONA_DESIGN.TARGET WHERE NOT EXIST( REGION_ID IN(".$zz.") )";
		    //else
			

//Pour les notanat

if ((strlen($_POST["notidanat"])>1)&&($_POST["fatemode"]==0))
{
$nottabanat=split(":",$_POST["notidanat"]);
array_pop($nottabanat);

$notzz=join(",",$nottabanat);			

if (strlen($_POST["idanat"])==0)
	{//possible DISTINCT 
	$sql_region=" INTERSECT (
					SELECT \"IN_SITU_ID\" FROM \"TARGET\" EXCEPT
					SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" IN (".$notzz.")";

	}
else { 
	$sql_region=" INTERSECT (
					SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" IN (".$zz.")
					EXCEPT
					SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" IN (".$notzz.")";
	}
}
else {
	print "ici";
	print ">>".$_POST["notidanat"]." >>".$_POST["fatemode"];

			$sql_region=" INTERSECT (
					SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" IN (".$zz.")";
		
	}


			if($_POST["only"]==1){  //gestion du only  //avec le or on veut juste l'un des trucs selectionnes comme marqué ou une combinaison mais pas plus.
			    $sql_region.=" EXCEPT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" NOT IN (".$zz.")";
			}
			$sql_region.=")";
			/*
			$sql_region=" INTERSECT SELECT IN_SITU_ID FROM CIONA_DESIGN.TARGET WHERE ( REGION_ID IN(".$zz.") )";
			
			if($_POST["only"]==1){  //gestion du only  //avec le or on veut juste l'un des trucs selectionnes comme marqué ou une combinaison mais pas plus.
			    $sql_region.=" intersect select IN_SITU_ID from (select CIONA_DESIGN.TARGET.*, count(*) over (partition by in_situ_id) count from CIONA_DESIGN.TARGET) where count = 1";
			}
		    */
		}
		
		
		
	}
// Les Fates
$sql_fate="";
$tabfate="";
if(isset($_POST["fate"])) $tabfate=$_POST["fate"];
$tabfatelist="";
if(is_array($tabfate)){
$tabfatelist=join("','",$tabfate);
}else{
$tabfatelist=$tabfate;
//$tabfate=split("','",$tabfatelist);
}

//Traitement pour elimination eventuelle des Magic quotes !
$tabfatelist=str_replace("\\","",$tabfatelist);



//Ajout pour les NotFates

$tabnotfate="";
if (isset($_POST["notfate"])) 
{$tabnotfate=$_POST["notfate"];
$tabnotfatelist="";
if (is_array($tabnotfate)){
$tabnotfatelist=join("','",$tabnotfate);
}
else{
$tabnotfatelist=$tabnotfate;
}

//Traitement pour elimination eventuelle des Magic quotes !
$tabnotfatelist=str_replace("\\","",$tabnotfatelist);
}

/*
print "Fate    : $tabfatelist <br>";
print "Not Fate: $tabnotfatelist <br>";
*/

//print $tabfate[0];
if((strlen($tabfatelist)>0)&&($_POST["fatemode"]==1))
{ 
//Modif pour conformité a notfate
if ((strlen($tabnotfatelist)>0))
{
$sql_fate="INTERSECT(
SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" in (select \"ANAT_ID\" from \"FATE\" where \"KEYWORDS\" in ('".$tabfatelist."') ) 
EXCEPT
SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" in ( (select \"ANAT_ID\" from \"FATE\" where \"KEYWORDS\" in ('".$tabnotfatelist."') ) )
)";
//print $sql_fate;
}
else
{
$sql_fate="INTERSECT(SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" in (select \"ANAT_ID\" from \"FATE\" where \"KEYWORDS\" in ('".$tabfatelist."') ))";

}


//print $sql_fate;
}
//Le cas ou l'on ne regarde que les NotFate
else
{
if ((strlen($tabnotfatelist)>0))
{
$sql_fate="INTERSECT(
SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" in ( (select \"ANAT_ID\" from \"FATE\"))
EXCEPT
SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"REGION_ID\" in ( (select \"ANAT_ID\" from \"FATE\" where \"KEYWORDS\" in ('".$tabnotfatelist."') ) )
)";
//print $sql_fate;
}
}








	//recup des dev stage id
	//ne s'utilise qu'en l'absence d'anat_id
	//if( (strlen($_POST["selectediddev"])>1)&&((! strlen($sql_region)>0)||$_POST["only"]==2) ){
	$sql_dev_stage="";
	if( (strlen($_POST["selectediddev"])>1)&&(! strlen($sql_region)>0) ){
	//if (strlen($_POST["selectediddev"])>1){
		$tabdev=split(":",$_POST["selectediddev"]);
		array_pop($tabdev);
		$zz=join(",",$tabdev);
		
		$sql_dev_stage="INTERSECT SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" WHERE ( \"DEV_STAGE_ID\" IN(".$zz.") )";
	}
	
	//verif les auteurs
	$sql_author="";
	if( strlen($_POST["author"])>1 ){
		$author=split(":",$_POST["author"]);
		$zz=join("','",$author);
		$zz=strtoupper($zz);
		
		$sql_author="INTERSECT SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" WHERE \"IN_SITU_ID\" IN(  select \"BIO_ID\" from \"ENTRY_ORIGIN\" where \"AUTHOR_ID\" in (select \"AUTHOR_ID\" from \"AUTHOR\" where UPPER(\"NAME\") in ('".$zz."') ) ) ";
	
	}
	
	
	//recup target
	$sql_target="";
	if(strlen($_POST["target"])>0){
		//Test pour savoir si c un refine foireux
		$test=explode(" ",$_POST["target"]);
		if ($test[0]=="Error!")
		{
   		print "<br><br><strong>ERROR:</strong> due to performance reasons, you cannot search for more than 100 molecules";
		include ("frames_footer.php");
		return;
		}
		

		$targetTMP = split(":",strtoupper($_POST["target"]));
		
		if(count($targetTMP)>100){
		    print "<br><br><strong>ERROR:</strong> due to performance reasons, you cannot search for more than 100 molecules";
		    include ("frames_footer.php");
			return;
		}
		
		if($_POST["BOOLtarg"]==1){//and

			if($_POST["MOLtype"]==2){ //si c un gene -> recup des clones correspondant
			/*
				for ($a=0; $a < count($targetTMP); ++$a){
                    $sql_target.=" INTERSECT SELECT IN_SITU_ID FROM CIONA_DESIGN.TARGET WHERE TARGET_ID IN ( select B.clone_id from ciona_design.clone_seq A, ciona_design.clone B where (A.clone_name=B.clone_name) and A.cluster_id=(select cluster_id from ciona_design.clustering where gene_id=(select gene_id from ciona_design.gene where name='$targetTMP[$a]')) ) OR TARGET_ID IN ( select clone_seq_id from ciona_design.clone_seq where cluster_id=(select cluster_id from ciona_design.clustering where gene_id=(select gene_id from ciona_design.gene where name='$targetTMP[$a]')) )";
				}
			*/
			
				////nouvelle version pour utiliser le clustering maison
				for ($a=0; $a < count($targetTMP); ++$a){
				/*
                    $sql_target.=" INTERSECT
					 SELECT IN_SITU_ID
					  FROM CIONA_DESIGN.TARGET
					   WHERE TARGET_ID IN ( select B.clone_id from ciona_design.clone_seq A, ciona_design.clone B where A.clone_name=B.clone_name and A.aniseed_cluster_id=(select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]') )
					    OR TARGET_ID IN ( select clone_seq_id from ciona_design.clone_seq where aniseed_cluster_id=(select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]'))
						 OR TARGET_ID IN (select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]')";
				*/
                    $sql_target.=" INTERSECT
					 SELECT \"IN_SITU_ID\"
					  FROM \"TARGET\"
					   WHERE \"TARGET_ID\" IN ( 
					   select B.\"CLONE_ID\" from \"CLONE_SEQ\" A, \"CLONE\" B where A.\"CLONE_NAME\"=B.\"CLONE_NAME\" and A.\"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]') 
					   UNION select \"CLONE_SEQ_ID\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]')
					   UNION select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]')";

				}
			
			}else{
                //si c un clone
				for ($a=0; $a < count($targetTMP); ++$a){
					$sql_target.=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"TARGET_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" WHERE upper(\"NAME\")='$targetTMP[$a]' OR upper(\"FULL_NAME\")='$targetTMP[$a]')";
				}
			}
			
		}else{//or
			
			if($_POST["MOLtype"]==2){ //si c un gene -> recup des clones correspondant
				$name='';
			    /*	
				for ($a=0; $a < count($targetTMP); ++$a){
					if($a==0)
						$name.=" (TARGET_ID IN ( select B.clone_id from ciona_design.clone_seq A, ciona_design.clone B where (A.clone_name=B.clone_name) and A.cluster_id=(select cluster_id from ciona_design.clustering where gene_id=(select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]')) ) OR TARGET_ID IN ( select clone_seq_id from ciona_design.clone_seq where cluster_id=(select cluster_id from ciona_design.clustering where gene_id=(select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]')) )) ";
					else
						$name.=" OR (TARGET_ID IN ( select B.clone_id from ciona_design.clone_seq A, ciona_design.clone B where (A.clone_name=B.clone_name) and A.cluster_id=(select cluster_id from ciona_design.clustering where gene_id=(select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]')) ) OR TARGET_ID IN ( select clone_seq_id from ciona_design.clone_seq where cluster_id=(select cluster_id from ciona_design.clustering where gene_id=(select gene_id from ciona_design.gene where upper(name)='$targetTMP[$a]')) )) ";
				}
				$sql_target=" INTERSECT SELECT IN_SITU_ID FROM CIONA_DESIGN.TARGET WHERE ".$name;
				*/
				
				////nouvelle version pour utiliser le clustering maison
                for ($a=0; $a < count($targetTMP); ++$a){
					if($a==0)
						$name.=" (\"TARGET_ID\" IN ( 
						select B.\"CLONE_ID\" from \"CLONE_SEQ\" A, \"CLONE\" B where A.\"CLONE_NAME\"=B.\"CLONE_NAME\" and A.\"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]')
						UNION select \"CLONE_SEQ_ID\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]')
						UNION select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]'
						";
					else
						//$name.=" OR (TARGET_ID IN ( 
						$name.= "UNION select B.\"CLONE_ID\" from \"CLONE_SEQ\" A, \"CLONE\" B where A.\"CLONE_NAME\"=B.\"CLONE_NAME\" and A.\"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]') 
						UNION select \"CLONE_SEQ_ID\" from \"CLONE_SEQ\" where \"ANISEED_CLUSTER_ID\"=(select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]')
						UNION select \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$targetTMP[$a]'
						";
				
				}
				$sql_target=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE ".$name."))";
                
			}else{
                //si c un clone
				$name='';
				for ($a=0; $a < count($targetTMP); ++$a){
					if($a==0)
						$name.=" (\"TARGET_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" WHERE(upper(\"NAME\")='$targetTMP[$a]' OR upper(\"FULL_NAME\")='$targetTMP[$a]'))) ";
					else
						$name.=" OR (\"TARGET_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" WHERE(upper(\"NAME\")='$targetTMP[$a]' OR upper(\"FULL_NAME\")='$targetTMP[$a]'))) ";
				}
				$sql_target=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"TARGET\" WHERE ".$name;
			}
			
		}
	}
	
	
	//recup des mutants
	$sql_mutant="";
	if((strlen($_POST["mutant"])>0)&&($_POST["BOOLmut"]<2)){
		$mutantTMP = split(":",strtoupper($_POST["mutant"]));
		if($_POST["BOOLmut"]==1){//and
			for ($a=0; $a < count($mutantTMP); ++$a){
				$sql_mutant.=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"MUTANT\" WHERE \"BIO_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" WHERE upper(\"NAME\")='$mutantTMP[$a]' OR upper(\"FULL_NAME\")='$mutantTMP[$a]')";
			}
		}else if($_POST["BOOLmut"]==0){//or
			$name='';
			for ($a=0; $a < count($mutantTMP); ++$a){
				if($a==0)
					$name.=" (\"BIO_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" WHERE(upper(\"NAME\")='$mutantTMP[$a]' OR upper(\"FULL_NAME\")='$mutantTMP[$a]'))) ";
				else
					$name.=" OR (\"BIO_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" WHERE(upper(\"NAME\")='$mutantTMP[$a]' OR upper(\"FULL_NAME\")='$mutantTMP[$a]'))) ";
			}
			$sql_mutant=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"MUTANT\" WHERE ".$name;
		}
		
	}else if($_POST["BOOLmut"]==2){//any
		    //$sql_mutant=" INTERSECT select in_situ_id from ciona_design.in_situ where wild_type='n'";
			$sql_mutant=" INTERSECT select \"IN_SITU_ID\" from \"MUTANT\"";
	}else if($_POST["BOOLmut"]==3){//none
		    //$sql_mutant=" INTERSECT select in_situ_id from ciona_design.in_situ where wild_type='y'";
			$sql_mutant=" INTERSECT select \"IN_SITU_ID\" from \"IN_SITU\" where \"IN_SITU_ID\" not in (select \"IN_SITU_ID\" from \"MUTANT\")";
	}
	
	
	//recup des manip d'embryon
	$sql_manip="";
	if($_POST["BOOLmanip"]==1){
	    $sql_manip.=" INTERSECT select \"IN_SITU_ID\" from \"EMBRYO_MANIP\"";
	}else if($_POST["BOOLmanip"]==2){
		$sql_manip=" INTERSECT select \"IN_SITU_ID\" from \"IN_SITU\" where \"IN_SITU_ID\" not in (select \"IN_SITU_ID\" from \"EMBRYO_MANIP\")";
	}
	
	
	////////////////////////////////
	//la MEGA requete //
	////////////////////////////////
	
	
	
	
	$requete="SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" WHERE (\"CHECKED\"='y') AND (\"PRIVATE\" = 'n') ";
	
	if( (strlen($_POST["idanat"])==0)&&(strlen($species)>0))
		$requete.=" INTERSECT SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" WHERE \"SPECIES\" = '$species' ";


	
	if(strlen($sql_dev_stage)>0)
		$requete.= $sql_dev_stage;
	
	if(strlen($sql_region)>0)
		$requete .= $sql_region;
	
	if(strlen($sql_fate)>0)
	    $requete .= $sql_fate;
	
	if(strlen($sql_author)>0)
		$requete .= $sql_author;
		
	if(strlen($sql_target)>0)
		$requete.= $sql_target;
		
	if(strlen($sql_mutant)>0)
		$requete.= $sql_mutant;
		
	if(strlen($sql_manip)>0)
		$requete .= $sql_manip;	
	
	if(strlen($_POST["Order"])>0){
		if ($_POST["Order"]=="SPECIES")
		 
		   $requete = "SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" A, \"DEV_STAGE\" B  WHERE \"IN_SITU_ID\" IN ($requete) and (A.\"DEV_STAGE_ID\"=B.\"DEV_STAGE_ID\") ORDER BY B.\"SPECIES\", B.\"DEV_PERCENT\", \"IN_SITU_ID\"";
			
		else
		    $requete = "SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" A, \"DEV_STAGE\" B WHERE \"IN_SITU_ID\" IN ($requete) and (A.\"DEV_STAGE_ID\"=B.\"DEV_STAGE_ID\") ORDER BY B.\"DEV_PERCENT\", B.\"SPECIES\", \"IN_SITU_ID\"";
			
	}
	
	
	

	//print $requete;
	

	

	
	$requete=ereg_replace('\\\\','',$requete); //Pour lever les ' mis par le POST
	//print $requete;
	
$stmt=pg_query($c,$requete);

$count=0;
$ISHID="";

if (pg_num_rows($stmt)>1000) 
	{
	print "Your query returns ".pg_num_rows($stmt)." records... Please refine your query. Display limit: 1000 records";
	return;
	}

while ($row=pg_fetch_array($stmt))
{
//print "#".$row["IN_SITU_ID"]."\n";
$ISHID=$row["IN_SITU_ID"];
//print "#".$row["IN_SITU_ID"]."<br>";
$r="SELECT \"SPECIES\" from \"IN_SITU\" where \"IN_SITU_ID\"='".$row["IN_SITU_ID"]."'";
$st=pg_query($c,$r);
$ro=pg_fetch_array($st);

print "SPECIES\t".$ro["SPECIES"]."\n";
//print "SPECIES:\t".$row["SPECIES"]."<br>";
$r="SELECT A.\"NAME\" from \"DEV_STAGE\" A where A.\"DEV_STAGE_ID\"= (select B.\"DEV_STAGE_ID\" from \"IN_SITU\" B where B.\"IN_SITU_ID\"=".$row["IN_SITU_ID"].")";
$st=pg_query($c,$r);
$ro=pg_fetch_array($st);
print "DEV_STAGE\t".$ro["NAME"]."\n";
//print "DEV_STAGE:\t".$row["DEV"]."<br>";


$requete = "SELECT distinct B.\"SURE\", B.\"PART_OF\", A.\"MOL_ID\", A.\"NAME\", A.\"TYPE\", C.\"NAME\" AS \"REGION\", C.\"ANAT_ID\", C.\"DEV_STAGE_ID\" FROM \"MOL_ENTITY\" A, \"TARGET\" B, \"ANAT_ENTITY\" C where (B.\"IN_SITU_ID\"=".$row["IN_SITU_ID"].") AND (B.\"TARGET_ID\"=A.\"MOL_ID\") AND (B.\"REGION_ID\"=C.\"ANAT_ID\")";
	$stmt2=pg_query($c,$requete);

	while ($row2=pg_fetch_array($stmt2))
	{
	
//	print "STAINING:\tSTAINED_REGION:\t".$row2["REGION"]."\tSTAINED_MOL:\t".$row2["NAME"]."<br>";
	


$str='';
			$str2='';
			$mol=null; $typemol=null;//sert a retrouver le gene correspondant
		
				
				    
					$mol[$row2["MOL_ID"]]=$row2["NAME"];
					$typemol[$row2["MOL_ID"]]=$row2["TYPE"];
				
			
			}		
			
			while (list($key, $value) = each($mol)) {
			      $count2=0;
			      $geneidlist="";
				  if( $typemol[$key]=="clone" ){
					  $requete="SELECT distinct \"ANISEED_CLUSTER_ID\" from \"CLONE_SEQ\" where \"CLONE_NAME\"='$value'";
					  	$resultset=pg_query($c,$requete);
						
						  
					while($row5=pg_fetch_array($resultset)){
						   $geneidlist[$count2++]=$row5["ANISEED_CLUSTER_ID"]; //+ieur genes poss si les est d'un meme clone correspondent a des modeles differents
					  }
				  }
				  else if( $typemol[$key]=="clone_sequence" ){
					  $requete="SELECT distinct \"ANISEED_CLUSTER_