<?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 ciona_design.IN_SITU A, ciona_design.DEV_STAGE B where A.SPECIES='Ciona intestinalis' AND A.DEV_STAGE_ID=B.DEV_STAGE_ID";

$stmt = OCIParse($connection, $requete);
	OCIExecute($stmt);



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

//Traitement sur les commentaires
$comments=OCIResult($stmt, "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 ciona_design.MOL_ENTITY A, ciona_design.TARGET B, ciona_design.ANAT_ENTITY C where (B.IN_SITU_ID=".OCIResult($stmt, "IN_SITU_ID").") AND (B.TARGET_ID=A.MOL_ID) AND (B.REGION_ID=C.ANAT_ID)";
	
$stmt2 = OCIParse($connection, $requete);
	OCIExecute($stmt2);
	while (OCIFetch($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[OCIResult($stmt2, "MOL_ID")]=OCIResult($stmt2, "NAME");
					$typemol[OCIResult($stmt2, "MOL_ID")]=OCIResult($stmt, "TYPE");
				
			
			}		
			
			while (list($key, $value) = each($mol)) {
			      $count2=0;
			      $geneidlist="";
				  if( $typemol[$key]=="clone" ){
					  $requete="SELECT distinct ANISEED_CLUSTER_ID from ciona_design.CLONE_SEQ where CLONE_NAME='$value'";
					  	$resultset = OCIParse($connection, $requete);
						OCIExecute($resultset);
						
						  
					while(OCIFetch($resultset)){
						   $geneidlist[$count2++]=OCIResult($resultset, "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 ciona_design.CLONE_SEQ where CLONE_SEQ_ID=$key";
					 $resultset = OCIParse($connection, $requete);
						OCIExecute($resultset);
					  while(OCIFetch($resultset)){
						   $geneidlist[$count2++]=OCIResult($resultset, "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 ciona_design.GENE A, ciona_design.SYNO B where A.GENE_ID=B.GENE_ID and B.GENE_ID=".$geneidlist[$a]."order by B.ORIGIN DESC";
					  $resultset = OCIParse($connection, $requete);
						OCIExecute($resultset);
					  $syno=""; $aaa=0;
					  while($resultset){
					      $genename=OCIResult($resultset, "NAME");
						 
						  }
				
					fwrite($handle,"PREDICTED_GENE:\t$genename\n");
					  //print "PREDICTED_GENE:\t$genename<br>";
		