<?php

  //////////////////////////////////  Connecting with DB
  require '../conf/config.php';
  if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
    exit;
  }

  //////////////////////////////////  Paramenters
  (isset($_GET['prm_Zoom']) && !empty($_GET['prm_Zoom'])) ? $prm_Zoom=$_GET['prm_Zoom']:returnError("Error");
  (isset($_GET['prm_Chr']) && !empty($_GET['prm_Chr'])) ? $prm_Chr=$_GET['prm_Chr']:returnError("Error");
  (isset($_GET['prm_Mode']) && !empty($_GET['prm_Mode'])) ? $prm_Mode=$_GET['prm_Mode']:returnError("Error");
  (isset($_GET['prm_Pos']) && !empty($_GET['prm_Pos'])) ? $prm_Pos=$_GET['prm_Pos']:returnError("Error");
  (isset($_GET['prm_QUAL']) && !empty($_GET['prm_QUAL'])) ? $prm_QUAL=$_GET['prm_QUAL']:returnError("Error");
  (isset($_GET['prm_DP']) && !empty($_GET['prm_DP'])) ? $prm_DP=$_GET['prm_DP']:returnError("Error");
  (isset($_GET['prm_DP4']) && !empty($_GET['prm_DP4'])) ? $prm_DP4=$_GET['prm_DP4']:returnError("Error");
  (isset($_GET['prm_Acc']) && !empty($_GET['prm_Acc'])) ? $prm_Acc=$_GET['prm_Acc'] : returnError("Error");

  list($zoom_v,$zoom_r,$zoom_chr)=_cvtZoom($prm_Zoom, $prm_Chr);
  
  $prm_Acc = $conn->real_escape_string($prm_Acc);
  
  $query="";
  if ( $prm_Mode == "dp+s" || $prm_Mode == "s" ) {
	$query = "SELECT pos,variant,REF,ALT,EFF,GT,QUAL FROM ".$prm_Acc."_variant WHERE chr= ? and pos>? and pos<=? and QUAL>=? and DP>=? and (DP3+DP4)>=? and variant='s' ";
  } else if ( $prm_Mode == "dp+i" || $prm_Mode == "i" ) {
	$query = "SELECT pos,variant,REF,ALT,EFF,GT,QUAL FROM ".$prm_Acc."_variant WHERE chr= ? and pos>? and pos<=? and QUAL>=? and DP>=? and (DP3+DP4)>=? and variant != 's' ";
  } else if ( $prm_Mode == "dp" ) {
	$query = "SELECT pos,variant,REF,ALT,EFF,GT,QUAL FROM ".$prm_Acc."_variant WHERE chr= ? and pos>? and pos<=? and QUAL>=? and DP>=? and (DP3+DP4)>=? and variant = 'dp' ";
  } else{
	$query = "SELECT pos,variant,REF,ALT,EFF,GT,QUAL FROM ".$prm_Acc."_variant WHERE chr= ? and pos > ? and pos <= ? and QUAL >= ? and DP>= ? and (DP3+DP4) >= ? ";
  }
   
  $query_eff="SELECT eff_grp FROM sys_snpEff WHERE eff_name like ? ";
   
  ////////////////////////////////////////////////// Range
  $e=$zoom_v*$zoom_r*$prm_Pos;
  $s=$e-$zoom_v*$zoom_r;

  ////////////////////////////////////////////////// SQL
  $datas = array();
  
  $stmt_eff = $conn->stmt_init();
  $stmt_eff->prepare($query_eff);
  
  $stmt = $conn->stmt_init();
  $stmt->prepare($query);
  $stmt->bind_param("siiiii", $prm_Chr, $s, $e, $prm_QUAL, $prm_DP, $prm_DP4); 
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($row0,$row1,$row2,$row3,$row4,$row5,$row6);
  
  while ($stmt->fetch()) {
	if ( $row1 == "s" ) $varType="SNP";
    if ( $row1 == "i" ) $varType="INS";
    if ( $row1 == "d" ) $varType="DEL";
	
	$each_effect=array();
	$eff_list=explode(",", $row4);
	$ret_eff=array();
	$t=count($eff_list);
	for($i=0; $i<$t; $i++){
		$effect=explode("(", $eff_list[$i]);
		$effquery=$effect[0];
		$stmt_eff->bind_param("s", $effquery); 
		$stmt_eff->execute();
		$stmt_eff->store_result();
		$stmt_eff->bind_result($row7);
		while ($stmt_eff->fetch()) {
			$ret_eff[] = $row7."|".$eff_list[$i];
		}
		$stmt_eff->free_result();
	}
	//$reseff=implode(",", $ret_eff);
    $datas[]="{\"pos\":\"".$row0."\",\"variant\":\"".$varType."\",\"ref\":\"".$row2."\",".
              "\"alt\":\"".$row3."\",\"eff\":\"".implode(",", $ret_eff)."\",\"gt\":\"".$row5."\",\"qual\":\"".$row6."\"}";
  }
  $stmt->free_result();
  $stmt->close();
  $stmt_eff->close();
  
  mysqli_close($conn);
  
  ////////////////////////////////////////////////// Return
  $rtnString="[";
  for ($i=0; $i<count($datas); $i++) {
    $rtnString=$rtnString.$datas[$i];
    if ( $i<count($datas)-1 ) $rtnString=$rtnString.",";
  }
  $rtnString=$rtnString."]";
  echo $rtnString;

  ////////////////////////////////////////////////// Exit
  exit;


  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);
  }

?>
