<?
include ("connection.php");

include ("circle.php");

print "<title> ANISEED V3.0 - Regulatory Network</title>";

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function simplify_hierarchy($hierarch,$leaf_lsts,$region_lst){

	$ret_list = (array) null;	
	$expand_list = (array) null;
	foreach($region_lst as $region){
		$expand_list = array_merge($expand_list,$leaf_lsts[$region]);
	}
		
	//WildCard!
	$parent_list = array('Whole embryo');
	while(true){
		$child_list = (array)null;
		foreach ($parent_list as $cur_anat){		
			if(count(array_intersect($leaf_lsts[$cur_anat],$expand_list))>=count($leaf_lsts[$cur_anat])){
				$ret_list[]=$cur_anat;
			} else {
				$child_list = array_merge($child_list, $hierarch[$cur_anat]);
			}
		}
		if(count($child_list)==0){
			break;
		}
		$parent_list = $child_list;
	}		
	return $ret_list;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$clustid=$_GET["clusterid"];
$aniseed_gm=str_replace("cluster","aniseedV3_",$clustid);

$regs=(array) null;

//// get gene models related to the gene give by clustid
$gm_name=(array) null;
$gm_id=(array) null;
$requete="select \"NAME\", \"GENE_ID\" from \"GENE\" where \"GENE_ID\" in (
SELECT \"GENE_MODEL_ID\" from \"GENE_CONCEPT\" where \"GENE_CLUSTER_ID\"='$clustid'
)";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt)) {
	$gm_name[count($gm_name)]=$row["NAME"];
	$gm_id[count($gm_id)]=$row["GENE_ID"];
}

//// agregation des modeles de genes
$agreg_gm="";
for ($i=0;$i<count($gm);$i++)	{
	if (strlen($agreg_gm)==0) $agreg_gm=$gm[$i];
	else $agreg_gm.=":".$gm[$i];
}

//// get name of the clustid
$name=(array) null;
$requete="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$clustid'";
$stmt=pg_query($c,$requete) or die (pg_last_error($c));
while ($row=pg_fetch_array($stmt))	 { $name[count($name)]=$row["GENE_CLUSTER_NAME"]; }

if (count($name)==0) {
	$requete="SELECT \"GENE_CLUSTER_NAME_BLAST\" from \"GENE_CONCEPT_NAME_BLAST\" where \"GENE_CLUSTER_ID\"='$clustid'";
	$stmt=pg_query($c,$requete);
	while ($row=pg_fetch_array($stmt)) { $name[count($name)]=$row["GENE_CLUSTER_NAME_BLAST"]; }
}





?>

<?

print "<br><br><font color=\"#D64242\" size=\"3\"><b>Gene Function</b></font><br><br>";

print "<table border=\"0\" width=\"100%\">";
$gene_name=implode (" / ",$name);
print "<tr><td><font color=\"#456898\" size=\"2\"><b>Gene Name :</b></font></td><td><font size=\"2\">$gene_name</font></td></tr>";
print "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
print "<tr><td><font color=\"#456898\" size=\"2\"><b>Aniseed Gene id:</b></font></td><td><font size=\"2\">$aniseed_gm</font></td></tr>";
print "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";

//ARE THERE ANY MORPHOLINOS AGAINST THIS GENE?
$khoueiry="SELECT \"MUTANT\".\"IN_SITU_ID\", \"CTRL_ISH_ID\", \"DEV_STAGE\".\"DEV_STAGE_ID\", \"DEV_STAGE\".\"NAME\" FROM 
	\"MUTANT\", \"IN_SITU\", \"DEV_STAGE\"  WHERE 
	\"IN_SITU\".\"IN_SITU_ID\"=\"MUTANT\".\"IN_SITU_ID\" AND \"DEV_STAGE\".\"DEV_STAGE_ID\"=\"IN_SITU\".\"DEV_STAGE_ID\" AND 
	\"MUTANT\".\"NAME\" IN 
		(SELECT A.\"NAME\" FROM \"GENE\" A, \"GENE_CONCEPT\" B WHERE A.\"GENE_ID\"=B.\"GENE_MODEL_ID\" AND \"GENE_CLUSTER_ID\"='$clustid') 
	ORDER BY \"DEV_STAGE\".\"DEV_PERCENT\" " ;
$stmt = pg_query($c,$khoueiry);
$mut_insitus=(array) null;
$mut_insitus_ctrl=(array) null;
$mut_dev_stages=(array) null;
$all_dev_stages=(array) null;
$all_dev_stages_ids=(array) null;
while($row=pg_fetch_array($stmt)){ 
	$mut_dev_stages[count($mut_insitus)] = $row["NAME"]; 
	$mut_insitus[count($mut_insitus)] = $row["IN_SITU_ID"]; 	
	
	$mut_insitus_ctrl[$row["IN_SITU_ID"]] = $row["CTRL_ISH_ID"]+0;
	
	if(!in_array($row["NAME"],$all_dev_stages)){
		$all_dev_stages_ids[count($all_dev_stages)] = $row["DEV_STAGE_ID"];
		$all_dev_stages[count($all_dev_stages)] = $row["NAME"];		
	}
}

//calculate hierarchy and stuff for anatomy simplification
$anat_ids=(array) null;
$hierarchy=(array) null;
$parent_lsts=(array) null;
$leaf_lsts=(array) null;

for ($i=0;$i<count($all_dev_stages);$i++) {
	$khoueiry = "SELECT A.\"NAME\" AS \"CHILD\", B.\"NAME\" AS \"PARENT\",  A.\"ANAT_ID\" AS \"CHILD_ID\", B.\"ANAT_ID\" AS \"PARENT_ID\" 	FROM \"ANAT_ENTITY\" A, \"ANAT_ENTITY\" B WHERE A.\"CHILD_OF\"=B.\"ANAT_ID\" AND A.\"DEV_STAGE_ID\"=".$all_dev_stages_ids[$i];
	$stmt = pg_query($c,$khoueiry);
	$anat_lst=(array) null;
	$inv_hierarchy=(array) null;
	while($row=pg_fetch_array($stmt)){
		$anat_ids[$all_dev_stages[$i]][$row["CHILD"]]=$row["CHILD_ID"];
		$anat_ids[$all_dev_stages[$i]][$row["PARENT"]]=$row["PARENT_ID"];
		$inv_hierarchy[$row["CHILD"]]=$row["PARENT"];	
		$hierarchy[$all_dev_stages[$i]][$row["PARENT"]][]=$row["CHILD"];
		$anat_lst[$row["CHILD"]]=1;	
		$anat_lst[$row["PARENT"]]=1;			
	}
	foreach ($anat_lst as $anat => $bogus){		

		$cur_par = $anat;		
		$par_lst=(array) null;
		while(true){
			$par_lst[count($par_lst)] = $cur_par;
			if(!isset($inv_hierarchy[$cur_par])){
				break;
			} 
			$cur_par = $inv_hierarchy[$cur_par];
		}
		$parent_lsts[$all_dev_stages[$i]][$anat] = $par_lst;
	
		$to_process=array($anat);
		while(count($to_process)>0){
			$cur = array_pop($to_process);
			if(!isset($hierarchy[$all_dev_stages[$i]][$cur])){
				$leaf_lsts[$all_dev_stages[$i]][$anat][] = $cur;
			} else {
				foreach($hierarchy[$all_dev_stages[$i]][$cur] as $child){
					array_push($to_process, $child);
				}
			}					
		}
		
	}	
}

for ($i=0;$i<count($mut_insitus);$i++) {

	// What is the gene that is being checked... 
	$insitt = $mut_insitus[$i];
	$cur_dev = $mut_dev_stages[$i];
	//print "<tr><td>$insitt</td><td>&nbsp;</td></tr>";
	
	//What are the different regions of this mutant... I'm assuming that target is always the same in the same in-situ (may not be true!!)
	$khoueiry = "SELECT DISTINCT \"REGION_ID\", \"ANAT_ENTITY\".\"NAME\" FROM \"TARGET\", \"ANAT_ENTITY\" WHERE 
		\"REGION_ID\"=\"ANAT_ID\" AND \"IN_SITU_ID\"=".$insitt;	
	$stmt = pg_query($c,$khoueiry);
	$mut_regions=(array) null;	
	while($row=pg_fetch_array($stmt)){ 
		$mut_regions[count($mut_regions)] = $row["NAME"];	
	}
	
	$mut_regions = simplify_hierarchy($hierarchy[$cur_dev],$leaf_lsts[$cur_dev],$mut_regions);
	
	$khoueiry = "		
		(SELECT DISTINCT \"GENE_CLUSTER_ID\" FROM \"TARGET\", \"GENE_CONCEPT\" WHERE 
			\"TARGET_ID\"=\"GENE_MODEL_ID\" AND \"IN_SITU_ID\"=$insitt)
	UNION
		(SELECT DISTINCT \"GENE_CLUSTER_ID\" FROM \"TARGET\", \"CLONE_SEQ\", \"GENE_CONCEPT\" WHERE 
			\"TARGET\".\"TARGET_ID\"=\"CLONE_SEQ\".\"CLONE_SEQ_ID\" AND 
			\"ANISEED_CLUSTER_ID\"=\"GENE_MODEL_ID\" AND \"IN_SITU_ID\"=$insitt)
	UNION
		(SELECT DISTINCT \"GENE_CLUSTER_ID\" FROM \"TARGET\", \"CLONE\", \"CLONE_SEQ\", \"GENE_CONCEPT\" WHERE 
			\"TARGET\".\"TARGET_ID\"=\"CLONE\".\"CLONE_ID\" AND \"CLONE_SEQ\".\"CLONE_NAME\"=\"CLONE\".\"CLONE_NAME\" AND 
			\"ANISEED_CLUSTER_ID\"=\"GENE_MODEL_ID\" AND \"IN_SITU_ID\"=$insitt)		
		";
		
	$stmt = pg_query($c,$khoueiry);
	$mut_genes=(array) null;		
	while($row=pg_fetch_array($stmt)){ $mut_genes[count($mut_genes)] = $row["GENE_CLUSTER_ID"];	}
	
	for ($j=0;$j<count($mut_genes);$j++) {
		//Get the wild-type expression of genes...
		$cgene = $mut_genes[$j];	
		$aniseed_cgene=str_replace("cluster","aniseedV3_",$cgene);		
		$khoueiry = "SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" WHERE 
			\"DEV_STAGE_ID\"=(SELECT \"DEV_STAGE_ID\" FROM \"IN_SITU\" WHERE \"IN_SITU_ID\"=$insitt) AND
			\"WILD_TYPE\"='y' AND \"IN_SITU_ID\" IN 
		( 
			(SELECT DISTINCT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"TARGET_ID\" IN 
			(SELECT \"GENE_MODEL_ID\" FROM \"GENE_CONCEPT\" WHERE \"GENE_CLUSTER_ID\"='$cgene'))
		UNION 
			(SELECT DISTINCT \"IN_SITU_ID\" FROM \"TARGET\", \"CLONE_SEQ\" WHERE 
				\"TARGET\".\"TARGET_ID\"=\"CLONE_SEQ\".\"CLONE_SEQ_ID\" AND \"ANISEED_CLUSTER_ID\" IN 
					(SELECT \"GENE_MODEL_ID\" FROM \"GENE_CONCEPT\" WHERE \"GENE_CLUSTER_ID\"='$cgene')) 
		UNION 
			(SELECT DISTINCT \"IN_SITU_ID\" FROM \"TARGET\", \"CLONE\", \"CLONE_SEQ\" WHERE 	
				\"TARGET\".\"TARGET_ID\"=\"CLONE\".\"CLONE_ID\" AND \"CLONE_SEQ\".\"CLONE_NAME\"=\"CLONE\".\"CLONE_NAME\" AND 
				\"CLONE_SEQ\".\"ANISEED_CLUSTER_ID\" IN 
					(SELECT \"GENE_MODEL_ID\" FROM \"GENE_CONCEPT\" WHERE \"GENE_CLUSTER_ID\"='$cgene'))
		) ";
	
		$stmt = pg_query($c,$khoueiry);
		$comp_insitus=(array) null;
		while($row=pg_fetch_array($stmt)){ $comp_insitus[count($comp_insitus)] = $row["IN_SITU_ID"]; }	
	
		for ($k=0;$k<count($comp_insitus);$k++) {
			$khoueiry = "SELECT DISTINCT \"REGION_ID\", \"ANAT_ENTITY\".\"NAME\" FROM \"TARGET\", \"ANAT_ENTITY\" WHERE 
				\"REGION_ID\"=\"ANAT_ID\" AND \"IN_SITU_ID\"=".$comp_insitus[$k];	
			$stmt = pg_query($c,$khoueiry);
			$comp_regions=(array) null;		
			while($row=pg_fetch_array($stmt)){ 
				$comp_regions[count($comp_regions)] = $row["NAME"]; 
			}		
			
			$comp_regions = simplify_hierarchy($hierarchy[$cur_dev],$leaf_lsts[$cur_dev],$comp_regions);
			
			for ($l=0;$l<count($comp_regions);$l++){
				$region = $comp_regions[$l];
				if(!in_array($region, $mut_regions) && $region!='No expression') { 
					$expressed = false;
					foreach ($mut_regions as $m_reg){ if(in_array($m_reg,$parent_lsts[$cur_dev][$region])){$expressed=true;}}	
					if($expressed==false){
						if($up_down=='downregulated'){						
							$regs[$cur_dev][$aniseed_cgene][$region]["downregulates"][$insitt][$comp_insitus[$k]] = 1;
						} else {	
							$regs[$cur_dev][$aniseed_cgene][$region]["upregulates"][$insitt][$comp_insitus[$k]] = 1;						
						}					
					}
				}	
			}				
		
			for ($l=0;$l<count($mut_regions);$l++){
				$region = $mut_regions[$l];
				if(!in_array($region, $comp_regions) && $region!='No expression') { 
					$expressed = false;
					foreach ($comp_regions as $c_reg){ if(in_array($c_reg,$parent_lsts[$cur_dev][$region])){$expressed=true;}}	
					if($expressed==false){
						if($up_down=='downregulated'){						
							$regs[$cur_dev][$aniseed_cgene][$region]["upregulates"][$insitt][$comp_insitus[$k]] = 1;
						} else {	
							$regs[$cur_dev][$aniseed_cgene][$region]["downregulates"][$insitt][$comp_insitus[$k]] = 1;
						}
					}
				}
			}
		}	
	}
}

for ($i=0;$i<count($all_dev_stages);$i++) {
//foreach($regs as $stage => $stage_list){

	$stage = $all_dev_stages[$i];
	$stage_id = $all_dev_stages_ids[$i];
	
	$stage_list = $regs[$stage];
	if(count($stage_list)>0){	
		print "<tr><td>$stage</td><td>&nbsp;</td></tr>";
	}
	
//foreach($regs as $stage => $stage_list){
	foreach ($stage_list as $victim => $cve){
		//$vic_cluster = $victim;
		//$victim=str_replace("cluster","aniseedV3_",$victim);
		$vic_cluster = str_replace("aniseedV3_","cluster",$victim);			
		
		//// get name of the clustid
		$name=(array) null;
		$requete="SELECT \"GENE_CLUSTER_NAME\" from \"GENE_CONCEPT_NAME\" where \"GENE_CLUSTER_ID\"='$vic_cluster'";
		$stmt=pg_query($c,$requete) or die (pg_last_error($c));
		while ($row=pg_fetch_array($stmt))	 { $name[count($name)]=$row["GENE_CLUSTER_NAME"]; }
		if (count($name)==0) {
			$requete="SELECT \"GENE_CLUSTER_NAME_BLAST\" from \"GENE_CONCEPT_NAME_BLAST\" where \"GENE_CLUSTER_ID\"='$vic_cluster'";
			$stmt=pg_query($c,$requete);
			while ($row=pg_fetch_array($stmt)) { $name[count($name)]=$row["GENE_CLUSTER_NAME_BLAST"]; }
		}
		$vic_name = implode (" / ",$name);
			
		foreach ($cve as $region => $cre){
			foreach($cre as $up_down => $udre){
			
				$in_sits_muts = (array)null;
				$in_sits_wts = (array)null;
				
				foreach ($udre as $mut_ish => $wt_ish){
					if(!in_array($mut_ish,$in_sits_muts)){ $in_sits_muts[]= $mut_ish; }
					foreach ($wt_ish as $wt => $bogus) {
						if(!in_array($wt,$in_sits_wts)){ $in_sits_wts[]= $wt; }
					}									
				}		
				
				$region_id = $anat_ids[$stage][$region];
				$region_link = "<a href=\"anatomy.php?dev=$stage_id&region=$region_id\" target=\"_blank\"> $region </a>";				
				$evidence = "<font size=\"1\"><a href=\"display-evidence.php?insitus=".implode(":",$in_sits_muts ).":".implode(":",$in_sits_wts )."\" target=\"_blank\">evidence</a></font>";	
				
				print "<tr><td>&nbsp;</td><td> $aniseed_gm $up_down <a href=\"gene-card.php?clusterid=$vic_cluster\" title=\"$vic_name\">  $victim </a> in $region_link ($evidence) </td></tr>";

				$t[count($t)]=$victim;
				$rel[count($rel)]="$victime@".strtoupper($up_down{0});

				display_network($t,$rel,$aniseed_gm,500,500);

				print_r($t);
				print_r($rel);



				break;
			}
			print "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
		}
	}
}


print "<tr><td>&nbsp;</td><td>&nbsp;</td></tr></table>";

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