diff --git a/src/main/java/htsjdk/variant/variantcontext/Allele.java b/src/main/java/htsjdk/variant/variantcontext/Allele.java index b256d2d9dd..002782570f 100644 --- a/src/main/java/htsjdk/variant/variantcontext/Allele.java +++ b/src/main/java/htsjdk/variant/variantcontext/Allele.java @@ -431,5 +431,8 @@ static boolean oneIsPrefixOfOther(final Allele a1, final Allele a2) { int length(); + /** + * @return true if Allele is either {@code } or {@code <*>} + */ boolean isNonRefAllele(); } diff --git a/src/main/java/htsjdk/variant/variantcontext/Genotype.java b/src/main/java/htsjdk/variant/variantcontext/Genotype.java index 8c2970042c..c75cc1ec5b 100644 --- a/src/main/java/htsjdk/variant/variantcontext/Genotype.java +++ b/src/main/java/htsjdk/variant/variantcontext/Genotype.java @@ -77,6 +77,20 @@ protected Genotype(final String sampleName, final String filters) { */ public abstract List getAlleles(); + /** + * @return true if any allele is REF + */ + public boolean hasRefAllele() { + return getAlleles().stream().anyMatch(A->A.isReference()); + }; + + /** + * @return true if any allele is ALT, (NO_CALL are ignored) + */ + public boolean hasAltAllele() { + return getAlleles().stream().anyMatch(A->!(A.isReference() || A.isNoCall())); + }; + /** * Returns how many times allele appears in this genotype object? * diff --git a/src/test/java/htsjdk/variant/variantcontext/GenotypeUnitTest.java b/src/test/java/htsjdk/variant/variantcontext/GenotypeUnitTest.java index a447a0beac..cf9e2c3afb 100644 --- a/src/test/java/htsjdk/variant/variantcontext/GenotypeUnitTest.java +++ b/src/test/java/htsjdk/variant/variantcontext/GenotypeUnitTest.java @@ -31,6 +31,7 @@ import htsjdk.variant.VariantBaseTest; import htsjdk.variant.vcf.VCFConstants; +import java.util.Arrays; import org.testng.Assert; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; @@ -70,6 +71,28 @@ public void testFilters() { Assert.assertFalse(makeGB().filter("").make().isFiltered(), "empty filters should count as unfiltered"); Assert.assertEquals(makeGB().filter("").make().getFilters(), null, "empty filter string should result in null filters"); } + + @Test + public void testHasAltAllele() { + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(A)).hasAltAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(A,Aref)).hasAltAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(A,Allele.NO_CALL)).hasAltAllele()); + Assert.assertFalse(GenotypeBuilder.create("s", Arrays.asList(Aref)).hasAltAllele()); + Assert.assertFalse(GenotypeBuilder.create("s", Arrays.asList(Aref,Aref)).hasAltAllele()); + Assert.assertFalse(GenotypeBuilder.create("s", Arrays.asList(Allele.NO_CALL,Allele.NO_CALL)).hasAltAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(Allele.NON_REF_ALLELE)).hasAltAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(Allele.SPAN_DEL)).hasAltAllele()); + } + + @Test + public void testHasRefAllele() { + Assert.assertFalse(GenotypeBuilder.create("s", Arrays.asList(A)).hasRefAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(A,Aref)).hasRefAllele()); + Assert.assertFalse(GenotypeBuilder.create("s", Arrays.asList(A,Allele.NO_CALL)).hasRefAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(Aref)).hasRefAllele()); + Assert.assertTrue(GenotypeBuilder.create("s", Arrays.asList(Aref,Aref)).hasRefAllele()); + Assert.assertFalse(GenotypeBuilder.create("s", Arrays.asList(Allele.NO_CALL,Allele.NO_CALL)).hasRefAllele()); + } // public Genotype(String sampleName, List alleles, double negLog10PError, Set filters, Map attributes, boolean isPhased) { // public Genotype(String sampleName, List alleles, double negLog10PError, Set filters, Map attributes, boolean isPhased, double[] log10Likelihoods) {