<?php
include("connection.php");
include("frames_header.php");
?>


<?

//Ouverture du fichier
$filename = "./ISHflat.file";

   if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
   }




//Pour Ciona seulement
// On recupere tous les insitus
$requete="SELECT A.\"IN_SITU_ID\",A.\"IM_COMMENTS\", A.\"SPECIES\",B.\"NAME\" AS \"DEV\" from \"IN_SITU\" A, \"DEV_STAGE\" B where A.\"SPECIES\"='Ciona intestinalis' AND A.\"DEV_STAGE_ID\"=B.\"DEV_STAGE_ID\"";
$stmt=pg_query($c,$requete);

$count=0;

while ($row=pg_fetch_array($stmt))
{
fwrite($handle,"#".$row["IN_SITU_ID"]."\n");
//print "#".$row["IN_SITU_ID"]."<br>";
fwrite($handle,"SPECIES:\t".$row["SPECIES"]."\n");
//print "SPECIES:\t".$row["SPECIES"]."<br>";
fwrite($handle,"DEV_STAGE:\t".$row["DEV"]."\n");
//print "DEV_STAGE:\t".$row["DEV"]."<br>";

//Traitement sur les commentaires
$comments=$row["IM_COMMENTS"];


$comments=ereg_replace("&nbsp;","",$comments);
$comments=ereg_replace("<[a-z]+>","",$comments);
$comments=ereg_replace("</[a-z]+>","",$comments);


fwrite($handle,"COMMENTS:\t$comments\n");
//print "COMMENTS:\t".$row["IM_COMMENTS"]."<br>";



//On recupere les stained regions (ANAT_ID)
//On recupere les stained molecule (NAME)
/*$requete="SELECT 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\")";*/
$requete = "SELECT 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 \"ANISEED_CLUSTER_ID\" from \"CLONE_SEQ\" where \"CLONE_SEQ_ID\"=$key";
					  $resultset=pg_query($c,$requete);
					  while($row5=pg_fetch_array($resultset)){
						   $geneidlist[$count2++]=$row5["ANISEED_CLUSTER_ID"];
					  }
				  }
				  else if( $typemol[$key]=="gene" ){
					  $geneidlist[$count2++]=$key;
				  }
				  
				  //recherche des noms possibles pour le gene correspondant				  if(count($geneidlist)>1)
				      $geneidlist=array_unique($geneidlist);
				  for($a=0; $a<count($geneidlist); ++$a){
					//print "=>>".$geneidlist[$a]."<br>";
					if( (isset($geneidlist[$a]))and(strlen($geneidlist[$a])>0) ){
					  $requete="SELECT distinct A.\"NAME\", B.\"SYNO\", B.\"ORIGIN\" from \"GENE\" A, \"SYNO\" B where A.\"GENE_ID\"=B.\"GENE_ID\" and B.\"GENE_ID\"=".$geneidlist[$a]."order by B.\"ORIGIN\" DESC";
					  $resultset=pg_query($c,$requete);
					  $syno=""; $aaa=0;
					  while($row6=pg_fetch_array($resultset)){
					      $genename=$row6["NAME"];
						 
						  }
				
					fwrite($handle,"PREDICTED_GENE:\t$genename\n");
					  //print "PREDICTED_GENE:\t$genename<br>";
					}
				  }

			}

$requete2 = "SELECT B.\"SURE\", B.\"PART_OF\", A.\"MOL_ID\", A.\"NAME\", A.\"TYPE\", C.\"NAME\" AS \"REGION\", C.\"ANAT_ID\", C.\"DEV_ST