Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jan 22, 2024
1 parent 0dcadf4 commit f814e57
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 453 deletions.
79 changes: 79 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['prettier', '@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
'plugin:prettier/recommended',
'plugin:unicorn/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: './tsconfig.lint.json',
},
rules: {
'@typescript-eslint/no-unused-vars': [
'warn',
{ argsIgnorePattern: '^_', ignoreRestSiblings: true },
],
'no-underscore-dangle': 0,
curly: 'error',
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/ban-ts-comment': 0,
semi: ['error', 'never'],
'unicorn/no-new-array': 'off',
'unicorn/no-empty-file': 'off',
'unicorn/prefer-type-error': 'off',
'unicorn/prefer-modern-math-apis': 'off',
'unicorn/prefer-node-protocol': 'off',
'unicorn/no-unreadable-array-destructuring': 'off',
'unicorn/no-abusive-eslint-disable': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/number-literal-case': 'off',
'unicorn/prefer-add-event-listener': 'off',
'unicorn/prefer-top-level-await': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-lonely-if': 'off',
'unicorn/consistent-destructuring': 'off',
'unicorn/prefer-module': 'off',
'unicorn/prefer-optional-catch-binding': 'off',
'unicorn/no-useless-undefined': 'off',
'unicorn/no-null': 'off',
'unicorn/no-nested-ternary': 'off',
'unicorn/filename-case': 'off',
'unicorn/catch-error-name': 'off',
'unicorn/prevent-abbreviations': 'off',
'unicorn/prefer-code-point': 'off',
'unicorn/numeric-separators-style': 'off',
'unicorn/no-array-for-each': 'off',
'unicorn/prefer-spread': 'off',
'unicorn/explicit-length-check': 'off',
'unicorn/prefer-regexp-test': 'off',
'unicorn/relative-url-style': 'off',
'unicorn/prefer-math-trunc': 'off',
'unicorn/prefer-query-selector': 'off',
'unicorn/no-negated-condition': 'off',
'unicorn/switch-case-braces': 'off',
'unicorn/prefer-switch': 'off',
'unicorn/better-regex': 'off',
'unicorn/no-for-loop': 'off',
'unicorn/escape-case': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/no-process-exit': 'off',

'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'@typescript-eslint/restrict-template-expressions': 'off',
'@typescript-eslint/require-await': 'off',
},
}
31 changes: 0 additions & 31 deletions .eslintrc.json

This file was deleted.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@
"@types/buffer-crc32": "^0.2.4",
"@types/jest": "^29.5.11",
"@types/long": "^4.0.0",
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"eslint": "^8.55.0",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-unicorn": "^49.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-unicorn": "^50.0.1",
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"prettier": "^3.1.1",
"prettier": "^3.2.4",
"rimraf": "^5.0.5",
"standard-changelog": "^5.0.0",
"ts-jest": "^29.1.1",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
},
"publishConfig": {
Expand Down
6 changes: 3 additions & 3 deletions src/bai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class BAI extends IndexFile {
let curr = 8
let firstDataLine: VirtualOffset | undefined

type BinIndex = { [key: string]: Chunk[] }
type BinIndex = Record<string, Chunk[]>
type LinearIndex = VirtualOffset[]
const indices = new Array<{
binIndex: BinIndex
Expand All @@ -63,7 +63,7 @@ export default class BAI extends IndexFile {
let stats

curr += 4
const binIndex: { [key: number]: Chunk[] } = {}
const binIndex: Record<number, Chunk[]> = {}

for (let j = 0; j < binCount; j += 1) {
const bin = bytes.readUInt32LE(curr)
Expand Down Expand Up @@ -137,7 +137,7 @@ 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.at(-1).blockPosition
if (e > (linearIndex.length - 1) * v) {
throw new Error('query outside of range of linear index')
}
Expand Down
42 changes: 15 additions & 27 deletions src/bamFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ export const BAM_MAGIC = 21840194

const blockLen = 1 << 16

function isGzip(buf: Buffer) {
return buf[0] === 31 && buf[1] === 139 && buf[2] === 8
}

async function gen2array<T>(gen: AsyncIterable<T[]>): Promise<T[]> {
let out: T[] = []
for await (const x of gen) {
Expand Down Expand Up @@ -158,10 +154,10 @@ export default class BamFile {
}
buffer = res.buffer.subarray(0, Math.min(res.bytesRead, ret))
} else {
buffer = (await this.bam.readFile(opts)) as Buffer
buffer = await this.bam.readFile(opts)
}

const uncba = isGzip(buffer) ? await unzip(buffer) : buffer
const uncba = await unzip(buffer)

if (uncba.readInt32LE(0) !== BAM_MAGIC) {
throw new Error('Not a BAM file')
Expand Down Expand Up @@ -202,7 +198,7 @@ export default class BamFile {
refSeqBytes: number,
opts?: BaseOpts,
): Promise<{
chrToIndex: { [key: string]: number }
chrToIndex: Record<string, number>
indexToChr: { refName: string; length: number }[]
}> {
if (start > refSeqBytes) {
Expand All @@ -220,10 +216,10 @@ export default class BamFile {
throw new Error('Error reading refseqs from header')
}
const ret = buffer.subarray(0, Math.min(bytesRead, refSeqBytes))
const uncba = isGzip(ret) ? await unzip(ret) : ret
const uncba = await unzip(ret)
const nRef = uncba.readInt32LE(start)
let p = start + 4
const chrToIndex: { [key: string]: number } = {}
const chrToIndex: Record<string, number> = {}
const indexToChr: { refName: string; length: number }[] = []
for (let i = 0; i < nRef; i += 1) {
const lName = uncba.readInt32LE(p)
Expand Down Expand Up @@ -317,10 +313,10 @@ export default class BamFile {

async fetchPairs(chrId: number, feats: BAMFeature[][], opts: BamOpts) {
const { pairAcrossChr, maxInsertSize = 200000 } = opts
const unmatedPairs: { [key: string]: boolean } = {}
const readIds: { [key: string]: number } = {}
const unmatedPairs: Record<string, boolean> = {}
const readIds: Record<string, number> = {}
feats.map(ret => {
const readNames: { [key: string]: number } = {}
const readNames: Record<string, number> = {}
for (const element of ret) {
const name = element.name()
const id = element.id()
Expand Down Expand Up @@ -408,21 +404,12 @@ export default class BamFile {
chunk.fetchedSize(),
opts,
)
if (isGzip(buffer)) {
const {
buffer: data,
cpositions,
dpositions,
} = await unzipChunkSlice(buffer, chunk)
return { data, cpositions, dpositions, chunk }
} else {
// const {
// buffer,
// cpositions: [],
// dpositions: [],
console.log(buffer)
return { data: buffer, cpositions: [], dpositions: [], chunk }
}
const {
buffer: data,
cpositions,
dpositions,
} = await unzipChunkSlice(buffer, chunk)
return { data, cpositions, dpositions, chunk }
}

async readBamFeatures(
Expand All @@ -442,6 +429,7 @@ export default class BamFile {

// increment position to the current decompressed status
if (dpositions) {
// eslint-disable-next-line no-empty
while (blockStart + chunk.minv.dataPosition >= dpositions[pos++]) {}
pos--
}
Expand Down
8 changes: 3 additions & 5 deletions src/csi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export default class CSI extends IndexFile {
const coordinateType =
formatFlags & 0x10000 ? 'zero-based-half-open' : '1-based-closed'
const format = (
{ 0: 'generic', 1: 'SAM', 2: 'VCF' } as {
[key: number]: string
}
{ 0: 'generic', 1: 'SAM', 2: 'VCF' } as Record<number, string>
)[formatFlags & 0xf]
if (!format) {
throw new Error(`invalid Tabix preset format flags ${formatFlags}`)
Expand Down Expand Up @@ -97,7 +95,7 @@ export default class CSI extends IndexFile {
const aux = auxLength >= 30 ? this.parseAuxData(bytes, 16) : undefined
const refCount = bytes.readInt32LE(16 + auxLength)

type BinIndex = { [key: string]: Chunk[] }
type BinIndex = Record<string, Chunk[]>

// read the indexes for each reference sequence
let curr = 16 + auxLength + 4
Expand All @@ -110,7 +108,7 @@ export default class CSI extends IndexFile {
// the binning index
const binCount = bytes.readInt32LE(curr)
curr += 4
const binIndex: { [key: string]: Chunk[] } = {}
const binIndex: Record<string, Chunk[]> = {}
let stats // < provided by parsing a pseudo-bin, if present
for (let j = 0; j < binCount; j++) {
const bin = bytes.readUInt32LE(curr)
Expand Down
2 changes: 1 addition & 1 deletion src/htsget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function concat(arr: HtsgetChunk[], opts?: Record<string, any>) {
} else {
//remove referer header, it is not even allowed to be specified
// @ts-expect-error
//eslint-disable-next-line @typescript-eslint/no-unused-vars

const { referer, ...rest } = headers
const res = await fetch(url, {
...opts,
Expand Down
12 changes: 5 additions & 7 deletions src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CIGAR_DECODER = 'MIDNSHP=X???????'.split('')
* Class of each BAM record returned by this API.
*/
export default class BamRecord {
private data = {} as { [key: string]: any }
private data = {} as Record<string, any>
private bytes: { start: number; end: number; byteArray: Buffer }
private _id: number
private _tagOffset: number | undefined
Expand Down Expand Up @@ -83,12 +83,12 @@ export default class BamRecord {
tags = tags.concat(this._tagList || [])

for (const k of Object.keys(this.data)) {
if (k[0] !== '_' && k !== 'next_seq_id') {
if (!k.startsWith('_') && k !== 'next_seq_id') {
tags.push(k)
}
}

const seen: { [key: string]: boolean } = {}
const seen: Record<string, boolean> = {}
return tags.filter(t => {
if (
(t in this.data && this.data[t] === undefined) ||
Expand Down Expand Up @@ -493,8 +493,6 @@ export default class BamRecord {
}
}

_flags() {}

length_on_ref() {
if (this.data.length_on_ref) {
return this.data.length_on_ref
Expand Down Expand Up @@ -605,9 +603,9 @@ export default class BamRecord {
}

toJSON() {
const data: { [key: string]: any } = {}
const data: Record<string, any> = {}
for (const k of Object.keys(this)) {
if (k.charAt(0) === '_' || k === 'bytes') {
if (k.startsWith('_') || k === 'bytes') {
continue
}
//@ts-ignore
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface BaseOpts {
}

export function makeOpts(obj: AbortSignal | BaseOpts = {}): BaseOpts {
return 'aborted' in obj ? ({ signal: obj } as BaseOpts) : (obj as BaseOpts)
return 'aborted' in obj ? ({ signal: obj } as BaseOpts) : obj
}

export function optimizeChunks(chunks: Chunk[], lowest?: VirtualOffset) {
Expand Down Expand Up @@ -140,7 +140,7 @@ export function parseNameBytes(
let currRefId = 0
let currNameStart = 0
const refIdToName = []
const refNameToId: { [key: string]: number } = {}
const refNameToId: Record<string, number> = {}
for (let i = 0; i < namesBytes.length; i += 1) {
if (!namesBytes[i]) {
if (currNameStart < i) {
Expand Down
4 changes: 2 additions & 2 deletions test/bai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ test('read as pairs', async () => {
expect(features.map(f => f.get('name')).sort()).toEqual(
features2.map(f => f.get('name')).sort(),
)
const f = features[features.length - 1]
const f2 = features2[features2.length - 1]
const f = features.at(-1)
const f2 = features2.at(-1)
expect(f.get('start')).toEqual(f2.get('start'))
})

Expand Down
6 changes: 3 additions & 3 deletions test/vanilla-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('using vanilla node filehandles', () => {
expect(indexData.bai).toEqual(true)
expect(await ti.lineCount(0)).toEqual(9596)
expect(await ti.hasRefSeq(0)).toEqual(true)
filehandle.close()
await filehandle.close()
})

it('gets features from volvox-sorted.bam', async () => {
Expand All @@ -39,7 +39,7 @@ describe('using vanilla node filehandles', () => {
expect(records[0].getReadBases()).toEqual(
'TTGTTGCGGAGTTGAACAACGGCATTAGGAACACTTCCGTCTCTCACTTTTATACGATTATGATTGGTTCTTTAGCCTTGGTTTAGATTGGTAGTAGTAG',
)
bam.close()
bai.close()
await bam.close()
await bai.close()
})
})
Loading

0 comments on commit f814e57

Please sign in to comment.