Skip to content

Commit d9eb9cd

Browse files
committed
Merge pull request #299 from edwardkw/ek_fixbai_296
Hooking up the index file to CRAM files in SamReaderFactory
2 parents 0b7bc0e + 3d69a57 commit d9eb9cd

File tree

14 files changed

+54
-3
lines changed

14 files changed

+54
-3
lines changed

src/java/htsjdk/samtools/SamReaderFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ public SamReader open(final SamInputResource resource) {
268268
bufferedStream.close();
269269
bufferedStream = null;
270270
}
271-
// Handle case in which file is a named pipe, e.g. /dev/stdin or created by mkfifo
271+
272+
// Always attempt to pass in the index. If it is null, that's fine. If the reference isn't supplied, use the default.
272273
if (referenceSource != null) {
273-
primitiveSamReader = new CRAMFileReader(sourceFile, bufferedStream, referenceSource);
274+
primitiveSamReader = new CRAMFileReader(sourceFile, indexMaybe == null ? null : indexMaybe.asFile(), referenceSource);
274275
} else {
275-
primitiveSamReader = new CRAMFileReader(sourceFile, bufferedStream);
276+
primitiveSamReader = new CRAMFileReader(sourceFile, indexMaybe == null ? null : indexMaybe.asFile(), new ReferenceSource(Defaults.REFERENCE_FASTA));
276277
}
277278
} else {
278279
if (indexDefined) {

src/tests/java/htsjdk/samtools/SAMFileReaderTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ public Object[][] variousFormatReaderTestCases() {
5353
return scenarios;
5454
}
5555

56+
// tests for CRAM indexing
57+
58+
@Test(dataProvider = "SmallCRAMTest")
59+
public void CRAMIndexTest(final String inputFile) {
60+
final File input = new File(TEST_DATA_DIR, inputFile);
61+
final SamReader reader = SamReaderFactory.makeDefault().open(input);
62+
Assert.assertTrue(reader.hasIndex());
63+
CloserUtil.close(reader);
64+
}
65+
66+
@DataProvider(name = "SmallCRAMTest")
67+
public Object[][] CRAMIndexTestData() {
68+
final Object[][] testFiles = new Object[][]{
69+
{"cram/test.cram"},
70+
};
71+
return testFiles;
72+
}
73+
74+
@Test(dataProvider = "NoIndexCRAMTest")
75+
public void CRAMNoIndexTest(final String inputFile) {
76+
final File input = new File(TEST_DATA_DIR, inputFile);
77+
final SamReader reader = SamReaderFactory.makeDefault().open(input);
78+
Assert.assertFalse(reader.hasIndex());
79+
CloserUtil.close(reader);
80+
}
81+
82+
@DataProvider(name = "NoIndexCRAMTest")
83+
public Object[][] CRAMNoIndexTestData() {
84+
final Object[][] testFiles = new Object[][]{
85+
{"cram/test2.cram"},
86+
};
87+
return testFiles;
88+
}
89+
5690
// Tests for the SAMRecordFactory usage
5791
class SAMRecordFactoryTester extends DefaultSAMRecordFactory {
5892
int samRecordsCreated;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
>Sheila
2+
GCTAGCTCAGAAAAAAAAAA
3.35 KB
Binary file not shown.
96 Bytes
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@HD VN:1.4 SO:unsorted
2+
@SQ SN:Sheila LN:20 M5:7ddd8a4b4f2c1dec43476a738b1a9b72 UR:file:/Users/edwardk/Documents/htsjdk/testdata/htsjdk/samtools/cram/auxf.fa
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
>Sheila
2+
GCTAGCTCAGAAAAAAAAAA
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sheila 20 8 20 21
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
>Sheila
2+
GCTAGCTCAGAAAAAAAAAA
3.35 KB
Binary file not shown.

0 commit comments

Comments
 (0)