From 0b978615e5e54a6afd18435af1de5ebd48d637dc Mon Sep 17 00:00:00 2001 From: Yuchen Jiang Date: Wed, 14 Oct 2015 12:42:05 -0400 Subject: [PATCH] fasta file loading fix --- VQI_GenomeBrowser.js | 8 +-- VQI_GenomeBrowserDemo.html | 4 +- dynamic_loading.php | 139 ++++++++++++++++++++++++------------- 3 files changed, 98 insertions(+), 53 deletions(-) diff --git a/VQI_GenomeBrowser.js b/VQI_GenomeBrowser.js index e6ab272..5b5ff47 100755 --- a/VQI_GenomeBrowser.js +++ b/VQI_GenomeBrowser.js @@ -1,5 +1,6 @@ function VQI_GenomeBrowser(id) { var resourceFolder = 'js/modules/VQI_GenomeBrowser/'; + // var resourceFolder = ''; var serviceURL = resourceFolder + 'dynamic_loading.php'; var deleteImage = resourceFolder + 'DeleteTrack.png'; var exportImage = resourceFolder + 'ExportTrack.png'; @@ -1876,7 +1877,7 @@ function VQI_GenomeBrowser(id) { var trackGroup = this.trackGroup; var file = args.fasta; - var indexFile = args.index + // var indexFile = args.index var drawTrack = function (data) { trackGroup.selectAll("text") @@ -1972,8 +1973,7 @@ function VQI_GenomeBrowser(id) { "chrom": chrom_curr, "start": xStart, "length": length, - "file": file, - "indexFile": indexFile + "file": file } }).success(function (returnData) { var data = returnData.split(""); @@ -2002,7 +2002,7 @@ function VQI_GenomeBrowser(id) { data: { "getFastaRange": "", "chrom": chrom_curr, - "indexFile": indexFile + "file": file } }).success(function (returnData) { range = returnData; diff --git a/VQI_GenomeBrowserDemo.html b/VQI_GenomeBrowserDemo.html index beac1d1..142468a 100644 --- a/VQI_GenomeBrowserDemo.html +++ b/VQI_GenomeBrowserDemo.html @@ -45,10 +45,10 @@ /*fasta: {fasta: "../../genome/hg19.fa", index: "../../genome/hg19.fa.fai"}, genes: {file: "../../genome/hg19_genes.txt", table_name:'hg19_gene_annotation'}, cpg: {file: "../../genome/hg19_genes.txt", table_name:'hg19_cpg'},*/ - //fasta: {fasta: "/Users/corywang/Downloads/hg19.fa.txt", index : "/Users/corywang/Downloads/hg19.fa.fai.txt"}, + fasta: {fasta: ""}, genes: {file: "hg19_refgene.txt", table_name: "hg19_refgene"}, - cpg: {file: "cpg_site_hg19.txt,shelve_hg19.txt,shoer_hg19.txt", table_name: "cpg_site_hg19,shelve_hg19,shoer_hg19"}, + // cpg: {file: "cpg_site_hg19.txt,shelve_hg19.txt,shoer_hg19.txt", table_name: "cpg_site_hg19,shelve_hg19,shoer_hg19"}, //custom: {file: "", table_name:'upload1436411775'} } // "MM9":{ diff --git a/dynamic_loading.php b/dynamic_loading.php index e62d5d0..a789968 100644 --- a/dynamic_loading.php +++ b/dynamic_loading.php @@ -63,11 +63,11 @@ ajaxReturn($result); } else if (isset($_POST["loadFastaData"])){ - $result = loadFastaData($_POST["chrom"],$_POST["start"], $_POST["length"], $_POST["file"],$_POST["indexFile"]); + $result = loadFastaData($_POST["chrom"],$_POST["start"], $_POST["length"], $_POST["file"]); ajaxReturn($result); } else if (isset($_POST["getFastaRange"])){ - $result = getFastaRange($_POST["chrom"],$_POST["indexFile"]); + $result = getFastaRange($_POST["chrom"],$_POST["file"]); ajaxReturn($result); } else if (isset($_POST["getFullTrackData"])){ @@ -124,71 +124,116 @@ } } - function loadFastaData($chrom, $start, $length, $file, $indexFile){ + function loadFastaData($chrom, $start, $length, $file){ - $indexFile = fopen($indexFile, "r"); - $indexArray = []; - while (($line = fgets($indexFile))) { - if($line !== ""){ + // $indexFile = fopen($indexFile, "r"); + // $indexArray = []; + // while (($line = fgets($indexFile))) { + // if($line !== ""){ - $lineArray = explode("\t", $line); - array_push($indexArray,$lineArray); + // $lineArray = explode("\t", $line); + // array_push($indexArray,$lineArray); - } - } - fclose($indexFile); + // } + // } + // fclose($indexFile); - foreach ($indexArray as $key => $value) { - if ($value[0] == $chrom){ - $chromStart = $value[2]; - $chromLength = $value[1]; - $lineLength = $value[3]; - } - } - + // foreach ($indexArray as $key => $value) { + // if ($value[0] == $chrom){ + // $chromStart = $value[2]; + // $chromLength = $value[1]; + // $lineLength = $value[3]; + // } + // } + $chromStart = strlen($chrom) + 2; $lineNum = floor($start/$lineLength); //the line number of the starting bit $startBit = $chromStart + $lineNum + $start; //starting bit position + if($file == "") + $filePath = $file.$chrom.".fa"; + else + $filePath = $file."/".$chrom.".fa"; - $file = new SplFileObject($file); - - $file->fseek($startBit); + //echo filesize($filePath); + // echo $filePath; + // $file = new SplFileObject($file); - $resultLine1 = $file->current(); - $file->next(); - $nextLine2 = $file->current(); - $file->next(); - $nextLine3 = $file->current(); + // $file->fseek($startBit); - $composeLine = $resultLine1 . $nextLine2 . $nextLine3; - $returnString = str_replace("\n", "", $composeLine); - return substr($returnString,0,$length); + // $resultLine1 = $file->current(); + // $file->next(); + // $nextLine2 = $file->current(); + // $file->next(); + // $nextLine3 = $file->current(); + $fp = fopen($filePath, 'r'); + + if($fp){ + fseek($fp, $startBit); + $line = fread($fp, 100); + + // $composeLine = $resultLine1 . $nextLine2 . $nextLine3; + $returnString = str_replace("\n", "", $line); + return substr($returnString,0,$length); + } + else{ + return ""; + } } - function getFastaRange($chrom, $indexFile){ + function my_fseek($fp,$pos,$first=0) { + + // set to 0 pos initially, one-time + if($first) fseek($fp,0,SEEK_SET); + + // get pos float value + $pos=floatval($pos); + + // within limits, use normal fseek + if($pos<=PHP_INT_MAX) + fseek($fp,$pos,SEEK_CUR); + // out of limits, use recursive fseek + else { + fseek($fp,PHP_INT_MAX,SEEK_CUR); + $pos -= PHP_INT_MAX; + my_fseek($fp,$pos); + } + + } + + function getFastaRange($chrom, $file){ + if($file == "") + $filePath = $file.$chrom.".fa"; + else + $filePath = $file."/".$chrom.".fa"; + + $fileSize = filesize($filePath); + $fileSize = $fileSize - (strlen($chrom) + 2); + $numOfLine = ceil($fileSize/51); + $range = $fileSize - $numOfLine - 1; + - $indexFile = fopen($indexFile, "r"); - $indexArray = []; - while (($line = fgets($indexFile))) { - if($line !== ""){ + // $indexFile = fopen($indexFile, "r"); + // $indexArray = []; + // while (($line = fgets($indexFile))) { + // if($line !== ""){ - $lineArray = explode("\t", $line); - array_push($indexArray,$lineArray); + // $lineArray = explode("\t", $line); + // array_push($indexArray,$lineArray); - } - } - fclose($indexFile); + // } + // } + // fclose($indexFile); - foreach ($indexArray as $key => $value) { - if ($value[0] == $chrom){ - $chromLength = $value[1]; - } - } + // foreach ($indexArray as $key => $value) { + // if ($value[0] == $chrom){ + // $chromLength = $value[1]; + // } + // } - return [0,$chromLength]; + return [0,$range]; }