Skip to content

Commit 68189f3

Browse files
author
Eric Banks
authored
Adds option to validator to skip mate validation (#1025)
Adds option to validator to skip mate validation (although we still validate mate cigars)
1 parent 060047e commit 68189f3

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/main/java/htsjdk/samtools/SamFileValidator.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class SamFileValidator {
9494
private SAMSortOrderChecker orderChecker;
9595
private Set<Type> errorsToIgnore;
9696
private boolean ignoreWarnings;
97+
private boolean skipMateValidation;
9798
private boolean bisulfiteSequenced;
9899
private IndexValidationStringency indexValidationStringency;
99100
private boolean sequenceDictionaryEmptyAndNoWarningEmitted;
@@ -114,6 +115,7 @@ public SamFileValidator(final PrintWriter out, final int maxTempFiles) {
114115
this.errorsToIgnore = EnumSet.noneOf(Type.class);
115116
this.verbose = false;
116117
this.ignoreWarnings = false;
118+
this.skipMateValidation = false;
117119
this.bisulfiteSequenced = false;
118120
this.sequenceDictionaryEmptyAndNoWarningEmitted = false;
119121
this.numWarnings = 0;
@@ -137,6 +139,23 @@ public void setIgnoreWarnings(final boolean ignoreWarnings) {
137139
this.ignoreWarnings = ignoreWarnings;
138140
}
139141

142+
/**
143+
* Sets whether or not we should run mate validation beyond the mate cigar check, which
144+
* is useful in extreme edge cases that would require a lot of memory to do the validation.
145+
*
146+
* @param skipMateValidation should this tool skip mate validation
147+
*/
148+
public void setSkipMateValidation(final boolean skipMateValidation) {
149+
this.skipMateValidation = skipMateValidation;
150+
}
151+
152+
/**
153+
* @return true if the validator will skip mate validation, otherwise false
154+
*/
155+
public boolean getSkipMateValidation() {
156+
return skipMateValidation;
157+
}
158+
140159
/**
141160
* Outputs validation summary report to out.
142161
*
@@ -242,7 +261,6 @@ private void validateSamFile(final SamReader samReader, final PrintWriter out) {
242261
}
243262
}
244263

245-
246264
/**
247265
* Report on reads marked as paired, for which the mate was not found.
248266
*/
@@ -508,6 +526,10 @@ private void validateMateFields(final SAMRecord record, final long recordNumber)
508526
}
509527
validateMateCigar(record, recordNumber);
510528

529+
if (skipMateValidation) {
530+
return;
531+
}
532+
511533
final PairEndInfo pairEndInfo = pairEndInfoByName.remove(record.getReferenceIndex(), record.getReadName());
512534
if (pairEndInfo == null) {
513535
pairEndInfoByName.put(record.getMateReferenceIndex(), record.getReadName(), new PairEndInfo(record, recordNumber));

src/test/java/htsjdk/samtools/ValidateSamFileTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,29 @@ public void testPairedRecords() throws IOException {
177177
Assert.assertEquals(results.get(SAMValidationError.Type.INVALID_UNALIGNED_MATE_START.getHistogramString()).getValue(), 1.0);
178178
}
179179

180+
@Test
181+
public void testSkipMateValidation() throws IOException {
182+
final SAMRecordSetBuilder samBuilder = new SAMRecordSetBuilder();
183+
184+
for (int i = 0; i < 5; i++) {
185+
samBuilder.addPair(String.valueOf(i), i, i, i + 100);
186+
}
187+
final Iterator<SAMRecord> records = samBuilder.iterator();
188+
records.next().setMateReferenceName("*");
189+
records.next().setMateAlignmentStart(Integer.MAX_VALUE);
190+
records.next().setMateAlignmentStart(records.next().getAlignmentStart() + 1);
191+
records.next().setMateNegativeStrandFlag(!records.next().getReadNegativeStrandFlag());
192+
records.next().setMateReferenceIndex(records.next().getReferenceIndex() + 1);
193+
records.next().setMateUnmappedFlag(!records.next().getReadUnmappedFlag());
194+
195+
final Histogram<String> results = executeValidationWithErrorIgnoring(samBuilder.getSamReader(), null, IndexValidationStringency.EXHAUSTIVE, Collections.EMPTY_LIST, true);
196+
197+
Assert.assertNull(results.get(SAMValidationError.Type.MISMATCH_FLAG_MATE_NEG_STRAND.getHistogramString()));
198+
Assert.assertNull(results.get(SAMValidationError.Type.MISMATCH_FLAG_MATE_UNMAPPED.getHistogramString()));
199+
Assert.assertNull(results.get(SAMValidationError.Type.MISMATCH_MATE_ALIGNMENT_START.getHistogramString()));
200+
Assert.assertNull(results.get(SAMValidationError.Type.MISMATCH_MATE_REF_INDEX.getHistogramString()));
201+
}
202+
180203
@Test(dataProvider = "missingMateTestCases")
181204
public void testMissingMate(final SAMFileHeader.SortOrder sortOrder) throws IOException {
182205
final SAMRecordSetBuilder samBuilder = new SAMRecordSetBuilder(true, sortOrder);
@@ -584,17 +607,21 @@ public void validateBamFileTerminationTest(final File file, final SAMValidationE
584607

585608
private Histogram<String> executeValidation(final SamReader samReader, final ReferenceSequenceFile reference,
586609
final IndexValidationStringency stringency) throws IOException {
587-
return executeValidationWithErrorIgnoring(samReader, reference, stringency, Collections.EMPTY_LIST);
610+
return executeValidationWithErrorIgnoring(samReader, reference, stringency, Collections.EMPTY_LIST, false);
588611
}
589612

590-
private Histogram<String> executeValidationWithErrorIgnoring(final SamReader samReader, final ReferenceSequenceFile reference,
591-
final IndexValidationStringency stringency, Collection<SAMValidationError.Type> ignoringError) throws IOException {
613+
private Histogram<String> executeValidationWithErrorIgnoring(final SamReader samReader,
614+
final ReferenceSequenceFile reference,
615+
final IndexValidationStringency stringency,
616+
final Collection<SAMValidationError.Type> ignoringError,
617+
final boolean skipMateValidation) throws IOException {
592618
final File outFile = File.createTempFile("validation", ".txt");
593619
outFile.deleteOnExit();
594620

595621
final PrintWriter out = new PrintWriter(outFile);
596622
final SamFileValidator samFileValidator = new SamFileValidator(out, 8000);
597623
samFileValidator.setIndexValidationStringency(stringency).setErrorsToIgnore(ignoringError);
624+
samFileValidator.setSkipMateValidation(skipMateValidation);
598625
samFileValidator.validateSamFileSummary(samReader, reference);
599626

600627
final LineNumberReader reader = new LineNumberReader(new FileReader(outFile));

0 commit comments

Comments
 (0)