Skip to content

Commit

Permalink
Add Bgzf fasta
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Aug 28, 2018
1 parent dd93bf8 commit 0200a31
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
86 changes: 86 additions & 0 deletions src/JBrowse/Store/SeqFeature/BgzipIndexedFasta.js
@@ -0,0 +1,86 @@
const LRU = cjsRequire('lru-cache')
const { BgzipIndexedFasta } = cjsRequire('@gmod/fasta')
const { Buffer } = cjsRequire('buffer')

const fastaIndexedFilesCache = LRU(5)

const BlobFilehandleWrapper = cjsRequire('../../Model/BlobFilehandleWrapper')

define( [ 'dojo/_base/declare',
'dojo/_base/lang',
'JBrowse/Model/XHRBlob',
'JBrowse/Store/SeqFeature/IndexedFasta'
],
function(
declare,
lang,
XHRBlob,
IndexedFasta
) {

return declare( IndexedFasta,
{

/**
* Storage backend for sequences in indexed fasta files
* served as static text files.
* @constructs
*/
constructor: function(args) {
let dataBlob
if (args.fasta)
dataBlob = new BlobFilehandleWrapper(args.fasta)
else if (args.urlTemplate)
dataBlob = new BlobFilehandleWrapper(new XHRBlob(this.resolveUrl(args.urlTemplate), { expectRanges: true }))
else
dataBlob = new BlobFilehandleWrapper(new XHRBlob('data.fa', { expectRanges: true }))

let indexBlob
if (args.fai)
indexBlob = new BlobFilehandleWrapper(args.fai)
else if (args.faiUrlTemplate)
indexBlob = new BlobFilehandleWrapper(new XHRBlob(this.resolveUrl(args.faiUrlTemplate)))
else if (args.urlTemplate)
indexBlob = new BlobFilehandleWrapper(new XHRBlob(this.resolveUrl(args.urlTemplate+'.fai')))
else throw new Error('no index provided, must provide a FASTA index')


let gziBlob
if(args.gzi)
gziBlob = new BlobFilehandleWrapper(args.gzi)
else if(args.gziUrlTemplate)
gziBlob = new BlobFilehandleWrapper(new XHRBlob(this.resolveUrl(args.gziUrlTemplate)))
else if (args.urlTemplate)
gziBlob = new BlobFilehandleWrapper(new XHRBlob(this.resolveUrl(args.urlTemplate+'.gzi')))
else throw new Error('no gzi index provided, must provide a GZI index')

this.source = dataBlob.toString()

// LRU-cache the CRAM object so we don't have to re-download the
// index when we switch chromosomes
const cacheKey = `data: ${dataBlob}, index: ${indexBlob}`
this.fasta = fastaIndexedFilesCache.get(cacheKey)
if (!this.fasta) {
this.fasta = new BgzipIndexedFasta({
fasta: dataBlob,
fai: indexBlob,
gzi: gziBlob
})

fastaIndexedFilesCache.set(cacheKey, this.fasta)
}
this.fasta.getSequenceList().then(
() => this._deferred.features.resolve({success:true}),
() => this._failAllDeferred()
)
},
saveStore: function() {
return {
urlTemplate: (this.config.file||this.config.blob).url,
faiUrlTemplate: this.config.fai.url,
gziUrlTemplate: this.config.gzi.url
};
}

});
});
8 changes: 4 additions & 4 deletions src/JBrowse/Store/SeqFeature/IndexedFasta.js
Expand Up @@ -40,7 +40,7 @@ return declare( [ SeqFeatureStore, DeferredFeaturesMixin ],
* served as static text files.
* @constructs
*/
constructor: async function(args) {
constructor: function(args) {
let dataBlob
if (args.fasta)
dataBlob = new BlobFilehandleWrapper(args.fasta)
Expand Down Expand Up @@ -70,7 +70,7 @@ return declare( [ SeqFeatureStore, DeferredFeaturesMixin ],
fai: indexBlob
})

fastaIndexedFilesCache.set(cacheKey, this.cram)
fastaIndexedFilesCache.set(cacheKey, this.fasta)
}
this.fasta.getSequenceList().then(
() => this._deferred.features.resolve({success:true}),
Expand All @@ -88,13 +88,13 @@ return declare( [ SeqFeatureStore, DeferredFeaturesMixin ],

getRefSeqs: function( featCallback, errorCallback ) {
this.fasta.getSequenceSizes().then((seqs) => {
console.log(seqs);
featCallback(seqs);
}, errorCallback);
},
saveStore: function() {
return {
urlTemplate: (this.config.file||this.config.blob).url
urlTemplate: (this.config.file||this.config.blob).url,
faiUrlTemplate: this.config.fai.url
};
}

Expand Down

0 comments on commit 0200a31

Please sign in to comment.