@gmod/bam 1.1.13 → 1.1.16

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 CHANGED
@@ -1,7 +1,22 @@
1
- ## [1.1.13](https://github.com/GMOD/bam-js/compare/v1.1.12...v1.1.13) (2022-02-25)
1
+ ## [1.1.16](https://github.com/GMOD/bam-js/compare/v1.1.15...v1.1.16) (2022-03-30)
2
+
3
+
4
+
5
+ - Add src directory for better source maps
6
+
7
+ ## [1.1.15](https://github.com/GMOD/bam-js/compare/v1.1.14...v1.1.15) (2022-03-18)
2
8
 
3
9
 
4
10
 
11
+ - Fix for htsget failing with message 'input must be buffer, number, or string, received object'
12
+ - Speed improvement by caching chunks of features
13
+
14
+ ## [1.1.14](https://github.com/GMOD/bam-js/compare/v1.1.13...v1.1.14) (2022-03-14)
15
+
16
+ - Fix seq function for corner case
17
+
18
+ ## [1.1.13](https://github.com/GMOD/bam-js/compare/v1.1.12...v1.1.13) (2022-02-25)
19
+
5
20
  - Optimize qual and sequence string record functions for less GC pressure
6
21
 
7
22
  <a name="1.1.12"></a>
package/dist/bamFile.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import Chunk from './chunk';
3
2
  import { GenericFilehandle } from 'generic-filehandle';
3
+ import Chunk from './chunk';
4
4
  import BAMFeature from './record';
5
5
  import { BamOpts, BaseOpts } from './util';
6
6
  export declare const BAM_MAGIC = 21840194;
@@ -14,6 +14,7 @@ export default class BamFile {
14
14
  protected chrToIndex: any;
15
15
  protected indexToChr: any;
16
16
  private yieldThreadTime;
17
+ private featureCache;
17
18
  /**
18
19
  * @param {object} args
19
20
  * @param {string} [args.bamPath]
@@ -56,7 +57,7 @@ export default class BamFile {
56
57
  getRecordsForRange(chr: string, min: number, max: number, opts?: BamOpts): Promise<BAMFeature[]>;
57
58
  streamRecordsForRange(chr: string, min: number, max: number, opts?: BamOpts): AsyncGenerator<BAMFeature[], void, unknown>;
58
59
  _fetchChunkFeatures(chunks: Chunk[], chrId: number, min: number, max: number, opts: BamOpts): AsyncGenerator<BAMFeature[], void, unknown>;
59
- fetchPairs(chrId: number, featPromises: Promise<BAMFeature[]>[], opts: BamOpts): Promise<BAMFeature[]>;
60
+ fetchPairs(chrId: number, feats: BAMFeature[][], opts: BamOpts): Promise<BAMFeature[]>;
60
61
  _readChunk({ chunk, opts }: {
61
62
  chunk: Chunk;
62
63
  opts: BaseOpts;
package/dist/bamFile.js CHANGED
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -75,17 +86,67 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
75
86
  };
76
87
  Object.defineProperty(exports, "__esModule", { value: true });
77
88
  exports.BAM_MAGIC = void 0;
78
- var bai_1 = __importDefault(require("./bai"));
79
- var csi_1 = __importDefault(require("./csi"));
80
89
  var buffer_crc32_1 = __importDefault(require("buffer-crc32"));
81
90
  var bgzf_filehandle_1 = require("@gmod/bgzf-filehandle");
82
91
  var object_entries_ponyfill_1 = __importDefault(require("object.entries-ponyfill"));
83
92
  var generic_filehandle_1 = require("generic-filehandle");
93
+ var abortable_promise_cache_1 = __importDefault(require("abortable-promise-cache"));
94
+ var quick_lru_1 = __importDefault(require("quick-lru"));
95
+ //locals
96
+ var bai_1 = __importDefault(require("./bai"));
97
+ var csi_1 = __importDefault(require("./csi"));
84
98
  var record_1 = __importDefault(require("./record"));
85
99
  var sam_1 = require("./sam");
86
100
  var util_1 = require("./util");
87
101
  exports.BAM_MAGIC = 21840194;
88
102
  var blockLen = 1 << 16;
103
+ function flat(arr) {
104
+ var _a;
105
+ return (_a = []).concat.apply(_a, arr);
106
+ }
107
+ function gen2array(gen) {
108
+ var gen_1, gen_1_1;
109
+ var e_1, _a;
110
+ return __awaiter(this, void 0, void 0, function () {
111
+ var out, x, e_1_1;
112
+ return __generator(this, function (_b) {
113
+ switch (_b.label) {
114
+ case 0:
115
+ out = [];
116
+ _b.label = 1;
117
+ case 1:
118
+ _b.trys.push([1, 6, 7, 12]);
119
+ gen_1 = __asyncValues(gen);
120
+ _b.label = 2;
121
+ case 2: return [4 /*yield*/, gen_1.next()];
122
+ case 3:
123
+ if (!(gen_1_1 = _b.sent(), !gen_1_1.done)) return [3 /*break*/, 5];
124
+ x = gen_1_1.value;
125
+ out.push(x);
126
+ _b.label = 4;
127
+ case 4: return [3 /*break*/, 2];
128
+ case 5: return [3 /*break*/, 12];
129
+ case 6:
130
+ e_1_1 = _b.sent();
131
+ e_1 = { error: e_1_1 };
132
+ return [3 /*break*/, 12];
133
+ case 7:
134
+ _b.trys.push([7, , 10, 11]);
135
+ if (!(gen_1_1 && !gen_1_1.done && (_a = gen_1.return))) return [3 /*break*/, 9];
136
+ return [4 /*yield*/, _a.call(gen_1)];
137
+ case 8:
138
+ _b.sent();
139
+ _b.label = 9;
140
+ case 9: return [3 /*break*/, 11];
141
+ case 10:
142
+ if (e_1) throw e_1.error;
143
+ return [7 /*endfinally*/];
144
+ case 11: return [7 /*endfinally*/];
145
+ case 12: return [2 /*return*/, out];
146
+ }
147
+ });
148
+ });
149
+ }
89
150
  var BamFile = /** @class */ (function () {
90
151
  /**
91
152
  * @param {object} args
@@ -96,6 +157,34 @@ var BamFile = /** @class */ (function () {
96
157
  */
97
158
  function BamFile(_a) {
98
159
  var bamFilehandle = _a.bamFilehandle, bamPath = _a.bamPath, bamUrl = _a.bamUrl, baiPath = _a.baiPath, baiFilehandle = _a.baiFilehandle, baiUrl = _a.baiUrl, csiPath = _a.csiPath, csiFilehandle = _a.csiFilehandle, csiUrl = _a.csiUrl, fetchSizeLimit = _a.fetchSizeLimit, chunkSizeLimit = _a.chunkSizeLimit, _b = _a.yieldThreadTime, yieldThreadTime = _b === void 0 ? 100 : _b, _c = _a.renameRefSeqs, renameRefSeqs = _c === void 0 ? function (n) { return n; } : _c;
160
+ var _this = this;
161
+ this.featureCache = new abortable_promise_cache_1.default({
162
+ //@ts-ignore
163
+ cache: new quick_lru_1.default({
164
+ maxSize: 50,
165
+ }),
166
+ //@ts-ignore
167
+ fill: function (_a, signal) {
168
+ var chunk = _a.chunk, opts = _a.opts;
169
+ return __awaiter(_this, void 0, void 0, function () {
170
+ var _b, data, cpositions, dpositions, feats;
171
+ return __generator(this, function (_c) {
172
+ switch (_c.label) {
173
+ case 0: return [4 /*yield*/, this._readChunk({
174
+ chunk: chunk,
175
+ opts: __assign(__assign({}, opts), { signal: signal }),
176
+ })];
177
+ case 1:
178
+ _b = _c.sent(), data = _b.data, cpositions = _b.cpositions, dpositions = _b.dpositions;
179
+ return [4 /*yield*/, this.readBamFeatures(data, cpositions, dpositions, chunk)];
180
+ case 2:
181
+ feats = _c.sent();
182
+ return [2 /*return*/, feats];
183
+ }
184
+ });
185
+ });
186
+ },
187
+ });
99
188
  this.renameRefSeq = renameRefSeqs;
100
189
  if (bamFilehandle) {
101
190
  this.bam = bamFilehandle;
@@ -205,108 +294,63 @@ var BamFile = /** @class */ (function () {
205
294
  });
206
295
  });
207
296
  };
208
- // the full length of the refseq block is not given in advance so this grabs a chunk and
209
- // doubles it if all refseqs haven't been processed
297
+ // the full length of the refseq block is not given in advance so this grabs
298
+ // a chunk and doubles it if all refseqs haven't been processed
210
299
  BamFile.prototype._readRefSeqs = function (start, refSeqBytes, opts) {
211
300
  if (opts === void 0) { opts = {}; }
212
301
  return __awaiter(this, void 0, void 0, function () {
213
- var res, bytesRead, buffer, uncba, nRef, p, chrToIndex, indexToChr, i, lName, refName, lRef;
214
- return __generator(this, function (_a) {
215
- switch (_a.label) {
302
+ var size, _a, bytesRead, buffer, uncba, nRef, p, chrToIndex, indexToChr, i, lName, refName, lRef;
303
+ return __generator(this, function (_b) {
304
+ switch (_b.label) {
216
305
  case 0:
217
306
  if (start > refSeqBytes) {
218
307
  return [2 /*return*/, this._readRefSeqs(start, refSeqBytes * 2, opts)];
219
308
  }
220
- return [4 /*yield*/, this.bam.read(Buffer.alloc(refSeqBytes + blockLen), 0, refSeqBytes, 0, opts)];
309
+ size = refSeqBytes + blockLen;
310
+ return [4 /*yield*/, this.bam.read(Buffer.alloc(size), 0, refSeqBytes, 0, opts)];
221
311
  case 1:
222
- res = _a.sent();
223
- bytesRead = res.bytesRead;
224
- buffer = res.buffer;
312
+ _a = _b.sent(), bytesRead = _a.bytesRead, buffer = _a.buffer;
225
313
  if (!bytesRead) {
226
314
  throw new Error('Error reading refseqs from header');
227
315
  }
228
- if (bytesRead < refSeqBytes) {
229
- buffer = buffer.subarray(0, bytesRead);
230
- }
231
- else {
232
- buffer = buffer.subarray(0, refSeqBytes);
233
- }
234
- return [4 /*yield*/, (0, bgzf_filehandle_1.unzip)(buffer)];
316
+ return [4 /*yield*/, (0, bgzf_filehandle_1.unzip)(buffer.subarray(0, Math.min(bytesRead, refSeqBytes)))];
235
317
  case 2:
236
- uncba = _a.sent();
318
+ uncba = _b.sent();
237
319
  nRef = uncba.readInt32LE(start);
238
320
  p = start + 4;
239
321
  chrToIndex = {};
240
322
  indexToChr = [];
241
- i = 0;
242
- _a.label = 3;
243
- case 3:
244
- if (!(i < nRef)) return [3 /*break*/, 6];
245
- return [4 /*yield*/, (0, util_1.abortBreakPoint)(opts.signal)];
246
- case 4:
247
- _a.sent();
248
- lName = uncba.readInt32LE(p);
249
- refName = this.renameRefSeq(uncba.toString('utf8', p + 4, p + 4 + lName - 1));
250
- lRef = uncba.readInt32LE(p + lName + 4);
251
- chrToIndex[refName] = i;
252
- indexToChr.push({ refName: refName, length: lRef });
253
- p = p + 8 + lName;
254
- if (p > uncba.length) {
255
- console.warn("BAM header is very big. Re-fetching ".concat(refSeqBytes, " bytes."));
256
- return [2 /*return*/, this._readRefSeqs(start, refSeqBytes * 2, opts)];
323
+ for (i = 0; i < nRef; i += 1) {
324
+ lName = uncba.readInt32LE(p);
325
+ refName = this.renameRefSeq(uncba.toString('utf8', p + 4, p + 4 + lName - 1));
326
+ lRef = uncba.readInt32LE(p + lName + 4);
327
+ chrToIndex[refName] = i;
328
+ indexToChr.push({ refName: refName, length: lRef });
329
+ p = p + 8 + lName;
330
+ if (p > uncba.length) {
331
+ console.warn("BAM header is very big. Re-fetching ".concat(refSeqBytes, " bytes."));
332
+ return [2 /*return*/, this._readRefSeqs(start, refSeqBytes * 2, opts)];
333
+ }
257
334
  }
258
- _a.label = 5;
259
- case 5:
260
- i += 1;
261
- return [3 /*break*/, 3];
262
- case 6: return [2 /*return*/, { chrToIndex: chrToIndex, indexToChr: indexToChr }];
335
+ return [2 /*return*/, { chrToIndex: chrToIndex, indexToChr: indexToChr }];
263
336
  }
264
337
  });
265
338
  });
266
339
  };
267
340
  BamFile.prototype.getRecordsForRange = function (chr, min, max, opts) {
268
- var e_1, _a;
269
341
  if (opts === void 0) { opts = {
270
342
  viewAsPairs: false,
271
343
  pairAcrossChr: false,
272
344
  maxInsertSize: 200000,
273
345
  }; }
274
346
  return __awaiter(this, void 0, void 0, function () {
275
- var records, _b, _c, chunk, e_1_1;
276
- return __generator(this, function (_d) {
277
- switch (_d.label) {
347
+ var _a;
348
+ return __generator(this, function (_b) {
349
+ switch (_b.label) {
278
350
  case 0:
279
- records = [];
280
- _d.label = 1;
281
- case 1:
282
- _d.trys.push([1, 6, 7, 12]);
283
- _b = __asyncValues(this.streamRecordsForRange(chr, min, max, opts));
284
- _d.label = 2;
285
- case 2: return [4 /*yield*/, _b.next()];
286
- case 3:
287
- if (!(_c = _d.sent(), !_c.done)) return [3 /*break*/, 5];
288
- chunk = _c.value;
289
- records = records.concat(chunk);
290
- _d.label = 4;
291
- case 4: return [3 /*break*/, 2];
292
- case 5: return [3 /*break*/, 12];
293
- case 6:
294
- e_1_1 = _d.sent();
295
- e_1 = { error: e_1_1 };
296
- return [3 /*break*/, 12];
297
- case 7:
298
- _d.trys.push([7, , 10, 11]);
299
- if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 9];
300
- return [4 /*yield*/, _a.call(_b)];
301
- case 8:
302
- _d.sent();
303
- _d.label = 9;
304
- case 9: return [3 /*break*/, 11];
305
- case 10:
306
- if (e_1) throw e_1.error;
307
- return [7 /*endfinally*/];
308
- case 11: return [7 /*endfinally*/];
309
- case 12: return [2 /*return*/, records];
351
+ _a = flat;
352
+ return [4 /*yield*/, gen2array(this.streamRecordsForRange(chr, min, max, opts))];
353
+ case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
310
354
  }
311
355
  });
312
356
  });
@@ -364,152 +408,111 @@ var BamFile = /** @class */ (function () {
364
408
  };
365
409
  BamFile.prototype._fetchChunkFeatures = function (chunks, chrId, min, max, opts) {
366
410
  return __asyncGenerator(this, arguments, function _fetchChunkFeatures_1() {
367
- var _a, viewAsPairs, featPromises, done, i, _b, data, cpositions, dpositions, chunk, promise, i;
368
- return __generator(this, function (_c) {
369
- switch (_c.label) {
411
+ var _a, viewAsPairs, feats, done, i, c, records, recs, i_1, feature;
412
+ return __generator(this, function (_b) {
413
+ switch (_b.label) {
370
414
  case 0:
371
415
  _a = opts.viewAsPairs, viewAsPairs = _a === void 0 ? false : _a;
372
- featPromises = [];
416
+ feats = [];
373
417
  done = false;
374
418
  i = 0;
375
- _c.label = 1;
419
+ _b.label = 1;
376
420
  case 1:
377
- if (!(i < chunks.length)) return [3 /*break*/, 5];
378
- return [4 /*yield*/, __await(this._readChunk({
379
- chunk: chunks[i],
421
+ if (!(i < chunks.length)) return [3 /*break*/, 6];
422
+ c = chunks[i];
423
+ return [4 /*yield*/, __await(this.featureCache.get(c.toString(), {
424
+ chunk: c,
380
425
  opts: opts,
381
- }))];
426
+ }, opts.signal))];
382
427
  case 2:
383
- _b = _c.sent(), data = _b.data, cpositions = _b.cpositions, dpositions = _b.dpositions, chunk = _b.chunk;
384
- promise = this.readBamFeatures(data, cpositions, dpositions, chunk).then(function (records) {
385
- var recs = [];
386
- for (var i_1 = 0; i_1 < records.length; i_1 += 1) {
387
- var feature = records[i_1];
388
- if (feature.seq_id() === chrId) {
389
- if (feature.get('start') >= max) {
390
- // past end of range, can stop iterating
391
- done = true;
392
- break;
393
- }
394
- else if (feature.get('end') >= min) {
395
- // must be in range
396
- recs.push(feature);
397
- }
428
+ records = (_b.sent());
429
+ recs = [];
430
+ for (i_1 = 0; i_1 < records.length; i_1 += 1) {
431
+ feature = records[i_1];
432
+ if (feature.seq_id() === chrId) {
433
+ if (feature.get('start') >= max) {
434
+ // past end of range, can stop iterating
435
+ done = true;
436
+ break;
437
+ }
438
+ else if (feature.get('end') >= min) {
439
+ // must be in range
440
+ recs.push(feature);
398
441
  }
399
442
  }
400
- return recs;
401
- });
402
- featPromises.push(promise);
403
- return [4 /*yield*/, __await(promise)];
404
- case 3:
405
- _c.sent();
406
- if (done) {
407
- return [3 /*break*/, 5];
408
443
  }
409
- _c.label = 4;
444
+ feats.push(recs);
445
+ return [4 /*yield*/, __await(recs)];
446
+ case 3: return [4 /*yield*/, _b.sent()];
410
447
  case 4:
448
+ _b.sent();
449
+ if (done) {
450
+ return [3 /*break*/, 6];
451
+ }
452
+ _b.label = 5;
453
+ case 5:
411
454
  i++;
412
455
  return [3 /*break*/, 1];
413
- case 5:
414
- (0, util_1.checkAbortSignal)(opts.signal);
415
- i = 0;
416
- _c.label = 6;
417
456
  case 6:
418
- if (!(i < featPromises.length)) return [3 /*break*/, 10];
419
- return [4 /*yield*/, __await(featPromises[i])];
420
- case 7: return [4 /*yield*/, _c.sent()];
421
- case 8:
422
- _c.sent();
423
- _c.label = 9;
424
- case 9:
425
- i++;
426
- return [3 /*break*/, 6];
427
- case 10:
428
457
  (0, util_1.checkAbortSignal)(opts.signal);
429
- if (!viewAsPairs) return [3 /*break*/, 13];
430
- return [4 /*yield*/, __await(this.fetchPairs(chrId, featPromises, opts))];
431
- case 11: return [4 /*yield*/, _c.sent()];
432
- case 12:
433
- _c.sent();
434
- _c.label = 13;
435
- case 13: return [2 /*return*/];
458
+ if (!viewAsPairs) return [3 /*break*/, 9];
459
+ return [4 /*yield*/, __await(this.fetchPairs(chrId, feats, opts))];
460
+ case 7: return [4 /*yield*/, _b.sent()];
461
+ case 8:
462
+ _b.sent();
463
+ _b.label = 9;
464
+ case 9: return [2 /*return*/];
436
465
  }
437
466
  });
438
467
  });
439
468
  };
440
- BamFile.prototype.fetchPairs = function (chrId, featPromises, opts) {
469
+ BamFile.prototype.fetchPairs = function (chrId, feats, opts) {
441
470
  return __awaiter(this, void 0, void 0, function () {
442
- var _a, pairAcrossChr, _b, maxInsertSize, unmatedPairs, readIds, matePromises, mateBlocks, mateChunks, i, mateTotalSize, mateFeatPromises, newMateFeats, featuresRet, newMates;
471
+ var _a, pairAcrossChr, _b, maxInsertSize, unmatedPairs, readIds, matePromises, mateChunks, _c, mateTotalSize, mateFeatPromises, _d;
443
472
  var _this = this;
444
- return __generator(this, function (_c) {
445
- switch (_c.label) {
473
+ return __generator(this, function (_e) {
474
+ switch (_e.label) {
446
475
  case 0:
447
476
  _a = opts.pairAcrossChr, pairAcrossChr = _a === void 0 ? false : _a, _b = opts.maxInsertSize, maxInsertSize = _b === void 0 ? 200000 : _b;
448
477
  unmatedPairs = {};
449
478
  readIds = {};
450
- return [4 /*yield*/, Promise.all(featPromises.map(function (f) { return __awaiter(_this, void 0, void 0, function () {
451
- var ret, readNames, i, name_1, id;
452
- return __generator(this, function (_a) {
453
- switch (_a.label) {
454
- case 0: return [4 /*yield*/, f];
455
- case 1:
456
- ret = _a.sent();
457
- readNames = {};
458
- for (i = 0; i < ret.length; i++) {
459
- name_1 = ret[i].name();
460
- id = ret[i].id();
461
- if (!readNames[name_1]) {
462
- readNames[name_1] = 0;
463
- }
464
- readNames[name_1]++;
465
- readIds[id] = 1;
466
- }
467
- (0, object_entries_ponyfill_1.default)(readNames).forEach(function (_a) {
468
- var k = _a[0], v = _a[1];
469
- if (v === 1) {
470
- unmatedPairs[k] = true;
471
- }
472
- });
473
- return [2 /*return*/];
474
- }
475
- });
476
- }); }))];
477
- case 1:
478
- _c.sent();
479
+ feats.map(function (ret) {
480
+ var readNames = {};
481
+ for (var i = 0; i < ret.length; i++) {
482
+ var name_1 = ret[i].name();
483
+ var id = ret[i].id();
484
+ if (!readNames[name_1]) {
485
+ readNames[name_1] = 0;
486
+ }
487
+ readNames[name_1]++;
488
+ readIds[id] = 1;
489
+ }
490
+ (0, object_entries_ponyfill_1.default)(readNames).forEach(function (_a) {
491
+ var k = _a[0], v = _a[1];
492
+ if (v === 1) {
493
+ unmatedPairs[k] = true;
494
+ }
495
+ });
496
+ });
479
497
  matePromises = [];
480
- return [4 /*yield*/, Promise.all(featPromises.map(function (f) { return __awaiter(_this, void 0, void 0, function () {
481
- var ret, i, name_2;
482
- return __generator(this, function (_a) {
483
- switch (_a.label) {
484
- case 0: return [4 /*yield*/, f];
485
- case 1:
486
- ret = _a.sent();
487
- for (i = 0; i < ret.length; i++) {
488
- name_2 = ret[i].name();
489
- if (unmatedPairs[name_2] &&
490
- (pairAcrossChr ||
491
- (ret[i]._next_refid() === chrId &&
492
- Math.abs(ret[i].get('start') - ret[i]._next_pos()) <
493
- maxInsertSize))) {
494
- matePromises.push(this.index.blocksForRange(ret[i]._next_refid(), ret[i]._next_pos(), ret[i]._next_pos() + 1, opts));
495
- }
496
- }
497
- return [2 /*return*/];
498
- }
499
- });
500
- }); }))];
501
- case 2:
502
- _c.sent();
498
+ feats.map(function (ret) {
499
+ for (var i = 0; i < ret.length; i++) {
500
+ var f = ret[i];
501
+ var name_2 = f.name();
502
+ var start = f.get('start');
503
+ var pnext = f._next_pos();
504
+ var rnext = f._next_refid();
505
+ if (unmatedPairs[name_2] &&
506
+ (pairAcrossChr ||
507
+ (rnext === chrId && Math.abs(start - pnext) < maxInsertSize))) {
508
+ matePromises.push(_this.index.blocksForRange(rnext, pnext, pnext + 1, opts));
509
+ }
510
+ }
511
+ });
512
+ _c = flat;
503
513
  return [4 /*yield*/, Promise.all(matePromises)];
504
- case 3:
505
- mateBlocks = _c.sent();
506
- mateChunks = [];
507
- for (i = 0; i < mateBlocks.length; i++) {
508
- mateChunks = mateChunks.concat(mateBlocks[i]);
509
- }
510
- // filter out duplicate chunks (the blocks are lists of chunks, blocks are
511
- // concatenated, then filter dup chunks)
512
- mateChunks = mateChunks
514
+ case 1:
515
+ mateChunks = _c.apply(void 0, [_e.sent()])
513
516
  .sort()
514
517
  .filter(function (item, pos, ary) { return !pos || item.toString() !== ary[pos - 1].toString(); });
515
518
  mateTotalSize = mateChunks
@@ -542,17 +545,9 @@ var BamFile = /** @class */ (function () {
542
545
  }
543
546
  });
544
547
  }); });
548
+ _d = flat;
545
549
  return [4 /*yield*/, Promise.all(mateFeatPromises)];
546
- case 4:
547
- newMateFeats = _c.sent();
548
- featuresRet = [];
549
- if (newMateFeats.length) {
550
- newMates = newMateFeats.reduce(function (result, current) {
551
- return result.concat(current);
552
- });
553
- featuresRet = featuresRet.concat(newMates);
554
- }
555
- return [2 /*return*/, featuresRet];
550
+ case 2: return [2 /*return*/, _d.apply(void 0, [_e.sent()])];
556
551
  }
557
552
  });
558
553
  });
@@ -560,28 +555,17 @@ var BamFile = /** @class */ (function () {
560
555
  BamFile.prototype._readChunk = function (_a) {
561
556
  var chunk = _a.chunk, opts = _a.opts;
562
557
  return __awaiter(this, void 0, void 0, function () {
563
- var signal, bufsize, res, bytesRead, buffer, _b, data, cpositions, dpositions;
564
- return __generator(this, function (_c) {
565
- switch (_c.label) {
558
+ var size, _b, buffer, bytesRead, _c, data, cpositions, dpositions;
559
+ return __generator(this, function (_d) {
560
+ switch (_d.label) {
566
561
  case 0:
567
- signal = opts.signal;
568
- bufsize = chunk.fetchedSize();
569
- return [4 /*yield*/, this.bam.read(Buffer.alloc(bufsize), 0, bufsize, chunk.minv.blockPosition, opts)];
562
+ size = chunk.fetchedSize();
563
+ return [4 /*yield*/, this.bam.read(Buffer.alloc(size), 0, size, chunk.minv.blockPosition, opts)];
570
564
  case 1:
571
- res = _c.sent();
572
- bytesRead = res.bytesRead;
573
- buffer = res.buffer;
574
- (0, util_1.checkAbortSignal)(signal);
575
- if (bytesRead < bufsize) {
576
- buffer = buffer.subarray(0, bytesRead);
577
- }
578
- else {
579
- buffer = buffer.subarray(0, bufsize);
580
- }
581
- return [4 /*yield*/, (0, bgzf_filehandle_1.unzipChunkSlice)(buffer, chunk)];
565
+ _b = _d.sent(), buffer = _b.buffer, bytesRead = _b.bytesRead;
566
+ return [4 /*yield*/, (0, bgzf_filehandle_1.unzipChunkSlice)(buffer.subarray(0, Math.min(bytesRead, size)), chunk)];
582
567
  case 2:
583
- _b = _c.sent(), data = _b.buffer, cpositions = _b.cpositions, dpositions = _b.dpositions;
584
- (0, util_1.checkAbortSignal)(signal);
568
+ _c = _d.sent(), data = _c.buffer, cpositions = _c.cpositions, dpositions = _c.dpositions;
585
569
  return [2 /*return*/, { data: data, cpositions: cpositions, dpositions: dpositions, chunk: chunk }];
586
570
  }
587
571
  });
@@ -635,7 +619,8 @@ var BamFile = /** @class */ (function () {
635
619
  (blockStart - dpositions[pos]) +
636
620
  chunk.minv.dataPosition +
637
621
  1
638
- : buffer_crc32_1.default.signed(ba.subarray(blockStart, blockEnd)),
622
+ : // must be slice, not subarray for buffer polyfill on web
623
+ buffer_crc32_1.default.signed(ba.slice(blockStart, blockEnd)),
639
624
  });
640
625
  sink.push(feature);
641
626
  if (!(this.yieldThreadTime && +Date.now() - last > this.yieldThreadTime)) return [3 /*break*/, 3];