<?php
  function _pregMatch($cmd) {
          if(preg_match("/\;|\||\&|\`|\(|\)|\<|\>|\*|\?|\{|\}|\[|\]|\!/",$cmd)) {
                  $errmsg = "Input Parameter Invalid!";
                  echo $errmsg;
                  exit;
          }
  }
  function _checkZoom($cmd) {
          if(preg_match("/[0-9].*[kb]/",$cmd))
{
                  $errmsg = "Error: Input Parameter Invalid!";
                  echo $errmsg;
                  exit;
          }
  }
/*
  function _stopError($str){
    echo $str;
    exit;
  }
*/
  function _validate_num($num){
    if (preg_match("/^\d+$/i", $num)) {
       return true;
    }else{
       return false;
    }
  }
  function _isTransposon($str) {
    if(preg_match("/^</", $str)) {
      return True;
    } else {
      return False;
    }
  }
  function _setSnpEffImpactFilterMap(&$map, $array) {
    // -1:NONE
    // 0: MODIFIER
    // 1: LOW
    // 2: MODERATE
    // 3: HIGH
//    $map = array("0" => True, "1" => True, "2" => True, "3" => True, "-1" => True);

    $keys = array_keys($map);
    foreach($keys as $key) { $map[$key] = False; }
    foreach($array as $impct) { $map[(string)$impct] = True; }

    $allTrue = True;
    foreach($keys as $key) { $allTrue = $allTrue && $map[$key]; }

    $doFilter = False;
    if(!$allTrue) $doFilter = True;	// If all are true, do not filter.
    return $doFilter;
  }
  function _parseSnpEffs($str) {
    $eff_list = explode(",", $str);
    $t=count($eff_list);
    $resarray = array();
    for($i=0; $i<$t; $i++){
	$resarray[] = _parseSnpEff($eff_list[$i]);
    }
    return $resarray;
  }
  function _parseSnpEff($str) {
    $EFFMAP = array('MODIFIER' => 0, 'LOW' => 1, 'MODERATE' => 2, 'HIGH' => 3);
    if(!empty($str)) {
                        $effname = ""; $alt = ""; $geneid = ""; $genename = ""; $impact = ""; $impactc = null;
                        if(preg_match("/^([^\|\(]+)\((.+)$/", $str, $matches)) {
                                // EFF
                                $effname = strtolower($matches[1]);
                                $EffInfo = explode("|", $matches[2]);
                                if(count($EffInfo) > 10) {
                                        // new EFF
                                        $geneid = $EffInfo[5];
                                        $genename = $EffInfo[8];
                                } else {
                                        // old EFF
                                        $geneid = $EffInfo[4];
                                        $genename = $EffInfo[7];
                                }
                                $alt = str_replace(")", "", array_pop($EffInfo));
                                $impact = $EffInfo[0];
                        } else {
                                // ANN
                                $effect = explode("|", $str);
                                $effname = strtolower($effect[1]);
                                $geneid = $effect[3];
                                $genename = $effect[6];
                                $alt = $effect[0];
                                $impact = $effect[2];
                        }
                        $impactc = $EFFMAP[$impact];
    }
    return array("id" => $geneid, "name" => $genename, "alt" => $alt, "eff" => $effname, "impact" => $impact, "impactc" => $impactc);
  }
?>
