Fix BEDCodec.canDecode() to handle block-compressed extensions #704

Merged
merged 2 commits into from Sep 13, 2016
Jump to file or symbol
Failed to load files and symbols.
+22 −1
Split
@@ -23,6 +23,7 @@
*/
package htsjdk.tribble.bed;
+import htsjdk.tribble.AbstractFeatureReader;
import htsjdk.tribble.AsciiFeatureCodec;
import htsjdk.tribble.annotation.Strand;
import htsjdk.tribble.index.tabix.TabixFormat;
@@ -40,6 +41,9 @@
*/
public class BEDCodec extends AsciiFeatureCodec<BEDFeature> {
+ /** Default extension for BED files. */
+ public static final String BED_EXTENSION = ".bed";
+
private static final Pattern SPLIT_PATTERN = Pattern.compile("\\t|( +)");
private final int startOffsetValue;
@@ -197,7 +201,13 @@ private void createExons(int start, String[] tokens, FullBEDFeature gene,
@Override
public boolean canDecode(final String path) {
- return path.toLowerCase().endsWith(".bed");
+ final String toDecode;
+ if (AbstractFeatureReader.hasBlockCompressedExtension(path)) {
+ toDecode = path.substring(0, path.lastIndexOf("."));
+ } else {
+ toDecode = path;
+ }
+ return toDecode.toLowerCase().endsWith(BED_EXTENSION);
}
public int getStartOffset() {
@@ -226,4 +226,15 @@ private void createIndex(File testFile, File idxFile) throws IOException {
public void testGetTabixFormat() {
Assert.assertEquals(new BEDCodec().getTabixFormat(), TabixFormat.BED);
}
+
@lbergelson

lbergelson Sep 12, 2016

Contributor

Could you add a small block compressed bedfile and a test that shows that it actually can read from it as well?

@magicDGS

magicDGS Sep 12, 2016

Contributor

I will add it tomorrow, I don't have now a command line available to create the test file.

@magicDGS

magicDGS Sep 13, 2016

Contributor

I think that I don't need to implement a new test for this. A FeatureReader for bgzip compressed files and tabix index queries is implemented in FeatureReaderTest.testBedQuery().

@lbergelson

lbergelson Sep 13, 2016

Contributor

ah, good point, I didn't see

+ @Test
+ public void testCanDecode() {
+ final BEDCodec codec = new BEDCodec();
+ final String pattern = "filename.%s%s";
+ for(final String bcExt: AbstractFeatureReader.BLOCK_COMPRESSED_EXTENSIONS) {
+ Assert.assertTrue(codec.canDecode(String.format(pattern, "bed", bcExt)));
+ Assert.assertFalse(codec.canDecode(String.format(pattern, "vcf", bcExt)));
+ Assert.assertFalse(codec.canDecode(String.format(pattern, "bed.gzip", bcExt)));
+ }
+ }
}