Skip to content

Commit

Permalink
Zero len array (#1674)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
gileshall and jmarshall authored Aug 3, 2023
1 parent 44817ee commit d486d53
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
3 changes: 0 additions & 3 deletions src/main/java/htsjdk/samtools/SAMRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
35 changes: 0 additions & 35 deletions src/main/java/htsjdk/samtools/cram/structure/Slice.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/htsjdk/samtools/SAMRecordUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit d486d53

Please sign in to comment.