-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from samtools/mds_add_char_parsing
HTSJDK changes needed for CollectSequencingArtifactMetrics in Picard
- Loading branch information
Showing
12 changed files
with
320 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package htsjdk.samtools.filter; | ||
|
||
import htsjdk.samtools.SAMException; | ||
import htsjdk.samtools.SAMRecord; | ||
|
||
/** | ||
* Filter things that fall outside a specified range of insert sizes. | ||
* This will automatically omit unpaired reads. | ||
*/ | ||
public class InsertSizeFilter implements SamRecordFilter { | ||
final int minInsertSize; | ||
final int maxInsertSize; | ||
|
||
public InsertSizeFilter(final int minInsertSize, final int maxInsertSize) { | ||
if (minInsertSize > maxInsertSize) throw new SAMException("Cannot have minInsertSize > maxInsertSize"); | ||
this.minInsertSize = minInsertSize; | ||
this.maxInsertSize = maxInsertSize; | ||
} | ||
|
||
@Override | ||
public boolean filterOut(final SAMRecord rec) { | ||
if (!rec.getReadPairedFlag()) return true; | ||
final int ins = Math.abs(rec.getInferredInsertSize()); | ||
return ins < minInsertSize || ins > maxInsertSize; | ||
} | ||
|
||
@Override | ||
public boolean filterOut(final SAMRecord r1, final SAMRecord r2) { | ||
return filterOut(r1) || filterOut(r2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package htsjdk.samtools.filter; | ||
|
||
import htsjdk.samtools.SAMRecord; | ||
|
||
/** | ||
* Filter things with low mapping quality. | ||
*/ | ||
public class MappingQualityFilter implements SamRecordFilter { | ||
|
||
private int minimumMappingQuality = Integer.MIN_VALUE; | ||
|
||
public MappingQualityFilter(final int minimumMappingQuality) { | ||
this.minimumMappingQuality = minimumMappingQuality; | ||
} | ||
|
||
@Override | ||
public boolean filterOut(final SAMRecord record) { | ||
return record.getMappingQuality() < this.minimumMappingQuality; | ||
} | ||
|
||
@Override | ||
public boolean filterOut(final SAMRecord first, final SAMRecord second) { | ||
return filterOut(first) || filterOut(second); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package htsjdk.samtools.util; | ||
|
||
/** | ||
* Miscellaneous util methods that don't fit anywhere else. | ||
*/ | ||
public class CodeUtil { | ||
|
||
/** Mimic of Oracle's nvl() - returns the first value if not null, otherwise the second value. */ | ||
public static <T> T getOrElse(final T value1, final T value2) { | ||
if (value1 != null) return value1; | ||
else return value2; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/tests/java/htsjdk/samtools/filter/InsertSizeFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package htsjdk.samtools.filter; | ||
|
||
import htsjdk.samtools.SAMRecord; | ||
import htsjdk.samtools.SAMRecordSetBuilder; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class InsertSizeFilterTest { | ||
private static final int READ_LENGTH = 20; | ||
private final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(); | ||
|
||
@BeforeTest | ||
public void setUp() { | ||
builder.setReadLength(READ_LENGTH); | ||
builder.addFrag("mapped_unpaired", 1, 1, false); | ||
builder.addUnmappedPair("unmapped_paired"); // insert size = 0 | ||
builder.addPair("mapped_paired_short", 1, 1, 31); // insert size = 50 | ||
builder.addPair("mapped_paired_long", 1, 1, 81); // insert size = 100 | ||
builder.addPair("mapped_paired_long_flipped", 1, 81, 1); // insert size = 100 | ||
} | ||
|
||
@Test(dataProvider = "data") | ||
public void testInsertSizeFilter(final int minInsertSize, final int maxInsertSize, final int expectedPassingRecords) { | ||
final InsertSizeFilter filter = new InsertSizeFilter(minInsertSize, maxInsertSize); | ||
int actualPassingRecords = 0; | ||
for (final SAMRecord rec : builder) { | ||
if (!filter.filterOut(rec)) actualPassingRecords++; | ||
} | ||
Assert.assertEquals(actualPassingRecords, expectedPassingRecords); | ||
} | ||
|
||
@DataProvider(name = "data") | ||
private Object[][] testData() { | ||
return new Object[][]{ | ||
{0, 0, 2}, | ||
{50, 50, 2}, | ||
{50, 100, 6}, | ||
{0, Integer.MAX_VALUE, 8} | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/tests/java/htsjdk/samtools/filter/MappingQualityFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package htsjdk.samtools.filter; | ||
|
||
import htsjdk.samtools.SAMRecord; | ||
import htsjdk.samtools.SAMRecordSetBuilder; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class MappingQualityFilterTest { | ||
private final SAMRecordSetBuilder builder = new SAMRecordSetBuilder(); | ||
|
||
@BeforeTest | ||
public void setUp() { | ||
// note the side effects... | ||
builder.addFrag("zeroMQ", 1, 1, false).setMappingQuality(0); | ||
builder.addFrag("lowMQ", 1, 1, false).setMappingQuality(10); | ||
builder.addFrag("highMQ", 1, 1, false).setMappingQuality(30); | ||
} | ||
|
||
@Test(dataProvider = "data") | ||
public void testMappingQualityFilter(final int minMappingQuality, final int expectedPassingRecords) { | ||
final MappingQualityFilter filter = new MappingQualityFilter(minMappingQuality); | ||
int actualPassingRecords = 0; | ||
for (final SAMRecord rec : builder) { | ||
if (!filter.filterOut(rec)) actualPassingRecords++; | ||
} | ||
Assert.assertEquals(actualPassingRecords, expectedPassingRecords); | ||
} | ||
|
||
@DataProvider(name = "data") | ||
private Object[][] testData() { | ||
return new Object[][]{ | ||
{0, 3}, | ||
{10, 2}, | ||
{30, 1} | ||
}; | ||
} | ||
} |
Oops, something went wrong.