Skip to content

Commit

Permalink
Add new Genotype methods hasRefAllele and hasAltAllele (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindenb authored Aug 8, 2023
1 parent 20334af commit 1e5ec5a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/htsjdk/variant/variantcontext/Allele.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,8 @@ static boolean oneIsPrefixOfOther(final Allele a1, final Allele a2) {

int length();

/**
* @return true if Allele is either {@code <NON_REF>} or {@code <*>}
*/
boolean isNonRefAllele();
}
14 changes: 14 additions & 0 deletions src/main/java/htsjdk/variant/variantcontext/Genotype.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ protected Genotype(final String sampleName, final String filters) {
*/
public abstract List<Allele> 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?
*
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/htsjdk/variant/variantcontext/GenotypeUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Allele> alleles, double negLog10PError, Set<String> filters, Map<String, ?> attributes, boolean isPhased) {
// public Genotype(String sampleName, List<Allele> alleles, double negLog10PError, Set<String> filters, Map<String, ?> attributes, boolean isPhased, double[] log10Likelihoods) {
Expand Down

0 comments on commit 1e5ec5a

Please sign in to comment.