From 4ff5190208161b94ff585c3cdf0a7849de08484d Mon Sep 17 00:00:00 2001 From: jmthibault79 Date: Mon, 3 Dec 2018 12:48:44 -0500 Subject: [PATCH] Relax Beta Encoding requirement to allow 0 bits (#1233) * allow BetaIntegerEncoding to be specified with 0 bits. This was disabled in #1199, as a probable error state, but it turns out that it's used in some crams created with htslib. --- .../samtools/cram/encoding/core/BetaIntegerEncoding.java | 4 ++-- .../samtools/cram/encoding/core/BetaIntegerCodecTest.java | 5 +++++ .../samtools/cram/encoding/core/BetaIntegerEncodingTest.java | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncoding.java b/src/main/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncoding.java index c1727daa0c..afe334f52e 100644 --- a/src/main/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncoding.java +++ b/src/main/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncoding.java @@ -36,8 +36,8 @@ public class BetaIntegerEncoding extends CRAMEncoding { public BetaIntegerEncoding(final int offset, final int bitsPerValue) { super(EncodingID.BETA); - if (bitsPerValue <= 0) { - throw new IllegalArgumentException("Number of bits per value must be positive"); + if (bitsPerValue < 0) { + throw new IllegalArgumentException("Number of bits per value must not be negative"); } else if (bitsPerValue > 32) { throw new IllegalArgumentException("Number of bits per value must be 32 or lower"); } diff --git a/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerCodecTest.java b/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerCodecTest.java index 229dc7d65d..5ed94859e0 100644 --- a/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerCodecTest.java +++ b/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerCodecTest.java @@ -47,6 +47,8 @@ public Object[][] basicTestData() { return new Object[][] { {8, -100, new int[]{100, 101, 102, (1<<8) + 98, (1<<8) + 99}}, {4, 10015, new int[]{-10015, -10014, -10001, -10000}}, + {0, 0, new int[]{0, 0, 0}}, + {0, 100, new int[]{-100, -100}}, }; } @@ -62,6 +64,7 @@ public Object[][] basicTestNoOffsetData() { return new Object[][] { {8, new int[]{0, 1, 2, 100, (1 << 8) - 2, (1 << 8) - 1}}, {16, new int[]{0, 1, 255, (1 << 16) - 2, (1 << 16) - 1}}, + {0, new int[]{0, 0, 0}}, }; } @@ -77,6 +80,7 @@ public Object[][] overflowData() { // tuples of bitsPerValue and offsets + values which are too big to store return new Object[][] { // first with zero offset + {0, 0, 1}, {1, 0, (1 << 1)}, {2, 0, (1 << 2)}, {4, 0, (1 << 4)}, @@ -84,6 +88,7 @@ public Object[][] overflowData() { {16, 0, (1 << 16)}, // adding offset of 1 will put it over + {0, 1, 0}, {1, 1, (1 << 1) - 1}, {2, 1, (1 << 2) - 1}, {4, 1, (1 << 4) - 1}, diff --git a/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncodingTest.java b/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncodingTest.java index 60f34f0e1e..a821e784bd 100644 --- a/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncodingTest.java +++ b/src/test/java/htsjdk/samtools/cram/encoding/core/BetaIntegerEncodingTest.java @@ -10,6 +10,7 @@ public class BetaIntegerEncodingTest extends HtsjdkTest { public Object[][] testData() { return new Object[][] { // positive values below the ITF8 single-byte limit (128) are encoded as-is + {0, 0, new byte[] { 0, 0 }}, {0, 8, new byte[] { 0, 8 }}, {127, 32, new byte[] { 127, 32 }}, @@ -32,12 +33,11 @@ public void paramsTest(final int offset, final int bitLength, final byte[] expec } - // sanity checks for bitsPerValue. Must be > 0 and <= 32 + // sanity checks for bitsPerValue. Must be between 0 and 32, inclusive @DataProvider(name = "bitsPerValue") public Object[][] bitsPerValueData() { return new Object[][] { - {0}, {-1}, {33} };