@gmod/bbi 1.0.28 → 1.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -52
- package/README.md +42 -27
- package/dist/bbi.d.ts +12 -13
- package/dist/bbi.js +361 -593
- package/dist/bigbed.d.ts +7 -16
- package/dist/bigbed.js +285 -455
- package/dist/bigwig.d.ts +2 -2
- package/dist/bigwig.js +88 -110
- package/dist/blockView.d.ts +2 -1
- package/dist/blockView.js +402 -508
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -35
- package/dist/range.js +115 -178
- package/dist/unzip-pako.d.ts +2 -0
- package/dist/unzip-pako.js +8 -0
- package/dist/unzip.d.ts +2 -0
- package/dist/unzip.js +5 -0
- package/dist/util.js +110 -114
- package/esm/bbi.d.ts +84 -0
- package/esm/bbi.js +259 -0
- package/esm/bigbed.d.ts +12 -0
- package/esm/bigbed.js +182 -0
- package/esm/bigwig.d.ts +13 -0
- package/esm/bigwig.js +35 -0
- package/esm/blockView.d.ts +42 -0
- package/esm/blockView.js +321 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +7 -0
- package/esm/range.d.ts +18 -0
- package/esm/range.js +126 -0
- package/esm/unzip-pako.d.ts +2 -0
- package/esm/unzip-pako.js +8 -0
- package/esm/unzip.d.ts +2 -0
- package/esm/unzip.js +5 -0
- package/esm/util.d.ts +24 -0
- package/esm/util.js +74 -0
- package/package.json +29 -31
- package/dist/declares.d.js +0 -2
package/dist/util.js
CHANGED
|
@@ -1,81 +1,92 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (_) try {
|
|
33
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.abortBreakPoint = exports.checkAbortSignal = exports.groupBlocks = exports.AbortError = void 0;
|
|
31
55
|
/* eslint no-bitwise: ["error", { "allow": ["|"] }] */
|
|
32
|
-
var AbortError =
|
|
33
|
-
|
|
34
|
-
function (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
_this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(AbortError).call(this, message));
|
|
42
|
-
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "code", void 0);
|
|
43
|
-
_this.code = 'ERR_ABORTED';
|
|
44
|
-
return _this;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return AbortError;
|
|
48
|
-
}((0, _wrapNativeSuper2.default)(Error)); // sort blocks by file offset and
|
|
49
|
-
// group blocks that are within 2KB of eachother
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
var AbortError = /** @class */ (function (_super) {
|
|
57
|
+
__extends(AbortError, _super);
|
|
58
|
+
function AbortError(message) {
|
|
59
|
+
var _this = _super.call(this, message) || this;
|
|
60
|
+
_this.code = 'ERR_ABORTED';
|
|
61
|
+
return _this;
|
|
62
|
+
}
|
|
63
|
+
return AbortError;
|
|
64
|
+
}(Error));
|
|
52
65
|
exports.AbortError = AbortError;
|
|
53
|
-
|
|
66
|
+
// sort blocks by file offset and
|
|
67
|
+
// group blocks that are within 2KB of eachother
|
|
54
68
|
function groupBlocks(blocks) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
blocks.sort(function (b0, b1) { return (b0.offset | 0) - (b1.offset | 0); });
|
|
70
|
+
var blockGroups = [];
|
|
71
|
+
var lastBlock;
|
|
72
|
+
var lastBlockEnd;
|
|
73
|
+
for (var i = 0; i < blocks.length; i += 1) {
|
|
74
|
+
if (lastBlock && blocks[i].offset - lastBlockEnd <= 2000) {
|
|
75
|
+
lastBlock.length += blocks[i].length - lastBlockEnd + blocks[i].offset;
|
|
76
|
+
lastBlock.blocks.push(blocks[i]);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
blockGroups.push((lastBlock = {
|
|
80
|
+
blocks: [blocks[i]],
|
|
81
|
+
length: blocks[i].length,
|
|
82
|
+
offset: blocks[i].offset,
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
lastBlockEnd = lastBlock.offset + lastBlock.length;
|
|
72
86
|
}
|
|
73
|
-
|
|
74
|
-
lastBlockEnd = lastBlock.offset + lastBlock.length;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return blockGroups;
|
|
87
|
+
return blockGroups;
|
|
78
88
|
}
|
|
89
|
+
exports.groupBlocks = groupBlocks;
|
|
79
90
|
/**
|
|
80
91
|
* Properly check if the given AbortSignal is aborted.
|
|
81
92
|
* Per the standard, if the signal reads as aborted,
|
|
@@ -87,55 +98,40 @@ function groupBlocks(blocks) {
|
|
|
87
98
|
* @param {AbortSignal} [signal] an AbortSignal, or anything with an `aborted` attribute
|
|
88
99
|
* @returns nothing
|
|
89
100
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
92
101
|
function checkAbortSignal(signal) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
if (!signal) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (signal.aborted) {
|
|
106
|
+
// console.log('bam aborted!')
|
|
107
|
+
if (typeof DOMException !== 'undefined') {
|
|
108
|
+
throw new DOMException('aborted', 'AbortError');
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
var e = new AbortError('aborted');
|
|
112
|
+
e.code = 'ERR_ABORTED';
|
|
113
|
+
throw e;
|
|
114
|
+
}
|
|
103
115
|
}
|
|
104
|
-
}
|
|
105
116
|
}
|
|
117
|
+
exports.checkAbortSignal = checkAbortSignal;
|
|
106
118
|
/**
|
|
107
119
|
* Skips to the next tick, then runs `checkAbortSignal`.
|
|
108
120
|
* Await this to inside an otherwise synchronous loop to
|
|
109
121
|
* provide a place to break when an abort signal is received.
|
|
110
122
|
* @param {AbortSignal} signal
|
|
111
123
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
function
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
switch (_context.prev = _context.next) {
|
|
125
|
-
case 0:
|
|
126
|
-
_context.next = 2;
|
|
127
|
-
return Promise.resolve();
|
|
128
|
-
|
|
129
|
-
case 2:
|
|
130
|
-
checkAbortSignal(signal);
|
|
131
|
-
|
|
132
|
-
case 3:
|
|
133
|
-
case "end":
|
|
134
|
-
return _context.stop();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}, _callee);
|
|
138
|
-
}));
|
|
139
|
-
return _abortBreakPoint.apply(this, arguments);
|
|
124
|
+
function abortBreakPoint(signal) {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
126
|
+
return __generator(this, function (_a) {
|
|
127
|
+
switch (_a.label) {
|
|
128
|
+
case 0: return [4 /*yield*/, Promise.resolve()];
|
|
129
|
+
case 1:
|
|
130
|
+
_a.sent();
|
|
131
|
+
checkAbortSignal(signal);
|
|
132
|
+
return [2 /*return*/];
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
140
136
|
}
|
|
141
|
-
|
|
137
|
+
exports.abortBreakPoint = abortBreakPoint;
|
package/esm/bbi.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { GenericFilehandle } from 'generic-filehandle';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { BlockView } from './blockView';
|
|
4
|
+
export interface Feature {
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
score: number;
|
|
8
|
+
rest?: string;
|
|
9
|
+
minScore?: number;
|
|
10
|
+
maxScore?: number;
|
|
11
|
+
summary?: boolean;
|
|
12
|
+
uniqueId?: string;
|
|
13
|
+
field?: number;
|
|
14
|
+
}
|
|
15
|
+
interface Statistics {
|
|
16
|
+
scoreSum: number;
|
|
17
|
+
basesCovered: number;
|
|
18
|
+
scoreSumSquares: number;
|
|
19
|
+
}
|
|
20
|
+
interface RefInfo {
|
|
21
|
+
name: string;
|
|
22
|
+
id: number;
|
|
23
|
+
length: number;
|
|
24
|
+
}
|
|
25
|
+
export interface Header {
|
|
26
|
+
autoSql: string;
|
|
27
|
+
totalSummary: Statistics;
|
|
28
|
+
zoomLevels: any;
|
|
29
|
+
unzoomedIndexOffset: number;
|
|
30
|
+
unzoomedDataOffset: number;
|
|
31
|
+
definedFieldCount: number;
|
|
32
|
+
uncompressBufSize: number;
|
|
33
|
+
chromTreeOffset: number;
|
|
34
|
+
fileSize: number;
|
|
35
|
+
extHeaderOffset: number;
|
|
36
|
+
isBigEndian: boolean;
|
|
37
|
+
fileType: string;
|
|
38
|
+
refsByName: {
|
|
39
|
+
[key: string]: number;
|
|
40
|
+
};
|
|
41
|
+
refsByNumber: {
|
|
42
|
+
[key: number]: RefInfo;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export interface RequestOptions {
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
headers?: Record<string, string>;
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
}
|
|
50
|
+
export declare abstract class BBI {
|
|
51
|
+
protected bbi: GenericFilehandle;
|
|
52
|
+
protected headerCache: any;
|
|
53
|
+
protected renameRefSeqs: (a: string) => string;
|
|
54
|
+
getHeader(opts?: RequestOptions | AbortSignal): any;
|
|
55
|
+
constructor(options?: {
|
|
56
|
+
filehandle?: GenericFilehandle;
|
|
57
|
+
path?: string;
|
|
58
|
+
url?: string;
|
|
59
|
+
renameRefSeqs?: (a: string) => string;
|
|
60
|
+
});
|
|
61
|
+
private _getHeader;
|
|
62
|
+
private _getMainHeader;
|
|
63
|
+
private _isBigEndian;
|
|
64
|
+
private _readChromTree;
|
|
65
|
+
protected getUnzoomedView(opts: RequestOptions): Promise<BlockView>;
|
|
66
|
+
protected abstract getView(scale: number, opts: RequestOptions): Promise<BlockView>;
|
|
67
|
+
/**
|
|
68
|
+
* Gets features from a BigWig file
|
|
69
|
+
*
|
|
70
|
+
* @param refName - The chromosome name
|
|
71
|
+
* @param start - The start of a region
|
|
72
|
+
* @param end - The end of a region
|
|
73
|
+
* @param opts - An object containing basesPerSpan (e.g. pixels per basepair) or scale used to infer the zoomLevel to use
|
|
74
|
+
*/
|
|
75
|
+
getFeatureStream(refName: string, start: number, end: number, opts?: RequestOptions & {
|
|
76
|
+
scale?: number;
|
|
77
|
+
basesPerSpan?: number;
|
|
78
|
+
}): Promise<Observable<Feature[]>>;
|
|
79
|
+
getFeatures(refName: string, start: number, end: number, opts?: RequestOptions & {
|
|
80
|
+
scale?: number;
|
|
81
|
+
basesPerSpan?: number;
|
|
82
|
+
}): Promise<Feature[]>;
|
|
83
|
+
}
|
|
84
|
+
export {};
|
package/esm/bbi.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BBI = void 0;
|
|
7
|
+
const binary_parser_1 = require("@gmod/binary-parser");
|
|
8
|
+
const generic_filehandle_1 = require("generic-filehandle");
|
|
9
|
+
const rxjs_1 = require("rxjs");
|
|
10
|
+
const operators_1 = require("rxjs/operators");
|
|
11
|
+
const abortable_promise_cache_1 = __importDefault(require("abortable-promise-cache"));
|
|
12
|
+
const quick_lru_1 = __importDefault(require("quick-lru"));
|
|
13
|
+
const blockView_1 = require("./blockView");
|
|
14
|
+
const BIG_WIG_MAGIC = -2003829722;
|
|
15
|
+
const BIG_BED_MAGIC = -2021002517;
|
|
16
|
+
/* get the compiled parsers for different sections of the bigwig file
|
|
17
|
+
*
|
|
18
|
+
* @param isBE - is big endian, typically false
|
|
19
|
+
* @return an object with compiled parsers
|
|
20
|
+
*/
|
|
21
|
+
function getParsers(isBE) {
|
|
22
|
+
const le = isBE ? 'big' : 'little';
|
|
23
|
+
const headerParser = new binary_parser_1.Parser()
|
|
24
|
+
.endianess(le)
|
|
25
|
+
.int32('magic')
|
|
26
|
+
.uint16('version')
|
|
27
|
+
.uint16('numZoomLevels')
|
|
28
|
+
.uint64('chromTreeOffset')
|
|
29
|
+
.uint64('unzoomedDataOffset')
|
|
30
|
+
.uint64('unzoomedIndexOffset')
|
|
31
|
+
.uint16('fieldCount')
|
|
32
|
+
.uint16('definedFieldCount')
|
|
33
|
+
.uint64('asOffset') // autoSql offset, used in bigbed
|
|
34
|
+
.uint64('totalSummaryOffset')
|
|
35
|
+
.uint32('uncompressBufSize')
|
|
36
|
+
.uint64('extHeaderOffset') // name index offset, used in bigbed
|
|
37
|
+
.array('zoomLevels', {
|
|
38
|
+
length: 'numZoomLevels',
|
|
39
|
+
type: new binary_parser_1.Parser()
|
|
40
|
+
.uint32('reductionLevel')
|
|
41
|
+
.uint32('reserved')
|
|
42
|
+
.uint64('dataOffset')
|
|
43
|
+
.uint64('indexOffset'),
|
|
44
|
+
});
|
|
45
|
+
const totalSummaryParser = new binary_parser_1.Parser()
|
|
46
|
+
.endianess(le)
|
|
47
|
+
.uint64('basesCovered')
|
|
48
|
+
.double('scoreMin')
|
|
49
|
+
.double('scoreMax')
|
|
50
|
+
.double('scoreSum')
|
|
51
|
+
.double('scoreSumSquares');
|
|
52
|
+
const chromTreeParser = new binary_parser_1.Parser()
|
|
53
|
+
.endianess(le)
|
|
54
|
+
.uint32('magic')
|
|
55
|
+
.uint32('blockSize')
|
|
56
|
+
.uint32('keySize')
|
|
57
|
+
.uint32('valSize')
|
|
58
|
+
.uint64('itemCount');
|
|
59
|
+
const isLeafNode = new binary_parser_1.Parser()
|
|
60
|
+
.endianess(le)
|
|
61
|
+
.uint8('isLeafNode')
|
|
62
|
+
.skip(1)
|
|
63
|
+
.uint16('cnt');
|
|
64
|
+
return {
|
|
65
|
+
chromTreeParser,
|
|
66
|
+
totalSummaryParser,
|
|
67
|
+
headerParser,
|
|
68
|
+
isLeafNode,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
class BBI {
|
|
72
|
+
/*
|
|
73
|
+
* @param filehandle - a filehandle from generic-filehandle or implementing something similar to the node10 fs.promises API
|
|
74
|
+
* @param path - a Local file path as a string
|
|
75
|
+
* @param url - a URL string
|
|
76
|
+
* @param renameRefSeqs - an optional method to rename the internal reference sequences using a mapping function
|
|
77
|
+
*/
|
|
78
|
+
constructor(options = {}) {
|
|
79
|
+
this.headerCache = new abortable_promise_cache_1.default({
|
|
80
|
+
cache: new quick_lru_1.default({ maxSize: 1 }),
|
|
81
|
+
fill: async (params, signal) => {
|
|
82
|
+
return this._getHeader({ ...params, signal });
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const { filehandle, renameRefSeqs, path, url } = options;
|
|
86
|
+
this.renameRefSeqs = renameRefSeqs || ((s) => s);
|
|
87
|
+
if (filehandle) {
|
|
88
|
+
this.bbi = filehandle;
|
|
89
|
+
}
|
|
90
|
+
else if (url) {
|
|
91
|
+
this.bbi = new generic_filehandle_1.RemoteFile(url);
|
|
92
|
+
}
|
|
93
|
+
else if (path) {
|
|
94
|
+
this.bbi = new generic_filehandle_1.LocalFile(path);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error('no file given');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/* fetch and parse header information from a bigwig or bigbed file
|
|
101
|
+
* @param abortSignal - abort the operation, can be null
|
|
102
|
+
* @return a Header object
|
|
103
|
+
*/
|
|
104
|
+
getHeader(opts = {}) {
|
|
105
|
+
const options = 'aborted' in opts ? { signal: opts } : opts;
|
|
106
|
+
return this.headerCache.get(JSON.stringify(options), options, options.signal);
|
|
107
|
+
}
|
|
108
|
+
async _getHeader(opts) {
|
|
109
|
+
const header = await this._getMainHeader(opts);
|
|
110
|
+
const chroms = await this._readChromTree(header, opts);
|
|
111
|
+
return { ...header, ...chroms };
|
|
112
|
+
}
|
|
113
|
+
async _getMainHeader(opts, requestSize = 2000) {
|
|
114
|
+
const { buffer } = await this.bbi.read(Buffer.alloc(requestSize), 0, requestSize, 0, opts);
|
|
115
|
+
const isBigEndian = this._isBigEndian(buffer);
|
|
116
|
+
const ret = getParsers(isBigEndian);
|
|
117
|
+
const header = ret.headerParser.parse(buffer).result;
|
|
118
|
+
header.fileType = header.magic === BIG_BED_MAGIC ? 'bigbed' : 'bigwig';
|
|
119
|
+
if (header.asOffset > requestSize ||
|
|
120
|
+
header.totalSummaryOffset > requestSize) {
|
|
121
|
+
return this._getMainHeader(opts, requestSize * 2);
|
|
122
|
+
}
|
|
123
|
+
if (header.asOffset) {
|
|
124
|
+
header.autoSql = buffer
|
|
125
|
+
.slice(header.asOffset, buffer.indexOf(0, header.asOffset))
|
|
126
|
+
.toString('utf8');
|
|
127
|
+
}
|
|
128
|
+
if (header.totalSummaryOffset > requestSize) {
|
|
129
|
+
return this._getMainHeader(opts, requestSize * 2);
|
|
130
|
+
}
|
|
131
|
+
if (header.totalSummaryOffset) {
|
|
132
|
+
const tail = buffer.slice(header.totalSummaryOffset);
|
|
133
|
+
header.totalSummary = ret.totalSummaryParser.parse(tail).result;
|
|
134
|
+
}
|
|
135
|
+
return { ...header, isBigEndian };
|
|
136
|
+
}
|
|
137
|
+
_isBigEndian(buffer) {
|
|
138
|
+
let ret = buffer.readInt32LE(0);
|
|
139
|
+
if (ret === BIG_WIG_MAGIC || ret === BIG_BED_MAGIC) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
ret = buffer.readInt32BE(0);
|
|
143
|
+
if (ret === BIG_WIG_MAGIC || ret === BIG_BED_MAGIC) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
throw new Error('not a BigWig/BigBed file');
|
|
147
|
+
}
|
|
148
|
+
// todo: add progress if long running
|
|
149
|
+
async _readChromTree(header, opts) {
|
|
150
|
+
const isBE = header.isBigEndian;
|
|
151
|
+
const le = isBE ? 'big' : 'little';
|
|
152
|
+
const refsByNumber = [];
|
|
153
|
+
const refsByName = {};
|
|
154
|
+
const { chromTreeOffset } = header;
|
|
155
|
+
let { unzoomedDataOffset } = header;
|
|
156
|
+
while (unzoomedDataOffset % 4 !== 0) {
|
|
157
|
+
unzoomedDataOffset += 1;
|
|
158
|
+
}
|
|
159
|
+
const { buffer: data } = await this.bbi.read(Buffer.alloc(unzoomedDataOffset - chromTreeOffset), 0, unzoomedDataOffset - chromTreeOffset, chromTreeOffset, opts);
|
|
160
|
+
const p = getParsers(isBE);
|
|
161
|
+
const { keySize } = p.chromTreeParser.parse(data).result;
|
|
162
|
+
const leafNodeParser = new binary_parser_1.Parser()
|
|
163
|
+
.endianess(le)
|
|
164
|
+
.string('key', { stripNull: true, length: keySize })
|
|
165
|
+
.uint32('refId')
|
|
166
|
+
.uint32('refSize');
|
|
167
|
+
const nonleafNodeParser = new binary_parser_1.Parser()
|
|
168
|
+
.endianess(le)
|
|
169
|
+
.skip(keySize)
|
|
170
|
+
.uint64('childOffset');
|
|
171
|
+
const rootNodeOffset = 32;
|
|
172
|
+
const bptReadNode = async (currentOffset) => {
|
|
173
|
+
let offset = currentOffset;
|
|
174
|
+
if (offset >= data.length) {
|
|
175
|
+
throw new Error('reading beyond end of buffer');
|
|
176
|
+
}
|
|
177
|
+
const ret = p.isLeafNode.parse(data.slice(offset));
|
|
178
|
+
const { isLeafNode, cnt } = ret.result;
|
|
179
|
+
offset += ret.offset;
|
|
180
|
+
if (isLeafNode) {
|
|
181
|
+
for (let n = 0; n < cnt; n += 1) {
|
|
182
|
+
const leafRet = leafNodeParser.parse(data.slice(offset));
|
|
183
|
+
offset += leafRet.offset;
|
|
184
|
+
const { key, refId, refSize } = leafRet.result;
|
|
185
|
+
const refRec = { name: key, id: refId, length: refSize };
|
|
186
|
+
refsByName[this.renameRefSeqs(key)] = refId;
|
|
187
|
+
refsByNumber[refId] = refRec;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
// parse index node
|
|
192
|
+
const nextNodes = [];
|
|
193
|
+
for (let n = 0; n < cnt; n += 1) {
|
|
194
|
+
const nonleafRet = nonleafNodeParser.parse(data.slice(offset));
|
|
195
|
+
let { childOffset } = nonleafRet.result;
|
|
196
|
+
offset += nonleafRet.offset;
|
|
197
|
+
childOffset -= chromTreeOffset;
|
|
198
|
+
nextNodes.push(bptReadNode(childOffset));
|
|
199
|
+
}
|
|
200
|
+
await Promise.all(nextNodes);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
await bptReadNode(rootNodeOffset);
|
|
204
|
+
return {
|
|
205
|
+
refsByName,
|
|
206
|
+
refsByNumber,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/*
|
|
210
|
+
* fetches the "unzoomed" view of the bigwig data. this is the default for bigbed
|
|
211
|
+
* @param abortSignal - a signal to optionally abort this operation
|
|
212
|
+
*/
|
|
213
|
+
async getUnzoomedView(opts) {
|
|
214
|
+
const { unzoomedIndexOffset, zoomLevels, refsByName, uncompressBufSize, isBigEndian, fileType, } = await this.getHeader(opts);
|
|
215
|
+
const nzl = zoomLevels[0];
|
|
216
|
+
const cirLen = nzl ? nzl.dataOffset - unzoomedIndexOffset : 4000;
|
|
217
|
+
return new blockView_1.BlockView(this.bbi, refsByName, unzoomedIndexOffset, cirLen, isBigEndian, uncompressBufSize > 0, fileType);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Gets features from a BigWig file
|
|
221
|
+
*
|
|
222
|
+
* @param refName - The chromosome name
|
|
223
|
+
* @param start - The start of a region
|
|
224
|
+
* @param end - The end of a region
|
|
225
|
+
* @param opts - An object containing basesPerSpan (e.g. pixels per basepair) or scale used to infer the zoomLevel to use
|
|
226
|
+
*/
|
|
227
|
+
async getFeatureStream(refName, start, end, opts = {
|
|
228
|
+
scale: 1,
|
|
229
|
+
}) {
|
|
230
|
+
await this.getHeader(opts);
|
|
231
|
+
const chrName = this.renameRefSeqs(refName);
|
|
232
|
+
let view;
|
|
233
|
+
if (opts.basesPerSpan) {
|
|
234
|
+
view = await this.getView(1 / opts.basesPerSpan, opts);
|
|
235
|
+
}
|
|
236
|
+
else if (opts.scale) {
|
|
237
|
+
view = await this.getView(opts.scale, opts);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
view = await this.getView(1, opts);
|
|
241
|
+
}
|
|
242
|
+
if (!view) {
|
|
243
|
+
throw new Error('unable to get block view for data');
|
|
244
|
+
}
|
|
245
|
+
return new rxjs_1.Observable((observer) => {
|
|
246
|
+
view.readWigData(chrName, start, end, observer, opts);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async getFeatures(refName, start, end, opts = {
|
|
250
|
+
scale: 1,
|
|
251
|
+
}) {
|
|
252
|
+
const ob = await this.getFeatureStream(refName, start, end, opts);
|
|
253
|
+
const ret = await ob
|
|
254
|
+
.pipe((0, operators_1.reduce)((acc, curr) => acc.concat(curr)))
|
|
255
|
+
.toPromise();
|
|
256
|
+
return ret || [];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
exports.BBI = BBI;
|
package/esm/bigbed.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BBI, Feature, RequestOptions } from './bbi';
|
|
2
|
+
import { BlockView } from './blockView';
|
|
3
|
+
export declare function filterUndef<T>(ts: (T | undefined)[]): T[];
|
|
4
|
+
export declare class BigBed extends BBI {
|
|
5
|
+
readIndicesCache: any;
|
|
6
|
+
constructor(opts?: any);
|
|
7
|
+
readIndices(opts?: AbortSignal | RequestOptions): any;
|
|
8
|
+
protected getView(scale: number, opts: RequestOptions): Promise<BlockView>;
|
|
9
|
+
private _readIndices;
|
|
10
|
+
private searchExtraIndexBlocks;
|
|
11
|
+
searchExtraIndex(name: string, opts?: RequestOptions): Promise<Feature[]>;
|
|
12
|
+
}
|