@gmod/cram 6.0.0 → 7.0.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 (178) hide show
  1. package/dist/craiIndex.js +2 -2
  2. package/dist/craiIndex.js.map +1 -1
  3. package/dist/cram-bundle.js +1 -1
  4. package/dist/cramFile/file.d.ts +2 -2
  5. package/dist/cramFile/file.js +9 -13
  6. package/dist/cramFile/file.js.map +1 -1
  7. package/dist/htscodecs/index.d.ts +1 -5
  8. package/dist/htscodecs/index.js +11 -72
  9. package/dist/htscodecs/index.js.map +1 -1
  10. package/dist/htscodecs-wasm.d.ts +7 -0
  11. package/dist/htscodecs-wasm.js +183 -0
  12. package/dist/htscodecs-wasm.js.map +1 -0
  13. package/dist/unzip.d.ts +1 -1
  14. package/dist/unzip.js +3 -3
  15. package/dist/unzip.js.map +1 -1
  16. package/dist/wasm/htscodecs.d.ts +5 -0
  17. package/dist/wasm/htscodecs.js +0 -0
  18. package/dist/wasm/htscodecs.js.map +1 -0
  19. package/esm/craiIndex.js +2 -2
  20. package/esm/craiIndex.js.map +1 -1
  21. package/esm/cramFile/file.d.ts +2 -2
  22. package/esm/cramFile/file.js +7 -11
  23. package/esm/cramFile/file.js.map +1 -1
  24. package/esm/htscodecs/index.d.ts +1 -5
  25. package/esm/htscodecs/index.js +3 -31
  26. package/esm/htscodecs/index.js.map +1 -1
  27. package/esm/htscodecs-wasm.d.ts +7 -0
  28. package/esm/htscodecs-wasm.js +171 -0
  29. package/esm/htscodecs-wasm.js.map +1 -0
  30. package/esm/unzip.d.ts +1 -1
  31. package/esm/unzip.js +3 -3
  32. package/esm/unzip.js.map +1 -1
  33. package/esm/wasm/htscodecs.d.ts +5 -0
  34. package/esm/wasm/htscodecs.js +0 -0
  35. package/esm/wasm/htscodecs.js.map +1 -0
  36. package/package.json +9 -9
  37. package/src/craiIndex.ts +4 -2
  38. package/src/cramFile/file.ts +7 -11
  39. package/src/htscodecs/index.ts +12 -38
  40. package/src/htscodecs-wasm.ts +210 -0
  41. package/src/unzip.ts +3 -3
  42. package/src/wasm/htscodecs.d.ts +58 -0
  43. package/src/wasm/htscodecs.js +0 -0
  44. package/dist/htscodecs/arith_gen.d.ts +0 -16
  45. package/dist/htscodecs/arith_gen.js +0 -323
  46. package/dist/htscodecs/arith_gen.js.map +0 -1
  47. package/dist/htscodecs/arith_sh.d.ts +0 -9
  48. package/dist/htscodecs/arith_sh.js +0 -134
  49. package/dist/htscodecs/arith_sh.js.map +0 -1
  50. package/dist/htscodecs/byte_model.d.ts +0 -6
  51. package/dist/htscodecs/byte_model.js +0 -121
  52. package/dist/htscodecs/byte_model.js.map +0 -1
  53. package/dist/htscodecs/fqzcomp.d.ts +0 -1
  54. package/dist/htscodecs/fqzcomp.js +0 -346
  55. package/dist/htscodecs/fqzcomp.js.map +0 -1
  56. package/dist/htscodecs/iostream.d.ts +0 -16
  57. package/dist/htscodecs/iostream.js +0 -148
  58. package/dist/htscodecs/iostream.js.map +0 -1
  59. package/dist/htscodecs/rans.d.ts +0 -1
  60. package/dist/htscodecs/rans.js +0 -224
  61. package/dist/htscodecs/rans.js.map +0 -1
  62. package/dist/htscodecs/rans4x16.d.ts +0 -1
  63. package/dist/htscodecs/rans4x16.js +0 -445
  64. package/dist/htscodecs/rans4x16.js.map +0 -1
  65. package/dist/htscodecs/tok3.d.ts +0 -1
  66. package/dist/htscodecs/tok3.js +0 -206
  67. package/dist/htscodecs/tok3.js.map +0 -1
  68. package/dist/rans/constants.d.ts +0 -4
  69. package/dist/rans/constants.js +0 -10
  70. package/dist/rans/constants.js.map +0 -1
  71. package/dist/rans/d04.d.ts +0 -1
  72. package/dist/rans/d04.js +0 -100
  73. package/dist/rans/d04.js.map +0 -1
  74. package/dist/rans/d14.d.ts +0 -1
  75. package/dist/rans/d14.js +0 -96
  76. package/dist/rans/d14.js.map +0 -1
  77. package/dist/rans/decoding.d.ts +0 -27
  78. package/dist/rans/decoding.js +0 -119
  79. package/dist/rans/decoding.js.map +0 -1
  80. package/dist/rans/frequencies.d.ts +0 -2
  81. package/dist/rans/frequencies.js +0 -112
  82. package/dist/rans/frequencies.js.map +0 -1
  83. package/dist/rans/index.d.ts +0 -1
  84. package/dist/rans/index.js +0 -198
  85. package/dist/rans/index.js.map +0 -1
  86. package/dist/seek-bzip/bitreader.d.ts +0 -27
  87. package/dist/seek-bzip/bitreader.js +0 -101
  88. package/dist/seek-bzip/bitreader.js.map +0 -1
  89. package/dist/seek-bzip/crc32.d.ts +0 -26
  90. package/dist/seek-bzip/crc32.js +0 -115
  91. package/dist/seek-bzip/crc32.js.map +0 -1
  92. package/dist/seek-bzip/index.d.ts +0 -9
  93. package/dist/seek-bzip/index.js +0 -585
  94. package/dist/seek-bzip/index.js.map +0 -1
  95. package/dist/seek-bzip/stream.d.ts +0 -8
  96. package/dist/seek-bzip/stream.js +0 -37
  97. package/dist/seek-bzip/stream.js.map +0 -1
  98. package/dist/seek-bzip/toHex.d.ts +0 -1
  99. package/dist/seek-bzip/toHex.js +0 -36
  100. package/dist/seek-bzip/toHex.js.map +0 -1
  101. package/esm/htscodecs/arith_gen.d.ts +0 -16
  102. package/esm/htscodecs/arith_gen.js +0 -317
  103. package/esm/htscodecs/arith_gen.js.map +0 -1
  104. package/esm/htscodecs/arith_sh.d.ts +0 -9
  105. package/esm/htscodecs/arith_sh.js +0 -131
  106. package/esm/htscodecs/arith_sh.js.map +0 -1
  107. package/esm/htscodecs/byte_model.d.ts +0 -6
  108. package/esm/htscodecs/byte_model.js +0 -118
  109. package/esm/htscodecs/byte_model.js.map +0 -1
  110. package/esm/htscodecs/fqzcomp.d.ts +0 -1
  111. package/esm/htscodecs/fqzcomp.js +0 -340
  112. package/esm/htscodecs/fqzcomp.js.map +0 -1
  113. package/esm/htscodecs/iostream.d.ts +0 -16
  114. package/esm/htscodecs/iostream.js +0 -145
  115. package/esm/htscodecs/iostream.js.map +0 -1
  116. package/esm/htscodecs/rans.d.ts +0 -1
  117. package/esm/htscodecs/rans.js +0 -218
  118. package/esm/htscodecs/rans.js.map +0 -1
  119. package/esm/htscodecs/rans4x16.d.ts +0 -1
  120. package/esm/htscodecs/rans4x16.js +0 -439
  121. package/esm/htscodecs/rans4x16.js.map +0 -1
  122. package/esm/htscodecs/tok3.d.ts +0 -1
  123. package/esm/htscodecs/tok3.js +0 -167
  124. package/esm/htscodecs/tok3.js.map +0 -1
  125. package/esm/rans/constants.d.ts +0 -4
  126. package/esm/rans/constants.js +0 -5
  127. package/esm/rans/constants.js.map +0 -1
  128. package/esm/rans/d04.d.ts +0 -1
  129. package/esm/rans/d04.js +0 -94
  130. package/esm/rans/d04.js.map +0 -1
  131. package/esm/rans/d14.d.ts +0 -1
  132. package/esm/rans/d14.js +0 -93
  133. package/esm/rans/d14.js.map +0 -1
  134. package/esm/rans/decoding.d.ts +0 -27
  135. package/esm/rans/decoding.js +0 -117
  136. package/esm/rans/decoding.js.map +0 -1
  137. package/esm/rans/frequencies.d.ts +0 -2
  138. package/esm/rans/frequencies.js +0 -105
  139. package/esm/rans/frequencies.js.map +0 -1
  140. package/esm/rans/index.d.ts +0 -1
  141. package/esm/rans/index.js +0 -192
  142. package/esm/rans/index.js.map +0 -1
  143. package/esm/seek-bzip/bitreader.d.ts +0 -27
  144. package/esm/seek-bzip/bitreader.js +0 -98
  145. package/esm/seek-bzip/bitreader.js.map +0 -1
  146. package/esm/seek-bzip/crc32.d.ts +0 -26
  147. package/esm/seek-bzip/crc32.js +0 -112
  148. package/esm/seek-bzip/crc32.js.map +0 -1
  149. package/esm/seek-bzip/index.d.ts +0 -9
  150. package/esm/seek-bzip/index.js +0 -579
  151. package/esm/seek-bzip/index.js.map +0 -1
  152. package/esm/seek-bzip/stream.d.ts +0 -8
  153. package/esm/seek-bzip/stream.js +0 -34
  154. package/esm/seek-bzip/stream.js.map +0 -1
  155. package/esm/seek-bzip/toHex.d.ts +0 -1
  156. package/esm/seek-bzip/toHex.js +0 -33
  157. package/esm/seek-bzip/toHex.js.map +0 -1
  158. package/src/htscodecs/arith_gen.ts +0 -371
  159. package/src/htscodecs/arith_sh.ts +0 -146
  160. package/src/htscodecs/byte_model.ts +0 -136
  161. package/src/htscodecs/fqzcomp.ts +0 -394
  162. package/src/htscodecs/iostream.ts +0 -159
  163. package/src/htscodecs/rans.ts +0 -250
  164. package/src/htscodecs/rans4x16.ts +0 -521
  165. package/src/htscodecs/tok3.ts +0 -197
  166. package/src/rans/README.md +0 -1
  167. package/src/rans/constants.ts +0 -5
  168. package/src/rans/d04.ts +0 -112
  169. package/src/rans/d14.ts +0 -106
  170. package/src/rans/decoding.ts +0 -138
  171. package/src/rans/frequencies.ts +0 -115
  172. package/src/rans/index.ts +0 -245
  173. package/src/seek-bzip/README.md +0 -6
  174. package/src/seek-bzip/bitreader.ts +0 -113
  175. package/src/seek-bzip/crc32.ts +0 -121
  176. package/src/seek-bzip/index.ts +0 -615
  177. package/src/seek-bzip/stream.ts +0 -33
  178. package/src/seek-bzip/toHex.ts +0 -33
@@ -1,521 +0,0 @@
1
- /* eslint-disable no-var */
2
- // @ts-nocheck
3
-
4
- /*
5
- * Copyright (c) 2019,2020 Genome Research Ltd.
6
- * Author(s): James Bonfield
7
- *
8
- * Redistribution and use in source and binary forms, with or without
9
- * modification, are permitted provided that the following conditions are met:
10
- *
11
- * 1. Redistributions of source code must retain the above copyright notice,
12
- * this list of conditions and the following disclaimer.
13
- *
14
- * 2. Redistributions in binary form must reproduce the above
15
- * copyright notice, this list of conditions and the following
16
- * disclaimer in the documentation and/or other materials provided
17
- * with the distribution.
18
- *
19
- * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
20
- * Institute nor the names of its contributors may be used to endorse
21
- * or promote products derived from this software without specific
22
- * prior written permission.
23
- *
24
- * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
25
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
28
- * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
- */
36
-
37
- import IOStream from './iostream.ts'
38
-
39
- // ----------------------------------------------------------------------
40
- // rANS primitives itself
41
- //
42
- // RansGet* is decoder side
43
-
44
- function RansGetCumulativeFreq(R, bits) {
45
- return R & ((1 << bits) - 1)
46
- }
47
-
48
- function RansGetSymbolFromFreq(C, f) {
49
- // NOTE: Inefficient.
50
- // In practice we would implement this via a precomputed
51
- // lookup table C2S[f]; see RansBuildC2S below.
52
- let s = 0
53
- while (f >= C[s + 1]) {
54
- s++
55
- }
56
-
57
- // console.error(f, C, s)
58
-
59
- return s
60
- }
61
-
62
- function RansBuildC2S(C, bits) {
63
- const max = 1 << bits
64
- const C2S = new Array(max)
65
- let s = 0
66
- for (let f = 0; f < max; f++) {
67
- while (f >= C[s + 1]) {
68
- s++
69
- }
70
- C2S[f] = s
71
- }
72
- return C2S
73
- }
74
-
75
- function RansAdvanceStep(R, c, f, bits) {
76
- return f * (R >> bits) + (R & ((1 << bits) - 1)) - c
77
- }
78
-
79
- function RansRenorm(src, R) {
80
- if (R < 1 << 15) {
81
- R = (R << 16) + src.ReadUint16()
82
- }
83
-
84
- return R
85
- }
86
-
87
- function DecodeRLEMeta(src, N) {
88
- const u_meta_len = src.ReadUint7()
89
- const rle_len = src.ReadUint7()
90
-
91
- // Decode RLE lengths
92
- if (u_meta_len & 1) {
93
- var rle_meta = src.ReadData((u_meta_len - 1) / 2)
94
- } else {
95
- const comp_meta_len = src.ReadUint7()
96
- var rle_meta = src.ReadData(comp_meta_len)
97
- rle_meta = RansDecode0(new IOStream(rle_meta), u_meta_len / 2, N)
98
- }
99
-
100
- // Decode list of symbols for which RLE lengths are applied
101
- var rle_meta = new IOStream(rle_meta)
102
- const L = new Array(256)
103
- let n = rle_meta.ReadByte()
104
- if (n == 0) {
105
- n = 256
106
- }
107
- for (let i = 0; i < n; i++) {
108
- L[rle_meta.ReadByte()] = 1
109
- }
110
-
111
- return [L, rle_meta, rle_len]
112
- }
113
-
114
- function DecodeRLE(buf, L, rle_meta, len) {
115
- const src = new IOStream(buf)
116
-
117
- const out = new Uint8Array(len)
118
-
119
- // Expand up buf+meta to out; i = buf index, j = out index
120
- let j = 0
121
- for (let i = 0; j < len; i++) {
122
- const sym = buf[i]
123
- if (L[sym]) {
124
- const run = rle_meta.ReadUint7()
125
- for (let r = 0; r <= run; r++) {
126
- out[j++] = sym
127
- }
128
- } else {
129
- out[j++] = sym
130
- }
131
- }
132
-
133
- return out
134
- }
135
-
136
- // Pack meta data is the number and value of distinct symbols plus
137
- // the length of the packed byte stream.
138
- function DecodePackMeta(src) {
139
- const nsym = src.ReadByte()
140
- const P = new Array(nsym)
141
-
142
- for (let i = 0; i < nsym; i++) {
143
- P[i] = src.ReadByte()
144
- }
145
-
146
- const len = src.ReadUint7()
147
-
148
- return [P, nsym, len]
149
- }
150
-
151
- // Extract bits from src producing output of length len.
152
- // Nsym is number of distinct symbols used.
153
- function DecodePack(data, P, nsym, len) {
154
- const out = new Uint8Array(len)
155
- let j = 0
156
-
157
- // Constant value
158
- if (nsym <= 1) {
159
- for (var i = 0; i < len; i++) {
160
- out[i] = P[0]
161
- }
162
- }
163
-
164
- // 1 bit per value
165
- else if (nsym <= 2) {
166
- for (i = 0; i < len; i++) {
167
- if (i % 8 == 0) {
168
- var v = data[j++]
169
- }
170
-
171
- out[i] = P[v & 1]
172
- v >>= 1
173
- }
174
- }
175
-
176
- // 2 bits per value
177
- else if (nsym <= 4) {
178
- for (i = 0; i < len; i++) {
179
- if (i % 4 == 0) {
180
- var v = data[j++]
181
- }
182
-
183
- out[i] = P[v & 3]
184
- v >>= 2
185
- }
186
- }
187
-
188
- // 4 bits per value
189
- else if (nsym <= 16) {
190
- for (i = 0; i < len; i++) {
191
- if (i % 2 == 0) {
192
- var v = data[j++]
193
- }
194
-
195
- out[i] = P[v & 15]
196
- v >>= 4
197
- }
198
- }
199
-
200
- return out
201
- }
202
-
203
- function RansDecodeStripe(src, len) {
204
- const N = src.ReadByte()
205
-
206
- // Retrieve lengths
207
- const clen = new Array(N)
208
- const ulen = new Array(N)
209
- for (var j = 0; j < N; j++) {
210
- clen[j] = src.ReadUint7()
211
- }
212
-
213
- // Decode streams
214
- const T = new Array(N)
215
- for (var j = 0; j < N; j++) {
216
- ulen[j] = Math.floor(len / N) + (len % N > j)
217
- T[j] = RansDecodeStream(src, ulen[j])
218
- }
219
-
220
- // Transpose
221
- const out = new Uint8Array(len)
222
- for (var j = 0; j < N; j++) {
223
- for (let i = 0; i < ulen[j]; i++) {
224
- out[i * N + j] = T[j][i]
225
- }
226
- }
227
-
228
- return out
229
- }
230
-
231
- // ----------------------------------------------------------------------
232
- // Main rANS entry function: decodes a compressed src and
233
- // returns the uncompressed buffer.
234
- export function decode(src) {
235
- const stream = new IOStream(src)
236
- return RansDecodeStream(stream, 0)
237
- }
238
-
239
- function RansDecodeStream(stream, n_out) {
240
- const format = stream.ReadByte()
241
- const order = format & 1
242
- const x32 = format & 4
243
- const stripe = format & 8
244
- const nosz = format & 16
245
- const cat = format & 32
246
- const rle = format & 64
247
- const pack = format & 128
248
-
249
- const Nway = x32 ? 32 : 4
250
-
251
- if (!nosz) {
252
- n_out = stream.ReadUint7()
253
- }
254
-
255
- // N-way interleaving
256
- if (stripe) {
257
- return RansDecodeStripe(stream, n_out)
258
- }
259
-
260
- // Bit packing
261
- if (pack) {
262
- var pack_len = n_out
263
- var [P, nsym, n_out] = DecodePackMeta(stream)
264
- }
265
-
266
- // Run length encoding
267
- if (rle) {
268
- var rle_len = n_out
269
- var [L, rle_meta, n_out] = DecodeRLEMeta(stream, Nway)
270
- }
271
-
272
- // Uncompress data (all, packed or run literals)
273
- if (cat) {
274
- var buf = stream.ReadData(n_out)
275
- } else if (order == 0) {
276
- var buf = RansDecode0(stream, n_out, Nway)
277
- } else {
278
- var buf = RansDecode1(stream, n_out, Nway)
279
- }
280
-
281
- // Apply expansion transforms
282
- if (rle) {
283
- buf = DecodeRLE(buf, L, rle_meta, rle_len)
284
- }
285
-
286
- if (pack) {
287
- buf = DecodePack(buf, P, nsym, pack_len)
288
- }
289
-
290
- return buf
291
- }
292
-
293
- // ----------------------------------------------------------------------
294
- // Order-0 decoder
295
-
296
- function ReadAlphabet(src) {
297
- const A = new Array(256)
298
- for (let i = 0; i < 256; i++) {
299
- A[i] = 0
300
- }
301
-
302
- let rle = 0
303
- let sym = src.ReadByte()
304
- let last_sym = sym
305
-
306
- do {
307
- A[sym] = 1
308
- if (rle > 0) {
309
- rle--
310
- sym++
311
- } else {
312
- sym = src.ReadByte()
313
- if (sym == last_sym + 1) {
314
- rle = src.ReadByte()
315
- }
316
- }
317
- last_sym = sym
318
- } while (sym != 0)
319
-
320
- return A
321
- }
322
-
323
- // Decode a single table of order-0 frequencies,
324
- // filling out the F and C arrays.
325
- function ReadFrequencies0(src, F, C) {
326
- // Initialise; not in the specification - implicit?
327
- for (var i = 0; i < 256; i++) {
328
- F[i] = 0
329
- }
330
-
331
- // Fetch alphabet
332
- const A = ReadAlphabet(src)
333
-
334
- // Fetch frequencies for the symbols listed in our alphabet
335
- for (var i = 0; i < 256; i++) {
336
- if (A[i] > 0) {
337
- F[i] = src.ReadUint7()
338
- }
339
- }
340
-
341
- NormaliseFrequencies0_Shift(F, 12)
342
-
343
- // Compute C[] from F[]
344
- C[0] = 0
345
- for (var i = 0; i <= 255; i++) {
346
- C[i + 1] = C[i] + F[i]
347
- }
348
- }
349
-
350
- function RansDecode0(src, nbytes, N) {
351
- // Decode frequencies
352
- const F = new Array(256)
353
- const C = new Array(256)
354
- ReadFrequencies0(src, F, C)
355
-
356
- // Fast lookup to avoid slow RansGetSymbolFromFreq
357
- const C2S = RansBuildC2S(C, 12)
358
-
359
- // Initialise rANS state
360
- const R = new Array(N)
361
- for (var i = 0; i < N; i++) {
362
- R[i] = src.ReadUint32()
363
- }
364
-
365
- // Main decode loop
366
- const output = new Uint8Array(nbytes)
367
- for (var i = 0; i < nbytes; i++) {
368
- const ix = i & (N - 1) // equiv to i%N as N is power of 2
369
- const f = RansGetCumulativeFreq(R[ix], 12)
370
- const s = C2S[f] // Equiv to RansGetSymbolFromFreq(C, f);
371
-
372
- output[i] = s
373
- R[ix] = RansAdvanceStep(R[ix], C[s], F[s], 12)
374
- R[ix] = RansRenorm(src, R[ix])
375
- }
376
-
377
- // Main decode loop
378
- return output
379
- }
380
-
381
- function NormaliseFrequencies0_Shift(F, bits) {
382
- // Compute total and number of bits to shift by
383
- let tot = 0
384
- for (var i = 0; i < 256; i++) {
385
- tot += F[i]
386
- }
387
-
388
- if (tot == 0 || tot == 1 << bits) {
389
- return
390
- }
391
-
392
- let shift = 0
393
- while (tot < 1 << bits) {
394
- tot *= 2
395
- shift++
396
- }
397
-
398
- // Scale total of frequencies to (1<<bits)
399
- for (var i = 0; i < 256; i++) {
400
- F[i] <<= shift
401
- }
402
- }
403
- // ----------------------------------------------------------------------
404
- // Order-1 decoder
405
-
406
- // Decode a table of order-1 frequencies,
407
- // filling out the F and C arrays.
408
- function ReadFrequencies1(src, F, C, shift) {
409
- // Initialise; not in the specification - implicit?
410
- for (var i = 0; i < 256; i++) {
411
- F[i] = new Array(256)
412
- C[i] = new Array(256)
413
- for (var j = 0; j < 256; j++) {
414
- F[i][j] = 0
415
- }
416
- }
417
-
418
- // Fetch alphabet
419
- const A = ReadAlphabet(src)
420
-
421
- // Read F[]
422
- for (var i = 0; i < 256; i++) {
423
- if (!A[i]) {
424
- continue
425
- }
426
-
427
- let run = 0
428
- for (var j = 0; j < 256; j++) {
429
- if (!A[j]) {
430
- continue
431
- }
432
-
433
- if (run > 0) {
434
- run--
435
- } else {
436
- F[i][j] = src.ReadUint7()
437
- if (F[i][j] == 0) {
438
- run = src.ReadByte()
439
- }
440
- }
441
- }
442
-
443
- NormaliseFrequencies0_Shift(F[i], shift)
444
-
445
- // Compute C[] from F[]
446
- C[i][0] = 0
447
- for (var j = 0; j < 256; j++) {
448
- C[i][j + 1] = C[i][j] + F[i][j]
449
- }
450
- }
451
- }
452
-
453
- function RansDecode1(src, nbytes, N) {
454
- // FIXME: this bit is missing from the RansDecode0 pseudocode.
455
-
456
- var comp = src.ReadByte()
457
- const shift = comp >> 4
458
-
459
- var freq_src = src
460
- if (comp & 1) {
461
- const ulen = src.ReadUint7()
462
- const clen = src.ReadUint7()
463
- var comp = new IOStream(src.ReadData(clen))
464
- var freq_src = new IOStream(RansDecode0(comp, ulen, 4))
465
- }
466
-
467
- // Decode frequencies
468
- const F = new Array(256)
469
- const C = new Array(256)
470
- ReadFrequencies1(freq_src, F, C, shift)
471
-
472
- // Fast lookup to avoid slow RansGetSymbolFromFreq
473
- const C2S = new Array(256)
474
- for (
475
- var i = 0;
476
- i < 256;
477
- i++ // Could do only for symbols in alphabet?
478
- ) {
479
- C2S[i] = RansBuildC2S(C[i], shift)
480
- }
481
-
482
- // Initialise rANS state
483
- const R = new Array(N)
484
- const L = new Array(N)
485
- for (var j = 0; j < N; j++) {
486
- R[j] = src.ReadUint32()
487
- L[j] = 0
488
- }
489
-
490
- // Main decode loop
491
- const output = new Uint8Array(nbytes)
492
- const nbytesx = Math.floor(nbytes / N)
493
- for (var i = 0; i < nbytesx; i++) {
494
- for (var j = 0; j < N; j++) {
495
- var f = RansGetCumulativeFreq(R[j], shift)
496
-
497
- // var s = RansGetSymbolFromFreq(C[L[j]], f);
498
- var s = C2S[L[j]][f] // Precomputed version of above
499
-
500
- output[i + j * nbytesx] = s
501
- R[j] = RansAdvanceStep(R[j], C[L[j]][s], F[L[j]][s], shift)
502
- R[j] = RansRenorm(src, R[j])
503
- L[j] = s
504
- }
505
- }
506
-
507
- // Now deal with the remainder if buffer size is not a multiple of N,
508
- // using the last rANS state exclusively. (It'd have been nice to have
509
- // designed this to just act as if we kept going with a bail out.)
510
- i = N * i
511
- while (i < nbytes) {
512
- var f = RansGetCumulativeFreq(R[N - 1], shift)
513
- var s = RansGetSymbolFromFreq(C[L[N - 1]], f)
514
- output[i++] = s
515
- R[N - 1] = RansAdvanceStep(R[N - 1], C[L[N - 1]][s], F[L[N - 1]][s], shift)
516
- R[N - 1] = RansRenorm(src, R[N - 1])
517
- L[N - 1] = s
518
- }
519
-
520
- return output
521
- }
@@ -1,197 +0,0 @@
1
- /* eslint-disable no-var */
2
- // @ts-nocheck
3
-
4
- /*
5
- * Copyright (c) 2019 Genome Research Ltd.
6
- * Author(s): James Bonfield
7
- *
8
- * Redistribution and use in source and binary forms, with or without
9
- * modification, are permitted provided that the following conditions are met:
10
- *
11
- * 1. Redistributions of source code must retain the above copyright notice,
12
- * this list of conditions and the following disclaimer.
13
- *
14
- * 2. Redistributions in binary form must reproduce the above
15
- * copyright notice, this list of conditions and the following
16
- * disclaimer in the documentation and/or other materials provided
17
- * with the distribution.
18
- *
19
- * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
20
- * Institute nor the names of its contributors may be used to endorse
21
- * or promote products derived from this software without specific
22
- * prior written permission.
23
- *
24
- * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
25
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
28
- * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
- */
36
-
37
- // Name tokeniser
38
- //
39
- // This is a reference implementation designed to match the
40
- // written specification as closely as possible. It is *NOT*
41
- // an efficient implementation, but see comments below.
42
-
43
- import { concatUint8Array } from '../util.ts'
44
- import arith_gen from './arith_gen.ts'
45
- import IOStream from './iostream.ts'
46
- import * as rans from './rans4x16.ts'
47
-
48
- const arith = new arith_gen()
49
-
50
- const TOK_TYPE = 0
51
- const TOK_STRING = 1
52
- const TOK_CHAR = 2
53
- const TOK_DIGITS0 = 3
54
- const TOK_DZLEN = 4
55
- const TOK_DUP = 5
56
- const TOK_DIFF = 6
57
- const TOK_DIGITS = 7
58
- const TOK_DELTA = 8
59
- const TOK_DELTA0 = 9
60
- const TOK_MATCH = 10
61
- const TOK_NOP = 11
62
- const TOK_END = 12
63
-
64
- // ----------------------------------------------------------------------
65
- // Token byte streams
66
- function DecodeTokenByteStreams(src, in_size, use_arith, nnames) {
67
- let t = -1
68
-
69
- const B = new Array(256)
70
-
71
- while (!src.EOF()) {
72
- const ttype = src.ReadByte()
73
- const tok_new = ttype & 128
74
- const tok_dup = ttype & 64
75
- const type = ttype & 63
76
-
77
- if (tok_new) {
78
- t++
79
- B[t] = new Array(13)
80
- }
81
-
82
- if (type != TOK_TYPE && tok_new) {
83
- const M = new Array(nnames - 1).fill(TOK_MATCH)
84
- B[t][TOK_TYPE] = new IOStream(concatUint8Array([new Uint8Array(type), M]))
85
- }
86
-
87
- if (tok_dup) {
88
- const dup_pos = src.ReadByte()
89
- const dup_type = src.ReadByte()
90
- B[t][type] = new IOStream(B[dup_pos][dup_type].buf)
91
- } else {
92
- const clen = src.ReadUint7()
93
- const data = src.ReadData(clen)
94
-
95
- B[t][type] = use_arith ? arith.decode(data) : rans.decode(data)
96
- B[t][type] = new IOStream(B[t][type])
97
- }
98
- }
99
-
100
- return B
101
- }
102
-
103
- // ----------------------------------------------------------------------
104
- // Token decode
105
- function LeftPadNumber(val, len) {
106
- let str = val + ''
107
- while (str.length < len) {
108
- str = '0' + str
109
- }
110
-
111
- return str
112
- }
113
-
114
- function DecodeSingleName(B, N, T, n) {
115
- let type = B[0][TOK_TYPE].ReadByte()
116
- const dist = B[0][type].ReadUint32()
117
- const m = n - dist
118
-
119
- if (type == TOK_DUP) {
120
- N[n] = N[m]
121
- T[n] = T[m]
122
- return N[n]
123
- }
124
-
125
- let t = 1
126
- N[n] = ''
127
- T[n] = new Array(256)
128
- do {
129
- type = B[t][TOK_TYPE].ReadByte()
130
-
131
- switch (type) {
132
- case TOK_CHAR:
133
- T[n][t] = B[t][TOK_CHAR].ReadChar()
134
- break
135
-
136
- case TOK_STRING:
137
- T[n][t] = B[t][TOK_STRING].ReadString()
138
- break
139
-
140
- case TOK_DIGITS:
141
- T[n][t] = B[t][TOK_DIGITS].ReadUint32()
142
- break
143
-
144
- case TOK_DIGITS0:
145
- var d = B[t][TOK_DIGITS0].ReadUint32()
146
- var l = B[t][TOK_DZLEN].ReadByte()
147
- T[n][t] = LeftPadNumber(d, l)
148
- break
149
-
150
- case TOK_DELTA:
151
- T[n][t] = (T[m][t] >> 0) + B[t][TOK_DELTA].ReadByte()
152
- break
153
-
154
- case TOK_DELTA0:
155
- var d = (T[m][t] >> 0) + B[t][TOK_DELTA0].ReadByte()
156
- var l = T[m][t].length
157
- T[n][t] = LeftPadNumber(d, l)
158
- break
159
-
160
- case TOK_MATCH:
161
- T[n][t] = T[m][t]
162
- break
163
-
164
- default:
165
- T[n][t] = ''
166
- break
167
- }
168
-
169
- N[n] += T[n][t++]
170
- } while (type != TOK_END)
171
-
172
- return N[n]
173
- }
174
-
175
- // ----------------------------------------------------------------------
176
- // Main tokeniser decode entry function: decodes a compressed src and
177
- // returns the uncompressed buffer.
178
- export function decode(src, len, separator) {
179
- var src = new IOStream(src)
180
- const ulen = src.ReadUint32()
181
- const nnames = src.ReadUint32()
182
- const use_arith = src.ReadByte()
183
-
184
- const B = DecodeTokenByteStreams(src, len, use_arith, nnames)
185
- const N = new Array(nnames)
186
- const T = new Array(nnames)
187
-
188
- let str = ''
189
- if (separator === undefined) {
190
- separator = '\n'
191
- }
192
- for (let i = 0; i < nnames; i++) {
193
- str += DecodeSingleName(B, N, T, i) + separator
194
- }
195
-
196
- return str
197
- }
@@ -1 +0,0 @@
1
- Ported from htsjdk's rANS implementation
@@ -1,5 +0,0 @@
1
- const TF_SHIFT = 12
2
- const TOTFREQ = 1 << TF_SHIFT
3
- const RANS_BYTE_L = 1 << 23
4
-
5
- export { RANS_BYTE_L, TF_SHIFT, TOTFREQ }