Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on PF_READS == 0 for AlignmentSummaryMetricsCollector #1871

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
rickymagner marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -768,4 +769,37 @@ public void testReadLengthHistogram(final boolean plotChart) throws IOException
}
}
}

@Test(expectedExceptions = PicardException.class)
public void testNoPFReads() throws IOException {
rickymagner marked this conversation as resolved.
Show resolved Hide resolved
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