diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/annotator/AlleleBalanceBySample.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/annotator/AlleleBalanceBySample.java index 75c4037d5..820fd248a 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/annotator/AlleleBalanceBySample.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/annotator/AlleleBalanceBySample.java @@ -47,7 +47,7 @@ public class AlleleBalanceBySample extends GenotypeAnnotation implements Experim if (!g.isHet()) return null; - Set altAlleles = vc.getAlternateAlleles(); + Collection altAlleles = vc.getAlternateAlleles(); if ( altAlleles.size() == 0 ) return null; diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/AlleleFrequencyCalculationModel.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/AlleleFrequencyCalculationModel.java index 70f3c6a1a..1bb697056 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/AlleleFrequencyCalculationModel.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/AlleleFrequencyCalculationModel.java @@ -33,6 +33,7 @@ import org.broadinstitute.sting.utils.variantcontext.Genotype; import org.broadinstitute.sting.utils.variantcontext.VariantContext; import java.io.PrintStream; +import java.util.List; import java.util.Map; import java.util.Set; @@ -76,7 +77,7 @@ public abstract class AlleleFrequencyCalculationModel implements Cloneable { */ protected abstract void getLog10PNonRef(RefMetaDataTracker tracker, ReferenceContext ref, - Map GLs, Set Alleles, + Map GLs, List Alleles, double[] log10AlleleFrequencyPriors, double[] log10AlleleFrequencyPosteriors); diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ExactAFCalculationModel.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ExactAFCalculationModel.java index 8a3a97823..f87eae781 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ExactAFCalculationModel.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/ExactAFCalculationModel.java @@ -29,19 +29,14 @@ import org.apache.log4j.Logger; import org.broadinstitute.sting.gatk.contexts.ReferenceContext; import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; import org.broadinstitute.sting.utils.MathUtils; -import org.broadinstitute.sting.utils.SimpleTimer; import org.broadinstitute.sting.utils.Utils; import org.broadinstitute.sting.utils.exceptions.UserException; import org.broadinstitute.sting.utils.variantcontext.Allele; import org.broadinstitute.sting.utils.variantcontext.Genotype; import org.broadinstitute.sting.utils.variantcontext.VariantContext; -import sun.reflect.generics.reflectiveObjects.NotImplementedException; import java.io.PrintStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +import java.util.*; public class ExactAFCalculationModel extends AlleleFrequencyCalculationModel { // @@ -58,7 +53,7 @@ public class ExactAFCalculationModel extends AlleleFrequencyCalculationModel { public void getLog10PNonRef(RefMetaDataTracker tracker, ReferenceContext ref, - Map GLs, Setalleles, + Map GLs, List alleles, double[] log10AlleleFrequencyPriors, double[] log10AlleleFrequencyPosteriors) { final int numAlleles = alleles.size(); diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/GridSearchAFEstimation.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/GridSearchAFEstimation.java index 10b646d63..f4195e5f0 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/GridSearchAFEstimation.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/GridSearchAFEstimation.java @@ -54,7 +54,7 @@ public class GridSearchAFEstimation extends AlleleFrequencyCalculationModel { protected void getLog10PNonRef(RefMetaDataTracker tracker, ReferenceContext ref, - Map GLs, Setalleles, + Map GLs, List alleles, double[] log10AlleleFrequencyPriors, double[] log10AlleleFrequencyPosteriors) { initializeAFMatrix(GLs); diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java index 9e8cc07a6..b72b68f9f 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/genotyper/UnifiedGenotyperEngine.java @@ -423,7 +423,7 @@ public class UnifiedGenotyperEngine { int endLoc = calculateEndPos(vc.getAlleles(), vc.getReference(), loc); - Set myAlleles = vc.getAlleles(); + Set myAlleles = new HashSet(vc.getAlleles()); // strip out the alternate allele if it's a ref call if ( bestAFguess == 0 && UAC.GenotypingMode == GenotypeLikelihoodsCalculationModel.GENOTYPING_MODE.DISCOVERY ) { myAlleles = new HashSet(1); @@ -447,7 +447,7 @@ public class UnifiedGenotyperEngine { return new VariantCallContext(vcCall, confidentlyCalled(phredScaledConfidence, PofF)); } - private int calculateEndPos(Set alleles, Allele refAllele, GenomeLoc loc) { + private int calculateEndPos(Collection alleles, Allele refAllele, GenomeLoc loc) { // TODO - temp fix until we can deal with extended events properly // for indels, stop location is one more than ref allele length boolean isSNP = true, hasNullAltAllele = false; diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/varianteval/evaluators/ValidationReport.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/varianteval/evaluators/ValidationReport.java index c60586017..3b4967cad 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/varianteval/evaluators/ValidationReport.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/varianteval/evaluators/ValidationReport.java @@ -10,6 +10,7 @@ import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import org.broadinstitute.sting.utils.variantcontext.Allele; import org.broadinstitute.sting.utils.variantcontext.VariantContext; +import java.util.Collection; import java.util.Set; /** @@ -142,8 +143,8 @@ public class ValidationReport extends VariantEvaluator implements StandardEval { public boolean haveDifferentAltAlleles(VariantContext eval, VariantContext comp) { - Set evalAlts = eval.getAlternateAlleles(); - Set compAlts = comp.getAlternateAlleles(); + Collection evalAlts = eval.getAlternateAlleles(); + Collection compAlts = comp.getAlternateAlleles(); if ( evalAlts.size() != compAlts.size() ) { return true; } else { diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantValidationAssessor.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantValidationAssessor.java index 8eaf976d0..4e6cc722d 100644 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantValidationAssessor.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/variantutils/VariantValidationAssessor.java @@ -237,7 +237,7 @@ public class VariantValidationAssessor extends RodWalker infoMap.put("HomVarPct", String.format("%.1f", 100.0*homVarProp)); infoMap.put("HetPct", String.format("%.1f", 100.0*hetProp)); infoMap.put("HW", String.format("%.2f", hwScore)); - Set altAlleles = vContext.getAlternateAlleles(); + Collection altAlleles = vContext.getAlternateAlleles(); int altAlleleCount = altAlleles.size() == 0 ? 0 : vContext.getChromosomeCount(altAlleles.iterator().next()); if ( !isViolation && altAlleleCount > 0 ) numTrueVariants++; diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContext.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContext.java index 05e21c8b8..eac6d70b5 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContext.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContext.java @@ -181,7 +181,7 @@ public class VariantContext implements Feature { // to enable tribble intergrati protected Type type = null; /** A set of the alleles segregating in this context */ - protected LinkedHashSet alleles = null; + final protected List alleles; /** A mapping from sampleName -> genotype objects for all genotypes associated with this context */ protected Map genotypes = null; @@ -355,7 +355,7 @@ public class VariantContext implements Feature { // to enable tribble intergrati if ( alleles == null ) { throw new IllegalArgumentException("Alleles cannot be null"); } // we need to make this a LinkedHashSet in case the user prefers a given ordering of alleles - this.alleles = alleleCollectionToSet(new LinkedHashSet(), alleles); + this.alleles = makeAlleles(alleles); if ( genotypes == null ) { genotypes = NO_GENOTYPES; } @@ -445,7 +445,7 @@ public class VariantContext implements Feature { // to enable tribble intergrati * @param alleles the set of allele segregating alleles at this site. Must include those in genotypes, but may be more * @return vc subcontext */ - public VariantContext subContextFromGenotypes(Collection genotypes, Set alleles) { + public VariantContext subContextFromGenotypes(Collection genotypes, Collection alleles) { return new VariantContext(getSource(), contig, start, stop, alleles, genotypes != null ? genotypeCollectionToMap(new TreeMap(), genotypes) : null, getNegLog10PError(), filtersWereApplied() ? getFilters() : null, getAttributes(), getReferenceBaseForIndel()); } @@ -687,17 +687,6 @@ public class VariantContext implements Feature { // to enable tribble intergrati return ref; } - /** Private helper routine that grabs the reference allele but doesn't throw an error if there's no such allele */ - -// private Allele getReferenceWithoutError() { -// for ( Allele allele : getAlleles() ) { -// if ( allele.isReference() ) { -// return allele; -// } -// } -// -// return null; -// } /** * @return true if the context is strictly bi-allelic @@ -754,7 +743,7 @@ public class VariantContext implements Feature { // to enable tribble intergrati * * @return the set of alleles */ - public Set getAlleles() { return alleles; } + public List getAlleles() { return alleles; } /** * Gets the alternate alleles. This method should return all the alleles present at the location, @@ -763,14 +752,8 @@ public class VariantContext implements Feature { // to enable tribble intergrati * * @return the set of alternate alleles */ - public Set getAlternateAlleles() { - LinkedHashSet altAlleles = new LinkedHashSet(); - for ( Allele allele : alleles ) { - if ( allele.isNonReference() ) - altAlleles.add(allele); - } - - return Collections.unmodifiableSet(altAlleles); + public List getAlternateAlleles() { + return alleles.subList(1, alleles.size()); } /** @@ -797,14 +780,7 @@ public class VariantContext implements Feature { // to enable tribble intergrati * @throws IllegalArgumentException if i is invalid */ public Allele getAlternateAllele(int i) { - int n = 0; - - for ( Allele allele : alleles ) { - if ( allele.isNonReference() && n++ == i ) - return allele; - } - - throw new IllegalArgumentException("Requested " + i + " alternative allele but there are only " + n + " alternative alleles " + this); + return alleles.get(i+1); } /** @@ -813,8 +789,8 @@ public class VariantContext implements Feature { // to enable tribble intergrati * regardless of ordering. Otherwise returns false. */ public boolean hasSameAlternateAllelesAs ( VariantContext other ) { - Set thisAlternateAlleles = getAlternateAlleles(); - Set otherAlternateAlleles = other.getAlternateAlleles(); + List thisAlternateAlleles = getAlternateAlleles(); + List otherAlternateAlleles = other.getAlternateAlleles(); if ( thisAlternateAlleles.size() != otherAlternateAlleles.size() ) { return false; @@ -1121,7 +1097,7 @@ public class VariantContext implements Feature { // to enable tribble intergrati if ( !hasGenotypes() ) return; - Set reportedAlleles = getAlleles(); + List reportedAlleles = getAlleles(); Set observedAlleles = new HashSet(); observedAlleles.add(getReference()); for ( Genotype g : getGenotypes().values() ) { @@ -1371,17 +1347,34 @@ public class VariantContext implements Feature { // to enable tribble intergrati } // protected basic manipulation routines - private static LinkedHashSet alleleCollectionToSet(LinkedHashSet dest, Collection alleles) { - for ( Allele a : alleles ) { - for ( Allele b : dest ) { + private static List makeAlleles(Collection alleles) { + final List alleleList = new ArrayList(alleles.size()); + + boolean sawRef = false; + for ( final Allele a : alleles ) { + for ( final Allele b : alleleList ) { if ( a.equals(b, true) ) throw new IllegalArgumentException("Duplicate allele added to VariantContext: " + a); } - dest.add(a); + // deal with the case where the first allele isn't the reference + if ( a.isReference() ) { + if ( sawRef ) + throw new IllegalArgumentException("Alleles for a VariantContext must contain a single reference allele: " + alleles); + alleleList.add(0, a); + sawRef = true; + } + else + alleleList.add(a); } - return dest; + if ( alleleList.isEmpty() ) + throw new IllegalArgumentException("Cannot create a VariantContext with an empty allele list"); + + if ( alleleList.get(0).isNonReference() ) + throw new IllegalArgumentException("Alleles for a VariantContext must contain a single reference allele: " + alleles); + + return alleleList; } public static Map genotypeCollectionToMap(Map dest, Collection genotypes) { diff --git a/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContextUtils.java b/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContextUtils.java index 437693a46..74ab7074a 100755 --- a/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContextUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/variantcontext/VariantContextUtils.java @@ -666,7 +666,7 @@ public class VariantContextUtils { return merged; } - private static final boolean hasPLIncompatibleAlleles(final Set alleleSet1, final Set alleleSet2) { + private static final boolean hasPLIncompatibleAlleles(final Collection alleleSet1, final Collection alleleSet2) { final Iterator it1 = alleleSet1.iterator(); final Iterator it2 = alleleSet2.iterator(); diff --git a/public/java/test/org/broadinstitute/sting/gatk/walkers/diffengine/DiffableReaderUnitTest.java b/public/java/test/org/broadinstitute/sting/gatk/walkers/diffengine/DiffableReaderUnitTest.java index dee7bbd88..46b0df5b4 100644 --- a/public/java/test/org/broadinstitute/sting/gatk/walkers/diffengine/DiffableReaderUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/gatk/walkers/diffengine/DiffableReaderUnitTest.java @@ -70,7 +70,7 @@ public class DiffableReaderUnitTest extends BaseTest { private static void testLeaf(DiffNode rec, String field, Object expected) { DiffElement value = rec.getElement(field); Assert.assertNotNull(value, "Expected to see leaf named " + field + " in rec " + rec); - Assert.assertEquals(value.getValue().getValue(), expected, "Expected to leaf named " + field + " to have value " + expected + " in rec " + rec); + Assert.assertEquals(value.getValue().getValue(), expected, "Expected to see leaf named " + field + " to have value " + expected + " in rec " + rec + " but got instead " + value.getValue().getValue()); } @Test(enabled = true, dependsOnMethods = "testPluggableDiffableReaders") @@ -95,7 +95,7 @@ public class DiffableReaderUnitTest extends BaseTest { testLeaf(rec1, "POS", 2646); testLeaf(rec1, "ID", "rs62635284"); testLeaf(rec1, "REF", Allele.create("G", true)); - testLeaf(rec1, "ALT", new HashSet(Arrays.asList(Allele.create("A")))); + testLeaf(rec1, "ALT", Arrays.asList(Allele.create("A"))); testLeaf(rec1, "QUAL", 0.15); testLeaf(rec1, "FILTER", Collections.emptySet()); testLeaf(rec1, "AC", "2"); diff --git a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUnitTest.java index 39f8c0f39..9026f09d6 100755 --- a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUnitTest.java @@ -13,6 +13,7 @@ import org.testng.Assert; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; @@ -261,11 +262,16 @@ public class VariantContextUnitTest extends BaseTest { new VariantContext("test", insLoc, insLocStart, insLocStop, Arrays.asList(delRef, del)); } - @Test (expectedExceptions = IllegalStateException.class) + @Test (expectedExceptions = IllegalArgumentException.class) public void testBadConstructorArgs3() { new VariantContext("test", insLoc, insLocStart, insLocStop, Arrays.asList(del)); } + @Test (expectedExceptions = IllegalArgumentException.class) + public void testBadConstructorArgs4() { + new VariantContext("test", insLoc, insLocStart, insLocStop, Collections.emptyList()); + } + @Test (expectedExceptions = IllegalArgumentException.class) public void testBadConstructorArgsDuplicateAlleles1() { new VariantContext("test", insLoc, insLocStart, insLocStop, Arrays.asList(Aref, T, T)); diff --git a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUtilsUnitTest.java b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUtilsUnitTest.java index c8cbbfd8e..845d9c216 100644 --- a/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUtilsUnitTest.java +++ b/public/java/test/org/broadinstitute/sting/utils/variantcontext/VariantContextUtilsUnitTest.java @@ -439,7 +439,7 @@ public class VariantContextUtilsUnitTest extends BaseTest { new MergeGenotypesTest("PerserveAlleles", "1,2", makeVC("1", Arrays.asList(Aref, T), makeG("s1", Aref, T, 1)), makeVC("2", Arrays.asList(Aref, C), makeG("s2", Aref, C, 2)), - makeVC("3", Arrays.asList(Aref, C, T), makeG("s1", Aref, T, 1), makeG("s2", Aref, C, 2))); + makeVC("3", Arrays.asList(Aref, T, C), makeG("s1", Aref, T, 1), makeG("s2", Aref, C, 2))); new MergeGenotypesTest("TakeGenotypePartialOverlap-1,2", "1,2", makeVC("1", Arrays.asList(Aref, T), makeG("s1", Aref, T, 1)),