@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,33 +0,0 @@
1
- // from https://www.xaymar.com/articles/2020/12/08/fastest-uint8array-to-hex-string-conversion-in-javascript/
2
- // Pre-Init
3
- const LUT_HEX_4b = [
4
- '0',
5
- '1',
6
- '2',
7
- '3',
8
- '4',
9
- '5',
10
- '6',
11
- '7',
12
- '8',
13
- '9',
14
- 'A',
15
- 'B',
16
- 'C',
17
- 'D',
18
- 'E',
19
- 'F',
20
- ];
21
- const LUT_HEX_8b = new Array(0x100);
22
- for (let n = 0; n < 0x100; n++) {
23
- LUT_HEX_8b[n] = `${LUT_HEX_4b[(n >>> 4) & 0xf]}${LUT_HEX_4b[n & 0xf]}`;
24
- }
25
- // End Pre-Init
26
- export function toHex(buffer) {
27
- let out = '';
28
- for (let idx = 0, edx = buffer.length; idx < edx; idx++) {
29
- out += LUT_HEX_8b[buffer[idx]];
30
- }
31
- return out;
32
- }
33
- //# sourceMappingURL=toHex.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"toHex.js","sourceRoot":"","sources":["../../src/seek-bzip/toHex.ts"],"names":[],"mappings":"AAAA,6GAA6G;AAE7G,WAAW;AACX,MAAM,UAAU,GAAG;IACjB,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;IACH,GAAG;CACJ,CAAA;AACD,MAAM,UAAU,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,CAAA;AACnC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;IAC/B,UAAU,CAAC,CAAC,CAAC,GAAG,GAAG,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,GAAG,GAAG,CAAC,EAAE,CAAA;AACxE,CAAC;AACD,eAAe;AACf,MAAM,UAAU,KAAK,CAAC,MAAkB;IACtC,IAAI,GAAG,GAAG,EAAE,CAAA;IACZ,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC;QACxD,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,GAAG,CAAE,CAAC,CAAA;IACjC,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
@@ -1,371 +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 { decode } from '../seek-bzip/index.ts'
38
-
39
- import RangeCoder from './arith_sh.ts'
40
- import ByteModel from './byte_model.ts'
41
- import IOStream from './iostream.ts'
42
- import { concatUint8Array } from '../util.ts'
43
-
44
- const ARITH_ORDER = 1
45
- const ARITH_EXT = 4
46
- const ARITH_STRIPE = 8
47
- const ARITH_NOSIZE = 16
48
- const ARITH_CAT = 32
49
- const ARITH_RLE = 64
50
- const ARITH_PACK = 128
51
-
52
- export default class RangeCoderGen {
53
- stream: IOStream
54
- decode(src: Uint8Array) {
55
- this.stream = new IOStream(src)
56
- return this.decodeStream(this.stream)
57
- }
58
-
59
- decodeStream(stream: IOStream, n_out = 0) {
60
- const flags = this.stream.ReadByte()
61
- if (!(flags & ARITH_NOSIZE)) {
62
- n_out = this.stream.ReadUint7()
63
- }
64
- let e_len = n_out
65
-
66
- const order = flags & ARITH_ORDER
67
-
68
- // 4-way recursion
69
- if (flags & ARITH_STRIPE) {
70
- return this.decodeStripe(this.stream, n_out)
71
- }
72
-
73
- // Meta data
74
- if (flags & ARITH_PACK) {
75
- var P
76
- ;[P, e_len] = this.decodePackMeta(this.stream)
77
- }
78
-
79
- // NOP, useful for tiny blocks
80
- if (flags & ARITH_CAT) {
81
- var data = this.decodeCat(this.stream, e_len)
82
- }
83
- // Entropy decode
84
- else if (flags & ARITH_EXT) {
85
- var data = this.decodeExt(this.stream, e_len)
86
- } else if (flags & ARITH_RLE) {
87
- var data = order
88
- ? this.decodeRLE1(this.stream, e_len)
89
- : this.decodeRLE0(this.stream, e_len)
90
- } else {
91
- var data = order
92
- ? this.decode1(this.stream, e_len)
93
- : this.decode0(this.stream, e_len)
94
- }
95
-
96
- // Transforms
97
- if (flags & ARITH_PACK) {
98
- data = this.decodePack(data, P, n_out)
99
- }
100
-
101
- return data
102
- }
103
-
104
- // ----------------------------------------------------------------------
105
- // Order-0 codec
106
- decode0(stream, n_out) {
107
- const output = new Uint8Array(n_out)
108
-
109
- let max_sym = stream.ReadByte()
110
- if (max_sym == 0) {
111
- max_sym = 256
112
- }
113
-
114
- const byte_model = new ByteModel(max_sym)
115
-
116
- const rc = new RangeCoder(stream)
117
- rc.RangeStartDecode(stream)
118
-
119
- for (let i = 0; i < n_out; i++) {
120
- output[i] = byte_model.ModelDecode(stream, rc)
121
- }
122
-
123
- return output
124
- }
125
-
126
- // ----------------------------------------------------------------------
127
- // Order-1 codec
128
-
129
- decode1(stream, n_out) {
130
- const output = new Uint8Array(n_out)
131
-
132
- let max_sym = stream.ReadByte()
133
- if (max_sym == 0) {
134
- max_sym = 256
135
- }
136
-
137
- const byte_model = new Array(max_sym)
138
- for (var i = 0; i < max_sym; i++) {
139
- byte_model[i] = new ByteModel(max_sym)
140
- }
141
-
142
- const rc = new RangeCoder(stream)
143
- rc.RangeStartDecode(stream)
144
-
145
- let last = 0
146
- for (var i = 0; i < n_out; i++) {
147
- output[i] = byte_model[last].ModelDecode(stream, rc)
148
- last = output[i]
149
- }
150
-
151
- return output
152
- }
153
-
154
- // ----------------------------------------------------------------------
155
- // External codec
156
- decodeExt(stream, n_out) {
157
- return decode(stream.buf.slice(stream.pos))
158
- }
159
-
160
- // ----------------------------------------------------------------------
161
- // Order-0 RLE codec
162
- decodeRLE0(stream, n_out) {
163
- const output = new Uint8Array(n_out)
164
-
165
- let max_sym = stream.ReadByte()
166
- if (max_sym == 0) {
167
- max_sym = 256
168
- }
169
-
170
- const model_lit = new ByteModel(max_sym)
171
- const model_run = new Array(258)
172
- for (var i = 0; i <= 257; i++) {
173
- model_run[i] = new ByteModel(4)
174
- }
175
-
176
- const rc = new RangeCoder(stream)
177
- rc.RangeStartDecode(stream)
178
-
179
- var i = 0
180
- while (i < n_out) {
181
- output[i] = model_lit.ModelDecode(stream, rc)
182
- let part = model_run[output[i]].ModelDecode(stream, rc)
183
- let run = part
184
- let rctx = 256
185
- while (part == 3) {
186
- part = model_run[rctx].ModelDecode(stream, rc)
187
- rctx = 257
188
- run += part
189
- }
190
- for (let j = 1; j <= run; j++) {
191
- output[i + j] = output[i]
192
- }
193
- i += run + 1
194
- }
195
-
196
- return output
197
- }
198
-
199
- // ----------------------------------------------------------------------
200
- // Order-1 RLE codec
201
-
202
- decodeRLE1(stream, n_out) {
203
- const output = new Uint8Array(n_out)
204
-
205
- let max_sym = stream.ReadByte()
206
- if (max_sym == 0) {
207
- max_sym = 256
208
- }
209
-
210
- const model_lit = new Array(max_sym)
211
- for (var i = 0; i < max_sym; i++) {
212
- model_lit[i] = new ByteModel(max_sym)
213
- }
214
-
215
- const model_run = new Array(258)
216
- for (var i = 0; i <= 257; i++) {
217
- model_run[i] = new ByteModel(4)
218
- }
219
-
220
- const rc = new RangeCoder(stream)
221
- rc.RangeStartDecode(stream)
222
-
223
- let last = 0
224
- var i = 0
225
- while (i < n_out) {
226
- output[i] = model_lit[last].ModelDecode(stream, rc)
227
- last = output[i]
228
- let part = model_run[output[i]].ModelDecode(stream, rc)
229
- let run = part
230
- let rctx = 256
231
- while (part == 3) {
232
- part = model_run[rctx].ModelDecode(stream, rc)
233
- rctx = 257
234
- run += part
235
- }
236
- for (let j = 1; j <= run; j++) {
237
- output[i + j] = output[i]
238
- }
239
- i += run + 1
240
- }
241
-
242
- return output
243
- }
244
-
245
- // ----------------------------------------------------------------------
246
- // Pack method
247
- decodePackMeta(stream) {
248
- this.nsym = stream.ReadByte()
249
-
250
- const M = new Array(this.nsym)
251
- for (let i = 0; i < this.nsym; i++) {
252
- M[i] = stream.ReadByte()
253
- }
254
-
255
- const e_len = stream.ReadUint7() // Could be derived data from nsym and n_out
256
-
257
- return [M, e_len]
258
- }
259
-
260
- decodePack(data, M, len) {
261
- const out = new Uint8Array(len)
262
-
263
- if (this.nsym <= 1) {
264
- // Constant value
265
- for (var i = 0; i < len; i++) {
266
- out[i] = M[0]
267
- }
268
- } else if (this.nsym <= 2) {
269
- // 1 bit per value
270
- for (var i = 0, j = 0; i < len; i++) {
271
- if (i % 8 == 0) {
272
- var v = data[j++]
273
- }
274
- out[i] = M[v & 1]
275
- v >>= 1
276
- }
277
- } else if (this.nsym <= 4) {
278
- // 2 bits per value
279
- for (var i = 0, j = 0; i < len; i++) {
280
- if (i % 4 == 0) {
281
- var v = data[j++]
282
- }
283
- out[i] = M[v & 3]
284
- v >>= 2
285
- }
286
- } else if (this.nsym <= 16) {
287
- // 4 bits per value
288
- for (var i = 0, j = 0; i < len; i++) {
289
- if (i % 2 == 0) {
290
- var v = data[j++]
291
- }
292
- out[i] = M[v & 15]
293
- v >>= 4
294
- }
295
- } else {
296
- // 8 bits per value: NOP
297
- return data
298
- }
299
-
300
- return out
301
- }
302
-
303
- // Compute M array and return meta-data stream
304
- packMeta(src) {
305
- const stream = new IOStream('', 0, 1024)
306
-
307
- // Count symbols
308
- const M = new Array(256)
309
- for (var i = 0; i < src.length; i++) {
310
- M[src[i]] = 1
311
- }
312
-
313
- // Write Map
314
- for (var nsym = 0, i = 0; i < 256; i++) {
315
- if (M[i]) {
316
- M[i] = ++nsym
317
- }
318
- } // map to 1..N
319
- stream.WriteByte(nsym)
320
-
321
- // FIXME: add check for nsym > 16?
322
- // Or just accept it as an inefficient waste of time.
323
- for (var i = 0; i < 256; i++) {
324
- if (M[i]) {
325
- stream.WriteByte(i) // adjust to 0..N-1
326
- M[i]--
327
- }
328
- }
329
-
330
- return [stream, M, nsym]
331
- }
332
-
333
- decodeStripe(stream, len) {
334
- const N = stream.ReadByte()
335
-
336
- // Retrieve lengths
337
- const clen = new Array(N)
338
- const ulen = new Array(N)
339
- for (var j = 0; j < N; j++) {
340
- clen[j] = stream.ReadUint7()
341
- }
342
-
343
- // Decode streams
344
- const T = new Array(N)
345
- for (var j = 0; j < N; j++) {
346
- ulen[j] = Math.floor(len / N) + (len % N > j)
347
- T[j] = this.decodeStream(stream, ulen[j])
348
- }
349
-
350
- // Transpose
351
- const out = new Uint8Array(len)
352
- for (var j = 0; j < N; j++) {
353
- for (let i = 0; i < ulen[j]; i++) {
354
- out[i * N + j] = T[j][i]
355
- }
356
- }
357
-
358
- return out
359
- }
360
-
361
- // ----------------------------------------------------------------------
362
- // Cat method
363
- decodeCat(stream, len) {
364
- const out = new Uint8Array(len)
365
- for (let i = 0; i < len; i++) {
366
- out[i] = stream.ReadByte()
367
- }
368
-
369
- return out
370
- }
371
- }
@@ -1,146 +0,0 @@
1
- // @ts-nocheck
2
-
3
- /*
4
- * Copyright (c) 2019 Genome Research Ltd.
5
- * Author(s): James Bonfield
6
- *
7
- * Redistribution and use in source and binary forms, with or without
8
- * modification, are permitted provided that the following conditions are met:
9
- *
10
- * 1. Redistributions of source code must retain the above copyright notice,
11
- * this list of conditions and the following disclaimer.
12
- *
13
- * 2. Redistributions in binary form must reproduce the above
14
- * copyright notice, this list of conditions and the following
15
- * disclaimer in the documentation and/or other materials provided
16
- * with the distribution.
17
- *
18
- * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
19
- * Institute nor the names of its contributors may be used to endorse
20
- * or promote products derived from this software without specific
21
- * prior written permission.
22
- *
23
- * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
24
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
27
- * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
- */
35
-
36
- // An arithmetic coder, based on Eugene Shelwien's reimplementation of
37
- // Michael Schindler range coder.
38
- //
39
- // Order-0 byte stream of ~/scratch/data/q40b
40
- // C: 3.1s decode (approx same vs 32-bit and 64-bit)
41
- // Arith_sh.js 6.7s decode (32-bit with carries)
42
- // Arith.js 317.0s decode (64-bit no carries); int64 crippling it.
43
-
44
- // ----------------------------------------------------------------------
45
- // Arithmetic (range) coder
46
- export default class RangeCoder {
47
- constructor(src) {
48
- this.low = 0
49
- this.range = 0xffffffff
50
- this.code = 0
51
- this.FFnum = 0
52
- this.carry = 0
53
- this.cache = 0
54
- }
55
-
56
- RangeStartDecode(src) {
57
- for (let i = 0; i < 5; i++) {
58
- this.code = (this.code << 8) + src.ReadByte()
59
- }
60
- this.code &= 0xffffffff
61
- this.code >>>= 0 // force to be +ve int
62
- }
63
-
64
- RangeGetFrequency(tot_freq) {
65
- this.range = Math.floor(this.range / tot_freq)
66
- // return this.code / this.range;
67
- return Math.floor(this.code / this.range)
68
-
69
- // Conceptual scenario; return freq only and don't modify range yet
70
- // return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
71
- }
72
-
73
- RangeDecode(src, sym_low, sym_freq, tot_freq) {
74
- // Conceptually we divide range here, but in practice we cached it earlier
75
- // this.range = Math.floor(this.range / tot_freq);
76
-
77
- this.code -= sym_low * this.range
78
- this.range *= sym_freq
79
-
80
- while (this.range < 1 << 24) {
81
- this.range *= 256
82
- this.code = this.code * 256 + src.ReadByte()
83
- }
84
- }
85
-
86
- RangeShiftLow(dst) {
87
- // We know range is < (1<<24) as we got here. We already have a
88
- // cached copy of 8 bits from low. Is this correct, or does it need
89
- // fixing? Possible scenarios.
90
- // 1. Low < 0xff000000 thus low+range < 0xffffffff and cache
91
- // cannot possibly change. Output cache and as many ffs as needed.
92
- // 2. We already detected an overflow in RangeEncode, setting carry.
93
- // In this case output cached byte + 1 and any 00s needed.
94
- // 3. Neither case - range is low but we haven't yet detected if we're
95
- // XXffffff or XY000000 scenario. Increase counter for ff/00s.
96
-
97
- if ((this.low < 0xff000000) | this.carry) {
98
- // cached byte if no overflow, byte+1 otherwise
99
- dst.WriteByte(this.cache + this.carry)
100
-
101
- // Flush any tracked FFs (no carry) or 00s (carry).
102
- while (this.FFnum) {
103
- dst.WriteByte(this.carry - 1)
104
- this.FFnum--
105
- }
106
-
107
- // Take a copy of top byte ready for next flush
108
- this.cache = this.low >>> 24
109
- this.carry = 0
110
- } else {
111
- this.FFnum++ // keep track of number of trailing ff/00 bytes to write
112
- }
113
- this.low <<= 8
114
- this.low >>>= 0 // force to be +ve int
115
- }
116
-
117
- RangeEncode(dst, sym_low, sym_freq, tot_freq) {
118
- const old_low = this.low
119
- this.range = Math.floor(this.range / tot_freq)
120
- this.low += sym_low * this.range
121
- this.low >>>= 0 // Truncate to +ve int so we can spot overflow
122
- this.range *= sym_freq
123
-
124
- // "low + sym*range < old_low" means we overflow; set carry.
125
- // NB: can this.low < old_low occur twice before range < (1<<24)?
126
- // We claim not, but prove it!
127
- if (this.low < old_low) {
128
- if (this.carry != 0) {
129
- console.log('ERROR: Multiple carry')
130
- }
131
- this.carry = 1
132
- }
133
-
134
- // Renormalise if range gets too small
135
- while (this.range < 1 << 24) {
136
- this.range *= 256
137
- this.RangeShiftLow(dst)
138
- }
139
- }
140
-
141
- RangeFinishEncode(dst) {
142
- for (let i = 0; i < 5; i++) {
143
- this.RangeShiftLow(dst)
144
- }
145
- }
146
- }
@@ -1,136 +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
- // An adaptive probability model for encoding and decoding of symbols
38
- // within a given alphabet, using the range coder to get/put the
39
- // compressed data.
40
-
41
- const MAX_FREQ = (1 << 16) - 17
42
- const STEP = 16
43
-
44
- export default class ByteModel {
45
- constructor(max_sym = 256) {
46
- this.total_freq = max_sym
47
- this.max_sym = max_sym - 1
48
- this.S = []
49
- this.F = []
50
-
51
- for (let i = 0; i <= this.max_sym; i++) {
52
- this.S[i] = i
53
- this.F[i] = 1
54
- }
55
- }
56
-
57
- ModelDecode(src, rc) {
58
- // Find symbol
59
- const freq = rc.RangeGetFrequency(this.total_freq)
60
-
61
- // Linear scan to find cumulative frequency 'freq'
62
- let acc = 0
63
- let x = 0
64
- while (acc + this.F[x] <= freq) {
65
- acc += this.F[x++]
66
- }
67
-
68
- // for (var acc = 0; (acc += this.F[x]) <= freq; x++)
69
- // ;
70
- // acc -= this.F[x];
71
-
72
- // Update range coder
73
- rc.RangeDecode(src, acc, this.F[x], this.total_freq)
74
-
75
- // Update model
76
- this.F[x] += STEP
77
- this.total_freq += STEP
78
- if (this.total_freq > MAX_FREQ) {
79
- this.ModelRenormalise()
80
- }
81
-
82
- // Keep symbols approximately frequency sorted
83
- const sym = this.S[x]
84
- if (x > 0 && this.F[x] > this.F[x - 1]) {
85
- let tmp = this.F[x]
86
- this.F[x] = this.F[x - 1]
87
- this.F[x - 1] = tmp
88
-
89
- tmp = this.S[x]
90
- this.S[x] = this.S[x - 1]
91
- this.S[x - 1] = tmp
92
- }
93
-
94
- return sym
95
- }
96
-
97
- ModelRenormalise() {
98
- // Halve all the frequencies, being careful not to hit zero
99
- this.total_freq = 0
100
- for (let i = 0; i <= this.max_sym; i++) {
101
- this.F[i] -= Math.floor(this.F[i] / 2)
102
- this.total_freq += this.F[i]
103
- }
104
- }
105
-
106
- ModelEncode(dst, rc, sym) {
107
- // Find cumulative frequency
108
- let acc = 0
109
- for (var x = 0; this.S[x] != sym; x++) {
110
- acc += this.F[x]
111
- }
112
-
113
- // Encode
114
- rc.RangeEncode(dst, acc, this.F[x], this.total_freq)
115
-
116
- // Update model
117
- this.F[x] += STEP
118
- this.total_freq += STEP
119
- if (this.total_freq > MAX_FREQ) {
120
- // FIXME x2
121
- this.ModelRenormalise()
122
- }
123
-
124
- // Keep symbols approximately frequency sorted
125
- var sym = this.S[x]
126
- if (x > 0 && this.F[x] > this.F[x - 1]) {
127
- let tmp = this.F[x]
128
- this.F[x] = this.F[x - 1]
129
- this.F[x - 1] = tmp
130
-
131
- tmp = this.S[x]
132
- this.S[x] = this.S[x - 1]
133
- this.S[x - 1] = tmp
134
- }
135
- }
136
- }