Skip to content

Commit

Permalink
Fix an NPE in CRAMRecordReadFeatures.restoreReadBases (#1655)
Browse files Browse the repository at this point in the history
* Fix an NPE in CRAMRecordReadFeatures.restoreReadBases() when reading crams with embedded references.
* Fix for igvteam/igv#1286
  • Loading branch information
lbergelson authored Feb 21, 2023
1 parent b0a5165 commit 8f8d567
Show file tree
Hide file tree
Showing 5 changed files with 1,357 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ public static byte[] restoreReadBases(
}

private static byte getByteOrDefault(final byte[] array, final int pos, final byte outOfBoundsValue) {
if (array == null) {
return outOfBoundsValue;
}
return pos >= array.length ?
outOfBoundsValue :
array[pos];
Expand Down
32 changes: 30 additions & 2 deletions src/test/java/htsjdk/samtools/CRAMReferencelessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CRAMReferencelessTest extends HtsjdkTest {
private static final File TEST_DATA_DIR = new File("src/test/resources/htsjdk/samtools/cram");

@Test
private void testReadCRAMWithEmbeddedReference() throws IOException {
public void testReadCRAMWithEmbeddedReference() throws IOException {
try (final SamReader cramReader = SamReaderFactory.makeDefault()
.validationStringency(ValidationStringency.LENIENT)
.referenceSource(new ReferenceSource(new File(TEST_DATA_DIR, "human_g1k_v37.20.21.1-100.fasta")))
Expand All @@ -24,16 +24,44 @@ private void testReadCRAMWithEmbeddedReference() throws IOException {
.open(new File(TEST_DATA_DIR, "referenceEmbedded.NA12878.20.21.1-100.100-SeqsPerSlice.500-unMapped.cram"))) {
final Iterator<SAMRecord> cramIterator = cramReader.iterator();
final Iterator<SAMRecord> cramEmbeddedIterator = cramReaderEmbedded.iterator();
int count = 0;
while (cramIterator.hasNext() && cramEmbeddedIterator.hasNext()) {
count++;
final SAMRecord cramRecord = cramIterator.next();
final SAMRecord cramRecordEmbedded = cramEmbeddedIterator.next();
Assert.assertEquals(cramRecordEmbedded, cramRecord);
}
Assert.assertTrue( count >0, "Expected reads but there were none.");
}
}

// test for https://github.com/igvteam/igv/issues/1286
// cram was generated subset from an example cram using samtools and -O no_ref
@Test
private void testReadCRAMNoReferenceRequired() throws IOException {
public void testForNPE() throws IOException {
try (final SamReader cramReader = SamReaderFactory.makeDefault()
.validationStringency(ValidationStringency.LENIENT)
.open(new File(TEST_DATA_DIR, "testIGV1286.sam"));
final SamReader cramReaderEmbedded = SamReaderFactory.makeDefault()
.validationStringency(ValidationStringency.LENIENT)
.open(new File(TEST_DATA_DIR, "testIGV1286.cram"))) {
final Iterator<SAMRecord> cramIterator = cramReader.iterator();
final Iterator<SAMRecord> cramEmbeddedIterator = cramReaderEmbedded.iterator();
int count = 0;
while (cramIterator.hasNext() && cramEmbeddedIterator.hasNext()) {
count++;
final SAMRecord cramRecord = cramIterator.next();
final SAMRecord cramRecordEmbedded = cramEmbeddedIterator.next();
Assert.assertEquals(cramRecordEmbedded, cramRecord);
}
Assert.assertEquals(count, 2);
Assert.assertFalse(cramIterator.hasNext());
Assert.assertFalse(cramEmbeddedIterator.hasNext());
}
}

@Test
public void testReadCRAMNoReferenceRequired() throws IOException {
// test reading a cram with no reference compression (RR=false in compression header)
try (final SamReader samReader = SamReaderFactory.makeDefault()
.validationStringency(ValidationStringency.LENIENT)
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 8f8d567

Please sign in to comment.