From 553d39bb00886b0ad8ac4463a5319d4e8a9f6886 Mon Sep 17 00:00:00 2001 From: hanna Date: Thu, 25 Feb 2010 01:20:22 +0000 Subject: [PATCH] Clean up the code a bit following the introduction of reduceByInterval. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2887 348d0f76-0448-11de-a6fe-93d51630548a --- .../datasources/providers/AllLocusView.java | 2 +- .../IndexDelimitedLocusShardStrategy.java | 15 +----- .../gatk/iterators/GenomeLocusIterator.java | 46 +++++-------------- .../providers/LocusReferenceViewTest.java | 4 +- 4 files changed, 16 insertions(+), 51 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/datasources/providers/AllLocusView.java b/java/src/org/broadinstitute/sting/gatk/datasources/providers/AllLocusView.java index d4978373a..d978e77f2 100755 --- a/java/src/org/broadinstitute/sting/gatk/datasources/providers/AllLocusView.java +++ b/java/src/org/broadinstitute/sting/gatk/datasources/providers/AllLocusView.java @@ -47,7 +47,7 @@ public class AllLocusView extends LocusView { public AllLocusView(ShardDataProvider provider) { super( provider ); // Seed the state tracking members with the first possible seek position and the first possible locus context. - locusIterator = new GenomeLocusIterator( Collections.singletonList(provider.getLocus()) ); + locusIterator = new GenomeLocusIterator(provider.getLocus()); if( locusIterator.hasNext() ) { nextPosition = locusIterator.next(); nextLocus = hasNextLocus() ? nextLocus() : createEmptyLocus(nextPosition); diff --git a/java/src/org/broadinstitute/sting/gatk/datasources/shards/IndexDelimitedLocusShardStrategy.java b/java/src/org/broadinstitute/sting/gatk/datasources/shards/IndexDelimitedLocusShardStrategy.java index cadcb8b32..266887914 100755 --- a/java/src/org/broadinstitute/sting/gatk/datasources/shards/IndexDelimitedLocusShardStrategy.java +++ b/java/src/org/broadinstitute/sting/gatk/datasources/shards/IndexDelimitedLocusShardStrategy.java @@ -66,20 +66,7 @@ public class IndexDelimitedLocusShardStrategy implements ShardStrategy { throw new StingException("Cannot power an IndexDelimitedLocusShardStrategy with this data source."); blockDrivenDataSource = (BlockDrivenSAMDataSource)dataSource; - - // Create a list of contig name -> genome loc, sorted in INSERTION ORDER. - LinkedHashMap> locationToReference = new LinkedHashMap>(); - for(GenomeLoc location: locations) { - if(!locationToReference.containsKey(location.getContig())) - locationToReference.put(location.getContig(),new ArrayList()); - locationToReference.get(location.getContig()).add(location); - } - - // TODO: Not sure there's any reason to pre-separate the contigs now that we're using a streaming approach to file pointer allocation. - for(String contig: locationToReference.keySet()) { - filePointers.addAll(batchLociIntoBins(locationToReference.get(contig),blockDrivenDataSource.getNumIndexLevels()-1)); - } - + filePointers.addAll(batchLociIntoBins(locations.toList(),blockDrivenDataSource.getNumIndexLevels()-1)); filePointerIterator = filePointers.iterator(); } diff --git a/java/src/org/broadinstitute/sting/gatk/iterators/GenomeLocusIterator.java b/java/src/org/broadinstitute/sting/gatk/iterators/GenomeLocusIterator.java index 009b371ed..a078d9d4e 100755 --- a/java/src/org/broadinstitute/sting/gatk/iterators/GenomeLocusIterator.java +++ b/java/src/org/broadinstitute/sting/gatk/iterators/GenomeLocusIterator.java @@ -5,7 +5,6 @@ import org.broadinstitute.sting.utils.GenomeLocParser; import java.util.NoSuchElementException; import java.util.Iterator; -import java.util.List; /** * User: hanna * Date: May 12, 2009 @@ -24,28 +23,24 @@ import java.util.List; */ public class GenomeLocusIterator implements Iterator { /** - * An iterator to the entire data structure over which we're iterating. + * The entire region over which we're iterating. */ - private final Iterator locusIterator; + private GenomeLoc completeLocus; /** - * The multi-base pair long locus referring to the current locus. + * The current position in the traversal. */ - private GenomeLoc currentLocus = null; - - /** - * The 1 base pair long location. - */ - private GenomeLoc currentLocation = null; + private GenomeLoc currentLocus; /** * Creates an iterator that can traverse over the entire * reference specified in the given ShardDataProvider. - * @param loci the list of loci over which to iterate. + * @param completeLocus Data provider to use as a backing source. + * Provider must have a reference (hasReference() == true). */ - public GenomeLocusIterator( List loci ) { - this.locusIterator = loci.iterator(); - seedNextLocus(); + public GenomeLocusIterator( GenomeLoc completeLocus ) { + this.completeLocus = completeLocus; + this.currentLocus = GenomeLocParser.createGenomeLoc(completeLocus.getContig(),completeLocus.getStart()); } /** @@ -53,7 +48,7 @@ public class GenomeLocusIterator implements Iterator { * @return True if the iterator has more elements. False otherwise. */ public boolean hasNext() { - return currentLocation != null; + return !currentLocus.isPast(completeLocus); } /** @@ -63,29 +58,12 @@ public class GenomeLocusIterator implements Iterator { public GenomeLoc next() { if( !hasNext() ) throw new NoSuchElementException("No elements remaining in bounded reference region."); - GenomeLoc toReturn = currentLocation.clone(); - seedNextLocus(); + GenomeLoc toReturn = (GenomeLoc)currentLocus.clone(); + currentLocus = GenomeLocParser.incPos(currentLocus); return toReturn; } public void remove() { throw new UnsupportedOperationException( "ReferenceLocusIterator is read-only" ); } - - /** - * Position currentLocation at the next locus, if possible. - */ - private void seedNextLocus() { - if(currentLocus != null && currentLocation != null) - currentLocation = GenomeLocParser.incPos(currentLocation); - - // If initializing or the location was pushed off the current locus, reinitialize using the next locus. - if(currentLocus == null || currentLocation == null || currentLocation.isPast(currentLocus)) { - currentLocus = currentLocation = null; - if(locusIterator.hasNext()){ - currentLocus = locusIterator.next(); - currentLocation = GenomeLocParser.createGenomeLoc(currentLocus.getContig(),currentLocus.getStart()); - } - } - } } diff --git a/java/test/org/broadinstitute/sting/gatk/datasources/providers/LocusReferenceViewTest.java b/java/test/org/broadinstitute/sting/gatk/datasources/providers/LocusReferenceViewTest.java index c161f444e..2e7b7b646 100755 --- a/java/test/org/broadinstitute/sting/gatk/datasources/providers/LocusReferenceViewTest.java +++ b/java/test/org/broadinstitute/sting/gatk/datasources/providers/LocusReferenceViewTest.java @@ -89,9 +89,9 @@ public class LocusReferenceViewTest extends ReferenceViewTemplate { */ protected void validateLocation( GenomeLoc loc ) { Shard shard = new LocusShard(Collections.singletonList(loc)); - GenomeLocusIterator shardIterator = new GenomeLocusIterator(shard.getGenomeLocs()); + GenomeLocusIterator shardIterator = new GenomeLocusIterator(loc); - ShardDataProvider dataProvider = new ShardDataProvider(shard, shard.getGenomeLocs().get(0), null, sequenceFile, null); + ShardDataProvider dataProvider = new ShardDataProvider(shard, loc, null, sequenceFile, null); LocusReferenceView view = new LocusReferenceView(dataProvider); while (shardIterator.hasNext()) {