Cleanup RefMetaDataTracker before refactoring ReadMetaDataTracker

This commit is contained in:
Mark DePristo 2012-08-29 09:25:05 -04:00
parent b85ded8389
commit 8fc6a0a68b
6 changed files with 44 additions and 47 deletions

View File

@ -117,6 +117,15 @@ public final class RodBinding<T extends Feature> {
this.bound = true; this.bound = true;
} }
/**
* For testing purposes only. Creates a RodBinding sufficient for looking up associations to rawName
* @param type
* @param rawName
*/
public RodBinding(Class<T> type, final String rawName) {
this(type, rawName, "missing", type.getSimpleName(), new Tags());
}
/** /**
* Make an unbound RodBinding<T>. Only available for creating the globally unique UNBOUND object * Make an unbound RodBinding<T>. Only available for creating the globally unique UNBOUND object
* @param type class this unbound RodBinding creates * @param type class this unbound RodBinding creates

View File

@ -58,7 +58,7 @@ public class ManagingReferenceOrderedView implements ReferenceOrderedView {
// todo -- warning, I removed the reference to the name from states // todo -- warning, I removed the reference to the name from states
bindings.add( state.iterator.seekForward(loc) ); bindings.add( state.iterator.seekForward(loc) );
return new RefMetaDataTracker(bindings, referenceContext); return new RefMetaDataTracker(bindings);
} }
/** /**

View File

@ -101,7 +101,7 @@ public class RodLocusView extends LocusView implements ReferenceOrderedView {
public RefMetaDataTracker getReferenceOrderedDataAtLocus( GenomeLoc loc, ReferenceContext referenceContext ) { public RefMetaDataTracker getReferenceOrderedDataAtLocus( GenomeLoc loc, ReferenceContext referenceContext ) {
// special case the interval again -- add it into the ROD // special case the interval again -- add it into the ROD
if ( interval != null ) { allTracksHere.add(interval); } if ( interval != null ) { allTracksHere.add(interval); }
return new RefMetaDataTracker(allTracksHere, referenceContext); return new RefMetaDataTracker(allTracksHere);
} }
public boolean hasNext() { public boolean hasNext() {

View File

@ -5,7 +5,6 @@ import com.google.java.contract.Requires;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.broad.tribble.Feature; import org.broad.tribble.Feature;
import org.broadinstitute.sting.commandline.RodBinding; import org.broadinstitute.sting.commandline.RodBinding;
import org.broadinstitute.sting.gatk.contexts.ReferenceContext;
import org.broadinstitute.sting.gatk.refdata.utils.GATKFeature; import org.broadinstitute.sting.gatk.refdata.utils.GATKFeature;
import org.broadinstitute.sting.gatk.refdata.utils.RODRecordList; import org.broadinstitute.sting.gatk.refdata.utils.RODRecordList;
import org.broadinstitute.sting.utils.GenomeLoc; import org.broadinstitute.sting.utils.GenomeLoc;
@ -32,11 +31,10 @@ import java.util.*;
* Time: 3:05:23 PM * Time: 3:05:23 PM
*/ */
public class RefMetaDataTracker { public class RefMetaDataTracker {
// TODO: this should be a list, not a map, actually // TODO: this should be a list, not a bindings, actually
private final static RODRecordList EMPTY_ROD_RECORD_LIST = new RODRecordListImpl("EMPTY"); private final static RODRecordList EMPTY_ROD_RECORD_LIST = new RODRecordListImpl("EMPTY");
final Map<String, RODRecordList> map; final Map<String, RODRecordList> bindings;
final ReferenceContext ref;
final protected static Logger logger = Logger.getLogger(RefMetaDataTracker.class); final protected static Logger logger = Logger.getLogger(RefMetaDataTracker.class);
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
@ -48,28 +46,25 @@ public class RefMetaDataTracker {
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
/** /**
* Only for testing -- not accesssible in any other context * Create an tracker with no bindings
*/ */
public RefMetaDataTracker() { public RefMetaDataTracker() {
ref = null; bindings = Collections.emptyMap();
map = Collections.emptyMap();
} }
public RefMetaDataTracker(final Collection<RODRecordList> allBindings, final ReferenceContext ref) { public RefMetaDataTracker(final Collection<RODRecordList> allBindings) {
this.ref = ref; // set up the bindings
// set up the map
if ( allBindings.isEmpty() ) if ( allBindings.isEmpty() )
map = Collections.emptyMap(); bindings = Collections.emptyMap();
else { else {
Map<String, RODRecordList> tmap = new HashMap<String, RODRecordList>(allBindings.size()); final Map<String, RODRecordList> tmap = new HashMap<String, RODRecordList>(allBindings.size());
for ( RODRecordList rod : allBindings ) { for ( RODRecordList rod : allBindings ) {
if ( rod != null && ! rod.isEmpty() ) if ( rod != null && ! rod.isEmpty() )
tmap.put(canonicalName(rod.getName()), rod); tmap.put(canonicalName(rod.getName()), rod);
} }
// ensure that no one modifies the map itself // ensure that no one modifies the bindings itself
map = Collections.unmodifiableMap(tmap); bindings = Collections.unmodifiableMap(tmap);
} }
} }
@ -99,7 +94,7 @@ public class RefMetaDataTracker {
@Requires({"type != null"}) @Requires({"type != null"})
@Ensures("result != null") @Ensures("result != null")
public <T extends Feature> List<T> getValues(final Class<T> type) { public <T extends Feature> List<T> getValues(final Class<T> type) {
return addValues(map.keySet(), type, new ArrayList<T>(), null, false, false); return addValues(bindings.keySet(), type, new ArrayList<T>(), null, false, false);
} }
/** /**
@ -114,7 +109,7 @@ public class RefMetaDataTracker {
@Requires({"type != null", "onlyAtThisLoc != null"}) @Requires({"type != null", "onlyAtThisLoc != null"})
@Ensures("result != null") @Ensures("result != null")
public <T extends Feature> List<T> getValues(final Class<T> type, final GenomeLoc onlyAtThisLoc) { public <T extends Feature> List<T> getValues(final Class<T> type, final GenomeLoc onlyAtThisLoc) {
return addValues(map.keySet(), type, new ArrayList<T>(), onlyAtThisLoc, true, false); return addValues(bindings.keySet(), type, new ArrayList<T>(), onlyAtThisLoc, true, false);
} }
/** /**
@ -296,7 +291,7 @@ public class RefMetaDataTracker {
*/ */
@Requires({"rodBinding != null"}) @Requires({"rodBinding != null"})
public boolean hasValues(final RodBinding rodBinding) { public boolean hasValues(final RodBinding rodBinding) {
return map.containsKey(canonicalName(rodBinding.getName())); return bindings.containsKey(canonicalName(rodBinding.getName()));
} }
/** /**
@ -306,7 +301,7 @@ public class RefMetaDataTracker {
* @return List of all tracks * @return List of all tracks
*/ */
public List<RODRecordList> getBoundRodTracks() { public List<RODRecordList> getBoundRodTracks() {
return new ArrayList<RODRecordList>(map.values()); return new ArrayList<RODRecordList>(bindings.values());
} }
/** /**
@ -314,38 +309,30 @@ public class RefMetaDataTracker {
* @return the number of tracks with at least one bound Feature * @return the number of tracks with at least one bound Feature
*/ */
public int getNTracksWithBoundFeatures() { public int getNTracksWithBoundFeatures() {
return map.size(); return bindings.size();
} }
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
// // Protected accessors using strings for unit testing
//
// old style accessors
//
// TODO -- DELETE ME
//
//
// ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------
@Deprecated protected boolean hasValues(final String name) {
public boolean hasValues(final String name) { return bindings.containsKey(canonicalName(name));
return map.containsKey(canonicalName(name));
} }
@Deprecated protected <T extends Feature> List<T> getValues(final Class<T> type, final String name) {
public <T extends Feature> List<T> getValues(final Class<T> type, final String name) {
return addValues(name, type, new ArrayList<T>(), getTrackDataByName(name), null, false, false); return addValues(name, type, new ArrayList<T>(), getTrackDataByName(name), null, false, false);
} }
@Deprecated
public <T extends Feature> List<T> getValues(final Class<T> type, final String name, final GenomeLoc onlyAtThisLoc) { protected <T extends Feature> List<T> getValues(final Class<T> type, final String name, final GenomeLoc onlyAtThisLoc) {
return addValues(name, type, new ArrayList<T>(), getTrackDataByName(name), onlyAtThisLoc, true, false); return addValues(name, type, new ArrayList<T>(), getTrackDataByName(name), onlyAtThisLoc, true, false);
} }
@Deprecated
public <T extends Feature> T getFirstValue(final Class<T> type, final String name) { protected <T extends Feature> T getFirstValue(final Class<T> type, final String name) {
return safeGetFirst(getValues(type, name)); return safeGetFirst(getValues(type, name));
} }
@Deprecated
public <T extends Feature> T getFirstValue(final Class<T> type, final String name, final GenomeLoc onlyAtThisLoc) { protected <T extends Feature> T getFirstValue(final Class<T> type, final String name, final GenomeLoc onlyAtThisLoc) {
return safeGetFirst(getValues(type, name, onlyAtThisLoc)); return safeGetFirst(getValues(type, name, onlyAtThisLoc));
} }
@ -366,7 +353,7 @@ public class RefMetaDataTracker {
* @return * @return
*/ */
@Requires({"l != null"}) @Requires({"l != null"})
final private <T extends Feature> T safeGetFirst(final List<T> l) { private <T extends Feature> T safeGetFirst(final List<T> l) {
return l.isEmpty() ? null : l.get(0); return l.isEmpty() ? null : l.get(0);
} }
@ -435,7 +422,7 @@ public class RefMetaDataTracker {
*/ */
private RODRecordList getTrackDataByName(final String name) { private RODRecordList getTrackDataByName(final String name) {
final String luName = canonicalName(name); final String luName = canonicalName(name);
RODRecordList l = map.get(luName); RODRecordList l = bindings.get(luName);
return l == null ? EMPTY_ROD_RECORD_LIST : l; return l == null ? EMPTY_ROD_RECORD_LIST : l;
} }
@ -448,7 +435,7 @@ public class RefMetaDataTracker {
* @param name the name of the rod * @param name the name of the rod
* @return canonical name of the rod * @return canonical name of the rod
*/ */
private final String canonicalName(final String name) { private String canonicalName(final String name) {
// todo -- remove me after switch to RodBinding syntax // todo -- remove me after switch to RodBinding syntax
return name.toLowerCase(); return name.toLowerCase();
} }

View File

@ -1,6 +1,7 @@
package org.broadinstitute.sting.gatk.datasources.providers; package org.broadinstitute.sting.gatk.datasources.providers;
import org.broad.tribble.Feature; import org.broad.tribble.Feature;
import org.broadinstitute.sting.commandline.RodBinding;
import org.broadinstitute.sting.commandline.Tags; import org.broadinstitute.sting.commandline.Tags;
import org.broadinstitute.sting.gatk.datasources.reads.MockLocusShard; import org.broadinstitute.sting.gatk.datasources.reads.MockLocusShard;
import org.broadinstitute.sting.gatk.datasources.rmd.ReferenceOrderedDataSource; import org.broadinstitute.sting.gatk.datasources.rmd.ReferenceOrderedDataSource;
@ -89,7 +90,7 @@ public class ReferenceOrderedViewUnitTest extends BaseTest {
ReferenceOrderedView view = new ManagingReferenceOrderedView( provider ); ReferenceOrderedView view = new ManagingReferenceOrderedView( provider );
RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(genomeLocParser.createGenomeLoc("chrM",20), null); RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(genomeLocParser.createGenomeLoc("chrM",20), null);
TableFeature datum = tracker.getFirstValue(TableFeature.class, "tableTest"); TableFeature datum = tracker.getFirstValue(new RodBinding<TableFeature>(TableFeature.class, "tableTest"));
Assert.assertEquals(datum.get("COL1"),"C","datum parameter for COL1 is incorrect"); Assert.assertEquals(datum.get("COL1"),"C","datum parameter for COL1 is incorrect");
Assert.assertEquals(datum.get("COL2"),"D","datum parameter for COL2 is incorrect"); Assert.assertEquals(datum.get("COL2"),"D","datum parameter for COL2 is incorrect");
@ -115,13 +116,13 @@ public class ReferenceOrderedViewUnitTest extends BaseTest {
ReferenceOrderedView view = new ManagingReferenceOrderedView( provider ); ReferenceOrderedView view = new ManagingReferenceOrderedView( provider );
RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(genomeLocParser.createGenomeLoc("chrM",20), null); RefMetaDataTracker tracker = view.getReferenceOrderedDataAtLocus(genomeLocParser.createGenomeLoc("chrM",20), null);
TableFeature datum1 = tracker.getFirstValue(TableFeature.class, "tableTest1"); TableFeature datum1 = tracker.getFirstValue(new RodBinding<TableFeature>(TableFeature.class, "tableTest1"));
Assert.assertEquals(datum1.get("COL1"),"C","datum1 parameter for COL1 is incorrect"); Assert.assertEquals(datum1.get("COL1"),"C","datum1 parameter for COL1 is incorrect");
Assert.assertEquals(datum1.get("COL2"),"D","datum1 parameter for COL2 is incorrect"); Assert.assertEquals(datum1.get("COL2"),"D","datum1 parameter for COL2 is incorrect");
Assert.assertEquals(datum1.get("COL3"),"E","datum1 parameter for COL3 is incorrect"); Assert.assertEquals(datum1.get("COL3"),"E","datum1 parameter for COL3 is incorrect");
TableFeature datum2 = tracker.getFirstValue(TableFeature.class, "tableTest2"); TableFeature datum2 = tracker.getFirstValue(new RodBinding<TableFeature>(TableFeature.class, "tableTest2"));
Assert.assertEquals(datum2.get("COL1"),"C","datum2 parameter for COL1 is incorrect"); Assert.assertEquals(datum2.get("COL1"),"C","datum2 parameter for COL1 is incorrect");
Assert.assertEquals(datum2.get("COL2"),"D","datum2 parameter for COL2 is incorrect"); Assert.assertEquals(datum2.get("COL2"),"D","datum2 parameter for COL2 is incorrect");

View File

@ -133,7 +133,7 @@ public class RefMetaDataTrackerUnitTest {
List<RODRecordList> x = new ArrayList<RODRecordList>(); List<RODRecordList> x = new ArrayList<RODRecordList>();
if ( AValues != null ) x.add(AValues); if ( AValues != null ) x.add(AValues);
if ( BValues != null ) x.add(BValues); if ( BValues != null ) x.add(BValues);
return new RefMetaDataTracker(x, context); return new RefMetaDataTracker(x);
} }
public int nBoundTracks() { public int nBoundTracks() {