@gmod/cram 3.0.7 → 4.0.1
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/dist/craiIndex.js +70 -87
- package/dist/craiIndex.js.map +1 -1
- package/dist/cram-bundle.js +1 -1
- package/dist/cram-bundle.js.LICENSE.txt +0 -9
- package/dist/cramFile/codecs/_base.d.ts +1 -1
- package/dist/cramFile/codecs/beta.d.ts +2 -2
- package/dist/cramFile/codecs/beta.js +1 -1
- package/dist/cramFile/codecs/beta.js.map +1 -1
- package/dist/cramFile/codecs/byteArrayLength.d.ts +2 -2
- package/dist/cramFile/codecs/byteArrayStop.d.ts +4 -4
- package/dist/cramFile/codecs/byteArrayStop.js +1 -1
- package/dist/cramFile/codecs/byteArrayStop.js.map +1 -1
- package/dist/cramFile/codecs/external.d.ts +1 -1
- package/dist/cramFile/codecs/external.js +1 -1
- package/dist/cramFile/codecs/external.js.map +1 -1
- package/dist/cramFile/codecs/gamma.d.ts +1 -1
- package/dist/cramFile/codecs/gamma.js +1 -1
- package/dist/cramFile/codecs/gamma.js.map +1 -1
- package/dist/cramFile/codecs/getBits.d.ts +1 -1
- package/dist/cramFile/codecs/getBits.js.map +1 -1
- package/dist/cramFile/codecs/huffman.d.ts +1 -1
- package/dist/cramFile/codecs/huffman.js +1 -1
- package/dist/cramFile/codecs/huffman.js.map +1 -1
- package/dist/cramFile/codecs/index.d.ts +1 -1
- package/dist/cramFile/codecs/index.js +5 -5
- package/dist/cramFile/codecs/index.js.map +1 -1
- package/dist/cramFile/codecs/subexp.d.ts +2 -2
- package/dist/cramFile/codecs/subexp.js +1 -1
- package/dist/cramFile/codecs/subexp.js.map +1 -1
- package/dist/cramFile/container/compressionScheme.d.ts +2 -2
- package/dist/cramFile/container/compressionScheme.js +1 -1
- package/dist/cramFile/container/compressionScheme.js.map +1 -1
- package/dist/cramFile/container/index.d.ts +1 -2
- package/dist/cramFile/container/index.js +62 -79
- package/dist/cramFile/container/index.js.map +1 -1
- package/dist/cramFile/file.d.ts +9 -13
- package/dist/cramFile/file.js +218 -247
- package/dist/cramFile/file.js.map +1 -1
- package/dist/cramFile/record.js +2 -2
- package/dist/cramFile/record.js.map +1 -1
- package/dist/cramFile/sectionParsers.d.ts +16 -16
- package/dist/cramFile/sectionParsers.js +12 -9
- package/dist/cramFile/sectionParsers.js.map +1 -1
- package/dist/cramFile/slice/decodeRecord.d.ts +3 -3
- package/dist/cramFile/slice/decodeRecord.js +11 -11
- package/dist/cramFile/slice/decodeRecord.js.map +1 -1
- package/dist/cramFile/slice/index.d.ts +2 -2
- package/dist/cramFile/slice/index.js +241 -262
- package/dist/cramFile/slice/index.js.map +1 -1
- package/dist/cramFile/util.d.ts +4 -2
- package/dist/cramFile/util.js +29 -6
- package/dist/cramFile/util.js.map +1 -1
- package/dist/htscodecs/arith_gen.d.ts +9 -17
- package/dist/htscodecs/arith_gen.js +32 -272
- package/dist/htscodecs/arith_gen.js.map +1 -1
- package/dist/htscodecs/fqzcomp.d.ts +1 -2
- package/dist/htscodecs/fqzcomp.js +2 -421
- package/dist/htscodecs/fqzcomp.js.map +1 -1
- package/dist/htscodecs/index.d.ts +5 -5
- package/dist/htscodecs/index.js +10 -10
- package/dist/htscodecs/index.js.map +1 -1
- package/dist/htscodecs/iostream.d.ts +2 -1
- package/dist/htscodecs/iostream.js +3 -2
- package/dist/htscodecs/iostream.js.map +1 -1
- package/dist/htscodecs/rans.d.ts +1 -2
- package/dist/htscodecs/rans.js +3 -270
- package/dist/htscodecs/rans.js.map +1 -1
- package/dist/htscodecs/rans4x16.d.ts +0 -1
- package/dist/htscodecs/rans4x16.js +6 -497
- package/dist/htscodecs/rans4x16.js.map +1 -1
- package/dist/htscodecs/tok3.js +17 -1
- package/dist/htscodecs/tok3.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/indexedCramFile.d.ts +4 -4
- package/dist/indexedCramFile.js +97 -108
- package/dist/indexedCramFile.js.map +1 -1
- package/dist/io/index.d.ts +3 -3
- package/dist/io/index.js +6 -6
- package/dist/io/index.js.map +1 -1
- package/dist/rans/constants.d.ts +1 -1
- package/dist/rans/constants.js +1 -1
- package/dist/rans/constants.js.map +1 -1
- package/dist/rans/d04.js.map +1 -1
- package/dist/rans/decoding.js.map +1 -1
- package/dist/rans/frequencies.js.map +1 -1
- package/dist/rans/index.js +5 -5
- package/dist/rans/index.js.map +1 -1
- package/dist/unzip.d.ts +1 -1
- package/dist/unzip.js +5 -3
- package/dist/unzip.js.map +1 -1
- package/esm/craiIndex.js +10 -12
- package/esm/craiIndex.js.map +1 -1
- package/esm/cramFile/codecs/_base.d.ts +1 -1
- package/esm/cramFile/codecs/beta.d.ts +2 -2
- package/esm/cramFile/codecs/beta.js +1 -1
- package/esm/cramFile/codecs/beta.js.map +1 -1
- package/esm/cramFile/codecs/byteArrayLength.d.ts +2 -2
- package/esm/cramFile/codecs/byteArrayStop.d.ts +4 -4
- package/esm/cramFile/codecs/byteArrayStop.js +1 -1
- package/esm/cramFile/codecs/byteArrayStop.js.map +1 -1
- package/esm/cramFile/codecs/external.d.ts +1 -1
- package/esm/cramFile/codecs/external.js +1 -1
- package/esm/cramFile/codecs/external.js.map +1 -1
- package/esm/cramFile/codecs/gamma.d.ts +1 -1
- package/esm/cramFile/codecs/gamma.js +1 -1
- package/esm/cramFile/codecs/gamma.js.map +1 -1
- package/esm/cramFile/codecs/getBits.d.ts +1 -1
- package/esm/cramFile/codecs/getBits.js.map +1 -1
- package/esm/cramFile/codecs/huffman.d.ts +1 -1
- package/esm/cramFile/codecs/huffman.js +1 -1
- package/esm/cramFile/codecs/huffman.js.map +1 -1
- package/esm/cramFile/codecs/index.d.ts +1 -1
- package/esm/cramFile/codecs/index.js +5 -5
- package/esm/cramFile/codecs/index.js.map +1 -1
- package/esm/cramFile/codecs/subexp.d.ts +2 -2
- package/esm/cramFile/codecs/subexp.js +1 -1
- package/esm/cramFile/codecs/subexp.js.map +1 -1
- package/esm/cramFile/container/compressionScheme.d.ts +2 -2
- package/esm/cramFile/container/compressionScheme.js +1 -1
- package/esm/cramFile/container/compressionScheme.js.map +1 -1
- package/esm/cramFile/container/index.d.ts +1 -2
- package/esm/cramFile/container/index.js +4 -7
- package/esm/cramFile/container/index.js.map +1 -1
- package/esm/cramFile/file.d.ts +9 -13
- package/esm/cramFile/file.js +47 -52
- package/esm/cramFile/file.js.map +1 -1
- package/esm/cramFile/record.js +2 -2
- package/esm/cramFile/record.js.map +1 -1
- package/esm/cramFile/sectionParsers.d.ts +16 -16
- package/esm/cramFile/sectionParsers.js +7 -7
- package/esm/cramFile/sectionParsers.js.map +1 -1
- package/esm/cramFile/slice/decodeRecord.d.ts +3 -3
- package/esm/cramFile/slice/decodeRecord.js +11 -11
- package/esm/cramFile/slice/decodeRecord.js.map +1 -1
- package/esm/cramFile/slice/index.d.ts +2 -2
- package/esm/cramFile/slice/index.js +7 -6
- package/esm/cramFile/slice/index.js.map +1 -1
- package/esm/cramFile/util.d.ts +4 -2
- package/esm/cramFile/util.js +19 -2
- package/esm/cramFile/util.js.map +1 -1
- package/esm/htscodecs/arith_gen.d.ts +9 -17
- package/esm/htscodecs/arith_gen.js +32 -272
- package/esm/htscodecs/arith_gen.js.map +1 -1
- package/esm/htscodecs/fqzcomp.d.ts +1 -2
- package/esm/htscodecs/fqzcomp.js +2 -421
- package/esm/htscodecs/fqzcomp.js.map +1 -1
- package/esm/htscodecs/index.d.ts +5 -5
- package/esm/htscodecs/index.js +10 -10
- package/esm/htscodecs/index.js.map +1 -1
- package/esm/htscodecs/iostream.d.ts +2 -1
- package/esm/htscodecs/iostream.js +3 -2
- package/esm/htscodecs/iostream.js.map +1 -1
- package/esm/htscodecs/rans.d.ts +1 -2
- package/esm/htscodecs/rans.js +3 -270
- package/esm/htscodecs/rans.js.map +1 -1
- package/esm/htscodecs/rans4x16.d.ts +0 -1
- package/esm/htscodecs/rans4x16.js +6 -497
- package/esm/htscodecs/rans4x16.js.map +1 -1
- package/esm/htscodecs/tok3.js +17 -1
- package/esm/htscodecs/tok3.js.map +1 -1
- package/esm/index.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/indexedCramFile.d.ts +4 -4
- package/esm/indexedCramFile.js +10 -10
- package/esm/indexedCramFile.js.map +1 -1
- package/esm/io/index.d.ts +3 -3
- package/esm/io/index.js +2 -2
- package/esm/io/index.js.map +1 -1
- package/esm/rans/constants.d.ts +1 -1
- package/esm/rans/constants.js +1 -1
- package/esm/rans/constants.js.map +1 -1
- package/esm/rans/d04.js.map +1 -1
- package/esm/rans/decoding.js.map +1 -1
- package/esm/rans/frequencies.js.map +1 -1
- package/esm/rans/index.js +5 -5
- package/esm/rans/index.js.map +1 -1
- package/esm/unzip.d.ts +1 -1
- package/esm/unzip.js +4 -1
- package/esm/unzip.js.map +1 -1
- package/package.json +7 -9
- package/src/craiIndex.ts +17 -16
- package/src/cramFile/codecs/_base.ts +1 -1
- package/src/cramFile/codecs/beta.ts +3 -3
- package/src/cramFile/codecs/byteArrayLength.ts +1 -1
- package/src/cramFile/codecs/byteArrayStop.ts +3 -4
- package/src/cramFile/codecs/external.ts +3 -3
- package/src/cramFile/codecs/gamma.ts +2 -2
- package/src/cramFile/codecs/getBits.ts +1 -1
- package/src/cramFile/codecs/huffman.ts +2 -3
- package/src/cramFile/codecs/index.ts +7 -8
- package/src/cramFile/codecs/subexp.ts +3 -3
- package/src/cramFile/container/compressionScheme.ts +3 -3
- package/src/cramFile/container/index.ts +7 -10
- package/src/cramFile/file.ts +71 -77
- package/src/cramFile/record.ts +3 -3
- package/src/cramFile/sectionParsers.ts +27 -22
- package/src/cramFile/slice/decodeRecord.ts +17 -17
- package/src/cramFile/slice/index.ts +8 -9
- package/src/cramFile/util.ts +24 -5
- package/src/htscodecs/arith_gen.js +33 -296
- package/src/htscodecs/fqzcomp.js +2 -476
- package/src/htscodecs/index.js +9 -11
- package/src/htscodecs/iostream.js +3 -2
- package/src/htscodecs/rans.js +3 -315
- package/src/htscodecs/rans4x16.js +6 -557
- package/src/htscodecs/tok3.js +18 -1
- package/src/index.ts +1 -1
- package/src/indexedCramFile.ts +6 -5
- package/src/io/index.ts +6 -5
- package/src/rans/constants.ts +1 -1
- package/src/rans/d04.ts +0 -1
- package/src/rans/decoding.ts +0 -1
- package/src/rans/frequencies.ts +0 -1
- package/src/rans/index.ts +5 -7
- package/src/unzip.ts +5 -1
- package/dist/cramFile/filehandle.d.ts +0 -1
- package/dist/cramFile/filehandle.js +0 -3
- package/dist/cramFile/filehandle.js.map +0 -1
- package/dist/htscodecs/main_arith_gen.d.ts +0 -1
- package/dist/htscodecs/main_arith_gen.js +0 -86
- package/dist/htscodecs/main_arith_gen.js.map +0 -1
- package/dist/htscodecs/main_fqzcomp.d.ts +0 -1
- package/dist/htscodecs/main_fqzcomp.js +0 -112
- package/dist/htscodecs/main_fqzcomp.js.map +0 -1
- package/dist/htscodecs/main_rans.d.ts +0 -1
- package/dist/htscodecs/main_rans.js +0 -83
- package/dist/htscodecs/main_rans.js.map +0 -1
- package/dist/htscodecs/main_rans4x16.d.ts +0 -1
- package/dist/htscodecs/main_rans4x16.js +0 -82
- package/dist/htscodecs/main_rans4x16.js.map +0 -1
- package/dist/htscodecs/main_tok3.d.ts +0 -1
- package/dist/htscodecs/main_tok3.js +0 -84
- package/dist/htscodecs/main_tok3.js.map +0 -1
- package/dist/unzip-pako.d.ts +0 -2
- package/dist/unzip-pako.js +0 -9
- package/dist/unzip-pako.js.map +0 -1
- package/esm/cramFile/filehandle.d.ts +0 -1
- package/esm/cramFile/filehandle.js +0 -2
- package/esm/cramFile/filehandle.js.map +0 -1
- package/esm/htscodecs/main_arith_gen.d.ts +0 -1
- package/esm/htscodecs/main_arith_gen.js +0 -86
- package/esm/htscodecs/main_arith_gen.js.map +0 -1
- package/esm/htscodecs/main_fqzcomp.d.ts +0 -1
- package/esm/htscodecs/main_fqzcomp.js +0 -112
- package/esm/htscodecs/main_fqzcomp.js.map +0 -1
- package/esm/htscodecs/main_rans.d.ts +0 -1
- package/esm/htscodecs/main_rans.js +0 -83
- package/esm/htscodecs/main_rans.js.map +0 -1
- package/esm/htscodecs/main_rans4x16.d.ts +0 -1
- package/esm/htscodecs/main_rans4x16.js +0 -82
- package/esm/htscodecs/main_rans4x16.js.map +0 -1
- package/esm/htscodecs/main_tok3.d.ts +0 -1
- package/esm/htscodecs/main_tok3.js +0 -84
- package/esm/htscodecs/main_tok3.js.map +0 -1
- package/esm/unzip-pako.d.ts +0 -2
- package/esm/unzip-pako.js +0 -6
- package/esm/unzip-pako.js.map +0 -1
- package/src/cramFile/filehandle.ts +0 -1
- package/src/htscodecs/Makefile +0 -142
- package/src/htscodecs/README.md +0 -64
- package/src/htscodecs/main_arith_gen.js +0 -96
- package/src/htscodecs/main_fqzcomp.js +0 -113
- package/src/htscodecs/main_rans.js +0 -88
- package/src/htscodecs/main_rans4x16.js +0 -87
- package/src/htscodecs/main_tok3.js +0 -86
- package/src/unzip-pako.ts +0 -6
package/dist/cramFile/file.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
1
|
import QuickLRU from 'quick-lru';
|
|
3
|
-
import { BlockHeader, CompressionMethod } from './sectionParsers';
|
|
4
2
|
import CramContainer from './container';
|
|
5
3
|
import CramRecord from './record';
|
|
6
|
-
import {
|
|
4
|
+
import { BlockHeader, CompressionMethod } from './sectionParsers';
|
|
5
|
+
import type { GenericFilehandle } from 'generic-filehandle2';
|
|
7
6
|
export interface CramFileSource {
|
|
8
|
-
filehandle?:
|
|
7
|
+
filehandle?: GenericFilehandle;
|
|
9
8
|
url?: string;
|
|
10
9
|
path?: string;
|
|
11
10
|
}
|
|
@@ -19,7 +18,7 @@ export type CramFileBlock = BlockHeader & {
|
|
|
19
18
|
_endPosition: number;
|
|
20
19
|
contentPosition: number;
|
|
21
20
|
_size: number;
|
|
22
|
-
content:
|
|
21
|
+
content: Uint8Array;
|
|
23
22
|
crc32?: number;
|
|
24
23
|
};
|
|
25
24
|
export default class CramFile {
|
|
@@ -33,11 +32,8 @@ export default class CramFile {
|
|
|
33
32
|
featureCache: QuickLRU<string, Promise<CramRecord[]>>;
|
|
34
33
|
private header;
|
|
35
34
|
constructor(args: CramFileArgs);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
buffer: Buffer;
|
|
39
|
-
}>;
|
|
40
|
-
stat(): Promise<import("generic-filehandle").Stats>;
|
|
35
|
+
stat(): Promise<import("generic-filehandle2").Stats>;
|
|
36
|
+
read(length: number, position: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
41
37
|
getDefinition(): Promise<{
|
|
42
38
|
magic: string;
|
|
43
39
|
majorVersion: number;
|
|
@@ -71,14 +67,14 @@ export default class CramFile {
|
|
|
71
67
|
}) | undefined>;
|
|
72
68
|
_parseSection<T>(section: {
|
|
73
69
|
maxLength: number;
|
|
74
|
-
parser: (buffer:
|
|
70
|
+
parser: (buffer: Uint8Array, offset: number) => {
|
|
75
71
|
offset: number;
|
|
76
72
|
value: T;
|
|
77
73
|
};
|
|
78
|
-
}, position: number, size?: number, preReadBuffer?:
|
|
74
|
+
}, position: number, size?: number, preReadBuffer?: Uint8Array): Promise<(T & {
|
|
79
75
|
_endPosition: number;
|
|
80
76
|
_size: number;
|
|
81
77
|
}) | undefined>;
|
|
82
|
-
_uncompress(compressionMethod: CompressionMethod, inputBuffer:
|
|
78
|
+
_uncompress(compressionMethod: CompressionMethod, inputBuffer: Uint8Array, uncompressedSize: number): Promise<any>;
|
|
83
79
|
readBlock(position: number): Promise<CramFileBlock | undefined>;
|
|
84
80
|
}
|
package/dist/cramFile/file.js
CHANGED
|
@@ -1,32 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const buffer_1 = require("buffer");
|
|
16
|
-
const crc32_1 = __importDefault(require("crc/crc32"));
|
|
17
|
-
const quick_lru_1 = __importDefault(require("quick-lru"));
|
|
18
|
-
const htscodecs_1 = __importDefault(require("../htscodecs"));
|
|
19
6
|
const bzip2_1 = __importDefault(require("bzip2"));
|
|
7
|
+
const crc32_1 = __importDefault(require("crc/calculators/crc32"));
|
|
8
|
+
const quick_lru_1 = __importDefault(require("quick-lru"));
|
|
20
9
|
const xz_decompress_1 = require("xz-decompress");
|
|
21
10
|
const errors_1 = require("../errors");
|
|
22
|
-
|
|
23
|
-
const
|
|
11
|
+
const htscodecs_1 = __importDefault(require("../htscodecs"));
|
|
12
|
+
const io_1 = require("../io");
|
|
24
13
|
const rans_1 = __importDefault(require("../rans"));
|
|
25
|
-
const
|
|
14
|
+
const sam_1 = require("../sam");
|
|
15
|
+
const unzip_1 = require("../unzip");
|
|
26
16
|
const container_1 = __importDefault(require("./container"));
|
|
27
|
-
const
|
|
17
|
+
const sectionParsers_1 = require("./sectionParsers");
|
|
28
18
|
const util_1 = require("./util");
|
|
29
|
-
const sam_1 = require("../sam");
|
|
30
19
|
function bufferToStream(buf) {
|
|
31
20
|
return new ReadableStream({
|
|
32
21
|
start(controller) {
|
|
@@ -51,13 +40,12 @@ function getEndianness() {
|
|
|
51
40
|
}
|
|
52
41
|
class CramFile {
|
|
53
42
|
constructor(args) {
|
|
54
|
-
var _a;
|
|
55
43
|
this.file = (0, io_1.open)(args.url, args.path, args.filehandle);
|
|
56
44
|
this.validateChecksums = true;
|
|
57
45
|
this.fetchReferenceSequenceCallback = args.seqFetch;
|
|
58
46
|
this.options = {
|
|
59
47
|
checkSequenceMD5: args.checkSequenceMD5,
|
|
60
|
-
cacheSize:
|
|
48
|
+
cacheSize: args.cacheSize ?? 20000,
|
|
61
49
|
};
|
|
62
50
|
// cache of features in a slice, keyed by the slice offset. caches all of
|
|
63
51
|
// the features in a slice, or none. the cache is actually used by the
|
|
@@ -69,269 +57,252 @@ class CramFile {
|
|
|
69
57
|
throw new Error('Detected big-endian machine, may be unable to run');
|
|
70
58
|
}
|
|
71
59
|
}
|
|
72
|
-
// can just read this object like a filehandle
|
|
73
|
-
read(buffer, offset, length, position) {
|
|
74
|
-
return this.file.read(buffer, offset, length, position);
|
|
75
|
-
}
|
|
76
60
|
// can just stat this object like a filehandle
|
|
77
61
|
stat() {
|
|
78
62
|
return this.file.stat();
|
|
79
63
|
}
|
|
64
|
+
// can just stat this object like a filehandle
|
|
65
|
+
read(length, position) {
|
|
66
|
+
return this.file.read(length, position);
|
|
67
|
+
}
|
|
80
68
|
// memoized
|
|
81
|
-
getDefinition() {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
return definition;
|
|
91
|
-
});
|
|
69
|
+
async getDefinition() {
|
|
70
|
+
const { maxLength, parser } = (0, sectionParsers_1.cramFileDefinition)();
|
|
71
|
+
const headbytes = await this.file.read(maxLength, 0);
|
|
72
|
+
const definition = parser(headbytes).value;
|
|
73
|
+
if (definition.majorVersion !== 2 && definition.majorVersion !== 3) {
|
|
74
|
+
throw new errors_1.CramUnimplementedError(`CRAM version ${definition.majorVersion} not supported`);
|
|
75
|
+
}
|
|
76
|
+
return definition;
|
|
92
77
|
}
|
|
93
78
|
// memoize
|
|
94
|
-
getSamHeader() {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
79
|
+
async getSamHeader() {
|
|
80
|
+
const firstContainer = await this.getContainerById(0);
|
|
81
|
+
if (!firstContainer) {
|
|
82
|
+
throw new errors_1.CramMalformedError('file contains no containers');
|
|
83
|
+
}
|
|
84
|
+
const firstBlock = await firstContainer.getFirstBlock();
|
|
85
|
+
if (firstBlock === undefined) {
|
|
86
|
+
return (0, sam_1.parseHeaderText)('');
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
104
89
|
const content = firstBlock.content;
|
|
105
|
-
const
|
|
90
|
+
const dataView = new DataView(content.buffer);
|
|
91
|
+
const headerLength = dataView.getInt32(0, true);
|
|
106
92
|
const textStart = 4;
|
|
107
|
-
const
|
|
93
|
+
const decoder = new TextDecoder('utf8');
|
|
94
|
+
const text = decoder.decode(content.subarray(textStart, textStart + headerLength));
|
|
108
95
|
this.header = text;
|
|
109
96
|
return (0, sam_1.parseHeaderText)(text);
|
|
110
|
-
}
|
|
97
|
+
}
|
|
111
98
|
}
|
|
112
|
-
getHeaderText() {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return this.header;
|
|
116
|
-
});
|
|
99
|
+
async getHeaderText() {
|
|
100
|
+
await this.getSamHeader();
|
|
101
|
+
return this.header;
|
|
117
102
|
}
|
|
118
|
-
getContainerById(containerNumber) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|
|
148
|
-
position = block._endPosition;
|
|
103
|
+
async getContainerById(containerNumber) {
|
|
104
|
+
const { majorVersion } = await this.getDefinition();
|
|
105
|
+
const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
|
|
106
|
+
let position = sectionParsers.cramFileDefinition.maxLength;
|
|
107
|
+
const { size: fileSize } = await this.file.stat();
|
|
108
|
+
const { cramContainerHeader1 } = sectionParsers;
|
|
109
|
+
// skip with a series of reads to the proper container
|
|
110
|
+
let currentContainer;
|
|
111
|
+
for (let i = 0; i <= containerNumber; i++) {
|
|
112
|
+
// if we are about to go off the end of the file
|
|
113
|
+
// and have not found that container, it does not exist
|
|
114
|
+
if (position + cramContainerHeader1.maxLength + 8 >= fileSize) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
currentContainer = this.getContainerAtPosition(position);
|
|
118
|
+
const currentHeader = await currentContainer.getHeader();
|
|
119
|
+
if (!currentHeader) {
|
|
120
|
+
throw new errors_1.CramMalformedError(`container ${containerNumber} not found in file`);
|
|
121
|
+
}
|
|
122
|
+
// if this is the first container, read all the blocks in the container
|
|
123
|
+
// to determine its length, because we cannot trust the container
|
|
124
|
+
// header's given length due to a bug somewhere in htslib
|
|
125
|
+
if (i === 0) {
|
|
126
|
+
position = currentHeader._endPosition;
|
|
127
|
+
for (let j = 0; j < currentHeader.numBlocks; j++) {
|
|
128
|
+
const block = await this.readBlock(position);
|
|
129
|
+
if (block === undefined) {
|
|
130
|
+
return undefined;
|
|
149
131
|
}
|
|
150
|
-
|
|
151
|
-
else {
|
|
152
|
-
// otherwise, just traverse to the next container using the container's
|
|
153
|
-
// length
|
|
154
|
-
position += currentHeader._size + currentHeader.length;
|
|
132
|
+
position = block._endPosition;
|
|
155
133
|
}
|
|
156
134
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
-
const b = buffer_1.Buffer.allocUnsafe(length);
|
|
163
|
-
yield this.file.read(b, 0, length, position);
|
|
164
|
-
const calculatedCrc32 = crc32_1.default.unsigned(b);
|
|
165
|
-
if (calculatedCrc32 !== recordedCrc32) {
|
|
166
|
-
throw new errors_1.CramMalformedError(`crc mismatch in ${description}: recorded CRC32 = ${recordedCrc32}, but calculated CRC32 = ${calculatedCrc32}`);
|
|
135
|
+
else {
|
|
136
|
+
// otherwise, just traverse to the next container using the container's
|
|
137
|
+
// length
|
|
138
|
+
position += currentHeader._size + currentHeader.length;
|
|
167
139
|
}
|
|
168
|
-
}
|
|
140
|
+
}
|
|
141
|
+
return currentContainer;
|
|
142
|
+
}
|
|
143
|
+
async checkCrc32(position, length, recordedCrc32, description) {
|
|
144
|
+
const b = await this.file.read(length, position);
|
|
145
|
+
// this shift >>> 0 is equivalent to crc32(b).unsigned but uses the
|
|
146
|
+
// internal calculator of crc32 to avoid accidentally importing buffer
|
|
147
|
+
// https://github.com/alexgorbatchev/crc/blob/31fc3853e417b5fb5ec83335428805842575f699/src/define_crc.ts#L5
|
|
148
|
+
const calculatedCrc32 = (0, crc32_1.default)(b) >>> 0;
|
|
149
|
+
if (calculatedCrc32 !== recordedCrc32) {
|
|
150
|
+
throw new errors_1.CramMalformedError(`crc mismatch in ${description}: recorded CRC32 = ${recordedCrc32}, but calculated CRC32 = ${calculatedCrc32}`);
|
|
151
|
+
}
|
|
169
152
|
}
|
|
170
153
|
/**
|
|
171
154
|
* @returns {Promise[number]} the number of containers in the file
|
|
172
155
|
*/
|
|
173
|
-
containerCount() {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
return undefined;
|
|
195
|
-
}
|
|
196
|
-
position = block._endPosition;
|
|
156
|
+
async containerCount() {
|
|
157
|
+
const { majorVersion } = await this.getDefinition();
|
|
158
|
+
const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
|
|
159
|
+
const { size: fileSize } = await this.file.stat();
|
|
160
|
+
const { cramContainerHeader1 } = sectionParsers;
|
|
161
|
+
let containerCount = 0;
|
|
162
|
+
let position = sectionParsers.cramFileDefinition.maxLength;
|
|
163
|
+
while (position + cramContainerHeader1.maxLength + 8 < fileSize) {
|
|
164
|
+
const currentHeader = await this.getContainerAtPosition(position).getHeader();
|
|
165
|
+
if (!currentHeader) {
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
// if this is the first container, read all the blocks in the container,
|
|
169
|
+
// because we cannot trust the container header's given length due to a
|
|
170
|
+
// bug somewhere in htslib
|
|
171
|
+
if (containerCount === 0) {
|
|
172
|
+
position = currentHeader._endPosition;
|
|
173
|
+
for (let j = 0; j < currentHeader.numBlocks; j++) {
|
|
174
|
+
const block = await this.readBlock(position);
|
|
175
|
+
if (block === undefined) {
|
|
176
|
+
return undefined;
|
|
197
177
|
}
|
|
178
|
+
position = block._endPosition;
|
|
198
179
|
}
|
|
199
|
-
else {
|
|
200
|
-
// otherwise, just traverse to the next container using the container's
|
|
201
|
-
// length
|
|
202
|
-
position += currentHeader._size + currentHeader.length;
|
|
203
|
-
}
|
|
204
|
-
containerCount += 1;
|
|
205
180
|
}
|
|
206
|
-
|
|
207
|
-
|
|
181
|
+
else {
|
|
182
|
+
// otherwise, just traverse to the next container using the container's
|
|
183
|
+
// length
|
|
184
|
+
position += currentHeader._size + currentHeader.length;
|
|
185
|
+
}
|
|
186
|
+
containerCount += 1;
|
|
187
|
+
}
|
|
188
|
+
return containerCount;
|
|
208
189
|
}
|
|
209
190
|
getContainerAtPosition(position) {
|
|
210
191
|
return new container_1.default(this, position);
|
|
211
192
|
}
|
|
212
|
-
readBlockHeader(position) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
193
|
+
async readBlockHeader(position) {
|
|
194
|
+
const { majorVersion } = await this.getDefinition();
|
|
195
|
+
const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
|
|
196
|
+
const { cramBlockHeader } = sectionParsers;
|
|
197
|
+
const { size: fileSize } = await this.file.stat();
|
|
198
|
+
if (position + cramBlockHeader.maxLength >= fileSize) {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
const buffer = await this.file.read(cramBlockHeader.maxLength, position);
|
|
202
|
+
return (0, util_1.parseItem)(buffer, cramBlockHeader.parser, 0, position);
|
|
203
|
+
}
|
|
204
|
+
async _parseSection(section, position, size = section.maxLength, preReadBuffer) {
|
|
205
|
+
let buffer;
|
|
206
|
+
if (preReadBuffer) {
|
|
207
|
+
buffer = preReadBuffer;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const { size: fileSize } = await this.file.stat();
|
|
211
|
+
if (position + size >= fileSize) {
|
|
219
212
|
return undefined;
|
|
220
213
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
214
|
+
buffer = await this.file.read(size, position);
|
|
215
|
+
}
|
|
216
|
+
const data = (0, util_1.parseItem)(buffer, section.parser, 0, position);
|
|
217
|
+
if (data._size !== size) {
|
|
218
|
+
throw new errors_1.CramMalformedError(`section read error: requested size ${size} does not equal parsed size ${data._size}`);
|
|
219
|
+
}
|
|
220
|
+
return data;
|
|
225
221
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
222
|
+
async _uncompress(compressionMethod, inputBuffer, uncompressedSize) {
|
|
223
|
+
if (compressionMethod === 'gzip') {
|
|
224
|
+
return (0, unzip_1.unzip)(inputBuffer);
|
|
225
|
+
}
|
|
226
|
+
else if (compressionMethod === 'bzip2') {
|
|
227
|
+
const bits = bzip2_1.default.array(inputBuffer);
|
|
228
|
+
let size = bzip2_1.default.header(bits);
|
|
229
|
+
let chunk;
|
|
230
|
+
const chunks = [];
|
|
231
|
+
do {
|
|
232
|
+
chunk = bzip2_1.default.decompress(bits, size);
|
|
233
|
+
if (chunk !== -1) {
|
|
234
|
+
chunks.push(chunk);
|
|
235
|
+
size -= chunk.length;
|
|
236
236
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
else if (compressionMethod === 'lzma') {
|
|
268
|
-
const decompressedResponse = new Response(new xz_decompress_1.XzReadableStream(bufferToStream(inputBuffer)));
|
|
269
|
-
const ret = buffer_1.Buffer.from(yield decompressedResponse.arrayBuffer());
|
|
270
|
-
ret.copy(outputBuffer);
|
|
271
|
-
}
|
|
272
|
-
else if (compressionMethod === 'rans') {
|
|
273
|
-
(0, rans_1.default)(inputBuffer, outputBuffer);
|
|
274
|
-
// htscodecs r4x8 is slower, but compatible.
|
|
275
|
-
// htscodecs.r4x8_uncompress(inputBuffer, outputBuffer);
|
|
276
|
-
}
|
|
277
|
-
else if (compressionMethod === 'rans4x16') {
|
|
278
|
-
htscodecs_1.default.r4x16_uncompress(inputBuffer, outputBuffer);
|
|
279
|
-
}
|
|
280
|
-
else if (compressionMethod === 'arith') {
|
|
281
|
-
htscodecs_1.default.arith_uncompress(inputBuffer, outputBuffer);
|
|
282
|
-
}
|
|
283
|
-
else if (compressionMethod === 'fqzcomp') {
|
|
284
|
-
htscodecs_1.default.fqzcomp_uncompress(inputBuffer, outputBuffer);
|
|
285
|
-
}
|
|
286
|
-
else if (compressionMethod === 'tok3') {
|
|
287
|
-
htscodecs_1.default.tok3_uncompress(inputBuffer, outputBuffer);
|
|
288
|
-
}
|
|
289
|
-
else {
|
|
290
|
-
throw new errors_1.CramUnimplementedError(`${compressionMethod} decompression not yet implemented`);
|
|
291
|
-
}
|
|
292
|
-
});
|
|
237
|
+
} while (chunk !== -1);
|
|
238
|
+
return (0, util_1.concatUint8Array)(chunks);
|
|
239
|
+
}
|
|
240
|
+
else if (compressionMethod === 'lzma') {
|
|
241
|
+
const decompressedResponse = new Response(new xz_decompress_1.XzReadableStream(bufferToStream(inputBuffer)));
|
|
242
|
+
return new Uint8Array(await decompressedResponse.arrayBuffer());
|
|
243
|
+
}
|
|
244
|
+
else if (compressionMethod === 'rans') {
|
|
245
|
+
const outputBuffer = new Uint8Array(uncompressedSize);
|
|
246
|
+
(0, rans_1.default)(inputBuffer, outputBuffer);
|
|
247
|
+
return outputBuffer;
|
|
248
|
+
// htscodecs r4x8 is slower, but compatible.
|
|
249
|
+
// htscodecs.r4x8_uncompress(inputBuffer, outputBuffer);
|
|
250
|
+
}
|
|
251
|
+
else if (compressionMethod === 'rans4x16') {
|
|
252
|
+
return htscodecs_1.default.r4x16_uncompress(inputBuffer);
|
|
253
|
+
}
|
|
254
|
+
else if (compressionMethod === 'arith') {
|
|
255
|
+
return htscodecs_1.default.arith_uncompress(inputBuffer);
|
|
256
|
+
}
|
|
257
|
+
else if (compressionMethod === 'fqzcomp') {
|
|
258
|
+
return htscodecs_1.default.fqzcomp_uncompress(inputBuffer);
|
|
259
|
+
}
|
|
260
|
+
else if (compressionMethod === 'tok3') {
|
|
261
|
+
return htscodecs_1.default.tok3_uncompress(inputBuffer);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
throw new errors_1.CramUnimplementedError(`${compressionMethod} decompression not yet implemented`);
|
|
265
|
+
}
|
|
293
266
|
}
|
|
294
|
-
readBlock(position) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
267
|
+
async readBlock(position) {
|
|
268
|
+
const { majorVersion } = await this.getDefinition();
|
|
269
|
+
const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
|
|
270
|
+
const blockHeader = await this.readBlockHeader(position);
|
|
271
|
+
if (blockHeader === undefined) {
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
const blockContentPosition = blockHeader._endPosition;
|
|
275
|
+
const d = await this.file.read(blockHeader.compressedSize, blockContentPosition);
|
|
276
|
+
const uncompressedData = blockHeader.compressionMethod !== 'raw'
|
|
277
|
+
? await this._uncompress(blockHeader.compressionMethod, d, blockHeader.uncompressedSize)
|
|
278
|
+
: d;
|
|
279
|
+
const block = {
|
|
280
|
+
...blockHeader,
|
|
281
|
+
_endPosition: blockContentPosition,
|
|
282
|
+
contentPosition: blockContentPosition,
|
|
283
|
+
content: uncompressedData,
|
|
284
|
+
};
|
|
285
|
+
if (majorVersion >= 3) {
|
|
286
|
+
// parse the crc32
|
|
287
|
+
const crc = await this._parseSection(sectionParsers.cramBlockCrc32, blockContentPosition + blockHeader.compressedSize);
|
|
288
|
+
if (crc === undefined) {
|
|
300
289
|
return undefined;
|
|
301
290
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
const compressedData = buffer_1.Buffer.allocUnsafe(blockHeader.compressedSize);
|
|
307
|
-
yield this.read(compressedData, 0, blockHeader.compressedSize, blockContentPosition);
|
|
308
|
-
yield this._uncompress(blockHeader.compressionMethod, compressedData, uncompressedData);
|
|
309
|
-
}
|
|
310
|
-
else {
|
|
311
|
-
yield this.read(uncompressedData, 0, blockHeader.uncompressedSize, blockContentPosition);
|
|
312
|
-
}
|
|
313
|
-
if (majorVersion >= 3) {
|
|
314
|
-
// parse the crc32
|
|
315
|
-
const crc = yield this._parseSection(sectionParsers.cramBlockCrc32, blockContentPosition + blockHeader.compressedSize);
|
|
316
|
-
if (crc === undefined) {
|
|
317
|
-
return undefined;
|
|
318
|
-
}
|
|
319
|
-
block.crc32 = crc.crc32;
|
|
320
|
-
// check the block data crc32
|
|
321
|
-
if (this.validateChecksums) {
|
|
322
|
-
yield this.checkCrc32(position, blockHeader._size + blockHeader.compressedSize, crc.crc32, 'block data');
|
|
323
|
-
}
|
|
324
|
-
// make the endposition and size reflect the whole block
|
|
325
|
-
block._endPosition = crc._endPosition;
|
|
326
|
-
block._size =
|
|
327
|
-
block.compressedSize + sectionParsers.cramBlockCrc32.maxLength;
|
|
291
|
+
block.crc32 = crc.crc32;
|
|
292
|
+
// check the block data crc32
|
|
293
|
+
if (this.validateChecksums) {
|
|
294
|
+
await this.checkCrc32(position, blockHeader._size + blockHeader.compressedSize, crc.crc32, 'block data');
|
|
328
295
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
296
|
+
// make the endposition and size reflect the whole block
|
|
297
|
+
block._endPosition = crc._endPosition;
|
|
298
|
+
block._size =
|
|
299
|
+
block.compressedSize + sectionParsers.cramBlockCrc32.maxLength;
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
block._endPosition = blockContentPosition + block.compressedSize;
|
|
303
|
+
block._size = block.compressedSize;
|
|
304
|
+
}
|
|
305
|
+
return block;
|
|
335
306
|
}
|
|
336
307
|
}
|
|
337
308
|
exports.default = CramFile;
|