Skip to content

Commit

Permalink
investigate issue 316 in htsjdk. added a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
yfarjoun committed Dec 16, 2015
1 parent 2fa3f62 commit 3e7d61e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/java/htsjdk/samtools/SAMRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1712,7 +1713,13 @@ public List<SAMValidationError> validateCigar(final long recordNumber) {
List<SAMValidationError> 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;
}
Expand Down
20 changes: 19 additions & 1 deletion src/tests/java/htsjdk/samtools/SAMRecordUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.testng.annotations.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<SAMValidationError> 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();
Expand Down

0 comments on commit 3e7d61e

Please sign in to comment.