Skip to content

Commit

Permalink
Wow
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Nov 11, 2024
1 parent 11bcd2f commit a85c13f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "all",
"singleQuote": true,
"arrowParens": "avoid",
"proseWrap": "always"
}
25 changes: 23 additions & 2 deletions src/bai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function reg2bins(beg: number, end: number) {
}

export default class BAI extends IndexFile {
public setupP?: ReturnType<BAI['_parse']>

async lineCount(refId: number, opts?: BaseOpts) {
const indexData = await this.parse(opts)
return indexData.indices[refId]?.stats?.lineCount || 0
Expand Down Expand Up @@ -166,10 +168,12 @@ export default class BAI extends IndexFile {
}

const indexData = await this.parse(opts)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!indexData) {
return []
}
const ba = indexData.indices[refId]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!ba) {
return []
}
Expand All @@ -181,10 +185,11 @@ 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]
for (const chunk of binChunks) {
chunks.push(new Chunk(chunk.minv, chunk.maxv, bin))
for (const binChunk of binChunks) {
binChunks.push(new Chunk(binChunk.minv, binChunk.maxv, bin))
}
}
}
Expand All @@ -199,11 +204,27 @@ 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
}
}

return optimizeChunks(chunks, lowest)
}

async parse(opts: BaseOpts = {}) {
if (!this.setupP) {
this.setupP = this._parse(opts).catch((e: unknown) => {
this.setupP = undefined
throw e
})
}
return this.setupP
}

async hasRefSeq(seqId: number, opts: BaseOpts = {}) {
const header = await this.parse(opts)
return !!header.indices[seqId]?.binIndex
}
}
27 changes: 0 additions & 27 deletions src/indexFile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { GenericFilehandle } from 'generic-filehandle'
import VirtualOffset from './virtualOffset'
import Chunk from './chunk'
import { BaseOpts } from './util'

export default abstract class IndexFile {
public filehandle: GenericFilehandle
public renameRefSeq: (s: string) => string
public setupP?: Promise<any>

/**
* @param {filehandle} filehandle
Expand All @@ -23,7 +21,6 @@ export default abstract class IndexFile {
this.renameRefSeq = renameRefSeq
}
public abstract lineCount(refId: number): Promise<number>
protected abstract _parse(opts?: BaseOpts): Promise<any>
public abstract indexCov(
refId: number,
start?: number,
Expand All @@ -36,28 +33,4 @@ export default abstract class IndexFile {
end: number,
opts?: BaseOpts,
): Promise<Chunk[]>

_findFirstData(data: any, virtualOffset: VirtualOffset) {
const currentFdl = data.firstDataLine
if (currentFdl) {
data.firstDataLine =
currentFdl.compareTo(virtualOffset) > 0 ? virtualOffset : currentFdl
} else {
data.firstDataLine = virtualOffset
}
}

async parse(opts: BaseOpts = {}) {
if (!this.setupP) {
this.setupP = this._parse(opts).catch(e => {
this.setupP = undefined
throw e
})
}
return this.setupP
}

async hasRefSeq(seqId: number, opts: BaseOpts = {}) {
return !!(await this.parse(opts)).indices[seqId]?.binIndex
}
}

0 comments on commit a85c13f

Please sign in to comment.