-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbamFile.ts
486 lines (446 loc) · 13.7 KB
/
bamFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import AbortablePromiseCache from '@gmod/abortable-promise-cache'
import { unzip, unzipChunkSlice } from '@gmod/bgzf-filehandle'
import crc32 from 'crc/calculators/crc32'
import { LocalFile, RemoteFile } from 'generic-filehandle2'
import QuickLRU from 'quick-lru'
import BAI from './bai'
import Chunk from './chunk'
import CSI from './csi'
import NullFilehandle from './nullFilehandle'
import BAMFeature from './record'
import { parseHeaderText } from './sam'
import {
BamOpts,
BaseOpts,
checkAbortSignal,
gen2array,
makeOpts,
timeout,
} from './util'
import type { GenericFilehandle } from 'generic-filehandle2'
export const BAM_MAGIC = 21840194
const blockLen = 1 << 16
interface Args {
chunk: Chunk
opts: BaseOpts
}
export default class BamFile {
public renameRefSeq: (a: string) => string
public bam: GenericFilehandle
public header?: string
public chrToIndex?: Record<string, number>
public indexToChr?: { refName: string; length: number }[]
public yieldThreadTime: number
public index?: BAI | CSI
public htsget = false
public headerP?: ReturnType<BamFile['getHeaderPre']>
private featureCache = new AbortablePromiseCache<Args, BAMFeature[]>({
cache: new QuickLRU({
maxSize: 50,
}),
fill: async (args: Args, signal) => {
const { chunk, opts } = args
const { data, cpositions, dpositions } = await this._readChunk({
chunk,
opts: { ...opts, signal },
})
return this.readBamFeatures(data, cpositions, dpositions, chunk)
},
})
constructor({
bamFilehandle,
bamPath,
bamUrl,
baiPath,
baiFilehandle,
baiUrl,
csiPath,
csiFilehandle,
csiUrl,
htsget,
yieldThreadTime = 100,
renameRefSeqs = n => n,
}: {
bamFilehandle?: GenericFilehandle
bamPath?: string
bamUrl?: string
baiPath?: string
baiFilehandle?: GenericFilehandle
baiUrl?: string
csiPath?: string
csiFilehandle?: GenericFilehandle
csiUrl?: string
renameRefSeqs?: (a: string) => string
yieldThreadTime?: number
htsget?: boolean
}) {
this.renameRefSeq = renameRefSeqs
if (bamFilehandle) {
this.bam = bamFilehandle
} else if (bamPath) {
this.bam = new LocalFile(bamPath)
} else if (bamUrl) {
this.bam = new RemoteFile(bamUrl)
} else if (htsget) {
this.htsget = true
this.bam = new NullFilehandle()
} else {
throw new Error('unable to initialize bam')
}
if (csiFilehandle) {
this.index = new CSI({ filehandle: csiFilehandle })
} else if (csiPath) {
this.index = new CSI({ filehandle: new LocalFile(csiPath) })
} else if (csiUrl) {
this.index = new CSI({ filehandle: new RemoteFile(csiUrl) })
} else if (baiFilehandle) {
this.index = new BAI({ filehandle: baiFilehandle })
} else if (baiPath) {
this.index = new BAI({ filehandle: new LocalFile(baiPath) })
} else if (baiUrl) {
this.index = new BAI({ filehandle: new RemoteFile(baiUrl) })
} else if (bamPath) {
this.index = new BAI({ filehandle: new LocalFile(`${bamPath}.bai`) })
} else if (bamUrl) {
this.index = new BAI({ filehandle: new RemoteFile(`${bamUrl}.bai`) })
} else if (htsget) {
this.htsget = true
} else {
throw new Error('unable to infer index format')
}
this.yieldThreadTime = yieldThreadTime
}
async getHeaderPre(origOpts?: BaseOpts) {
const opts = makeOpts(origOpts)
if (!this.index) {
return
}
const indexData = await this.index.parse(opts)
const ret = indexData.firstDataLine
? indexData.firstDataLine.blockPosition + 65535
: undefined
let buffer
if (ret) {
const s = ret + blockLen
buffer = await this.bam.read(s, 0)
} else {
buffer = await this.bam.readFile(opts)
}
const uncba = await unzip(buffer)
const dataView = new DataView(uncba.buffer)
if (dataView.getInt32(0, true) !== BAM_MAGIC) {
throw new Error('Not a BAM file')
}
const headLen = dataView.getInt32(4, true)
const decoder = new TextDecoder('utf8')
this.header = decoder.decode(uncba.subarray(8, 8 + headLen))
const { chrToIndex, indexToChr } = await this._readRefSeqs(
headLen + 8,
65535,
opts,
)
this.chrToIndex = chrToIndex
this.indexToChr = indexToChr
return parseHeaderText(this.header)
}
getHeader(opts?: BaseOpts) {
if (!this.headerP) {
this.headerP = this.getHeaderPre(opts).catch((e: unknown) => {
this.headerP = undefined
throw e
})
}
return this.headerP
}
async getHeaderText(opts: BaseOpts = {}) {
await this.getHeader(opts)
return this.header
}
// the full length of the refseq block is not given in advance so this grabs
// a chunk and doubles it if all refseqs haven't been processed
async _readRefSeqs(
start: number,
refSeqBytes: number,
opts?: BaseOpts,
): Promise<{
chrToIndex: Record<string, number>
indexToChr: { refName: string; length: number }[]
}> {
if (start > refSeqBytes) {
return this._readRefSeqs(start, refSeqBytes * 2, opts)
}
// const size = refSeqBytes + blockLen <-- use this?
const buffer = await this.bam.read(refSeqBytes, 0, opts)
const uncba = await unzip(buffer)
const dataView = new DataView(uncba.buffer)
const nRef = dataView.getInt32(start, true)
let p = start + 4
const chrToIndex: Record<string, number> = {}
const indexToChr: { refName: string; length: number }[] = []
const decoder = new TextDecoder('utf8')
for (let i = 0; i < nRef; i += 1) {
const lName = dataView.getInt32(p, true)
const refName = this.renameRefSeq(
decoder.decode(uncba.subarray(p + 4, p + 4 + lName - 1)),
)
const lRef = dataView.getInt32(p + lName + 4, true)
chrToIndex[refName] = i
indexToChr.push({ refName, length: lRef })
p = p + 8 + lName
if (p > uncba.length) {
console.warn(
`BAM header is very big. Re-fetching ${refSeqBytes} bytes.`,
)
return this._readRefSeqs(start, refSeqBytes * 2, opts)
}
}
return { chrToIndex, indexToChr }
}
async getRecordsForRange(
chr: string,
min: number,
max: number,
opts?: BamOpts,
) {
return gen2array(this.streamRecordsForRange(chr, min, max, opts))
}
async *streamRecordsForRange(
chr: string,
min: number,
max: number,
opts?: BamOpts,
) {
await this.getHeader(opts)
const chrId = this.chrToIndex?.[chr]
if (chrId === undefined || !this.index) {
yield []
} else {
const chunks = await this.index.blocksForRange(chrId, min - 1, max, opts)
yield* this._fetchChunkFeatures(chunks, chrId, min, max, opts)
}
}
async *_fetchChunkFeatures(
chunks: Chunk[],
chrId: number,
min: number,
max: number,
opts: BamOpts = {},
) {
const { viewAsPairs } = opts
const feats = [] as BAMFeature[][]
let done = false
for (const chunk of chunks) {
const records = await this.featureCache.get(
chunk.toString(),
{ chunk, opts },
opts.signal,
)
const recs = [] as BAMFeature[]
for (const feature of records) {
if (feature.ref_id === chrId) {
if (feature.start >= max) {
// past end of range, can stop iterating
done = true
break
} else if (feature.end >= min) {
// must be in range
recs.push(feature)
}
}
}
feats.push(recs)
yield recs
if (done) {
break
}
}
checkAbortSignal(opts.signal)
if (viewAsPairs) {
yield this.fetchPairs(chrId, feats, opts)
}
}
async fetchPairs(chrId: number, feats: BAMFeature[][], opts: BamOpts) {
const { pairAcrossChr, maxInsertSize = 200000 } = opts
const unmatedPairs: Record<string, boolean> = {}
const readIds: Record<string, number> = {}
feats.map(ret => {
const readNames: Record<string, number> = {}
for (const element of ret) {
const name = element.name
const id = element.id
if (!readNames[name]) {
readNames[name] = 0
}
readNames[name]++
readIds[id] = 1
}
for (const [k, v] of Object.entries(readNames)) {
if (v === 1) {
unmatedPairs[k] = true
}
}
})
const matePromises: Promise<Chunk[]>[] = []
feats.map(ret => {
for (const f of ret) {
const name = f.name
const start = f.start
const pnext = f.next_pos
const rnext = f.next_refid
if (
this.index &&
unmatedPairs[name] &&
(pairAcrossChr ||
(rnext === chrId && Math.abs(start - pnext) < maxInsertSize))
) {
matePromises.push(
this.index.blocksForRange(rnext, pnext, pnext + 1, opts),
)
}
}
})
// filter out duplicate chunks (the blocks are lists of chunks, blocks are
// concatenated, then filter dup chunks)
const map = new Map<string, Chunk>()
const res = await Promise.all(matePromises)
for (const m of res.flat()) {
if (!map.has(m.toString())) {
map.set(m.toString(), m)
}
}
const mateFeatPromises = await Promise.all(
[...map.values()].map(async c => {
const { data, cpositions, dpositions, chunk } = await this._readChunk({
chunk: c,
opts,
})
const mateRecs = [] as BAMFeature[]
for (const feature of await this.readBamFeatures(
data,
cpositions,
dpositions,
chunk,
)) {
if (unmatedPairs[feature.name] && !readIds[feature.id]) {
mateRecs.push(feature)
}
}
return mateRecs
}),
)
return mateFeatPromises.flat()
}
async _readChunk({ chunk, opts }: { chunk: Chunk; opts: BaseOpts }) {
const buf = await this.bam.read(
chunk.fetchedSize(),
chunk.minv.blockPosition,
opts,
)
const {
buffer: data,
cpositions,
dpositions,
} = await unzipChunkSlice(buf, chunk)
return { data, cpositions, dpositions, chunk }
}
async readBamFeatures(
ba: Uint8Array,
cpositions: number[],
dpositions: number[],
chunk: Chunk,
) {
let blockStart = 0
const sink = [] as BAMFeature[]
let pos = 0
let last = +Date.now()
const dataView = new DataView(ba.buffer)
while (blockStart + 4 < ba.length) {
const blockSize = dataView.getInt32(blockStart, true)
const blockEnd = blockStart + 4 + blockSize - 1
// increment position to the current decompressed status
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (dpositions) {
while (blockStart + chunk.minv.dataPosition >= dpositions[pos++]!) {}
pos--
}
// only try to read the feature if we have all the bytes for it
if (blockEnd < ba.length) {
const feature = new BAMFeature({
bytes: {
byteArray: ba,
start: blockStart,
end: blockEnd,
},
// the below results in an automatically calculated file-offset based
// ID if the info for that is available, otherwise crc32 of the
// features
//
// cpositions[pos] refers to actual file offset of a bgzip block
// boundaries
//
// we multiply by (1 <<8) in order to make sure each block has a
// "unique" address space so that data in that block could never
// overlap
//
// then the blockStart-dpositions is an uncompressed file offset from
// that bgzip block boundary, and since the cpositions are multiplied
// by (1 << 8) these uncompressed offsets get a unique space
//
// this has an extra chunk.minv.dataPosition added on because it
// blockStart starts at 0 instead of chunk.minv.dataPosition
//
// the +1 is just to avoid any possible uniqueId 0 but this does not
// realistically happen
fileOffset:
cpositions.length > 0
? cpositions[pos]! * (1 << 8) +
(blockStart - dpositions[pos]!) +
chunk.minv.dataPosition +
1
: // this shift >>> 0 is equivalent to crc32(b).unsigned but uses the
// internal calculator of crc32 to avoid accidentally importing buffer
// https://github.com/alexgorbatchev/crc/blob/31fc3853e417b5fb5ec83335428805842575f699/src/define_crc.ts#L5
crc32(ba.subarray(blockStart, blockEnd)) >>> 0,
})
sink.push(feature)
if (this.yieldThreadTime && +Date.now() - last > this.yieldThreadTime) {
await timeout(1)
last = +Date.now()
}
}
blockStart = blockEnd + 1
}
return sink
}
async hasRefSeq(seqName: string) {
const seqId = this.chrToIndex?.[seqName]
return seqId === undefined ? false : this.index?.hasRefSeq(seqId)
}
async lineCount(seqName: string) {
const seqId = this.chrToIndex?.[seqName]
return seqId === undefined || !this.index ? 0 : this.index.lineCount(seqId)
}
async indexCov(seqName: string, start?: number, end?: number) {
if (!this.index) {
return []
}
await this.index.parse()
const seqId = this.chrToIndex?.[seqName]
return seqId === undefined ? [] : this.index.indexCov(seqId, start, end)
}
async blocksForRange(
seqName: string,
start: number,
end: number,
opts?: BaseOpts,
) {
if (!this.index) {
return []
}
await this.index.parse()
const seqId = this.chrToIndex?.[seqName]
return seqId === undefined
? []
: this.index.blocksForRange(seqId, start, end, opts)
}
}