@gmod/bam 1.1.14 → 1.1.17
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 +14 -1
- package/dist/bamFile.d.ts +3 -2
- package/dist/bamFile.js +209 -224
- package/dist/bamFile.js.map +1 -1
- package/dist/csi.d.ts +2 -2
- package/dist/csi.js +3 -6
- package/dist/csi.js.map +1 -1
- package/dist/record.js +8 -5
- package/dist/record.js.map +1 -1
- package/esm/bamFile.d.ts +3 -2
- package/esm/bamFile.js +76 -87
- package/esm/bamFile.js.map +1 -1
- package/esm/csi.d.ts +2 -2
- package/esm/csi.js +3 -6
- package/esm/csi.js.map +1 -1
- package/esm/record.js +8 -5
- package/esm/record.js.map +1 -1
- package/package.json +22 -18
- package/src/bai.ts +227 -0
- package/src/bamFile.ts +546 -0
- package/src/chunk.ts +52 -0
- package/src/constants.ts +26 -0
- package/src/csi.ts +243 -0
- package/src/declare.d.ts +2 -0
- package/src/errors.ts +22 -0
- package/src/htsget.ts +138 -0
- package/src/index.ts +7 -0
- package/src/indexFile.ts +63 -0
- package/src/record.ts +610 -0
- package/src/sam.ts +15 -0
- package/src/util.ts +116 -0
- package/src/virtualOffset.ts +47 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
## [1.1.
|
|
1
|
+
## [1.1.17](https://github.com/GMOD/bam-js/compare/v1.1.16...v1.1.17) (2022-07-18)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
- Bump devDeps and generic-filehandle to 3.0.0
|
|
2
6
|
|
|
7
|
+
## [1.1.16](https://github.com/GMOD/bam-js/compare/v1.1.15...v1.1.16) (2022-03-30)
|
|
3
8
|
|
|
9
|
+
- Add src directory for better source maps
|
|
10
|
+
|
|
11
|
+
## [1.1.15](https://github.com/GMOD/bam-js/compare/v1.1.14...v1.1.15) (2022-03-18)
|
|
12
|
+
|
|
13
|
+
- Fix for htsget failing with message 'input must be buffer, number, or string, received object'
|
|
14
|
+
- Speed improvement by caching chunks of features
|
|
15
|
+
|
|
16
|
+
## [1.1.14](https://github.com/GMOD/bam-js/compare/v1.1.13...v1.1.14) (2022-03-14)
|
|
4
17
|
|
|
5
18
|
- Fix seq function for corner case
|
|
6
19
|
|
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,
|
|
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
|
|
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
|
|
214
|
-
return __generator(this, function (
|
|
215
|
-
switch (
|
|
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
|
-
|
|
309
|
+
size = refSeqBytes + blockLen;
|
|
310
|
+
return [4 /*yield*/, this.bam.read(Buffer.alloc(size), 0, refSeqBytes, 0, opts)];
|
|
221
311
|
case 1:
|
|
222
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
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
|
|
276
|
-
return __generator(this, function (
|
|
277
|
-
switch (
|
|
347
|
+
var _a;
|
|
348
|
+
return __generator(this, function (_b) {
|
|
349
|
+
switch (_b.label) {
|
|
278
350
|
case 0:
|
|
279
|
-
|
|
280
|
-
|
|
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,
|
|
368
|
-
return __generator(this, function (
|
|
369
|
-
switch (
|
|
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
|
-
|
|
416
|
+
feats = [];
|
|
373
417
|
done = false;
|
|
374
418
|
i = 0;
|
|
375
|
-
|
|
419
|
+
_b.label = 1;
|
|
376
420
|
case 1:
|
|
377
|
-
if (!(i < chunks.length)) return [3 /*break*/,
|
|
378
|
-
|
|
379
|
-
|
|
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
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (feature.
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
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
|
-
|
|
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*/,
|
|
430
|
-
return [4 /*yield*/, __await(this.fetchPairs(chrId,
|
|
431
|
-
case
|
|
432
|
-
case
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
case
|
|
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,
|
|
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,
|
|
471
|
+
var _a, pairAcrossChr, _b, maxInsertSize, unmatedPairs, readIds, matePromises, mateChunks, _c, mateTotalSize, mateFeatPromises, _d;
|
|
443
472
|
var _this = this;
|
|
444
|
-
return __generator(this, function (
|
|
445
|
-
switch (
|
|
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
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
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
|
|
505
|
-
|
|
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
|
|
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
|
|
564
|
-
return __generator(this, function (
|
|
565
|
-
switch (
|
|
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
|
-
|
|
568
|
-
|
|
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
|
-
|
|
572
|
-
|
|
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
|
-
|
|
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
|
-
:
|
|
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];
|