diff --git a/java/src/org/broadinstitute/sting/gatk/dataSources/simpleDataSources/ReferenceDataSource.java b/java/src/org/broadinstitute/sting/gatk/dataSources/simpleDataSources/ReferenceDataSource.java index 292caf010..ccde116d4 100644 --- a/java/src/org/broadinstitute/sting/gatk/dataSources/simpleDataSources/ReferenceDataSource.java +++ b/java/src/org/broadinstitute/sting/gatk/dataSources/simpleDataSources/ReferenceDataSource.java @@ -1,5 +1,6 @@ package org.broadinstitute.sting.gatk.dataSources.simpleDataSources; +import org.broadinstitute.sting.gatk.iterators.BoundedReferenceIterator; import org.broadinstitute.sting.gatk.iterators.ReferenceIterator; import org.broadinstitute.sting.utils.FastaSequenceFile2; import org.broadinstitute.sting.utils.GenomeLoc; @@ -44,17 +45,11 @@ public class ReferenceDataSource implements SimpleDataSource { * @param location the genome location to extract data for * @return an iterator of the appropriate type, that is limited by the region */ - public ReferenceIterator seek(GenomeLoc location) { - ReferenceIterator refSite = refIter.seekForward(location); - return refSite; + public BoundedReferenceIterator seek(GenomeLoc location) { + BoundedReferenceIterator ret = new BoundedReferenceIterator(refIter.seekForward(location), location); + return ret; } - /** - * Constructor - ReferenceDataSource - * - * @param refFileName the reference file - * @throws SimpleDataSourceLoadException - */ public ReferenceDataSource(String refFileName) throws SimpleDataSourceLoadException { if (refFileName == null) { throw new SimpleDataSourceLoadException("ReferenceDataSource: refFileName passed in is null"); @@ -63,6 +58,7 @@ public class ReferenceDataSource implements SimpleDataSource { if (!infile.canRead()) { throw new SimpleDataSourceLoadException("ReferenceDataSource: Unable to load file: " + refFileName); } + //this.refFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(refFileName); refFile = new FastaSequenceFile2(new File(refFileName)); refIter = new ReferenceIterator(this.refFile); diff --git a/java/src/org/broadinstitute/sting/gatk/iterators/BoundedReferenceIterator.java b/java/src/org/broadinstitute/sting/gatk/iterators/BoundedReferenceIterator.java new file mode 100644 index 000000000..b7c316cad --- /dev/null +++ b/java/src/org/broadinstitute/sting/gatk/iterators/BoundedReferenceIterator.java @@ -0,0 +1,114 @@ +package org.broadinstitute.sting.gatk.iterators; + +import org.broadinstitute.sting.utils.GenomeLoc; + +import java.util.Iterator; + +/** + * + * User: aaron + * Date: Apr 2, 2009 + * Time: 2:12:12 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 Apr 2, 2009 + *

+ * Class EnclosedIterator + *

+ * This class is a decorator class from Reference Iterator (though it is constrained + * by the fact that referenceIterator.seekForwardOffset explicitly returns a referenceIterator + * for now + *

+ * TODO: Fix the underlying iterator and this class to model a real decorator pattern + */ +public class BoundedReferenceIterator implements Iterator { + // the location to screen over + private final GenomeLoc mLoc; + private final ReferenceIterator referenceIterator; + + /** + * Default constructor + * + * @param referenceIterator + * @param loc + */ + public BoundedReferenceIterator(ReferenceIterator referenceIterator, GenomeLoc loc) { + this.referenceIterator = referenceIterator; + this.mLoc = loc; + } + + + /** + * isSubRegion + *

+ * returns true if we include the whole passed in region + * + * @param loc the genome region to check + * @return true if we include THE WHOLE specified region + */ + protected boolean isSubRegion(GenomeLoc loc) { + // if the location is null, we assume we're all inclusive (we represent the whole genome). + if (mLoc == null || loc.isBetween(mLoc, mLoc)) { + return true; + } + return false; + } + + /** + * returns true if we include the whole passed in region + * + * @param contig + * @param start + * @param stop + * @return true if we enclose the passed region, false otherwise + */ + protected boolean isSubRegion(final String contig, final int start, final int stop) { + final GenomeLoc lc = new GenomeLoc(contig, start, stop); + return isSubRegion(lc); + } + + /** + * If we're less then the limiting genomeLoc + * + * @param loc + * @return + */ + protected boolean isLessThan(GenomeLoc loc) { + return loc.isPast(mLoc); + } + + + // our adapted next function + public boolean hasNext() { + // first check that we are within the search place + GenomeLoc loc = referenceIterator.getLocation(); + if (!isSubRegion(loc)) { + return false; + } + + return referenceIterator.hasNext(); + } + + public ReferenceIterator next() { + return referenceIterator.next(); + } + + public void remove() { + referenceIterator.remove(); + } + + +}