Skip to content

Commit

Permalink
Make the CRAM MD5 failure message more user friendly. (#1607)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnbroad authored Apr 25, 2022
1 parent a38c78d commit 6507249
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/main/java/htsjdk/samtools/cram/structure/Slice.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,7 @@ public void normalizeCRAMRecords(final List<CRAMCompressionRecord> cramCompressi
getAlignmentContext().getReferenceContext().getReferenceSequenceID()
);

if (!referenceMD5IsValid(referenceBases)) {
throw new CRAMException(String.format(
"Reference sequence MD5 mismatch for slice: %s, expected MD5 %s",
getAlignmentContext(),
String.format("%032x", new BigInteger(1, getReferenceMD5()))));
}
validateReferenceBases(referenceBases);
}
} else {
// RR = false might mean that no reference compression was used, or that an embedded reference
Expand Down Expand Up @@ -690,19 +685,21 @@ private void validateAlignmentSpanForReference(final byte[] referenceBases) {
}

//VisibleForTesting
boolean referenceMD5IsValid(final byte[] referenceBases) {
void validateReferenceBases(final byte[] referenceBases) {
if (alignmentContext.getReferenceContext().isMappedSingleRef() && compressionHeader.isReferenceRequired()) {
validateAlignmentSpanForReference(referenceBases);
if (!referenceMD5IsValid(
referenceBases,
alignmentContext.getAlignmentStart(),
alignmentContext.getAlignmentSpan(),
referenceMD5)) {
throw new CRAMException(String.format("Reference MD5 failed to validate against %s",
String.format("%032x", new BigInteger(1, referenceMD5))));
throw new CRAMException(
String.format(
"The MD5 for the reference failed to validate against the expected value %032x. %s.",
new BigInteger(1, referenceMD5),
"This indicates that the supplied reference is not the one originally used to create the CRAM."));
}
}
return true;
}

private static boolean referenceMD5IsValid(
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/htsjdk/samtools/cram/structure/SliceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void testValidateReferenceMD5Unmapped() {
final Slice slice = container.getSlices().get(0);
Assert.assertEquals(slice.getAlignmentContext().getReferenceContext(), ReferenceContext.UNMAPPED_UNPLACED_CONTEXT);

Assert.assertTrue(slice.referenceMD5IsValid(null));
Assert.assertTrue(slice.referenceMD5IsValid(new byte[0]));
Assert.assertTrue(slice.referenceMD5IsValid(new byte[1024]));
slice.validateReferenceBases(null);
slice.validateReferenceBases(new byte[0]);
slice.validateReferenceBases(new byte[1024]);
}

@Test
Expand Down

0 comments on commit 6507249

Please sign in to comment.