From 3e7d61e4d25b692543a97252f82a361c70bf841d Mon Sep 17 00:00:00 2001 From: Yossi Farjoun Date: Sun, 6 Dec 2015 13:29:03 -0500 Subject: [PATCH] investigate issue 316 in htsjdk. added a test. --- src/java/htsjdk/samtools/SAMRecord.java | 9 ++++++++- .../htsjdk/samtools/SAMRecordUnitTest.java | 20 ++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/java/htsjdk/samtools/SAMRecord.java b/src/java/htsjdk/samtools/SAMRecord.java index cfa922f74a..9826fe8c2c 100644 --- a/src/java/htsjdk/samtools/SAMRecord.java +++ b/src/java/htsjdk/samtools/SAMRecord.java @@ -32,6 +32,7 @@ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -1712,7 +1713,13 @@ public List validateCigar(final long recordNumber) { List ret = null; if (null != getHeader() && getValidationStringency() != ValidationStringency.SILENT && !this.getReadUnmappedFlag()) { - ret = SAMUtils.validateCigar(this, getCigar(), getReferenceIndex(), getAlignmentBlocks(), recordNumber, "Read CIGAR"); + try { + //make sure that the cashed version is good + //wrapped in a try to catch an un-parsable string + return SAMUtils.validateCigar(this, getCigar(), getReferenceIndex(), getAlignmentBlocks(), recordNumber, "Read CIGAR"); + } catch( final IllegalArgumentException e){ + return Collections.singletonList(new SAMValidationError(SAMValidationError.Type.INVALID_CIGAR,e.getMessage(),getReadName(),recordNumber)); + } } return ret; } diff --git a/src/tests/java/htsjdk/samtools/SAMRecordUnitTest.java b/src/tests/java/htsjdk/samtools/SAMRecordUnitTest.java index a3c3e68086..380e77010d 100644 --- a/src/tests/java/htsjdk/samtools/SAMRecordUnitTest.java +++ b/src/tests/java/htsjdk/samtools/SAMRecordUnitTest.java @@ -31,6 +31,7 @@ import org.testng.annotations.Test; import java.io.*; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -456,7 +457,7 @@ record = new SAMRecord(header); Assert.fail("Unexpected exception", e); } - record.setAttribute(tag, (long)Integer.MIN_VALUE-1L); + record.setAttribute(tag, (long) Integer.MIN_VALUE - 1L); } @Test(expectedExceptions = SAMException.class) @@ -777,6 +778,23 @@ public void testNullHeaderSerialization(final File inputFile) throws Exception { Assert.assertEquals(deserializedSAMRecord, initialSAMRecord, "Deserialized SAMRecord not equal to original SAMRecord"); } + + @Test + public void testValidateNonsenseCigar(){ + // Create nonsense record + SAMRecord rec = createTestRecordHelper(); + rec.setCigarString("nonsense"); + + //The default validationStringency of a sam record is SILENT. + rec.setValidationStringency(ValidationStringency.STRICT); + // Validate record + List err = rec.validateCigar(-1); + + Assert.assertNotNull(err); + Assert.assertEquals(err.size(), 1); + Assert.assertEquals(err.get(0).getType(), SAMValidationError.Type.INVALID_CIGAR); + } + @Test public void testNullHeaderRecordValidation() { final SAMRecord sam = createTestRecordHelper();