Search in sources :

Example 1 with SAMRecordToReadIterator

use of org.broadinstitute.hellbender.utils.iterators.SAMRecordToReadIterator in project gatk by broadinstitute.

the class ReadsDataSource method prepareIteratorsForTraversal.

/**
     * Prepare iterators over all readers in response to a request for a complete iteration or query
     *
     * @param queryIntervals Intervals to bound the iteration (reads must overlap one of these intervals). If null, iteration is unbounded.
     * @return Iterator over all reads in this data source, limited to overlap with the supplied intervals
     */
private Iterator<GATKRead> prepareIteratorsForTraversal(final List<SimpleInterval> queryIntervals, final boolean queryUnmapped) {
    // htsjdk requires that only one iterator be open at a time per reader, so close out
    // any previous iterations
    closePreviousIterationsIfNecessary();
    final boolean traversalIsBounded = (queryIntervals != null && !queryIntervals.isEmpty()) || queryUnmapped;
    // Set up an iterator for each reader, bounded to overlap with the supplied intervals if there are any
    for (Map.Entry<SamReader, CloseableIterator<SAMRecord>> readerEntry : readers.entrySet()) {
        if (traversalIsBounded) {
            readerEntry.setValue(new SamReaderQueryingIterator(readerEntry.getKey(), readers.size() > 1 ? getIntervalsOverlappingReader(readerEntry.getKey(), queryIntervals) : queryIntervals, queryUnmapped));
        } else {
            readerEntry.setValue(readerEntry.getKey().iterator());
        }
    }
    // Create a merging iterator over all readers if necessary. In the case where there's only a single reader,
    // return its iterator directly to avoid the overhead of the merging iterator.
    Iterator<SAMRecord> startingIterator = null;
    if (readers.size() == 1) {
        startingIterator = readers.entrySet().iterator().next().getValue();
    } else {
        startingIterator = new MergingSamRecordIterator(headerMerger, readers, true);
    }
    return new SAMRecordToReadIterator(startingIterator);
}
Also used : SamReaderQueryingIterator(org.broadinstitute.hellbender.utils.iterators.SamReaderQueryingIterator) CloseableIterator(htsjdk.samtools.util.CloseableIterator) SAMRecordToReadIterator(org.broadinstitute.hellbender.utils.iterators.SAMRecordToReadIterator)

Aggregations

CloseableIterator (htsjdk.samtools.util.CloseableIterator)1 SAMRecordToReadIterator (org.broadinstitute.hellbender.utils.iterators.SAMRecordToReadIterator)1 SamReaderQueryingIterator (org.broadinstitute.hellbender.utils.iterators.SamReaderQueryingIterator)1