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) { function VQI_GenomeBrowser(id) {
var resourceFolder = 'js/modules/VQI_GenomeBrowser/'; var resourceFolder = 'js/modules/VQI_GenomeBrowser/';
// var resourceFolder = '';
var serviceURL = resourceFolder + 'dynamic_loading.php'; var serviceURL = resourceFolder + 'dynamic_loading.php';
var deleteImage = resourceFolder + 'DeleteTrack.png'; var deleteImage = resourceFolder + 'DeleteTrack.png';
var exportImage = resourceFolder + 'ExportTrack.png'; var exportImage = resourceFolder + 'ExportTrack.png';
Expand Down Expand Up @@ -1876,7 +1877,7 @@ function VQI_GenomeBrowser(id) {
var trackGroup = this.trackGroup; var trackGroup = this.trackGroup;


var file = args.fasta; var file = args.fasta;
var indexFile = args.index // var indexFile = args.index


var drawTrack = function (data) { var drawTrack = function (data) {
trackGroup.selectAll("text") trackGroup.selectAll("text")
Expand Down Expand Up @@ -1972,8 +1973,7 @@ function VQI_GenomeBrowser(id) {
"chrom": chrom_curr, "chrom": chrom_curr,
"start": xStart, "start": xStart,
"length": length, "length": length,
"file": file, "file": file
"indexFile": indexFile
} }
}).success(function (returnData) { }).success(function (returnData) {
var data = returnData.split(""); var data = returnData.split("");
Expand Down Expand Up @@ -2002,7 +2002,7 @@ function VQI_GenomeBrowser(id) {
data: { data: {
"getFastaRange": "", "getFastaRange": "",
"chrom": chrom_curr, "chrom": chrom_curr,
"indexFile": indexFile "file": file
} }
}).success(function (returnData) { }).success(function (returnData) {
range = 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"}, /*fasta: {fasta: "../../genome/hg19.fa", index: "../../genome/hg19.fa.fai"},
genes: {file: "../../genome/hg19_genes.txt", table_name:'hg19_gene_annotation'}, genes: {file: "../../genome/hg19_genes.txt", table_name:'hg19_gene_annotation'},
cpg: {file: "../../genome/hg19_genes.txt", table_name:'hg19_cpg'},*/ 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"}, 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'} //custom: {file: "", table_name:'upload1436411775'}
} }
// "MM9":{ // "MM9":{
Expand Down
139 changes: 92 additions & 47 deletions dynamic_loading.php
Expand Up @@ -63,11 +63,11 @@
ajaxReturn($result); ajaxReturn($result);
} }
else if (isset($_POST["loadFastaData"])){ 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); ajaxReturn($result);
} }
else if (isset($_POST["getFastaRange"])){ else if (isset($_POST["getFastaRange"])){
$result = getFastaRange($_POST["chrom"],$_POST["indexFile"]); $result = getFastaRange($_POST["chrom"],$_POST["file"]);
ajaxReturn($result); ajaxReturn($result);
} }
else if (isset($_POST["getFullTrackData"])){ 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"); // $indexFile = fopen($indexFile, "r");
$indexArray = []; // $indexArray = [];
while (($line = fgets($indexFile))) { // while (($line = fgets($indexFile))) {
if($line !== ""){ // if($line !== ""){


$lineArray = explode("\t", $line); // $lineArray = explode("\t", $line);
array_push($indexArray,$lineArray); // array_push($indexArray,$lineArray);


} // }
} // }
fclose($indexFile); // fclose($indexFile);






foreach ($indexArray as $key => $value) { // foreach ($indexArray as $key => $value) {
if ($value[0] == $chrom){ // if ($value[0] == $chrom){
$chromStart = $value[2]; // $chromStart = $value[2];
$chromLength = $value[1]; // $chromLength = $value[1];
$lineLength = $value[3]; // $lineLength = $value[3];
} // }
} // }



$chromStart = strlen($chrom) + 2;
$lineNum = floor($start/$lineLength); //the line number of the starting bit $lineNum = floor($start/$lineLength); //the line number of the starting bit
$startBit = $chromStart + $lineNum + $start; //starting bit position $startBit = $chromStart + $lineNum + $start; //starting bit position


if($file == "")
$filePath = $file.$chrom.".fa";
else
$filePath = $file."/".$chrom.".fa";


$file = new SplFileObject($file); //echo filesize($filePath);

// echo $filePath;
$file->fseek($startBit); // $file = new SplFileObject($file);


$resultLine1 = $file->current(); // $file->fseek($startBit);
$file->next();
$nextLine2 = $file->current();
$file->next();
$nextLine3 = $file->current();


$composeLine = $resultLine1 . $nextLine2 . $nextLine3; // $resultLine1 = $file->current();
$returnString = str_replace("\n", "", $composeLine); // $file->next();
return substr($returnString,0,$length); // $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"); // $indexFile = fopen($indexFile, "r");
$indexArray = []; // $indexArray = [];
while (($line = fgets($indexFile))) { // while (($line = fgets($indexFile))) {
if($line !== ""){ // if($line !== ""){


$lineArray = explode("\t", $line); // $lineArray = explode("\t", $line);
array_push($indexArray,$lineArray); // array_push($indexArray,$lineArray);


} // }
} // }
fclose($indexFile); // fclose($indexFile);


foreach ($indexArray as $key => $value) { // foreach ($indexArray as $key => $value) {
if ($value[0] == $chrom){ // if ($value[0] == $chrom){
$chromLength = $value[1]; // $chromLength = $value[1];
} // }
} // }


return [0,$chromLength]; return [0,$range];
} }




Expand Down

0 comments on commit 0b97861

Please sign in to comment.