<?php
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
	//connection
	include("connection.php");
//Title before frames_header
print '<title>ANISEED - Transcript model result</title>';

    include("frames_header.php");
?>


<script>
function MakeList() {
		
		for(a=0; a<document.check.length; ++a){
			
			if (document.check[a].checked)
					{
					//document.write(document.forms.check.gene[a].value);
					document.formulaire3.genelist.value+=document.check[a].value+":";
					}
			//document.alert('salut');*/
		}
//document.alert(document.formulaire3.genelist);
}


function MakeList2() {
		
		for(a=0; a<document.check.length; ++a){
			
			if (document.check[a].checked)
					{
					//document.write(document.forms.check.gene[a].value);
					document.formulaire4.genelist.value+=document.check[a].value+":";
					}
			//document.alert('salut');*/
		}
//document.alert(document.formulaire3.genelist);
}

</script>


You can either click on the transcript name of interest to get the
associated transcript card, or tick right of the transcript ID name to constitute
a list of transcripts of interest that can be further restricted using the
"Refine" button that enables sequential requests.
<br>


<center>

<?php

print '<form name="check" action="">';

	$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 '<font color="#D64242" size="3"><b>Matching records</b></font><br><br>';
		print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
		print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Genbank ID</b></font></td><td><font color="#456898" size="2"><b>Clone name</b></font></td><td><font color="#456898" size="2"><b>End</b></font></td><td><font color="#456898" size="2"><b>Species</b></font></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"]."\"><font color=\"#000000\" size=\"-1\">".$row["GENBANK_ID"]."</font></a></td><td><a href=\"molecule-clone.php?name=".$row["GENBANK_ID"]."\"><font color=\"#000000\" size=\"-1\">".$row["CLONE_NAME"]."</font></a></td><td><font color=\"#656565\" size=\"-1\">".$row["END"]."</font></td><td><font color=\"#656565\" size=\"-1\">".$row["SPECIES"]."</font></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 '<font color="#D64242" size="3"><b>Matching records</b></font><br>';
		print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
		print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Clone name</b></font></td><td><font color="#456898" size="2"><b>Species</b></font></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"]."\"><font color=\"#000000\" size=\"-1\">".$row["CLONE_NAME"]."</font></a></td><td><font color=\"#656565\" size=\"-1\">".$row["SPECIES"]."</font></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"]==4)	{
		
	//Recherche d'une construction
		
	//Traitement sur la chaine
	$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);

	//Tableau fictif
	$fictif=(array) null;
	$species=(array) null;

	$requete="SELECT * from \"CONSTRUCTION\"";
	$stmt=pg_query($c,$requete);
	while ($row=pg_fetch_array($stmt)) {
		$fictif[$row["CONSTRUCTION_ID"]]=strtoupper($row["NAME"]);
		$species[$row["CONSTRUCTION_ID"]]=$row;
	}
	
	//Les resultats	
	print '<font color="#D64242" size="3"><b>Matching records</b></font><br><br>';	
	print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
	print '<tr align="center" valign="middle">
			<td><font color="#456898" size="2">Reference</font></td>
			<td><font color="#456898" size="2">Construction Name</font></td>
			<!-- <td><font color="#456898" size="2">Species</font></td> -->
		</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
					
	//Boucle
	$bool=false;

	//Selection des resultats
	foreach ($fictif as $key => $val)
	{
		$cur=$fictif[$key];
		$avant=strlen($cur);

		$cur=str_replace($name,"",$cur);
		$apres=strlen($cur);

		$requete="SELECT \"SPECIES\" from \"MOL_ENTITY\" where \"MOL_ID\"='".$key."'";
		$stmt=pg_query($c,$requete);
		$row=pg_fetch_array($stmt);

		if ($avant!=$apres) {
			$bool=true;
			print '<tr>';
			//print '<td align="center"><a href="molecule-promoter.php?name='.$key.'">'.$key.'</a></td>';	
			print '<td align="center">'.$species[$key]["STABLE_ID"].'</td>';
			print '<td align="center"><a href="construct-edit.php?constructid='.$key.'">'.$fictif[$key].'</a></td>';
			//print '<td align="center">'.$row["SPECIES"].'</td>';
			print '</tr>';
		}
	}


	print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
	print '</table>';

	if (!$bool) {
		print "<center><strong>No match</strong></center>";
		include ("frames_footer.php");
		return;
	}


	}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 '<font color="#D64242" size="3"><b>Matching records</b></font><br><br>';
			
			print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
			print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Transcript model ID</b></font></td><td><font color="#456898" size="2"><b>Transcript name</b></font></td><td><font color="#456898" size="2"><b>Species</b></font></td></tr>';
			$count=0;
			$last="";
			$lastcount=0;
			while( $row=pg_fetch_array($stmt) ){
			
				$nname=$row["NAME"];
				
				//print "Last=".$lastcount." count=".$count;

				$lastcount++;

				$nspecies=$row["SPECIES"];
				if($last != $row["NAME"]){
					$last = $row["NAME"];
					++$count;
					$lastcount=$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>";
			*/
			
//MODIF DE LA A -->
				print "<tr align=\"center\" valign=\"middle\" >";
				
				if ($count==$lastcount)
				{
				print "<td><font color=\"#656565\" size=\"-1\">
				$nname
				</font>
				<input type=\"checkbox\" name=\"gene[]\" value=\"$nname\" checked>
				
				</td>";
				}
				else
				print "<td></td>";
/*
				print $nname."==>";
				$requete0="SELECT \"SYNO\" from \"SYNO\" where \"GENE_ID\"=(SELECT \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$nname')";
				$stmt0=pg_query($c,$requete0);
				while ($row0=pg_fetch_array($stmt0)) print "(".$row0["SYNO"].")";
*/	
				print "
				<td><a href=\"molecule-gene.php?name=".$row["NAME"]."\"><font color=\"#000000\" size=\"-1\">".$row["SYNO"]."</font></a>";
				if($row["ORIGIN"]=="user-JGI")
					print '*';
				print "</td><td><font color=\"#656565\" size=\"-1\">".$nspecies."</font></td></tr>";
// LA <--			



				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></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
	
	
	$nameblast=ereg_replace(" ","_",$name);
	$nameblast2=ereg_replace(" ","",$name);

	
			print "<br></center><strong><em>Best BLAST hits records:</em></strong><center><br>";
			print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
			print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Transcript model ID</b></font></td><td><font color="#456898" size="2"><b>Species</b></font></td><td><font color="#456898" size="2"><b>BLAST hit description</b></font></td><td><font color="#456898" size="2"><b>ID</b></font></td><td><font color="#456898" size="2"><b>Evalue</b></font></td><td><font color="#456898" size="2"><b>Score</b></font></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 '%$nameblast%') OR (UPPER(B.\"DESCRIPTION\") LIKE '%$nameblast2%')  ) AND (B.\"MOL_ID\"=A.\"GENE_ID\") AND (B.\"MOL_ID\"=C.\"MOL_ID\")";
			//print $requete;
			if( strlen($species)>1 ){
				$requete.=" AND (C.\"SPECIES\" ='$species') ";
			}
			$requete.="order by B.\"SCORE\" DESC,A.\"GENE_ID\"";
			$stmt = pg_query($c,$requete);
			$last="";

			$lastcount=0;

			while( $row=pg_fetch_array($stmt) ){
				$nname=$row["NAME"];
				$lastcount++;
				
				if($last != $row["NAME"]){
					$last = $row["NAME"];
					++$count;
					$lastcount=$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>";
				*/

print "<tr align=\"center\" valign=\"top\" >";
if ($count==$lastcount)
{
print "
<td>
<font color=\"#656565\" size=\"-1\">$nname</font>
<input type=\"checkbox\" name=\"gene[]\" value=\"$nname\" checked>
</td>";
}
else
print "<td></td>";
print "
<td><font color=\"#656565\" size=\"-1\">".$row["SPECIES"]."</font></td><td><font color=\"000000\" size=\"-1\"><a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$row["DESCRIPTION"]."</a></font></td><td><font color=\"#656565\" size=\"-1\">".$row["ID"]."%</font></td><td><font color=\"#656565\" size=\"-1\">".$row["EVALUE"]."</font></td><td><font color=\"#656565\" size=\"-1\">".$row["SCORE"]."</font></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><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>";
			}
			
			if($count>500){
				print "...<br>More than 500 results has been returned.<br>Please refine your query";
			}
        }

if(substr_count($genelist,":")<1000){


print '</form>';

print '<form name="formulaire3" action="" method="post">';
	
	print '<input type="hidden" value="" name="genelist">';
	print '&nbsp;&nbsp;&nbsp;
	
	<input name="submit" type="image" src="pictures/button-subset.jpg" onClick="javascript: window.document.formulaire3.action= window.document.formulaire3.redirect.value; MakeList(); submit;">';
	
	print '<br>
		   <select name="redirect" size="1">
			   <option value="GO-search.php">Gene ontology</option>
			   <option value="IP-search.php">Interpro</option>
			   <option value="insitu-search.php">In situ</option>
			   <option value="DDD-search.php">Digital Differential Display</option>
			   <option value="MW-search.php">Molecular weight</option>
			   <option value="ID.php">Export ID list</option>
		   </select>';
		   
	print '</form>';

print "<br>";
print '<form name="formulaire4" action="SequenceFASTA.php" method="post">';
	
	print '<input type="hidden" value="" name="genelist">';
	//print '&nbsp;&nbsp;&nbsp; input name="submit" type="image" src="pictures/button-sequence.jpg" onClick="MakeList2(); submit;">';
	
	
	print '</form>';

}else{print '<br><font size="-1" color="#999999">Refine mode not available for set > 1000</font>';}



	
print '</center>';
	}else{
        print '<br><br><br><br>';
        print '<center><img src="pictures/construction.gif"><br><strong>Under Construction</strong></center>';
        print '<br><br><br><br>';
	}
?>


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