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

<?php
if (isset($_POST["filepath"]))
	$path=$_POST["filepath"];
else $path="";

//$path="updates\GO\process.ontology";
//$path="updates\GO\component.ontology";
//$path="updates\GO\function.ontology";

if (!($fd=fopen ($path, "r")))
{
	echo "$path cannot be read<br>";
	include("frames_footer.php");
	return;
}
else{
	echo "$path is open <br>";
}


if (isset($_POST["test"]))
	$test=$_POST["test"];
else $test='n';
if($test=='y'){
	echo "<h2>Test mode selected. No data will be recorded.</h2><br>";
}


$count=0;
$res=array(null);
while(!feof($fd))
{
	$line = fgets($fd, 4096);
	
	//print "$line<br>";
	if(ereg("^( +)[^ ]+",$line,$data)){
		$level=strlen($data[1]);
		
		ereg("(GO:[0-9]+)",$line,$data);
		$go=$data[1];
		
		//print "$go=$level<br>";
		
		if(isset($res[$go])){
			if( ereg("^$level",$res[$go]))
				continue;
			if( ereg(",$level,",$res[$go]))
				continue;
			if( ereg(",$level$",$res[$go]))
				continue;
			
			$res[$go].=",$level";
			
		}else{
			$res[$go]="$level";
		}
		/*
		if(++$count==500)
			break;
		*/
	}
}

foreach ($res as $key => $value) {
	//print "$key -> $value<br>";
	
	$data=explode(",", $value);
	natsort($data);
	$value=join(",", $data);
	
	print "$key -> $value<br>";
	
	if($test=='n'){
		$requete= "update \"GO\" set \"LEVEL\"= '$value' where \"GO_ID\"='$key'";
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		pg_query($c,"commit");
	}
	
}

?>

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