diff --git a/src/main/java/htsjdk/variant/variantcontext/VariantContext.java b/src/main/java/htsjdk/variant/variantcontext/VariantContext.java index f64b0ff8e..d3d97040a 100644 --- a/src/main/java/htsjdk/variant/variantcontext/VariantContext.java +++ b/src/main/java/htsjdk/variant/variantcontext/VariantContext.java @@ -37,18 +37,7 @@ import htsjdk.variant.vcf.VCFHeaderLineType; import java.io.Serializable; -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -1699,10 +1688,8 @@ public Allele getAltAlleleWithHighestAlleleCount() { return getAlternateAllele(0); return getAlternateAlleles().stream() - .map(allele -> new Tuple<>(allele, getCalledChrCount(allele))) - .max((alleleAndCount1, alleleAndCount2) -> Integer.compare(alleleAndCount1.b, alleleAndCount2.b)) - .get() - .a; + .max(Comparator.comparing(this::getCalledChrCount)) + .orElse(null); } /** diff --git a/src/test/java/htsjdk/variant/variantcontext/VariantContextUnitTest.java b/src/test/java/htsjdk/variant/variantcontext/VariantContextUnitTest.java index be55f8f58..5fe92c6d2 100644 --- a/src/test/java/htsjdk/variant/variantcontext/VariantContextUnitTest.java +++ b/src/test/java/htsjdk/variant/variantcontext/VariantContextUnitTest.java @@ -464,6 +464,8 @@ public void testAccessingCompleteGenotypes() { Assert.assertEquals(4, vc.getCalledChrCount(T)); Assert.assertEquals(3, vc.getCalledChrCount(ATC)); Assert.assertEquals(2, vc.getCalledChrCount(Allele.NO_CALL)); + + Assert.assertEquals(T, vc.getAltAlleleWithHighestAlleleCount()); } @Test @@ -487,6 +489,16 @@ public void testAccessingRefGenotypes() { Assert.assertEquals(4, vc.getCalledChrCount(Aref)); Assert.assertEquals(0, vc.getCalledChrCount(T)); Assert.assertEquals(2, vc.getCalledChrCount(Allele.NO_CALL)); + + //bi allelic, only one alt allele + Allele expected; + if (alleles.size()>1) { + expected = alleles.get(1); + } else { + expected = null; + } + + Assert.assertEquals( vc.getAltAlleleWithHighestAlleleCount(), expected); } } @@ -600,6 +612,21 @@ public void testVCFfromGenotypes() { Assert.assertEquals(4, vc125.getCalledChrCount(Aref)); } + @Test + public void testMonomorphicVariant() { + Genotype g1 = GenotypeBuilder.create("AA", Arrays.asList(Aref, Aref)); + Genotype g2 = GenotypeBuilder.create("BB", Arrays.asList(Aref, Allele.NO_CALL)); + Genotype g3 = GenotypeBuilder.create("CC", Arrays.asList(Allele.NO_CALL,Allele.NO_CALL)); + GenotypesContext gc = GenotypesContext.create(g1, g2, g3); + VariantContext vc = new VariantContextBuilder("genotypes", snpLoc, snpLocStart, snpLocStop, Collections.singletonList(Aref)).genotypes(gc).make(); + + Assert.assertEquals(vc.getType(), VariantContext.Type.NO_VARIATION); + Assert.assertNull(vc.getAltAlleleWithHighestAlleleCount()); + Assert.assertEquals(vc.getCalledChrCount(Aref), 3); + + } + + public void testGetGenotypeMethods() { Genotype g1 = GenotypeBuilder.create("AA", Arrays.asList(Aref, Aref)); Genotype g2 = GenotypeBuilder.create("AT", Arrays.asList(Aref, T));