Skip to content

Commit

Permalink
fix(namedallelematcher): fix permutation generation with mix of phase…
Browse files Browse the repository at this point in the history
…d/unphased data
  • Loading branch information
markwoon committed Aug 27, 2024
1 parent fb1ebbd commit 2146db0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class CombinationUtil {
*/
public static Set<String> generatePermutations(List<SampleAllele> alleles) {
Preconditions.checkNotNull(alleles);
Preconditions.checkArgument(alleles.size() > 0, "No alleles to generate permutations for");
Preconditions.checkArgument(!alleles.isEmpty(), "No alleles to generate permutations for");

boolean isHaploid = alleles.stream().allMatch(sa -> sa.getAllele2() == null);
Set<String> rez = generatePermutations(alleles, 0, isHaploid, true, "");
if (alleles.get(0).isEffectivelyPhased() && !isHaploid) {
if (!isHaploid) {
rez.addAll(generatePermutations(alleles, 0, false, false, ""));
}
if (rez.size() == 0) {
if (rez.isEmpty()) {
throw new IllegalStateException("No permutations generated from " + alleles.size() + " alleles");
}
return rez;
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/org/pharmgkb/pharmcat/TestVcfBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.jupiter.api.TestInfo;
import org.pharmgkb.pharmcat.definition.DefinitionReader;
Expand Down Expand Up @@ -86,13 +87,29 @@ public TestVcfBuilder variation(String gene, String rsid, String... cpicAlleles)
return this;
}

public TestVcfBuilder variation(String gene, String rsid, boolean isPhased, String... cpicAlleles) {
VcfEdit edit = new VcfEdit(rsid, cpicAlleles);
edit.isPhased = isPhased;
m_edits.computeIfAbsent(gene, g -> new HashMap<>())
.put(edit.id, edit);
return this;
}

public TestVcfBuilder variation(String gene, String chrom, long position, String... cpicAlleles) {
VcfEdit edit = new VcfEdit(chrom, position, cpicAlleles);
m_edits.computeIfAbsent(gene, g -> new HashMap<>())
.put(edit.id, edit);
return this;
}

public TestVcfBuilder variation(String gene, String chrom, long position, boolean isPhased, String... cpicAlleles) {
VcfEdit edit = new VcfEdit(chrom, position, cpicAlleles);
edit.isPhased = isPhased;
m_edits.computeIfAbsent(gene, g -> new HashMap<>())
.put(edit.id, edit);
return this;
}

public TestVcfBuilder variationAsIs(String gene, String rsid, String gt, String ref, String... vcfAltAlleles) {
VcfEdit edit = new VcfEdit(rsid, null);
edit.gt = gt;
Expand Down Expand Up @@ -318,7 +335,11 @@ private void handleCustomEdit(DefinitionFile definitionFile, PrintWriter writer,
StringBuilder builder = new StringBuilder();
for (String cpicAllele : edit.cpicAlleles) {
if (!builder.isEmpty()) {
builder.append(m_isPhased ? "|" : "/");
if (edit.isPhased == null) {
builder.append(m_isPhased ? "|" : "/");
} else {
builder.append(edit.isPhased ? "|" : "/");
}
}
String vcfAllele = vl.getCpicToVcfAlleleMap().get(cpicAllele);
if (vcfAllele == null) {
Expand Down Expand Up @@ -364,6 +385,7 @@ private static class VcfEdit {
private String[] vcfAltAlleles;
private String ref;
private String gt;
private Boolean isPhased;

private VcfEdit(String rsid, @Nullable String[] cpicAlleles) {
this.id = rsid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,44 @@ void testUnknownAltMultisample() throws Exception {
}


/**
* This tests permutation generation works as expected when we get all phased data, all unphased data, and a mix of
* phased and unphased data.
*/
@Test
void testPermutationGeneration(TestInfo testInfo) throws Exception {
Path definitionFile = DataManager.DEFAULT_DEFINITION_DIR.resolve("CYP2C19_translation.json");

// all phased
assertDiplotypePairs(Lists.newArrayList("*2/*17"), testMatchNamedAlleles(definitionFile,
new TestVcfBuilder(testInfo, "*2/*17")
.phased()
.variation("CYP2C19", "rs12248560", "C", "T")
.variation("CYP2C19", "rs12769205", "G", "A")
.variation("CYP2C19", "rs4244285", "A", "G")
.variation("CYP2C19", "rs3758581", "G", "G")
.generate()));

// all unphased
assertDiplotypePairs(Lists.newArrayList("*2/*17"), testMatchNamedAlleles(definitionFile,
new TestVcfBuilder(testInfo, "*2/*17")
.variation("CYP2C19", "rs12248560", "C", "T")
.variation("CYP2C19", "rs12769205", "G", "A")
.variation("CYP2C19", "rs4244285", "A", "G")
.variation("CYP2C19", "rs3758581", "G", "G")
.generate()));

// mix of phased and unphased
assertDiplotypePairs(Lists.newArrayList("*2/*17"), testMatchNamedAlleles(definitionFile,
new TestVcfBuilder(testInfo, "*2/*17")
.variation("CYP2C19", "rs12248560", "C", "T")
.variation("CYP2C19", "rs12769205", true, "G", "A")
.variation("CYP2C19", "rs4244285", true, "A", "G")
.variation("CYP2C19", "rs3758581", "G", "G")
.generate()));
}


@SuppressWarnings("unused")
private static void printWarnings(Result result) {
for (String key : result.getVcfWarnings().keySet()) {
Expand Down

0 comments on commit 2146db0

Please sign in to comment.