UNPKG

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.

81 lines 3.69 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readBamHeaderData = void 0; const BinaryParser_1 = require("../util/BinaryParser"); const pako_1 = require("pako"); const misc_1 = require("../util/misc"); const BAM_MAGIC = 0x014d4142; class BamHeaderReader { constructor(bamDataLoader, fetchSize = 56000) { this.bamDataLoader = bamDataLoader; this.fetchSize = fetchSize; } readHeaderData() { return __awaiter(this, void 0, void 0, function* () { const magic = yield this.readUInt(); if (magic !== BAM_MAGIC) throw Error("Invalid Bam File!"); const textLen = yield this.readInt(); const headerText = yield this.readString(textLen); const numRefs = yield this.readInt(); const chromToId = {}; const idToChrom = []; for (let refId = 0; refId < numRefs; refId++) { const nameLen = yield this.readInt(); const refName = yield this.readString(nameLen); yield this.readInt(); chromToId[refName] = refId; idToChrom.push(refName); } return { text: headerText, chromToId, idToChrom }; }); } readUInt() { return __awaiter(this, void 0, void 0, function* () { yield this.loadIfNeeded(4); return this.parser.getUInt(); }); } readInt() { return __awaiter(this, void 0, void 0, function* () { yield this.loadIfNeeded(4); return this.parser.getInt(); }); } readString(len) { return __awaiter(this, void 0, void 0, function* () { yield this.loadIfNeeded(len); return this.parser.getString(len); }); } loadIfNeeded(bytesNeeded) { return __awaiter(this, void 0, void 0, function* () { if (this.parser !== undefined && this.parser.remLength() >= bytesNeeded) return; const start = this.rawLoadedData === undefined ? 0 : this.rawLoadedData.byteLength; const newHeaderData = yield this.bamDataLoader.load(start, this.fetchSize); this.rawLoadedData = this.rawLoadedData === undefined ? newHeaderData : misc_1.appendBuffer(this.rawLoadedData, newHeaderData); const unzippedHeaderData = pako_1.inflate(new Uint8Array(this.rawLoadedData)); const currentParserPos = this.parser === undefined ? 0 : this.parser.position; this.parser = new BinaryParser_1.BinaryParser(unzippedHeaderData.buffer); this.parser.position = currentParserPos; }); } } function readBamHeaderData(bamDataLoader, fetchSize) { return __awaiter(this, void 0, void 0, function* () { return new BamHeaderReader(bamDataLoader, fetchSize).readHeaderData(); }); } exports.readBamHeaderData = readBamHeaderData; //# sourceMappingURL=BamHeaderReader.js.map