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
This commit is contained in:
parent
5dda448ae0
commit
0215905bb6
|
|
@ -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
|
||||||
|
* <p/>
|
||||||
|
* Class StingSAMIteratorAdapter
|
||||||
|
* <p/>
|
||||||
|
* This class adapts other SAMRecord iterators to the StingSAMIterator
|
||||||
|
*/
|
||||||
|
public class StingSAMIteratorAdapter {
|
||||||
|
|
||||||
|
public static StingSAMIterator adapt(Iterator<SAMRecord> iter) {
|
||||||
|
return new PrivateStringSAMIterator(iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StingSAMIterator adapt(CloseableIterator<SAMRecord> iter) {
|
||||||
|
return new PrivateStringSAMCloseableIterator(iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this class wraps iterators<SAMRecord> 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<SAMRecord> iter = null;
|
||||||
|
|
||||||
|
PrivateStringSAMIterator(Iterator<SAMRecord> 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<SAMRecord> iterator() {
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this class wraps closeable iterators<SAMRecord> in a StingSAMIterator, which means adding the
|
||||||
|
* methods that implement the iterable<> interface.
|
||||||
|
*/
|
||||||
|
class PrivateStringSAMCloseableIterator implements StingSAMIterator {
|
||||||
|
private CloseableIterator<SAMRecord> iter = null;
|
||||||
|
|
||||||
|
PrivateStringSAMCloseableIterator(CloseableIterator<SAMRecord> 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<SAMRecord> iterator() {
|
||||||
|
return iter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
|
* <p/>
|
||||||
|
* Class StingSAMIteratorTest
|
||||||
|
* <p/>
|
||||||
|
* Tests the StingSAMIteratorAdapter class.
|
||||||
|
*/
|
||||||
|
public class StingSAMIteratorAdapterTest extends BaseTest {
|
||||||
|
|
||||||
|
class MyTestIterator implements Iterator<SAMRecord> {
|
||||||
|
|
||||||
|
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<SAMRecord> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue