@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.
Files changed (269) hide show
  1. package/dist/craiIndex.js +70 -87
  2. package/dist/craiIndex.js.map +1 -1
  3. package/dist/cram-bundle.js +1 -1
  4. package/dist/cram-bundle.js.LICENSE.txt +0 -9
  5. package/dist/cramFile/codecs/_base.d.ts +1 -1
  6. package/dist/cramFile/codecs/beta.d.ts +2 -2
  7. package/dist/cramFile/codecs/beta.js +1 -1
  8. package/dist/cramFile/codecs/beta.js.map +1 -1
  9. package/dist/cramFile/codecs/byteArrayLength.d.ts +2 -2
  10. package/dist/cramFile/codecs/byteArrayStop.d.ts +4 -4
  11. package/dist/cramFile/codecs/byteArrayStop.js +1 -1
  12. package/dist/cramFile/codecs/byteArrayStop.js.map +1 -1
  13. package/dist/cramFile/codecs/external.d.ts +1 -1
  14. package/dist/cramFile/codecs/external.js +1 -1
  15. package/dist/cramFile/codecs/external.js.map +1 -1
  16. package/dist/cramFile/codecs/gamma.d.ts +1 -1
  17. package/dist/cramFile/codecs/gamma.js +1 -1
  18. package/dist/cramFile/codecs/gamma.js.map +1 -1
  19. package/dist/cramFile/codecs/getBits.d.ts +1 -1
  20. package/dist/cramFile/codecs/getBits.js.map +1 -1
  21. package/dist/cramFile/codecs/huffman.d.ts +1 -1
  22. package/dist/cramFile/codecs/huffman.js +1 -1
  23. package/dist/cramFile/codecs/huffman.js.map +1 -1
  24. package/dist/cramFile/codecs/index.d.ts +1 -1
  25. package/dist/cramFile/codecs/index.js +5 -5
  26. package/dist/cramFile/codecs/index.js.map +1 -1
  27. package/dist/cramFile/codecs/subexp.d.ts +2 -2
  28. package/dist/cramFile/codecs/subexp.js +1 -1
  29. package/dist/cramFile/codecs/subexp.js.map +1 -1
  30. package/dist/cramFile/container/compressionScheme.d.ts +2 -2
  31. package/dist/cramFile/container/compressionScheme.js +1 -1
  32. package/dist/cramFile/container/compressionScheme.js.map +1 -1
  33. package/dist/cramFile/container/index.d.ts +1 -2
  34. package/dist/cramFile/container/index.js +62 -79
  35. package/dist/cramFile/container/index.js.map +1 -1
  36. package/dist/cramFile/file.d.ts +9 -13
  37. package/dist/cramFile/file.js +218 -247
  38. package/dist/cramFile/file.js.map +1 -1
  39. package/dist/cramFile/record.js +2 -2
  40. package/dist/cramFile/record.js.map +1 -1
  41. package/dist/cramFile/sectionParsers.d.ts +16 -16
  42. package/dist/cramFile/sectionParsers.js +12 -9
  43. package/dist/cramFile/sectionParsers.js.map +1 -1
  44. package/dist/cramFile/slice/decodeRecord.d.ts +3 -3
  45. package/dist/cramFile/slice/decodeRecord.js +11 -11
  46. package/dist/cramFile/slice/decodeRecord.js.map +1 -1
  47. package/dist/cramFile/slice/index.d.ts +2 -2
  48. package/dist/cramFile/slice/index.js +241 -262
  49. package/dist/cramFile/slice/index.js.map +1 -1
  50. package/dist/cramFile/util.d.ts +4 -2
  51. package/dist/cramFile/util.js +29 -6
  52. package/dist/cramFile/util.js.map +1 -1
  53. package/dist/htscodecs/arith_gen.d.ts +9 -17
  54. package/dist/htscodecs/arith_gen.js +32 -272
  55. package/dist/htscodecs/arith_gen.js.map +1 -1
  56. package/dist/htscodecs/fqzcomp.d.ts +1 -2
  57. package/dist/htscodecs/fqzcomp.js +2 -421
  58. package/dist/htscodecs/fqzcomp.js.map +1 -1
  59. package/dist/htscodecs/index.d.ts +5 -5
  60. package/dist/htscodecs/index.js +10 -10
  61. package/dist/htscodecs/index.js.map +1 -1
  62. package/dist/htscodecs/iostream.d.ts +2 -1
  63. package/dist/htscodecs/iostream.js +3 -2
  64. package/dist/htscodecs/iostream.js.map +1 -1
  65. package/dist/htscodecs/rans.d.ts +1 -2
  66. package/dist/htscodecs/rans.js +3 -270
  67. package/dist/htscodecs/rans.js.map +1 -1
  68. package/dist/htscodecs/rans4x16.d.ts +0 -1
  69. package/dist/htscodecs/rans4x16.js +6 -497
  70. package/dist/htscodecs/rans4x16.js.map +1 -1
  71. package/dist/htscodecs/tok3.js +17 -1
  72. package/dist/htscodecs/tok3.js.map +1 -1
  73. package/dist/index.d.ts +1 -1
  74. package/dist/index.js +2 -2
  75. package/dist/index.js.map +1 -1
  76. package/dist/indexedCramFile.d.ts +4 -4
  77. package/dist/indexedCramFile.js +97 -108
  78. package/dist/indexedCramFile.js.map +1 -1
  79. package/dist/io/index.d.ts +3 -3
  80. package/dist/io/index.js +6 -6
  81. package/dist/io/index.js.map +1 -1
  82. package/dist/rans/constants.d.ts +1 -1
  83. package/dist/rans/constants.js +1 -1
  84. package/dist/rans/constants.js.map +1 -1
  85. package/dist/rans/d04.js.map +1 -1
  86. package/dist/rans/decoding.js.map +1 -1
  87. package/dist/rans/frequencies.js.map +1 -1
  88. package/dist/rans/index.js +5 -5
  89. package/dist/rans/index.js.map +1 -1
  90. package/dist/unzip.d.ts +1 -1
  91. package/dist/unzip.js +5 -3
  92. package/dist/unzip.js.map +1 -1
  93. package/esm/craiIndex.js +10 -12
  94. package/esm/craiIndex.js.map +1 -1
  95. package/esm/cramFile/codecs/_base.d.ts +1 -1
  96. package/esm/cramFile/codecs/beta.d.ts +2 -2
  97. package/esm/cramFile/codecs/beta.js +1 -1
  98. package/esm/cramFile/codecs/beta.js.map +1 -1
  99. package/esm/cramFile/codecs/byteArrayLength.d.ts +2 -2
  100. package/esm/cramFile/codecs/byteArrayStop.d.ts +4 -4
  101. package/esm/cramFile/codecs/byteArrayStop.js +1 -1
  102. package/esm/cramFile/codecs/byteArrayStop.js.map +1 -1
  103. package/esm/cramFile/codecs/external.d.ts +1 -1
  104. package/esm/cramFile/codecs/external.js +1 -1
  105. package/esm/cramFile/codecs/external.js.map +1 -1
  106. package/esm/cramFile/codecs/gamma.d.ts +1 -1
  107. package/esm/cramFile/codecs/gamma.js +1 -1
  108. package/esm/cramFile/codecs/gamma.js.map +1 -1
  109. package/esm/cramFile/codecs/getBits.d.ts +1 -1
  110. package/esm/cramFile/codecs/getBits.js.map +1 -1
  111. package/esm/cramFile/codecs/huffman.d.ts +1 -1
  112. package/esm/cramFile/codecs/huffman.js +1 -1
  113. package/esm/cramFile/codecs/huffman.js.map +1 -1
  114. package/esm/cramFile/codecs/index.d.ts +1 -1
  115. package/esm/cramFile/codecs/index.js +5 -5
  116. package/esm/cramFile/codecs/index.js.map +1 -1
  117. package/esm/cramFile/codecs/subexp.d.ts +2 -2
  118. package/esm/cramFile/codecs/subexp.js +1 -1
  119. package/esm/cramFile/codecs/subexp.js.map +1 -1
  120. package/esm/cramFile/container/compressionScheme.d.ts +2 -2
  121. package/esm/cramFile/container/compressionScheme.js +1 -1
  122. package/esm/cramFile/container/compressionScheme.js.map +1 -1
  123. package/esm/cramFile/container/index.d.ts +1 -2
  124. package/esm/cramFile/container/index.js +4 -7
  125. package/esm/cramFile/container/index.js.map +1 -1
  126. package/esm/cramFile/file.d.ts +9 -13
  127. package/esm/cramFile/file.js +47 -52
  128. package/esm/cramFile/file.js.map +1 -1
  129. package/esm/cramFile/record.js +2 -2
  130. package/esm/cramFile/record.js.map +1 -1
  131. package/esm/cramFile/sectionParsers.d.ts +16 -16
  132. package/esm/cramFile/sectionParsers.js +7 -7
  133. package/esm/cramFile/sectionParsers.js.map +1 -1
  134. package/esm/cramFile/slice/decodeRecord.d.ts +3 -3
  135. package/esm/cramFile/slice/decodeRecord.js +11 -11
  136. package/esm/cramFile/slice/decodeRecord.js.map +1 -1
  137. package/esm/cramFile/slice/index.d.ts +2 -2
  138. package/esm/cramFile/slice/index.js +7 -6
  139. package/esm/cramFile/slice/index.js.map +1 -1
  140. package/esm/cramFile/util.d.ts +4 -2
  141. package/esm/cramFile/util.js +19 -2
  142. package/esm/cramFile/util.js.map +1 -1
  143. package/esm/htscodecs/arith_gen.d.ts +9 -17
  144. package/esm/htscodecs/arith_gen.js +32 -272
  145. package/esm/htscodecs/arith_gen.js.map +1 -1
  146. package/esm/htscodecs/fqzcomp.d.ts +1 -2
  147. package/esm/htscodecs/fqzcomp.js +2 -421
  148. package/esm/htscodecs/fqzcomp.js.map +1 -1
  149. package/esm/htscodecs/index.d.ts +5 -5
  150. package/esm/htscodecs/index.js +10 -10
  151. package/esm/htscodecs/index.js.map +1 -1
  152. package/esm/htscodecs/iostream.d.ts +2 -1
  153. package/esm/htscodecs/iostream.js +3 -2
  154. package/esm/htscodecs/iostream.js.map +1 -1
  155. package/esm/htscodecs/rans.d.ts +1 -2
  156. package/esm/htscodecs/rans.js +3 -270
  157. package/esm/htscodecs/rans.js.map +1 -1
  158. package/esm/htscodecs/rans4x16.d.ts +0 -1
  159. package/esm/htscodecs/rans4x16.js +6 -497
  160. package/esm/htscodecs/rans4x16.js.map +1 -1
  161. package/esm/htscodecs/tok3.js +17 -1
  162. package/esm/htscodecs/tok3.js.map +1 -1
  163. package/esm/index.d.ts +1 -1
  164. package/esm/index.js +1 -1
  165. package/esm/index.js.map +1 -1
  166. package/esm/indexedCramFile.d.ts +4 -4
  167. package/esm/indexedCramFile.js +10 -10
  168. package/esm/indexedCramFile.js.map +1 -1
  169. package/esm/io/index.d.ts +3 -3
  170. package/esm/io/index.js +2 -2
  171. package/esm/io/index.js.map +1 -1
  172. package/esm/rans/constants.d.ts +1 -1
  173. package/esm/rans/constants.js +1 -1
  174. package/esm/rans/constants.js.map +1 -1
  175. package/esm/rans/d04.js.map +1 -1
  176. package/esm/rans/decoding.js.map +1 -1
  177. package/esm/rans/frequencies.js.map +1 -1
  178. package/esm/rans/index.js +5 -5
  179. package/esm/rans/index.js.map +1 -1
  180. package/esm/unzip.d.ts +1 -1
  181. package/esm/unzip.js +4 -1
  182. package/esm/unzip.js.map +1 -1
  183. package/package.json +7 -9
  184. package/src/craiIndex.ts +17 -16
  185. package/src/cramFile/codecs/_base.ts +1 -1
  186. package/src/cramFile/codecs/beta.ts +3 -3
  187. package/src/cramFile/codecs/byteArrayLength.ts +1 -1
  188. package/src/cramFile/codecs/byteArrayStop.ts +3 -4
  189. package/src/cramFile/codecs/external.ts +3 -3
  190. package/src/cramFile/codecs/gamma.ts +2 -2
  191. package/src/cramFile/codecs/getBits.ts +1 -1
  192. package/src/cramFile/codecs/huffman.ts +2 -3
  193. package/src/cramFile/codecs/index.ts +7 -8
  194. package/src/cramFile/codecs/subexp.ts +3 -3
  195. package/src/cramFile/container/compressionScheme.ts +3 -3
  196. package/src/cramFile/container/index.ts +7 -10
  197. package/src/cramFile/file.ts +71 -77
  198. package/src/cramFile/record.ts +3 -3
  199. package/src/cramFile/sectionParsers.ts +27 -22
  200. package/src/cramFile/slice/decodeRecord.ts +17 -17
  201. package/src/cramFile/slice/index.ts +8 -9
  202. package/src/cramFile/util.ts +24 -5
  203. package/src/htscodecs/arith_gen.js +33 -296
  204. package/src/htscodecs/fqzcomp.js +2 -476
  205. package/src/htscodecs/index.js +9 -11
  206. package/src/htscodecs/iostream.js +3 -2
  207. package/src/htscodecs/rans.js +3 -315
  208. package/src/htscodecs/rans4x16.js +6 -557
  209. package/src/htscodecs/tok3.js +18 -1
  210. package/src/index.ts +1 -1
  211. package/src/indexedCramFile.ts +6 -5
  212. package/src/io/index.ts +6 -5
  213. package/src/rans/constants.ts +1 -1
  214. package/src/rans/d04.ts +0 -1
  215. package/src/rans/decoding.ts +0 -1
  216. package/src/rans/frequencies.ts +0 -1
  217. package/src/rans/index.ts +5 -7
  218. package/src/unzip.ts +5 -1
  219. package/dist/cramFile/filehandle.d.ts +0 -1
  220. package/dist/cramFile/filehandle.js +0 -3
  221. package/dist/cramFile/filehandle.js.map +0 -1
  222. package/dist/htscodecs/main_arith_gen.d.ts +0 -1
  223. package/dist/htscodecs/main_arith_gen.js +0 -86
  224. package/dist/htscodecs/main_arith_gen.js.map +0 -1
  225. package/dist/htscodecs/main_fqzcomp.d.ts +0 -1
  226. package/dist/htscodecs/main_fqzcomp.js +0 -112
  227. package/dist/htscodecs/main_fqzcomp.js.map +0 -1
  228. package/dist/htscodecs/main_rans.d.ts +0 -1
  229. package/dist/htscodecs/main_rans.js +0 -83
  230. package/dist/htscodecs/main_rans.js.map +0 -1
  231. package/dist/htscodecs/main_rans4x16.d.ts +0 -1
  232. package/dist/htscodecs/main_rans4x16.js +0 -82
  233. package/dist/htscodecs/main_rans4x16.js.map +0 -1
  234. package/dist/htscodecs/main_tok3.d.ts +0 -1
  235. package/dist/htscodecs/main_tok3.js +0 -84
  236. package/dist/htscodecs/main_tok3.js.map +0 -1
  237. package/dist/unzip-pako.d.ts +0 -2
  238. package/dist/unzip-pako.js +0 -9
  239. package/dist/unzip-pako.js.map +0 -1
  240. package/esm/cramFile/filehandle.d.ts +0 -1
  241. package/esm/cramFile/filehandle.js +0 -2
  242. package/esm/cramFile/filehandle.js.map +0 -1
  243. package/esm/htscodecs/main_arith_gen.d.ts +0 -1
  244. package/esm/htscodecs/main_arith_gen.js +0 -86
  245. package/esm/htscodecs/main_arith_gen.js.map +0 -1
  246. package/esm/htscodecs/main_fqzcomp.d.ts +0 -1
  247. package/esm/htscodecs/main_fqzcomp.js +0 -112
  248. package/esm/htscodecs/main_fqzcomp.js.map +0 -1
  249. package/esm/htscodecs/main_rans.d.ts +0 -1
  250. package/esm/htscodecs/main_rans.js +0 -83
  251. package/esm/htscodecs/main_rans.js.map +0 -1
  252. package/esm/htscodecs/main_rans4x16.d.ts +0 -1
  253. package/esm/htscodecs/main_rans4x16.js +0 -82
  254. package/esm/htscodecs/main_rans4x16.js.map +0 -1
  255. package/esm/htscodecs/main_tok3.d.ts +0 -1
  256. package/esm/htscodecs/main_tok3.js +0 -84
  257. package/esm/htscodecs/main_tok3.js.map +0 -1
  258. package/esm/unzip-pako.d.ts +0 -2
  259. package/esm/unzip-pako.js +0 -6
  260. package/esm/unzip-pako.js.map +0 -1
  261. package/src/cramFile/filehandle.ts +0 -1
  262. package/src/htscodecs/Makefile +0 -142
  263. package/src/htscodecs/README.md +0 -64
  264. package/src/htscodecs/main_arith_gen.js +0 -96
  265. package/src/htscodecs/main_fqzcomp.js +0 -113
  266. package/src/htscodecs/main_rans.js +0 -88
  267. package/src/htscodecs/main_rans4x16.js +0 -87
  268. package/src/htscodecs/main_tok3.js +0 -86
  269. package/src/unzip-pako.ts +0 -6
@@ -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 { Filehandle } from './filehandle';
4
+ import { BlockHeader, CompressionMethod } from './sectionParsers';
5
+ import type { GenericFilehandle } from 'generic-filehandle2';
7
6
  export interface CramFileSource {
8
- filehandle?: 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: Buffer;
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
- read(buffer: Buffer, offset: number, length: number, position: number): Promise<{
37
- bytesRead: number;
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: Buffer, offset: number) => {
70
+ parser: (buffer: Uint8Array, offset: number) => {
75
71
  offset: number;
76
72
  value: T;
77
73
  };
78
- }, position: number, size?: number, preReadBuffer?: Buffer): Promise<(T & {
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: Buffer, outputBuffer: Buffer): Promise<void>;
78
+ _uncompress(compressionMethod: CompressionMethod, inputBuffer: Uint8Array, uncompressedSize: number): Promise<any>;
83
79
  readBlock(position: number): Promise<CramFileBlock | undefined>;
84
80
  }
@@ -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
- // locals
23
- const unzip_1 = require("../unzip");
11
+ const htscodecs_1 = __importDefault(require("../htscodecs"));
12
+ const io_1 = require("../io");
24
13
  const rans_1 = __importDefault(require("../rans"));
25
- const sectionParsers_1 = require("./sectionParsers");
14
+ const sam_1 = require("../sam");
15
+ const unzip_1 = require("../unzip");
26
16
  const container_1 = __importDefault(require("./container"));
27
- const io_1 = require("../io");
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: (_a = args.cacheSize) !== null && _a !== void 0 ? _a : 20000,
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
- return __awaiter(this, void 0, void 0, function* () {
83
- const { maxLength, parser } = (0, sectionParsers_1.cramFileDefinition)();
84
- const headbytes = buffer_1.Buffer.allocUnsafe(maxLength);
85
- yield this.file.read(headbytes, 0, maxLength, 0);
86
- const definition = parser(headbytes).value;
87
- if (definition.majorVersion !== 2 && definition.majorVersion !== 3) {
88
- throw new errors_1.CramUnimplementedError(`CRAM version ${definition.majorVersion} not supported`);
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
- return __awaiter(this, void 0, void 0, function* () {
96
- const firstContainer = yield this.getContainerById(0);
97
- if (!firstContainer) {
98
- throw new errors_1.CramMalformedError('file contains no containers');
99
- }
100
- const firstBlock = yield firstContainer.getFirstBlock();
101
- if (firstBlock === undefined) {
102
- return (0, sam_1.parseHeaderText)('');
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 headerLength = content.readInt32LE(0);
90
+ const dataView = new DataView(content.buffer);
91
+ const headerLength = dataView.getInt32(0, true);
106
92
  const textStart = 4;
107
- const text = content.toString('utf8', textStart, textStart + headerLength);
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
- return __awaiter(this, void 0, void 0, function* () {
114
- yield this.getSamHeader();
115
- return this.header;
116
- });
99
+ async getHeaderText() {
100
+ await this.getSamHeader();
101
+ return this.header;
117
102
  }
118
- getContainerById(containerNumber) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- const { majorVersion } = yield this.getDefinition();
121
- const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
122
- let position = sectionParsers.cramFileDefinition.maxLength;
123
- const { size: fileSize } = yield this.file.stat();
124
- const { cramContainerHeader1 } = sectionParsers;
125
- // skip with a series of reads to the proper container
126
- let currentContainer;
127
- for (let i = 0; i <= containerNumber; i++) {
128
- // if we are about to go off the end of the file
129
- // and have not found that container, it does not exist
130
- if (position + cramContainerHeader1.maxLength + 8 >= fileSize) {
131
- return undefined;
132
- }
133
- currentContainer = this.getContainerAtPosition(position);
134
- const currentHeader = yield currentContainer.getHeader();
135
- if (!currentHeader) {
136
- throw new errors_1.CramMalformedError(`container ${containerNumber} not found in file`);
137
- }
138
- // if this is the first container, read all the blocks in the container
139
- // to determine its length, because we cannot trust the container
140
- // header's given length due to a bug somewhere in htslib
141
- if (i === 0) {
142
- position = currentHeader._endPosition;
143
- for (let j = 0; j < currentHeader.numBlocks; j++) {
144
- const block = yield this.readBlock(position);
145
- if (block === undefined) {
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
- return currentContainer;
158
- });
159
- }
160
- checkCrc32(position, length, recordedCrc32, description) {
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
- return __awaiter(this, void 0, void 0, function* () {
175
- const { majorVersion } = yield this.getDefinition();
176
- const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
177
- const { size: fileSize } = yield this.file.stat();
178
- const { cramContainerHeader1 } = sectionParsers;
179
- let containerCount = 0;
180
- let position = sectionParsers.cramFileDefinition.maxLength;
181
- while (position + cramContainerHeader1.maxLength + 8 < fileSize) {
182
- const currentHeader = yield this.getContainerAtPosition(position).getHeader();
183
- if (!currentHeader) {
184
- break;
185
- }
186
- // if this is the first container, read all the blocks in the container,
187
- // because we cannot trust the container header's given length due to a
188
- // bug somewhere in htslib
189
- if (containerCount === 0) {
190
- position = currentHeader._endPosition;
191
- for (let j = 0; j < currentHeader.numBlocks; j++) {
192
- const block = yield this.readBlock(position);
193
- if (block === undefined) {
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
- return containerCount;
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
- return __awaiter(this, void 0, void 0, function* () {
214
- const { majorVersion } = yield this.getDefinition();
215
- const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
216
- const { cramBlockHeader } = sectionParsers;
217
- const { size: fileSize } = yield this.file.stat();
218
- if (position + cramBlockHeader.maxLength >= fileSize) {
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
- const buffer = buffer_1.Buffer.allocUnsafe(cramBlockHeader.maxLength);
222
- yield this.file.read(buffer, 0, cramBlockHeader.maxLength, position);
223
- return (0, util_1.parseItem)(buffer, cramBlockHeader.parser, 0, position);
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
- _parseSection(section_1, position_1) {
227
- return __awaiter(this, arguments, void 0, function* (section, position, size = section.maxLength, preReadBuffer) {
228
- let buffer;
229
- if (preReadBuffer) {
230
- buffer = preReadBuffer;
231
- }
232
- else {
233
- const { size: fileSize } = yield this.file.stat();
234
- if (position + size >= fileSize) {
235
- return undefined;
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
- buffer = buffer_1.Buffer.allocUnsafe(size);
238
- yield this.file.read(buffer, 0, size, position);
239
- }
240
- const data = (0, util_1.parseItem)(buffer, section.parser, 0, position);
241
- if (data._size !== size) {
242
- throw new errors_1.CramMalformedError(`section read error: requested size ${size} does not equal parsed size ${data._size}`);
243
- }
244
- return data;
245
- });
246
- }
247
- _uncompress(compressionMethod, inputBuffer, outputBuffer) {
248
- return __awaiter(this, void 0, void 0, function* () {
249
- if (compressionMethod === 'gzip') {
250
- const result = (0, unzip_1.unzip)(inputBuffer);
251
- result.copy(outputBuffer);
252
- }
253
- else if (compressionMethod === 'bzip2') {
254
- const bits = bzip2_1.default.array(inputBuffer);
255
- let size = bzip2_1.default.header(bits);
256
- let j = 0;
257
- let chunk;
258
- do {
259
- chunk = bzip2_1.default.decompress(bits, size);
260
- if (chunk !== -1) {
261
- buffer_1.Buffer.from(chunk).copy(outputBuffer, j);
262
- j += chunk.length;
263
- size -= chunk.length;
264
- }
265
- } while (chunk !== -1);
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
- return __awaiter(this, void 0, void 0, function* () {
296
- const { majorVersion } = yield this.getDefinition();
297
- const sectionParsers = (0, sectionParsers_1.getSectionParsers)(majorVersion);
298
- const blockHeader = yield this.readBlockHeader(position);
299
- if (blockHeader === undefined) {
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
- const blockContentPosition = blockHeader._endPosition;
303
- const uncompressedData = buffer_1.Buffer.allocUnsafe(blockHeader.uncompressedSize);
304
- const block = Object.assign(Object.assign({}, blockHeader), { _endPosition: blockContentPosition, contentPosition: blockContentPosition, content: uncompressedData });
305
- if (blockHeader.compressionMethod !== 'raw') {
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
- else {
330
- block._endPosition = blockContentPosition + block.compressedSize;
331
- block._size = block.compressedSize;
332
- }
333
- return block;
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;