@gmod/tabix 1.5.0 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +3 -2
- package/dist/chunk.js +34 -62
- package/dist/chunk.js.map +1 -0
- package/dist/csi.d.ts +0 -1
- package/dist/csi.js +329 -533
- package/dist/csi.js.map +1 -0
- package/dist/index.js +12 -36
- package/dist/index.js.map +1 -0
- package/dist/indexFile.d.ts +1 -1
- package/dist/indexFile.js +113 -174
- package/dist/indexFile.js.map +1 -0
- package/dist/tabixIndexedFile.d.ts +8 -4
- package/dist/tabixIndexedFile.js +477 -774
- package/dist/tabixIndexedFile.js.map +1 -0
- package/dist/tbi.js +313 -485
- package/dist/tbi.js.map +1 -0
- package/dist/util.js +133 -148
- package/dist/util.js.map +1 -0
- package/dist/virtualOffset.js +45 -68
- package/dist/virtualOffset.js.map +1 -0
- package/esm/chunk.d.ts +18 -0
- package/esm/chunk.js +33 -0
- package/esm/chunk.js.map +1 -0
- package/esm/csi.d.ts +71 -0
- package/esm/csi.js +230 -0
- package/esm/csi.js.map +1 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +5 -0
- package/esm/index.js.map +1 -0
- package/esm/indexFile.d.ts +32 -0
- package/esm/indexFile.js +40 -0
- package/esm/indexFile.js.map +1 -0
- package/esm/tabixIndexedFile.d.ts +118 -0
- package/esm/tabixIndexedFile.js +398 -0
- package/esm/tabixIndexedFile.js.map +1 -0
- package/esm/tbi.d.ts +45 -0
- package/esm/tbi.js +216 -0
- package/esm/tbi.js.map +1 -0
- package/esm/util.d.ts +25 -0
- package/esm/util.js +91 -0
- package/esm/util.js.map +1 -0
- package/esm/virtualOffset.d.ts +10 -0
- package/esm/virtualOffset.js +37 -0
- package/esm/virtualOffset.js.map +1 -0
- package/package.json +27 -44
- package/src/chunk.ts +52 -0
- package/src/csi.ts +277 -0
- package/src/declare.d.ts +2 -0
- package/src/index.ts +5 -0
- package/src/indexFile.ts +79 -0
- package/src/tabixIndexedFile.ts +519 -0
- package/src/tbi.ts +250 -0
- package/src/util.ts +103 -0
- package/src/virtualOffset.ts +47 -0
- package/dist/declare.d.js +0 -2
package/esm/tbi.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tbi.js","sourceRoot":"","sources":["../src/tbi.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,aAAa,EAAE,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,KAAK,MAAM,SAAS,CAAA;AAC3B,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAA;AAC7C,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAA;AACvE,OAAO,SAAsB,MAAM,aAAa,CAAA;AAEhD,MAAM,SAAS,GAAG,QAAQ,CAAA,CAAC,QAAQ;AACnC,MAAM,cAAc,GAAG,EAAE,CAAA;AAEzB;;GAEG;AACH,SAAS,QAAQ,CAAC,GAAW,EAAE,GAAW;IACxC,GAAG,IAAI,CAAC,CAAA,CAAC,8BAA8B;IACvC,GAAG,IAAI,CAAC,CAAA;IACR,OAAO;QACL,CAAC,CAAC,EAAE,CAAC,CAAC;QACN,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;QAClC,CAAC,EAAE,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC;QACpC,CAAC,GAAG,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,GAAG,GAAG,CAAC,GAAG,IAA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GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE;wBACzC,MAAM,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAA;qBAClE;iBACF;aACF;SACF;QAED,0EAA0E;QAC1E,mCAAmC;QACnC,MAAM,KAAK,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAA;QACnC,IAAI,MAAM,GAAG,IAAI,CAAA;QACjB,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAA;QAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAA;QAC7C,KAAK,IAAI,CAAC,GAAG,MAAM,EAAE,CAAC,IAAI,MAAM,EAAE,EAAE,CAAC,EAAE;YACrC,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;YAC5B,IAAI,EAAE,EAAE;gBACN,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;oBACvC,MAAM,GAAG,EAAE,CAAA;iBACZ;aACF;SACF;QAED,OAAO,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACvC,CAAC;CACF"}
|
package/esm/util.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference types="long" />
|
|
2
|
+
import Chunk from './chunk';
|
|
3
|
+
import VirtualOffset from './virtualOffset';
|
|
4
|
+
export declare function longToNumber(long: Long): number;
|
|
5
|
+
/**
|
|
6
|
+
* Properly check if the given AbortSignal is aborted.
|
|
7
|
+
* Per the standard, if the signal reads as aborted,
|
|
8
|
+
* this function throws either a DOMException AbortError, or a regular error
|
|
9
|
+
* with a `code` attribute set to `ERR_ABORTED`.
|
|
10
|
+
*
|
|
11
|
+
* For convenience, passing `undefined` is a no-op
|
|
12
|
+
*
|
|
13
|
+
* @param {AbortSignal} [signal] an AbortSignal, or anything with an `aborted` attribute
|
|
14
|
+
* @returns nothing
|
|
15
|
+
*/
|
|
16
|
+
export declare function checkAbortSignal(signal?: AbortSignal): void;
|
|
17
|
+
/**
|
|
18
|
+
* Skips to the next tick, then runs `checkAbortSignal`.
|
|
19
|
+
* Await this to inside an otherwise synchronous loop to
|
|
20
|
+
* provide a place to break when an abort signal is received.
|
|
21
|
+
* @param {AbortSignal} signal
|
|
22
|
+
*/
|
|
23
|
+
export declare function abortBreakPoint(signal?: AbortSignal): Promise<void>;
|
|
24
|
+
export declare function canMergeBlocks(chunk1: Chunk, chunk2: Chunk): boolean;
|
|
25
|
+
export declare function optimizeChunks(chunks: Chunk[], lowest: VirtualOffset): Chunk[];
|
package/esm/util.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export function longToNumber(long) {
|
|
2
|
+
if (long.greaterThan(Number.MAX_SAFE_INTEGER) ||
|
|
3
|
+
long.lessThan(Number.MIN_SAFE_INTEGER)) {
|
|
4
|
+
throw new Error('integer overflow');
|
|
5
|
+
}
|
|
6
|
+
return long.toNumber();
|
|
7
|
+
}
|
|
8
|
+
class AbortError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Properly check if the given AbortSignal is aborted.
|
|
12
|
+
* Per the standard, if the signal reads as aborted,
|
|
13
|
+
* this function throws either a DOMException AbortError, or a regular error
|
|
14
|
+
* with a `code` attribute set to `ERR_ABORTED`.
|
|
15
|
+
*
|
|
16
|
+
* For convenience, passing `undefined` is a no-op
|
|
17
|
+
*
|
|
18
|
+
* @param {AbortSignal} [signal] an AbortSignal, or anything with an `aborted` attribute
|
|
19
|
+
* @returns nothing
|
|
20
|
+
*/
|
|
21
|
+
export function checkAbortSignal(signal) {
|
|
22
|
+
if (!signal) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (signal.aborted) {
|
|
26
|
+
// console.log('bam aborted!')
|
|
27
|
+
if (typeof DOMException !== 'undefined') {
|
|
28
|
+
// eslint-disable-next-line no-undef
|
|
29
|
+
throw new DOMException('aborted', 'AbortError');
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const e = new AbortError('aborted');
|
|
33
|
+
e.code = 'ERR_ABORTED';
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Skips to the next tick, then runs `checkAbortSignal`.
|
|
40
|
+
* Await this to inside an otherwise synchronous loop to
|
|
41
|
+
* provide a place to break when an abort signal is received.
|
|
42
|
+
* @param {AbortSignal} signal
|
|
43
|
+
*/
|
|
44
|
+
export async function abortBreakPoint(signal) {
|
|
45
|
+
await Promise.resolve();
|
|
46
|
+
checkAbortSignal(signal);
|
|
47
|
+
}
|
|
48
|
+
export function canMergeBlocks(chunk1, chunk2) {
|
|
49
|
+
return (chunk2.minv.blockPosition - chunk1.maxv.blockPosition < 65000 &&
|
|
50
|
+
chunk2.maxv.blockPosition - chunk1.minv.blockPosition < 5000000);
|
|
51
|
+
}
|
|
52
|
+
export function optimizeChunks(chunks, lowest) {
|
|
53
|
+
const mergedChunks = [];
|
|
54
|
+
let lastChunk = null;
|
|
55
|
+
if (chunks.length === 0) {
|
|
56
|
+
return chunks;
|
|
57
|
+
}
|
|
58
|
+
chunks.sort(function (c0, c1) {
|
|
59
|
+
const dif = c0.minv.blockPosition - c1.minv.blockPosition;
|
|
60
|
+
if (dif !== 0) {
|
|
61
|
+
return dif;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return c0.minv.dataPosition - c1.minv.dataPosition;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
chunks.forEach(chunk => {
|
|
68
|
+
if (!lowest || chunk.maxv.compareTo(lowest) > 0) {
|
|
69
|
+
if (lastChunk === null) {
|
|
70
|
+
mergedChunks.push(chunk);
|
|
71
|
+
lastChunk = chunk;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
if (canMergeBlocks(lastChunk, chunk)) {
|
|
75
|
+
if (chunk.maxv.compareTo(lastChunk.maxv) > 0) {
|
|
76
|
+
lastChunk.maxv = chunk.maxv;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
mergedChunks.push(chunk);
|
|
81
|
+
lastChunk = chunk;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// else {
|
|
86
|
+
// console.log(`skipping chunk ${chunk}`)
|
|
87
|
+
// }
|
|
88
|
+
});
|
|
89
|
+
return mergedChunks;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=util.js.map
|
package/esm/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,YAAY,CAAC,IAAU;IACrC,IACE,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,gBAAgB,CAAC;QACzC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,gBAAgB,CAAC,EACtC;QACA,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;KACpC;IACD,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAA;AACxB,CAAC;AAED,MAAM,UAAW,SAAQ,KAAK;CAE7B;AACD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAoB;IACnD,IAAI,CAAC,MAAM,EAAE;QACX,OAAM;KACP;IAED,IAAI,MAAM,CAAC,OAAO,EAAE;QAClB,8BAA8B;QAC9B,IAAI,OAAO,YAAY,KAAK,WAAW,EAAE;YACvC,qCAAqC;YACrC,MAAM,IAAI,YAAY,CAAC,SAAS,EAAE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AM,CAAC,GAAG,CAAC,EAAE;YAC/C,IAAI,SAAS,KAAK,IAAI,EAAE;gBACtB,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;gBACxB,SAAS,GAAG,KAAK,CAAA;aAClB;iBAAM;gBACL,IAAI,cAAc,CAAC,SAAS,EAAE,KAAK,CAAC,EAAE;oBACpC,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;wBAC5C,SAAS,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAA;qBAC5B;iBACF;qBAAM;oBACL,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;oBACxB,SAAS,GAAG,KAAK,CAAA;iBAClB;aACF;SACF;QACD,SAAS;QACT,2CAA2C;QAC3C,IAAI;IACN,CAAC,CAAC,CAAA;IAEF,OAAO,YAAY,CAAA;AACrB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export default class VirtualOffset {
|
|
3
|
+
blockPosition: number;
|
|
4
|
+
dataPosition: number;
|
|
5
|
+
constructor(blockPosition: number, dataPosition: number);
|
|
6
|
+
toString(): string;
|
|
7
|
+
compareTo(b: VirtualOffset): number;
|
|
8
|
+
static min(...args: VirtualOffset[]): VirtualOffset;
|
|
9
|
+
}
|
|
10
|
+
export declare function fromBytes(bytes: Buffer, offset?: number, bigendian?: boolean): VirtualOffset;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export default class VirtualOffset {
|
|
2
|
+
constructor(blockPosition, dataPosition) {
|
|
3
|
+
this.blockPosition = blockPosition; // < offset of the compressed data block
|
|
4
|
+
this.dataPosition = dataPosition; // < offset into the uncompressed data
|
|
5
|
+
}
|
|
6
|
+
toString() {
|
|
7
|
+
return `${this.blockPosition}:${this.dataPosition}`;
|
|
8
|
+
}
|
|
9
|
+
compareTo(b) {
|
|
10
|
+
return (this.blockPosition - b.blockPosition || this.dataPosition - b.dataPosition);
|
|
11
|
+
}
|
|
12
|
+
static min(...args) {
|
|
13
|
+
let min;
|
|
14
|
+
let i = 0;
|
|
15
|
+
for (; !min; i += 1) {
|
|
16
|
+
min = args[i];
|
|
17
|
+
}
|
|
18
|
+
for (; i < args.length; i += 1) {
|
|
19
|
+
if (min.compareTo(args[i]) > 0) {
|
|
20
|
+
min = args[i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return min;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function fromBytes(bytes, offset = 0, bigendian = false) {
|
|
27
|
+
if (bigendian) {
|
|
28
|
+
throw new Error('big-endian virtual file offsets not implemented');
|
|
29
|
+
}
|
|
30
|
+
return new VirtualOffset(bytes[offset + 7] * 0x10000000000 +
|
|
31
|
+
bytes[offset + 6] * 0x100000000 +
|
|
32
|
+
bytes[offset + 5] * 0x1000000 +
|
|
33
|
+
bytes[offset + 4] * 0x10000 +
|
|
34
|
+
bytes[offset + 3] * 0x100 +
|
|
35
|
+
bytes[offset + 2], (bytes[offset + 1] << 8) | bytes[offset]);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=virtualOffset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtualOffset.js","sourceRoot":"","sources":["../src/virtualOffset.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,aAAa;IAGhC,YAAY,aAAqB,EAAE,YAAoB;QACrD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA,CAAC,wCAAwC;QAC3E,IAAI,CAAC,YAAY,GAAG,YAAY,CAAA,CAAC,sCAAsC;IACzE,CAAC;IAED,QAAQ;QACN,OAAO,GAAG,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,YAAY,EAAE,CAAA;IACrD,CAAC;IAED,SAAS,CAAC,CAAgB;QACxB,OAAO,CACL,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC,aAAa,IAAI,IAAI,CA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|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "Read Tabix-indexed files, supports both .tbi and .csi indexes",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "GMOD/tabix-js",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
|
+
"module": "esm/index.js",
|
|
8
9
|
"author": {
|
|
9
10
|
"name": "Robert Buels",
|
|
10
11
|
"email": "rbuels@gmail.com",
|
|
@@ -14,30 +15,23 @@
|
|
|
14
15
|
"node": ">=6"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
|
-
"dist"
|
|
18
|
+
"dist",
|
|
19
|
+
"esm",
|
|
20
|
+
"src"
|
|
18
21
|
],
|
|
19
22
|
"scripts": {
|
|
20
23
|
"test": "jest",
|
|
21
24
|
"coverage": "npm test -- --coverage",
|
|
22
25
|
"lint": "eslint --ext .js,.ts src test",
|
|
23
|
-
"clean": "rimraf dist",
|
|
26
|
+
"clean": "rimraf dist esm",
|
|
24
27
|
"prebuild": "npm run clean && npm run lint",
|
|
25
|
-
"build:
|
|
26
|
-
"build:
|
|
27
|
-
"build": "npm run build:
|
|
28
|
+
"build:esm": "tsc --target es2018 --outDir esm",
|
|
29
|
+
"build:es5": "tsc --target es5 --outDir dist",
|
|
30
|
+
"build": "npm run build:esm && npm run build:es5",
|
|
28
31
|
"preversion": "npm run lint && npm test && npm run build",
|
|
29
|
-
"watch": "npm-watch",
|
|
30
32
|
"prepublishOnly": "npm run lint && npm test && npm run build",
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
},
|
|
34
|
-
"watch": {
|
|
35
|
-
"test": "{src,test}/*.js",
|
|
36
|
-
"lint": "{src,test}/*.js",
|
|
37
|
-
"build": "src"
|
|
38
|
-
},
|
|
39
|
-
"jest": {
|
|
40
|
-
"testEnvironment": "node"
|
|
33
|
+
"version": "standard-changelog && git add CHANGELOG.md",
|
|
34
|
+
"postversion": "git push --follow-tags"
|
|
41
35
|
},
|
|
42
36
|
"keywords": [
|
|
43
37
|
"bionode",
|
|
@@ -45,41 +39,30 @@
|
|
|
45
39
|
"genomics"
|
|
46
40
|
],
|
|
47
41
|
"dependencies": {
|
|
48
|
-
"@babel/runtime-corejs2": "^7.3.1",
|
|
49
42
|
"@gmod/bgzf-filehandle": "^1.3.3",
|
|
50
|
-
"abortable-promise-cache": "^1.
|
|
43
|
+
"abortable-promise-cache": "^1.4.1",
|
|
51
44
|
"es6-promisify": "^6.0.1",
|
|
52
45
|
"generic-filehandle": "^2.0.1",
|
|
53
46
|
"long": "^4.0.0",
|
|
54
|
-
"quick-lru": "^
|
|
47
|
+
"quick-lru": "^4.0.0"
|
|
55
48
|
},
|
|
56
49
|
"devDependencies": {
|
|
57
|
-
"@
|
|
58
|
-
"@babel/core": "^7.3.3",
|
|
59
|
-
"@babel/plugin-transform-runtime": "^7.2.0",
|
|
60
|
-
"@babel/preset-env": "^7.3.1",
|
|
61
|
-
"@babel/preset-flow": "^7.0.0",
|
|
62
|
-
"@babel/preset-typescript": "^7.6.0",
|
|
50
|
+
"@types/jest": "^27.0.3",
|
|
63
51
|
"@types/long": "^4.0.0",
|
|
64
|
-
"@types/node": "^
|
|
52
|
+
"@types/node": "^17.0.25",
|
|
65
53
|
"@types/quick-lru": "2.0.0",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
67
|
-
"@typescript-eslint/parser": "^
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"eslint": "^
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"npm-watch": "^0.5.0",
|
|
79
|
-
"prettier": "^1.11.1",
|
|
80
|
-
"rimraf": "^2.6.2",
|
|
81
|
-
"standard-changelog": "^1.0.0",
|
|
82
|
-
"typescript": "^3.6.3"
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^5.7.0",
|
|
55
|
+
"@typescript-eslint/parser": "^5.7.0",
|
|
56
|
+
"eslint": "^8.13.0",
|
|
57
|
+
"eslint-config-prettier": "^8.3.0",
|
|
58
|
+
"eslint-plugin-import": "^2.25.3",
|
|
59
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
60
|
+
"jest": "^27.4.3",
|
|
61
|
+
"prettier": "^2.5.1",
|
|
62
|
+
"rimraf": "^3.0.2",
|
|
63
|
+
"standard-changelog": "^2.0.27",
|
|
64
|
+
"ts-jest": "^27.0.7",
|
|
65
|
+
"typescript": "^4.5.2"
|
|
83
66
|
},
|
|
84
67
|
"publishConfig": {
|
|
85
68
|
"access": "public"
|
package/src/chunk.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import VirtualOffset from './virtualOffset'
|
|
2
|
+
|
|
3
|
+
// little class representing a chunk in the index
|
|
4
|
+
export default class Chunk {
|
|
5
|
+
public minv: VirtualOffset
|
|
6
|
+
public maxv: VirtualOffset
|
|
7
|
+
public bin: number
|
|
8
|
+
public _fetchedSize?: number
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {VirtualOffset} minv
|
|
12
|
+
* @param {VirtualOffset} maxv
|
|
13
|
+
* @param {number} bin
|
|
14
|
+
* @param {number} [fetchedSize]
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
minv: VirtualOffset,
|
|
18
|
+
maxv: VirtualOffset,
|
|
19
|
+
bin: number,
|
|
20
|
+
fetchedSize = undefined,
|
|
21
|
+
) {
|
|
22
|
+
this.minv = minv
|
|
23
|
+
this.maxv = maxv
|
|
24
|
+
this.bin = bin
|
|
25
|
+
this._fetchedSize = fetchedSize
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
toUniqueString() {
|
|
29
|
+
return `${this.minv}..${this.maxv} (bin ${
|
|
30
|
+
this.bin
|
|
31
|
+
}, fetchedSize ${this.fetchedSize()})`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
toString() {
|
|
35
|
+
return this.toUniqueString()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
compareTo(b: Chunk) {
|
|
39
|
+
return (
|
|
40
|
+
this.minv.compareTo(b.minv) ||
|
|
41
|
+
this.maxv.compareTo(b.maxv) ||
|
|
42
|
+
this.bin - b.bin
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fetchedSize() {
|
|
47
|
+
if (this._fetchedSize !== undefined) {
|
|
48
|
+
return this._fetchedSize
|
|
49
|
+
}
|
|
50
|
+
return this.maxv.blockPosition + (1 << 16) - this.minv.blockPosition
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/csi.ts
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import Long from 'long'
|
|
2
|
+
import { unzip } from '@gmod/bgzf-filehandle'
|
|
3
|
+
|
|
4
|
+
import VirtualOffset, { fromBytes } from './virtualOffset'
|
|
5
|
+
import Chunk from './chunk'
|
|
6
|
+
import { longToNumber, optimizeChunks } from './util'
|
|
7
|
+
|
|
8
|
+
import IndexFile, { Options } from './indexFile'
|
|
9
|
+
|
|
10
|
+
const CSI1_MAGIC = 21582659 // CSI\1
|
|
11
|
+
const CSI2_MAGIC = 38359875 // CSI\2
|
|
12
|
+
|
|
13
|
+
function lshift(num: number, bits: number) {
|
|
14
|
+
return num * 2 ** bits
|
|
15
|
+
}
|
|
16
|
+
function rshift(num: number, bits: number) {
|
|
17
|
+
return Math.floor(num / 2 ** bits)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default class CSI extends IndexFile {
|
|
21
|
+
private maxBinNumber: number
|
|
22
|
+
private depth: number
|
|
23
|
+
private minShift: number
|
|
24
|
+
constructor(args: any) {
|
|
25
|
+
super(args)
|
|
26
|
+
this.maxBinNumber = 0
|
|
27
|
+
this.depth = 0
|
|
28
|
+
this.minShift = 0
|
|
29
|
+
}
|
|
30
|
+
async lineCount(refName: string, opts: Options = {}): Promise<number> {
|
|
31
|
+
const indexData = await this.parse(opts)
|
|
32
|
+
if (!indexData) {
|
|
33
|
+
return -1
|
|
34
|
+
}
|
|
35
|
+
const refId = indexData.refNameToId[refName]
|
|
36
|
+
const idx = indexData.indices[refId]
|
|
37
|
+
if (!idx) {
|
|
38
|
+
return -1
|
|
39
|
+
}
|
|
40
|
+
const { stats } = indexData.indices[refId]
|
|
41
|
+
if (stats) {
|
|
42
|
+
return stats.lineCount
|
|
43
|
+
}
|
|
44
|
+
return -1
|
|
45
|
+
}
|
|
46
|
+
async indexCov() {
|
|
47
|
+
throw new Error('CSI indexes do not support indexcov')
|
|
48
|
+
return []
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
parseAuxData(bytes: Buffer, offset: number, auxLength: number) {
|
|
52
|
+
if (auxLength < 30) {
|
|
53
|
+
return {
|
|
54
|
+
refIdToName: [],
|
|
55
|
+
refNameToId: {},
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const formatFlags = bytes.readInt32LE(offset)
|
|
60
|
+
const coordinateType =
|
|
61
|
+
formatFlags & 0x10000 ? 'zero-based-half-open' : '1-based-closed'
|
|
62
|
+
const format = (
|
|
63
|
+
{ 0: 'generic', 1: 'SAM', 2: 'VCF' } as {
|
|
64
|
+
[key: number]: string
|
|
65
|
+
}
|
|
66
|
+
)[formatFlags & 0xf]
|
|
67
|
+
if (!format) {
|
|
68
|
+
throw new Error(`invalid Tabix preset format flags ${formatFlags}`)
|
|
69
|
+
}
|
|
70
|
+
const columnNumbers = {
|
|
71
|
+
ref: bytes.readInt32LE(offset + 4),
|
|
72
|
+
start: bytes.readInt32LE(offset + 8),
|
|
73
|
+
end: bytes.readInt32LE(offset + 12),
|
|
74
|
+
}
|
|
75
|
+
const metaValue = bytes.readInt32LE(offset + 16)
|
|
76
|
+
const metaChar = metaValue ? String.fromCharCode(metaValue) : ''
|
|
77
|
+
const skipLines = bytes.readInt32LE(offset + 20)
|
|
78
|
+
const nameSectionLength = bytes.readInt32LE(offset + 24)
|
|
79
|
+
|
|
80
|
+
const { refIdToName, refNameToId } = this._parseNameBytes(
|
|
81
|
+
bytes.slice(offset + 28, offset + 28 + nameSectionLength),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
refIdToName,
|
|
86
|
+
refNameToId,
|
|
87
|
+
skipLines,
|
|
88
|
+
metaChar,
|
|
89
|
+
columnNumbers,
|
|
90
|
+
format,
|
|
91
|
+
coordinateType,
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
_parseNameBytes(namesBytes: Buffer) {
|
|
96
|
+
let currRefId = 0
|
|
97
|
+
let currNameStart = 0
|
|
98
|
+
const refIdToName = []
|
|
99
|
+
const refNameToId: { [key: string]: number } = {}
|
|
100
|
+
for (let i = 0; i < namesBytes.length; i += 1) {
|
|
101
|
+
if (!namesBytes[i]) {
|
|
102
|
+
if (currNameStart < i) {
|
|
103
|
+
let refName = namesBytes.toString('utf8', currNameStart, i)
|
|
104
|
+
refName = this.renameRefSeq(refName)
|
|
105
|
+
refIdToName[currRefId] = refName
|
|
106
|
+
refNameToId[refName] = currRefId
|
|
107
|
+
}
|
|
108
|
+
currNameStart = i + 1
|
|
109
|
+
currRefId += 1
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { refNameToId, refIdToName }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// fetch and parse the index
|
|
116
|
+
|
|
117
|
+
async _parse(opts: Options = {}) {
|
|
118
|
+
const bytes = await unzip((await this.filehandle.readFile(opts)) as Buffer)
|
|
119
|
+
|
|
120
|
+
// check TBI magic numbers
|
|
121
|
+
let csiVersion
|
|
122
|
+
if (bytes.readUInt32LE(0) === CSI1_MAGIC) {
|
|
123
|
+
csiVersion = 1
|
|
124
|
+
} else if (bytes.readUInt32LE(0) === CSI2_MAGIC) {
|
|
125
|
+
csiVersion = 2
|
|
126
|
+
} else {
|
|
127
|
+
throw new Error('Not a CSI file')
|
|
128
|
+
// TODO: do we need to support big-endian CSI files?
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.minShift = bytes.readInt32LE(4)
|
|
132
|
+
this.depth = bytes.readInt32LE(8)
|
|
133
|
+
this.maxBinNumber = ((1 << ((this.depth + 1) * 3)) - 1) / 7
|
|
134
|
+
const maxRefLength = 2 ** (this.minShift + this.depth * 3)
|
|
135
|
+
|
|
136
|
+
const auxLength = bytes.readInt32LE(12)
|
|
137
|
+
let aux: {
|
|
138
|
+
refIdToName: string[]
|
|
139
|
+
refNameToId: { [key: string]: number }
|
|
140
|
+
} = {
|
|
141
|
+
refIdToName: [],
|
|
142
|
+
refNameToId: {},
|
|
143
|
+
}
|
|
144
|
+
if (auxLength) {
|
|
145
|
+
aux = this.parseAuxData(bytes, 16, auxLength)
|
|
146
|
+
}
|
|
147
|
+
const refCount = bytes.readInt32LE(16 + auxLength)
|
|
148
|
+
|
|
149
|
+
// read the indexes for each reference sequence
|
|
150
|
+
let firstDataLine: VirtualOffset | undefined
|
|
151
|
+
let currOffset = 16 + auxLength + 4
|
|
152
|
+
const indices = new Array(refCount).fill(0).map(() => {
|
|
153
|
+
// the binning index
|
|
154
|
+
const binCount = bytes.readInt32LE(currOffset)
|
|
155
|
+
currOffset += 4
|
|
156
|
+
const binIndex: { [key: string]: Chunk[] } = {}
|
|
157
|
+
let stats // < provided by parsing a pseudo-bin, if present
|
|
158
|
+
for (let j = 0; j < binCount; j += 1) {
|
|
159
|
+
const bin = bytes.readUInt32LE(currOffset)
|
|
160
|
+
if (bin > this.maxBinNumber) {
|
|
161
|
+
// this is a fake bin that actually has stats information
|
|
162
|
+
// about the reference sequence in it
|
|
163
|
+
stats = this.parsePseudoBin(bytes, currOffset + 4)
|
|
164
|
+
currOffset += 4 + 8 + 4 + 16 + 16
|
|
165
|
+
} else {
|
|
166
|
+
const loffset = fromBytes(bytes, currOffset + 4)
|
|
167
|
+
firstDataLine = this._findFirstData(firstDataLine, loffset)
|
|
168
|
+
const chunkCount = bytes.readInt32LE(currOffset + 12)
|
|
169
|
+
currOffset += 16
|
|
170
|
+
const chunks = new Array(chunkCount)
|
|
171
|
+
for (let k = 0; k < chunkCount; k += 1) {
|
|
172
|
+
const u = fromBytes(bytes, currOffset)
|
|
173
|
+
const v = fromBytes(bytes, currOffset + 8)
|
|
174
|
+
currOffset += 16
|
|
175
|
+
// this._findFirstData(data, u)
|
|
176
|
+
chunks[k] = new Chunk(u, v, bin)
|
|
177
|
+
}
|
|
178
|
+
binIndex[bin] = chunks
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return { binIndex, stats }
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
...aux,
|
|
187
|
+
csi: true,
|
|
188
|
+
refCount,
|
|
189
|
+
maxBlockSize: 1 << 16,
|
|
190
|
+
firstDataLine,
|
|
191
|
+
csiVersion,
|
|
192
|
+
indices,
|
|
193
|
+
depth: this.depth,
|
|
194
|
+
maxBinNumber: this.maxBinNumber,
|
|
195
|
+
maxRefLength,
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
parsePseudoBin(bytes: Buffer, offset: number) {
|
|
200
|
+
const lineCount = longToNumber(
|
|
201
|
+
Long.fromBytesLE(
|
|
202
|
+
Array.prototype.slice.call(bytes, offset + 28, offset + 36),
|
|
203
|
+
true,
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
return { lineCount }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async blocksForRange(
|
|
210
|
+
refName: string,
|
|
211
|
+
min: number,
|
|
212
|
+
max: number,
|
|
213
|
+
opts: Options = {},
|
|
214
|
+
) {
|
|
215
|
+
if (min < 0) {
|
|
216
|
+
min = 0
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const indexData = await this.parse(opts)
|
|
220
|
+
if (!indexData) {
|
|
221
|
+
return []
|
|
222
|
+
}
|
|
223
|
+
const refId = indexData.refNameToId[refName]
|
|
224
|
+
const ba = indexData.indices[refId]
|
|
225
|
+
if (!ba) {
|
|
226
|
+
return []
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// const { linearIndex, binIndex } = indexes
|
|
230
|
+
|
|
231
|
+
const overlappingBins = this.reg2bins(min, max) // List of bin #s that overlap min, max
|
|
232
|
+
const chunks: Chunk[] = []
|
|
233
|
+
|
|
234
|
+
// Find chunks in overlapping bins. Leaf bins (< 4681) are not pruned
|
|
235
|
+
for (const [start, end] of overlappingBins) {
|
|
236
|
+
for (let bin = start; bin <= end; bin++) {
|
|
237
|
+
if (ba.binIndex[bin]) {
|
|
238
|
+
const binChunks = ba.binIndex[bin]
|
|
239
|
+
for (let c = 0; c < binChunks.length; ++c) {
|
|
240
|
+
chunks.push(new Chunk(binChunks[c].minv, binChunks[c].maxv, bin))
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return optimizeChunks(chunks, new VirtualOffset(0, 0))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* calculate the list of bins that may overlap with region [beg,end) (zero-based half-open)
|
|
251
|
+
*/
|
|
252
|
+
reg2bins(beg: number, end: number) {
|
|
253
|
+
beg -= 1 // < convert to 1-based closed
|
|
254
|
+
if (beg < 1) {
|
|
255
|
+
beg = 1
|
|
256
|
+
}
|
|
257
|
+
if (end > 2 ** 50) {
|
|
258
|
+
end = 2 ** 34
|
|
259
|
+
} // 17 GiB ought to be enough for anybody
|
|
260
|
+
end -= 1
|
|
261
|
+
let l = 0
|
|
262
|
+
let t = 0
|
|
263
|
+
let s = this.minShift + this.depth * 3
|
|
264
|
+
const bins = []
|
|
265
|
+
for (; l <= this.depth; s -= 3, t += lshift(1, l * 3), l += 1) {
|
|
266
|
+
const b = t + rshift(beg, s)
|
|
267
|
+
const e = t + rshift(end, s)
|
|
268
|
+
if (e - b + bins.length > this.maxBinNumber) {
|
|
269
|
+
throw new Error(
|
|
270
|
+
`query ${beg}-${end} is too large for current binning scheme (shift ${this.minShift}, depth ${this.depth}), try a smaller query or a coarser index binning scheme`,
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
bins.push([b, e])
|
|
274
|
+
}
|
|
275
|
+
return bins
|
|
276
|
+
}
|
|
277
|
+
}
|
package/src/declare.d.ts
ADDED