@gmod/cram 4.0.3 → 4.0.5

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 (132) hide show
  1. package/README.md +53 -51
  2. package/dist/cram-bundle.js +1 -1
  3. package/dist/cramFile/codecs/_base.d.ts +1 -1
  4. package/dist/cramFile/codecs/byteArrayLength.js +3 -2
  5. package/dist/cramFile/codecs/byteArrayLength.js.map +1 -1
  6. package/dist/cramFile/codecs/external.d.ts +1 -1
  7. package/dist/cramFile/codecs/external.js +1 -4
  8. package/dist/cramFile/codecs/external.js.map +1 -1
  9. package/dist/cramFile/container/index.d.ts +16 -34
  10. package/dist/cramFile/container/index.js +5 -20
  11. package/dist/cramFile/container/index.js.map +1 -1
  12. package/dist/cramFile/file.d.ts +8 -6
  13. package/dist/cramFile/file.js +80 -82
  14. package/dist/cramFile/file.js.map +1 -1
  15. package/dist/cramFile/record.js.map +1 -1
  16. package/dist/cramFile/sectionParsers.js +3 -2
  17. package/dist/cramFile/sectionParsers.js.map +1 -1
  18. package/dist/cramFile/slice/decodeRecord.d.ts +2 -2
  19. package/dist/cramFile/slice/decodeRecord.js +10 -7
  20. package/dist/cramFile/slice/decodeRecord.js.map +1 -1
  21. package/dist/cramFile/slice/index.js +14 -16
  22. package/dist/cramFile/slice/index.js.map +1 -1
  23. package/dist/cramFile/util.d.ts +5 -2
  24. package/dist/cramFile/util.js +75 -79
  25. package/dist/cramFile/util.js.map +1 -1
  26. package/dist/htscodecs/arith_gen.d.ts +5 -7
  27. package/dist/htscodecs/arith_gen.js +122 -105
  28. package/dist/htscodecs/arith_gen.js.map +1 -1
  29. package/dist/htscodecs/arith_sh.d.ts +1 -8
  30. package/dist/htscodecs/arith_sh.js +16 -10
  31. package/dist/htscodecs/arith_sh.js.map +1 -1
  32. package/dist/htscodecs/byte_model.d.ts +1 -6
  33. package/dist/htscodecs/byte_model.js +25 -17
  34. package/dist/htscodecs/byte_model.js.map +1 -1
  35. package/dist/htscodecs/fqzcomp.d.ts +1 -1
  36. package/dist/htscodecs/fqzcomp.js +98 -77
  37. package/dist/htscodecs/fqzcomp.js.map +1 -1
  38. package/dist/htscodecs/index.d.ts +5 -5
  39. package/dist/htscodecs/index.js +53 -16
  40. package/dist/htscodecs/index.js.map +1 -1
  41. package/dist/htscodecs/iostream.d.ts +9 -20
  42. package/dist/htscodecs/iostream.js +21 -116
  43. package/dist/htscodecs/iostream.js.map +1 -1
  44. package/dist/htscodecs/rans.d.ts +1 -1
  45. package/dist/htscodecs/rans.js +65 -54
  46. package/dist/htscodecs/rans.js.map +1 -1
  47. package/dist/htscodecs/rans4x16.d.ts +1 -1
  48. package/dist/htscodecs/rans4x16.js +151 -111
  49. package/dist/htscodecs/rans4x16.js.map +1 -1
  50. package/dist/htscodecs/tok3.d.ts +1 -2
  51. package/dist/htscodecs/tok3.js +82 -239
  52. package/dist/htscodecs/tok3.js.map +1 -1
  53. package/dist/util.d.ts +1 -0
  54. package/dist/util.js +20 -0
  55. package/dist/util.js.map +1 -0
  56. package/esm/cramFile/codecs/_base.d.ts +1 -1
  57. package/esm/cramFile/codecs/byteArrayLength.js +3 -2
  58. package/esm/cramFile/codecs/byteArrayLength.js.map +1 -1
  59. package/esm/cramFile/codecs/external.d.ts +1 -1
  60. package/esm/cramFile/codecs/external.js +2 -5
  61. package/esm/cramFile/codecs/external.js.map +1 -1
  62. package/esm/cramFile/container/index.d.ts +16 -34
  63. package/esm/cramFile/container/index.js +5 -20
  64. package/esm/cramFile/container/index.js.map +1 -1
  65. package/esm/cramFile/file.d.ts +8 -6
  66. package/esm/cramFile/file.js +40 -75
  67. package/esm/cramFile/file.js.map +1 -1
  68. package/esm/cramFile/record.js.map +1 -1
  69. package/esm/cramFile/sectionParsers.js +3 -2
  70. package/esm/cramFile/sectionParsers.js.map +1 -1
  71. package/esm/cramFile/slice/decodeRecord.d.ts +2 -2
  72. package/esm/cramFile/slice/decodeRecord.js +10 -7
  73. package/esm/cramFile/slice/decodeRecord.js.map +1 -1
  74. package/esm/cramFile/slice/index.js +14 -16
  75. package/esm/cramFile/slice/index.js.map +1 -1
  76. package/esm/cramFile/util.d.ts +5 -2
  77. package/esm/cramFile/util.js +74 -77
  78. package/esm/cramFile/util.js.map +1 -1
  79. package/esm/htscodecs/arith_gen.d.ts +5 -7
  80. package/esm/htscodecs/arith_gen.js +108 -97
  81. package/esm/htscodecs/arith_gen.js.map +1 -1
  82. package/esm/htscodecs/arith_sh.d.ts +1 -8
  83. package/esm/htscodecs/arith_sh.js +14 -11
  84. package/esm/htscodecs/arith_sh.js.map +1 -1
  85. package/esm/htscodecs/byte_model.d.ts +1 -6
  86. package/esm/htscodecs/byte_model.js +23 -18
  87. package/esm/htscodecs/byte_model.js.map +1 -1
  88. package/esm/htscodecs/fqzcomp.d.ts +1 -1
  89. package/esm/htscodecs/fqzcomp.js +91 -76
  90. package/esm/htscodecs/fqzcomp.js.map +1 -1
  91. package/esm/htscodecs/index.d.ts +5 -5
  92. package/esm/htscodecs/index.js +14 -20
  93. package/esm/htscodecs/index.js.map +1 -1
  94. package/esm/htscodecs/iostream.d.ts +9 -20
  95. package/esm/htscodecs/iostream.js +19 -117
  96. package/esm/htscodecs/iostream.js.map +1 -1
  97. package/esm/htscodecs/rans.d.ts +1 -1
  98. package/esm/htscodecs/rans.js +61 -56
  99. package/esm/htscodecs/rans.js.map +1 -1
  100. package/esm/htscodecs/rans4x16.d.ts +1 -1
  101. package/esm/htscodecs/rans4x16.js +143 -109
  102. package/esm/htscodecs/rans4x16.js.map +1 -1
  103. package/esm/htscodecs/tok3.d.ts +1 -2
  104. package/esm/htscodecs/tok3.js +41 -237
  105. package/esm/htscodecs/tok3.js.map +1 -1
  106. package/esm/util.d.ts +1 -0
  107. package/esm/util.js +17 -0
  108. package/esm/util.js.map +1 -0
  109. package/package.json +3 -6
  110. package/src/cramFile/codecs/_base.ts +1 -1
  111. package/src/cramFile/codecs/byteArrayLength.ts +4 -12
  112. package/src/cramFile/codecs/external.ts +3 -7
  113. package/src/cramFile/container/index.ts +5 -24
  114. package/src/cramFile/file.ts +44 -79
  115. package/src/cramFile/record.ts +1 -1
  116. package/src/cramFile/sectionParsers.ts +5 -2
  117. package/src/cramFile/slice/decodeRecord.ts +29 -28
  118. package/src/cramFile/slice/index.ts +25 -31
  119. package/src/cramFile/util.ts +107 -106
  120. package/src/htscodecs/{arith_gen.js → arith_gen.ts} +133 -95
  121. package/src/htscodecs/{arith_sh.js → arith_sh.ts} +17 -9
  122. package/src/htscodecs/{byte_model.js → byte_model.ts} +26 -16
  123. package/src/htscodecs/{fqzcomp.js → fqzcomp.ts} +108 -74
  124. package/src/htscodecs/{index.js → index.ts} +14 -20
  125. package/src/htscodecs/iostream.ts +159 -0
  126. package/src/htscodecs/{rans.js → rans.ts} +73 -56
  127. package/src/htscodecs/{rans4x16.js → rans4x16.ts} +180 -111
  128. package/src/htscodecs/tok3.ts +197 -0
  129. package/src/util.ts +16 -0
  130. package/errors.js +0 -27
  131. package/src/htscodecs/iostream.js +0 -257
  132. package/src/htscodecs/tok3.js +0 -413
@@ -1,3 +1,5 @@
1
+ // @ts-nocheck
2
+
1
3
  /*
2
4
  * Copyright (c) 2019 Genome Research Ltd.
3
5
  * Author(s): James Bonfield
@@ -39,9 +41,9 @@
39
41
  // Arith_sh.js 6.7s decode (32-bit with carries)
40
42
  // Arith.js 317.0s decode (64-bit no carries); int64 crippling it.
41
43
 
42
- //----------------------------------------------------------------------
44
+ // ----------------------------------------------------------------------
43
45
  // Arithmetic (range) coder
44
- module.exports = class RangeCoder {
46
+ export default class RangeCoder {
45
47
  constructor(src) {
46
48
  this.low = 0
47
49
  this.range = 0xffffffff
@@ -52,23 +54,25 @@ module.exports = class RangeCoder {
52
54
  }
53
55
 
54
56
  RangeStartDecode(src) {
55
- for (var i = 0; i < 5; i++) this.code = (this.code << 8) + src.ReadByte()
57
+ for (let i = 0; i < 5; i++) {
58
+ this.code = (this.code << 8) + src.ReadByte()
59
+ }
56
60
  this.code &= 0xffffffff
57
61
  this.code >>>= 0 // force to be +ve int
58
62
  }
59
63
 
60
64
  RangeGetFrequency(tot_freq) {
61
65
  this.range = Math.floor(this.range / tot_freq)
62
- //return this.code / this.range;
66
+ // return this.code / this.range;
63
67
  return Math.floor(this.code / this.range)
64
68
 
65
69
  // Conceptual scenario; return freq only and don't modify range yet
66
- //return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
70
+ // return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
67
71
  }
68
72
 
69
73
  RangeDecode(src, sym_low, sym_freq, tot_freq) {
70
74
  // Conceptually we divide range here, but in practice we cached it earlier
71
- //this.range = Math.floor(this.range / tot_freq);
75
+ // this.range = Math.floor(this.range / tot_freq);
72
76
 
73
77
  this.code -= sym_low * this.range
74
78
  this.range *= sym_freq
@@ -111,7 +115,7 @@ module.exports = class RangeCoder {
111
115
  }
112
116
 
113
117
  RangeEncode(dst, sym_low, sym_freq, tot_freq) {
114
- var old_low = this.low
118
+ const old_low = this.low
115
119
  this.range = Math.floor(this.range / tot_freq)
116
120
  this.low += sym_low * this.range
117
121
  this.low >>>= 0 // Truncate to +ve int so we can spot overflow
@@ -121,7 +125,9 @@ module.exports = class RangeCoder {
121
125
  // NB: can this.low < old_low occur twice before range < (1<<24)?
122
126
  // We claim not, but prove it!
123
127
  if (this.low < old_low) {
124
- if (this.carry != 0) console.log('ERROR: Multiple carry')
128
+ if (this.carry != 0) {
129
+ console.log('ERROR: Multiple carry')
130
+ }
125
131
  this.carry = 1
126
132
  }
127
133
 
@@ -133,6 +139,8 @@ module.exports = class RangeCoder {
133
139
  }
134
140
 
135
141
  RangeFinishEncode(dst) {
136
- for (var i = 0; i < 5; i++) this.RangeShiftLow(dst)
142
+ for (let i = 0; i < 5; i++) {
143
+ this.RangeShiftLow(dst)
144
+ }
137
145
  }
138
146
  }
@@ -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
@@ -38,14 +41,14 @@
38
41
  const MAX_FREQ = (1 << 16) - 17
39
42
  const STEP = 16
40
43
 
41
- module.exports = class ByteModel {
44
+ export default class ByteModel {
42
45
  constructor(max_sym = 256) {
43
46
  this.total_freq = max_sym
44
47
  this.max_sym = max_sym - 1
45
- this.S = new Array()
46
- this.F = new Array()
48
+ this.S = []
49
+ this.F = []
47
50
 
48
- for (var i = 0; i <= this.max_sym; i++) {
51
+ for (let i = 0; i <= this.max_sym; i++) {
49
52
  this.S[i] = i
50
53
  this.F[i] = 1
51
54
  }
@@ -53,12 +56,14 @@ module.exports = class ByteModel {
53
56
 
54
57
  ModelDecode(src, rc) {
55
58
  // Find symbol
56
- var freq = rc.RangeGetFrequency(this.total_freq)
59
+ const freq = rc.RangeGetFrequency(this.total_freq)
57
60
 
58
61
  // Linear scan to find cumulative frequency 'freq'
59
- var acc = 0
60
- var x = 0
61
- while (acc + this.F[x] <= freq) acc += this.F[x++]
62
+ let acc = 0
63
+ let x = 0
64
+ while (acc + this.F[x] <= freq) {
65
+ acc += this.F[x++]
66
+ }
62
67
 
63
68
  // for (var acc = 0; (acc += this.F[x]) <= freq; x++)
64
69
  // ;
@@ -70,12 +75,14 @@ module.exports = class ByteModel {
70
75
  // Update model
71
76
  this.F[x] += STEP
72
77
  this.total_freq += STEP
73
- if (this.total_freq > MAX_FREQ) this.ModelRenormalise()
78
+ if (this.total_freq > MAX_FREQ) {
79
+ this.ModelRenormalise()
80
+ }
74
81
 
75
82
  // Keep symbols approximately frequency sorted
76
- var sym = this.S[x]
83
+ const sym = this.S[x]
77
84
  if (x > 0 && this.F[x] > this.F[x - 1]) {
78
- var tmp = this.F[x]
85
+ let tmp = this.F[x]
79
86
  this.F[x] = this.F[x - 1]
80
87
  this.F[x - 1] = tmp
81
88
 
@@ -90,7 +97,7 @@ module.exports = class ByteModel {
90
97
  ModelRenormalise() {
91
98
  // Halve all the frequencies, being careful not to hit zero
92
99
  this.total_freq = 0
93
- for (var i = 0; i <= this.max_sym; i++) {
100
+ for (let i = 0; i <= this.max_sym; i++) {
94
101
  this.F[i] -= Math.floor(this.F[i] / 2)
95
102
  this.total_freq += this.F[i]
96
103
  }
@@ -98,8 +105,10 @@ module.exports = class ByteModel {
98
105
 
99
106
  ModelEncode(dst, rc, sym) {
100
107
  // Find cumulative frequency
101
- var acc = 0
102
- for (var x = 0; this.S[x] != sym; x++) acc += this.F[x]
108
+ let acc = 0
109
+ for (var x = 0; this.S[x] != sym; x++) {
110
+ acc += this.F[x]
111
+ }
103
112
 
104
113
  // Encode
105
114
  rc.RangeEncode(dst, acc, this.F[x], this.total_freq)
@@ -107,14 +116,15 @@ module.exports = class ByteModel {
107
116
  // Update model
108
117
  this.F[x] += STEP
109
118
  this.total_freq += STEP
110
- if (this.total_freq > MAX_FREQ)
119
+ if (this.total_freq > MAX_FREQ) {
111
120
  // FIXME x2
112
121
  this.ModelRenormalise()
122
+ }
113
123
 
114
124
  // Keep symbols approximately frequency sorted
115
125
  var sym = this.S[x]
116
126
  if (x > 0 && this.F[x] > this.F[x - 1]) {
117
- var tmp = this.F[x]
127
+ let tmp = this.F[x]
118
128
  this.F[x] = this.F[x - 1]
119
129
  this.F[x - 1] = tmp
120
130
 
@@ -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
- }