<?php

 require '../conf/config.php';

 //////////////////////////////////  Connecting with DB
 $conn = mysql_connect($host, $user, $pswd);
 $db = mysql_select_db($db);

 (isset($_POST['prm_Ref']) && !empty($_POST['prm_Ref'])) ? $prm_Ref=$_POST['prm_Ref']:returnError("Error");
 (isset($_POST['prm_Zoom']) && !empty($_POST['prm_Zoom'])) ? $prm_Zoom=$_POST['prm_Zoom']:returnError("Error");
 (isset($_POST['prm_Chr']) && !empty($_POST['prm_Chr'])) ? $prm_Chr=$_POST['prm_Chr']:returnError("Error");
 (isset($_POST['prm_Pos']) && !empty($_POST['prm_Pos'])) ? $prm_Pos=$_POST['prm_Pos']:returnError("Error");
 (isset($_POST['prm_QUAL']) && !empty($_POST['prm_QUAL'])) ? $prm_QUAL=$_POST['prm_QUAL']:returnError("Error");
 (isset($_POST['prm_DP']) && !empty($_POST['prm_DP'])) ? $prm_DP=$_POST['prm_DP']:returnError("Error");
 (isset($_POST['prm_DP4']) && !empty($_POST['prm_DP4'])) ? $prm_DP4=$_POST['prm_DP4']:returnError("Error");
 (isset($_POST['prm_Block']) && !empty($_POST['prm_Block'])) ? $prm_Block=$_POST['prm_Block'] : returnError("Error");
 (isset($_POST['prm_Lists']) && !empty($_POST['prm_Lists'])) ? $prm_Lists=$_POST['prm_Lists'] : returnError("Error");

	if (!is_numeric($prm_Pos)) {	
		$prm_Pos=mysql_real_escape_string($prm_Pos);
	}
	if (!is_numeric($prm_DP)) {
		$prm_DP=mysql_real_escape_string($prm_DP);
	}
	if (!is_numeric($prm_QUAL)) {
		$prm_QUAL=mysql_real_escape_string($prm_QUAL);
	}
 
 $db_range_end=$prm_Pos+$prm_Block;
 list($zoom_v,$zoom_r,$zoom_chr)=_cvtZoom($prm_Zoom, $prm_Chr);

 $rtnString="";
 for($j=0;$j<count($prm_Lists);$j++){

    $accid=$prm_Lists[$j];
    $rtnString=$rtnString.$accid."|[";

    for($i=$prm_Pos;$i<$db_range_end;$i++){

      $rtnString=$rtnString."{";

      ////////////////////////////////////////////////// SELECT TABLE DEPTH
      $e=$zoom_v*$i;
      $s=$e-$zoom_v;
      $query1 = "SELECT sum(".$zoom_chr.")/".$zoom_v." as sum FROM ".$accid."_accession
                  WHERE pos>".$s." and pos <=".$e;
      $result = mysql_query($query1);
      $row = mysql_fetch_row($result, MYSQL_NUM);
	  
	  //if($row[0] != ""){
		//$avgdp = ($row[0]*10);
		//$avgdp = (int)$avgdp;
		//$avgdp = ($avgdp/10);
	  //}
	  
      if ( $row[0] != "" ) {
        $rtnString=$rtnString."\"dp\":\"".(int)$row[0]."\"";
		//$rtnString=$rtnString."\"dp\":\"".$avgdp."\"";
      } else {
        $rtnString=$rtnString."\"dp\":\"\"";
      }

      ////////////////////////////////////////////////// SELECT TABLE VARIANT
      $e=$zoom_v*$zoom_r*$i;
      $s=$e-$zoom_v*$zoom_r;

    if ( $prm_Ref == "Reference" ) {

      // Over 1b
      if($zoom_r*$zoom_v > 1){
        $query2 = "SELECT variant, count(variant), max(eff_grp) FROM ".$accid."_variant WHERE chr='"
                .$prm_Chr."' and pos>".$s." and pos<=".$e." and QUAL>=".$prm_QUAL." and DP>=".$prm_DP.
                " and (DP3+DP4)>=".$prm_DP4." GROUP BY variant";
      // 1b mode: + ALT, REF
      } else {
        $query2 = "SELECT variant, count(variant), max(eff_grp), REF, ALT, GT FROM ".$accid."_variant WHERE chr='"
                .$prm_Chr."' and pos>".$s." and pos<=".$e." and QUAL>=".$prm_QUAL." and DP>=".$prm_DP.
                " and (DP3+DP4)>=".$prm_DP4." GROUP BY variant";
      }
      $result = mysql_query($query2);
      while ($row = mysql_fetch_row($result, MYSQL_NUM)) {
        if($zoom_r*$zoom_v > 1){
          $rtnString=$rtnString.",\"".$row[0]."\":\"".$row[1]."-".$row[2]."\"";
        } else {
          $rtnString=$rtnString.",\"".$row[0]."\":\"".$row[1]."-".$row[2]."-".$row[3]."-".$row[4]."-".$row[5]."\"";
        }
      }

    } else {
      $query = "SELECT var.variant, count(pos) FROM ".$prm_Ref."_accession
                JOIN (SELECT pos, alt, gt, variant FROM
                     (SELECT pos, alt, gt, variant FROM ".$accid."_variant
                      WHERE QUAL>=".$prm_QUAL." and DP>=".$prm_DP." and (DP3+DP4)>=".$prm_DP4." and
                        pos >".$s." and pos<=".$e." and chr='".$prm_Chr."'
                  UNION ALL
                      SELECT pos, alt, gt, variant from ".$prm_Ref."_variant
                      WHERE QUAL>=".$prm_QUAL." and DP>=".$prm_DP." and (DP3+DP4)>=".$prm_DP4." and
                        pos >".$s." and pos<=".$e." and chr='".$prm_Chr."') AS a
                  group by pos, alt, gt having count(*)<2) AS var
                using (pos) where ".$prm_Chr."_b > 1 group by (variant)";
      $result = mysql_query($query);
      while ($row = mysql_fetch_row($result, MYSQL_NUM)) {
        if($zoom_r*$zoom_v > 1){
          $rtnString=$rtnString.",\"".$row[0]."\":\"".$row[1]."-\"";
        } else {
          $rtnString=$rtnString.",\"".$row[0]."\":\"".$row[1]."---\"";
        }
      }
    }

      $rtnString=$rtnString."}";
      if ($i<($db_range_end-1)) {
        $rtnString=$rtnString.",";
      }

    }

    $rtnString=$rtnString."]";
    if($j<count($prm_Lists)-1){
      $rtnString=$rtnString.";";
    }

 }

 echo $rtnString;

 function returnError($str){
   echo $str;
 }

 function _cvtZoom($str1, $str2) {
   preg_match("/^[0-9]+/", $str1, $r1);
   preg_match('/[b,k]$/', $str1, $r2);
   $v=$r1[0];
   if($r2[0] == "b"){
     $r=1;
     $c=$str2."_b";
   } else {
     $r=1000;
     $c=$str2."_k";
   }
   return array((int)$v,(int)$r, $c);
 }

?>
