@genome-spy/app 0.56.1 → 0.58.0

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.
@@ -1,1370 +0,0 @@
1
- import { L as V, u as k, a as Q } from "./long-BXK3UEks.js";
2
- import { b as I } from "./index-B6haPltu.js";
3
- import { L as A } from "./__vite-browser-external-C--ziKoh.js";
4
- import { R } from "./remoteFile-DuIsnepi.js";
5
- import { _ as Y, L as W } from "./index-BhtH2nrr.js";
6
- class D {
7
- constructor(e, t) {
8
- this.blockPosition = e, this.dataPosition = t;
9
- }
10
- toString() {
11
- return `${this.blockPosition}:${this.dataPosition}`;
12
- }
13
- compareTo(e) {
14
- return this.blockPosition - e.blockPosition || this.dataPosition - e.dataPosition;
15
- }
16
- static min(...e) {
17
- let t, n = 0;
18
- for (; !t; n += 1)
19
- t = e[n];
20
- for (; n < e.length; n += 1)
21
- t.compareTo(e[n]) > 0 && (t = e[n]);
22
- return t;
23
- }
24
- }
25
- function x(o, e = 0, t = !1) {
26
- if (t)
27
- throw new Error("big-endian virtual file offsets not implemented");
28
- return new D(o[e + 7] * 1099511627776 + o[e + 6] * 4294967296 + o[e + 5] * 16777216 + o[e + 4] * 65536 + o[e + 3] * 256 + o[e + 2], o[e + 1] << 8 | o[e]);
29
- }
30
- class $ {
31
- constructor(e, t, n, r) {
32
- this.minv = e, this.maxv = t, this.bin = n, this._fetchedSize = r;
33
- }
34
- toUniqueString() {
35
- return `${this.minv.toString()}..${this.maxv.toString()} (bin ${this.bin}, fetchedSize ${this.fetchedSize()})`;
36
- }
37
- toString() {
38
- return this.toUniqueString();
39
- }
40
- compareTo(e) {
41
- return this.minv.compareTo(e.minv) || this.maxv.compareTo(e.maxv) || this.bin - e.bin;
42
- }
43
- fetchedSize() {
44
- return this._fetchedSize !== void 0 ? this._fetchedSize : this.maxv.blockPosition + 65536 - this.minv.blockPosition;
45
- }
46
- }
47
- function X(o) {
48
- return new Promise((e) => setTimeout(e, o));
49
- }
50
- function J(o) {
51
- if (o.greaterThan(Number.MAX_SAFE_INTEGER) || o.lessThan(Number.MIN_SAFE_INTEGER))
52
- throw new Error("integer overflow");
53
- return o.toNumber();
54
- }
55
- function K(o) {
56
- if (o && o.aborted)
57
- if (typeof DOMException > "u") {
58
- const e = new Error("aborted");
59
- throw e.code = "ERR_ABORTED", e;
60
- } else
61
- throw new DOMException("aborted", "AbortError");
62
- }
63
- function Z(o, e) {
64
- return e.minv.blockPosition - o.maxv.blockPosition < 65e3 && e.maxv.blockPosition - o.minv.blockPosition < 5e6;
65
- }
66
- function ee(o = {}) {
67
- return "aborted" in o ? { signal: o } : o;
68
- }
69
- function U(o, e) {
70
- const t = [];
71
- let n;
72
- if (o.length === 0)
73
- return o;
74
- o.sort((r, s) => {
75
- const a = r.minv.blockPosition - s.minv.blockPosition;
76
- return a === 0 ? r.minv.dataPosition - s.minv.dataPosition : a;
77
- });
78
- for (const r of o)
79
- (!e || r.maxv.compareTo(e) > 0) && (n === void 0 ? (t.push(r), n = r) : Z(n, r) ? r.maxv.compareTo(n.maxv) > 0 && (n.maxv = r.maxv) : (t.push(r), n = r));
80
- return t;
81
- }
82
- function z(o, e) {
83
- return { lineCount: J(V.fromBytesLE(Array.prototype.slice.call(o, e, e + 8), !0)) };
84
- }
85
- function L(o, e) {
86
- return o ? o.compareTo(e) > 0 ? e : o : e;
87
- }
88
- function te(o, e = (t) => t) {
89
- let t = 0, n = 0;
90
- const r = [], s = {};
91
- for (let a = 0; a < o.length; a += 1)
92
- if (!o[a]) {
93
- if (n < a) {
94
- let h = o.toString("utf8", n, a);
95
- h = e(h), r[t] = h, s[h] = t;
96
- }
97
- n = a + 1, t += 1;
98
- }
99
- return { refNameToId: s, refIdToName: r };
100
- }
101
- class O {
102
- /**
103
- * @param {filehandle} filehandle
104
- * @param {function} [renameRefSeqs]
105
- */
106
- constructor({ filehandle: e, renameRefSeq: t = (n) => n }) {
107
- this.filehandle = e, this.renameRefSeq = t;
108
- }
109
- }
110
- const ne = 21578050;
111
- function re(o, e) {
112
- return o - o % e;
113
- }
114
- function se(o, e) {
115
- return o - o % e + e;
116
- }
117
- function ie(o, e) {
118
- return e -= 1, [
119
- [0, 0],
120
- [1 + (o >> 26), 1 + (e >> 26)],
121
- [9 + (o >> 23), 9 + (e >> 23)],
122
- [73 + (o >> 20), 73 + (e >> 20)],
123
- [585 + (o >> 17), 585 + (e >> 17)],
124
- [4681 + (o >> 14), 4681 + (e >> 14)]
125
- ];
126
- }
127
- class v extends O {
128
- async lineCount(e, t) {
129
- var n, r;
130
- return ((r = (n = (await this.parse(t)).indices[e]) === null || n === void 0 ? void 0 : n.stats) === null || r === void 0 ? void 0 : r.lineCount) || 0;
131
- }
132
- // fetch and parse the index
133
- async _parse(e) {
134
- const t = await this.filehandle.readFile(e);
135
- if (t.readUInt32LE(0) !== ne)
136
- throw new Error("Not a BAI file");
137
- const n = t.readInt32LE(4), s = ((1 << (5 + 1) * 3) - 1) / 7;
138
- let a = 8, h;
139
- const c = new Array(n);
140
- for (let i = 0; i < n; i++) {
141
- const l = t.readInt32LE(a);
142
- let u;
143
- a += 4;
144
- const f = {};
145
- for (let g = 0; g < l; g += 1) {
146
- const m = t.readUInt32LE(a);
147
- if (a += 4, m === s + 1)
148
- a += 4, u = z(t, a + 16), a += 32;
149
- else {
150
- if (m > s + 1)
151
- throw new Error("bai index contains too many bins, please use CSI");
152
- {
153
- const p = t.readInt32LE(a);
154
- a += 4;
155
- const w = new Array(p);
156
- for (let E = 0; E < p; E++) {
157
- const y = x(t, a);
158
- a += 8;
159
- const S = x(t, a);
160
- a += 8, h = L(h, y), w[E] = new $(y, S, m);
161
- }
162
- f[m] = w;
163
- }
164
- }
165
- }
166
- const d = t.readInt32LE(a);
167
- a += 4;
168
- const _ = new Array(d);
169
- for (let g = 0; g < d; g++) {
170
- const m = x(t, a);
171
- a += 8, h = L(h, m), _[g] = m;
172
- }
173
- c[i] = { binIndex: f, linearIndex: _, stats: u };
174
- }
175
- return {
176
- bai: !0,
177
- firstDataLine: h,
178
- maxBlockSize: 65536,
179
- indices: c,
180
- refCount: n
181
- };
182
- }
183
- async indexCov(e, t, n, r = {}) {
184
- const a = t !== void 0, c = (await this.parse(r)).indices[e];
185
- if (!c)
186
- return [];
187
- const { linearIndex: i = [], stats: l } = c;
188
- if (i.length === 0)
189
- return [];
190
- const u = n === void 0 ? (i.length - 1) * 16384 : se(n, 16384), f = t === void 0 ? 0 : re(t, 16384), d = a ? new Array((u - f) / 16384) : new Array(i.length - 1), _ = i[i.length - 1].blockPosition;
191
- if (u > (i.length - 1) * 16384)
192
- throw new Error("query outside of range of linear index");
193
- let g = i[f / 16384].blockPosition;
194
- for (let m = f / 16384, p = 0; m < u / 16384; m++, p++)
195
- d[p] = {
196
- score: i[m + 1].blockPosition - g,
197
- start: m * 16384,
198
- end: m * 16384 + 16384
199
- }, g = i[m + 1].blockPosition;
200
- return d.map((m) => ({
201
- ...m,
202
- score: m.score * ((l == null ? void 0 : l.lineCount) || 0) / _
203
- }));
204
- }
205
- async blocksForRange(e, t, n, r = {}) {
206
- t < 0 && (t = 0);
207
- const s = await this.parse(r);
208
- if (!s)
209
- return [];
210
- const a = s.indices[e];
211
- if (!a)
212
- return [];
213
- const h = ie(t, n), c = [];
214
- for (const [d, _] of h)
215
- for (let g = d; g <= _; g++)
216
- if (a.binIndex[g]) {
217
- const m = a.binIndex[g];
218
- for (const p of m)
219
- c.push(p);
220
- }
221
- const i = a.linearIndex.length;
222
- let l;
223
- const u = Math.min(t >> 14, i - 1), f = Math.min(n >> 14, i - 1);
224
- for (let d = u; d <= f; ++d) {
225
- const _ = a.linearIndex[d];
226
- _ && (!l || _.compareTo(l) < 0) && (l = _);
227
- }
228
- return U(c, l);
229
- }
230
- async parse(e = {}) {
231
- return this.setupP || (this.setupP = this._parse(e).catch((t) => {
232
- throw this.setupP = void 0, t;
233
- })), this.setupP;
234
- }
235
- async hasRefSeq(e, t = {}) {
236
- var n;
237
- return !!(!((n = (await this.parse(t)).indices[e]) === null || n === void 0) && n.binIndex);
238
- }
239
- }
240
- function ae(o) {
241
- return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
242
- }
243
- const oe = new Int32Array([
244
- 0,
245
- 1996959894,
246
- 3993919788,
247
- 2567524794,
248
- 124634137,
249
- 1886057615,
250
- 3915621685,
251
- 2657392035,
252
- 249268274,
253
- 2044508324,
254
- 3772115230,
255
- 2547177864,
256
- 162941995,
257
- 2125561021,
258
- 3887607047,
259
- 2428444049,
260
- 498536548,
261
- 1789927666,
262
- 4089016648,
263
- 2227061214,
264
- 450548861,
265
- 1843258603,
266
- 4107580753,
267
- 2211677639,
268
- 325883990,
269
- 1684777152,
270
- 4251122042,
271
- 2321926636,
272
- 335633487,
273
- 1661365465,
274
- 4195302755,
275
- 2366115317,
276
- 997073096,
277
- 1281953886,
278
- 3579855332,
279
- 2724688242,
280
- 1006888145,
281
- 1258607687,
282
- 3524101629,
283
- 2768942443,
284
- 901097722,
285
- 1119000684,
286
- 3686517206,
287
- 2898065728,
288
- 853044451,
289
- 1172266101,
290
- 3705015759,
291
- 2882616665,
292
- 651767980,
293
- 1373503546,
294
- 3369554304,
295
- 3218104598,
296
- 565507253,
297
- 1454621731,
298
- 3485111705,
299
- 3099436303,
300
- 671266974,
301
- 1594198024,
302
- 3322730930,
303
- 2970347812,
304
- 795835527,
305
- 1483230225,
306
- 3244367275,
307
- 3060149565,
308
- 1994146192,
309
- 31158534,
310
- 2563907772,
311
- 4023717930,
312
- 1907459465,
313
- 112637215,
314
- 2680153253,
315
- 3904427059,
316
- 2013776290,
317
- 251722036,
318
- 2517215374,
319
- 3775830040,
320
- 2137656763,
321
- 141376813,
322
- 2439277719,
323
- 3865271297,
324
- 1802195444,
325
- 476864866,
326
- 2238001368,
327
- 4066508878,
328
- 1812370925,
329
- 453092731,
330
- 2181625025,
331
- 4111451223,
332
- 1706088902,
333
- 314042704,
334
- 2344532202,
335
- 4240017532,
336
- 1658658271,
337
- 366619977,
338
- 2362670323,
339
- 4224994405,
340
- 1303535960,
341
- 984961486,
342
- 2747007092,
343
- 3569037538,
344
- 1256170817,
345
- 1037604311,
346
- 2765210733,
347
- 3554079995,
348
- 1131014506,
349
- 879679996,
350
- 2909243462,
351
- 3663771856,
352
- 1141124467,
353
- 855842277,
354
- 2852801631,
355
- 3708648649,
356
- 1342533948,
357
- 654459306,
358
- 3188396048,
359
- 3373015174,
360
- 1466479909,
361
- 544179635,
362
- 3110523913,
363
- 3462522015,
364
- 1591671054,
365
- 702138776,
366
- 2966460450,
367
- 3352799412,
368
- 1504918807,
369
- 783551873,
370
- 3082640443,
371
- 3233442989,
372
- 3988292384,
373
- 2596254646,
374
- 62317068,
375
- 1957810842,
376
- 3939845945,
377
- 2647816111,
378
- 81470997,
379
- 1943803523,
380
- 3814918930,
381
- 2489596804,
382
- 225274430,
383
- 2053790376,
384
- 3826175755,
385
- 2466906013,
386
- 167816743,
387
- 2097651377,
388
- 4027552580,
389
- 2265490386,
390
- 503444072,
391
- 1762050814,
392
- 4150417245,
393
- 2154129355,
394
- 426522225,
395
- 1852507879,
396
- 4275313526,
397
- 2312317920,
398
- 282753626,
399
- 1742555852,
400
- 4189708143,
401
- 2394877945,
402
- 397917763,
403
- 1622183637,
404
- 3604390888,
405
- 2714866558,
406
- 953729732,
407
- 1340076626,
408
- 3518719985,
409
- 2797360999,
410
- 1068828381,
411
- 1219638859,
412
- 3624741850,
413
- 2936675148,
414
- 906185462,
415
- 1090812512,
416
- 3747672003,
417
- 2825379669,
418
- 829329135,
419
- 1181335161,
420
- 3412177804,
421
- 3160834842,
422
- 628085408,
423
- 1382605366,
424
- 3423369109,
425
- 3138078467,
426
- 570562233,
427
- 1426400815,
428
- 3317316542,
429
- 2998733608,
430
- 733239954,
431
- 1555261956,
432
- 3268935591,
433
- 3050360625,
434
- 752459403,
435
- 1541320221,
436
- 2607071920,
437
- 3965973030,
438
- 1969922972,
439
- 40735498,
440
- 2617837225,
441
- 3943577151,
442
- 1913087877,
443
- 83908371,
444
- 2512341634,
445
- 3803740692,
446
- 2075208622,
447
- 213261112,
448
- 2463272603,
449
- 3855990285,
450
- 2094854071,
451
- 198958881,
452
- 2262029012,
453
- 4057260610,
454
- 1759359992,
455
- 534414190,
456
- 2176718541,
457
- 4139329115,
458
- 1873836001,
459
- 414664567,
460
- 2282248934,
461
- 4279200368,
462
- 1711684554,
463
- 285281116,
464
- 2405801727,
465
- 4167216745,
466
- 1634467795,
467
- 376229701,
468
- 2685067896,
469
- 3608007406,
470
- 1308918612,
471
- 956543938,
472
- 2808555105,
473
- 3495958263,
474
- 1231636301,
475
- 1047427035,
476
- 2932959818,
477
- 3654703836,
478
- 1088359270,
479
- 936918e3,
480
- 2847714899,
481
- 3736837829,
482
- 1202900863,
483
- 817233897,
484
- 3183342108,
485
- 3401237130,
486
- 1404277552,
487
- 615818150,
488
- 3134207493,
489
- 3453421203,
490
- 1423857449,
491
- 601450431,
492
- 3009837614,
493
- 3294710456,
494
- 1567103746,
495
- 711928724,
496
- 3020668471,
497
- 3272380065,
498
- 1510334235,
499
- 755167117
500
- ]);
501
- function H(o) {
502
- if (Buffer.isBuffer(o))
503
- return o;
504
- if (typeof o == "number")
505
- return Buffer.alloc(o);
506
- if (typeof o == "string")
507
- return Buffer.from(o);
508
- throw new Error("input must be buffer, number, or string, received " + typeof o);
509
- }
510
- function ce(o) {
511
- const e = H(4);
512
- return e.writeInt32BE(o, 0), e;
513
- }
514
- function T(o, e) {
515
- o = H(o), Buffer.isBuffer(e) && (e = e.readUInt32BE(0));
516
- let t = ~~e ^ -1;
517
- for (var n = 0; n < o.length; n++)
518
- t = oe[(t ^ o[n]) & 255] ^ t >>> 8;
519
- return t ^ -1;
520
- }
521
- function M() {
522
- return ce(T.apply(null, arguments));
523
- }
524
- M.signed = function() {
525
- return T.apply(null, arguments);
526
- };
527
- M.unsigned = function() {
528
- return T.apply(null, arguments) >>> 0;
529
- };
530
- var he = M;
531
- const de = /* @__PURE__ */ ae(he), fe = 21582659, le = 38359875;
532
- function ue(o, e) {
533
- return o * 2 ** e;
534
- }
535
- function B(o, e) {
536
- return Math.floor(o / 2 ** e);
537
- }
538
- class P extends O {
539
- constructor() {
540
- super(...arguments), this.maxBinNumber = 0, this.depth = 0, this.minShift = 0;
541
- }
542
- async lineCount(e, t) {
543
- var n, r;
544
- return ((r = (n = (await this.parse(t)).indices[e]) === null || n === void 0 ? void 0 : n.stats) === null || r === void 0 ? void 0 : r.lineCount) || 0;
545
- }
546
- async indexCov() {
547
- return [];
548
- }
549
- parseAuxData(e, t) {
550
- const n = e.readInt32LE(t), r = n & 65536 ? "zero-based-half-open" : "1-based-closed", s = { 0: "generic", 1: "SAM", 2: "VCF" }[n & 15];
551
- if (!s)
552
- throw new Error(`invalid Tabix preset format flags ${n}`);
553
- const a = {
554
- ref: e.readInt32LE(t + 4),
555
- start: e.readInt32LE(t + 8),
556
- end: e.readInt32LE(t + 12)
557
- }, h = e.readInt32LE(t + 16), c = h ? String.fromCharCode(h) : "", i = e.readInt32LE(t + 20), l = e.readInt32LE(t + 24);
558
- return {
559
- columnNumbers: a,
560
- coordinateType: r,
561
- metaValue: h,
562
- metaChar: c,
563
- skipLines: i,
564
- format: s,
565
- formatFlags: n,
566
- ...te(e.subarray(t + 28, t + 28 + l), this.renameRefSeq)
567
- };
568
- }
569
- // fetch and parse the index
570
- async _parse(e) {
571
- const t = await this.filehandle.readFile(e), n = await k(t);
572
- let r;
573
- if (n.readUInt32LE(0) === fe)
574
- r = 1;
575
- else if (n.readUInt32LE(0) === le)
576
- r = 2;
577
- else
578
- throw new Error("Not a CSI file");
579
- this.minShift = n.readInt32LE(4), this.depth = n.readInt32LE(8), this.maxBinNumber = ((1 << (this.depth + 1) * 3) - 1) / 7;
580
- const s = n.readInt32LE(12), a = s >= 30 ? this.parseAuxData(n, 16) : void 0, h = n.readInt32LE(16 + s);
581
- let c = 16 + s + 4, i;
582
- const l = new Array(h);
583
- for (let u = 0; u < h; u++) {
584
- const f = n.readInt32LE(c);
585
- c += 4;
586
- const d = {};
587
- let _;
588
- for (let g = 0; g < f; g++) {
589
- const m = n.readUInt32LE(c);
590
- if (c += 4, m > this.maxBinNumber)
591
- _ = z(n, c + 28), c += 44;
592
- else {
593
- i = L(i, x(n, c)), c += 8;
594
- const p = n.readInt32LE(c);
595
- c += 4;
596
- const w = new Array(p);
597
- for (let E = 0; E < p; E += 1) {
598
- const y = x(n, c);
599
- c += 8;
600
- const S = x(n, c);
601
- c += 8, i = L(i, y), w[E] = new $(y, S, m);
602
- }
603
- d[m] = w;
604
- }
605
- }
606
- l[u] = { binIndex: d, stats: _ };
607
- }
608
- return {
609
- csiVersion: r,
610
- firstDataLine: i,
611
- indices: l,
612
- refCount: h,
613
- csi: !0,
614
- maxBlockSize: 65536,
615
- ...a
616
- };
617
- }
618
- async blocksForRange(e, t, n, r = {}) {
619
- t < 0 && (t = 0);
620
- const s = await this.parse(r), a = s == null ? void 0 : s.indices[e];
621
- if (!a)
622
- return [];
623
- const h = this.reg2bins(t, n);
624
- if (h.length === 0)
625
- return [];
626
- const c = [];
627
- for (const [i, l] of h)
628
- for (let u = i; u <= l; u++)
629
- if (a.binIndex[u]) {
630
- const f = a.binIndex[u];
631
- for (const d of f)
632
- c.push(d);
633
- }
634
- return U(c, new D(0, 0));
635
- }
636
- /**
637
- * calculate the list of bins that may overlap with region [beg,end)
638
- * (zero-based half-open)
639
- */
640
- reg2bins(e, t) {
641
- e -= 1, e < 1 && (e = 1), t > 2 ** 50 && (t = 2 ** 34), t -= 1;
642
- let n = 0, r = 0, s = this.minShift + this.depth * 3;
643
- const a = [];
644
- for (; n <= this.depth; s -= 3, r += ue(1, n * 3), n += 1) {
645
- const h = r + B(e, s), c = r + B(t, s);
646
- if (c - h + a.length > this.maxBinNumber)
647
- throw new Error(`query ${e}-${t} is too large for current binning scheme (shift ${this.minShift}, depth ${this.depth}), try a smaller query or a coarser index binning scheme`);
648
- a.push([h, c]);
649
- }
650
- return a;
651
- }
652
- async parse(e = {}) {
653
- return this.setupP || (this.setupP = this._parse(e).catch((t) => {
654
- throw this.setupP = void 0, t;
655
- })), this.setupP;
656
- }
657
- async hasRefSeq(e, t = {}) {
658
- var n;
659
- return !!(!((n = (await this.parse(t)).indices[e]) === null || n === void 0) && n.binIndex);
660
- }
661
- }
662
- const b = {
663
- // the read is paired in sequencing, no matter whether it is mapped in a pair
664
- BAM_FPAIRED: 1,
665
- // the read is mapped in a proper pair
666
- BAM_FPROPER_PAIR: 2,
667
- // the read itself is unmapped; conflictive with BAM_FPROPER_PAIR
668
- BAM_FUNMAP: 4,
669
- // the mate is unmapped
670
- BAM_FMUNMAP: 8,
671
- // the read is mapped to the reverse strand
672
- BAM_FREVERSE: 16,
673
- // the mate is mapped to the reverse strand
674
- BAM_FMREVERSE: 32,
675
- // this is read1
676
- BAM_FREAD1: 64,
677
- // this is read2
678
- BAM_FREAD2: 128,
679
- // not primary alignment
680
- BAM_FSECONDARY: 256,
681
- // QC failure
682
- BAM_FQCFAIL: 512,
683
- // optical or PCR duplicate
684
- BAM_FDUP: 1024,
685
- // supplementary alignment
686
- BAM_FSUPPLEMENTARY: 2048
687
- }, F = "=ACMGRSVTWYHKDBN".split(""), C = "MIDNSHP=X???????".split("");
688
- class me {
689
- constructor(e) {
690
- this.data = {}, this._tagList = [], this._allTagsParsed = !1;
691
- const { bytes: t, fileOffset: n } = e, { byteArray: r, start: s } = t;
692
- this.data = {}, this.bytes = t, this._id = n, this._refID = r.readInt32LE(s + 4), this.data.start = r.readInt32LE(s + 8), this.flags = (r.readInt32LE(s + 16) & 4294901760) >> 16;
693
- }
694
- get(e) {
695
- return this[e] ? this.data[e] ? this.data[e] : (this.data[e] = this[e](), this.data[e]) : this._get(e.toLowerCase());
696
- }
697
- end() {
698
- return this.get("start") + this.get("length_on_ref");
699
- }
700
- seq_id() {
701
- return this._refID;
702
- }
703
- // same as get(), except requires lower-case arguments. used
704
- // internally to save lots of calls to field.toLowerCase()
705
- _get(e) {
706
- return e in this.data ? this.data[e] : (this.data[e] = this._parseTag(e), this.data[e]);
707
- }
708
- _tags() {
709
- this._parseAllTags();
710
- let e = ["seq"];
711
- this.isSegmentUnmapped() || e.push("start", "end", "strand", "score", "qual", "MQ", "CIGAR", "length_on_ref", "template_length"), this.isPaired() && e.push("next_segment_position", "pair_orientation"), e = e.concat(this._tagList || []);
712
- for (const n of Object.keys(this.data))
713
- !n.startsWith("_") && n !== "next_seq_id" && e.push(n);
714
- const t = {};
715
- return e.filter((n) => {
716
- if (n in this.data && this.data[n] === void 0 || n === "CG" || n === "cg")
717
- return !1;
718
- const r = n.toLowerCase(), s = t[r];
719
- return t[r] = !0, !s;
720
- });
721
- }
722
- parent() {
723
- }
724
- children() {
725
- return this.get("subfeatures");
726
- }
727
- id() {
728
- return this._id;
729
- }
730
- // special parsers
731
- /**
732
- * Mapping quality score.
733
- */
734
- mq() {
735
- const e = (this.get("_bin_mq_nl") & 65280) >> 8;
736
- return e === 255 ? void 0 : e;
737
- }
738
- score() {
739
- return this.get("mq");
740
- }
741
- qual() {
742
- var e;
743
- return (e = this.qualRaw()) === null || e === void 0 ? void 0 : e.join(" ");
744
- }
745
- qualRaw() {
746
- if (this.isSegmentUnmapped())
747
- return;
748
- const { start: e, byteArray: t } = this.bytes, n = e + 36 + this.get("_l_read_name") + this.get("_n_cigar_op") * 4 + this.get("_seq_bytes"), r = this.get("seq_length");
749
- return t.subarray(n, n + r);
750
- }
751
- strand() {
752
- return this.isReverseComplemented() ? -1 : 1;
753
- }
754
- multi_segment_next_segment_strand() {
755
- if (!this.isMateUnmapped())
756
- return this.isMateReverseComplemented() ? -1 : 1;
757
- }
758
- name() {
759
- return this.get("_read_name");
760
- }
761
- _read_name() {
762
- const e = this.get("_l_read_name"), { byteArray: t, start: n } = this.bytes;
763
- return t.toString("ascii", n + 36, n + 36 + e - 1);
764
- }
765
- /**
766
- * Get the value of a tag, parsing the tags as far as necessary.
767
- * Only called if we have not already parsed that field.
768
- */
769
- _parseTag(e) {
770
- if (this._allTagsParsed)
771
- return;
772
- const { byteArray: t, start: n } = this.bytes;
773
- let r = this._tagOffset || n + 36 + this.get("_l_read_name") + this.get("_n_cigar_op") * 4 + this.get("_seq_bytes") + this.get("seq_length");
774
- const s = this.bytes.end;
775
- let a;
776
- for (; r < s && a !== e; ) {
777
- const h = String.fromCharCode(t[r], t[r + 1]);
778
- a = h.toLowerCase();
779
- const c = String.fromCharCode(t[r + 2]);
780
- r += 3;
781
- let i;
782
- switch (c) {
783
- case "A": {
784
- i = String.fromCharCode(t[r]), r += 1;
785
- break;
786
- }
787
- case "i": {
788
- i = t.readInt32LE(r), r += 4;
789
- break;
790
- }
791
- case "I": {
792
- i = t.readUInt32LE(r), r += 4;
793
- break;
794
- }
795
- case "c": {
796
- i = t.readInt8(r), r += 1;
797
- break;
798
- }
799
- case "C": {
800
- i = t.readUInt8(r), r += 1;
801
- break;
802
- }
803
- case "s": {
804
- i = t.readInt16LE(r), r += 2;
805
- break;
806
- }
807
- case "S": {
808
- i = t.readUInt16LE(r), r += 2;
809
- break;
810
- }
811
- case "f": {
812
- i = t.readFloatLE(r), r += 4;
813
- break;
814
- }
815
- case "Z":
816
- case "H": {
817
- for (i = ""; r <= s; ) {
818
- const l = t[r++];
819
- if (l === 0)
820
- break;
821
- i += String.fromCharCode(l);
822
- }
823
- break;
824
- }
825
- case "B": {
826
- i = "";
827
- const l = t[r++], u = String.fromCharCode(l), f = t.readInt32LE(r);
828
- if (r += 4, u === "i")
829
- if (h === "CG")
830
- for (let d = 0; d < f; d++) {
831
- const _ = t.readInt32LE(r), g = _ >> 4, m = C[_ & 15];
832
- i += g + m, r += 4;
833
- }
834
- else
835
- for (let d = 0; d < f; d++)
836
- i += t.readInt32LE(r), d + 1 < f && (i += ","), r += 4;
837
- if (u === "I")
838
- if (h === "CG")
839
- for (let d = 0; d < f; d++) {
840
- const _ = t.readUInt32LE(r), g = _ >> 4, m = C[_ & 15];
841
- i += g + m, r += 4;
842
- }
843
- else
844
- for (let d = 0; d < f; d++)
845
- i += t.readUInt32LE(r), d + 1 < f && (i += ","), r += 4;
846
- if (u === "s")
847
- for (let d = 0; d < f; d++)
848
- i += t.readInt16LE(r), d + 1 < f && (i += ","), r += 2;
849
- if (u === "S")
850
- for (let d = 0; d < f; d++)
851
- i += t.readUInt16LE(r), d + 1 < f && (i += ","), r += 2;
852
- if (u === "c")
853
- for (let d = 0; d < f; d++)
854
- i += t.readInt8(r), d + 1 < f && (i += ","), r += 1;
855
- if (u === "C")
856
- for (let d = 0; d < f; d++)
857
- i += t.readUInt8(r), d + 1 < f && (i += ","), r += 1;
858
- if (u === "f")
859
- for (let d = 0; d < f; d++)
860
- i += t.readFloatLE(r), d + 1 < f && (i += ","), r += 4;
861
- break;
862
- }
863
- default:
864
- console.warn(`Unknown BAM tag type '${c}', tags may be incomplete`), i = void 0, r = s;
865
- }
866
- if (this._tagOffset = r, this._tagList.push(h), a === e)
867
- return i;
868
- this.data[a] = i;
869
- }
870
- this._allTagsParsed = !0;
871
- }
872
- _parseAllTags() {
873
- this._parseTag("");
874
- }
875
- _parseCigar(e) {
876
- return (
877
- //@ts-ignore
878
- e.match(/\d+\D/g).map((t) => [t.match(/\D/)[0].toUpperCase(), Number.parseInt(t, 10)])
879
- );
880
- }
881
- /**
882
- * @returns {boolean} true if the read is paired, regardless of whether both segments are mapped
883
- */
884
- isPaired() {
885
- return !!(this.flags & b.BAM_FPAIRED);
886
- }
887
- /** @returns {boolean} true if the read is paired, and both segments are mapped */
888
- isProperlyPaired() {
889
- return !!(this.flags & b.BAM_FPROPER_PAIR);
890
- }
891
- /** @returns {boolean} true if the read itself is unmapped; conflictive with isProperlyPaired */
892
- isSegmentUnmapped() {
893
- return !!(this.flags & b.BAM_FUNMAP);
894
- }
895
- /** @returns {boolean} true if the read itself is unmapped; conflictive with isProperlyPaired */
896
- isMateUnmapped() {
897
- return !!(this.flags & b.BAM_FMUNMAP);
898
- }
899
- /** @returns {boolean} true if the read is mapped to the reverse strand */
900
- isReverseComplemented() {
901
- return !!(this.flags & b.BAM_FREVERSE);
902
- }
903
- /** @returns {boolean} true if the mate is mapped to the reverse strand */
904
- isMateReverseComplemented() {
905
- return !!(this.flags & b.BAM_FMREVERSE);
906
- }
907
- /** @returns {boolean} true if this is read number 1 in a pair */
908
- isRead1() {
909
- return !!(this.flags & b.BAM_FREAD1);
910
- }
911
- /** @returns {boolean} true if this is read number 2 in a pair */
912
- isRead2() {
913
- return !!(this.flags & b.BAM_FREAD2);
914
- }
915
- /** @returns {boolean} true if this is a secondary alignment */
916
- isSecondary() {
917
- return !!(this.flags & b.BAM_FSECONDARY);
918
- }
919
- /** @returns {boolean} true if this read has failed QC checks */
920
- isFailedQc() {
921
- return !!(this.flags & b.BAM_FQCFAIL);
922
- }
923
- /** @returns {boolean} true if the read is an optical or PCR duplicate */
924
- isDuplicate() {
925
- return !!(this.flags & b.BAM_FDUP);
926
- }
927
- /** @returns {boolean} true if this is a supplementary alignment */
928
- isSupplementary() {
929
- return !!(this.flags & b.BAM_FSUPPLEMENTARY);
930
- }
931
- cigar() {
932
- if (this.isSegmentUnmapped())
933
- return;
934
- const { byteArray: e, start: t } = this.bytes, n = this.get("_n_cigar_op");
935
- let r = t + 36 + this.get("_l_read_name");
936
- const s = this.get("seq_length");
937
- let a = "", h = 0, c = e.readInt32LE(r), i = c >> 4, l = C[c & 15];
938
- if (l === "S" && i === s)
939
- return r += 4, c = e.readInt32LE(r), i = c >> 4, l = C[c & 15], l !== "N" && console.warn("CG tag with no N tag"), this.data.length_on_ref = i, this.get("CG");
940
- for (let u = 0; u < n; ++u)
941
- c = e.readInt32LE(r), i = c >> 4, l = C[c & 15], a += i + l, l !== "H" && l !== "S" && l !== "I" && (h += i), r += 4;
942
- return this.data.length_on_ref = h, a;
943
- }
944
- length_on_ref() {
945
- return this.data.length_on_ref ? this.data.length_on_ref : (this.get("cigar"), this.data.length_on_ref);
946
- }
947
- _n_cigar_op() {
948
- return this.get("_flag_nc") & 65535;
949
- }
950
- _l_read_name() {
951
- return this.get("_bin_mq_nl") & 255;
952
- }
953
- /**
954
- * number of bytes in the sequence field
955
- */
956
- _seq_bytes() {
957
- return this.get("seq_length") + 1 >> 1;
958
- }
959
- getReadBases() {
960
- return this.seq();
961
- }
962
- seq() {
963
- const { byteArray: e, start: t } = this.bytes, n = t + 36 + this.get("_l_read_name") + this.get("_n_cigar_op") * 4, r = this.get("_seq_bytes"), s = this.get("seq_length");
964
- let a = "", h = 0;
965
- for (let c = 0; c < r; ++c) {
966
- const i = e[n + c];
967
- a += F[(i & 240) >> 4], h++, h < s && (a += F[i & 15], h++);
968
- }
969
- return a;
970
- }
971
- // adapted from igv.js
972
- getPairOrientation() {
973
- if (!this.isSegmentUnmapped() && !this.isMateUnmapped() && this._refID === this._next_refid()) {
974
- const e = this.isReverseComplemented() ? "R" : "F", t = this.isMateReverseComplemented() ? "R" : "F";
975
- let n = " ", r = " ";
976
- this.isRead1() ? (n = "1", r = "2") : this.isRead2() && (n = "2", r = "1");
977
- const s = [];
978
- return this.template_length() > 0 ? (s[0] = e, s[1] = n, s[2] = t, s[3] = r) : (s[2] = e, s[3] = n, s[0] = t, s[1] = r), s.join("");
979
- }
980
- return "";
981
- }
982
- _bin_mq_nl() {
983
- return this.bytes.byteArray.readInt32LE(this.bytes.start + 12);
984
- }
985
- _flag_nc() {
986
- return this.bytes.byteArray.readInt32LE(this.bytes.start + 16);
987
- }
988
- seq_length() {
989
- return this.bytes.byteArray.readInt32LE(this.bytes.start + 20);
990
- }
991
- _next_refid() {
992
- return this.bytes.byteArray.readInt32LE(this.bytes.start + 24);
993
- }
994
- _next_pos() {
995
- return this.bytes.byteArray.readInt32LE(this.bytes.start + 28);
996
- }
997
- template_length() {
998
- return this.bytes.byteArray.readInt32LE(this.bytes.start + 32);
999
- }
1000
- toJSON() {
1001
- const e = {};
1002
- for (const t of Object.keys(this))
1003
- t.startsWith("_") || t === "bytes" || (e[t] = this[t]);
1004
- return e;
1005
- }
1006
- }
1007
- function G(o) {
1008
- const e = o.split(/\r?\n/), t = [];
1009
- for (const n of e) {
1010
- const [r, ...s] = n.split(/\t/);
1011
- r && t.push({
1012
- tag: r.slice(1),
1013
- data: s.map((a) => {
1014
- const h = a.indexOf(":"), c = a.slice(0, h), i = a.slice(h + 1);
1015
- return { tag: c, value: i };
1016
- })
1017
- });
1018
- }
1019
- return t;
1020
- }
1021
- const j = 21840194, q = 65536;
1022
- async function ge(o) {
1023
- let e = [];
1024
- for await (const t of o)
1025
- e = e.concat(t);
1026
- return e;
1027
- }
1028
- class _e {
1029
- read() {
1030
- throw new Error("never called");
1031
- }
1032
- stat() {
1033
- throw new Error("never called");
1034
- }
1035
- readFile() {
1036
- throw new Error("never called");
1037
- }
1038
- close() {
1039
- throw new Error("never called");
1040
- }
1041
- }
1042
- class pe {
1043
- constructor({ bamFilehandle: e, bamPath: t, bamUrl: n, baiPath: r, baiFilehandle: s, baiUrl: a, csiPath: h, csiFilehandle: c, csiUrl: i, htsget: l, yieldThreadTime: u = 100, renameRefSeqs: f = (d) => d }) {
1044
- if (this.htsget = !1, this.featureCache = new Y({
1045
- cache: new W({
1046
- maxSize: 50
1047
- }),
1048
- fill: async (d, _) => {
1049
- const { chunk: g, opts: m } = d, { data: p, cpositions: w, dpositions: E } = await this._readChunk({
1050
- chunk: g,
1051
- opts: { ...m, signal: _ }
1052
- });
1053
- return this.readBamFeatures(p, w, E, g);
1054
- }
1055
- }), this.renameRefSeq = f, e)
1056
- this.bam = e;
1057
- else if (t)
1058
- this.bam = new A(t);
1059
- else if (n)
1060
- this.bam = new R(n);
1061
- else if (l)
1062
- this.htsget = !0, this.bam = new _e();
1063
- else
1064
- throw new Error("unable to initialize bam");
1065
- if (c)
1066
- this.index = new P({ filehandle: c });
1067
- else if (h)
1068
- this.index = new P({ filehandle: new A(h) });
1069
- else if (i)
1070
- this.index = new P({ filehandle: new R(i) });
1071
- else if (s)
1072
- this.index = new v({ filehandle: s });
1073
- else if (r)
1074
- this.index = new v({ filehandle: new A(r) });
1075
- else if (a)
1076
- this.index = new v({ filehandle: new R(a) });
1077
- else if (t)
1078
- this.index = new v({ filehandle: new A(`${t}.bai`) });
1079
- else if (n)
1080
- this.index = new v({ filehandle: new R(`${n}.bai`) });
1081
- else if (l)
1082
- this.htsget = !0;
1083
- else
1084
- throw new Error("unable to infer index format");
1085
- this.yieldThreadTime = u;
1086
- }
1087
- async getHeaderPre(e) {
1088
- const t = ee(e);
1089
- if (!this.index)
1090
- return;
1091
- const n = await this.index.parse(t), r = n.firstDataLine ? n.firstDataLine.blockPosition + 65535 : void 0;
1092
- let s;
1093
- if (r) {
1094
- const l = r + q, u = await this.bam.read(I.Buffer.alloc(l), 0, l, 0, t);
1095
- if (!u.bytesRead)
1096
- throw new Error("Error reading header");
1097
- s = u.buffer.subarray(0, Math.min(u.bytesRead, r));
1098
- } else
1099
- s = await this.bam.readFile(t);
1100
- const a = await k(s);
1101
- if (a.readInt32LE(0) !== j)
1102
- throw new Error("Not a BAM file");
1103
- const h = a.readInt32LE(4);
1104
- this.header = a.toString("utf8", 8, 8 + h);
1105
- const { chrToIndex: c, indexToChr: i } = await this._readRefSeqs(h + 8, 65535, t);
1106
- return this.chrToIndex = c, this.indexToChr = i, G(this.header);
1107
- }
1108
- getHeader(e) {
1109
- return this.headerP || (this.headerP = this.getHeaderPre(e).catch((t) => {
1110
- throw this.headerP = void 0, t;
1111
- })), this.headerP;
1112
- }
1113
- async getHeaderText(e = {}) {
1114
- return await this.getHeader(e), this.header;
1115
- }
1116
- // the full length of the refseq block is not given in advance so this grabs
1117
- // a chunk and doubles it if all refseqs haven't been processed
1118
- async _readRefSeqs(e, t, n) {
1119
- if (e > t)
1120
- return this._readRefSeqs(e, t * 2, n);
1121
- const r = t + q, { bytesRead: s, buffer: a } = await this.bam.read(I.Buffer.alloc(r), 0, t, 0, n);
1122
- if (!s)
1123
- throw new Error("Error reading refseqs from header");
1124
- const h = await k(a.subarray(0, Math.min(s, t))), c = h.readInt32LE(e);
1125
- let i = e + 4;
1126
- const l = {}, u = [];
1127
- for (let f = 0; f < c; f += 1) {
1128
- const d = h.readInt32LE(i), _ = this.renameRefSeq(h.toString("utf8", i + 4, i + 4 + d - 1)), g = h.readInt32LE(i + d + 4);
1129
- if (l[_] = f, u.push({ refName: _, length: g }), i = i + 8 + d, i > h.length)
1130
- return console.warn(`BAM header is very big. Re-fetching ${t} bytes.`), this._readRefSeqs(e, t * 2, n);
1131
- }
1132
- return { chrToIndex: l, indexToChr: u };
1133
- }
1134
- async getRecordsForRange(e, t, n, r) {
1135
- return ge(this.streamRecordsForRange(e, t, n, r));
1136
- }
1137
- async *streamRecordsForRange(e, t, n, r) {
1138
- var s;
1139
- await this.getHeader(r);
1140
- const a = (s = this.chrToIndex) === null || s === void 0 ? void 0 : s[e];
1141
- if (a === void 0 || !this.index)
1142
- yield [];
1143
- else {
1144
- const h = await this.index.blocksForRange(a, t - 1, n, r);
1145
- yield* this._fetchChunkFeatures(h, a, t, n, r);
1146
- }
1147
- }
1148
- async *_fetchChunkFeatures(e, t, n, r, s = {}) {
1149
- const { viewAsPairs: a } = s, h = [];
1150
- let c = !1;
1151
- for (const i of e) {
1152
- const l = await this.featureCache.get(i.toString(), { chunk: i, opts: s }, s.signal), u = [];
1153
- for (const f of l)
1154
- if (f.seq_id() === t)
1155
- if (f.get("start") >= r) {
1156
- c = !0;
1157
- break;
1158
- } else f.get("end") >= n && u.push(f);
1159
- if (h.push(u), yield u, c)
1160
- break;
1161
- }
1162
- K(s.signal), a && (yield this.fetchPairs(t, h, s));
1163
- }
1164
- async fetchPairs(e, t, n) {
1165
- const { pairAcrossChr: r, maxInsertSize: s = 2e5 } = n, a = {}, h = {};
1166
- t.map((f) => {
1167
- const d = {};
1168
- for (const _ of f) {
1169
- const g = _.name(), m = _.id();
1170
- d[g] || (d[g] = 0), d[g]++, h[m] = 1;
1171
- }
1172
- for (const [_, g] of Object.entries(d))
1173
- g === 1 && (a[_] = !0);
1174
- });
1175
- const c = [];
1176
- t.map((f) => {
1177
- for (const d of f) {
1178
- const _ = d.name(), g = d.get("start"), m = d._next_pos(), p = d._next_refid();
1179
- this.index && a[_] && (r || p === e && Math.abs(g - m) < s) && c.push(this.index.blocksForRange(p, m, m + 1, n));
1180
- }
1181
- });
1182
- const i = /* @__PURE__ */ new Map(), l = await Promise.all(c);
1183
- for (const f of l.flat())
1184
- i.has(f.toString()) || i.set(f.toString(), f);
1185
- return (await Promise.all([...i.values()].map(async (f) => {
1186
- const { data: d, cpositions: _, dpositions: g, chunk: m } = await this._readChunk({
1187
- chunk: f,
1188
- opts: n
1189
- }), p = [];
1190
- for (const w of await this.readBamFeatures(d, _, g, m))
1191
- a[w.get("name")] && !h[w.id()] && p.push(w);
1192
- return p;
1193
- }))).flat();
1194
- }
1195
- async _readRegion(e, t, n = {}) {
1196
- const { bytesRead: r, buffer: s } = await this.bam.read(I.Buffer.alloc(t), 0, t, e, n);
1197
- return s.subarray(0, Math.min(r, t));
1198
- }
1199
- async _readChunk({ chunk: e, opts: t }) {
1200
- const n = await this._readRegion(e.minv.blockPosition, e.fetchedSize(), t), { buffer: r, cpositions: s, dpositions: a } = await Q(n, e);
1201
- return { data: r, cpositions: s, dpositions: a, chunk: e };
1202
- }
1203
- async readBamFeatures(e, t, n, r) {
1204
- let s = 0;
1205
- const a = [];
1206
- let h = 0, c = +Date.now();
1207
- for (; s + 4 < e.length; ) {
1208
- const i = e.readInt32LE(s), l = s + 4 + i - 1;
1209
- if (n) {
1210
- for (; s + r.minv.dataPosition >= n[h++]; )
1211
- ;
1212
- h--;
1213
- }
1214
- if (l < e.length) {
1215
- const u = new me({
1216
- bytes: {
1217
- byteArray: e,
1218
- start: s,
1219
- end: l
1220
- },
1221
- // the below results in an automatically calculated file-offset based
1222
- // ID if the info for that is available, otherwise crc32 of the
1223
- // features
1224
- //
1225
- // cpositions[pos] refers to actual file offset of a bgzip block
1226
- // boundaries
1227
- //
1228
- // we multiply by (1 <<8) in order to make sure each block has a
1229
- // "unique" address space so that data in that block could never
1230
- // overlap
1231
- //
1232
- // then the blockStart-dpositions is an uncompressed file offset from
1233
- // that bgzip block boundary, and since the cpositions are multiplied
1234
- // by (1 << 8) these uncompressed offsets get a unique space
1235
- //
1236
- // this has an extra chunk.minv.dataPosition added on because it
1237
- // blockStart starts at 0 instead of chunk.minv.dataPosition
1238
- //
1239
- // the +1 is just to avoid any possible uniqueId 0 but this does not
1240
- // realistically happen
1241
- fileOffset: t.length > 0 ? t[h] * 256 + (s - n[h]) + r.minv.dataPosition + 1 : (
1242
- // must be slice, not subarray for buffer polyfill on web
1243
- de.signed(e.slice(s, l))
1244
- )
1245
- });
1246
- a.push(u), this.yieldThreadTime && +Date.now() - c > this.yieldThreadTime && (await X(1), c = +Date.now());
1247
- }
1248
- s = l + 1;
1249
- }
1250
- return a;
1251
- }
1252
- async hasRefSeq(e) {
1253
- var t, n;
1254
- const r = (t = this.chrToIndex) === null || t === void 0 ? void 0 : t[e];
1255
- return r === void 0 ? !1 : (n = this.index) === null || n === void 0 ? void 0 : n.hasRefSeq(r);
1256
- }
1257
- async lineCount(e) {
1258
- var t;
1259
- const n = (t = this.chrToIndex) === null || t === void 0 ? void 0 : t[e];
1260
- return n === void 0 || !this.index ? 0 : this.index.lineCount(n);
1261
- }
1262
- async indexCov(e, t, n) {
1263
- var r;
1264
- if (!this.index)
1265
- return [];
1266
- await this.index.parse();
1267
- const s = (r = this.chrToIndex) === null || r === void 0 ? void 0 : r[e];
1268
- return s === void 0 ? [] : this.index.indexCov(s, t, n);
1269
- }
1270
- async blocksForRange(e, t, n, r) {
1271
- var s;
1272
- if (!this.index)
1273
- return [];
1274
- await this.index.parse();
1275
- const a = (s = this.chrToIndex) === null || s === void 0 ? void 0 : s[e];
1276
- return a === void 0 ? [] : this.index.blocksForRange(a, t, n, r);
1277
- }
1278
- }
1279
- async function N(o, e) {
1280
- const t = await Promise.all(o.map(async (n) => {
1281
- const { url: r, headers: s } = n;
1282
- if (r.startsWith("data:"))
1283
- return I.Buffer.from(r.split(",")[1], "base64");
1284
- {
1285
- const { referer: a, ...h } = s, c = await fetch(r, {
1286
- ...e,
1287
- headers: { ...e == null ? void 0 : e.headers, ...h }
1288
- });
1289
- if (!c.ok)
1290
- throw new Error(`HTTP ${c.status} fetching ${r}: ${await c.text()}`);
1291
- return I.Buffer.from(await c.arrayBuffer());
1292
- }
1293
- }));
1294
- return I.Buffer.concat(await Promise.all(t.map((n) => k(n))));
1295
- }
1296
- class ye extends pe {
1297
- constructor(e) {
1298
- super({ htsget: !0 }), this.baseUrl = e.baseUrl, this.trackId = e.trackId;
1299
- }
1300
- async *streamRecordsForRange(e, t, n, r) {
1301
- var s;
1302
- const h = `${`${this.baseUrl}/${this.trackId}`}?referenceName=${e}&start=${t}&end=${n}&format=BAM`, c = (s = this.chrToIndex) === null || s === void 0 ? void 0 : s[e];
1303
- if (c === void 0)
1304
- yield [];
1305
- else {
1306
- const i = await fetch(h, { ...r });
1307
- if (!i.ok)
1308
- throw new Error(`HTTP ${i.status} fetching ${h}: ${await i.text()}`);
1309
- const l = await i.json(), u = await N(l.htsget.urls.slice(1), r);
1310
- yield* this._fetchChunkFeatures([
1311
- // fake stuff to pretend to be a Chunk
1312
- {
1313
- buffer: u,
1314
- _fetchedSize: void 0,
1315
- bin: 0,
1316
- compareTo() {
1317
- return 0;
1318
- },
1319
- toUniqueString() {
1320
- return `${e}_${t}_${n}`;
1321
- },
1322
- fetchedSize() {
1323
- return 0;
1324
- },
1325
- minv: {
1326
- dataPosition: 0,
1327
- blockPosition: 0,
1328
- compareTo: () => 0
1329
- },
1330
- maxv: {
1331
- dataPosition: Number.MAX_SAFE_INTEGER,
1332
- blockPosition: 0,
1333
- compareTo: () => 0
1334
- },
1335
- toString() {
1336
- return `${e}_${t}_${n}`;
1337
- }
1338
- }
1339
- ], c, t, n, r);
1340
- }
1341
- }
1342
- async _readChunk({ chunk: e }) {
1343
- if (!e.buffer)
1344
- throw new Error("expected chunk.buffer in htsget");
1345
- return { data: e.buffer, cpositions: [], dpositions: [], chunk: e };
1346
- }
1347
- async getHeader(e = {}) {
1348
- const t = `${this.baseUrl}/${this.trackId}?referenceName=na&class=header`, n = await fetch(t, e);
1349
- if (!n.ok)
1350
- throw new Error(`HTTP ${n.status} fetching ${t}: ${await n.text()}`);
1351
- const r = await n.json(), s = await N(r.htsget.urls, e);
1352
- if (s.readInt32LE(0) !== j)
1353
- throw new Error("Not a BAM file");
1354
- const a = s.readInt32LE(4), h = s.toString("utf8", 8, 8 + a), c = G(h), i = [], l = {}, u = c.filter((f) => f.tag === "SQ");
1355
- for (const [f, d] of u.entries()) {
1356
- let _ = "", g = 0;
1357
- for (const m of d.data)
1358
- m.tag === "SN" ? _ = m.value : m.tag === "LN" && (g = +m.value);
1359
- l[_] = f, i[f] = { refName: _, length: g };
1360
- }
1361
- return this.chrToIndex = l, this.indexToChr = i, c;
1362
- }
1363
- }
1364
- export {
1365
- v as BAI,
1366
- pe as BamFile,
1367
- me as BamRecord,
1368
- P as CSI,
1369
- ye as HtsgetFile
1370
- };