-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix CRAMIterator when next() is called without hasNext()
* fixing a bug in CRAMIterator which previously threw if next() was called without first calling hasNext() * adding CRAMTestUtils and extracting a method to it from CramEdgeCasesTest
- Loading branch information
1 parent
23f3223
commit 4cf7ce9
Showing
4 changed files
with
88 additions
and
38 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
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,64 @@ | ||
package htsjdk.samtools; | ||
|
||
import htsjdk.samtools.cram.ref.CRAMReferenceSource; | ||
import htsjdk.samtools.cram.ref.ReferenceSource; | ||
import htsjdk.samtools.reference.InMemoryReferenceSequenceFile; | ||
import htsjdk.samtools.seekablestream.SeekableStream; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
public final class CRAMTestUtils { | ||
|
||
private CRAMTestUtils(){}; | ||
|
||
/** | ||
* Write a collection of SAMRecords into an in memory Cram file and then open a reader over it | ||
* @param records a Collection of SAMRecords | ||
* @param ref a set of bases to use as the single reference contig named "chr1" | ||
* @return a CRAMFileReader reading from an in memory buffer that has had the records written into it | ||
*/ | ||
public static CRAMFileReader writeAndReadFromInMemoryCram(Collection<SAMRecord> records, byte[] ref) throws IOException { | ||
InMemoryReferenceSequenceFile refFile = new InMemoryReferenceSequenceFile(); | ||
refFile.add("chr1", ref); | ||
ReferenceSource source = new ReferenceSource(refFile); | ||
final SAMFileHeader header = records.iterator().next().getHeader(); | ||
return writeAndReadFromInMemoryCram(records, source, header); | ||
} | ||
|
||
/** | ||
* Write a collection of SAMRecords into an in memory Cram file and then open a reader over it | ||
* @param records a SAMRecordSetBuilder which has been initialized with records | ||
* @return a CRAMFileReader reading from an in memory buffer that has had the records written into it, uses a fake reference with all A's | ||
*/ | ||
public static CRAMFileReader writeAndReadFromInMemoryCram(SAMRecordSetBuilder records) throws IOException { | ||
return writeAndReadFromInMemoryCram(records.getRecords(), getFakeReferenceSource(), records.getHeader()); | ||
} | ||
|
||
private static CRAMFileReader writeAndReadFromInMemoryCram(Collection<SAMRecord> records, CRAMReferenceSource source, SAMFileHeader header) throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
CRAMFileWriter cramFileWriter = new CRAMFileWriter(baos, source, header, "whatever"); | ||
|
||
for (SAMRecord record : records) { | ||
cramFileWriter.addAlignment(record); | ||
} | ||
cramFileWriter.close(); | ||
|
||
return new CRAMFileReader(new ByteArrayInputStream(baos.toByteArray()), (SeekableStream) null, source, ValidationStringency.SILENT); | ||
} | ||
|
||
/** | ||
* return a CRAMReferenceSource that returns all A's for any sequence queried | ||
*/ | ||
public static CRAMReferenceSource getFakeReferenceSource(){ | ||
return (sequenceRecord, tryNameVariants) -> { | ||
byte[] bases = new byte[sequenceRecord.getSequenceLength()]; | ||
Arrays.fill(bases, (byte)'A'); | ||
return bases; | ||
}; | ||
} | ||
} |