<?php

$file1  = $_POST["file"];
$chr = $_POST["chr"];
$start=(int)$_POST["startpos"];
$end=(int)$_POST["endpos"];
$track  = $_POST["trackname"];
$description = $_POST["description"];
$color=$_POST["itemrgb"];
$download=$_POST["download"];
$rgb1=$_POST["rgb1"];
$rgb2=$_POST["rgb2"];
$rgb3=$_POST["rgb3"];
$rgbarray=array($rgb1,$rgb2,$rgb3);
$rgbquery=join(",",$rgbarray);
$id = uniqid('extract'); 
$action=$_POST["action"];
$sort=$_POST["sort"];
$direction=$_POST["direction"];

if ($download=="Excel file")
$extension=".xls";

if ($download=="Bed file")
$extension=".BED";

if ($direction=="Desending")
$sortmethod="DESC";

if ($direction=="Ascending")
$sortmethod="ASC";

if ($start>$end)
{die("chromosome start position is greater than end position!");}

$header = "track name = $track description = '$description' itemRgb='$color'\n";

//print "The file you have chosen is: $file1<br/>";
//print "Chromosome: $chr<br/>";
//print "Position $start to $end <br/>";

$order="";

$con = mysql_connect("localhost","root2","SMUCKrasp23");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }  
  
mysql_select_db("dasr", $con)
or die ("could not select database!");

if ($chr=="All chromosomes" and !$start and !$end)
$query = "";

if (!$start and !$end and $chr != "All chromosomes")
$query = "WHERE chrom = '$chr'";

if ($start or $end)
$query= "WHERE chrom = '$chr' AND chromstart> $start  AND chromend< $end;";

if ($chr and $start and $end and $rgb1 and $rgb2 and $rgb3)
$query= "WHERE chrom = '$chr' AND chromstart> $start  AND chromend< $end and itemrgb='$rgbquery'";

if (!$chr and !$start and !$end and $rgb1 and $rgb2 and $rgb3)
$query= "WHERE itemrgb='$rgbquery'"; 

if ($chr and !$start and !$end and $rgb1 and $rgb2 and $rgb3)
$query= "WHERE chrom = '$chr' AND itemrgb='$rgbquery'";

if ($sort=="Position")
$order="ORDER BY chrom $sortmethod,chromstart $sortmethod"; 

if ($sort=="RGB")
$order="ORDER BY itemrgb $sortmethod";

if (!$sort)
$order="";

$result = mysql_query("SELECT * FROM $file1 $query $order ;");

if ($action=="Download bed file"){
$new_extract = "temp/$id"."$extension";
system("chmod a+x 777 $new_extract");
$fh=fopen($new_extract,"w") or die ("cannot open file");
fwrite($fh, "$header");
while($row = mysql_fetch_array($result)) {
	fwrite($fh,"$row[chrom]\t$row[chromstart]\t$row[chromend]\t$row[name]\t$row[score]\t$row[strand]\t$row[thickstart]\t$row[thickend]\t$row[itemrgb]\t$row[blockcount]\t$row[blocksizes]\t$row[blockstarts]\n");
	}
fclose($fh);
echo "DASR_extract/"."$new_extract";
}

if ($action=="View bed file"){
echo "$header";
echo "<table border='1'>";
echo "<table border='1'>
<tr>
<th>summarybed_id</th>
<th>bedfile_id</th>
<th>chrom</th>
<th>chromstart</th>
<th>name</th>
<th>score</th>
<th>strand</th>
<th>thickstart</th>
<th>thickend</th>
<th>itemrgb</th>
<th>blockcount</th>
<th>blocksizes</th>
<th>blockstarts</th>
</tr>";

while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['summarybed_id'] . "</td>";
echo "<td>" . $row['bedfile_id'] . "</td>";
echo "<td>" . $row['chrom'] . "</td>";
echo "<td>" . $row['chromstart'] . "</td>";
echo "<td>" . $row['chromend'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['strand'] . "</td>";
echo "<td>" . $row['thickstart'] . "</td>";
echo "<td>" . $row['thickend'] . "</td>";
echo "<td>" . $row['itemrgb'] . "</td>";
echo "<td>" . $row['blockcount'] . "</td>";
echo "<td>" . $row['blocksizes'] . "</td>";
echo "<td>" . $row['blockstarts'] . "</td>";
echo "</tr>";
}
echo "</table>";
}


//$csv_output = "column 1,column2"; 
//$csv_output .= "\n"; 

//$output="";

//while($row = mysql_fetch_array($result)) { 
   //$output .= "$row[summarybed_id]\t$row[bedfile_id]\t$row[chrom]\t$row[chromstart]\t$row[chromend]\t$row[name]\t$row[score]\t$row[strand]\t$row[thickstart]\t$row[thickend]\t$row[itemrgb]\t$row[blockcount]\t$row[blocksizes]\t$row[blockstarts]\n";
  // } 

 //header("Content-type: application/BED");
 //header("Content-disposition: BED" . ".BED");
 
 //print $output;



mysql_close($con);
exit;  

?>  

