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

<center>
<?php
	
	$name=$_GET["name"];
	if(strlen($name)==0){
	    print '<center><h2><strong>Missing parameter: molecule name.</strong></h2></center>';
		include("frames_footer.php");
		return;
	}
	
	
	
	$species=$_GET["species"];
	
	
    if( $_GET["type"] == 1 ){
	    //recherche de seq de clone
	    $name=strtoupper($name);
		$name=ereg_replace("^ +","",$name);
		$name=ereg_replace(" +$","",$name);
		$requete="select B.\"SPECIES\", A.* from \"CLONE_SEQ\" A, \"MOL_ENTITY\" B where (B.\"MOL_ID\"=A.\"CLONE_SEQ_ID\")and(UPPER(A.\"NAME\") like '%$name%' or UPPER(A.\"GENBANK_ID\") like '%$name%')";
		
		if( strlen($species)>1 ){
		    $requete.=" and (B.\"SPECIES\"='$species')";
		}
        
		$requete.=" order by A.\"CLONE_NAME\"";
		
		$stmt = pg_query($c,$requete);
		
		print "<h2><strong><em>Matching records:</em></strong> </h2>";
		print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
		print '<tr align="center" valign="middle" bgcolor="#990000"><td>Genbank ID</td><td>Clone name</td><td>End</td><td background="pictures/barre-red.jpg">Species</td></tr>';
		$count=0;
		while( $row=pg_fetch_array($stmt) ){
		    ++$count;
			print "<tr align=\"center\" valign=\"middle\" >"."<td><a href=\"molecule-clone.php?name=".$row["GENBANK_ID"]."\">".$row["GENBANK_ID"]."</a></td><td><a href=\"molecule-clone.php?name=".$row["GENBANK_ID"]."\">".$row["CLONE_NAME"]."</a></td><td>".$row["END"]."</td><td>".$row["SPECIES"]."</td></tr>";
			print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
			if($count>500){
			    //OCICancel($stmt);
			pg_cancel_query($stmt);
			    break;
			}
		}

		print '</table>';
		
		if($count==0){
		    print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
		}else{
		    print "<br><center>$count result(s)</center><br>";
		}
		if($count>500){
		    print "...<br>More than 500 results has been returned.<br>Please refine your query";
		}
		
    }else if( $_GET["type"] == 3 ){
	    //recherche de clone
	    $name=strtoupper($name);
		$name=ereg_replace("^ +","",$name);
		$name=ereg_replace(" +$","",$name);
		$requete="select B.\"SPECIES\", A.* from \"CLONE\" A, \"MOL_ENTITY\" B where (B.\"MOL_ID\"=A.\"CLONE_ID\")and(UPPER(A.\"CLONE_NAME\") like '%$name%')";
		
		if( strlen($species)>1 ){
		    $requete.=" and (B.\"SPECIES\"='$species')";
		}        	
		$requete.=" order by A.\"CLONE_NAME\"";
		
		$stmt = pg_query($c,$requete);
		
		print "<h2><strong><em>Matching records:</em></strong> </h2>";
		print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
		print '<tr align="center" valign="middle" bgcolor="#990000"><td>Clone name</td><td background="pictures/barre-red.jpg">Species</td></tr>';
		$count=0;
		while( $row=pg_fetch_array($stmt) ){
		    ++$count;
			print "<tr align=\"center\" valign=\"middle\" >"."<td><a href=\"molecule-clone2.php?name=".$row["CLONE_NAME"]."\">".$row["CLONE_NAME"]."</a></td><td>".$row["SPECIES"]."</td></tr>";
			print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td></tr>';
			
			if($count>500){
			    //OCICancel($stmt);
				pg_cancel_query($stmt);
			    break;
			}
		}

		print '</table>';
		
		if($count==0){
		    print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
		}else{
		    print "<br><center>$count result(s)</center><br>";
		}
		if($count>500){
		    print "...<br>More than 500 results has been returned.<br>Please refine your query";
		}
		
		
    }else if($_GET["type"] == 2){
	
	    //recherche de gene
		
		$orig="''";
	    if($_GET["auto"]=="y")
		    $orig.=",'phylogeny'";
	    if($_GET["user"]=="y")
		    $orig.=",'user-JGI'";
		
		
        $name=strtoupper($name);
        $name=ereg_replace("[^a-zA-Z0-9]"," ",$name);
		$name=ereg_replace("([a-zA-Z])([0-9])","\\1 \\2",$name);
		$name=ereg_replace("([0-9])([a-zA-Z])","\\1 \\2",$name);
		$name=ereg_replace(" +"," ",$name);
		$name=ereg_replace("^ ","",$name);
		$name=ereg_replace(" $","",$name);

        if($orig!="''"){
			$requete="SELECT A.\"GENE_ID\", A.\"NAME\", B.\"SYNO\", B.\"ORIGIN\", C.\"SPECIES\" FROM \"GENE\" A, \"SYNO\" B, \"MOL_ENTITY\" C WHERE (B.\"ORIGIN\" in ($orig) or B.\"ORIGIN\" is NULL) AND (B.\"SYNO_UPPER_CASE\" LIKE '%$name%') AND (B.\"GENE_ID\"=A.\"GENE_ID\") AND (B.\"GENE_ID\"=C.\"MOL_ID\") ";
			
			if( strlen($species)>1 ){
				$requete.=" AND (C.\"SPECIES\" ='$species') ";
			}
			$requete.="order by A.\"GENE_ID\"";
			
			$stmt = pg_query($c,$requete);
			
			print "<h2><strong><em>Matching records:</em></strong> </h2>";
			print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
			print '<tr align="center" valign="middle" bgcolor="#990000"><td>Gene ID</td><td>Gene name</td><td background="pictures/barre-red.jpg">Species</td></tr>';
			$count=0;
			$last="";
			while( $row=pg_fetch_array($stmt) ){
				
				$nname=$row["NAME"];
				$nspecies=$row["SPECIES"];
				if($last != $row["NAME"]){
					$last = $row["NAME"];
					++$count;
					print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
				}else{
					$nname="";
					$nspecies="";
				}
				
				print "<tr align=\"center\" valign=\"middle\" >"."<td><a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$nname."</a></td><td><a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$row["SYNO"]."</a>";
				if($row["ORIGIN"]=="user-JGI")
					print '*';
				print "</td><td>".$nspecies."</td></tr>";
				
				if($count>500){
					//OCICancel($stmt);
					pg_cancel_query($c);
					break;
				}
			}
			print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
			print '</table>';
			
			if($count==0){
				print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
			}else{
				print "<br><center>$count result(s)</center><br>";
				print "</center><br><br>* Manual annotation<center>";
			}
			
			if($count>500){
				print "...<br>More than 500 results has been returned.<br>Please refine your query";
			}
        }

        ////////////////////////////////////////////////////////////////////////////////////////
		if( (isset($_GET["blast"]))and($_GET["blast"]=="y") ){//recherche dans les 5 best blasts
		
			print "<br></center><strong><em>Best BLAST hits records:</em></strong><center>";
			print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
			print '<tr align="center" valign="middle" bgcolor="#990000"><td>Gene ID</td><td>Species</td><td>BLAST hit description</td><td>ID</td><td>Evalue</td><td background="pictures/barre-red.jpg">Score</td></tr>';
			$count=0;
			$requete="SELECT A.\"GENE_ID\", A.\"NAME\", B.\"ID\", B.\"SCORE\", B.\"EVALUE\", B.\"DESCRIPTION\", C.\"SPECIES\" FROM \"GENE\" A, \"BLAST\" B, \"MOL_ENTITY\" C WHERE (UPPER(B.\"DESCRIPTION\") LIKE '%$name%') AND (B.\"MOL_ID\"=A.\"GENE_ID\") AND (B.\"MOL_ID\"=C.\"MOL_ID\") ";
			if( strlen($species)>1 ){
				$requete.=" AND (C.\"SPECIES\" ='$species') ";
			}
			$requete.="order by A.\"GENE_ID\"";
			$stmt = pg_query($c,$requete);
			$last="";
			while( $row=pg_fetch_array($stmt) ){
				$nname=$row["NAME"];
				if($last != $row["NAME"]){
					$last = $row["NAME"];
					++$count;
					print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
				}else{
					$nname="";
				}
			    
				print "<tr align=\"center\" valign=\"top\" >"."<td><a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$nname."</a></td><td>".$row["SPECIES"]."</td><td><font size=\"-1\"><a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$row["DESCRIPTION"]."</a></font></td><td>".$row["ID"]."%</td><td>".$row["EVALUE"]."</td><td>".$row["SCORE"]."</td></tr>";
				if($count>500){
					//OCICancel($stmt);
					pg_cancel_query($stmt);
					break;
				}
			}
			print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
			print '</table>';
			
			if($count==0){
				print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
			}else{
				print "<br><center>$count result(s)</center><br><br>";
			}
			
			if($count>500){
				print "...<br>More than 500 results has been returned.<br>Please refine your query";
			}
        }
	
	
	}else{
        print '<br><br><br><br>';
        print '<center><img src="pictures/construction.gif"><br><strong>Under Construction</strong></center>';
        print '<br><br><br><br>';
	}
?>
</center>

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