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


<?php

if (strlen($_POST["species"])>0)
	{
	$species=$_POST["species"];
	}
else
	{
	print "<br><br><center><strong>Please, choose a species</stron></center><br><br>";
	include ("frames_footer.php");
	return;
	}


$testsize=0;
$test=$_POST["test"];
if($test!='y'){
	$test='n';
}else print "test mode<br>";

//$path=$_POST["filepath"];
$path="./ensembl.gff";
print $path;
$file = fopen ("$path", "r");

//pour le scaffold
//$scaff="";
$scaffoldOK=false;
$requete="SELECT * from \"SCAFFOLD\"";
$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0) $scaffoldOK=true;

pg_query($c,"begin");
while ($line = fgets($file)){
//while (!feof($file)){
//$line=fgets($file,8192);


	$line=rtrim($line);

	if( eregi("^Scaffold",$line) ){
	    $data=split("\t",$line);

		//pour le scaffold
		$data[0]=strtolower($data[0]);
		$req="SELECT * from \"SCAFFOLD\" where \"SCAFFOLD\"='".$data[0]."'";
		$st=pg_query($c,$req);
		if (pg_num_rows($st)==0)
			{
			//On fait une opération car on a trouvé un nouveau scaffold
			 if($test=="n"){
				
					if ($scaffoldOK)
					{
					//La table scaffold est vide
					$requete="INSERT into \"SCAFFOLD\" (\"SCAFFOLD_ID\",\"SPECIES\")
					values ('".$data[0]."','$species')";
					$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));				

					}
		



				
					}

			//$scaff=$data[0];
			}


		if($data[2]=="exon"){
		  /*  ereg('name "(.+)";',$data[8],$reg);

			$gene=$reg[1];

			if (strlen($gene)==0)
				{*/
				ereg('Transcript (.+) ;',$data[8],$reg);
				$gene=$reg[1];
				/*}*/


			$alldata[$gene][scaffold]=strtolower($data[0]);
			$alldata[$gene][exon].=$data[3].'-'.$data[4].';';
			$alldata[$gene][strand]=$data[6];

			//print "$gene => ".$data[0]." / ".$data[3]."-".$data[4]." / ".$data[6]."<br>";

		}
    }
}
pg_query($c,"commit");

//print_r($alldata);
//return;

foreach($alldata as $key=>$value){
    if ( (strlen($alldata[$key][scaffold])==0)or(strlen($alldata[$key][strand])==0)or(strlen($alldata[$key][exon])==0) ){
	    print "ERROR: Missing value";
		return;
	}
    
	if ( strlen($alldata[$key][exon])>$testsize)
	    $testsize=strlen($alldata[$key][exon]);
	
	$minmax=split (';',$alldata[$key][exon]);
	$max=0; $min=1000000000;
	for($a=0;$a<count($minmax);++$a){
	    ereg("([0-9]+)\-([0-9]+)",$minmax[$a],$reg);
		$aa=$reg[1];  $bb=$reg[2];
		
		if($aa<$min)
		    $min=$aa;
		if($bb<$min)
		    $min=$bb;
		
		if($aa>$max)
		    $max=$aa;
		if($bb>$max)
		    $max=$bb;
	}
	
    print $key.':<br>'.$alldata[$key][scaffold]."<br>".$alldata[$key][strand]."<br>".$alldata[$key][exon]."<br>$min-$max<br><br>";

    if($test=="n"){
		$requete="update \"GENE\" set \"STRAND\"='".$alldata[$key][strand]."',\"INTRON_EXON\"='".$alldata[$key][exon]."',\"SCAFFOLD_ID\"='".$alldata[$key][scaffold]."', \"BEGIN\"='".$min."', \"END\"='".$max."' where \"NAME\"='".$key."'";
		
		/*Debut de la transaction*/

		$stmt=pg_query($c,"begin");

		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		
		// Validation de la transaction
		$committed = pg_query($c,"commit");
		// V�ification de la validation : si une erreur est survenue, afficher le message d'erreur
		if(!$committed) {
			//$error = OCIError($connection);
			echo 'ERROR:'.$error['message'];
			return;
		}
		
	}
}

	fclose ($file);
    print "<br><br>ok, no problems. max intron/exon record size=$testsize<br><br>";
?>


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