@gmod/cram 4.0.3 → 4.0.4

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 (91) hide show
  1. package/dist/cram-bundle.js +1 -1
  2. package/dist/cramFile/file.js +43 -9
  3. package/dist/cramFile/file.js.map +1 -1
  4. package/dist/cramFile/slice/decodeRecord.js +3 -3
  5. package/dist/cramFile/slice/decodeRecord.js.map +1 -1
  6. package/dist/cramFile/util.d.ts +0 -2
  7. package/dist/cramFile/util.js +3 -31
  8. package/dist/cramFile/util.js.map +1 -1
  9. package/dist/htscodecs/arith_gen.d.ts +5 -7
  10. package/dist/htscodecs/arith_gen.js +122 -105
  11. package/dist/htscodecs/arith_gen.js.map +1 -1
  12. package/dist/htscodecs/arith_sh.d.ts +1 -8
  13. package/dist/htscodecs/arith_sh.js +16 -10
  14. package/dist/htscodecs/arith_sh.js.map +1 -1
  15. package/dist/htscodecs/byte_model.d.ts +1 -6
  16. package/dist/htscodecs/byte_model.js +25 -17
  17. package/dist/htscodecs/byte_model.js.map +1 -1
  18. package/dist/htscodecs/fqzcomp.d.ts +1 -1
  19. package/dist/htscodecs/fqzcomp.js +98 -77
  20. package/dist/htscodecs/fqzcomp.js.map +1 -1
  21. package/dist/htscodecs/index.d.ts +5 -5
  22. package/dist/htscodecs/index.js +53 -16
  23. package/dist/htscodecs/index.js.map +1 -1
  24. package/dist/htscodecs/iostream.d.ts +9 -20
  25. package/dist/htscodecs/iostream.js +21 -116
  26. package/dist/htscodecs/iostream.js.map +1 -1
  27. package/dist/htscodecs/rans.d.ts +1 -1
  28. package/dist/htscodecs/rans.js +65 -54
  29. package/dist/htscodecs/rans.js.map +1 -1
  30. package/dist/htscodecs/rans4x16.d.ts +1 -1
  31. package/dist/htscodecs/rans4x16.js +151 -111
  32. package/dist/htscodecs/rans4x16.js.map +1 -1
  33. package/dist/htscodecs/tok3.d.ts +1 -2
  34. package/dist/htscodecs/tok3.js +82 -239
  35. package/dist/htscodecs/tok3.js.map +1 -1
  36. package/dist/util.d.ts +1 -0
  37. package/dist/util.js +20 -0
  38. package/dist/util.js.map +1 -0
  39. package/esm/cramFile/file.js +3 -2
  40. package/esm/cramFile/file.js.map +1 -1
  41. package/esm/cramFile/slice/decodeRecord.js +3 -3
  42. package/esm/cramFile/slice/decodeRecord.js.map +1 -1
  43. package/esm/cramFile/util.d.ts +0 -2
  44. package/esm/cramFile/util.js +3 -29
  45. package/esm/cramFile/util.js.map +1 -1
  46. package/esm/htscodecs/arith_gen.d.ts +5 -7
  47. package/esm/htscodecs/arith_gen.js +108 -97
  48. package/esm/htscodecs/arith_gen.js.map +1 -1
  49. package/esm/htscodecs/arith_sh.d.ts +1 -8
  50. package/esm/htscodecs/arith_sh.js +14 -11
  51. package/esm/htscodecs/arith_sh.js.map +1 -1
  52. package/esm/htscodecs/byte_model.d.ts +1 -6
  53. package/esm/htscodecs/byte_model.js +23 -18
  54. package/esm/htscodecs/byte_model.js.map +1 -1
  55. package/esm/htscodecs/fqzcomp.d.ts +1 -1
  56. package/esm/htscodecs/fqzcomp.js +91 -76
  57. package/esm/htscodecs/fqzcomp.js.map +1 -1
  58. package/esm/htscodecs/index.d.ts +5 -5
  59. package/esm/htscodecs/index.js +14 -20
  60. package/esm/htscodecs/index.js.map +1 -1
  61. package/esm/htscodecs/iostream.d.ts +9 -20
  62. package/esm/htscodecs/iostream.js +19 -117
  63. package/esm/htscodecs/iostream.js.map +1 -1
  64. package/esm/htscodecs/rans.d.ts +1 -1
  65. package/esm/htscodecs/rans.js +61 -56
  66. package/esm/htscodecs/rans.js.map +1 -1
  67. package/esm/htscodecs/rans4x16.d.ts +1 -1
  68. package/esm/htscodecs/rans4x16.js +143 -109
  69. package/esm/htscodecs/rans4x16.js.map +1 -1
  70. package/esm/htscodecs/tok3.d.ts +1 -2
  71. package/esm/htscodecs/tok3.js +41 -237
  72. package/esm/htscodecs/tok3.js.map +1 -1
  73. package/esm/util.d.ts +1 -0
  74. package/esm/util.js +17 -0
  75. package/esm/util.js.map +1 -0
  76. package/package.json +2 -3
  77. package/src/cramFile/file.ts +3 -2
  78. package/src/cramFile/slice/decodeRecord.ts +3 -5
  79. package/src/cramFile/util.ts +6 -39
  80. package/src/htscodecs/{arith_gen.js → arith_gen.ts} +133 -95
  81. package/src/htscodecs/{arith_sh.js → arith_sh.ts} +17 -9
  82. package/src/htscodecs/{byte_model.js → byte_model.ts} +26 -16
  83. package/src/htscodecs/{fqzcomp.js → fqzcomp.ts} +108 -74
  84. package/src/htscodecs/{index.js → index.ts} +14 -20
  85. package/src/htscodecs/iostream.ts +159 -0
  86. package/src/htscodecs/{rans.js → rans.ts} +73 -56
  87. package/src/htscodecs/{rans4x16.js → rans4x16.ts} +180 -111
  88. package/src/htscodecs/tok3.ts +197 -0
  89. package/src/util.ts +16 -0
  90. package/src/htscodecs/iostream.js +0 -257
  91. package/src/htscodecs/tok3.js +0 -413
@@ -1,3 +1,6 @@
1
+ /* eslint-disable no-var */
2
+ // @ts-nocheck
3
+
1
4
  /*
2
5
  * Copyright (c) 2019 Genome Research Ltd.
3
6
  * Author(s): James Bonfield
@@ -31,46 +34,50 @@
31
34
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
35
  */
33
36
 
34
- const IOStream = require('./iostream')
35
- const ByteModel = require('./byte_model')
36
- const RangeCoder = require('./arith_sh')
37
+ import RangeCoder from './arith_sh'
38
+ import ByteModel from './byte_model'
39
+ import IOStream from './iostream'
37
40
 
38
- //----------------------------------------------------------------------
41
+ // ----------------------------------------------------------------------
39
42
  // Main arithmetic entry function: decodes a compressed src and
40
43
  // returns the uncompressed buffer.
41
44
 
42
45
  function read_array(src, tab, size) {
43
- var j = 0 // array value
44
- var z = 0 // array index: tab[j]
45
- var last = -1
46
+ let j = 0 // array value
47
+ let z = 0 // array index: tab[j]
48
+ let last = -1
46
49
 
47
50
  // Remove first level of run-length encoding
48
- var R = new Array(1024) // runs
51
+ const R = new Array(1024) // runs
49
52
  while (z < size) {
50
- var run = src.ReadByte()
53
+ const run = src.ReadByte()
51
54
  R[j++] = run
52
55
  z += run
53
56
 
54
57
  if (run == last) {
55
- var copy = src.ReadByte()
58
+ let copy = src.ReadByte()
56
59
  z += run * copy
57
- while (copy--) R[j++] = run
60
+ while (copy--) {
61
+ R[j++] = run
62
+ }
58
63
  }
59
64
  last = run
60
65
  }
61
66
 
62
67
  // Now expand runs in R to tab, noting 255 is max run
63
- var i = 0
68
+ let i = 0
64
69
  j = 0
65
70
  z = 0
66
71
  while (z < size) {
67
- var run_len = 0
72
+ let run_len = 0
68
73
  do {
69
74
  var part = R[j++]
70
75
  run_len += part
71
76
  } while (part == 255)
72
77
 
73
- while (run_len--) tab[z++] = i
78
+ while (run_len--) {
79
+ tab[z++] = i
80
+ }
74
81
  i++
75
82
  }
76
83
  }
@@ -91,11 +98,13 @@ const GFLAG_DO_REV = 4
91
98
 
92
99
  // Compute a new context from our current state and qual q
93
100
  function fqz_update_ctx(params, state, q) {
94
- var last = params.context
101
+ let last = params.context
95
102
  state.qctx = (state.qctx << params.qshift) + params.qtab[q] // >>> 0
96
103
  last += (state.qctx & ((1 << params.qbits) - 1)) << params.qloc // >>> 0
97
104
 
98
- if (params.do_pos) last += params.ptab[Math.min(state.p, 1023)] << params.ploc
105
+ if (params.do_pos) {
106
+ last += params.ptab[Math.min(state.p, 1023)] << params.ploc
107
+ }
99
108
 
100
109
  if (params.do_delta) {
101
110
  last += params.dtab[Math.min(state.delta, 255)] << params.dloc
@@ -106,7 +115,9 @@ function fqz_update_ctx(params, state, q) {
106
115
  state.prevq = q
107
116
  }
108
117
 
109
- if (params.do_sel) last += state.s << params.sloc
118
+ if (params.do_sel) {
119
+ last += state.s << params.sloc
120
+ }
110
121
 
111
122
  state.p--
112
123
 
@@ -114,7 +125,7 @@ function fqz_update_ctx(params, state, q) {
114
125
  }
115
126
 
116
127
  function decode_fqz_single_param(src) {
117
- var p = {} // params
128
+ const p = {} // params
118
129
 
119
130
  // Load FQZ parameters
120
131
  p.context = src.ReadUint16()
@@ -130,7 +141,7 @@ function decode_fqz_single_param(src) {
130
141
 
131
142
  p.max_sym = src.ReadByte()
132
143
 
133
- var x = src.ReadByte()
144
+ let x = src.ReadByte()
134
145
  p.qbits = x >> 4
135
146
  p.qshift = x & 15
136
147
  x = src.ReadByte()
@@ -143,10 +154,14 @@ function decode_fqz_single_param(src) {
143
154
  // Qual map, eg to "unbin" Illumina qualities
144
155
  p.qmap = new Array(256)
145
156
  if (p.pflags & FLAG_QMAP) {
146
- for (var i = 0; i < p.max_sym; i++) p.qmap[i] = src.ReadByte()
157
+ for (var i = 0; i < p.max_sym; i++) {
158
+ p.qmap[i] = src.ReadByte()
159
+ }
147
160
  } else {
148
161
  // Useful optimisation to speed up main loop
149
- for (var i = 0; i < 256; i++) p.qmap[i] = i // NOP
162
+ for (var i = 0; i < 256; i++) {
163
+ p.qmap[i] = i
164
+ } // NOP
150
165
  }
151
166
 
152
167
  // Read tables
@@ -155,70 +170,86 @@ function decode_fqz_single_param(src) {
155
170
  read_array(src, p.qtab, 256)
156
171
  } else {
157
172
  // Useful optimisation to speed up main loop
158
- for (var i = 0; i < 256; i++) p.qtab[i] = i // NOP
173
+ for (var i = 0; i < 256; i++) {
174
+ p.qtab[i] = i
175
+ } // NOP
159
176
  }
160
177
 
161
178
  p.ptab = new Array(1024)
162
- if (p.pflags & FLAG_PTAB) read_array(src, p.ptab, 1024)
179
+ if (p.pflags & FLAG_PTAB) {
180
+ read_array(src, p.ptab, 1024)
181
+ }
163
182
 
164
183
  p.dtab = new Array(256)
165
- if (p.pflags & FLAG_DTAB) read_array(src, p.dtab, 256)
184
+ if (p.pflags & FLAG_DTAB) {
185
+ read_array(src, p.dtab, 256)
186
+ }
166
187
 
167
188
  return p
168
189
  }
169
190
 
170
191
  function decode_fqz_params(src) {
171
- var gparams = {
192
+ const gparams = {
172
193
  max_sym: 0,
173
194
  }
174
195
 
175
196
  // Check fqz format version
176
- var vers = src.ReadByte()
197
+ const vers = src.ReadByte()
177
198
  if (vers != 5) {
178
199
  console.error('Invalid FQZComp version number')
179
200
  return
180
201
  }
181
202
 
182
- var gflags = src.ReadByte()
183
- var nparam = gflags & GFLAG_MULTI_PARAM ? src.ReadByte() : 1
184
- var max_sel = gflags.nparam > 1 ? gflags.nparam - 1 : 0 // Note max_sel, not num_sel
203
+ const gflags = src.ReadByte()
204
+ const nparam = gflags & GFLAG_MULTI_PARAM ? src.ReadByte() : 1
205
+ let max_sel = gflags.nparam > 1 ? gflags.nparam - 1 : 0 // Note max_sel, not num_sel
185
206
 
186
- var stab = new Array(256)
207
+ const stab = new Array(256)
187
208
  if (gflags & GFLAG_HAVE_STAB) {
188
209
  max_sel = src.ReadByte()
189
210
  read_array(src, stab, 256)
190
211
  } else {
191
- for (var i = 0; i < nparam; i++) stab[i] = i
192
- for (; i < 256; i++) stab[i] = nparam - 1
212
+ for (var i = 0; i < nparam; i++) {
213
+ stab[i] = i
214
+ }
215
+ for (; i < 256; i++) {
216
+ stab[i] = nparam - 1
217
+ }
193
218
  }
194
219
  gparams.do_rev = gflags & GFLAG_DO_REV
195
220
  gparams.stab = stab
196
221
  gparams.max_sel = max_sel
197
222
 
198
223
  gparams.params = new Array(gparams.nparam)
199
- for (var p = 0; p < nparam; p++) {
224
+ for (let p = 0; p < nparam; p++) {
200
225
  gparams.params[p] = decode_fqz_single_param(src)
201
- if (gparams.max_sym < gparams.params[p].max_sym)
226
+ if (gparams.max_sym < gparams.params[p].max_sym) {
202
227
  gparams.max_sym = gparams.params[p].max_sym
228
+ }
203
229
  }
204
230
 
205
231
  return gparams
206
232
  }
207
233
 
208
234
  function fqz_create_models(gparams) {
209
- var model = {}
235
+ const model = {}
210
236
 
211
237
  model.qual = new Array(1 << 16)
212
- for (var i = 0; i < 1 << 16; i++)
213
- model.qual[i] = new ByteModel(gparams.max_sym + 1) // +1 as max value not num. values
238
+ for (var i = 0; i < 1 << 16; i++) {
239
+ model.qual[i] = new ByteModel(gparams.max_sym + 1)
240
+ } // +1 as max value not num. values
214
241
 
215
242
  model.len = new Array(4)
216
- for (var i = 0; i < 4; i++) model.len[i] = new ByteModel(256)
243
+ for (var i = 0; i < 4; i++) {
244
+ model.len[i] = new ByteModel(256)
245
+ }
217
246
 
218
247
  model.rev = new ByteModel(2)
219
248
  model.dup = new ByteModel(2)
220
249
 
221
- if (gparams.max_sel > 0) model.sel = new ByteModel(gparams.max_sel + 1) // +1 as max value not num. values
250
+ if (gparams.max_sel > 0) {
251
+ model.sel = new ByteModel(gparams.max_sel + 1)
252
+ } // +1 as max value not num. values
222
253
 
223
254
  return model
224
255
  }
@@ -227,14 +258,10 @@ function fqz_create_models(gparams) {
227
258
  // Returns 1 if dup, otherwise 0
228
259
  function decode_fqz_new_record(src, rc, gparams, model, state, rev) {
229
260
  // Parameter selector
230
- if (gparams.max_sel > 0) {
231
- state.s = model.sel.ModelDecode(src, rc)
232
- } else {
233
- state.s = 0
234
- }
261
+ state.s = gparams.max_sel > 0 ? model.sel.ModelDecode(src, rc) : 0
235
262
  state.x = gparams.stab[state.s]
236
263
 
237
- var params = gparams.params[state.x]
264
+ const params = gparams.params[state.x]
238
265
 
239
266
  // Reset contexts at the start of each new record
240
267
  if (params.fixed_len >= 0) {
@@ -243,17 +270,23 @@ function decode_fqz_new_record(src, rc, gparams, model, state, rev) {
243
270
  len |= model.len[1].ModelDecode(src, rc) << 8
244
271
  len |= model.len[2].ModelDecode(src, rc) << 16
245
272
  len |= model.len[3].ModelDecode(src, rc) << 24
246
- if (params.fixed_len > 0) params.fixed_len = -len
273
+ if (params.fixed_len > 0) {
274
+ params.fixed_len = -len
275
+ }
247
276
  } else {
248
277
  len = -params.fixed_len
249
278
  }
250
279
  state.len = len
251
280
 
252
- if (gparams.do_rev) rev[state.rec] = model.rev.ModelDecode(src, rc)
281
+ if (gparams.do_rev) {
282
+ rev[state.rec] = model.rev.ModelDecode(src, rc)
283
+ }
253
284
 
254
285
  state.is_dup = 0
255
286
  if (params.pflags & FLAG_DEDUP) {
256
- if (model.dup.ModelDecode(src, rc)) state.is_dup = 1
287
+ if (model.dup.ModelDecode(src, rc)) {
288
+ state.is_dup = 1
289
+ }
257
290
  }
258
291
 
259
292
  state.p = len // number of remaining bytes in this record
@@ -263,24 +296,26 @@ function decode_fqz_new_record(src, rc, gparams, model, state, rev) {
263
296
  state.rec++
264
297
  }
265
298
 
266
- function decode_fqz(src, q_lens) {
299
+ function decode_fqz(src: IOStream, q_lens: number) {
267
300
  // Decode parameter block
268
- var n_out = src.ReadUint7()
269
- var gparams = decode_fqz_params(src)
270
- if (!gparams) return
301
+ const n_out = src.ReadUint7()
302
+ const gparams = decode_fqz_params(src)
303
+ if (!gparams) {
304
+ return
305
+ }
271
306
  var params = gparams.params
272
- var rev = new Array(q_lens.length)
307
+ const rev = new Array(q_lens.length)
273
308
 
274
309
  // Create initial models
275
- var model = fqz_create_models(gparams)
310
+ const model = fqz_create_models(gparams)
276
311
 
277
312
  // Create our entropy encoder and output buffers
278
- var rc = new RangeCoder(src)
313
+ const rc = new RangeCoder(src)
279
314
  rc.RangeStartDecode(src)
280
- var output = new Uint8Array(n_out)
315
+ const output = new Uint8Array(n_out)
281
316
 
282
317
  // Internal FQZ state
283
- var state = {
318
+ const state = {
284
319
  qctx: 0, // Qual-only sub-context
285
320
  prevq: 0, // Previous quality value
286
321
  delta: 0, // Running delta (q vs prevq)
@@ -293,15 +328,16 @@ function decode_fqz(src, q_lens) {
293
328
  }
294
329
 
295
330
  // The main decode loop itself
296
- var i = 0 // position in output buffer
331
+ let i = 0 // position in output buffer
297
332
  while (i < n_out) {
298
333
  if (state.p == 0) {
299
334
  decode_fqz_new_record(src, rc, gparams, model, state, rev)
300
335
  if (state.is_dup > 0) {
301
336
  if (model.dup.ModelDecode(src, rc)) {
302
337
  // Duplicate of last line
303
- for (var x = 0; x < len; x++)
338
+ for (let x = 0; x < len; x++) {
304
339
  output[i + x] = output[i + x - state.len]
340
+ }
305
341
  i += state.len
306
342
  state.p = 0
307
343
  continue
@@ -314,30 +350,32 @@ function decode_fqz(src, q_lens) {
314
350
  }
315
351
 
316
352
  // Decode the current quality (possibly mapped via qmap)
317
- var Q = model.qual[last].ModelDecode(src, rc)
353
+ const Q = model.qual[last].ModelDecode(src, rc)
318
354
 
319
- //if (params.do_qmap)
355
+ // if (params.do_qmap)
320
356
  // output[i++] = params.qmap[Q];
321
- //else
357
+ // else
322
358
  // output[i++] = Q
323
359
  output[i++] = params.qmap[Q] // optimised version of above
324
360
  last = fqz_update_ctx(params, state, Q)
325
361
  }
326
362
 
327
- if (gparams.do_rev) reverse_qualities(output, n_out, rev, q_lens)
363
+ if (gparams.do_rev) {
364
+ reverse_qualities(output, n_out, rev, q_lens)
365
+ }
328
366
 
329
367
  return output
330
368
  }
331
369
 
332
370
  function reverse_qualities(qual, qual_len, rev, len) {
333
- var rec = 0
334
- var i = 0
371
+ let rec = 0
372
+ let i = 0
335
373
  while (i < qual_len) {
336
374
  if (rev[rec]) {
337
- var j = 0
338
- var k = len[rec] - 1
375
+ let j = 0
376
+ let k = len[rec] - 1
339
377
  while (j < k) {
340
- var tmp = qual[i + j]
378
+ const tmp = qual[i + j]
341
379
  qual[i + j] = qual[i + k]
342
380
  qual[i + k] = tmp
343
381
  j++
@@ -349,12 +387,8 @@ function reverse_qualities(qual, qual_len, rev, len) {
349
387
  }
350
388
  }
351
389
 
352
- function decode(src, q_lens) {
353
- var stream = new IOStream(src)
354
-
355
- //var n_out = stream.ReadUint32(); stream.ReadUint32(); // move to main
390
+ export function decode(src: Uint8Array, q_lens) {
391
+ const stream = new IOStream(src)
356
392
 
357
393
  return decode_fqz(stream, q_lens)
358
394
  }
359
-
360
- module.exports = { decode }
@@ -1,3 +1,5 @@
1
+ // @ts-nocheck
2
+ //
1
3
  /*
2
4
  * Copyright (c) 2020 Genome Research Ltd.
3
5
  * Author(s): James Bonfield
@@ -37,41 +39,33 @@
37
39
  // This JavaScript file is not part of the reference implementation
38
40
  // and is simply and interface to get a consistent interface for cram-js.
39
41
 
40
- var r4x8 = require('./rans')
41
- var r4x16 = require('./rans4x16')
42
- var arith = require('./arith_gen')
43
- var fqzcomp = require('./fqzcomp')
44
- var tok3 = require('./tok3')
42
+ import arith from './arith_gen'
43
+ import * as fqzcomp from './fqzcomp'
44
+ import * as r4x8 from './rans'
45
+ import * as r4x16 from './rans4x16'
46
+ import * as tok3 from './tok3'
45
47
 
46
- function r4x8_uncompress(inputBuffer, outputBuffer) {
48
+ export function r4x8_uncompress(inputBuffer: Uint8Array) {
47
49
  return r4x8.decode(inputBuffer)
48
50
  }
49
51
 
50
- function r4x16_uncompress(inputBuffer) {
52
+ export function r4x16_uncompress(inputBuffer: Uint8Array) {
51
53
  return r4x16.decode(inputBuffer)
52
54
  }
53
55
 
54
- function arith_uncompress(inputBuffer) {
56
+ export function arith_uncompress(inputBuffer: Uint8Array) {
55
57
  // fix by @cmdcolin for CRAM 3.1
56
58
  // xref https://github.com/jkbonfield/htscodecs/pull/1/files
57
59
  return new arith().decode(inputBuffer)
58
60
  }
59
61
 
60
- function fqzcomp_uncompress(inputBuffer) {
61
- var q_lens = new Array()
62
+ export function fqzcomp_uncompress(inputBuffer: Uint8Array) {
63
+ const q_lens = []
62
64
  return fqzcomp.decode(inputBuffer, q_lens)
63
65
  }
64
66
 
65
- function tok3_uncompress(inputBuffer) {
67
+ export function tok3_uncompress(inputBuffer: Uint8Array) {
66
68
  // Returns in string form instead of buffer
67
- var out = tok3.decode(inputBuffer, 0, '\0')
69
+ const out = tok3.decode(inputBuffer, 0, '\0')
68
70
  return Uint8Array.from(Array.from(out).map(letter => letter.charCodeAt(0)))
69
71
  }
70
-
71
- module.exports = {
72
- r4x8_uncompress: r4x8_uncompress,
73
- r4x16_uncompress: r4x16_uncompress,
74
- arith_uncompress: arith_uncompress,
75
- fqzcomp_uncompress: fqzcomp_uncompress,
76
- tok3_uncompress: tok3_uncompress,
77
- }
@@ -0,0 +1,159 @@
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
+ // Turn a buffer into a fake stream with get / put commands.
37
+ // This enables up to closely match the published pseudocode.
38
+ export default class IOStream {
39
+ buf: Uint8Array
40
+ length: number
41
+ pos: number
42
+ dataView: DataView
43
+
44
+ constructor(buf: Uint8Array, start_pos = 0, size = 0) {
45
+ if (size !== 0) {
46
+ this.buf = new Uint8Array(size)
47
+ this.length = size
48
+ } else {
49
+ this.buf = buf
50
+ this.length = buf.length
51
+ }
52
+ this.dataView = new DataView(this.buf.buffer)
53
+ this.pos = start_pos
54
+ }
55
+
56
+ // ----------
57
+ // Reading
58
+ EOF() {
59
+ return this.pos >= this.length
60
+ }
61
+
62
+ ReadData(len: number) {
63
+ const A = this.buf.slice(this.pos, this.pos + len)
64
+ this.pos += len
65
+ return A
66
+ }
67
+
68
+ ReadByte() {
69
+ const b = this.buf[this.pos]!
70
+ this.pos++
71
+ return b
72
+ }
73
+
74
+ ReadChar() {
75
+ const b = this.buf[this.pos]!
76
+ this.pos++
77
+ return String.fromCharCode(b)
78
+ }
79
+
80
+ ReadUint16() {
81
+ let i = this.ReadByte()
82
+ i |= this.ReadByte() << 8
83
+ return i
84
+ }
85
+
86
+ ReadUint32() {
87
+ const i = this.dataView.getInt32(this.pos, true)
88
+ this.pos += 4
89
+ return i
90
+ }
91
+
92
+ // nul terminated string
93
+ ReadString() {
94
+ let s = ''
95
+ let b: number
96
+ do {
97
+ b = this.buf[this.pos++]!
98
+ if (b) {
99
+ s += String.fromCharCode(b)
100
+ }
101
+ } while (b)
102
+ return s
103
+ }
104
+
105
+ ReadUint7() {
106
+ // Variable sized unsigned integers
107
+ let i = 0
108
+ let c: number
109
+ do {
110
+ c = this.ReadByte()
111
+ i = (i << 7) | (c & 0x7f)
112
+ } while (c & 0x80)
113
+
114
+ return i
115
+ }
116
+
117
+ ReadITF8() {
118
+ let i = this.buf[this.pos]!
119
+ this.pos++
120
+
121
+ // process.stderr.write("i="+i+"\n");
122
+
123
+ if (i >= 0xf0) {
124
+ // 1111xxxx => +4 bytes
125
+ i = (i & 0x0f) << 28
126
+ i +=
127
+ (this.buf[this.pos + 0]! << 20) +
128
+ (this.buf[this.pos + 1]! << 12) +
129
+ (this.buf[this.pos + 2]! << 4) +
130
+ (this.buf[this.pos + 3]! >> 4)
131
+ this.pos += 4
132
+ // process.stderr.write(" 4i="+i+"\n");
133
+ } else if (i >= 0xe0) {
134
+ // 1110xxxx => +3 bytes
135
+ i = (i & 0x0f) << 24
136
+ i +=
137
+ (this.buf[this.pos + 0]! << 16) +
138
+ (this.buf[this.pos + 1]! << 8) +
139
+ (this.buf[this.pos + 2]! << 0)
140
+ this.pos += 3
141
+ // process.stderr.write(" 3i="+i+"\n");
142
+ } else if (i >= 0xc0) {
143
+ // 110xxxxx => +2 bytes
144
+ i = (i & 0x1f) << 16
145
+ i += (this.buf[this.pos + 0]! << 8) + (this.buf[this.pos + 1]! << 0)
146
+ this.pos += 2
147
+ // process.stderr.write(" 2i="+i+"\n");
148
+ } else if (i >= 0x80) {
149
+ // 10xxxxxx => +1 bytes
150
+ i = (i & 0x3f) << 8
151
+ i += this.buf[this.pos]!
152
+ this.pos++
153
+ } else {
154
+ // 0xxxxxxx => +0 bytes
155
+ }
156
+
157
+ return i
158
+ }
159
+ }