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


<?php
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<center><strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
	    print '</center></body></html>';
	   include ("frames_footer.php");
		return;
	}
?>


<?
$name=$_GET["name"];
$supplier=$_GET["supplier"];
$type=$_GET["type"];
$comments=$_GET["comments"];
$sequence=$_GET["sequence"];
$comseq=$_GET["com-sequence"];
$pmid=$_GET["pmid"];


print "$name<br>$supplier<br>$type<br>$comments<br>$sequence<br>$comseq<br>$pmid";


//Test de presence
if (strlen($name)==0) {
print "<center><strong>Please choose a name !</strong></center>";
include("frames_footer.php");
return;
}

pg_query($c,"begin");


$requete1="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\",\"CURATED\") values (nextval('\"S_BIO_ID\"'),'y')";

pg_query($c,$requete1) or die (pg_last_error($c));


//Pour la table MOL_ENTITY
$requete3="INSERT INTO \"MOL_ENTITY\" (\"MOL_ID\",\"NAME\",\"TYPE\") values (currval('\"S_BIO_ID\"'),'$name','tool');";
//print "<br><br>$requete3";

pg_query($c,$requete3) or die (pg_last_error($c));



$requete="insert into \"MOLECULAR_TOOL\" (\"MOLECULAR_TOOL_ID\",\"NAME\",\"TYPE\",";
//print $requete;

if (strlen($supplier)!=0) $requete.="\"SUPPLIER\",";
if (strlen($comments)!=0) $requete.="\"COMMENTS\",";

$requete=substr($requete,0,strlen($requete)-1);


$requete.=" ) values (currval('\"S_BIO_ID\"'),'$name','$type',";
if (strlen($supplier)!=0) $requete.="'$supplier',";
if (strlen($comments)!=0) 
	{
	$comments=str_replace("'","",$comments);
	$comments=str_replace("\n"," ",$comments);
	$comments=str_replace("\r"," ",$comments);

	$requete.="'$comments',";
	}

$requete=substr($requete,0,strlen($requete)-1);

$requete.=" );";

//print $requete;
pg_query($c,$requete) or die (pg_last_error($c));




//Pour inserer une nouvelle sequence
if (strlen($sequence)!=0)
	{
	//Traitement préalable sur la sequence
	$seq=strtoupper($sequence);
				$freqA=substr_count(strtoupper ($seq), "A"); 
				//echo "<br>freq A = $freqA";
				$freqG=substr_count(strtoupper ($seq), "G"); 
				//echo "<br>freq G  = $freqG";
				$freqC=substr_count(strtoupper ($seq), "C"); 
// 				echo "<br>freq C = $freqC";
				$freqT=substr_count(strtoupper ($seq), "T"); 
// 				echo "<br>freq T = $freqT";
				
				$freqtot= $freqA+$freqC+$freqG+$freqT;
				
				$seq2=ereg_replace("[^[:alpha:]]","",$seq);
				$size=strlen($seq2);
				$freqother=$size-$freqtot;
// 				echo "<br>freq OTHER = $freqother";
// 				echo "<br>size= $size<br>";
				
				$seqcomments="BASE COUNT       ".$freqA."a      ".$freqC."c      ".$freqG."g      ".$freqT."t      ".$freqother."others\n";


						$oid=pg_lo_create($c); /*Creation de l'oid de la sequence*/
						$requete4="INSERT into \"SEQUENCE\" (\"MOL_ID\", \"SEQUENCE\", \"LENGTH\", \"COMMENTS\") values (currval('\"S_BIO_ID\"'),'$oid','$size','$seqcomments')";
						$result=pg_query($c, $requete4) or die("<strong>Error</strong> ".pg_last_error($c));
						$handler=pg_lo_open($c,$oid,"w"); /*Ouverture du flux*/
						pg_lo_write($handler,$seq); /*Ecriture de la sequence*/
						pg_lo_close($handler); /*Fermeture du flux*/

						//print $requete4;

	}


if (strlen($pmid)!=0)
	{

$resource=curl_init("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?id=$pmid&db=pubmed&report=xml");
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
$contents=curl_exec($resource);
curl_close($resource);



//print $contents;
//Nom de l'article
$contents2=str_replace("&lt;ArticleTitle&gt;","§",$contents);
$contents2=str_replace("&lt;/ArticleTitle&gt;","§",$contents2);
$tab=split("§",$contents2);
$title=$tab[1];

//Nom du journal
$contents2=str_replace("&lt;MedlineTA&gt;","§",$contents);
$contents2=str_replace("&lt;/MedlineTA&gt;","§",$contents2);
$tab=split("§",$contents2);
$paper=$tab[1];

//Nom des auteurs
$contents2=str_replace("&lt;LastName&gt;","§",$contents);
$contents2=str_replace("&lt;/LastName&gt;","§",$contents2);
$tab=split("§",$contents2);
for ($i=1;$i<count($tab)-1;$i++)
	if ($i%2!=0) $auteurN[count($auteurN)]=$tab[$i];
$contents2=str_replace("&lt;Initials&gt;","§",$contents);
$contents2=str_replace("&lt;/Initials&gt;","§",$contents2);
$tab=split("§",$contents2);
for ($i=1;$i<count($tab)-1;$i++)
	if ($i%2!=0) $auteurI[count($auteurI)]=$tab[$i];
$auteur="";
for ($i=0;$i<count($auteurN)-1;$i++)
$auteur.=$auteurN[$i]." ".$auteurI[$i].", ";
$auteur.=$auteurN[count($auteurN)-1]." ".$auteurI[count($auteurN)-1];

//Le volume
$contents2=str_replace("&lt;Volume&gt;","§",$contents);
$contents2=str_replace("&lt;/Volume&gt;","§",$contents2);
$tab=split("§",$contents2);
$volume=$tab[1];

//L'issue
$contents2=str_replace("&lt;Issue&gt;","§",$contents);
$contents2=str_replace("&lt;/Issue&gt;","§",$contents2);
$tab=split("§",$contents2);
$issue=$tab[1];

//L'année
$contents2=str_replace("&lt;Year&gt;","§",$contents);
$contents2=str_replace("&lt;/Year&gt;","§",$contents2);
$tab=split("§",$contents2);
$year=$tab[1];

//la page
$contents2=str_replace("&lt;MedlinePgn&gt;","§",$contents);
$contents2=str_replace("&lt;/MedlinePgn&gt;","§",$contents2);
$tab=split("§",$contents2);
$page=$tab[1];


$paper.=". ".$year." ".$month.";".$volume."(".$issue."):".$page;

//}FIN IF TEMPORAIRE


$requete2="INSERT INTO \"REFERENCE\" (\"BIO_ID\",\"PUBMED_ID\",\"TITLE\",\"PAPER\",\"AUTHORS\") values (currval('\"S_BIO_ID\"'),'$pmid','$title','$paper','$auteur');";

//print "<br><br>$requete2";
pg_query($c,$requete2) or die (pg_last_error($c));

	
	}



/*if (strlen($sequence)!=0) $COUNT+=4;
if (strlen($comseq)!=0) $COUNT+=8;
*/


pg_query($c,"commit");


print "<center><h1>Insert done : new molecular tool <em> $name </em> has been created</h1></center>";






?>

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