";
#################### Output data from Mysql into text files #########################
$q1 = "select filetype,name from bedfile_real where bedfile_id = $bedfile_id1";
$q2 = "select filetype,name from bedfile_real where bedfile_id = $bedfile_id2";
$row = mysql_fetch_row(mysql_query ($q1));
$table1 = $row[0];$filename1 = $row[1]; // need table1 name
$row = mysql_fetch_row(mysql_query ($q2));
$table2 = $row[0];$filename2 = $row[1]; // need table2 name
print "Compared Files: (1)$filename1 (2)$filename2
";
$table1 = $table1."_real";
$q3 = "select chrom,chromstart,chromend,name,score,strand from $table1 where bedfile_id = $bedfile_id1 into outfile \"/var/www/DASR_2011/DASR_compare/temp/1_$filename1\"";
$table2 = $table2."_real";
$q4 = "select chrom,chromstart,chromend,name,score,strand from $table2 where bedfile_id = $bedfile_id2 into outfile \"/var/www/DASR_2011/DASR_compare/temp/2_$filename2\"";
mysql_query($q3) or die ("Error q3!");
mysql_query($q4) or die ("Error q4!");
#################### Run bedtool #########################
$temp = "temp/result.bed";
$action = "/var/www/DASR_2011/BEDTools-Version-2.11.2/bin/intersectBed -wao -a temp/1_$filename1 -b temp/2_$filename2 > $temp"; // original overlapped file
system($action); // call BEDTools
############### Get cutoff processed file(combined columns)################
$random_number = genRandomString();
print "
Please Keep Your Retrieval ID: $random_number
";
$new_file = "temp/$random_number"."_combinedcolumns".".txt"; //combined columns
overlaps_cut($new_file,$temp,$overlaps);
###############overlap_bedfile(overlap interval)################
$new_file1 = "temp/$random_number"."_overlapbedfile".".txt"; // overlap_bedfile
$fh =fopen($new_file,"r") or exit("Unable to open $new_file!"); // read from an original overlapped file, $temp
$fh1=fopen($new_file1,"w") or exit("Unable to write $new_file1!"); // write into a new overlapped file
while(!feof($fh))
{
$line = fgets($fh);
$chr = explode("\t",$line);
for ( $i=1; $i<=sizeof($chr)-1; $i++)
{
if ($chr[$i] == $chr[0])
{
$i += 1;
$low = max($chr[$i], $chr[1]); // low boundary of the overlap interval
$high = min($chr[$i+1],$chr[2]); // high boundary of the overlap interval
fwrite($fh1,"$chr[0]\t$low\t$high\n");
}
}
}
fclose($fh);
fclose($fh1);
system("chmod a+x 777 $new_file");
system("chmod a+x 777 $new_file1");
#################### Plot Graphics #######################
$chromosome = chromosome_count($new_file);
$fmt_xaxis = join("|",array_keys($chromosome));
$fmt_yaxis = join(",",array_values($chromosome));
//cht (chart type), chd (data), and chs (chart size)
//print max(array_values($chromosome));
$chart1 = "";
//print $chart1;
//print "Figure 1: Overlap-counts distribute on all Chromosome.";
$file1_percentage = get_percentage_array(chromosome_count($new_file),chromosome_count("temp/1_$filename1"));
$file2_percentage = get_percentage_array(chromosome_count($new_file),chromosome_count("temp/2_$filename2"));
$fmt_xaxis = join("|",array_keys($file1_percentage));
$fmt_yaxis_1 = join(",",array_values($file1_percentage));
$fmt_yaxis_2 = join(",",array_values($file2_percentage));
$chart2 = "
";
//print $chart2;
//print "Figure 2: Overlaps-Per-Read(OPR) for $filename1 and $filename2.";
//print "Intuitively, a higher OPR curve indicates that two samples overlaps better.";
#################### Remove the unnecessary files #######################
system("rm temp/result.bed temp/2_$filename2 temp/1_$filename1");
##################### Download #######################
//print "Format1-Combined Colummns:Download HERE";
//print "Format2-OverlapBedfile:Download HERE";
########################### Output Return #######################
$path = $_SERVER['DOCUMENT_ROOT']."/DASR_2011/DASR_compare/";
return
array("overlap_bedfile" => "$path$new_file1",
"overlap_combined_columns" => "$path$new_file",
"overlaps_chart1" => "$chart1",
"overlaps_chart2" => "$chart2");
}
###################### small functions #########################
function genRandomString() {
$length = 10;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = NULL;
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
function overlaps_cut($new_file,$temp,$overlaps)
{
$fh =fopen($temp,"r") or exit("Unable to open $temp!"); // read from an original overlapped file, $temp
$fh1=fopen($new_file,"w") or exit("Unable to write $new_file!"); // write into a new overlapped file
while(!feof($fh))
{
$line = fgets($fh);
$chr = explode("\t",$line);
if ((int)$chr[sizeof($chr)-1] >= $overlaps) // according to some criteria
{
//print "$chr $overlaps";
fwrite($fh1,$line);
}
}
fclose($fh);
fclose($fh1);
}
?>