<?php

	header('Content-disposition: attachment; filename=regulatory_regions.fasta'); 
	header('Content-Type: application/force-download');

	include ("connection.php");
	
    $khoueiry = "SELECT * FROM \"PROMOTER\" WHERE \"IS_NATURAL\"='y' AND \"PROMOTER_ID\"=\"BASE_REG_ID\";";	
	$stmt = pg_query($c,$khoueiry);
	
	while($row=pg_fetch_array($stmt)){
		$id = $row["PROMOTER_ID"]  ;
    	$khoueiry2 = "SELECT * FROM \"SEQUENCE\" WHERE \"MOL_ID\"=".$id.";";	
		$stmt2 = pg_query($c,$khoueiry2);
	
		if($row2=pg_fetch_array($stmt2)){
	    	$lob = $row2["SEQUENCE"];		
			pg_query ($c, "begin");		
			$handle_lo = pg_lo_open($c,$lob,"r");
			$seq=pg_lo_read($handle_lo);
			pg_lo_close($handle);
			pg_query($c,"end");	
			
			echo '>'.$row["PROMOTER_ID"].";".$row["NAME"]."\n";
			echo $seq."\n";
		}		  
	}	
	
	
?>