From 2c97cce4a12ce72af6d4582700437328a6fb397b Mon Sep 17 00:00:00 2001 From: meganshand Date: Mon, 7 Jan 2019 14:15:15 -0500 Subject: [PATCH] IntervalKeepPairFilter now filters out single ended reads (#1252) * fix an exception when seeing unpaired reads --- .../samtools/filter/IntervalKeepPairFilter.java | 6 +++--- .../filter/IntervalKeepPairFilterTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/htsjdk/samtools/filter/IntervalKeepPairFilter.java b/src/main/java/htsjdk/samtools/filter/IntervalKeepPairFilter.java index c4e01aae2d..9a6d564020 100644 --- a/src/main/java/htsjdk/samtools/filter/IntervalKeepPairFilter.java +++ b/src/main/java/htsjdk/samtools/filter/IntervalKeepPairFilter.java @@ -58,8 +58,8 @@ public IntervalKeepPairFilter(final List intervals) { * overlaps the current interval using overlap detector. If yes, return * false -> don't filter it out. * - * If a read is secondary or supplementary, filter read out. Use - * {@link IntervalFilter} if you want to keep these reads, but NOTE: the + * If a read is secondary, supplementary, or single ended, filter read out. + * Use {@link IntervalFilter} if you want to keep these reads, but NOTE: the * resulting bam may not be valid. * * @param record the SAMRecord to evaluate @@ -67,7 +67,7 @@ public IntervalKeepPairFilter(final List intervals) { */ @Override public boolean filterOut(final SAMRecord record) { - if (record.isSecondaryOrSupplementary()) { + if (record.isSecondaryOrSupplementary() || !record.getReadPairedFlag()) { return true; } diff --git a/src/test/java/htsjdk/samtools/filter/IntervalKeepPairFilterTest.java b/src/test/java/htsjdk/samtools/filter/IntervalKeepPairFilterTest.java index 59e60d03b9..187313d0a0 100644 --- a/src/test/java/htsjdk/samtools/filter/IntervalKeepPairFilterTest.java +++ b/src/test/java/htsjdk/samtools/filter/IntervalKeepPairFilterTest.java @@ -48,6 +48,8 @@ public void setUp() { builder.addFrag("mapped_pair_chr1", 0, 1, false, false, "151M", null, -1, true, false); // Supplementary alignment are never kept by the interval filter. builder.addFrag("mapped_pair_chr1", 0, 1, false, false, "151M", null, -1, false, true); + // Single ended read should never be kept by the interval filter. + builder.addFrag("single_ended", 0, 1, false); } @Test(dataProvider = "testData") @@ -96,6 +98,21 @@ public void testNotPrimaryReads() { Assert.assertFalse(notPrimary); } + @Test + public void testSingleEndedReads() { + final List intervalList = new ArrayList<>(); + final Interval interval1 = new Interval("chr1", 1, 999); + intervalList.add(interval1); + + final IntervalKeepPairFilter filter = new IntervalKeepPairFilter(intervalList); + + boolean singleEnded = StreamSupport.stream(builder.spliterator(), false) + .filter(rec -> !filter.filterOut(rec)) + .anyMatch(rec -> rec.getReadName().equals("single_ended")); + + Assert.assertFalse(singleEnded); + } + @DataProvider(name = "testData") private Object[][] testData() { Interval interval = new Interval("chr1", 1, 999);