Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Aug 29, 2024
1 parent f6271b3 commit bcb86f4
Show file tree
Hide file tree
Showing 20 changed files with 826 additions and 1,724 deletions.
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tseslint from 'typescript-eslint'

export default tseslint.config(
{
ignores: ['**/dist/**/*', '**/esm/**/*'],
ignores: ['esm/**/*', 'dist/**/*', '*.js', '*.mjs', 'example/*'],
},
{
languageOptions: {
Expand Down
17 changes: 9 additions & 8 deletions src/craiIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ type ParsedIndex = Record<string, Slice[] | undefined>
function addRecordToIndex(index: ParsedIndex, record: number[]) {
const [seqId, start, span, containerStart, sliceStart, sliceBytes] = record

if (!index[seqId]) {
index[seqId] = []
const s = seqId!
if (!index[s]) {
index[s] = []
}

index[seqId].push({
start,
span,
containerStart,
sliceStart,
sliceBytes,
index[s].push({
start: start!,
span: span!,
containerStart: containerStart!,
sliceStart: sliceStart!,
sliceBytes: sliceBytes!,
})
}

Expand Down
5 changes: 0 additions & 5 deletions src/cramFile/codecs/byteArrayLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export default class ByteArrayStopCodec extends CramCodec<
) {
super(parameters, dataType)
this.instantiateCodec = instantiateCodec
if (dataType !== 'byteArray') {
throw new TypeError(
`byteArrayLength does not support data type ${dataType}`,
)
}
}

decode(
Expand Down
12 changes: 0 additions & 12 deletions src/cramFile/codecs/byteArrayStop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ export default class ByteArrayStopCodec extends CramCodec<
'byteArray',
ByteArrayStopCramEncoding['parameters']
> {
constructor(
parameters: ByteArrayStopCramEncoding['parameters'],
dataType: 'byteArray',
) {
super(parameters, dataType)
if (dataType !== 'byteArray') {
throw new TypeError(
`byteArrayStop codec does not support data type ${dataType}`,
)
}
}

decode(
slice: CramSlice,
coreDataBlock: CramFileBlock,
Expand Down
2 changes: 1 addition & 1 deletion src/cramFile/codecs/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ export default class ExternalCodec extends CramCodec<
'attempted to read beyond end of block. this file seems truncated.',
)
}
return contentBlock.content[cursor.bytePosition++]
return contentBlock.content[cursor.bytePosition++]!
}
}
2 changes: 1 addition & 1 deletion src/cramFile/codecs/getBits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getBits(
for (let dlen = numBits; dlen; dlen--) {
// get the next `dlen` bits in the input, put them in val
val <<= 1
val |= (data[cursor.bytePosition] >> cursor.bitPosition) & 1
val |= (data[cursor.bytePosition]! >> cursor.bitPosition) & 1
cursor.bitPosition -= 1
if (cursor.bitPosition < 0) {
cursor.bytePosition += 1
Expand Down
22 changes: 11 additions & 11 deletions src/cramFile/codecs/huffman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class HuffmanIntCodec extends CramCodec<

// if this is a degenerate zero-length huffman code, special-case the
// decoding
if (this.sortedCodes[0].bitLength === 0) {
if (this.sortedCodes[0]!.bitLength === 0) {
this._decode = this._decodeZeroLengthCode
}
}
Expand All @@ -58,10 +58,10 @@ export default class HuffmanIntCodec extends CramCodec<
let codes = new Array<{ symbol: number; bitLength: number }>(
this.parameters.numCodes,
)
for (let i = 0; i < this.parameters.numCodes; i += 1) {
for (let i = 0; i < this.parameters.numCodes; i++) {
codes[i] = {
symbol: this.parameters.symbols[i],
bitLength: this.parameters.bitLengths[i],
symbol: this.parameters.symbols[i]!,
bitLength: this.parameters.bitLengths[i]!,
}
}
// sort the codes by bit length and symbol value
Expand All @@ -74,7 +74,7 @@ export default class HuffmanIntCodec extends CramCodec<
if (!this.codeBook[code.bitLength]) {
this.codeBook[code.bitLength] = []
}
this.codeBook[code.bitLength].push(code.symbol)
this.codeBook[code.bitLength]!.push(code.symbol)
})
}

Expand Down Expand Up @@ -117,7 +117,7 @@ export default class HuffmanIntCodec extends CramCodec<

this.bitCodeToValue = new Array(maxBitCode + 1).fill(-1)
for (let i = 0; i < this.sortedBitCodes.length; i += 1) {
this.bitCodeToValue[this.sortedCodes[i].bitCode] = i
this.bitCodeToValue[this.sortedCodes[i]!.bitCode] = i
}
}

Expand All @@ -136,7 +136,7 @@ export default class HuffmanIntCodec extends CramCodec<

// the special case for zero-length codes
_decodeZeroLengthCode() {
return this.sortedCodes[0].value
return this.sortedCodes[0]!.value
}

_decode(slice: CramSlice, coreDataBlock: CramFileBlock, coreCursor: Cursor) {
Expand All @@ -145,19 +145,19 @@ export default class HuffmanIntCodec extends CramCodec<
let prevLen = 0
let bits = 0
for (let i = 0; i < this.sortedCodes.length; i += 1) {
const length = this.sortedCodes[i].bitLength
const length = this.sortedCodes[i]!.bitLength
bits <<= length - prevLen
bits |= getBits(input, coreCursor, length - prevLen)
prevLen = length
{
const index = this.bitCodeToValue[bits]
const index = this.bitCodeToValue[bits]!
if (index > -1 && this.sortedBitLengthsByBitCode[index] === length) {
return this.sortedValuesByBitCode[index]
return this.sortedValuesByBitCode[index]!
}

for (
let j = i;
this.sortedCodes[j + 1].bitLength === length &&
this.sortedCodes[j + 1]!.bitLength === length &&
j < this.sortedCodes.length;
j += 1
) {
Expand Down
50 changes: 26 additions & 24 deletions src/cramFile/container/compressionScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,30 @@ function parseSubstitutionMatrix(byteArray: number[]) {
matrix[i] = new Array(4)
}

matrix[0][(byteArray[0] >> 6) & 3] = 'C'
matrix[0][(byteArray[0] >> 4) & 3] = 'G'
matrix[0][(byteArray[0] >> 2) & 3] = 'T'
matrix[0][(byteArray[0] >> 0) & 3] = 'N'

matrix[1][(byteArray[1] >> 6) & 3] = 'A'
matrix[1][(byteArray[1] >> 4) & 3] = 'G'
matrix[1][(byteArray[1] >> 2) & 3] = 'T'
matrix[1][(byteArray[1] >> 0) & 3] = 'N'

matrix[2][(byteArray[2] >> 6) & 3] = 'A'
matrix[2][(byteArray[2] >> 4) & 3] = 'C'
matrix[2][(byteArray[2] >> 2) & 3] = 'T'
matrix[2][(byteArray[2] >> 0) & 3] = 'N'

matrix[3][(byteArray[3] >> 6) & 3] = 'A'
matrix[3][(byteArray[3] >> 4) & 3] = 'C'
matrix[3][(byteArray[3] >> 2) & 3] = 'G'
matrix[3][(byteArray[3] >> 0) & 3] = 'N'

matrix[4][(byteArray[4] >> 6) & 3] = 'A'
matrix[4][(byteArray[4] >> 4) & 3] = 'C'
matrix[4][(byteArray[4] >> 2) & 3] = 'G'
matrix[4][(byteArray[4] >> 0) & 3] = 'T'
matrix[0]![(byteArray[0]! >> 6) & 3] = 'C'
matrix[0]![(byteArray[0]! >> 4) & 3] = 'G'
matrix[0]![(byteArray[0]! >> 2) & 3] = 'T'
matrix[0]![(byteArray[0]! >> 0) & 3] = 'N'

matrix[1]![(byteArray[1]! >> 6) & 3] = 'A'
matrix[1]![(byteArray[1]! >> 4) & 3] = 'G'
matrix[1]![(byteArray[1]! >> 2) & 3] = 'T'
matrix[1]![(byteArray[1]! >> 0) & 3] = 'N'

matrix[2]![(byteArray[2]! >> 6) & 3] = 'A'
matrix[2]![(byteArray[2]! >> 4) & 3] = 'C'
matrix[2]![(byteArray[2]! >> 2) & 3] = 'T'
matrix[2]![(byteArray[2]! >> 0) & 3] = 'N'

matrix[3]![(byteArray[3]! >> 6) & 3] = 'A'
matrix[3]![(byteArray[3]! >> 4) & 3] = 'C'
matrix[3]![(byteArray[3]! >> 2) & 3] = 'G'
matrix[3]![(byteArray[3]! >> 0) & 3] = 'N'

matrix[4]![(byteArray[4]! >> 6) & 3] = 'A'
matrix[4]![(byteArray[4]! >> 4) & 3] = 'C'
matrix[4]![(byteArray[4]! >> 2) & 3] = 'G'
matrix[4]![(byteArray[4]! >> 0) & 3] = 'T'

return matrix
}
Expand Down Expand Up @@ -145,8 +145,10 @@ export default class CramContainerCompressionScheme {
this.dataSeriesCodecCache[dataSeriesName]
if (r === undefined) {
const encodingData = this.dataSeriesEncoding[dataSeriesName]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (encodingData) {
const dataType = dataSeriesTypes[dataSeriesName]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!dataType) {
throw new CramMalformedError(
`data series name ${dataSeriesName} not defined in file compression header`,
Expand Down
2 changes: 1 addition & 1 deletion src/cramFile/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export default class CramFile {
},
position: number,
size = section.maxLength,
preReadBuffer = undefined,
preReadBuffer?: Buffer,
) {
let buffer: Buffer
if (preReadBuffer) {
Expand Down
13 changes: 3 additions & 10 deletions src/cramFile/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function decodeReadSequence(cramRecord: CramRecord, refRegion: RefRegion) {
let currentReadFeature = 0
while (bases.length < cramRecord.readLength) {
if (currentReadFeature < cramRecord.readFeatures.length) {
const feature = cramRecord.readFeatures[currentReadFeature]
const feature = cramRecord.readFeatures[currentReadFeature]!
if (feature.code === 'Q' || feature.code === 'q') {
currentReadFeature += 1
} else if (feature.pos === bases.length + 1) {
Expand Down Expand Up @@ -90,10 +90,7 @@ function decodeReadSequence(cramRecord: CramRecord, refRegion: RefRegion) {
// put down a chunk of sequence up to the next read feature
const chunk = refRegion.seq.slice(
regionPos,
regionPos +
cramRecord.readFeatures[currentReadFeature].pos -
bases.length -
1,
regionPos + feature.pos - bases.length - 1,
)
bases += chunk
regionPos += chunk.length
Expand Down Expand Up @@ -131,10 +128,6 @@ function decodeBaseSubstitution(
compressionScheme: CramContainerCompressionScheme,
readFeature: ReadFeature,
) {
if (!refRegion) {
return
}

// decode base substitution code using the substitution matrix
const refCoord = readFeature.refPos - refRegion.start
const refBase = refRegion.seq.charAt(refCoord)
Expand All @@ -145,7 +138,7 @@ function decodeBaseSubstitution(
if (baseNumber === undefined) {
baseNumber = 4
}
const substitutionScheme = compressionScheme.substitutionMatrix[baseNumber]
const substitutionScheme = compressionScheme.substitutionMatrix[baseNumber]!
const base = substitutionScheme[readFeature.data]
if (base) {
readFeature.sub = base
Expand Down
8 changes: 4 additions & 4 deletions src/cramFile/sectionParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ export function cramPreservationMap() {
const ents = []
for (let i = 0; i < mapCount; i++) {
const key =
String.fromCharCode(buffer[offset]) +
String.fromCharCode(buffer[offset + 1])
String.fromCharCode(buffer[offset]!) +
String.fromCharCode(buffer[offset + 1]!)
offset += 2

if (
Expand Down Expand Up @@ -550,8 +550,8 @@ function cramDataSeriesEncodingMap() {
const ents = []
for (let i = 0; i < mapCount; i++) {
const key =
String.fromCharCode(buffer[offset]) +
String.fromCharCode(buffer[offset + 1])
String.fromCharCode(buffer[offset]!) +
String.fromCharCode(buffer[offset + 1]!)
offset += 2

const { value, offset: newOffset4 } = cramEncodingSub(buffer, offset)
Expand Down
39 changes: 15 additions & 24 deletions src/cramFile/slice/decodeRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { DataSeriesEncodingKey } from '../codecs/dataSeriesTypes'
function readNullTerminatedString(buffer: Uint8Array) {
let r = ''
for (let i = 0; i < buffer.length && buffer[i] !== 0; i++) {
r += String.fromCharCode(buffer[i])
r += String.fromCharCode(buffer[i]!)
}
return r
}
Expand All @@ -32,46 +32,46 @@ function readNullTerminatedString(buffer: Uint8Array) {
* @private
*/
function parseTagValueArray(buffer: Buffer) {
const arrayType = String.fromCharCode(buffer[0])
const length = Int32Array.from(buffer.slice(1))[0]
const arrayType = String.fromCharCode(buffer[0]!)
const length = Int32Array.from(buffer.slice(1))[0]!

const array: number[] = new Array(length)
buffer = buffer.slice(5)

if (arrayType === 'c') {
const arr = new Int8Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else if (arrayType === 'C') {
const arr = new Uint8Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else if (arrayType === 's') {
const arr = new Int16Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else if (arrayType === 'S') {
const arr = new Uint16Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else if (arrayType === 'i') {
const arr = new Int32Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else if (arrayType === 'I') {
const arr = new Uint32Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else if (arrayType === 'f') {
const arr = new Float32Array(buffer.buffer)
for (let i = 0; i < length; i += 1) {
array[i] = arr[i]
array[i] = arr[i]!
}
} else {
throw new Error(`unknown type: ${arrayType}`)
Expand Down Expand Up @@ -311,26 +311,17 @@ export default function decodeRecord(

const tags: Record<string, any> = {}
// TN = tag names
const TN = compressionScheme.getTagNames(TLindex)
const TN = compressionScheme.getTagNames(TLindex)!
const ntags = TN.length

for (let i = 0; i < ntags; i += 1) {
const tagId = TN[i]
const tagId = TN[i]!
const tagName = tagId.slice(0, 2)
const tagType = tagId.slice(2, 3)

const tagCodec = compressionScheme.getCodecForTag(tagId)
if (!tagCodec) {
throw new CramMalformedError(
`no codec defined for auxiliary tag ${tagId}`,
)
}
const tagData = tagCodec.decode(
slice,
coreDataBlock,
blocksByContentId,
cursors,
)
const tagData = compressionScheme
.getCodecForTag(tagId)
.decode(slice, coreDataBlock, blocksByContentId, cursors)
tags[tagName] = parseTagData(tagType, tagData)
}

Expand Down
Loading

0 comments on commit bcb86f4

Please sign in to comment.