Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two methods for Genotype: HasRefAllele HasAltAllele #1678

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. This method is very confusingly named.

}
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