@gmod/cram 1.6.3 → 1.7.0

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 (255) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/craiIndex.d.ts +19 -12
  3. package/dist/craiIndex.js +63 -123
  4. package/dist/craiIndex.js.map +1 -1
  5. package/dist/cram-bundle.js +2 -17
  6. package/dist/cram-bundle.js.LICENSE.txt +17 -0
  7. package/dist/cramFile/codecs/_base.d.ts +26 -5
  8. package/dist/cramFile/codecs/_base.js +3 -39
  9. package/dist/cramFile/codecs/_base.js.map +1 -1
  10. package/dist/cramFile/codecs/beta.d.ts +7 -3
  11. package/dist/cramFile/codecs/beta.js +13 -31
  12. package/dist/cramFile/codecs/beta.js.map +1 -1
  13. package/dist/cramFile/codecs/byteArrayLength.d.ts +13 -7
  14. package/dist/cramFile/codecs/byteArrayLength.js +22 -41
  15. package/dist/cramFile/codecs/byteArrayLength.js.map +1 -1
  16. package/dist/cramFile/codecs/byteArrayStop.d.ts +9 -5
  17. package/dist/cramFile/codecs/byteArrayStop.js +25 -46
  18. package/dist/cramFile/codecs/byteArrayStop.js.map +1 -1
  19. package/dist/cramFile/codecs/dataSeriesTypes.d.ts +4 -0
  20. package/dist/cramFile/codecs/dataSeriesTypes.js +3 -0
  21. package/dist/cramFile/codecs/dataSeriesTypes.js.map +1 -0
  22. package/dist/cramFile/codecs/external.d.ts +10 -6
  23. package/dist/cramFile/codecs/external.js +26 -44
  24. package/dist/cramFile/codecs/external.js.map +1 -1
  25. package/dist/cramFile/codecs/gamma.d.ts +7 -3
  26. package/dist/cramFile/codecs/gamma.js +16 -34
  27. package/dist/cramFile/codecs/gamma.js.map +1 -1
  28. package/dist/cramFile/codecs/getBits.d.ts +7 -0
  29. package/dist/cramFile/codecs/getBits.js +26 -0
  30. package/dist/cramFile/codecs/getBits.js.map +1 -0
  31. package/dist/cramFile/codecs/huffman.d.ts +17 -13
  32. package/dist/cramFile/codecs/huffman.js +76 -85
  33. package/dist/cramFile/codecs/huffman.js.map +1 -1
  34. package/dist/cramFile/codecs/index.d.ts +4 -2
  35. package/dist/cramFile/codecs/index.js +12 -13
  36. package/dist/cramFile/codecs/index.js.map +1 -1
  37. package/dist/cramFile/codecs/subexp.d.ts +7 -3
  38. package/dist/cramFile/codecs/subexp.js +19 -36
  39. package/dist/cramFile/codecs/subexp.js.map +1 -1
  40. package/dist/cramFile/constants.d.ts +35 -35
  41. package/dist/cramFile/constants.js +1 -1
  42. package/dist/cramFile/constants.js.map +1 -1
  43. package/dist/cramFile/container/compressionScheme.d.ts +57 -11
  44. package/dist/cramFile/container/compressionScheme.js +37 -32
  45. package/dist/cramFile/container/compressionScheme.js.map +1 -1
  46. package/dist/cramFile/container/index.d.ts +23 -9
  47. package/dist/cramFile/container/index.js +74 -144
  48. package/dist/cramFile/container/index.js.map +1 -1
  49. package/dist/cramFile/encoding.d.ts +78 -0
  50. package/dist/cramFile/encoding.js +3 -0
  51. package/dist/cramFile/encoding.js.map +1 -0
  52. package/dist/cramFile/file.d.ts +91 -41
  53. package/dist/cramFile/file.js +234 -368
  54. package/dist/cramFile/file.js.map +1 -1
  55. package/dist/cramFile/filehandle.d.ts +2 -0
  56. package/dist/cramFile/filehandle.js +3 -0
  57. package/dist/cramFile/filehandle.js.map +1 -0
  58. package/dist/cramFile/index.d.ts +1 -1
  59. package/dist/cramFile/index.js +1 -1
  60. package/dist/cramFile/index.js.map +1 -1
  61. package/dist/cramFile/record.d.ts +61 -17
  62. package/dist/cramFile/record.js +153 -77
  63. package/dist/cramFile/record.js.map +1 -1
  64. package/dist/cramFile/sectionParsers.d.ts +99 -8
  65. package/dist/cramFile/sectionParsers.js +70 -80
  66. package/dist/cramFile/sectionParsers.js.map +1 -1
  67. package/dist/cramFile/slice/decodeRecord.d.ts +30 -2
  68. package/dist/cramFile/slice/decodeRecord.js +148 -118
  69. package/dist/cramFile/slice/decodeRecord.js.map +1 -1
  70. package/dist/cramFile/slice/index.d.ts +21 -14
  71. package/dist/cramFile/slice/index.js +286 -381
  72. package/dist/cramFile/slice/index.js.map +1 -1
  73. package/dist/cramFile/util.d.ts +11 -5
  74. package/dist/cramFile/util.js +19 -97
  75. package/dist/cramFile/util.js.map +1 -1
  76. package/dist/errors.d.ts +5 -10
  77. package/dist/errors.js +11 -62
  78. package/dist/errors.js.map +1 -1
  79. package/dist/index.d.ts +3 -3
  80. package/dist/index.js +3 -3
  81. package/dist/index.js.map +1 -1
  82. package/dist/indexedCramFile.d.ts +37 -12
  83. package/dist/indexedCramFile.js +114 -154
  84. package/dist/indexedCramFile.js.map +1 -1
  85. package/dist/io/index.d.ts +5 -5
  86. package/dist/io/index.js +9 -9
  87. package/dist/io/index.js.map +1 -1
  88. package/dist/rans/constants.js +3 -3
  89. package/dist/rans/constants.js.map +1 -1
  90. package/dist/rans/d04.js +15 -15
  91. package/dist/rans/d04.js.map +1 -1
  92. package/dist/rans/d14.js +21 -21
  93. package/dist/rans/d14.js.map +1 -1
  94. package/dist/rans/decoding.js +27 -30
  95. package/dist/rans/decoding.js.map +1 -1
  96. package/dist/rans/frequencies.js +11 -11
  97. package/dist/rans/frequencies.js.map +1 -1
  98. package/dist/rans/index.js +46 -49
  99. package/dist/rans/index.js.map +1 -1
  100. package/dist/sam.d.ts +8 -1
  101. package/dist/sam.js +7 -7
  102. package/dist/sam.js.map +1 -1
  103. package/dist/typescript.d.ts +3 -0
  104. package/dist/typescript.js +11 -0
  105. package/dist/typescript.js.map +1 -0
  106. package/dist/unzip-pako.js +1 -1
  107. package/dist/unzip-pako.js.map +1 -1
  108. package/dist/unzip.js +1 -1
  109. package/dist/unzip.js.map +1 -1
  110. package/errors.js +11 -62
  111. package/esm/craiIndex.d.ts +19 -12
  112. package/esm/craiIndex.js +8 -24
  113. package/esm/craiIndex.js.map +1 -1
  114. package/esm/cramFile/codecs/_base.d.ts +26 -5
  115. package/esm/cramFile/codecs/_base.js +1 -35
  116. package/esm/cramFile/codecs/_base.js.map +1 -1
  117. package/esm/cramFile/codecs/beta.d.ts +7 -3
  118. package/esm/cramFile/codecs/beta.js +4 -3
  119. package/esm/cramFile/codecs/beta.js.map +1 -1
  120. package/esm/cramFile/codecs/byteArrayLength.d.ts +13 -7
  121. package/esm/cramFile/codecs/byteArrayLength.js +1 -1
  122. package/esm/cramFile/codecs/byteArrayLength.js.map +1 -1
  123. package/esm/cramFile/codecs/byteArrayStop.d.ts +9 -5
  124. package/esm/cramFile/codecs/byteArrayStop.js +7 -9
  125. package/esm/cramFile/codecs/byteArrayStop.js.map +1 -1
  126. package/esm/cramFile/codecs/dataSeriesTypes.d.ts +4 -0
  127. package/esm/cramFile/codecs/dataSeriesTypes.js +2 -0
  128. package/esm/cramFile/codecs/dataSeriesTypes.js.map +1 -0
  129. package/esm/cramFile/codecs/external.d.ts +10 -6
  130. package/esm/cramFile/codecs/external.js +4 -3
  131. package/esm/cramFile/codecs/external.js.map +1 -1
  132. package/esm/cramFile/codecs/gamma.d.ts +7 -3
  133. package/esm/cramFile/codecs/gamma.js +5 -4
  134. package/esm/cramFile/codecs/gamma.js.map +1 -1
  135. package/esm/cramFile/codecs/getBits.d.ts +7 -0
  136. package/esm/cramFile/codecs/getBits.js +21 -0
  137. package/esm/cramFile/codecs/getBits.js.map +1 -0
  138. package/esm/cramFile/codecs/huffman.d.ts +17 -13
  139. package/esm/cramFile/codecs/huffman.js +22 -9
  140. package/esm/cramFile/codecs/huffman.js.map +1 -1
  141. package/esm/cramFile/codecs/index.d.ts +4 -2
  142. package/esm/cramFile/codecs/index.js +1 -1
  143. package/esm/cramFile/codecs/index.js.map +1 -1
  144. package/esm/cramFile/codecs/subexp.d.ts +7 -3
  145. package/esm/cramFile/codecs/subexp.js +7 -5
  146. package/esm/cramFile/codecs/subexp.js.map +1 -1
  147. package/esm/cramFile/constants.d.ts +35 -35
  148. package/esm/cramFile/constants.js.map +1 -1
  149. package/esm/cramFile/container/compressionScheme.d.ts +57 -11
  150. package/esm/cramFile/container/compressionScheme.js +15 -8
  151. package/esm/cramFile/container/compressionScheme.js.map +1 -1
  152. package/esm/cramFile/container/index.d.ts +23 -9
  153. package/esm/cramFile/container/index.js +11 -9
  154. package/esm/cramFile/container/index.js.map +1 -1
  155. package/esm/cramFile/encoding.d.ts +78 -0
  156. package/esm/cramFile/encoding.js +2 -0
  157. package/esm/cramFile/encoding.js.map +1 -0
  158. package/esm/cramFile/file.d.ts +91 -41
  159. package/esm/cramFile/file.js +59 -47
  160. package/esm/cramFile/file.js.map +1 -1
  161. package/esm/cramFile/filehandle.d.ts +2 -0
  162. package/esm/cramFile/filehandle.js +2 -0
  163. package/esm/cramFile/filehandle.js.map +1 -0
  164. package/esm/cramFile/index.d.ts +1 -1
  165. package/esm/cramFile/index.js.map +1 -1
  166. package/esm/cramFile/record.d.ts +61 -17
  167. package/esm/cramFile/record.js +83 -5
  168. package/esm/cramFile/record.js.map +1 -1
  169. package/esm/cramFile/sectionParsers.d.ts +99 -8
  170. package/esm/cramFile/sectionParsers.js +7 -17
  171. package/esm/cramFile/sectionParsers.js.map +1 -1
  172. package/esm/cramFile/slice/decodeRecord.d.ts +30 -2
  173. package/esm/cramFile/slice/decodeRecord.js +102 -70
  174. package/esm/cramFile/slice/decodeRecord.js.map +1 -1
  175. package/esm/cramFile/slice/index.d.ts +21 -14
  176. package/esm/cramFile/slice/index.js +77 -38
  177. package/esm/cramFile/slice/index.js.map +1 -1
  178. package/esm/cramFile/util.d.ts +11 -5
  179. package/esm/cramFile/util.js +11 -82
  180. package/esm/cramFile/util.js.map +1 -1
  181. package/esm/errors.d.ts +5 -10
  182. package/esm/errors.js +0 -5
  183. package/esm/errors.js.map +1 -1
  184. package/esm/index.d.ts +3 -3
  185. package/esm/index.js.map +1 -1
  186. package/esm/indexedCramFile.d.ts +37 -12
  187. package/esm/indexedCramFile.js +19 -8
  188. package/esm/indexedCramFile.js.map +1 -1
  189. package/esm/io/index.d.ts +5 -5
  190. package/esm/io/index.js +3 -3
  191. package/esm/io/index.js.map +1 -1
  192. package/esm/sam.d.ts +8 -1
  193. package/esm/sam.js.map +1 -1
  194. package/esm/typescript.d.ts +3 -0
  195. package/esm/typescript.js +7 -0
  196. package/esm/typescript.js.map +1 -0
  197. package/package.json +18 -11
  198. package/src/{craiIndex.js → craiIndex.ts} +37 -31
  199. package/src/cramFile/codecs/_base.ts +45 -0
  200. package/src/cramFile/codecs/beta.ts +34 -0
  201. package/src/cramFile/codecs/{byteArrayLength.js → byteArrayLength.ts} +27 -5
  202. package/src/cramFile/codecs/{byteArrayStop.js → byteArrayStop.ts} +25 -12
  203. package/src/cramFile/codecs/dataSeriesTypes.ts +39 -0
  204. package/src/cramFile/codecs/{external.js → external.ts} +28 -12
  205. package/src/cramFile/codecs/gamma.ts +42 -0
  206. package/src/cramFile/codecs/getBits.ts +28 -0
  207. package/src/cramFile/codecs/{huffman.js → huffman.ts} +48 -15
  208. package/src/cramFile/codecs/{index.js → index.ts} +9 -3
  209. package/src/cramFile/codecs/subexp.ts +45 -0
  210. package/src/cramFile/{constants.js → constants.ts} +0 -0
  211. package/src/cramFile/container/{compressionScheme.js → compressionScheme.ts} +50 -18
  212. package/src/cramFile/container/{index.js → index.ts} +13 -13
  213. package/src/cramFile/encoding.ts +98 -0
  214. package/src/cramFile/{file.js → file.ts} +136 -62
  215. package/src/cramFile/filehandle.ts +3 -0
  216. package/src/cramFile/{index.js → index.ts} +0 -0
  217. package/src/cramFile/{record.js → record.ts} +185 -14
  218. package/src/cramFile/{sectionParsers.js → sectionParsers.ts} +148 -20
  219. package/src/cramFile/slice/{decodeRecord.js → decodeRecord.ts} +158 -105
  220. package/src/cramFile/slice/{index.js → index.ts} +138 -63
  221. package/src/cramFile/{util.js → util.ts} +28 -17
  222. package/src/{errors.js → errors.ts} +0 -5
  223. package/src/{index.js → index.ts} +0 -0
  224. package/src/{indexedCramFile.js → indexedCramFile.ts} +79 -19
  225. package/src/io/{index.js → index.ts} +10 -5
  226. package/src/{sam.js → sam.ts} +7 -2
  227. package/src/typescript.ts +17 -0
  228. package/src/typings/binary-parser.d.ts +44 -0
  229. package/src/typings/bzip2.d.ts +7 -0
  230. package/src/typings/htscodecs.d.ts +6 -0
  231. package/dist/io/bufferCache.d.ts +0 -12
  232. package/dist/io/bufferCache.js +0 -112
  233. package/dist/io/bufferCache.js.map +0 -1
  234. package/dist/io/localFile.d.ts +0 -10
  235. package/dist/io/localFile.js +0 -108
  236. package/dist/io/localFile.js.map +0 -1
  237. package/dist/io/remoteFile.d.ts +0 -16
  238. package/dist/io/remoteFile.js +0 -143
  239. package/dist/io/remoteFile.js.map +0 -1
  240. package/esm/io/bufferCache.d.ts +0 -12
  241. package/esm/io/bufferCache.js +0 -54
  242. package/esm/io/bufferCache.js.map +0 -1
  243. package/esm/io/localFile.d.ts +0 -10
  244. package/esm/io/localFile.js +0 -31
  245. package/esm/io/localFile.js.map +0 -1
  246. package/esm/io/remoteFile.d.ts +0 -16
  247. package/esm/io/remoteFile.js +0 -64
  248. package/esm/io/remoteFile.js.map +0 -1
  249. package/src/cramFile/codecs/_base.js +0 -49
  250. package/src/cramFile/codecs/beta.js +0 -23
  251. package/src/cramFile/codecs/gamma.js +0 -30
  252. package/src/cramFile/codecs/subexp.js +0 -32
  253. package/src/io/bufferCache.js +0 -66
  254. package/src/io/localFile.js +0 -35
  255. package/src/io/remoteFile.js +0 -71
@@ -8,52 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
13
  };
41
14
  Object.defineProperty(exports, "__esModule", { value: true });
42
- var unzip_1 = require("../unzip");
43
- var buffer_crc32_1 = __importDefault(require("buffer-crc32"));
44
- var quick_lru_1 = __importDefault(require("quick-lru"));
45
- var errors_1 = require("../errors");
46
- var rans_1 = __importDefault(require("../rans"));
47
- var sectionParsers_1 = require("./sectionParsers");
48
- var htscodecs_1 = __importDefault(require("@jkbonfield/htscodecs"));
49
- var container_1 = __importDefault(require("./container"));
50
- var io_1 = require("../io");
51
- var util_1 = require("./util");
52
- var sam_1 = require("../sam");
15
+ const unzip_1 = require("../unzip");
16
+ const buffer_crc32_1 = __importDefault(require("buffer-crc32"));
17
+ const quick_lru_1 = __importDefault(require("quick-lru"));
18
+ const errors_1 = require("../errors");
19
+ const rans_1 = __importDefault(require("../rans"));
20
+ const sectionParsers_1 = require("./sectionParsers");
21
+ const htscodecs_1 = __importDefault(require("@jkbonfield/htscodecs"));
22
+ const container_1 = __importDefault(require("./container"));
23
+ const io_1 = require("../io");
24
+ const util_1 = require("./util");
25
+ const sam_1 = require("../sam");
53
26
  //source:https://abdulapopoola.com/2019/01/20/check-endianness-with-javascript/
54
27
  function getEndianness() {
55
- var uInt32 = new Uint32Array([0x11223344]);
56
- var uInt8 = new Uint8Array(uInt32.buffer);
28
+ const uInt32 = new Uint32Array([0x11223344]);
29
+ const uInt8 = new Uint8Array(uInt32.buffer);
57
30
  if (uInt8[0] === 0x44) {
58
31
  return 0; //little-endian
59
32
  }
@@ -64,26 +37,15 @@ function getEndianness() {
64
37
  return 2; //mixed-endian?
65
38
  }
66
39
  }
67
- var CramFile = /** @class */ (function () {
68
- /**
69
- * @param {object} args
70
- * @param {object} [args.filehandle] - a filehandle that implements the stat() and
71
- * read() methods of the Node filehandle API https://nodejs.org/api/fs.html#fs_class_filehandle
72
- * @param {object} [args.path] - path to the cram file
73
- * @param {object} [args.url] - url for the cram file. also supports file:// urls for local files
74
- * @param {function} [args.seqFetch] - a function with signature
75
- * `(seqId, startCoordinate, endCoordinate)` that returns a promise for a string of sequence bases
76
- * @param {number} [args.cacheSize] optional maximum number of CRAM records to cache. default 20,000
77
- * @param {boolean} [args.checkSequenceMD5] - default true. if false, disables verifying the MD5
78
- * checksum of the reference sequence underlying a slice. In some applications, this check can cause an inconvenient amount (many megabases) of sequences to be fetched.
79
- */
80
- function CramFile(args) {
40
+ class CramFile {
41
+ constructor(args) {
42
+ var _a;
81
43
  this.file = (0, io_1.open)(args.url, args.path, args.filehandle);
82
44
  this.validateChecksums = true;
83
45
  this.fetchReferenceSequenceCallback = args.seqFetch;
84
46
  this.options = {
85
- checkSequenceMD5: args.checkSequenceMD5 !== false,
86
- cacheSize: args.cacheSize !== undefined ? args.cacheSize : 20000,
47
+ checkSequenceMD5: args.checkSequenceMD5,
48
+ cacheSize: (_a = args.cacheSize) !== null && _a !== void 0 ? _a : 20000,
87
49
  };
88
50
  // cache of features in a slice, keyed by the
89
51
  // slice offset. caches all of the features in a slice, or none.
@@ -96,292 +58,210 @@ var CramFile = /** @class */ (function () {
96
58
  throw new Error('Detected big-endian machine, may be unable to run');
97
59
  }
98
60
  }
99
- CramFile.prototype.toString = function () {
100
- if (this.file.filename) {
101
- return this.file.filename;
102
- }
103
- if (this.file.url) {
104
- return this.file.url;
105
- }
106
- return '(cram file)';
107
- };
61
+ // toString() {
62
+ // if (this.file.filename) {
63
+ // return this.file.filename
64
+ // }
65
+ // if (this.file.url) {
66
+ // return this.file.url
67
+ // }
68
+ //
69
+ // return '(cram file)'
70
+ // }
108
71
  // can just read this object like a filehandle
109
- CramFile.prototype.read = function (buffer, offset, length, position) {
72
+ read(buffer, offset, length, position) {
110
73
  return this.file.read(buffer, offset, length, position);
111
- };
74
+ }
112
75
  // can just stat this object like a filehandle
113
- CramFile.prototype.stat = function () {
76
+ stat() {
114
77
  return this.file.stat();
115
- };
78
+ }
116
79
  // memoized
117
- CramFile.prototype.getDefinition = function () {
118
- return __awaiter(this, void 0, void 0, function () {
119
- var headbytes, definition;
120
- return __generator(this, function (_a) {
121
- switch (_a.label) {
122
- case 0:
123
- headbytes = Buffer.allocUnsafe(sectionParsers_1.cramFileDefinition.maxLength);
124
- return [4 /*yield*/, this.file.read(headbytes, 0, sectionParsers_1.cramFileDefinition.maxLength, 0)];
125
- case 1:
126
- _a.sent();
127
- definition = sectionParsers_1.cramFileDefinition.parser.parse(headbytes).result;
128
- if (definition.majorVersion !== 2 && definition.majorVersion !== 3) {
129
- throw new errors_1.CramUnimplementedError("CRAM version ".concat(definition.majorVersion, " not supported"));
130
- }
131
- return [2 /*return*/, definition];
132
- }
133
- });
80
+ getDefinition() {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const headbytes = Buffer.allocUnsafe(sectionParsers_1.cramFileDefinition.maxLength);
83
+ yield this.file.read(headbytes, 0, sectionParsers_1.cramFileDefinition.maxLength, 0);
84
+ const definition = sectionParsers_1.cramFileDefinition.parser.parse(headbytes)
85
+ .result;
86
+ if (definition.majorVersion !== 2 && definition.majorVersion !== 3) {
87
+ throw new errors_1.CramUnimplementedError(`CRAM version ${definition.majorVersion} not supported`);
88
+ }
89
+ return definition;
134
90
  });
135
- };
91
+ }
136
92
  // memoize
137
- CramFile.prototype.getSamHeader = function () {
138
- return __awaiter(this, void 0, void 0, function () {
139
- var firstContainer, content, headerLength, textStart, text;
140
- return __generator(this, function (_a) {
141
- switch (_a.label) {
142
- case 0: return [4 /*yield*/, this.getContainerById(0)];
143
- case 1:
144
- firstContainer = _a.sent();
145
- if (!firstContainer) {
146
- throw new errors_1.CramMalformedError('file contains no containers');
147
- }
148
- return [4 /*yield*/, firstContainer.getFirstBlock()
149
- // find the end of the trailing zeros in the header text
150
- ];
151
- case 2:
152
- content = (_a.sent()).content;
153
- headerLength = content.readInt32LE(0);
154
- textStart = 4;
155
- text = content.toString('utf8', textStart, textStart + headerLength);
156
- this.header = text;
157
- return [2 /*return*/, (0, sam_1.parseHeaderText)(text)];
158
- }
159
- });
93
+ getSamHeader() {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ const firstContainer = yield this.getContainerById(0);
96
+ if (!firstContainer) {
97
+ throw new errors_1.CramMalformedError('file contains no containers');
98
+ }
99
+ const firstBlock = yield firstContainer.getFirstBlock();
100
+ if (firstBlock === undefined) {
101
+ return (0, sam_1.parseHeaderText)('');
102
+ }
103
+ const content = firstBlock.content;
104
+ // find the end of the trailing zeros in the header text
105
+ const headerLength = content.readInt32LE(0);
106
+ const textStart = 4;
107
+ // let textEnd = content.length - 1
108
+ // while (textEnd >= textStart && !content[textEnd]) textEnd -= 1
109
+ // trim off the trailing zeros
110
+ const text = content.toString('utf8', textStart, textStart + headerLength);
111
+ this.header = text;
112
+ return (0, sam_1.parseHeaderText)(text);
160
113
  });
161
- };
162
- CramFile.prototype.getHeaderText = function () {
163
- return __awaiter(this, void 0, void 0, function () {
164
- return __generator(this, function (_a) {
165
- switch (_a.label) {
166
- case 0: return [4 /*yield*/, this.getSamHeader()];
167
- case 1:
168
- _a.sent();
169
- return [2 /*return*/, this.header];
170
- }
171
- });
114
+ }
115
+ getHeaderText() {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ yield this.getSamHeader();
118
+ return this.header;
172
119
  });
173
- };
120
+ }
174
121
  // memoize
175
- CramFile.prototype.getSectionParsers = function () {
176
- return __awaiter(this, void 0, void 0, function () {
177
- var majorVersion;
178
- return __generator(this, function (_a) {
179
- switch (_a.label) {
180
- case 0: return [4 /*yield*/, this.getDefinition()];
181
- case 1:
182
- majorVersion = (_a.sent()).majorVersion;
183
- return [2 /*return*/, (0, sectionParsers_1.getSectionParsers)(majorVersion)];
184
- }
185
- });
122
+ getSectionParsers() {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ const { majorVersion } = yield this.getDefinition();
125
+ return (0, sectionParsers_1.getSectionParsers)(majorVersion);
186
126
  });
187
- };
188
- CramFile.prototype.getContainerById = function (containerNumber) {
189
- return __awaiter(this, void 0, void 0, function () {
190
- var sectionParsers, position, fileSize, cramContainerHeader1, currentContainer, i, currentHeader, j, block;
191
- return __generator(this, function (_a) {
192
- switch (_a.label) {
193
- case 0: return [4 /*yield*/, this.getSectionParsers()];
194
- case 1:
195
- sectionParsers = _a.sent();
196
- position = sectionParsers.cramFileDefinition.maxLength;
197
- return [4 /*yield*/, this.file.stat()];
198
- case 2:
199
- fileSize = (_a.sent()).size;
200
- cramContainerHeader1 = sectionParsers.cramContainerHeader1;
201
- i = 0;
202
- _a.label = 3;
203
- case 3:
204
- if (!(i <= containerNumber)) return [3 /*break*/, 11];
205
- // if we are about to go off the end of the file
206
- // and have not found that container, it does not exist
207
- if (position + cramContainerHeader1.maxLength + 8 >= fileSize) {
208
- return [2 /*return*/, undefined];
209
- }
210
- currentContainer = this.getContainerAtPosition(position);
211
- return [4 /*yield*/, currentContainer.getHeader()];
212
- case 4:
213
- currentHeader = _a.sent();
214
- if (!currentHeader) {
215
- throw new errors_1.CramMalformedError("container ".concat(containerNumber, " not found in file"));
127
+ }
128
+ getContainerById(containerNumber) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ const sectionParsers = yield this.getSectionParsers();
131
+ let position = sectionParsers.cramFileDefinition.maxLength;
132
+ const { size: fileSize } = yield this.file.stat();
133
+ const { cramContainerHeader1 } = sectionParsers;
134
+ // skip with a series of reads to the proper container
135
+ let currentContainer;
136
+ for (let i = 0; i <= containerNumber; i += 1) {
137
+ // if we are about to go off the end of the file
138
+ // and have not found that container, it does not exist
139
+ if (position + cramContainerHeader1.maxLength + 8 >= fileSize) {
140
+ return undefined;
141
+ }
142
+ currentContainer = this.getContainerAtPosition(position);
143
+ const currentHeader = yield currentContainer.getHeader();
144
+ if (!currentHeader) {
145
+ throw new errors_1.CramMalformedError(`container ${containerNumber} not found in file`);
146
+ }
147
+ // if this is the first container, read all the blocks in the
148
+ // container to determine its length, because we cannot trust
149
+ // the container header's given length due to a bug somewhere
150
+ // in htslib
151
+ if (i === 0) {
152
+ position = currentHeader._endPosition;
153
+ for (let j = 0; j < currentHeader.numBlocks; j += 1) {
154
+ const block = yield this.readBlock(position);
155
+ if (block === undefined) {
156
+ return undefined;
216
157
  }
217
- if (!(i === 0)) return [3 /*break*/, 9];
218
- position = currentHeader._endPosition;
219
- j = 0;
220
- _a.label = 5;
221
- case 5:
222
- if (!(j < currentHeader.numBlocks)) return [3 /*break*/, 8];
223
- return [4 /*yield*/, this.readBlock(position)];
224
- case 6:
225
- block = _a.sent();
226
158
  position = block._endPosition;
227
- _a.label = 7;
228
- case 7:
229
- j += 1;
230
- return [3 /*break*/, 5];
231
- case 8: return [3 /*break*/, 10];
232
- case 9:
233
- // otherwise, just traverse to the next container using the container's length
234
- position += currentHeader._size + currentHeader.length;
235
- _a.label = 10;
236
- case 10:
237
- i += 1;
238
- return [3 /*break*/, 3];
239
- case 11: return [2 /*return*/, currentContainer];
159
+ }
240
160
  }
241
- });
242
- });
243
- };
244
- CramFile.prototype.checkCrc32 = function (position, length, recordedCrc32, description) {
245
- return __awaiter(this, void 0, void 0, function () {
246
- var b, calculatedCrc32;
247
- return __generator(this, function (_a) {
248
- switch (_a.label) {
249
- case 0:
250
- b = Buffer.allocUnsafe(length);
251
- return [4 /*yield*/, this.file.read(b, 0, length, position)];
252
- case 1:
253
- _a.sent();
254
- calculatedCrc32 = buffer_crc32_1.default.unsigned(b);
255
- if (calculatedCrc32 !== recordedCrc32) {
256
- throw new errors_1.CramMalformedError("crc mismatch in ".concat(description, ": recorded CRC32 = ").concat(recordedCrc32, ", but calculated CRC32 = ").concat(calculatedCrc32));
257
- }
258
- return [2 /*return*/];
161
+ else {
162
+ // otherwise, just traverse to the next container using the container's length
163
+ position += currentHeader._size + currentHeader.length;
259
164
  }
260
- });
165
+ }
166
+ return currentContainer;
261
167
  });
262
- };
168
+ }
169
+ checkCrc32(position, length, recordedCrc32, description) {
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ const b = Buffer.allocUnsafe(length);
172
+ yield this.file.read(b, 0, length, position);
173
+ const calculatedCrc32 = buffer_crc32_1.default.unsigned(b);
174
+ if (calculatedCrc32 !== recordedCrc32) {
175
+ throw new errors_1.CramMalformedError(`crc mismatch in ${description}: recorded CRC32 = ${recordedCrc32}, but calculated CRC32 = ${calculatedCrc32}`);
176
+ }
177
+ });
178
+ }
263
179
  /**
264
180
  * @returns {Promise[number]} the number of containers in the file
265
181
  */
266
- CramFile.prototype.containerCount = function () {
267
- return __awaiter(this, void 0, void 0, function () {
268
- var sectionParsers, fileSize, cramContainerHeader1, containerCount, position, currentHeader, j, block;
269
- return __generator(this, function (_a) {
270
- switch (_a.label) {
271
- case 0: return [4 /*yield*/, this.getSectionParsers()];
272
- case 1:
273
- sectionParsers = _a.sent();
274
- return [4 /*yield*/, this.file.stat()];
275
- case 2:
276
- fileSize = (_a.sent()).size;
277
- cramContainerHeader1 = sectionParsers.cramContainerHeader1;
278
- containerCount = 0;
279
- position = sectionParsers.cramFileDefinition.maxLength;
280
- _a.label = 3;
281
- case 3:
282
- if (!(position + cramContainerHeader1.maxLength + 8 < fileSize)) return [3 /*break*/, 11];
283
- return [4 /*yield*/, this.getContainerAtPosition(position).getHeader()];
284
- case 4:
285
- currentHeader = _a.sent();
286
- if (!currentHeader) {
287
- return [3 /*break*/, 11];
182
+ containerCount() {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ const sectionParsers = yield this.getSectionParsers();
185
+ const { size: fileSize } = yield this.file.stat();
186
+ const { cramContainerHeader1 } = sectionParsers;
187
+ let containerCount = 0;
188
+ let position = sectionParsers.cramFileDefinition.maxLength;
189
+ while (position + cramContainerHeader1.maxLength + 8 < fileSize) {
190
+ const currentHeader = yield this.getContainerAtPosition(position).getHeader();
191
+ if (!currentHeader) {
192
+ break;
193
+ }
194
+ // if this is the first container, read all the blocks in the
195
+ // container, because we cannot trust the container
196
+ // header's given length due to a bug somewhere in htslib
197
+ if (containerCount === 0) {
198
+ position = currentHeader._endPosition;
199
+ for (let j = 0; j < currentHeader.numBlocks; j += 1) {
200
+ const block = yield this.readBlock(position);
201
+ if (block === undefined) {
202
+ return undefined;
288
203
  }
289
- if (!(containerCount === 0)) return [3 /*break*/, 9];
290
- position = currentHeader._endPosition;
291
- j = 0;
292
- _a.label = 5;
293
- case 5:
294
- if (!(j < currentHeader.numBlocks)) return [3 /*break*/, 8];
295
- return [4 /*yield*/, this.readBlock(position)];
296
- case 6:
297
- block = _a.sent();
298
204
  position = block._endPosition;
299
- _a.label = 7;
300
- case 7:
301
- j += 1;
302
- return [3 /*break*/, 5];
303
- case 8: return [3 /*break*/, 10];
304
- case 9:
305
- // otherwise, just traverse to the next container using the container's length
306
- position += currentHeader._size + currentHeader.length;
307
- _a.label = 10;
308
- case 10:
309
- containerCount += 1;
310
- return [3 /*break*/, 3];
311
- case 11: return [2 /*return*/, containerCount];
205
+ }
206
+ }
207
+ else {
208
+ // otherwise, just traverse to the next container using the container's length
209
+ position += currentHeader._size + currentHeader.length;
312
210
  }
313
- });
211
+ containerCount += 1;
212
+ }
213
+ return containerCount;
314
214
  });
315
- };
316
- CramFile.prototype.getContainerAtPosition = function (position) {
215
+ }
216
+ getContainerAtPosition(position) {
317
217
  return new container_1.default(this, position);
318
- };
319
- CramFile.prototype.readBlockHeader = function (position) {
320
- return __awaiter(this, void 0, void 0, function () {
321
- var sectionParsers, cramBlockHeader, fileSize, buffer;
322
- return __generator(this, function (_a) {
323
- switch (_a.label) {
324
- case 0: return [4 /*yield*/, this.getSectionParsers()];
325
- case 1:
326
- sectionParsers = _a.sent();
327
- cramBlockHeader = sectionParsers.cramBlockHeader;
328
- return [4 /*yield*/, this.file.stat()];
329
- case 2:
330
- fileSize = (_a.sent()).size;
331
- if (position + cramBlockHeader.maxLength >= fileSize) {
332
- return [2 /*return*/, undefined];
333
- }
334
- buffer = Buffer.allocUnsafe(cramBlockHeader.maxLength);
335
- return [4 /*yield*/, this.file.read(buffer, 0, cramBlockHeader.maxLength, position)];
336
- case 3:
337
- _a.sent();
338
- return [2 /*return*/, (0, util_1.parseItem)(buffer, cramBlockHeader.parser, 0, position)];
339
- }
340
- });
218
+ }
219
+ readBlockHeader(position) {
220
+ return __awaiter(this, void 0, void 0, function* () {
221
+ const sectionParsers = yield this.getSectionParsers();
222
+ const { cramBlockHeader } = sectionParsers;
223
+ const { size: fileSize } = yield this.file.stat();
224
+ if (position + cramBlockHeader.maxLength >= fileSize) {
225
+ return undefined;
226
+ }
227
+ const buffer = Buffer.allocUnsafe(cramBlockHeader.maxLength);
228
+ yield this.file.read(buffer, 0, cramBlockHeader.maxLength, position);
229
+ return (0, util_1.parseItem)(buffer, cramBlockHeader.parser, 0, position);
341
230
  });
342
- };
343
- CramFile.prototype._parseSection = function (section, position, size, preReadBuffer) {
344
- if (size === void 0) { size = section.maxLength; }
345
- return __awaiter(this, void 0, void 0, function () {
346
- var buffer, fileSize, data;
347
- return __generator(this, function (_a) {
348
- switch (_a.label) {
349
- case 0:
350
- if (!preReadBuffer) return [3 /*break*/, 1];
351
- buffer = preReadBuffer;
352
- return [3 /*break*/, 4];
353
- case 1: return [4 /*yield*/, this.file.stat()];
354
- case 2:
355
- fileSize = (_a.sent()).size;
356
- if (position + size >= fileSize) {
357
- return [2 /*return*/, undefined];
358
- }
359
- buffer = Buffer.allocUnsafe(size);
360
- return [4 /*yield*/, this.file.read(buffer, 0, size, position)];
361
- case 3:
362
- _a.sent();
363
- _a.label = 4;
364
- case 4:
365
- data = (0, util_1.parseItem)(buffer, section.parser, 0, position);
366
- if (data._size !== size) {
367
- throw new errors_1.CramMalformedError("section read error: requested size ".concat(size, " does not equal parsed size ").concat(data._size));
368
- }
369
- return [2 /*return*/, data];
231
+ }
232
+ _parseSection(section, position, size = section.maxLength, preReadBuffer = undefined) {
233
+ return __awaiter(this, void 0, void 0, function* () {
234
+ let buffer;
235
+ if (preReadBuffer) {
236
+ buffer = preReadBuffer;
237
+ }
238
+ else {
239
+ const { size: fileSize } = yield this.file.stat();
240
+ if (position + size >= fileSize) {
241
+ return undefined;
370
242
  }
371
- });
243
+ buffer = Buffer.allocUnsafe(size);
244
+ yield this.file.read(buffer, 0, size, position);
245
+ }
246
+ const data = (0, util_1.parseItem)(buffer, section.parser, 0, position);
247
+ if (data._size !== size) {
248
+ throw new errors_1.CramMalformedError(`section read error: requested size ${size} does not equal parsed size ${data._size}`);
249
+ }
250
+ return data;
372
251
  });
373
- };
374
- CramFile.prototype._uncompress = function (compressionMethod, inputBuffer, outputBuffer) {
252
+ }
253
+ _uncompress(compressionMethod, inputBuffer, outputBuffer) {
375
254
  if (compressionMethod === 'gzip') {
376
- var result = (0, unzip_1.unzip)(inputBuffer);
255
+ const result = (0, unzip_1.unzip)(inputBuffer);
377
256
  result.copy(outputBuffer);
378
257
  }
379
258
  else if (compressionMethod === 'bzip2') {
380
- var bits = bzip2.array(inputBuffer);
381
- var size = bzip2.header(bits);
382
- var j = 0;
259
+ const bits = bzip2.array(inputBuffer);
260
+ let size = bzip2.header(bits);
261
+ let j = 0;
262
+ let chunk;
383
263
  do {
384
- var chunk = bzip2.decompress(bits, size);
264
+ chunk = bzip2.decompress(bits, size);
385
265
  if (chunk != -1) {
386
266
  Buffer.from(chunk).copy(outputBuffer, j);
387
267
  j += chunk.length;
@@ -407,68 +287,54 @@ var CramFile = /** @class */ (function () {
407
287
  htscodecs_1.default.tok3_uncompress(inputBuffer, outputBuffer);
408
288
  }
409
289
  else {
410
- throw new errors_1.CramUnimplementedError("".concat(compressionMethod, " decompression not yet implemented"));
290
+ throw new errors_1.CramUnimplementedError(`${compressionMethod} decompression not yet implemented`);
411
291
  }
412
- };
413
- CramFile.prototype.readBlock = function (position) {
414
- return __awaiter(this, void 0, void 0, function () {
415
- var majorVersion, sectionParsers, block, blockContentPosition, uncompressedData, compressedData, crc;
416
- return __generator(this, function (_a) {
417
- switch (_a.label) {
418
- case 0: return [4 /*yield*/, this.getDefinition()];
419
- case 1:
420
- majorVersion = (_a.sent()).majorVersion;
421
- return [4 /*yield*/, this.getSectionParsers()];
422
- case 2:
423
- sectionParsers = _a.sent();
424
- return [4 /*yield*/, this.readBlockHeader(position)];
425
- case 3:
426
- block = _a.sent();
427
- blockContentPosition = block._endPosition;
428
- block.contentPosition = block._endPosition;
429
- uncompressedData = Buffer.allocUnsafe(block.uncompressedSize);
430
- if (!(block.compressionMethod !== 'raw')) return [3 /*break*/, 5];
431
- compressedData = Buffer.allocUnsafe(block.compressedSize);
432
- return [4 /*yield*/, this.read(compressedData, 0, block.compressedSize, blockContentPosition)];
433
- case 4:
434
- _a.sent();
435
- this._uncompress(block.compressionMethod, compressedData, uncompressedData);
436
- return [3 /*break*/, 7];
437
- case 5: return [4 /*yield*/, this.read(uncompressedData, 0, block.uncompressedSize, blockContentPosition)];
438
- case 6:
439
- _a.sent();
440
- _a.label = 7;
441
- case 7:
442
- block.content = uncompressedData;
443
- if (!(majorVersion >= 3)) return [3 /*break*/, 11];
444
- return [4 /*yield*/, this._parseSection(sectionParsers.cramBlockCrc32, blockContentPosition + block.compressedSize)];
445
- case 8:
446
- crc = _a.sent();
447
- block.crc32 = crc.crc32;
448
- if (!this.validateChecksums) return [3 /*break*/, 10];
449
- return [4 /*yield*/, this.checkCrc32(position, block._size + block.compressedSize, block.crc32, 'block data')];
450
- case 9:
451
- _a.sent();
452
- _a.label = 10;
453
- case 10:
454
- // make the endposition and size reflect the whole block
455
- block._endPosition = crc._endPosition;
456
- block._size =
457
- block.compressedSize + sectionParsers.cramBlockCrc32.maxLength;
458
- return [3 /*break*/, 12];
459
- case 11:
460
- block._endPosition = blockContentPosition + block.compressedSize;
461
- block._size = block.compressedSize;
462
- _a.label = 12;
463
- case 12: return [2 /*return*/, block];
292
+ }
293
+ readBlock(position) {
294
+ return __awaiter(this, void 0, void 0, function* () {
295
+ const { majorVersion } = yield this.getDefinition();
296
+ const sectionParsers = yield this.getSectionParsers();
297
+ const blockHeader = yield this.readBlockHeader(position);
298
+ if (blockHeader === undefined) {
299
+ return undefined;
300
+ }
301
+ const blockContentPosition = blockHeader._endPosition;
302
+ const uncompressedData = Buffer.allocUnsafe(blockHeader.uncompressedSize);
303
+ const block = Object.assign(Object.assign({}, blockHeader), { _endPosition: blockContentPosition, contentPosition: blockContentPosition, content: uncompressedData });
304
+ if (blockHeader.compressionMethod !== 'raw') {
305
+ const compressedData = Buffer.allocUnsafe(blockHeader.compressedSize);
306
+ yield this.read(compressedData, 0, blockHeader.compressedSize, blockContentPosition);
307
+ this._uncompress(blockHeader.compressionMethod, compressedData, uncompressedData);
308
+ }
309
+ else {
310
+ yield this.read(uncompressedData, 0, blockHeader.uncompressedSize, blockContentPosition);
311
+ }
312
+ if (majorVersion >= 3) {
313
+ // parse the crc32
314
+ const crc = yield this._parseSection(sectionParsers.cramBlockCrc32, blockContentPosition + blockHeader.compressedSize);
315
+ if (crc === undefined) {
316
+ return undefined;
464
317
  }
465
- });
318
+ block.crc32 = crc.crc32;
319
+ // check the block data crc32
320
+ if (this.validateChecksums) {
321
+ yield this.checkCrc32(position, blockHeader._size + blockHeader.compressedSize, crc.crc32, 'block data');
322
+ }
323
+ // make the endposition and size reflect the whole block
324
+ block._endPosition = crc._endPosition;
325
+ block._size =
326
+ block.compressedSize + sectionParsers.cramBlockCrc32.maxLength;
327
+ }
328
+ else {
329
+ block._endPosition = blockContentPosition + block.compressedSize;
330
+ block._size = block.compressedSize;
331
+ }
332
+ return block;
466
333
  });
467
- };
468
- return CramFile;
469
- }());
334
+ }
335
+ }
470
336
  exports.default = CramFile;
471
337
  'getDefinition getSectionParsers getSamHeader'
472
338
  .split(' ')
473
- .forEach(function (method) { return (0, util_1.tinyMemoize)(CramFile, method); });
339
+ .forEach(method => (0, util_1.tinyMemoize)(CramFile, method));
474
340
  //# sourceMappingURL=file.js.map