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

	include("utils.php");

	//CONSTRUCTION_ID
	$id = $_GET["id"];	
	
	$khoueiry = "SELECT * FROM \"MOTIF_REGUL\" WHERE \"MOTIF_ID\"=".$id.";";		
	$stmt = pg_query($c,$khoueiry);
	if($row=pg_fetch_array($stmt)){
	    $name=$row["NAME"];
	    $motif_id = $row["MOTIF_ID"];
	    $begin = $row["BEGIN"];
	    $comments=$row["COMMENTS"];
	    $type=$row["TYPE"];
	    //not editable, for the moment...
	    //$size=$row["SIZE"];
	    //$matrix=$row["MATRIX"];	    
	}
	
	$test="SELECT \"IS_ADMIN\" from \"AUTHOR\" where \"LOGIN\"='".$_SESSION["login"]."'";
	$stest=pg_query($c,$test);
	$rtest=pg_fetch_array($stest);
	if ($rtest["IS_ADMIN"]=='y') {		
	    print '<div align="right"><img src="pictures/button-cur-delete.gif" border="0" title="Delete" onClick="window.open(\'motif-delete.php?id='.$id.'\')"></div>';
	}	
    
    print "<h2><strong>Motif: </strong> </h2>";
  	print "<a href=\"motif-edit-name.php?name=$name&motif=".$motif_id."\" target=\"_blank\"><img src=\"pictures/button-cur-edit.gif\" width=\"25\" border=\"0\" title=\"Edit\"></a> &nbsp;&nbsp; <strong> Name:</strong> ".$name."&nbsp;&nbsp;($type)<br/>";
    print "<a href=\"motif-edit-comments.php?motif=".$motif_id."&comments=".$comments."\" target=\"_blank\"><img src=\"pictures/button-cur-edit.gif\" width=\"25\" border=\"0\" title=\"Edit\"></a> &nbsp;&nbsp; <strong>Comments: </strong> ".$comments." <br/><br/>";  

		///////////////////////////////////////Associated GENES///////////////////////////////////////////////
		print "<br><u><strong>Associated Gene Models:</strong></u>
		&nbsp;<a href=\"motif-gene-add.php?id=$motif_id\" target=\"_blank\"><img src=\"pictures/button-cur-add.jpg\" width=\"20\" border=\"0\" title=\"Associate a gene model to this motif\"></a><br/><br/>";
		$khoueiry="select b.\"NAME\", b.\"GENE_ID\" from \"GENE_MOTIF\" a, \"GENE\" b WHERE a.\"MOTIF_ID\"=$motif_id AND a.\"GENE_ID\"=b.\"GENE_ID\"";
		$stmt = pg_query($c,$khoueiry);
		$count=0;
		while($row=pg_fetch_array($stmt)){
		    ++$count;
		    print "<a href=\"motif-gene-delete.php?id=".$motif_id."&geneid=".$row["GENE_ID"]."\" target=\"_blank\"><img src=\"pictures/button-cur-delete.gif\" width=\"25\" border=\"0\" title=\"Delete this gene\"></a> <strong>Associated gene: &nbsp;&nbsp;</strong> <a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$row["NAME"]."</a><br/>";		    
		}
		if($count==0) print "No associated genes<br>";


/*	
	//New section... include later					
	//include the update matrix... beautify it...
	print "<strong>Size of the Motif: </strong>".$size."<a href=\"motif-change-size.php?&motif=".$motif_id."&size=".$size."\" target=\"_blank\"><img src=\"pictures/button-cur-edit.gif\" width=\"25\" border=\"0\" title=\"Edit\"></a><br/>";
	print "<pre>".$matrix."</pre><br/>";
	
	$M = read_matrix($matrix);
		
	//Just to start...
	//count($M_A) must be equal to row["SIZE"]!... maybe check this?
	$consensus = "";
	for($i=0;$i<count($M["A"]);$i++){
	  $max = $M["A"][$i]; $cur = 'A';
	  //Do a more sophisticated consensus... or/and the Logo...
	  if($M["C"][$i]>$max){ $max = $M["C"][$i]; $cur = 'C'; }
  	  if($M["G"][$i]>$max){ $max = $M["G"][$i]; $cur = 'G'; }
   	  if($M["T"][$i]>$max){ $max = $M["T"][$i]; $cur = 'T'; }	
	  $consensus = $consensus.$cur;		  
	}	
	
	print "<strong>".$consensus."</strong><br/>";
	//define a consensus... use MEME
	//print a logo??? 
*/	
	//////////////////////////////////////ASSOCIATED SEQUENCES////////////////////////////////////////////////////
	print "<br><u><strong><font size=\"+2\">Associated Sequences:</font></strong></u><br/>";
	$khoueiry = "SELECT A.\"SEQUENCE\", A.\"PROMOTER_ID\", B.\"NAME\" FROM \"MOTIF_COORD\" A, \"PROMOTER\" B WHERE \"MOTIF_ID\"=".$id." AND A.\"PROMOTER_ID\"=B.\"PROMOTER_ID\";";		
	$stmt = pg_query($c,$khoueiry);
	while($row=pg_fetch_array($stmt)){
	  	//Enable editing this... MATRIX_SEQ_BEGIN
		print "<strong>Sequence: </strong>".$row["SEQUENCE"]." <strong> Origin: </strong><a href=\"reg-region-edit.php?id=".$row["PROMOTER_ID"]."\">".$row["NAME"]."</a><br>";		  
	}

/*
	//////////////////////////////////////ASSOCIATED GENES////////////////////////////////////////////////////////
	print "<br><u><strong><font size=\"+2\">Associated Genes:</font></strong></u> 
	&nbsp;&nbsp;&nbsp;<a href=\"motif-gene-add.php?id=$id\" target=\"_blank\"><img src=\"pictures/button-add.jpg\" width=\"70\" border=\"0\" title=\"Add a new associated gene\"></a>
	<br/>";
						
	//show all genes associated to this motif...
	$khoueiry = "SELECT B.\"NAME\", A.\"GENE_ID\" FROM \"GENE_MOTIF\" A, \"GENE\" B WHERE A.\"GENE_ID\"=B.\"GENE_ID\" AND B.\"MOTIF_ID\"=".$id.";";		
	$stmt = pg_query($c,$khoueiry);
	while($row=pg_fetch_array($stmt)){
		print "<a href=\"motif-gene-delete.php?id=".$id."&geneid=".$row["GENE_ID"]."\" target=\"_blank\"><img src=\"pictures/button-cur-delete.gif\" width=\"20\" border=\"0\" title=\"Delete this association\"></a><strong>Associated gene: &nbsp;&nbsp;</strong> <a href=\"molecule-gene.php?name=".$row["NAME"]."\">".$row["NAME"]."</a><br/>" ;
	}
*/				
	include ("frames_footer.php"); 

?>