genomic-reader
Version:
A Typescript library for reading BigWig, BigBed, 2bit, and Bam files. Capable of streaming. For use in the browser or on Node.js.
45 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bgzfUnzip = void 0;
const pako_1 = require("pako");
function bgzfUnzip(inputData, chunk) {
let pos = 0;
const decompressedBlocks = [];
const fileStartingOffset = chunk !== undefined ? chunk.start.blockPosition : undefined;
let stream;
do {
const remainingInput = inputData.slice(pos);
const inflator = new pako_1.Inflate();
stream = inflator.strm;
inflator.push(remainingInput, 2);
if (inflator.err)
throw new Error(inflator.msg);
decompressedBlocks.push(inflator.result);
if (chunk !== undefined) {
if (decompressedBlocks.length === 1 && chunk.start.dataPosition) {
decompressedBlocks[0] = decompressedBlocks[0].slice(chunk.start.dataPosition);
}
if (fileStartingOffset + pos >= chunk.end.blockPosition) {
const newEnd = chunk.end.blockPosition === chunk.start.blockPosition ?
chunk.end.dataPosition - chunk.start.dataPosition + 1 : chunk.end.dataPosition + 1;
const lastIndex = decompressedBlocks.length - 1;
decompressedBlocks[lastIndex] = decompressedBlocks[lastIndex].slice(0, newEnd);
break;
}
}
pos += stream.next_in;
} while (stream.avail_in);
const result = mergedTypedArrays(decompressedBlocks, Uint8Array);
return result.buffer;
}
exports.bgzfUnzip = bgzfUnzip;
function mergedTypedArrays(arrays, type = Uint8Array) {
const ret = new (type)(arrays.reduce((acc, arr) => acc + arr.byteLength, 0));
let off = 0;
arrays.forEach((arr) => {
ret.set(arr, off);
off += arr.byteLength;
});
return ret;
}
//# sourceMappingURL=Bgzf.js.map