From d486d53aac2cd52ac53d1c633ac89789871815a8 Mon Sep 17 00:00:00 2001 From: Giles Hall Date: Thu, 3 Aug 2023 14:32:46 -0400 Subject: [PATCH] Zero len array (#1674) * Remove setUnsignedArrayAttribute zero-length-array check This function was omitted from PR #1194. Zero-length 'B' arrays are valid, so remove this check too and add a test case. * Remove Slice setAttribute zero-length-array check Slices' optional tags follow the rules for BAM tagged fields, so zero-length 'B' arrays are valid. Slice tags are currently unused, so the question is somewhat moot and there are no test cases. * removed unused setAttribute methods --------- Co-authored-by: John Marshall --- src/main/java/htsjdk/samtools/SAMRecord.java | 3 -- .../htsjdk/samtools/cram/structure/Slice.java | 35 ------------------- .../htsjdk/samtools/SAMRecordUnitTest.java | 13 +++++++ 3 files changed, 13 insertions(+), 38 deletions(-) diff --git a/src/main/java/htsjdk/samtools/SAMRecord.java b/src/main/java/htsjdk/samtools/SAMRecord.java index 799488b091..dd15248fc7 100644 --- a/src/main/java/htsjdk/samtools/SAMRecord.java +++ b/src/main/java/htsjdk/samtools/SAMRecord.java @@ -1541,9 +1541,6 @@ public void setUnsignedArrayAttribute(final String tag, final Object value) { if (!value.getClass().isArray()) { throw new IllegalArgumentException("Non-array passed to setUnsignedArrayAttribute for tag " + tag); } - if (Array.getLength(value) == 0) { - throw new IllegalArgumentException("Empty array passed to setUnsignedArrayAttribute for tag " + tag); - } setAttribute(SAMTag.makeBinaryTag(tag), value, true); } diff --git a/src/main/java/htsjdk/samtools/cram/structure/Slice.java b/src/main/java/htsjdk/samtools/cram/structure/Slice.java index ae569d0836..2287295e86 100644 --- a/src/main/java/htsjdk/samtools/cram/structure/Slice.java +++ b/src/main/java/htsjdk/samtools/cram/structure/Slice.java @@ -735,41 +735,6 @@ public void setReferenceMD5(final CRAMReferenceRegion cramReferenceRegion) { } } - /** - * Hijacking attributes-related methods from SAMRecord: - */ - - /** - * Set a value for the tag. - * @param tag tag ID as a short integer as returned by {@link SAMTag#makeBinaryTag(String)} - * @param value tag value - */ - public void setAttribute(final String tag, final Object value) { - if (value != null && value.getClass().isArray() && Array.getLength(value) == 0) { - throw new IllegalArgumentException("Empty value passed for tag " + tag); - } - setAttribute(SAMTag.makeBinaryTag(tag), value); - } - - void setAttribute(final short tag, final Object value) { - setAttribute(tag, value, false); - } - - void setAttribute(final short tag, final Object value, final boolean isUnsignedArray) { - if (value == null) { - if (this.sliceTags != null) this.sliceTags = this.sliceTags.remove(tag); - } else { - final SAMBinaryTagAndValue tmp; - if (!isUnsignedArray) { - tmp = new SAMBinaryTagAndValue(tag, value); - } else { - tmp = new SAMBinaryTagAndUnsignedArrayValue(tag, value); - } - if (this.sliceTags == null) this.sliceTags = tmp; - else this.sliceTags = this.sliceTags.insert(tmp); - } - } - /** * Uses a Multiple Reference Slice Alignment Reader to determine the reference spans of a MULTI_REF Slice. * Used for creating CRAI/BAI index entries. diff --git a/src/test/java/htsjdk/samtools/SAMRecordUnitTest.java b/src/test/java/htsjdk/samtools/SAMRecordUnitTest.java index 349d50a9c1..e68ef70a93 100644 --- a/src/test/java/htsjdk/samtools/SAMRecordUnitTest.java +++ b/src/test/java/htsjdk/samtools/SAMRecordUnitTest.java @@ -1124,6 +1124,19 @@ public void test_setAttribute_empty_array() { Assert.assertNull(record.getStringAttribute(ARRAY_TAG)); } + @Test + public void test_setUnsignedArrayAttribute_empty_array() { + final SAMFileHeader header = new SAMFileHeader(); + final SAMRecord record = new SAMRecord(header); + Assert.assertNull(record.getStringAttribute(ARRAY_TAG)); + record.setUnsignedArrayAttribute(ARRAY_TAG, new int[0]); + Assert.assertNotNull(record.getUnsignedIntArrayAttribute(ARRAY_TAG)); + Assert.assertEquals(record.getUnsignedIntArrayAttribute(ARRAY_TAG), new int[0]); + Assert.assertEquals(record.getAttribute(ARRAY_TAG), new char[0]); + record.setAttribute(ARRAY_TAG, null); + Assert.assertNull(record.getStringAttribute(ARRAY_TAG)); + } + private static Object[][] getEmptyArrays() { return new Object[][]{ {new int[0], int[].class},