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.
90 lines • 2.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryParser = void 0;
class BinaryParser {
constructor(data, littleEndian = true) {
this.littleEndian = littleEndian;
this.view = new DataView(data);
this.position = 0;
this.length = this.view.byteLength;
}
remLength() {
return this.length - this.position;
}
getValue(readFunc, positionIncrement) {
let retValue = readFunc(this.position, this.littleEndian);
this.position += positionIncrement;
return retValue;
}
getByte() {
return this.getValue((p) => this.view.getUint8(p), 1);
}
getShort() {
return this.getValue((p, le) => this.view.getInt16(p, le), 2);
}
getUShort() {
return this.getValue((p, le) => this.view.getUint16(p, le), 2);
}
getInt() {
return this.getValue((p, le) => this.view.getInt32(p, le), 4);
}
getUInt() {
return this.getValue((p, le) => this.view.getUint32(p, le), 4);
}
getFloat() {
return this.getValue((p, le) => this.view.getFloat32(p, le), 4);
}
getDouble() {
return this.getValue((p, le) => this.view.getFloat64(p, le), 8);
}
getLong() {
let b = [];
for (let i = 0; i < 8; i++) {
b[i] = this.view.getUint8(this.position + i);
}
let value = 0;
if (this.littleEndian) {
for (let i = b.length - 1; i >= 0; i--) {
value = (value * 256) + b[i];
}
}
else {
for (let i = 0; i < b.length; i++) {
value = (value * 256) + b[i];
}
}
this.position += 8;
return value;
}
getString(len) {
let s = "", c;
while ((c = this.view.getUint8(this.position++)) != 0) {
s += String.fromCharCode(c);
if (len && s.length == len)
break;
}
return s;
}
getFixedLengthString(len) {
let s = "";
for (let i = 0; i < len; i++) {
let c = this.view.getUint8(this.position++);
if (c > 0) {
s += String.fromCharCode(c);
}
}
return s;
}
getFixedLengthTrimmedString(len) {
let s = "";
for (let i = 0; i < len; i++) {
let c = this.view.getUint8(this.position++);
if (c > 32) {
s += String.fromCharCode(c);
}
}
return s;
}
}
exports.BinaryParser = BinaryParser;
//# sourceMappingURL=BinaryParser.js.map