Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jan 2, 2025
1 parent 4102067 commit 03c9d74
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 274 deletions.
12 changes: 0 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
## [5.0.5](https://github.com/GMOD/bam-js/compare/v5.0.4...v5.0.5) (2024-12-18)



## [5.0.4](https://github.com/GMOD/bam-js/compare/v5.0.2...v5.0.4) (2024-12-18)



## [5.0.3](https://github.com/GMOD/bam-js/compare/v5.0.2...v5.0.3) (2024-12-18)



## [5.0.2](https://github.com/GMOD/bam-js/compare/v5.0.1...v5.0.2) (2024-12-17)



## [5.0.1](https://github.com/GMOD/bam-js/compare/v5.0.0...v5.0.1) (2024-12-12)



# [5.0.0](https://github.com/GMOD/bam-js/compare/v4.0.1...v5.0.0) (2024-12-12)



## [4.0.1](https://github.com/GMOD/bam-js/compare/v4.0.0...v4.0.1) (2024-11-12)

# [4.0.0](https://github.com/GMOD/bam-js/compare/v3.0.3...v4.0.0) (2024-11-12)
Expand Down
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default tseslint.config(
'unicorn/prefer-number-properties': 'off',
'unicorn/no-process-exit': 'off',
'unicorn/prefer-at': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
Expand Down
16 changes: 7 additions & 9 deletions src/bai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class BAI extends IndexFile {
const range = start !== undefined
const indexData = await this.parse(opts)
const seqIdx = indexData.indices[seqId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

if (!seqIdx) {
return []
}
Expand All @@ -141,18 +141,18 @@ export default class BAI extends IndexFile {
const depths = range
? new Array((e - s) / v)
: new Array(linearIndex.length - 1)
const totalSize = linearIndex[linearIndex.length - 1].blockPosition
const totalSize = linearIndex[linearIndex.length - 1]!.blockPosition
if (e > (linearIndex.length - 1) * v) {
throw new Error('query outside of range of linear index')
}
let currentPos = linearIndex[s / v].blockPosition
let currentPos = linearIndex[s / v]!.blockPosition
for (let i = s / v, j = 0; i < e / v; i++, j++) {
depths[j] = {
score: linearIndex[i + 1].blockPosition - currentPos,
score: linearIndex[i + 1]!.blockPosition - currentPos,
start: i * v,
end: i * v + v,
}
currentPos = linearIndex[i + 1].blockPosition
currentPos = linearIndex[i + 1]!.blockPosition
}
return depths.map(d => ({
...d,
Expand All @@ -176,7 +176,7 @@ export default class BAI extends IndexFile {
return []
}
const ba = indexData.indices[refId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

if (!ba) {
return []
}
Expand All @@ -188,9 +188,8 @@ export default class BAI extends IndexFile {
// Find chunks in overlapping bins. Leaf bins (< 4681) are not pruned
for (const [start, end] of overlappingBins) {
for (let bin = start; bin <= end; bin++) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (ba.binIndex[bin]) {
const binChunks = ba.binIndex[bin]
const binChunks = ba.binIndex[bin]!
for (const binChunk of binChunks) {
chunks.push(new Chunk(binChunk.minv, binChunk.maxv, bin))
}
Expand All @@ -207,7 +206,6 @@ export default class BAI extends IndexFile {
for (let i = minLin; i <= maxLin; ++i) {
const vp = ba.linearIndex[i]

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (vp && (!lowest || vp.compareTo(lowest) < 0)) {
lowest = vp
}
Expand Down
16 changes: 6 additions & 10 deletions src/bamFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,18 @@ export default class BamFile {
return mateFeatPromises.flat()
}

async _readRegion(position: number, size: number, opts: BaseOpts = {}) {
return this.bam.read(size, position, opts)
}

async _readChunk({ chunk, opts }: { chunk: Chunk; opts: BaseOpts }) {
const buffer = await this._readRegion(
chunk.minv.blockPosition,
const buf = await this.bam.read(
chunk.fetchedSize(),
chunk.minv.blockPosition,
opts,
)

const {
buffer: data,
cpositions,
dpositions,
} = await unzipChunkSlice(buffer, chunk)
} = await unzipChunkSlice(buf, chunk)
return { data, cpositions, dpositions, chunk }
}

Expand All @@ -413,7 +409,7 @@ export default class BamFile {
// 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++]) {}
while (blockStart + chunk.minv.dataPosition >= dpositions[pos++]!) {}
pos--
}

Expand Down Expand Up @@ -447,8 +443,8 @@ export default class BamFile {
// realistically happen
fileOffset:
cpositions.length > 0
? cpositions[pos] * (1 << 8) +
(blockStart - dpositions[pos]) +
? cpositions[pos]! * (1 << 8) +
(blockStart - dpositions[pos]!) +
chunk.minv.dataPosition +
1
: // this shift >>> 0 is equivalent to crc32(b).unsigned but uses the
Expand Down
5 changes: 2 additions & 3 deletions src/csi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class CSI extends IndexFile {

const indexData = await this.parse(opts)
const ba = indexData.indices[refId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

if (!ba) {
return []
}
Expand All @@ -177,9 +177,8 @@ export default class CSI extends IndexFile {
// Find chunks in overlapping bins. Leaf bins (< 4681) are not pruned
for (const [start, end] of overlappingBins) {
for (let bin = start; bin <= end; bin++) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (ba.binIndex[bin]) {
const binChunks = ba.binIndex[bin]
const binChunks = ba.binIndex[bin]!
for (const c of binChunks) {
chunks.push(c)
}
Expand Down
16 changes: 8 additions & 8 deletions src/long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ export const TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL

export function longFromBytesToUnsigned(source: Uint8Array, i = 0) {
const low =
source[i] |
(source[i + 1] << 8) |
(source[i + 2] << 16) |
(source[i + 3] << 24)
source[i]! |
(source[i + 1]! << 8) |
(source[i + 2]! << 16) |
(source[i + 3]! << 24)
const high =
source[i + 4] |
(source[i + 5] << 8) |
(source[i + 6] << 16) |
(source[i + 7] << 24)
source[i + 4]! |
(source[i + 5]! << 8) |
(source[i + 6]! << 16) |
(source[i + 7]! << 24)
return (high >>> 0) * TWO_PWR_32_DBL + (low >>> 0)
}
24 changes: 13 additions & 11 deletions src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class BamRecord {
get name() {
let str = ''
for (let i = 0; i < this.read_name_length - 1; i++) {
str += String.fromCharCode(this.byteArray[this.b0 + i])
str += String.fromCharCode(this.byteArray[this.b0 + i]!)
}
return str
}
Expand All @@ -93,12 +93,15 @@ export default class BamRecord {
const blockEnd = this.bytes.end
const tags = {} as Record<string, unknown>
while (p < blockEnd) {
const tag = String.fromCharCode(this.byteArray[p], this.byteArray[p + 1])
const type = String.fromCharCode(this.byteArray[p + 2])
const tag = String.fromCharCode(
this.byteArray[p]!,
this.byteArray[p + 1]!,
)
const type = String.fromCharCode(this.byteArray[p + 2]!)
p += 3

if (type === 'A') {
tags[tag] = String.fromCharCode(this.byteArray[p])
tags[tag] = String.fromCharCode(this.byteArray[p]!)
p += 1
} else if (type === 'i') {
tags[tag] = this.#dataView.getInt32(p, true)
Expand All @@ -124,7 +127,7 @@ export default class BamRecord {
} else if (type === 'Z' || type === 'H') {
const value = []
while (p <= blockEnd) {
const cc = this.byteArray[p++]
const cc = this.byteArray[p++]!
if (cc !== 0) {
value.push(String.fromCharCode(cc))
} else {
Expand All @@ -133,7 +136,7 @@ export default class BamRecord {
}
tags[tag] = value.join('')
} else if (type === 'B') {
const cc = this.byteArray[p++]
const cc = this.byteArray[p++]!
const Btype = String.fromCharCode(cc)
const limit = this.#dataView.getInt32(p, true)
p += 4
Expand All @@ -143,7 +146,7 @@ export default class BamRecord {
for (let k = 0; k < limit; k++) {
const cigop = this.#dataView.getInt32(p, true)
const lop = cigop >> 4
const op = CIGAR_DECODER[cigop & 0xf]
const op = CIGAR_DECODER[cigop & 0xf]!
value.push(lop + op)
p += 4
}
Expand All @@ -162,7 +165,7 @@ export default class BamRecord {
for (let k = 0; k < limit; k++) {
const cigop = this.#dataView.getUint32(p, true)
const lop = cigop >> 4
const op = CIGAR_DECODER[cigop & 0xf]
const op = CIGAR_DECODER[cigop & 0xf]!
value.push(lop + op)
p += 4
}
Expand Down Expand Up @@ -318,7 +321,7 @@ export default class BamRecord {
for (let c = 0; c < numCigarOps; ++c) {
cigop = this.#dataView.getInt32(p, true)
lop = cigop >> 4
op = CIGAR_DECODER[cigop & 0xf]
op = CIGAR_DECODER[cigop & 0xf]!
CIGAR.push(lop + op)
// soft clip, hard clip, and insertion don't count toward the length on
// the reference
Expand Down Expand Up @@ -357,14 +360,13 @@ export default class BamRecord {
}

get seq() {
const { byteArray } = this.bytes
const p = this.b0 + this.read_name_length + this.num_cigar_ops * 4
const seqBytes = this.num_seq_bytes
const len = this.seq_length
const buf = []
let i = 0
for (let j = 0; j < seqBytes; ++j) {
const sb = byteArray[p + j]
const sb = this.byteArray[p + j]!
buf.push(SEQRET_DECODER[(sb & 0xf0) >> 4])
i++
if (i < len) {
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function parseNameBytes(
if (currNameStart < i) {
let refName = ''
for (let j = currNameStart; j < i; j++) {
refName += String.fromCharCode(namesBytes[j])
refName += String.fromCharCode(namesBytes[j]!)
}
refName = renameRefSeq(refName)
refIdToName[currRefId] = refName
Expand Down
28 changes: 7 additions & 21 deletions src/virtualOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,19 @@ export default class VirtualOffset {
this.blockPosition - b.blockPosition || this.dataPosition - b.dataPosition
)
}

static min(...args: VirtualOffset[]) {
let min
let i = 0
for (; !min; i += 1) {
min = args[i]
}
for (; i < args.length; i += 1) {
if (min.compareTo(args[i]) > 0) {
min = args[i]
}
}
return min
}
}
export function fromBytes(bytes: Uint8Array, offset = 0, bigendian = false) {
if (bigendian) {
throw new Error('big-endian virtual file offsets not implemented')
}

return new VirtualOffset(
bytes[offset + 7] * 0x10000000000 +
bytes[offset + 6] * 0x100000000 +
bytes[offset + 5] * 0x1000000 +
bytes[offset + 4] * 0x10000 +
bytes[offset + 3] * 0x100 +
bytes[offset + 2],
(bytes[offset + 1] << 8) | bytes[offset],
bytes[offset + 7]! * 0x10000000000 +
bytes[offset + 6]! * 0x100000000 +
bytes[offset + 5]! * 0x1000000 +
bytes[offset + 4]! * 0x10000 +
bytes[offset + 3]! * 0x100 +
bytes[offset + 2]!,
(bytes[offset + 1]! << 8) | bytes[offset]!,
)
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true
}
}
Loading

0 comments on commit 03c9d74

Please sign in to comment.