@gmod/cram 3.0.6 → 3.0.7

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 (113) hide show
  1. package/dist/cram-bundle.js +1 -1
  2. package/dist/cramFile/codecs/byteArrayStop.d.ts +2 -2
  3. package/dist/cramFile/file.js +8 -10
  4. package/dist/cramFile/file.js.map +1 -1
  5. package/dist/cramFile/util.js.map +1 -1
  6. package/dist/htscodecs/arith_gen.d.ts +26 -0
  7. package/dist/htscodecs/arith_gen.js +558 -0
  8. package/dist/htscodecs/arith_gen.js.map +1 -0
  9. package/dist/htscodecs/arith_sh.d.ts +16 -0
  10. package/dist/htscodecs/arith_sh.js +128 -0
  11. package/dist/htscodecs/arith_sh.js.map +1 -0
  12. package/dist/htscodecs/byte_model.d.ts +11 -0
  13. package/dist/htscodecs/byte_model.js +113 -0
  14. package/dist/htscodecs/byte_model.js.map +1 -0
  15. package/dist/htscodecs/fqzcomp.d.ts +2 -0
  16. package/dist/htscodecs/fqzcomp.js +744 -0
  17. package/dist/htscodecs/fqzcomp.js.map +1 -0
  18. package/dist/htscodecs/index.d.ts +5 -0
  19. package/dist/htscodecs/index.js +70 -0
  20. package/dist/htscodecs/index.js.map +1 -0
  21. package/dist/htscodecs/iostream.d.ts +26 -0
  22. package/dist/htscodecs/iostream.js +242 -0
  23. package/dist/htscodecs/iostream.js.map +1 -0
  24. package/dist/htscodecs/main_arith_gen.d.ts +1 -0
  25. package/dist/htscodecs/main_arith_gen.js +86 -0
  26. package/dist/htscodecs/main_arith_gen.js.map +1 -0
  27. package/dist/htscodecs/main_fqzcomp.d.ts +1 -0
  28. package/dist/htscodecs/main_fqzcomp.js +112 -0
  29. package/dist/htscodecs/main_fqzcomp.js.map +1 -0
  30. package/dist/htscodecs/main_rans.d.ts +1 -0
  31. package/dist/htscodecs/main_rans.js +83 -0
  32. package/dist/htscodecs/main_rans.js.map +1 -0
  33. package/dist/htscodecs/main_rans4x16.d.ts +1 -0
  34. package/dist/htscodecs/main_rans4x16.js +82 -0
  35. package/dist/htscodecs/main_rans4x16.js.map +1 -0
  36. package/dist/htscodecs/main_tok3.d.ts +1 -0
  37. package/dist/htscodecs/main_tok3.js +84 -0
  38. package/dist/htscodecs/main_tok3.js.map +1 -0
  39. package/dist/htscodecs/rans.d.ts +2 -0
  40. package/dist/htscodecs/rans.js +480 -0
  41. package/dist/htscodecs/rans.js.map +1 -0
  42. package/dist/htscodecs/rans4x16.d.ts +2 -0
  43. package/dist/htscodecs/rans4x16.js +896 -0
  44. package/dist/htscodecs/rans4x16.js.map +1 -0
  45. package/dist/htscodecs/tok3.d.ts +2 -0
  46. package/dist/htscodecs/tok3.js +347 -0
  47. package/dist/htscodecs/tok3.js.map +1 -0
  48. package/esm/cramFile/codecs/byteArrayStop.d.ts +2 -2
  49. package/esm/cramFile/file.js +8 -10
  50. package/esm/cramFile/file.js.map +1 -1
  51. package/esm/cramFile/util.js.map +1 -1
  52. package/esm/htscodecs/arith_gen.d.ts +26 -0
  53. package/esm/htscodecs/arith_gen.js +558 -0
  54. package/esm/htscodecs/arith_gen.js.map +1 -0
  55. package/esm/htscodecs/arith_sh.d.ts +16 -0
  56. package/esm/htscodecs/arith_sh.js +128 -0
  57. package/esm/htscodecs/arith_sh.js.map +1 -0
  58. package/esm/htscodecs/byte_model.d.ts +11 -0
  59. package/esm/htscodecs/byte_model.js +113 -0
  60. package/esm/htscodecs/byte_model.js.map +1 -0
  61. package/esm/htscodecs/fqzcomp.d.ts +2 -0
  62. package/esm/htscodecs/fqzcomp.js +744 -0
  63. package/esm/htscodecs/fqzcomp.js.map +1 -0
  64. package/esm/htscodecs/index.d.ts +5 -0
  65. package/esm/htscodecs/index.js +70 -0
  66. package/esm/htscodecs/index.js.map +1 -0
  67. package/esm/htscodecs/iostream.d.ts +26 -0
  68. package/esm/htscodecs/iostream.js +242 -0
  69. package/esm/htscodecs/iostream.js.map +1 -0
  70. package/esm/htscodecs/main_arith_gen.d.ts +1 -0
  71. package/esm/htscodecs/main_arith_gen.js +86 -0
  72. package/esm/htscodecs/main_arith_gen.js.map +1 -0
  73. package/esm/htscodecs/main_fqzcomp.d.ts +1 -0
  74. package/esm/htscodecs/main_fqzcomp.js +112 -0
  75. package/esm/htscodecs/main_fqzcomp.js.map +1 -0
  76. package/esm/htscodecs/main_rans.d.ts +1 -0
  77. package/esm/htscodecs/main_rans.js +83 -0
  78. package/esm/htscodecs/main_rans.js.map +1 -0
  79. package/esm/htscodecs/main_rans4x16.d.ts +1 -0
  80. package/esm/htscodecs/main_rans4x16.js +82 -0
  81. package/esm/htscodecs/main_rans4x16.js.map +1 -0
  82. package/esm/htscodecs/main_tok3.d.ts +1 -0
  83. package/esm/htscodecs/main_tok3.js +84 -0
  84. package/esm/htscodecs/main_tok3.js.map +1 -0
  85. package/esm/htscodecs/rans.d.ts +2 -0
  86. package/esm/htscodecs/rans.js +480 -0
  87. package/esm/htscodecs/rans.js.map +1 -0
  88. package/esm/htscodecs/rans4x16.d.ts +2 -0
  89. package/esm/htscodecs/rans4x16.js +896 -0
  90. package/esm/htscodecs/rans4x16.js.map +1 -0
  91. package/esm/htscodecs/tok3.d.ts +2 -0
  92. package/esm/htscodecs/tok3.js +347 -0
  93. package/esm/htscodecs/tok3.js.map +1 -0
  94. package/package.json +1 -2
  95. package/src/cramFile/file.ts +8 -10
  96. package/src/cramFile/util.ts +0 -1
  97. package/src/htscodecs/Makefile +142 -0
  98. package/src/htscodecs/README.md +64 -0
  99. package/src/htscodecs/arith_gen.js +607 -0
  100. package/src/htscodecs/arith_sh.js +138 -0
  101. package/src/htscodecs/byte_model.js +126 -0
  102. package/src/htscodecs/fqzcomp.js +834 -0
  103. package/src/htscodecs/index.js +79 -0
  104. package/src/htscodecs/iostream.js +256 -0
  105. package/src/htscodecs/main_arith_gen.js +96 -0
  106. package/src/htscodecs/main_fqzcomp.js +113 -0
  107. package/src/htscodecs/main_rans.js +88 -0
  108. package/src/htscodecs/main_rans4x16.js +87 -0
  109. package/src/htscodecs/main_tok3.js +86 -0
  110. package/src/htscodecs/rans.js +545 -0
  111. package/src/htscodecs/rans4x16.js +1003 -0
  112. package/src/htscodecs/tok3.js +396 -0
  113. package/src/typings/htscodecs.d.ts +0 -6
@@ -0,0 +1,138 @@
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
+ // An arithmetic coder, based on Eugene Shelwien's reimplementation of
35
+ // Michael Schindler range coder.
36
+ //
37
+ // Order-0 byte stream of ~/scratch/data/q40b
38
+ // C: 3.1s decode (approx same vs 32-bit and 64-bit)
39
+ // Arith_sh.js 6.7s decode (32-bit with carries)
40
+ // Arith.js 317.0s decode (64-bit no carries); int64 crippling it.
41
+
42
+ //----------------------------------------------------------------------
43
+ // Arithmetic (range) coder
44
+ module.exports = class RangeCoder {
45
+ constructor(src) {
46
+ this.low = 0
47
+ this.range = 0xffffffff
48
+ this.code = 0
49
+ this.FFnum = 0
50
+ this.carry = 0
51
+ this.cache = 0
52
+ }
53
+
54
+ RangeStartDecode(src) {
55
+ for (var i = 0; i < 5; i++) this.code = (this.code << 8) + src.ReadByte()
56
+ this.code &= 0xffffffff
57
+ this.code >>>= 0 // force to be +ve int
58
+ }
59
+
60
+ RangeGetFrequency(tot_freq) {
61
+ this.range = Math.floor(this.range / tot_freq)
62
+ //return this.code / this.range;
63
+ return Math.floor(this.code / this.range)
64
+
65
+ // Conceptual scenario; return freq only and don't modify range yet
66
+ //return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
67
+ }
68
+
69
+ RangeDecode(src, sym_low, sym_freq, tot_freq) {
70
+ // Conceptually we divide range here, but in practice we cached it earlier
71
+ //this.range = Math.floor(this.range / tot_freq);
72
+
73
+ this.code -= sym_low * this.range
74
+ this.range *= sym_freq
75
+
76
+ while (this.range < 1 << 24) {
77
+ this.range *= 256
78
+ this.code = this.code * 256 + src.ReadByte()
79
+ }
80
+ }
81
+
82
+ RangeShiftLow(dst) {
83
+ // We know range is < (1<<24) as we got here. We already have a
84
+ // cached copy of 8 bits from low. Is this correct, or does it need
85
+ // fixing? Possible scenarios.
86
+ // 1. Low < 0xff000000 thus low+range < 0xffffffff and cache
87
+ // cannot possibly change. Output cache and as many ffs as needed.
88
+ // 2. We already detected an overflow in RangeEncode, setting carry.
89
+ // In this case output cached byte + 1 and any 00s needed.
90
+ // 3. Neither case - range is low but we haven't yet detected if we're
91
+ // XXffffff or XY000000 scenario. Increase counter for ff/00s.
92
+
93
+ if ((this.low < 0xff000000) | this.carry) {
94
+ // cached byte if no overflow, byte+1 otherwise
95
+ dst.WriteByte(this.cache + this.carry)
96
+
97
+ // Flush any tracked FFs (no carry) or 00s (carry).
98
+ while (this.FFnum) {
99
+ dst.WriteByte(this.carry - 1)
100
+ this.FFnum--
101
+ }
102
+
103
+ // Take a copy of top byte ready for next flush
104
+ this.cache = this.low >>> 24
105
+ this.carry = 0
106
+ } else {
107
+ this.FFnum++ // keep track of number of trailing ff/00 bytes to write
108
+ }
109
+ this.low <<= 8
110
+ this.low >>>= 0 // force to be +ve int
111
+ }
112
+
113
+ RangeEncode(dst, sym_low, sym_freq, tot_freq) {
114
+ var old_low = this.low
115
+ this.range = Math.floor(this.range / tot_freq)
116
+ this.low += sym_low * this.range
117
+ this.low >>>= 0 // Truncate to +ve int so we can spot overflow
118
+ this.range *= sym_freq
119
+
120
+ // "low + sym*range < old_low" means we overflow; set carry.
121
+ // NB: can this.low < old_low occur twice before range < (1<<24)?
122
+ // We claim not, but prove it!
123
+ if (this.low < old_low) {
124
+ if (this.carry != 0) console.log('ERROR: Multiple carry')
125
+ this.carry = 1
126
+ }
127
+
128
+ // Renormalise if range gets too small
129
+ while (this.range < 1 << 24) {
130
+ this.range *= 256
131
+ this.RangeShiftLow(dst)
132
+ }
133
+ }
134
+
135
+ RangeFinishEncode(dst) {
136
+ for (var i = 0; i < 5; i++) this.RangeShiftLow(dst)
137
+ }
138
+ }
@@ -0,0 +1,126 @@
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
+ // An adaptive probability model for encoding and decoding of symbols
35
+ // within a given alphabet, using the range coder to get/put the
36
+ // compressed data.
37
+
38
+ const MAX_FREQ = (1 << 16) - 17
39
+ const STEP = 16
40
+
41
+ module.exports = class ByteModel {
42
+ constructor(max_sym = 256) {
43
+ this.total_freq = max_sym
44
+ this.max_sym = max_sym - 1
45
+ this.S = new Array()
46
+ this.F = new Array()
47
+
48
+ for (var i = 0; i <= this.max_sym; i++) {
49
+ this.S[i] = i
50
+ this.F[i] = 1
51
+ }
52
+ }
53
+
54
+ ModelDecode(src, rc) {
55
+ // Find symbol
56
+ var freq = rc.RangeGetFrequency(this.total_freq)
57
+
58
+ // 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
+
63
+ // for (var acc = 0; (acc += this.F[x]) <= freq; x++)
64
+ // ;
65
+ // acc -= this.F[x];
66
+
67
+ // Update range coder
68
+ rc.RangeDecode(src, acc, this.F[x], this.total_freq)
69
+
70
+ // Update model
71
+ this.F[x] += STEP
72
+ this.total_freq += STEP
73
+ if (this.total_freq > MAX_FREQ) this.ModelRenormalise()
74
+
75
+ // Keep symbols approximately frequency sorted
76
+ var sym = this.S[x]
77
+ if (x > 0 && this.F[x] > this.F[x - 1]) {
78
+ var tmp = this.F[x]
79
+ this.F[x] = this.F[x - 1]
80
+ this.F[x - 1] = tmp
81
+
82
+ tmp = this.S[x]
83
+ this.S[x] = this.S[x - 1]
84
+ this.S[x - 1] = tmp
85
+ }
86
+
87
+ return sym
88
+ }
89
+
90
+ ModelRenormalise() {
91
+ // Halve all the frequencies, being careful not to hit zero
92
+ this.total_freq = 0
93
+ for (var i = 0; i <= this.max_sym; i++) {
94
+ this.F[i] -= Math.floor(this.F[i] / 2)
95
+ this.total_freq += this.F[i]
96
+ }
97
+ }
98
+
99
+ ModelEncode(dst, rc, sym) {
100
+ // Find cumulative frequency
101
+ var acc = 0
102
+ for (var x = 0; this.S[x] != sym; x++) acc += this.F[x]
103
+
104
+ // Encode
105
+ rc.RangeEncode(dst, acc, this.F[x], this.total_freq)
106
+
107
+ // Update model
108
+ this.F[x] += STEP
109
+ this.total_freq += STEP
110
+ if (this.total_freq > MAX_FREQ)
111
+ // FIXME x2
112
+ this.ModelRenormalise()
113
+
114
+ // Keep symbols approximately frequency sorted
115
+ var sym = this.S[x]
116
+ if (x > 0 && this.F[x] > this.F[x - 1]) {
117
+ var tmp = this.F[x]
118
+ this.F[x] = this.F[x - 1]
119
+ this.F[x - 1] = tmp
120
+
121
+ tmp = this.S[x]
122
+ this.S[x] = this.S[x - 1]
123
+ this.S[x - 1] = tmp
124
+ }
125
+ }
126
+ }