Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fasta file loading fix
  • Loading branch information
yuj12001 committed Oct 14, 2015
1 parent 4f20779 commit 0b97861
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 53 deletions.
8 changes: 4 additions & 4 deletions 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';
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -2002,7 +2002,7 @@ function VQI_GenomeBrowser(id) {
data: {
"getFastaRange": "",
"chrom": chrom_curr,
"indexFile": indexFile
"file": file
}
}).success(function (returnData) {
range = returnData;
Expand Down
4 changes: 2 additions & 2 deletions VQI_GenomeBrowserDemo.html
Expand Up @@ -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":{
Expand Down
139 changes: 92 additions & 47 deletions dynamic_loading.php
Expand Up @@ -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"])){
Expand Down Expand Up @@ -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];
}


Expand Down

0 comments on commit 0b97861

Please sign in to comment.