@Override public CloseableIterator<SAMRecord> getIterator(final SAMFileSpan chunks) { if (mStream == null) { throw new IllegalStateException("File reader is closed"); } if (mCurrentIterator != null) { throw new IllegalStateException("Iteration in progress"); } if (!(chunks instanceof BAMFileSpan)) { throw new IllegalStateException("BAMFileReader cannot handle this type of file span."); } // Create an iterator over the given chunk boundaries. mCurrentIterator = new BAMFileIndexIterator(((BAMFileSpan)chunks).toCoordinateArray()); return mCurrentIterator; }
@Override public CloseableIterator<SAMRecord> getIterator(final SAMFileSpan chunks) { if (mStream == null) { throw new IllegalStateException("File reader is closed"); } if (mCurrentIterator != null) { throw new IllegalStateException("Iteration in progress"); } if (!(chunks instanceof BAMFileSpan)) { throw new IllegalStateException("BAMFileReader cannot handle this type of file span."); } // Create an iterator over the given chunk boundaries. mCurrentIterator = new BAMFileIndexIterator(((BAMFileSpan)chunks).toCoordinateArray()); return mCurrentIterator; }
private static long[] coordinatesFromQueryIntervals(BAMIndex index, QueryInterval[] queries) { ArrayList<BAMFileSpan> spanList = new ArrayList<>(1); Arrays.asList(queries).forEach(qi -> spanList.add(index.getSpanOverlapping(qi.referenceIndex, qi.start, qi.end))); BAMFileSpan spanArray[] = new BAMFileSpan[spanList.size()]; for (int i = 0; i < spanList.size(); i++) { spanArray[i] = spanList.get(i); } return BAMFileSpan.merge(spanArray).toCoordinateArray(); }
private static long[] coordinatesFromQueryIntervals(BAMIndex index, QueryInterval[] queries) { ArrayList<BAMFileSpan> spanList = new ArrayList<>(1); Arrays.asList(queries).forEach(qi -> spanList.add(index.getSpanOverlapping(qi.referenceIndex, qi.start, qi.end))); BAMFileSpan spanArray[] = new BAMFileSpan[spanList.size()]; for (int i = 0; i < spanList.size(); i++) { spanArray[i] = spanList.get(i); } return BAMFileSpan.merge(spanArray).toCoordinateArray(); }
@Override public CloseableIterator<SAMRecord> getIterator(final SAMFileSpan chunks) { if (mStream == null) { throw new IllegalStateException("File reader is closed"); } if (mCurrentIterator != null) { throw new IllegalStateException("Iteration in progress"); } if (!(chunks instanceof BAMFileSpan)) { throw new IllegalStateException("BAMFileReader cannot handle this type of file span."); } // Create an iterator over the given chunk boundaries. mCurrentIterator = new BAMFileIndexIterator(((BAMFileSpan)chunks).toCoordinateArray()); return mCurrentIterator; }
@Override public CloseableIterator<SAMRecord> getIterator(final SAMFileSpan chunks) { if (mStream == null) { throw new IllegalStateException("File reader is closed"); } if (mCurrentIterator != null) { throw new IllegalStateException("Iteration in progress"); } if (!(chunks instanceof BAMFileSpan)) { throw new IllegalStateException("BAMFileReader cannot handle this type of file span."); } // Create an iterator over the given chunk boundaries. mCurrentIterator = new BAMFileIndexIterator(((BAMFileSpan)chunks).toCoordinateArray()); return mCurrentIterator; }
@Override public SAMRecordIterator iterator(final SAMFileSpan fileSpan) { // get the file coordinates for the span: final long[] coordinateArray = ((BAMFileSpan) fileSpan).toCoordinateArray(); if (coordinateArray == null || coordinateArray.length == 0) return emptyIterator; try { // create an input stream that reads the source cram stream only within the coordinate pairs: final SeekableStream seekableStream = getSeekableStreamOrFailWithRTE(); return new CRAMIterator(seekableStream, referenceSource, coordinateArray, validationStringency); } catch (final IOException e) { throw new RuntimeException(e); } }
@Override public SAMRecordIterator iterator(final SAMFileSpan fileSpan) { // get the file coordinates for the span: final long[] coordinateArray = ((BAMFileSpan) fileSpan).toCoordinateArray(); if (coordinateArray == null || coordinateArray.length == 0) return emptyIterator; // create an input stream that reads the source cram stream only within the coordinate pairs: final SeekableStream seekableStream = getSeekableStreamOrFailWithRTE(); return new CRAMIterator(seekableStream, referenceSource, coordinateArray, validationStringency); }
public long[] getBAMIndexPointers(File indexFile, SAMSequenceDictionary dictionary, String sequenceName, int alignmentStart, int alignmentEnd) { long[] filePointers = new long[0]; final int referenceIndex = dictionary.getSequenceIndex(sequenceName); if (referenceIndex != -1) { final BAMIndex fileIndex = BAMIndexFactory.SHARED_INSTANCE.createCachingIndex(indexFile, dictionary); final BAMFileSpan fileSpan = fileIndex.getSpanOverlapping(referenceIndex, alignmentStart, alignmentEnd); filePointers = fileSpan != null ? fileSpan.toCoordinateArray() : null; } return filePointers; } }
@Test(dataProvider = "testSpanOverlappings") public void testSpanOverlappings(SRAAccession acc, int reference, int firstLocus, int lastLocus, long[] spanCoordinates) { final SRAIndex index = getIndex(acc); final BAMFileSpan span = index.getSpanOverlapping(reference, firstLocus, lastLocus); long[] coordinatesFromIndex = span.toCoordinateArray(); Assert.assertTrue(Arrays.equals(coordinatesFromIndex, spanCoordinates), "Coordinates mismatch. Expected: " + Arrays.toString(spanCoordinates) + " but was : " + Arrays.toString(coordinatesFromIndex)); }
@Test public void testGetSearchBins() throws Exception { final DiskBasedBAMFileIndex bfi = new DiskBasedBAMFileIndex(new File(BAM_FILE.getPath() + ".bai"), null); // todo can null be replaced with a Sequence dictionary for the BAM_FILE? final long[] bins = bfi.getSpanOverlapping(1, 0, 0).toCoordinateArray(); /*** if (bins == null) { System.out.println("Search bins: " + bins); return; } System.out.println("Search bins:"); for (int i = 0; i < bins.length; i++) { System.out.println(" " + Long.toHexString(bins[i])); } ***/ assertNotNull(bins); assertEquals(bins.length, 2); }
private CloseableIterator<SAMRecord> createIndexIterator(final QueryInterval[] intervals, final boolean contained) { assertIntervalsOptimized(intervals); BAMFileSpan span = getFileSpan(intervals, getIndex()); // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(span == null ? null : span.toCoordinateArray()); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator, new BAMQueryMultipleIntervalsIteratorFilter(intervals, contained)); }
private CloseableIterator<SAMRecord> createIndexIterator(final QueryInterval[] intervals, final boolean contained) { assertIntervalsOptimized(intervals); BAMFileSpan span = getFileSpan(intervals, getIndex()); // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(span == null ? null : span.toCoordinateArray()); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator, new BAMQueryMultipleIntervalsIteratorFilter(intervals, contained)); }
/** * Prepare to iterate through SAMRecords in the given reference that start exactly at the given start coordinate. * @param referenceIndex Desired reference sequence. * @param start 1-based alignment start. */ private CloseableIterator<SAMRecord> createStartingAtIndexIterator(final int referenceIndex, final int start) { // Hit the index to determine the chunk boundaries for the required data. final BAMIndex fileIndex = getIndex(); final BAMFileSpan fileSpan = fileIndex.getSpanOverlapping(referenceIndex, start, 0); final long[] filePointers = fileSpan != null ? fileSpan.toCoordinateArray() : null; // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(filePointers); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator,new BAMStartingAtIteratorFilter(referenceIndex,start)); }
/** * Prepare to iterate through SAMRecords in the given reference that start exactly at the given start coordinate. * @param referenceIndex Desired reference sequence. * @param start 1-based alignment start. */ private CloseableIterator<SAMRecord> createStartingAtIndexIterator(final int referenceIndex, final int start) { // Hit the index to determine the chunk boundaries for the required data. final BAMIndex fileIndex = getIndex(); final BAMFileSpan fileSpan = fileIndex.getSpanOverlapping(referenceIndex, start, 0); final long[] filePointers = fileSpan != null ? fileSpan.toCoordinateArray() : null; // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(filePointers); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator,new BAMStartingAtIteratorFilter(referenceIndex,start)); }
private CloseableIterator<SAMRecord> createIndexIterator(final QueryInterval[] intervals, final boolean contained) { assertIntervalsOptimized(intervals); BAMFileSpan span = getFileSpan(intervals, getIndex()); // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(span == null ? null : span.toCoordinateArray()); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator, new BAMQueryMultipleIntervalsIteratorFilter(intervals, contained)); }
/** * Prepare to iterate through SAMRecords in the given reference that start exactly at the given start coordinate. * @param referenceIndex Desired reference sequence. * @param start 1-based alignment start. */ private CloseableIterator<SAMRecord> createStartingAtIndexIterator(final int referenceIndex, final int start) { // Hit the index to determine the chunk boundaries for the required data. final BAMIndex fileIndex = getIndex(); final BAMFileSpan fileSpan = fileIndex.getSpanOverlapping(referenceIndex, start, 0); final long[] filePointers = fileSpan != null ? fileSpan.toCoordinateArray() : null; // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(filePointers); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator,new BAMStartingAtIteratorFilter(referenceIndex,start)); }
/** * Prepare to iterate through SAMRecords in the given reference that start exactly at the given start coordinate. * @param referenceIndex Desired reference sequence. * @param start 1-based alignment start. */ private CloseableIterator<SAMRecord> createStartingAtIndexIterator(final int referenceIndex, final int start) { // Hit the index to determine the chunk boundaries for the required data. final BAMIndex fileIndex = getIndex(); final BAMFileSpan fileSpan = fileIndex.getSpanOverlapping(referenceIndex, start, 0); final long[] filePointers = fileSpan != null ? fileSpan.toCoordinateArray() : null; // Create an iterator over the above chunk boundaries. final BAMFileIndexIterator iterator = new BAMFileIndexIterator(filePointers); // Add some preprocessing filters for edge-case reads that don't fit into this // query type. return new BAMQueryFilteringIterator(iterator,new BAMStartingAtIteratorFilter(referenceIndex,start)); }
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); }
@Test public void testQueryIntervalWithFilePointers() throws IOException { CRAMFileReader reader = new CRAMFileReader( new ByteArraySeekableStream(cramBytes), new ByteArraySeekableStream(craiBytes), source, ValidationStringency.STRICT); QueryInterval[] query = new QueryInterval[]{new QueryInterval(0, 1519, 1520), new QueryInterval(1, 470535, 470536)}; BAMFileSpan fileSpan = BAMFileReader.getFileSpan(query, reader.getIndex()); final CloseableIterator<SAMRecord> iterator = reader.createIndexIterator(query, false, fileSpan.toCoordinateArray()); Assert.assertTrue(iterator.hasNext()); SAMRecord r1 = iterator.next(); Assert.assertEquals(r1.getReadName(), "3968040"); Assert.assertTrue(iterator.hasNext()); SAMRecord r2 = iterator.next(); Assert.assertEquals(r2.getReadName(), "140419"); Assert.assertFalse(iterator.hasNext()); iterator.close(); reader.close(); }