@gmod/cram 3.0.6 → 4.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 (248) hide show
  1. package/dist/craiIndex.js +70 -87
  2. package/dist/craiIndex.js.map +1 -1
  3. package/dist/cram-bundle.js +1 -1
  4. package/dist/cramFile/codecs/_base.d.ts +1 -1
  5. package/dist/cramFile/codecs/beta.d.ts +2 -2
  6. package/dist/cramFile/codecs/beta.js +1 -1
  7. package/dist/cramFile/codecs/beta.js.map +1 -1
  8. package/dist/cramFile/codecs/byteArrayLength.d.ts +2 -2
  9. package/dist/cramFile/codecs/byteArrayStop.d.ts +4 -4
  10. package/dist/cramFile/codecs/byteArrayStop.js +1 -1
  11. package/dist/cramFile/codecs/byteArrayStop.js.map +1 -1
  12. package/dist/cramFile/codecs/external.d.ts +1 -1
  13. package/dist/cramFile/codecs/external.js +1 -1
  14. package/dist/cramFile/codecs/external.js.map +1 -1
  15. package/dist/cramFile/codecs/gamma.d.ts +1 -1
  16. package/dist/cramFile/codecs/gamma.js +1 -1
  17. package/dist/cramFile/codecs/gamma.js.map +1 -1
  18. package/dist/cramFile/codecs/getBits.d.ts +1 -1
  19. package/dist/cramFile/codecs/getBits.js.map +1 -1
  20. package/dist/cramFile/codecs/huffman.d.ts +1 -1
  21. package/dist/cramFile/codecs/huffman.js +1 -1
  22. package/dist/cramFile/codecs/huffman.js.map +1 -1
  23. package/dist/cramFile/codecs/index.d.ts +1 -1
  24. package/dist/cramFile/codecs/index.js +5 -5
  25. package/dist/cramFile/codecs/index.js.map +1 -1
  26. package/dist/cramFile/codecs/subexp.d.ts +2 -2
  27. package/dist/cramFile/codecs/subexp.js +1 -1
  28. package/dist/cramFile/codecs/subexp.js.map +1 -1
  29. package/dist/cramFile/container/compressionScheme.d.ts +2 -2
  30. package/dist/cramFile/container/compressionScheme.js +1 -1
  31. package/dist/cramFile/container/compressionScheme.js.map +1 -1
  32. package/dist/cramFile/container/index.d.ts +1 -2
  33. package/dist/cramFile/container/index.js +62 -79
  34. package/dist/cramFile/container/index.js.map +1 -1
  35. package/dist/cramFile/file.d.ts +9 -13
  36. package/dist/cramFile/file.js +218 -249
  37. package/dist/cramFile/file.js.map +1 -1
  38. package/dist/cramFile/record.js +2 -2
  39. package/dist/cramFile/record.js.map +1 -1
  40. package/dist/cramFile/sectionParsers.d.ts +16 -16
  41. package/dist/cramFile/sectionParsers.js +12 -9
  42. package/dist/cramFile/sectionParsers.js.map +1 -1
  43. package/dist/cramFile/slice/decodeRecord.d.ts +3 -3
  44. package/dist/cramFile/slice/decodeRecord.js +11 -11
  45. package/dist/cramFile/slice/decodeRecord.js.map +1 -1
  46. package/dist/cramFile/slice/index.d.ts +2 -2
  47. package/dist/cramFile/slice/index.js +241 -262
  48. package/dist/cramFile/slice/index.js.map +1 -1
  49. package/dist/cramFile/util.d.ts +4 -2
  50. package/dist/cramFile/util.js +29 -6
  51. package/dist/cramFile/util.js.map +1 -1
  52. package/dist/htscodecs/arith_gen.d.ts +18 -0
  53. package/dist/htscodecs/arith_gen.js +318 -0
  54. package/dist/htscodecs/arith_gen.js.map +1 -0
  55. package/dist/htscodecs/arith_sh.d.ts +16 -0
  56. package/dist/htscodecs/arith_sh.js +128 -0
  57. package/dist/htscodecs/arith_sh.js.map +1 -0
  58. package/dist/htscodecs/byte_model.d.ts +11 -0
  59. package/dist/htscodecs/byte_model.js +113 -0
  60. package/dist/htscodecs/byte_model.js.map +1 -0
  61. package/dist/htscodecs/fqzcomp.d.ts +1 -0
  62. package/dist/htscodecs/fqzcomp.js +325 -0
  63. package/dist/htscodecs/fqzcomp.js.map +1 -0
  64. package/dist/htscodecs/index.d.ts +5 -0
  65. package/dist/htscodecs/index.js +70 -0
  66. package/dist/htscodecs/index.js.map +1 -0
  67. package/dist/htscodecs/iostream.d.ts +27 -0
  68. package/dist/htscodecs/iostream.js +243 -0
  69. package/dist/htscodecs/iostream.js.map +1 -0
  70. package/dist/htscodecs/rans.d.ts +1 -0
  71. package/dist/htscodecs/rans.js +213 -0
  72. package/dist/htscodecs/rans.js.map +1 -0
  73. package/dist/htscodecs/rans4x16.d.ts +1 -0
  74. package/dist/htscodecs/rans4x16.js +405 -0
  75. package/dist/htscodecs/rans4x16.js.map +1 -0
  76. package/dist/htscodecs/tok3.d.ts +2 -0
  77. package/dist/htscodecs/tok3.js +363 -0
  78. package/dist/htscodecs/tok3.js.map +1 -0
  79. package/dist/index.d.ts +1 -1
  80. package/dist/index.js +2 -2
  81. package/dist/index.js.map +1 -1
  82. package/dist/indexedCramFile.d.ts +4 -4
  83. package/dist/indexedCramFile.js +97 -108
  84. package/dist/indexedCramFile.js.map +1 -1
  85. package/dist/io/index.d.ts +2 -2
  86. package/dist/io/index.js +6 -6
  87. package/dist/io/index.js.map +1 -1
  88. package/dist/rans/constants.d.ts +1 -1
  89. package/dist/rans/constants.js +1 -1
  90. package/dist/rans/constants.js.map +1 -1
  91. package/dist/rans/d04.js.map +1 -1
  92. package/dist/rans/decoding.js.map +1 -1
  93. package/dist/rans/frequencies.js.map +1 -1
  94. package/dist/rans/index.js +5 -5
  95. package/dist/rans/index.js.map +1 -1
  96. package/dist/unzip.d.ts +1 -1
  97. package/dist/unzip.js +5 -3
  98. package/dist/unzip.js.map +1 -1
  99. package/esm/craiIndex.js +10 -12
  100. package/esm/craiIndex.js.map +1 -1
  101. package/esm/cramFile/codecs/_base.d.ts +1 -1
  102. package/esm/cramFile/codecs/beta.d.ts +2 -2
  103. package/esm/cramFile/codecs/beta.js +1 -1
  104. package/esm/cramFile/codecs/beta.js.map +1 -1
  105. package/esm/cramFile/codecs/byteArrayLength.d.ts +2 -2
  106. package/esm/cramFile/codecs/byteArrayStop.d.ts +4 -4
  107. package/esm/cramFile/codecs/byteArrayStop.js +1 -1
  108. package/esm/cramFile/codecs/byteArrayStop.js.map +1 -1
  109. package/esm/cramFile/codecs/external.d.ts +1 -1
  110. package/esm/cramFile/codecs/external.js +1 -1
  111. package/esm/cramFile/codecs/external.js.map +1 -1
  112. package/esm/cramFile/codecs/gamma.d.ts +1 -1
  113. package/esm/cramFile/codecs/gamma.js +1 -1
  114. package/esm/cramFile/codecs/gamma.js.map +1 -1
  115. package/esm/cramFile/codecs/getBits.d.ts +1 -1
  116. package/esm/cramFile/codecs/getBits.js.map +1 -1
  117. package/esm/cramFile/codecs/huffman.d.ts +1 -1
  118. package/esm/cramFile/codecs/huffman.js +1 -1
  119. package/esm/cramFile/codecs/huffman.js.map +1 -1
  120. package/esm/cramFile/codecs/index.d.ts +1 -1
  121. package/esm/cramFile/codecs/index.js +5 -5
  122. package/esm/cramFile/codecs/index.js.map +1 -1
  123. package/esm/cramFile/codecs/subexp.d.ts +2 -2
  124. package/esm/cramFile/codecs/subexp.js +1 -1
  125. package/esm/cramFile/codecs/subexp.js.map +1 -1
  126. package/esm/cramFile/container/compressionScheme.d.ts +2 -2
  127. package/esm/cramFile/container/compressionScheme.js +1 -1
  128. package/esm/cramFile/container/compressionScheme.js.map +1 -1
  129. package/esm/cramFile/container/index.d.ts +1 -2
  130. package/esm/cramFile/container/index.js +4 -7
  131. package/esm/cramFile/container/index.js.map +1 -1
  132. package/esm/cramFile/file.d.ts +9 -13
  133. package/esm/cramFile/file.js +54 -61
  134. package/esm/cramFile/file.js.map +1 -1
  135. package/esm/cramFile/record.js +2 -2
  136. package/esm/cramFile/record.js.map +1 -1
  137. package/esm/cramFile/sectionParsers.d.ts +16 -16
  138. package/esm/cramFile/sectionParsers.js +7 -7
  139. package/esm/cramFile/sectionParsers.js.map +1 -1
  140. package/esm/cramFile/slice/decodeRecord.d.ts +3 -3
  141. package/esm/cramFile/slice/decodeRecord.js +11 -11
  142. package/esm/cramFile/slice/decodeRecord.js.map +1 -1
  143. package/esm/cramFile/slice/index.d.ts +2 -2
  144. package/esm/cramFile/slice/index.js +7 -6
  145. package/esm/cramFile/slice/index.js.map +1 -1
  146. package/esm/cramFile/util.d.ts +4 -2
  147. package/esm/cramFile/util.js +19 -2
  148. package/esm/cramFile/util.js.map +1 -1
  149. package/esm/htscodecs/arith_gen.d.ts +18 -0
  150. package/esm/htscodecs/arith_gen.js +318 -0
  151. package/esm/htscodecs/arith_gen.js.map +1 -0
  152. package/esm/htscodecs/arith_sh.d.ts +16 -0
  153. package/esm/htscodecs/arith_sh.js +128 -0
  154. package/esm/htscodecs/arith_sh.js.map +1 -0
  155. package/esm/htscodecs/byte_model.d.ts +11 -0
  156. package/esm/htscodecs/byte_model.js +113 -0
  157. package/esm/htscodecs/byte_model.js.map +1 -0
  158. package/esm/htscodecs/fqzcomp.d.ts +1 -0
  159. package/esm/htscodecs/fqzcomp.js +325 -0
  160. package/esm/htscodecs/fqzcomp.js.map +1 -0
  161. package/esm/htscodecs/index.d.ts +5 -0
  162. package/esm/htscodecs/index.js +70 -0
  163. package/esm/htscodecs/index.js.map +1 -0
  164. package/esm/htscodecs/iostream.d.ts +27 -0
  165. package/esm/htscodecs/iostream.js +243 -0
  166. package/esm/htscodecs/iostream.js.map +1 -0
  167. package/esm/htscodecs/rans.d.ts +1 -0
  168. package/esm/htscodecs/rans.js +213 -0
  169. package/esm/htscodecs/rans.js.map +1 -0
  170. package/esm/htscodecs/rans4x16.d.ts +1 -0
  171. package/esm/htscodecs/rans4x16.js +405 -0
  172. package/esm/htscodecs/rans4x16.js.map +1 -0
  173. package/esm/htscodecs/tok3.d.ts +2 -0
  174. package/esm/htscodecs/tok3.js +363 -0
  175. package/esm/htscodecs/tok3.js.map +1 -0
  176. package/esm/index.d.ts +1 -1
  177. package/esm/index.js +1 -1
  178. package/esm/index.js.map +1 -1
  179. package/esm/indexedCramFile.d.ts +4 -4
  180. package/esm/indexedCramFile.js +10 -10
  181. package/esm/indexedCramFile.js.map +1 -1
  182. package/esm/io/index.d.ts +2 -2
  183. package/esm/io/index.js +1 -1
  184. package/esm/io/index.js.map +1 -1
  185. package/esm/rans/constants.d.ts +1 -1
  186. package/esm/rans/constants.js +1 -1
  187. package/esm/rans/constants.js.map +1 -1
  188. package/esm/rans/d04.js.map +1 -1
  189. package/esm/rans/decoding.js.map +1 -1
  190. package/esm/rans/frequencies.js.map +1 -1
  191. package/esm/rans/index.js +5 -5
  192. package/esm/rans/index.js.map +1 -1
  193. package/esm/unzip.d.ts +1 -1
  194. package/esm/unzip.js +4 -1
  195. package/esm/unzip.js.map +1 -1
  196. package/package.json +7 -10
  197. package/src/craiIndex.ts +17 -16
  198. package/src/cramFile/codecs/_base.ts +1 -1
  199. package/src/cramFile/codecs/beta.ts +3 -3
  200. package/src/cramFile/codecs/byteArrayLength.ts +1 -1
  201. package/src/cramFile/codecs/byteArrayStop.ts +3 -4
  202. package/src/cramFile/codecs/external.ts +3 -3
  203. package/src/cramFile/codecs/gamma.ts +2 -2
  204. package/src/cramFile/codecs/getBits.ts +1 -1
  205. package/src/cramFile/codecs/huffman.ts +2 -3
  206. package/src/cramFile/codecs/index.ts +7 -8
  207. package/src/cramFile/codecs/subexp.ts +3 -3
  208. package/src/cramFile/container/compressionScheme.ts +3 -3
  209. package/src/cramFile/container/index.ts +7 -10
  210. package/src/cramFile/file.ts +78 -86
  211. package/src/cramFile/record.ts +3 -3
  212. package/src/cramFile/sectionParsers.ts +27 -22
  213. package/src/cramFile/slice/decodeRecord.ts +17 -17
  214. package/src/cramFile/slice/index.ts +8 -9
  215. package/src/cramFile/util.ts +24 -6
  216. package/src/htscodecs/arith_gen.js +344 -0
  217. package/src/htscodecs/arith_sh.js +138 -0
  218. package/src/htscodecs/byte_model.js +126 -0
  219. package/src/htscodecs/fqzcomp.js +360 -0
  220. package/src/htscodecs/index.js +77 -0
  221. package/src/htscodecs/iostream.js +257 -0
  222. package/src/htscodecs/rans.js +233 -0
  223. package/src/htscodecs/rans4x16.js +452 -0
  224. package/src/htscodecs/tok3.js +413 -0
  225. package/src/index.ts +1 -1
  226. package/src/indexedCramFile.ts +6 -5
  227. package/src/io/index.ts +5 -4
  228. package/src/rans/constants.ts +1 -1
  229. package/src/rans/d04.ts +0 -1
  230. package/src/rans/decoding.ts +0 -1
  231. package/src/rans/frequencies.ts +0 -1
  232. package/src/rans/index.ts +5 -7
  233. package/src/unzip.ts +5 -1
  234. package/dist/cramFile/filehandle.d.ts +0 -1
  235. package/dist/cramFile/filehandle.js +0 -3
  236. package/dist/cramFile/filehandle.js.map +0 -1
  237. package/dist/unzip-pako.d.ts +0 -2
  238. package/dist/unzip-pako.js +0 -9
  239. package/dist/unzip-pako.js.map +0 -1
  240. package/esm/cramFile/filehandle.d.ts +0 -1
  241. package/esm/cramFile/filehandle.js +0 -2
  242. package/esm/cramFile/filehandle.js.map +0 -1
  243. package/esm/unzip-pako.d.ts +0 -2
  244. package/esm/unzip-pako.js +0 -6
  245. package/esm/unzip-pako.js.map +0 -1
  246. package/src/cramFile/filehandle.ts +0 -1
  247. package/src/typings/htscodecs.d.ts +0 -6
  248. package/src/unzip-pako.ts +0 -6
@@ -0,0 +1,360 @@
1
+ /*
2
+ * Copyright (c) 2019 Genome Research Ltd.
3
+ * Author(s): James Bonfield
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above
12
+ * copyright notice, this list of conditions and the following
13
+ * disclaimer in the documentation and/or other materials provided
14
+ * with the distribution.
15
+ *
16
+ * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
17
+ * Institute nor the names of its contributors may be used to endorse
18
+ * or promote products derived from this software without specific
19
+ * prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
22
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
25
+ * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ */
33
+
34
+ const IOStream = require('./iostream')
35
+ const ByteModel = require('./byte_model')
36
+ const RangeCoder = require('./arith_sh')
37
+
38
+ //----------------------------------------------------------------------
39
+ // Main arithmetic entry function: decodes a compressed src and
40
+ // returns the uncompressed buffer.
41
+
42
+ 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
+
47
+ // Remove first level of run-length encoding
48
+ var R = new Array(1024) // runs
49
+ while (z < size) {
50
+ var run = src.ReadByte()
51
+ R[j++] = run
52
+ z += run
53
+
54
+ if (run == last) {
55
+ var copy = src.ReadByte()
56
+ z += run * copy
57
+ while (copy--) R[j++] = run
58
+ }
59
+ last = run
60
+ }
61
+
62
+ // Now expand runs in R to tab, noting 255 is max run
63
+ var i = 0
64
+ j = 0
65
+ z = 0
66
+ while (z < size) {
67
+ var run_len = 0
68
+ do {
69
+ var part = R[j++]
70
+ run_len += part
71
+ } while (part == 255)
72
+
73
+ while (run_len--) tab[z++] = i
74
+ i++
75
+ }
76
+ }
77
+
78
+ const QMAX = 256
79
+
80
+ const FLAG_DEDUP = 2
81
+ const FLAG_FLEN = 4
82
+ const FLAG_SEL = 8 // whether selector is used in context
83
+ const FLAG_QMAP = 16
84
+ const FLAG_PTAB = 32
85
+ const FLAG_DTAB = 64
86
+ const FLAG_QTAB = 128
87
+
88
+ const GFLAG_MULTI_PARAM = 1
89
+ const GFLAG_HAVE_STAB = 2
90
+ const GFLAG_DO_REV = 4
91
+
92
+ // Compute a new context from our current state and qual q
93
+ function fqz_update_ctx(params, state, q) {
94
+ var last = params.context
95
+ state.qctx = (state.qctx << params.qshift) + params.qtab[q] // >>> 0
96
+ last += (state.qctx & ((1 << params.qbits) - 1)) << params.qloc // >>> 0
97
+
98
+ if (params.do_pos) last += params.ptab[Math.min(state.p, 1023)] << params.ploc
99
+
100
+ if (params.do_delta) {
101
+ last += params.dtab[Math.min(state.delta, 255)] << params.dloc
102
+ // Is it better to use q here or qtab[q]?
103
+ // If qtab[q] we can map eg [a-z0-9A-Z]->0 ,->1 and have
104
+ // delta being a token number count into comma separated lists?
105
+ state.delta += state.prevq != q ? 1 : 0
106
+ state.prevq = q
107
+ }
108
+
109
+ if (params.do_sel) last += state.s << params.sloc
110
+
111
+ state.p--
112
+
113
+ return last & 0xffff
114
+ }
115
+
116
+ function decode_fqz_single_param(src) {
117
+ var p = {} // params
118
+
119
+ // Load FQZ parameters
120
+ p.context = src.ReadUint16()
121
+ p.pflags = src.ReadByte()
122
+
123
+ p.do_dedup = p.pflags & FLAG_DEDUP
124
+ p.fixed_len = p.pflags & FLAG_FLEN
125
+ p.do_sel = p.pflags & FLAG_SEL
126
+ p.do_qmap = p.pflags & FLAG_QMAP
127
+ p.do_pos = p.pflags & FLAG_PTAB
128
+ p.do_delta = p.pflags & FLAG_DTAB
129
+ p.do_qtab = p.pflags & FLAG_QTAB
130
+
131
+ p.max_sym = src.ReadByte()
132
+
133
+ var x = src.ReadByte()
134
+ p.qbits = x >> 4
135
+ p.qshift = x & 15
136
+ x = src.ReadByte()
137
+ p.qloc = x >> 4
138
+ p.sloc = x & 15
139
+ x = src.ReadByte()
140
+ p.ploc = x >> 4
141
+ p.dloc = x & 15
142
+
143
+ // Qual map, eg to "unbin" Illumina qualities
144
+ p.qmap = new Array(256)
145
+ if (p.pflags & FLAG_QMAP) {
146
+ for (var i = 0; i < p.max_sym; i++) p.qmap[i] = src.ReadByte()
147
+ } else {
148
+ // Useful optimisation to speed up main loop
149
+ for (var i = 0; i < 256; i++) p.qmap[i] = i // NOP
150
+ }
151
+
152
+ // Read tables
153
+ p.qtab = new Array(1024)
154
+ if (p.qbits > 0 && p.pflags & FLAG_QTAB) {
155
+ read_array(src, p.qtab, 256)
156
+ } else {
157
+ // Useful optimisation to speed up main loop
158
+ for (var i = 0; i < 256; i++) p.qtab[i] = i // NOP
159
+ }
160
+
161
+ p.ptab = new Array(1024)
162
+ if (p.pflags & FLAG_PTAB) read_array(src, p.ptab, 1024)
163
+
164
+ p.dtab = new Array(256)
165
+ if (p.pflags & FLAG_DTAB) read_array(src, p.dtab, 256)
166
+
167
+ return p
168
+ }
169
+
170
+ function decode_fqz_params(src) {
171
+ var gparams = {
172
+ max_sym: 0,
173
+ }
174
+
175
+ // Check fqz format version
176
+ var vers = src.ReadByte()
177
+ if (vers != 5) {
178
+ console.error('Invalid FQZComp version number')
179
+ return
180
+ }
181
+
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
185
+
186
+ var stab = new Array(256)
187
+ if (gflags & GFLAG_HAVE_STAB) {
188
+ max_sel = src.ReadByte()
189
+ read_array(src, stab, 256)
190
+ } else {
191
+ for (var i = 0; i < nparam; i++) stab[i] = i
192
+ for (; i < 256; i++) stab[i] = nparam - 1
193
+ }
194
+ gparams.do_rev = gflags & GFLAG_DO_REV
195
+ gparams.stab = stab
196
+ gparams.max_sel = max_sel
197
+
198
+ gparams.params = new Array(gparams.nparam)
199
+ for (var p = 0; p < nparam; p++) {
200
+ gparams.params[p] = decode_fqz_single_param(src)
201
+ if (gparams.max_sym < gparams.params[p].max_sym)
202
+ gparams.max_sym = gparams.params[p].max_sym
203
+ }
204
+
205
+ return gparams
206
+ }
207
+
208
+ function fqz_create_models(gparams) {
209
+ var model = {}
210
+
211
+ 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
214
+
215
+ model.len = new Array(4)
216
+ for (var i = 0; i < 4; i++) model.len[i] = new ByteModel(256)
217
+
218
+ model.rev = new ByteModel(2)
219
+ model.dup = new ByteModel(2)
220
+
221
+ if (gparams.max_sel > 0) model.sel = new ByteModel(gparams.max_sel + 1) // +1 as max value not num. values
222
+
223
+ return model
224
+ }
225
+
226
+ // Initialise a new record, updating state.
227
+ // Returns 1 if dup, otherwise 0
228
+ function decode_fqz_new_record(src, rc, gparams, model, state, rev) {
229
+ // Parameter selector
230
+ if (gparams.max_sel > 0) {
231
+ state.s = model.sel.ModelDecode(src, rc)
232
+ } else {
233
+ state.s = 0
234
+ }
235
+ state.x = gparams.stab[state.s]
236
+
237
+ var params = gparams.params[state.x]
238
+
239
+ // Reset contexts at the start of each new record
240
+ if (params.fixed_len >= 0) {
241
+ // Not fixed or fixed but first record
242
+ var len = model.len[0].ModelDecode(src, rc)
243
+ len |= model.len[1].ModelDecode(src, rc) << 8
244
+ len |= model.len[2].ModelDecode(src, rc) << 16
245
+ len |= model.len[3].ModelDecode(src, rc) << 24
246
+ if (params.fixed_len > 0) params.fixed_len = -len
247
+ } else {
248
+ len = -params.fixed_len
249
+ }
250
+ state.len = len
251
+
252
+ if (gparams.do_rev) rev[state.rec] = model.rev.ModelDecode(src, rc)
253
+
254
+ state.is_dup = 0
255
+ if (params.pflags & FLAG_DEDUP) {
256
+ if (model.dup.ModelDecode(src, rc)) state.is_dup = 1
257
+ }
258
+
259
+ state.p = len // number of remaining bytes in this record
260
+ state.delta = 0
261
+ state.qctx = 0
262
+ state.prevq = 0
263
+ state.rec++
264
+ }
265
+
266
+ function decode_fqz(src, q_lens) {
267
+ // Decode parameter block
268
+ var n_out = src.ReadUint7()
269
+ var gparams = decode_fqz_params(src)
270
+ if (!gparams) return
271
+ var params = gparams.params
272
+ var rev = new Array(q_lens.length)
273
+
274
+ // Create initial models
275
+ var model = fqz_create_models(gparams)
276
+
277
+ // Create our entropy encoder and output buffers
278
+ var rc = new RangeCoder(src)
279
+ rc.RangeStartDecode(src)
280
+ var output = new Uint8Array(n_out)
281
+
282
+ // Internal FQZ state
283
+ var state = {
284
+ qctx: 0, // Qual-only sub-context
285
+ prevq: 0, // Previous quality value
286
+ delta: 0, // Running delta (q vs prevq)
287
+ p: 0, // Number of bases left in current record
288
+ s: 0, // Current parameter selector value (0 if unused)
289
+ x: 0, // "stab" tabulated copy of s
290
+ len: 0, // Length of current string
291
+ is_dup: 0, // This string is a duplicate of last
292
+ rec: 0, // Record number
293
+ }
294
+
295
+ // The main decode loop itself
296
+ var i = 0 // position in output buffer
297
+ while (i < n_out) {
298
+ if (state.p == 0) {
299
+ decode_fqz_new_record(src, rc, gparams, model, state, rev)
300
+ if (state.is_dup > 0) {
301
+ if (model.dup.ModelDecode(src, rc)) {
302
+ // Duplicate of last line
303
+ for (var x = 0; x < len; x++)
304
+ output[i + x] = output[i + x - state.len]
305
+ i += state.len
306
+ state.p = 0
307
+ continue
308
+ }
309
+ }
310
+ q_lens.push(state.len)
311
+
312
+ var params = gparams.params[state.x]
313
+ var last = params.context
314
+ }
315
+
316
+ // Decode the current quality (possibly mapped via qmap)
317
+ var Q = model.qual[last].ModelDecode(src, rc)
318
+
319
+ //if (params.do_qmap)
320
+ // output[i++] = params.qmap[Q];
321
+ //else
322
+ // output[i++] = Q
323
+ output[i++] = params.qmap[Q] // optimised version of above
324
+ last = fqz_update_ctx(params, state, Q)
325
+ }
326
+
327
+ if (gparams.do_rev) reverse_qualities(output, n_out, rev, q_lens)
328
+
329
+ return output
330
+ }
331
+
332
+ function reverse_qualities(qual, qual_len, rev, len) {
333
+ var rec = 0
334
+ var i = 0
335
+ while (i < qual_len) {
336
+ if (rev[rec]) {
337
+ var j = 0
338
+ var k = len[rec] - 1
339
+ while (j < k) {
340
+ var tmp = qual[i + j]
341
+ qual[i + j] = qual[i + k]
342
+ qual[i + k] = tmp
343
+ j++
344
+ k--
345
+ }
346
+ }
347
+
348
+ i += len[rec++]
349
+ }
350
+ }
351
+
352
+ function decode(src, q_lens) {
353
+ var stream = new IOStream(src)
354
+
355
+ //var n_out = stream.ReadUint32(); stream.ReadUint32(); // move to main
356
+
357
+ return decode_fqz(stream, q_lens)
358
+ }
359
+
360
+ module.exports = { decode }
@@ -0,0 +1,77 @@
1
+ /*
2
+ * Copyright (c) 2020 Genome Research Ltd.
3
+ * Author(s): James Bonfield
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above
12
+ * copyright notice, this list of conditions and the following
13
+ * disclaimer in the documentation and/or other materials provided
14
+ * with the distribution.
15
+ *
16
+ * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
17
+ * Institute nor the names of its contributors may be used to endorse
18
+ * or promote products derived from this software without specific
19
+ * prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
22
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
25
+ * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ */
33
+
34
+ // This is an interface to the htscodecs reference implementation of
35
+ // the CRAM 3.1 codecs.
36
+
37
+ // This JavaScript file is not part of the reference implementation
38
+ // and is simply and interface to get a consistent interface for cram-js.
39
+
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')
45
+
46
+ function r4x8_uncompress(inputBuffer, outputBuffer) {
47
+ return r4x8.decode(inputBuffer)
48
+ }
49
+
50
+ function r4x16_uncompress(inputBuffer) {
51
+ return r4x16.decode(inputBuffer)
52
+ }
53
+
54
+ function arith_uncompress(inputBuffer) {
55
+ // fix by @cmdcolin for CRAM 3.1
56
+ // xref https://github.com/jkbonfield/htscodecs/pull/1/files
57
+ return new arith().decode(inputBuffer)
58
+ }
59
+
60
+ function fqzcomp_uncompress(inputBuffer) {
61
+ var q_lens = new Array()
62
+ return fqzcomp.decode(inputBuffer, q_lens)
63
+ }
64
+
65
+ function tok3_uncompress(inputBuffer) {
66
+ // Returns in string form instead of buffer
67
+ var out = tok3.decode(inputBuffer, 0, '\0')
68
+ return Uint8Array.from(Array.from(out).map(letter => letter.charCodeAt(0)))
69
+ }
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,257 @@
1
+ /*
2
+ * Copyright (c) 2019 Genome Research Ltd.
3
+ * Author(s): James Bonfield
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above
12
+ * copyright notice, this list of conditions and the following
13
+ * disclaimer in the documentation and/or other materials provided
14
+ * with the distribution.
15
+ *
16
+ * 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
17
+ * Institute nor the names of its contributors may be used to endorse
18
+ * or promote products derived from this software without specific
19
+ * prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
22
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
25
+ * LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+ */
33
+
34
+ // Turn a buffer into a fake stream with get / put commands.
35
+ // This enables up to closely match the published pseudocode.
36
+ module.exports = class IOStream {
37
+ constructor(buf, start_pos = 0, size = 0) {
38
+ if (size != 0) {
39
+ this.buf = new Uint8Array(size)
40
+ this.length = size
41
+ } else {
42
+ this.buf = buf
43
+ this.length = buf.length
44
+ }
45
+ this.dataView = new DataView(this.buf.buffer)
46
+ this.pos = start_pos
47
+ }
48
+
49
+ // ----------
50
+ // Reading
51
+ EOF() {
52
+ return this.pos >= this.length
53
+ }
54
+
55
+ ReadData(len) {
56
+ var A = this.buf.slice(this.pos, this.pos + len)
57
+ this.pos += len
58
+ return A
59
+ }
60
+
61
+ ReadByte() {
62
+ const b = this.buf[this.pos]
63
+ this.pos++
64
+ return b
65
+ }
66
+
67
+ ReadChar() {
68
+ const b = this.buf[this.pos]
69
+ this.pos++
70
+ return String.fromCharCode(b)
71
+ }
72
+
73
+ ReadUint16() {
74
+ var i = this.ReadByte()
75
+ i |= this.ReadByte() << 8
76
+ return i
77
+ }
78
+
79
+ ReadUint32() {
80
+ const i = this.dataView.getInt32(this.pos, true)
81
+ this.pos += 4
82
+ return i
83
+ }
84
+
85
+ // nul terminated string
86
+ ReadString() {
87
+ var s = ''
88
+ do {
89
+ var b = this.buf[this.pos++]
90
+ if (b) s += String.fromCharCode(b)
91
+ } while (b)
92
+ return s
93
+ }
94
+
95
+ // ReadUint7() {
96
+ // // Variable sized unsigned integers
97
+ // var i = 0;
98
+ // var s = 0;
99
+ // do {
100
+ // var c = this.ReadByte();
101
+ // i = i | ((c & 0x7f)<<s);
102
+ // s += 7;
103
+ // } while ((c & 0x80))
104
+ //
105
+ // return i;
106
+ // }
107
+
108
+ ReadUint7() {
109
+ // Variable sized unsigned integers
110
+ var i = 0
111
+ do {
112
+ var c = this.ReadByte()
113
+ i = (i << 7) | (c & 0x7f)
114
+ } while (c & 0x80)
115
+
116
+ return i
117
+ }
118
+
119
+ ReadITF8() {
120
+ var i = this.buf[this.pos]
121
+ this.pos++
122
+
123
+ //process.stderr.write("i="+i+"\n");
124
+
125
+ if (i >= 0xf0) {
126
+ // 1111xxxx => +4 bytes
127
+ i = (i & 0x0f) << 28
128
+ i +=
129
+ (this.buf[this.pos + 0] << 20) +
130
+ (this.buf[this.pos + 1] << 12) +
131
+ (this.buf[this.pos + 2] << 4) +
132
+ (this.buf[this.pos + 3] >> 4)
133
+ this.pos += 4
134
+ //process.stderr.write(" 4i="+i+"\n");
135
+ } else if (i >= 0xe0) {
136
+ // 1110xxxx => +3 bytes
137
+ i = (i & 0x0f) << 24
138
+ i +=
139
+ (this.buf[this.pos + 0] << 16) +
140
+ (this.buf[this.pos + 1] << 8) +
141
+ (this.buf[this.pos + 2] << 0)
142
+ this.pos += 3
143
+ //process.stderr.write(" 3i="+i+"\n");
144
+ } else if (i >= 0xc0) {
145
+ // 110xxxxx => +2 bytes
146
+ i = (i & 0x1f) << 16
147
+ i += (this.buf[this.pos + 0] << 8) + (this.buf[this.pos + 1] << 0)
148
+ this.pos += 2
149
+ //process.stderr.write(" 2i="+i+"\n");
150
+ } else if (i >= 0x80) {
151
+ // 10xxxxxx => +1 bytes
152
+ i = (i & 0x3f) << 8
153
+ i += this.buf[this.pos]
154
+ this.pos++
155
+ //process.stderr.write(" 1i="+i+"\n");
156
+ } else {
157
+ // 0xxxxxxx => +0 bytes
158
+ }
159
+
160
+ return i
161
+ }
162
+
163
+ // ----------
164
+ // Writing
165
+ WriteByte(b) {
166
+ this.buf[this.pos++] = b
167
+ }
168
+
169
+ WriteChar(b) {
170
+ this.buf[this.pos++] = b.charCodeAt(0)
171
+ }
172
+
173
+ WriteString(str) {
174
+ for (var i = 0; i < str.length; i++)
175
+ this.buf[this.pos++] = str.charCodeAt(i)
176
+ this.buf[this.pos++] = 0
177
+ }
178
+
179
+ WriteData(buf, len) {
180
+ for (var i = 0; i < len; i++) this.buf[this.pos++] = buf[i]
181
+ }
182
+
183
+ WriteStream(stream) {
184
+ this.WriteData(stream.buf, stream.pos)
185
+ }
186
+
187
+ WriteUint16(u) {
188
+ //this.buf.writeInt16LE(u, this.pos);
189
+ this.WriteByte(u & 0xff)
190
+ this.WriteByte((u >> 8) & 0xff)
191
+ }
192
+
193
+ WriteUint32(u) {
194
+ this.buf.writeInt32LE(u, this.pos)
195
+ this.pos += 4
196
+ }
197
+
198
+ // WriteUint7(i) {
199
+ // do {
200
+ // this.WriteByte((i & 0x7f) | ((i > 0x80) << 7));
201
+ // i >>= 7;
202
+ // } while (i > 0);
203
+ // }
204
+
205
+ WriteUint7(i) {
206
+ var s = 0
207
+ var X = i
208
+ do {
209
+ s += 7
210
+ X >>= 7
211
+ } while (X > 0)
212
+
213
+ do {
214
+ s -= 7
215
+ this.WriteByte(((i >> s) & 0x7f) + ((s > 0) << 7))
216
+ } while (s > 0)
217
+ }
218
+
219
+ WriteITF8(i) {
220
+ // Horrid, ITF8 is unsigned, but we still write signed into it
221
+ if (i < 0) i = (1 << 32) + i
222
+
223
+ if (i <= 0x0000007f) {
224
+ // 1 byte
225
+ this.buf[this.pos++] = i
226
+ } else if (i <= 0x00003fff) {
227
+ // 2 bytes
228
+ this.buf[this.pos++] = 0x80 | Math.floor(i / 256)
229
+ this.buf[this.pos++] = i & 0xff
230
+ } else if (i < 0x0001ffff) {
231
+ // 3 bytes
232
+ this.buf[this.pos++] = 0xc0 | Math.floor(i / 65536)
233
+ this.buf[this.pos++] = Math.floor(i / 256) & 0xff
234
+ this.buf[this.pos++] = i & 0xff
235
+ } else if (i < 0x0fffffff) {
236
+ // 4 bytes
237
+ this.buf[this.pos++] = 0xe0 | Math.floor(i / 16777216)
238
+ this.buf[this.pos++] = Math.floor(i / 65536) & 0xff
239
+ this.buf[this.pos++] = Math.floor(i / 256) & 0xff
240
+ this.buf[this.pos++] = i & 0xff
241
+ } else {
242
+ // 5 bytes; oddly using 4.5 bytes
243
+ this.buf[this.pos++] = 0xf0 | Math.floor(i / 268435456)
244
+ this.buf[this.pos++] = Math.floor(i / 1048576) & 0xff
245
+ this.buf[this.pos++] = Math.floor(i / 4096) & 0xff
246
+ this.buf[this.pos++] = Math.floor(i / 4) & 0xff
247
+ this.buf[this.pos++] = i & 0x0f
248
+ }
249
+ }
250
+
251
+ // ----------
252
+ // Writing from end of buffer going backwards.
253
+ // Needed by rANS codec.
254
+ WriteByteNeg(b) {
255
+ this.buf[--this.pos] = b
256
+ }
257
+ }