Skip to content

Commit

Permalink
Fix crash on PF_READS == 0 for AlignmentSummaryMetricsCollector (#1871)
Browse files Browse the repository at this point in the history
* Create a (mostly) empty metrics file instead of giving an unhelpful stacktrace if PF_READS == 0.

* Added check for the PF_READS == 0 case.

* Added a corresponding test file.

* Use a PicardException with an informative message when PF_READS == 0 and TOTAL_READS > 0.
  • Loading branch information
kockan committed May 10, 2023
1 parent 62ec81c commit 78892bf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import htsjdk.samtools.util.CoordMath;
import htsjdk.samtools.util.Histogram;
import htsjdk.samtools.util.SequenceUtil;
import picard.PicardException;
import picard.metrics.PerUnitMetricCollector;
import picard.metrics.SAMRecordAndReference;
import picard.metrics.SAMRecordAndReferenceMultiLevelCollector;
Expand Down Expand Up @@ -261,7 +262,7 @@ public void acceptRecord(final SAMRecordAndReference samRecordAndReference) {
@Override
public void finish() {
//summarize read data
if (metrics.TOTAL_READS > 0) {
if (metrics.PF_READS > 0) {
metrics.PCT_PF_READS = (double) metrics.PF_READS / (double) metrics.TOTAL_READS;
metrics.PCT_ADAPTER = adapterReads / (double) metrics.PF_READS;
metrics.MEAN_READ_LENGTH = readLengthHistogram.getMean();
Expand Down Expand Up @@ -298,6 +299,10 @@ public void finish() {

metrics.PF_HQ_MEDIAN_MISMATCHES = hqMismatchHistogram.getMedian();
}
} else {
if (metrics.TOTAL_READS > 0) {
throw new PicardException("Input file contains no PF_READS.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import picard.PicardException;
import picard.cmdline.CommandLineProgramTest;
import picard.util.TestNGUtil;

Expand Down Expand Up @@ -771,4 +772,37 @@ public void testReadLengthHistogram(final boolean plotChart) throws IOException
}
}
}

@Test(expectedExceptions = PicardException.class)
public void testNoPFReads() throws IOException {
final File input = new File(TEST_DATA_DIR, "null.sam");
final File outfile = getTempOutputFile("test", ".txt");
final String[] args = new String[]{
"INPUT=" + input.getAbsolutePath(),
"OUTPUT=" + outfile.getAbsolutePath(),
};
Assert.assertEquals(runPicardCommandLine(args), 0);

final MetricsFile<AlignmentSummaryMetrics, Comparable<?>> output = new MetricsFile<>();
try (FileReader reader = new FileReader(outfile)) {
output.read(reader);
}

Assert.assertEquals(output.getMetrics().size(), 1);
for (final AlignmentSummaryMetrics metrics : output.getMetrics()) {
Assert.assertEquals(metrics.MEAN_READ_LENGTH, 0.0);
Assert.assertEquals(metrics.TOTAL_READS, 3);
Assert.assertEquals(metrics.PF_READS, 0);
Assert.assertEquals(metrics.PF_NOISE_READS, 0);
Assert.assertEquals(metrics.PF_HQ_ALIGNED_READS, 0);
Assert.assertEquals(metrics.PF_HQ_ALIGNED_Q20_BASES, 0);
Assert.assertEquals(metrics.PF_HQ_MEDIAN_MISMATCHES, 0.0);
Assert.assertEquals(metrics.PF_READS_ALIGNED, 0);
Assert.assertEquals(metrics.PF_READS_IMPROPER_PAIRS, 0);
Assert.assertEquals(metrics.PCT_PF_READS_IMPROPER_PAIRS, 0.0);
Assert.assertEquals(metrics.PF_ALIGNED_BASES, 0);
Assert.assertEquals(metrics.PF_MISMATCH_RATE, 0.0);
Assert.assertEquals(metrics.BAD_CYCLES, 0);
}
}
}
5 changes: 5 additions & 0 deletions testdata/picard/sam/AlignmentSummaryMetrics/null.sam
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@HD VN:1.6 GO:none SO:queryname
@RG ID:test SM:test_sample LB:test_lib PL:ILLUMINA PU:my_platform CN:BI DT:2023-01-01T00:00:00-0500 DS:description
aln1 516 * 0 0 * * 0 0 AGCTACTG 99999999 RG:Z:test
aln2 516 * 0 0 * * 0 0 GTCAGTCA 99999999 RG:Z:test
aln3 516 * 0 0 * * 0 0 CCTTGGAA 99999999 RG:Z:test

0 comments on commit 78892bf

Please sign in to comment.