Transparently read indexed block-gzipped (BGZF) files, such as those created by bgzip, using coordinates from the uncompressed file. The module is used in @gmod/indexedfasta to read bgzip-indexed fasta files (with gzi index, fai index, and fa).
Users can also use the unzip
function to unzip bgzip files whole (which pako has trouble with natively)
You can also use the unzipChunk or unzipChunkSlice functions to unzip ranges given by BAI or TBI files for BAM or tabix file formats (which are bgzip based).
The unzip
utility function properly decompresses BGZF chunks in both node and the browser, using pako
when running in the browser and native zlib
when running in node.
Install
$ npm install --save @gmod/bgzf-filehandle
Usage
const { BgzfFilehandle, unzip, unzipChunk } = require('@gmod/bgzf-filehandle')
const f = new BgzfFilehandle({ path: 'path/to/my_file.gz' })
const myBuf = Buffer.alloc(300)
await f.read(myBuf, 0, 300, 23234)
const { size } = f.stat()
const chunkDataBuffer = readDirectlyFromFile(someFile, 123, 456)
const unzippedBuffer = await unzip(chunkDataBuffer)
const { buffer, dpositions, cpositions } = await unzipChunk(chunkDataBuffer)
const { buffer, dpositions, cpositions } = await unzipChunkSlice(
chunkDataBuffer,
chunk,
)
Academic Use
This package was written with funding from the NHGRI as part of the JBrowse project. If you use it in an academic project that you publish, please cite the most recent JBrowse paper, which will be linked from jbrowse.org.
License
MIT © Robert Buels