Skip to content

Commit

Permalink
new method CRAMFileReader.createIndexIterator (#1120)
Browse files Browse the repository at this point in the history
* added a new method  CRAMFileReader.createIndexIterator get reads overlapping intervals in a cram file, analogous to BAMFileReader#createIndexIterator
* part of #1112
  • Loading branch information
tomwhite authored and lbergelson committed Jun 15, 2018
1 parent 8b55de6 commit 9ea0a03
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/main/java/htsjdk/samtools/CRAMFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,32 @@ void enableFileSource(final SamReader reader, final boolean enabled) {
iterator.setFileSource(enabled ? reader : null);
}

/**
* Prepare to iterate through SAMRecords that match the intersection of the given intervals and chunk boundaries.
* @param intervals the intervals to restrict reads to
* @param contained if <code>true</code>, return records that are strictly
* contained in the intervals, otherwise return records that overlap
* @param filePointers file pointer pairs corresponding to chunk boundaries for the
* intervals
*/
public CloseableIterator<SAMRecord> createIndexIterator(final QueryInterval[] intervals,
final boolean contained,
final long[] filePointers) {
return new CRAMIntervalIterator(intervals, contained, filePointers);
}

// convert queries -> merged BAMFileSpan -> coordinate array
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 class CRAMIntervalIterator extends BAMQueryMultipleIntervalsIteratorFilter
implements CloseableIterator<SAMRecord> {

Expand All @@ -475,9 +501,12 @@ private class CRAMIntervalIterator extends BAMQueryMultipleIntervalsIteratorFilt
SAMRecord nextRec = null;

public CRAMIntervalIterator(final QueryInterval[] queries, final boolean contained) {
this(queries, contained, coordinatesFromQueryIntervals(getIndex(), queries));
}

public CRAMIntervalIterator(final QueryInterval[] queries, final boolean contained, final long[] coordinates) {
super(queries, contained);

long[] coordinates = coordinatesFromQueryIntervals(getIndex(), queries);
if (coordinates != null && coordinates.length != 0) {
try {
unfilteredIterator = new CRAMIterator(
Expand All @@ -493,18 +522,6 @@ public CRAMIntervalIterator(final QueryInterval[] queries, final boolean contain
}
}

// convert queries -> merged BAMFileSpan -> coordinate array
private long[] coordinatesFromQueryIntervals(BAMIndex index, QueryInterval[] queries) {
ArrayList<BAMFileSpan> spanList = new ArrayList<>(1);
Arrays.asList(queries).forEach(qi -> spanList.add(mIndex.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 void close() {
if (unfilteredIterator != null) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/htsjdk/samtools/CRAMFileCRAIIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,29 @@ public void testQueryInterval() throws IOException {
reader.close();
}

@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();
}

@BeforeTest
public void prepare() throws IOException {
Log.setGlobalLogLevel(Log.LogLevel.ERROR);
Expand Down

0 comments on commit 9ea0a03

Please sign in to comment.