<?php
  
  require '../conf/config.php';
  set_time_limit(300);//5min
  ////////////////////////////////// Connecting with DB
  $conn = mysql_connect($host, $user, $pswd);
  $db = mysql_select_db($db);

  ////////////////////////////////// Parameters
  (isset($_GET['prm_fa_chr']) && !empty($_GET['prm_fa_chr'])) ? $prm_chr=$_GET['prm_fa_chr']:returnError("Error");
  (isset($_GET['prm_fa_s']) && !empty($_GET['prm_fa_s'])) ? $prm_s=$_GET['prm_fa_s']:returnError("Error");
  (isset($_GET['prm_fa_e']) && !empty($_GET['prm_fa_e'])) ? $prm_e=$_GET['prm_fa_e']:returnError("Error");
  (isset($_GET['prm_fa_qual']) && !empty($_GET['prm_fa_qual'])) ? $prm_qual=$_GET['prm_fa_qual']:returnError("Error");
  (isset($_GET['prm_fa_dp']) && !empty($_GET['prm_fa_dp'])) ? $prm_dp=$_GET['prm_fa_dp']:returnError("Error");
  (isset($_GET['prm_fa_list']) && !empty($_GET['prm_fa_list'])) ? $prm_list=$_GET['prm_fa_list'] : returnError("Error");
  (isset($_GET['prm_fa_name']) && !empty($_GET['prm_fa_name'])) ? $prm_name=$_GET['prm_fa_name'] : returnError("Error");
  (isset($_GET['prm_fa_nameS']) && !empty($_GET['prm_fa_nameS'])) ? $prm_nameS=$_GET['prm_fa_nameS'] : returnError("Error");
  (isset($_GET['prm_fa_ref']) && !empty($_GET['prm_fa_ref'])) ? $prm_ref=$_GET['prm_fa_ref'] : returnError("Error");
  (isset($_GET['prm_fa_width_php']) && !empty($_GET['prm_fa_width_php'])) ? $prm_width=$_GET['prm_fa_width_php'] : returnError("Error");
	
	if (!is_numeric($prm_s)) {
		$prm_s=mysql_real_escape_string($prm_s);
	}
	if (!is_numeric($prm_e)) {
		$prm_e=mysql_real_escape_string($prm_e);
	}
	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);
	}
	if (!is_numeric($prm_width)) {
		$prm_width=mysql_real_escape_string($prm_width);
	}
	if ($prm_width == -1){
		$prm_width=0;
	}

    if($prm_e-$prm_s>9999){  // Maximum size is 10K
		$prm_e=$prm_s+9999;
	} else if($prm_e-$prm_s < 0){
		$prm_e=$prm_s;
	}
	
	
	$file=$prm_chr."_".$prm_s."b-".$prm_e."b_".date("Y-m-d").".fasta";
	header("Content-Disposition: attachment; filename=\"".$file."\"");
	header("Content-type: application/octet-stream");
   
	$seqname = array();
	$list=explode(",",$prm_list);
    $list_cp=explode(",",$prm_list);
	
	$refseq = _GETREFSEQ($prm_chr, $prm_s, $prm_e);
	$seqname = _HEADERFA($list,$prm_s,$prm_e,$prm_name,$prm_nameS);
   
	$altpos =  _VAR($list,$prm_chr,$prm_s,$prm_e);
   
	echo ">REF_".$prm_ref."\n";
    $pr_refseq = _rpl_REF("REFERENCE",$altpos,$prm_s,$prm_e,$refseq,$list_cp);
	if ($prm_width != 0 ){ 
		$pr_refseq = wordwrap($pr_refseq, $prm_width, "\n", true);
	}
	
    echo $pr_refseq."\n";
	
	$t=count($seqname);
	
	for($i=0; $i<$t; $i++){
		$sn = array_shift($seqname);
		echo ">".$sn."\n";
		
		$altseq = strtoupper($refseq);
		
		$ac = array_shift($list);
		$empty_sites = _EMPDP($ac,$prm_chr,$prm_s,$prm_e);
		$altseq = _rpl_N($empty_sites,$prm_s,$altseq);
		$altseq = _rpl_VAR($ac,$altpos,$prm_s,$prm_e,$altseq,$list_cp,$prm_qual,$prm_dp);
		if ($prm_width != 0 ){ 
			$altseq = wordwrap($altseq, $prm_width, "\n", true);
		}
		echo $altseq."\n";
	}
	
   //-----------------------------------------functions------------------------------------------------------------//
   
    function _rpl_REF($a,$b,$c,$d,$e,$f){
	$rtnseq = $e;
	$list = $a;
	$strbuf=array();
	$x=count($f);
	
	$f_chk=$f;
	$lenmax=array();
	
	for($i=0;$i<$x;$i++){
		$each_acces = array_shift($f_chk);
		
		for($z=$c; $z<=$d; $z++){
			
			if(array_key_exists($each_acces.":".$z, $b)){
				$ins_inf = explode(":",$b[$each_acces.":".$z]);
				if($ins_inf[0] == "i"){
					
					$reflen = strlen($ins_inf[1]);
					$altlen = strlen($ins_inf[2]);
					$len_i = $altlen - $reflen;
					$replace_pos = $z + 1 - $c;

					if(array_key_exists($replace_pos, $lenmax)){
						if($len_i > $lenmax[$replace_pos]){
							$lenmax[$replace_pos]=$len_i;
						}
					}else{
						$lenmax[$replace_pos]=$len_i;
					}
					
				} 
			}
		}
	}
	
	for($i=0;$i<$x;$i++){
		$each_acces = array_shift($f);
		
		for($z=$c; $z<=$d; $z++){
			
			if(array_key_exists($each_acces.":".$z, $b)){
				$ins_inf2 = explode(":",$b[$each_acces.":".$z]);
				if($ins_inf2[0] == "i" and $list != $each_acces ){
					
					$reflen = strlen($ins_inf2[1]);
					$altlen = strlen($ins_inf2[2]);
					$len_i = $altlen - $reflen;
					$replace_pos = $z + 1 - $c;
					$gap="";
					
					if($lenmax[$replace_pos] > $len_i){
						$len_i=$len_i+($lenmax[$replace_pos]-$len_i);
					}
					
					for($i=0; $i<$len_i; $i++){
						$gap=$gap."-";
					}
						$strbuf[$replace_pos]=$gap.":".$len_i.":".$replace_pos;
				} else if($ins_inf2[0] == "i" and $list == $each_acces ){
					//
				}
			}
		}
	}
	$pointer=0;
	ksort($strbuf);
	$ct=count($strbuf);
	for($i=0; $i<$ct; $i++){
		$str = array_shift($strbuf);
		$str_array = explode(":",$str);
		$replace_pos = $str_array[2] + $pointer;
		$rtnseq = substr_replace($rtnseq, $str_array[0], $replace_pos ,0);
		$pointer +=$str_array[1];
	}
	return $rtnseq;
   }
   
   function _rpl_INS($a,$b,$c,$d,$e,$f,$ql,$dp){
	$rtnseq = $e;

	$list = $a;
	$strbuf=array();
	$ins_check=array();
	$x=count($f);
	$f_chk=$f;
	$lenmax=array();
	
	for($i=0;$i<$x;$i++){
		$each_acces = array_shift($f_chk);
		
		for($z=$c; $z<=$d; $z++){
			
			if(array_key_exists($each_acces.":".$z, $b)){
				$ins_inf = explode(":",$b[$each_acces.":".$z]);
				if($ins_inf[0] == "i"){
					
					$reflen = strlen($ins_inf[1]);
					$altlen = strlen($ins_inf[2]);
					$len_i = $altlen - $reflen;
					$replace_pos = $z + 1 - $c;

					if(array_key_exists($replace_pos, $lenmax)){
						if($len_i > $lenmax[$replace_pos]){
							$lenmax[$replace_pos]=$len_i;
						}
					}else{
						$lenmax[$replace_pos]=$len_i;
					}
					
				} 
			}
		}
	}
	
	for($i=0;$i<$x;$i++){
		$each_acces = array_shift($f);
		
		for($z=$c; $z<=$d; $z++){
			
			if(array_key_exists($each_acces.":".$z, $b)){
				$ins_inf = explode(":",$b[$each_acces.":".$z]);
				if($ins_inf[0] == "i" and $list != $each_acces and ! array_key_exists($z, $ins_check)){
					
					$reflen = strlen($ins_inf[1]);
					$altlen = strlen($ins_inf[2]);
					$len_i = $altlen - $reflen;
					$replace_pos = $z + 1 - $c;
					$gap="";
					
					if($lenmax[$replace_pos] > $len_i){
						$len_i=$len_i+($lenmax[$replace_pos]-$len_i);
					}
					for($i=0; $i<$len_i; $i++){
						$gap=$gap."-";
					}
						$strbuf[$replace_pos]=$gap.":".$len_i.":".$replace_pos;
				} else if($ins_inf[0] == "i" and $list == $each_acces ){
					$ins_check[$z]=1;
					$reflen = strlen($ins_inf[1]);
					$altlen = strlen($ins_inf[2]);
					$len_i = $altlen - $reflen;
					$replace_pos = $z + 1 - $c;
					
					if($ql <= $ins_inf[3] and $dp <= $ins_inf[4]){
						$gap="";
						
						if($lenmax[$replace_pos] > $len_i){

							for($i=0; $i<$lenmax[$replace_pos]-$len_i; $i++){
								$gap=$gap."-";
							}
							$len_org=$len_i;
							$len_i=$len_i+($lenmax[$replace_pos]-$len_i);
							
							$strbuf[$replace_pos]=substr($ins_inf[2], 1, $len_org).$gap.":".$len_i.":".$replace_pos;
							
						}else{
							$strbuf[$replace_pos]=substr($ins_inf[2], 1, $len_i).":".$len_i.":".$replace_pos;
						}
						
					}else{
						$gap="";
						
						if($lenmax[$replace_pos] > $len_i){
							$len_i=$len_i+($lenmax[$replace_pos]-$len_i);
						}
						
						for($i=0; $i<$len_i; $i++){
							$gap=$gap."N";
						}
						$strbuf[$replace_pos]=$gap.":".$len_i.":".$replace_pos;
					}
					
				}
			}
		}
	}
	$pointer=0;
	ksort($strbuf);
	$ct=count($strbuf);
	for($i=0; $i<$ct; $i++){
		$str = array_shift($strbuf);
		$str_array = explode(":",$str);
		$replace_pos = $str_array[2] + $pointer;
		$rtnseq = substr_replace($rtnseq, $str_array[0], $replace_pos ,0);
		$pointer +=$str_array[1];

	}
	return $rtnseq;
   }
   
   function _rpl_VAR($a,$b,$c,$d,$e,$f,$ql,$dp){
	$rtnseq = $e;
		$list = $a;
		$pointer=0;
		$strbuf=array();
		for($y=$c; $y<=$d; $y++){
			if(array_key_exists($list.":".$y, $b)){
				$var_inf = explode(":",$b[$list.":".$y]);
				
				if($var_inf[0] == "s"){
					
					if($ql <= $var_inf[3] and $dp <= $var_inf[4]){
						$replace_pos = $y - $c + $pointer;
						$rtnseq = substr_replace($rtnseq, $var_inf[2], $replace_pos, 1 );

					}else{
						$replace_pos = $y - $c + $pointer;
						$rtnseq = substr_replace($rtnseq, "N", $replace_pos, 1 );

					}
					
				} else if ($var_inf[0] == "i"){
					
					$reflen = strlen($var_inf[1]);
					$altlen = strlen($var_inf[2]);
					$len_i = $altlen - $reflen;
					$replace_pos = $y + 1 - $c + $pointer;
					
					if($ql <= $var_inf[3] and $dp <= $var_inf[4]){
						
						$strbuf[$replace_pos]=substr($var_inf[2], 1, $len_i).":".$len_i.":".$replace_pos;
						
					}else{
						$gap="";
						for($i=0; $i<$len_i; $i++){
							$gap=$gap."N";
						}
						$strbuf[$replace_pos]=$gap.":".$len_i.":".$replace_pos;
						
					}
					
				} else if ($var_inf[0] == "d"){
					
					$reflen = strlen($var_inf[1]);
					$altlen = strlen($var_inf[2]);
					$len_d = $reflen - $altlen;
					$replace_pos = $y - $c + $pointer;
					
					if($ql <= $var_inf[3] and $dp <= $var_inf[4]){
						for($i=0; $i<$len_d; $i++){
							$rtnseq = substr_replace($rtnseq, "-", $replace_pos, 1 );
							$replace_pos++;
						}
					}else{
						for($i=0; $i<$len_d; $i++){
							$rtnseq = substr_replace($rtnseq, "N", $replace_pos, 1 );

							$replace_pos++;
						}
					}
				}
				
			}
		}
		
	 $pr_rtnseq = _rpl_INS($list,$b,$c,$d,$rtnseq,$f,$ql,$dp);
	
	return $pr_rtnseq;
   }
   
   
   function _rpl_N($a,$b,$c){
	$rtnseq = $c;
	$x=count($a);
	for($i=0;$i<$x;$i++){
		$Npos=array_shift($a);
		$replace_pos = $Npos - $b;
		$rtnseq = substr_replace($rtnseq, "N", $replace_pos, 1 );
	}
	return $rtnseq;
   }
   
   function _VAR($list,$b,$c,$d){
    $var_array = array();
	$x=count($list);
	for($i=0;$i<$x;$i++){
		$p=array_shift($list);
		$sql = "SELECT pos,variant,ref,alt,qual,dp FROM ".$p."_variant WHERE chr='".$b."' and pos>=".$c." and pos <=".$d;
		$result = mysql_query ( $sql ) or ( "Error " . mysql_error () );
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			$var_array[$p.":".$row[0]]=$row[1].":".strtoupper($row[2]).":".strtoupper($row[3]).":".$row[4].":".$row[5];
		}
	}
    return $var_array;
  }
   
   function _EMPDP($a, $b, $c, $d){
    $null=array();
    $sql="SELECT pos FROM ".$a."_accession WHERE ".$b."_b is null and pos>=".$c." and pos <=".$d;
    $result = mysql_query ( $sql ) or ( "Error " . mysql_error () );
	while ($row = mysql_fetch_row($result, MYSQL_NUM)){
        $null[]=$row[0];
     }
	 
    return $null;
  }

   
   function _GETREFSEQ($a,$b,$c){
		$refseq="";
		$sql = "SELECT ".$a."_f FROM sys_fasta WHERE pos>=".$b." and pos<=".$c;
		$result = mysql_query ( $sql ) or ( "Error " . mysql_error () );
		while  ($row = mysql_fetch_row($result, MYSQL_NUM)){
			$refseq=$refseq.$row[0];
		}
		return $refseq;
   }
   
  function _HEADERFA($a,$b,$c,$d,$e){
	$str=array();
	if ($d == "id" ) {
        $d="accession_id";
    } else if ( $d == "name"  ) {
        $d="accession_name";
    }
	if ($e == "subvariety" ) {
        $e="sub_variety";
    } 
    for($i=0;$i<count($a);$i++){
		$sql="SELECT ".$d.", ".$e." FROM sys_accession WHERE accession_db='".$a[$i]."'";
		$result = mysql_query ( $sql ) or ( "Error " . mysql_error () );
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			$str[$i] = $row[0]."_".$row[1];
		}
    }
    return $str;
  }

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

?>