<?php
//--------------------function of get_percentage_array---------------
//--------------------input two arrays -------------------------------
function get_percentage_array($percentage, $chromosome1) {
    foreach(array_keys($percentage) as $key) {
        if($chromosome1[$key] == 0){
            $percentage[$key] = 0;
        } else {
            $percentage[$key] = round($percentage[$key] / $chromosome1[$key], 4);
        }
    }
    return $percentage;
}

//-------------------function of plot need array---------------
function plot_array($array) {
    $fmt_xaxis = join("|", array_keys($array));
    $fmt_yaxis = join(",", array_values($array));
    //cht (chart type), chd (data), and chs (chart size)
    //print max(array_values($chromosome));
    print "<br/>"."<img src='https://chart.googleapis.com/chart?".
    "cht=bvs".
    "&chxt=x,y".
    "&chm=N,000000,0,-1,11".
    "&chxl=0:|$fmt_xaxis".
    "&chd=t:$fmt_yaxis".
    "&chxr=1,0," . max(array_values($array)).
    "&chds=0," . max(array_values($array)).
    "&chbh=25,15,4".
    "&chs=1000x200'".
    "/>";
}

//-------------------function of plot need filename---------------
function plot($filename) {
    $chromosome = chromosome_count($filename);
    $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));
    print "<br/>"."<img src='https://chart.googleapis.com/chart?".
    "cht=bvs".
    "&chxt=x,y".
    "&chm=N,000000,0,-1,11".
    "&chxl=0:|$fmt_xaxis".
    "&chd=t:$fmt_yaxis".
    "&chxr=1,0," . max(array_values($chromosome)).
    "&chds=0," . max(array_values($chromosome)).
    "&chbh=25,15,4".
    "&chs=1000x200'".
    "/>";
}
//--------------------function of plot_percentage---------------
function plot_percentage($filename,$filename1) {
    $percentage = chromosome_count($filename);
    $chromosome1 = chromosome_count($filename1);
    foreach(array_keys($percentage) as $key) {
        if($chromosome1[$key] == 0) {
            $percentage[$key] = 0;
        } else {
            $percentage[$key] = round(100 * $percentage[$key] / $chromosome1[$key], 2);
        }
    }
    $fmt_xaxis = join("|",array_keys($percentage));
    $fmt_yaxis = join(",",array_values($percentage));
    //cht (chart type), chd (data), and chs (chart size)
    //print max(array_values($chromosome));
    //print "<br/>Overlap Percentage on each Chr:";
    print "<br/>"."<img src = 'https://chart.googleapis.com/chart?".
    "cht=bvs".
    "&chxt=x,y".
    "&chm=N*p0*,000000,0,-1,11".
    "&chxl=0:|$fmt_xaxis".
    "&chd=t:$fmt_yaxis".
    "&chxr=1,0," . max(array_values($percentage)).
    "&chds=0," . max(array_values($percentage)).
    "&chbh=25,15,4".
    "&chs=1000x200'".
    "/>";
}
//--------------------function of chromosome_count-------------------
function chromosome_count($filename)  {
    $chromosome = array(
    "chr1"=>0,"chr2"=>0,"chr3"=>0,
    "chr4"=>0,"chr5"=>0,"chr6"=>0,
    "chr7"=>0,"chr8"=>0,"chr9"=>0,
    "chr10"=>0,"chr11"=>0,"chr12"=>0,
    "chr13"=>0,"chr14"=>0,"chr15"=>0,
    "chr16"=>0,"chr17"=>0,"chr18"=>0,
    "chr19"=>0,"chr20"=>0,"chr21"=>0,
    "chr22"=>0,"chrX"=>0,"chrY"=>0
    );
    $file = fopen($filename, "r") or exit("Unable to open file!");
    while(!feof($file)) {
        $line = fgets($file) . "<br />";
        $chr  = explode("	",$line);
        $chr  = $chr[0];
        if(in_array($chr, array_keys($chromosome))) {
            $chromosome[$chr]++;
        }
    }
    fclose($file);
    return $chromosome;
}
//--------------------function of repeats length-------------------
function repeats_length($filename) {
    $chromosome = chromosome_count($filename);
    $lengths = array(
    "chr1"=>0,"chr2"=>0,"chr3"=>0,
    "chr4"=>0,"chr5"=>0,"chr6"=>0,
    "chr7"=>0,"chr8"=>0,"chr9"=>0,
    "chr10"=>0,"chr11"=>0,"chr12"=>0,
    "chr13"=>0,"chr14"=>0,"chr15"=>0,
    "chr16"=>0,"chr17"=>0,"chr18"=>0,
    "chr19"=>0,"chr20"=>0,"chr21"=>0,
    "chr22"=>0,"chrX"=>0,"chrY"=>0
    );
    $file = fopen($filename,"r") or exit("Unable to open file!");
    while(!feof($file)) {
        $line = fgets($file). "<br />";
        //$chr = explode("	",$line);
        $chr = explode("\t",$line);
        $chr1 = $chr[0];
        $length = (int)$chr[2] - (int)$chr[1] + 1;
        $lengths[$chr1] = $lengths[$chr1] + $length;
    }
    foreach(array_keys($lengths) as $key) {
        if($chromosome[$key] == 0) {
            $lengths[$key] = 0;
        } else {
            $lengths[$key] = $lengths[$key]/$chromosome[$key];
        }
    }
    fclose($file);
    return $lengths;
}
//-----------------------For the overlapped counts and percentage ---//
function print_counts_percentage($chromosome, $chromosome1) {  // $chromosome is after bedtool, $chromosome1 is before bedtool
    $total  = 0;  // $total for overlaps
    $total1 = 0; // $total1 for whole counts in .narrowPeak
    print "<br/><strong>chromosomes: numbers of overlaps(Site counts,  Percentage)</strong>";
    foreach ($chromosome as $key => $value) {
        if($chromosome1[$key] == 0) {
            $per = 0;
        } else {
            $per = round(100 * $value / $chromosome1[$key], 2);
        }
        print "<br/> $key : $value ($chromosome1[$key], $per%)";
        $total += $value;
        $total1 += $chromosome1[$key];
    }
    print "<br/>Total overlaps: $total($total1,  ".round(100*$total/$total1,2)."%)";
}

?>
