@gmod/bbi 1.0.30 → 1.0.33

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/dist/util.js CHANGED
@@ -1,85 +1,92 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.groupBlocks = groupBlocks;
9
- exports.checkAbortSignal = checkAbortSignal;
10
- exports.abortBreakPoint = abortBreakPoint;
11
- exports.AbortError = void 0;
12
-
13
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
14
-
15
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
16
-
17
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
18
-
19
- var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
20
-
21
- var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
22
-
23
- var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
24
-
25
- var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
26
-
27
- var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));
28
-
29
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
30
-
31
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
32
-
33
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
34
-
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;
35
55
  /* eslint no-bitwise: ["error", { "allow": ["|"] }] */
36
- var AbortError = /*#__PURE__*/function (_Error) {
37
- (0, _inherits2.default)(AbortError, _Error);
38
-
39
- var _super = _createSuper(AbortError);
40
-
41
- function AbortError(message) {
42
- var _this;
43
-
44
- (0, _classCallCheck2.default)(this, AbortError);
45
- _this = _super.call(this, message);
46
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "code", void 0);
47
- _this.code = 'ERR_ABORTED';
48
- return _this;
49
- }
50
-
51
- return AbortError;
52
- }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error)); // sort blocks by file offset and
53
- // group blocks that are within 2KB of eachother
54
-
55
-
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));
56
65
  exports.AbortError = AbortError;
57
-
66
+ // sort blocks by file offset and
67
+ // group blocks that are within 2KB of eachother
58
68
  function groupBlocks(blocks) {
59
- blocks.sort(function (b0, b1) {
60
- return (b0.offset | 0) - (b1.offset | 0);
61
- });
62
- var blockGroups = [];
63
- var lastBlock;
64
- var lastBlockEnd;
65
-
66
- for (var i = 0; i < blocks.length; i += 1) {
67
- if (lastBlock && blocks[i].offset - lastBlockEnd <= 2000) {
68
- lastBlock.length += blocks[i].length - lastBlockEnd + blocks[i].offset;
69
- lastBlock.blocks.push(blocks[i]);
70
- } else {
71
- blockGroups.push(lastBlock = {
72
- blocks: [blocks[i]],
73
- length: blocks[i].length,
74
- offset: blocks[i].offset
75
- });
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;
76
86
  }
77
-
78
- lastBlockEnd = lastBlock.offset + lastBlock.length;
79
- }
80
-
81
- return blockGroups;
87
+ return blockGroups;
82
88
  }
89
+ exports.groupBlocks = groupBlocks;
83
90
  /**
84
91
  * Properly check if the given AbortSignal is aborted.
85
92
  * Per the standard, if the signal reads as aborted,
@@ -91,53 +98,40 @@ function groupBlocks(blocks) {
91
98
  * @param {AbortSignal} [signal] an AbortSignal, or anything with an `aborted` attribute
92
99
  * @returns nothing
93
100
  */
94
-
95
-
96
101
  function checkAbortSignal(signal) {
97
- if (!signal) return;
98
-
99
- if (signal.aborted) {
100
- // console.log('bam aborted!')
101
- if (typeof DOMException !== 'undefined') {
102
- throw new DOMException('aborted', 'AbortError');
103
- } else {
104
- var e = new AbortError('aborted');
105
- e.code = 'ERR_ABORTED';
106
- throw e;
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
+ }
107
115
  }
108
- }
109
116
  }
117
+ exports.checkAbortSignal = checkAbortSignal;
110
118
  /**
111
119
  * Skips to the next tick, then runs `checkAbortSignal`.
112
120
  * Await this to inside an otherwise synchronous loop to
113
121
  * provide a place to break when an abort signal is received.
114
122
  * @param {AbortSignal} signal
115
123
  */
116
-
117
-
118
- function abortBreakPoint(_x) {
119
- return _abortBreakPoint.apply(this, arguments);
120
- }
121
-
122
- function _abortBreakPoint() {
123
- _abortBreakPoint = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(signal) {
124
- return _regenerator.default.wrap(function _callee$(_context) {
125
- while (1) {
126
- switch (_context.prev = _context.next) {
127
- case 0:
128
- _context.next = 2;
129
- return Promise.resolve();
130
-
131
- case 2:
132
- checkAbortSignal(signal);
133
-
134
- case 3:
135
- case "end":
136
- return _context.stop();
137
- }
138
- }
139
- }, _callee);
140
- }));
141
- 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
+ });
142
136
  }
143
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LlxuICogQHBhcmFtIHtBYm9ydFNpZ25hbH0gc2lnbmFsXG4gKi9cbmV4cG9ydCBhc3luYyBmdW5jdGlvbiBhYm9ydEJyZWFrUG9pbnQoc2lnbmFsPzogQWJvcnRTaWduYWwpOiBQcm9taXNlPHZvaWQ+IHtcbiAgYXdhaXQgUHJvbWlzZS5yZXNvbHZlKClcbiAgY2hlY2tBYm9ydFNpZ25hbChzaWduYWwpXG59XG4iXX0=
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
+ .subarray(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.subarray(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.subarray(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.subarray(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.subarray(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;
@@ -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
+ }