GenotypeMap contains a Map, not extends it

-- On path to replacing it with GenotypeCollection
This commit is contained in:
Mark DePristo 2011-11-14 12:55:03 -05:00
parent fee9b367e4
commit 79987d685c
2 changed files with 132 additions and 7 deletions

View File

@ -24,21 +24,46 @@
package org.broadinstitute.sting.utils.variantcontext;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;
/**
*
*/
public class GenotypeMap extends TreeMap<String, Genotype> implements Map<String, Genotype> {
public class GenotypeMap implements Map<String, Genotype> {
final TreeMap<String, Genotype> genotypes;
boolean immutable = false;
public final static GenotypeMap NO_GENOTYPES = new GenotypeMap();
// ---------------------------------------------------------------------------
//
// private constructors -- you have to use static create methods to make these classes
//
// ---------------------------------------------------------------------------
private GenotypeMap() {
this(false);
}
private GenotypeMap(boolean immutable) {
this(new TreeMap<String, Genotype>(), immutable);
}
private GenotypeMap(final TreeMap<String, Genotype> genotypes, final boolean immutable) {
this.genotypes = genotypes;
this.immutable = immutable;
}
// ---------------------------------------------------------------------------
//
// public static factory methods
//
// ---------------------------------------------------------------------------
public static final GenotypeMap create() {
return new GenotypeMap();
}
public static final GenotypeMap create(int nGenotypes) {
public static final GenotypeMap create(final int nGenotypes) {
return new GenotypeMap();
}
@ -46,6 +71,9 @@ public class GenotypeMap extends TreeMap<String, Genotype> implements Map<String
return create(genotypes.values());
}
// todo -- differentiate between empty constructor and copy constructor
// todo -- create constructor (Genotype ... genotypes)
public static final GenotypeMap create(final Map<String, Genotype> genotypes) {
return create(genotypes.values());
}
@ -54,13 +82,110 @@ public class GenotypeMap extends TreeMap<String, Genotype> implements Map<String
if ( genotypes == null )
return null; // todo -- really should return an empty map
else {
GenotypeMap genotypeMap = new GenotypeMap();
GenotypeMap genotypeMap = new GenotypeMap().mutable();
for ( final Genotype g : genotypes ) {
if ( genotypeMap.containsKey(g.getSampleName() ) )
throw new IllegalArgumentException("Duplicate genotype added to VariantContext: " + g);
genotypeMap.put(g.getSampleName(), g);
}
//return genotypeMap.immutable(); // todo enable when we have time to dive into mutability issue
return genotypeMap;
}
}
// ---------------------------------------------------------------------------
//
// Mutability methods
//
// ---------------------------------------------------------------------------
public final GenotypeMap mutable() {
immutable = false;
return this;
}
public final GenotypeMap immutable() {
immutable = true;
return this;
}
public boolean isMutable() {
return ! immutable;
}
public final void checkImmutability() {
if ( immutable )
throw new IllegalAccessError("GenotypeMap is currently immutable, but a mutator method was invoked on it");
}
// ---------------------------------------------------------------------------
//
// Map methods
//
// ---------------------------------------------------------------------------
@Override
public void clear() {
checkImmutability();
genotypes.clear();
}
@Override
public int size() {
return genotypes.size();
}
@Override
public boolean isEmpty() {
return genotypes.isEmpty();
}
@Override
public boolean containsKey(final Object o) {
return genotypes.containsKey(o);
}
@Override
public boolean containsValue(final Object o) {
return genotypes.containsValue(o);
}
@Override
public Genotype get(final Object o) {
return genotypes.get(o);
}
@Override
public Genotype put(final String s, final Genotype genotype) {
checkImmutability();
return genotypes.put(s, genotype);
}
@Override
public Genotype remove(final Object o) {
checkImmutability();
return genotypes.remove(o);
}
@Override
public void putAll(final Map<? extends String, ? extends Genotype> map) {
checkImmutability();
genotypes.putAll(map);
}
@Override
public Set<String> keySet() {
return Collections.unmodifiableSet(genotypes.keySet());
}
@Override
public Collection<Genotype> values() {
return genotypes.values();
}
@Override
public Set<Entry<String, Genotype>> entrySet() {
return genotypes.entrySet();
}
}

View File

@ -46,7 +46,7 @@ public class UnifiedGenotyperIntegrationTest extends WalkerTest {
public void testWithAllelesPassedIn2() {
WalkerTest.WalkerTestSpec spec2 = new WalkerTest.WalkerTestSpec(
baseCommand + " --output_mode EMIT_ALL_SITES --genotyping_mode GENOTYPE_GIVEN_ALLELES -alleles " + validationDataLocation + "allelesForUG.vcf -I " + validationDataLocation + "pilot2_daughters.chr20.10k-11k.bam -o %s -L 20:10,000,000-10,025,000", 1,
Arrays.asList("14f5cdfc6818cbba600cbdf5fe285275"));
Arrays.asList("9834f0cef1cd6ba4943a5aaee1ee8be8"));
executeTest("test MultiSample Pilot2 with alleles passed in and emitting all sites", spec2);
}