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

	$reg_id=$_POST["regid"];
	$sequence = $_POST["sequence"];
	
	$khoueiry = "SELECT * FROM \"PROMOTER\" WHERE \"PROMOTER_ID\"=".$reg_id.";";	
	$stmt = pg_query($c,$khoueiry);
	if($row=pg_fetch_array($stmt)){	    	    
	    $name=$row["NAME"];
	    $comments=$row["COMMENTS"];
	    $begin = $row["BEGIN"];
	    $end = $row["END"];
	}
	
	$khoueiry="select \"SEQUENCE\", \"LENGTH\", \"COMMENTS\", \"MOL_ID\" from \"SEQUENCE\" where \"MOL_ID\"=$reg_id";
	$stmt = pg_query($c,$khoueiry);
		
	if($row=pg_fetch_array($stmt)){
		$lob = $row["SEQUENCE"];

		pg_query ($c, "begin");		
		$handle_lo = pg_lo_open($c,$lob,"r");
		$seq=pg_lo_read($handle_lo);
		pg_query($c,"end");
			
		$seq=ereg_replace("<br>","",$seq);
		$seq=ereg_replace("\n","",$seq);	
	}

	$motif_list = array();
	$motif_len = strlen($sequence);
	$mtf = 0;
	print '<br/><strong>&nbsp;&nbsp; Please provide absolute coordinates';
	print '<br/>&nbsp;&nbsp; Relative coordinates will be calculated after submission</strong><br/>';
	print '<form name="form1" method="POST" action="reg-region-add-multi-motif-results.php">';
	print '<input type="hidden" name="regid" value="'.$reg_id.'"/>';

	//MAKE WITH A TABLE FOR IT TO BE ALL NICE AND DANDY!!
	print '&nbsp;&nbsp;Regulatory motif name:  <select name="motif" size="1">';
	$query="SELECT \"NAME\", \"MOTIF_ID\" FROM \"MOTIF_REGUL\" WHERE \"TYPE\"='manual' ORDER BY \"NAME\"";
	$stmt = pg_query($c,$query);
	while($row=pg_fetch_array($stmt)) {
		print "<option value=\"".$row["MOTIF_ID"]."\">".$row["NAME"]."</option>";							  
	}
	print '</select>&nbsp;&nbsp;<br>';
			
	for($i = 0;$i<strlen($seq)-$motif_len;$i++){	
		if (strcasecmp($sequence, substr($seq,$i,$motif_len)) == 0) {

			//if(($begin<0) && ($begin+$i>=0)){
			//	$begin = $begin++;
			//}
			
			#$motif_list[$mtf] = array("BEGIN"=>($begin+$i),"STOP"=>($begin+$i+$motif_len-1));
			$motif_list[$mtf] = array("BEGIN"=>($i+1),"STOP"=>($i+$motif_len));
			
			#print '&nbsp;&nbsp; Begin: &nbsp;&nbsp; <input type="text" size="10" name="begin'.$mtf.'" value="'.($begin+$i).'"/>';
			print '&nbsp;&nbsp; Begin: &nbsp;&nbsp; <input type="text" size="10" name="begin'.$mtf.'" value="'.($i+1).'"/>';
			#print ' &nbsp;&nbsp; Stop: &nbsp;&nbsp; <input type="text" size="10" name="stop'.$mtf.'" value="'.($begin+$i+$motif_len-1).'"/>';	
			print ' &nbsp;&nbsp; Stop: &nbsp;&nbsp; <input type="text" size="10" name="stop'.$mtf.'" value="'.($i+$motif_len).'"/>';	
			print ' &nbsp;&nbsp; Comments: <input type="text" size="40" name="comments'.$mtf.'"/> ';			
			print '<input type="checkbox" name="being'.$mtf.'" value="1"><br>';
			$mtf++;			
		}		
	}	
	print '<input type="hidden" name="number" value="'.$mtf.'"/>';
	print '<br><center><img src="pictures/button-cur-delete.gif" title="Cancel" onClick="javascript:self.close();"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	print '<input name="submit" type="image" src="pictures/button-cur-validate.gif" onClick="submit" title="Ok"></center>';
	print '</form>';
	
	
	$seq = chunk_split($seq, 70, "\n");
	$motif_seq = '';
	$pos = 0;
	$mtf = 0;
    while($mtf<count($motif_list)){
        $motif = $motif_list[$mtf];
		
		$motif_pos_a = $motif["BEGIN"]-1;
		$motif_pos_b = $motif["STOP"]-1;
	  	//Position 0 does not exist
		//$motif_pos_a = $motif["BEGIN"] - $begin;
		//if(($begin<0) && ($motif["BEGIN"]>0)){ $motif_pos_a--; }		  	
		//$motif_pos_b = $motif["STOP"] - $begin;
		//if(($begin<0) && ($motif["STOP"]>0)){ $motif_pos_b--; }
		  	
		$motif_pos_a = $motif_pos_a + floor($motif_pos_a / 70);
		$motif_pos_b = $motif_pos_b + floor($motif_pos_b / 70);			
			
		$motif_seq = $motif_seq.substr($seq,$pos,($motif_pos_a)-$pos)."<font color=\"#FF0000\"><strong>";
		
		$motif_length = $motif_pos_b - $motif_pos_a + 1;
		$pos = $motif_pos_a;									
		$motif_seq = $motif_seq . substr($seq,$pos,$motif_length) . "</strong></font>";
			
		$pos = $pos + $motif_length;
			
		//deal with overlapping motifs!
		while(isset($motif_list[$mtf+1]) && ($motif_list[$mtf+1]["STOP"]<=$motif_list[$mtf]["STOP"])){
			  $mtf++;				  
		} 
		if(isset($motif_list[$mtf+1]) && ($motif_list[$mtf+1]["BEGIN"]<$motif_list[$mtf]["STOP"])){
			$motif_list[$mtf+1]["BEGIN"]=$motif_list[$mtf]["STOP"]+1;	  
		}
						
		$mtf++;			 	 
	}			
	
	$motif_seq = $motif_seq . substr($seq,$pos);
	
	print "<pre><font face=\"Courier New, Courier, mono\" size=\"3\">".strtoupper($motif_seq)."</font></pre>";
	
?>

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