From 3300ca906ada513f329db07b218da1a25e88f2f6 Mon Sep 17 00:00:00 2001 From: hanna Date: Fri, 27 Nov 2009 22:25:52 +0000 Subject: [PATCH] An iterator for Eric to use when injecting his new wrapping reads -- a stopgap solution for getting additional caching functionality into a SAMRecord. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2173 348d0f76-0448-11de-a6fe-93d51630548a --- .../simpleDataSources/SAMDataSource.java | 4 +- .../gatk/iterators/ReadWrappingIterator.java | 74 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 java/src/org/broadinstitute/sting/gatk/iterators/ReadWrappingIterator.java diff --git a/java/src/org/broadinstitute/sting/gatk/datasources/simpleDataSources/SAMDataSource.java b/java/src/org/broadinstitute/sting/gatk/datasources/simpleDataSources/SAMDataSource.java index 58c1fd461..2dde22630 100755 --- a/java/src/org/broadinstitute/sting/gatk/datasources/simpleDataSources/SAMDataSource.java +++ b/java/src/org/broadinstitute/sting/gatk/datasources/simpleDataSources/SAMDataSource.java @@ -403,7 +403,9 @@ public class SAMDataSource implements SimpleDataSource { */ private StingSAMIterator createIterator( DataStreamSegment segment ) { StingSAMIterator iterator = resourcePool.iterator(segment); - return new MalformedSAMFilteringIterator( getHeader(), iterator, violations ); + StingSAMIterator malformedWrappedIterator = new MalformedSAMFilteringIterator( getHeader(), iterator, violations ); + StingSAMIterator readWrappingIterator = new ReadWrappingIterator(malformedWrappedIterator); + return readWrappingIterator; } /** diff --git a/java/src/org/broadinstitute/sting/gatk/iterators/ReadWrappingIterator.java b/java/src/org/broadinstitute/sting/gatk/iterators/ReadWrappingIterator.java new file mode 100644 index 000000000..0dc92d589 --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/iterators/ReadWrappingIterator.java @@ -0,0 +1,74 @@ +package org.broadinstitute.sting.gatk.iterators; + +import net.sf.samtools.SAMRecord; +import org.broadinstitute.sting.gatk.Reads; + +/** + * An iterator which wraps each SAMRecord inside a wrapper class, bringing new functionality to the read while + * presenting the original SAMRecord interface. + * + * @author mhanna + * @version 0.1 + */ +public class ReadWrappingIterator implements StingSAMIterator { + /** + * Iterator to which to pass + */ + private StingSAMIterator wrappedIterator; + + /** + * Decorate the given iterator inside a ReadWrappingIterator. + * @param wrappedIterator + */ + public ReadWrappingIterator(StingSAMIterator wrappedIterator) { + this.wrappedIterator = wrappedIterator; + } + + /** + * Get metadata about the reads' sources, etc. + * @return Source info about the reads. + */ + public Reads getSourceInfo() { + return wrappedIterator.getSourceInfo(); + } + + /** + * Convenience function for use in foreach loops. Dangerous because it does not actually + * reset the iterator. + * @return An iterator through the current data stream. + */ + public StingSAMIterator iterator() { + // NOTE: this iterator doesn't perform any kind of reset operation; it just returns itself. + // can we do something better? Do we really have to provide support for the Iterable interface? + return this; + } + + /** + * Close this iterator. + */ + public void close() { + wrappedIterator.close(); + } + + /** + * Does the iterator contain more values? + * @return True if there are more left to return, false otherwise. + */ + public boolean hasNext() { + return wrappedIterator.hasNext(); + } + + /** + * Get the next value in the sequence. + * @return Next value in the sequence. By convention, a NoSuchElementException should be thrown if + * no next exists. + */ + public SAMRecord next() { + return wrappedIterator.next(); + } + + /** + * Remove the current element from the list. Unsupported in this wrapper. + */ + public void remove() { throw new UnsupportedOperationException("Cannot remove from a ReadWrappingIterator"); } +}