Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero len array #1674

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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