From 0215905bb66eec6636405ab2d12468b00cc761f4 Mon Sep 17 00:00:00 2001 From: aaron Date: Thu, 14 May 2009 15:17:32 +0000 Subject: [PATCH] Added an adapter class, that will adapt plain iterators and closeable iterators of SAMRecords into STingSAMIterators. Also unit tests. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@697 348d0f76-0448-11de-a6fe-93d51630548a --- .../iterators/StingSAMIteratorAdapter.java | 111 +++++++++++++ .../StingSAMIteratorAdapterTest.java | 151 ++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100755 java/src/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapter.java create mode 100755 java/test/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapterTest.java diff --git a/java/src/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapter.java b/java/src/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapter.java new file mode 100755 index 000000000..724d39b2f --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapter.java @@ -0,0 +1,111 @@ +package org.broadinstitute.sting.gatk.iterators; + +import net.sf.samtools.SAMRecord; +import net.sf.samtools.util.CloseableIterator; + +import java.util.Iterator; + +/** + * + * User: aaron + * Date: May 13, 2009 + * Time: 6:33:15 PM + * + * The Broad Institute + * SOFTWARE COPYRIGHT NOTICE AGREEMENT + * This software and its documentation are copyright 2009 by the + * Broad Institute/Massachusetts Institute of Technology. All rights are reserved. + * + * This software is supplied without any warranty or guaranteed support whatsoever. Neither + * the Broad Institute nor MIT can be responsible for its use, misuse, or functionality. + * + */ + + +/** + * @author aaron + * @version 1.0 + * @date May 13, 2009 + *

+ * Class StingSAMIteratorAdapter + *

+ * This class adapts other SAMRecord iterators to the StingSAMIterator + */ +public class StingSAMIteratorAdapter { + + public static StingSAMIterator adapt(Iterator iter) { + return new PrivateStringSAMIterator(iter); + } + + public static StingSAMIterator adapt(CloseableIterator iter) { + return new PrivateStringSAMCloseableIterator(iter); + } + +} + + +/** + * this class wraps iterators in a StingSAMIterator, which means just adding the + * methods that implement the iterable<> interface and the close() method from CloseableIterator + */ +class PrivateStringSAMIterator implements StingSAMIterator { + private Iterator iter = null; + + PrivateStringSAMIterator(Iterator iter) { + this.iter = iter; + } + + public void close() { + // do nothing, we can't close the iterator anyway. + } + + public boolean hasNext() { + return iter.hasNext(); + } + + public SAMRecord next() { + return iter.next(); + } + + public void remove() { + throw new UnsupportedOperationException("StingSAMIterator's don't allow remove()ing"); + } + + public Iterator iterator() { + return iter; + } +} + + +/** + * this class wraps closeable iterators in a StingSAMIterator, which means adding the + * methods that implement the iterable<> interface. + */ +class PrivateStringSAMCloseableIterator implements StingSAMIterator { + private CloseableIterator iter = null; + + PrivateStringSAMCloseableIterator(CloseableIterator iter) { + this.iter = iter; + } + + public void close() { + iter.close(); + } + + public boolean hasNext() { + return iter.hasNext(); + } + + public SAMRecord next() { + return iter.next(); + } + + public void remove() { + throw new UnsupportedOperationException("StingSAMIterator's don't allow remove()ing"); + } + + public Iterator iterator() { + return iter; + } +} + diff --git a/java/test/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapterTest.java b/java/test/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapterTest.java new file mode 100755 index 000000000..5f022162b --- /dev/null +++ b/java/test/org/broadinstitute/sting/gatk/iterators/StingSAMIteratorAdapterTest.java @@ -0,0 +1,151 @@ +package org.broadinstitute.sting.gatk.iterators; + +import net.sf.samtools.SAMRecord; +import net.sf.samtools.util.CloseableIterator; +import org.broadinstitute.sting.BaseTest; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import java.util.Iterator; + +/** + * + * User: aaron + * Date: May 13, 2009 + * Time: 6:58:21 PM + * + * The Broad Institute + * SOFTWARE COPYRIGHT NOTICE AGREEMENT + * This software and its documentation are copyright 2009 by the + * Broad Institute/Massachusetts Institute of Technology. All rights are reserved. + * + * This software is supplied without any warranty or guaranteed support whatsoever. Neither + * the Broad Institute nor MIT can be responsible for its use, misuse, or functionality. + * + */ + + +/** + * @author aaron + * @version 1.0 + * @date May 13, 2009 + *

+ * Class StingSAMIteratorTest + *

+ * Tests the StingSAMIteratorAdapter class. + */ +public class StingSAMIteratorAdapterTest extends BaseTest { + + class MyTestIterator implements Iterator { + + public int count = 0; + + public MyTestIterator() { + count = 0; + } + + public boolean hasNext() { + if (count < 100) { + ++count; + return true; + } else { + return false; + } + } + + public SAMRecord next() { + return null; + } + + public void remove() { + throw new UnsupportedOperationException("Unsupported"); + } + } + + class MyTestCloseableIterator implements CloseableIterator { + public int count = 0; + + public MyTestCloseableIterator() { + count = 0; + } + + public boolean hasNext() { + if (count < 100) { + ++count; + return true; + } else { + return false; + } + } + + public SAMRecord next() { + return null; + } + + public void remove() { + throw new UnsupportedOperationException("Unsupported"); + } + + public void close() { + count = -1; + } + } + + + @Test + public void testNormalIterator() { + final int COUNT = 100; + MyTestIterator it = new MyTestIterator(); + + StingSAMIterator samIt = StingSAMIteratorAdapter.adapt(it); + int countCheck = 0; + while (samIt.hasNext()) { + samIt.next(); + ++countCheck; + //logger.warn("cnt = " + countCheck); + } + + assertEquals(COUNT, countCheck); + + assertEquals(COUNT, countCheck); + } + + @Test + public void testCloseableIterator() { + final int COUNT = 100; + + MyTestCloseableIterator it = new MyTestCloseableIterator(); + + StingSAMIterator samIt = StingSAMIteratorAdapter.adapt(it); + + int countCheck = 0; + while (samIt.hasNext()) { + samIt.next(); + ++countCheck; + } + + assertEquals(COUNT, countCheck); + } + + @Test + public void testCloseOnCloseableIterator() { + final int COUNT = 100; + + MyTestCloseableIterator it = new MyTestCloseableIterator(); + + StingSAMIterator samIt = StingSAMIteratorAdapter.adapt(it); + + + int countCheck = 0; + while (samIt.hasNext()) { + samIt.next(); + ++countCheck; + } + + assertEquals(COUNT, countCheck); + + // check to see that the count get's set to -1 + samIt.close(); + assertEquals(-1, it.count); + } +}