public static CRAIEntry newEntryContOffset(final int containerStartOffset) { return newEntry(1, 0, 0, containerStartOffset, 0, 0); }
@Test public void testCompareTo () { final List<CRAIEntry> list = new ArrayList<>(2); CRAIEntry e1; CRAIEntry e2; e1 = newEntry(100, 0, 0); e2 = newEntry(200, 0, 0); list.add(e2); list.add(e1); Assert.assertTrue(list.get(1).getSequenceId() < list.get(0).getSequenceId()); Collections.sort(list); Assert.assertTrue(list.get(0).getSequenceId() < list.get(1).getSequenceId()); list.clear(); e1 = newEntry(1, 100, 0); e2 = newEntry(1, 200, 0); list.add(e2); list.add(e1); Assert.assertTrue(list.get(1).getAlignmentStart() < list.get(0).getAlignmentStart()); Collections.sort(list); Assert.assertTrue(list.get(0).getAlignmentStart() < list.get(1).getAlignmentStart()); list.clear(); e1 = newEntryContOffset(100); e2 = newEntryContOffset(200); list.add(e2); list.add(e1); Assert.assertTrue(list.get(1).getContainerStartByteOffset() < list.get(0).getContainerStartByteOffset()); Collections.sort(list); Assert.assertTrue(list.get(0).getContainerStartByteOffset() < list.get(1).getContainerStartByteOffset()); }
public static CRAIEntry newEntrySeqStart(final int seqId, final int start) { return newEntry(seqId, start, 0); }
@Test public void testIntersectsIncluded() { Assert.assertTrue(CRAIEntry.intersect(newEntry(1, 1, 2), newEntry(1, 1, 1))); Assert.assertTrue(CRAIEntry.intersect(newEntry(1, 1, 2), newEntry(1, 2, 1))); // is symmetrical? Assert.assertTrue(CRAIEntry.intersect(newEntry(1, 1, 1), newEntry(1, 1, 2))); Assert.assertTrue(CRAIEntry.intersect(newEntry(1, 2, 1), newEntry(1, 1, 2))); }
@Test public void testIntersectsOvertlaping() { Assert.assertFalse(CRAIEntry.intersect(newEntry(1, 1, 2), newEntry(1, 0, 1))); Assert.assertTrue(CRAIEntry.intersect(newEntry(1, 1, 2), newEntry(1, 0, 2))); Assert.assertTrue(CRAIEntry.intersect(newEntry(1, 1, 2), newEntry(1, 2, 1))); Assert.assertFalse(CRAIEntry.intersect(newEntry(1, 1, 2), newEntry(1, 3, 1))); }
public static CRAIEntry newEntry(final int sequenceId, final int start, final int span, final int containerStartOffset, final int sliceOffset, final int sliceSize) { return new CRAIEntry(sequenceId, start, span, containerStartOffset, sliceOffset, sliceSize); }
@Test public void testGetLeftmost() { final List<CRAIEntry> index = new ArrayList<>(); Assert.assertNull(CRAIIndex.getLeftmost(index)); final CRAIEntry e1 = CRAIEntryTest.newEntry(1, 2, 3, 4, 5, 6); index.add(e1); // trivial case of single entry in index: Assert.assertEquals(e1, CRAIIndex.getLeftmost(index)); final CRAIEntry e2 = CRAIEntryTest.updateStart(e1, e1.getAlignmentStart() + 1); index.add(e2); Assert.assertEquals(e1, CRAIIndex.getLeftmost(index)); }
public static CRAIEntry updateStart(final CRAIEntry toClone, final int alignmentStart) { return newEntry(toClone.getSequenceId(), alignmentStart, toClone.getAlignmentSpan()); }
@Test public void testFind() { final List<CRAIEntry> index = new ArrayList<>(); final int sequenceId = 1; CRAIEntry e = CRAIEntryTest.newEntry(sequenceId, 1, 1, 1, 1, 0); index.add(e); e = CRAIEntryTest.updateStartContOffset(e, 2, 2); index.add(e); e = CRAIEntryTest.updateStartContOffset(e, 3, 3); index.add(e); Assert.assertFalse(allFoundEntriesIntersectQueryInFind(index, sequenceId, 1, 0)); Assert.assertTrue(allFoundEntriesIntersectQueryInFind(index, sequenceId, 1, 1)); Assert.assertTrue(allFoundEntriesIntersectQueryInFind(index, sequenceId, 1, 2)); Assert.assertTrue(allFoundEntriesIntersectQueryInFind(index, sequenceId, 2, 1)); Assert.assertTrue(allFoundEntriesIntersectQueryInFind(index, sequenceId, 1, 3)); final int nonExistentSequenceId = 2; Assert.assertFalse(allFoundEntriesIntersectQueryInFind(index, nonExistentSequenceId, 2, 1)); // a query starting beyond all entries: Assert.assertFalse(allFoundEntriesIntersectQueryInFind(index, sequenceId, 4, 1)); }
private void doCRAITest(BiFunction<SAMSequenceDictionary, List<CRAIEntry>, SeekableStream> getBaiStreamForIndex) { final ArrayList<CRAIEntry> index = new ArrayList<>(); final CRAIEntry entry = CRAIEntryTest.newEntry(0, 1, 2, 5, 3, 4); index.add(entry); final SAMSequenceDictionary dictionary = new SAMSequenceDictionary(); dictionary.addSequence(new SAMSequenceRecord("1", 100)); final SeekableStream baiStream = getBaiStreamForIndex.apply(dictionary, index); final DiskBasedBAMFileIndex bamIndex = new DiskBasedBAMFileIndex(baiStream, dictionary); final BAMFileSpan span = bamIndex.getSpanOverlapping(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentStart()); Assert.assertNotNull(span); final long[] coordinateArray = span.toCoordinateArray(); Assert.assertEquals(coordinateArray.length, 2); Assert.assertEquals(coordinateArray[0] >> 16, entry.getContainerStartByteOffset()); Assert.assertEquals(coordinateArray[1] & 0xFFFF, 1); }
public static CRAIEntry updateStartContOffset(final CRAIEntry toClone, final int alignmentStart, final int containerStartOffset) { return newEntry(toClone.getSequenceId(), alignmentStart, toClone.getAlignmentSpan(), containerStartOffset, toClone.getSliceByteOffset(), toClone.getSliceByteSize()); } }
@Test public void testOpenIndexUrlAsBaiOrNull() throws IOException { final SAMSequenceDictionary dictionary = new SAMSequenceDictionary(); dictionary.addSequence(new SAMSequenceRecord("1", 100)); final File file = File.createTempFile("test", ".crai"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(fos, header); final CRAIEntry entry = CRAIEntryTest.newEntry(0, 1, 2, 5, 3, 4); indexer.addEntry(entry); indexer.finish(); fos.close(); final InputStream baiStream = SamIndexes.openIndexUrlAsBaiOrNull(file.toURI().toURL(), dictionary); Assert.assertNotNull(baiStream); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtil.copyStream(baiStream, baos); final CachingBAMFileIndex bamIndex = new CachingBAMFileIndex(new SeekableMemoryStream(baos.toByteArray(), null), dictionary); final BAMFileSpan span = bamIndex.getSpanOverlapping(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentStart()); Assert.assertNotNull(span); final long[] coordinateArray = span.toCoordinateArray(); Assert.assertEquals(coordinateArray.length, 2); Assert.assertEquals(coordinateArray[0] >> 16, entry.getContainerStartByteOffset()); Assert.assertEquals(coordinateArray[1] & 0xFFFF, 1); } }
@Test public void testCraiInMemory() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(baos, header); final CRAIEntry entry = CRAIEntryTest.newEntry(0, 1, 2, 5, 3, 4); indexer.addEntry(entry); indexer.finish(); baos.close(); final SAMSequenceDictionary dictionary = new SAMSequenceDictionary(); dictionary.addSequence(new SAMSequenceRecord("1", 100)); final InputStream baiStream = SamIndexes.asBaiStreamOrNull(new ByteArrayInputStream(baos.toByteArray()), dictionary); Assert.assertNotNull(baiStream); baos = new ByteArrayOutputStream(); IOUtil.copyStream(baiStream, baos); final CachingBAMFileIndex bamIndex = new CachingBAMFileIndex(new SeekableMemoryStream(baos.toByteArray(), null), dictionary); final BAMFileSpan span = bamIndex.getSpanOverlapping(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentStart()); Assert.assertNotNull(span); final long[] coordinateArray = span.toCoordinateArray(); Assert.assertEquals(coordinateArray.length, 2); Assert.assertEquals(coordinateArray[0] >> 16, entry.getContainerStartByteOffset()); Assert.assertEquals(coordinateArray[1] & 0xFFFF, 1); }
@Test public void testCraiFromFile() throws IOException { final File file = File.createTempFile("test", ".crai"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); SAMFileHeader header = new SAMFileHeader(); header.setSortOrder(SAMFileHeader.SortOrder.coordinate); CRAMCRAIIndexer indexer = new CRAMCRAIIndexer(fos, header); final CRAIEntry entry = CRAIEntryTest.newEntry(0, 1, 2, 5, 3, 4); indexer.addEntry(entry); indexer.finish(); fos.close(); final SAMSequenceDictionary dictionary = new SAMSequenceDictionary(); dictionary.addSequence(new SAMSequenceRecord("1", 100)); final SeekableStream baiStream = SamIndexes.asBaiSeekableStreamOrNull(new SeekableFileStream(file), dictionary); Assert.assertNotNull(baiStream); final CachingBAMFileIndex bamIndex = new CachingBAMFileIndex(baiStream, dictionary); final BAMFileSpan span = bamIndex.getSpanOverlapping(entry.getSequenceId(), entry.getAlignmentStart(), entry.getAlignmentStart()); Assert.assertNotNull(span); final long[] coordinateArray = span.toCoordinateArray(); Assert.assertEquals(coordinateArray.length, 2); Assert.assertEquals(coordinateArray[0] >> 16, entry.getContainerStartByteOffset()); Assert.assertEquals(coordinateArray[1] & 0xFFFF, 1); }