Search in sources :

Example 1 with HaplotypeMatch

use of org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch in project PharmCAT by PharmGKB.

the class ResultSerializer method toHtml.

public ResultSerializer toHtml(Result result, Path htmlFile) throws IOException {
    Preconditions.checkNotNull(result);
    Preconditions.checkNotNull(htmlFile);
    Preconditions.checkArgument(htmlFile.toString().endsWith(".html"));
    StringBuilder builder = new StringBuilder();
    for (GeneCall call : result.getGeneCalls()) {
        MatchData matchData = call.getMatchData();
        builder.append("<h3>").append(call.getGene()).append("</h3>");
        builder.append("<ul>");
        for (DiplotypeMatch diplotype : call.getDiplotypes()) {
            builder.append("<li>").append(diplotype.getName()).append(" (").append(diplotype.getScore()).append(")</li>");
        }
        builder.append("</ul>");
        builder.append("<table class=\"table table-striped table-hover table-condensed\">");
        // position
        builder.append("<tr>");
        builder.append("<th>Definition Position</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getPosition()).append("</th>");
        }
        builder.append("</tr>");
        // rsid
        builder.append("<tr>");
        builder.append("<th></th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>");
            if (v.getRsid() != null) {
                builder.append(v.getRsid());
            }
            builder.append("</th>");
        }
        builder.append("</tr>");
        // VCF position
        builder.append("<tr>");
        builder.append("<th>VCF Position</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getPosition()).append("</th>");
        }
        builder.append("</tr>");
        // sample
        builder.append("<tr>");
        builder.append("<th>VCF REF,ALTs</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getVcfAlleles()).append("</th>");
        }
        builder.append("</tr>");
        builder.append("<tr class=\"success\">");
        builder.append("<th>VCF Call</th>");
        for (Variant v : call.getVariants()) {
            builder.append("<th>").append(v.getVcfCall()).append("</th>");
        }
        builder.append("</tr>");
        Set<String> matchedHaplotypeNames = new HashSet<>();
        if (call.getHaplotypes().size() > 0) {
            for (HaplotypeMatch hm : call.getHaplotypes()) {
                matchedHaplotypeNames.add(hm.getHaplotype().getName());
                printAllele(builder, hm.getHaplotype().getName(), hm.getHaplotype().getPermutations().pattern(), "info");
                for (String seq : hm.getSequences()) {
                    printAllele(builder, null, seq, null);
                }
            }
        }
        if (m_alwaysShowUnmatchedHaplotypes || matchedHaplotypeNames.size() == 0) {
            for (NamedAllele haplotype : matchData.getHaplotypes()) {
                if (!matchedHaplotypeNames.contains(haplotype.getName())) {
                    printAllele(builder, haplotype.getName(), haplotype.getPermutations().pattern(), "danger");
                }
            }
        }
        builder.append("</table>");
        if (matchData.getMissingPositions().size() > 0) {
            builder.append("<p>There ");
            if (matchData.getMissingPositions().size() > 1) {
                builder.append("were ");
            } else {
                builder.append("was ");
            }
            builder.append(matchData.getMissingPositions().size()).append(" missing positions from the VCF file:</p>").append("<ul>");
            for (VariantLocus variant : matchData.getMissingPositions()) {
                builder.append("<li>").append(variant.getPosition()).append(" (").append(variant.getChromosomeHgvsName()).append(")</li>");
            }
            builder.append("</ul>");
            if (call.getUncallableHaplotypes().size() > 0) {
                builder.append("<p>The following haplotype(s) were eliminated from consideration:</p>").append("<ul>");
                for (String name : call.getUncallableHaplotypes()) {
                    builder.append("<li>").append(name).append("</li>");
                }
                builder.append("</ul>");
            }
            if (call.getHaplotypes().size() > 0) {
                builder.append("<p>The following haplotypes were called even though tag positions were missing:</p>").append("<ul>");
                for (HaplotypeMatch hm : call.getHaplotypes()) {
                    if (hm.getHaplotype().getMissingPositions().size() > 0) {
                        builder.append("<li>Called ").append(hm.getName()).append(" without ").append(hm.getHaplotype().getMissingPositions().stream().map(VariantLocus::getChromosomeHgvsName).collect(Collectors.joining(", "))).append("</li>");
                    }
                }
                builder.append("</ul>");
            }
        }
    }
    try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(htmlFile, StandardCharsets.UTF_8))) {
        Map<String, String> varMap = new HashMap<>();
        varMap.put("title", "PharmCAT Allele Call Report for " + result.getMetadata().getInputFilename());
        varMap.put("content", builder.toString());
        varMap.put("timestamp", m_dateFormat.format(new Date()));
        StringSubstitutor sub = new StringSubstitutor(varMap);
        writer.println(sub.replace(getHtmlTemplate()));
    }
    return this;
}
Also used : HashMap(java.util.HashMap) NamedAllele(org.pharmgkb.pharmcat.definition.model.NamedAllele) Date(java.util.Date) Variant(org.pharmgkb.pharmcat.haplotype.model.Variant) HaplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch) StringSubstitutor(org.apache.commons.text.StringSubstitutor) VariantLocus(org.pharmgkb.pharmcat.definition.model.VariantLocus) DiplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.DiplotypeMatch) GeneCall(org.pharmgkb.pharmcat.haplotype.model.GeneCall) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter)

Example 2 with HaplotypeMatch

use of org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch in project PharmCAT by PharmGKB.

the class DiplotypeMatcherTest method testComparePermutations.

@Test
void testComparePermutations() {
    VariantLocus var1 = new VariantLocus("chr1", 1, "g.1T>A");
    VariantLocus var2 = new VariantLocus("chr1", 2, "g.2T>A");
    VariantLocus var3 = new VariantLocus("chr1", 3, "g.3T>A");
    VariantLocus var4 = new VariantLocus("chr1", 4, "g.3T>A");
    VariantLocus[] variants = new VariantLocus[] { var1, var2, var3, var4 };
    String[] alleles = new String[] { "T", "A", "C", "C" };
    NamedAllele hap1 = new NamedAllele("*1", "*1", alleles, alleles, true);
    hap1.initialize(variants);
    alleles = new String[] { null, "T", "C", null };
    NamedAllele hap2 = new NamedAllele("*2", "*2", alleles, alleles, false);
    hap2.initialize(variants);
    alleles = new String[] { null, null, "GG", null };
    NamedAllele hap3 = new NamedAllele("*3", "*3", alleles, alleles, false);
    hap3.initialize(variants);
    Set<String> permutations = Sets.newHashSet("1:T;2:A;3:C;4:C;", "1:T;2:A;3:C;4:G;", "1:T;2:T;3:C;4:C;", "1:T;2:T;3:C;4:G;");
    SortedMap<String, SampleAllele> sampleAlleleMap = new TreeMap<>();
    sampleAlleleMap.put("chr1:1", new SampleAllele("chr1", 1, "T", "T", true, Lists.newArrayList("T")));
    sampleAlleleMap.put("chr1:2", new SampleAllele("chr1", 2, "A", "T", false, Lists.newArrayList("T")));
    sampleAlleleMap.put("chr1:3", new SampleAllele("chr1", 3, "C", "C", false, Lists.newArrayList("C")));
    sampleAlleleMap.put("chr1:4", new SampleAllele("chr1", 4, "C", "G", false, Lists.newArrayList("C")));
    MatchData dataset = new MatchData(sampleAlleleMap, variants, null, null);
    dataset.marshallHaplotypes(new TreeSet<>(Lists.newArrayList(hap1, hap2, hap3)));
    dataset.generateSamplePermutations();
    assertThat(dataset.getPermutations(), equalTo(permutations));
    DiplotypeMatcher diplotypeMatcher = new DiplotypeMatcher(dataset);
    SortedSet<HaplotypeMatch> matches = diplotypeMatcher.comparePermutations();
    assertEquals(2, matches.size());
    Iterator<HaplotypeMatch> it = matches.iterator();
    assertEquals(hap1, it.next().getHaplotype());
    assertEquals(hap2, it.next().getHaplotype());
}
Also used : NamedAllele(org.pharmgkb.pharmcat.definition.model.NamedAllele) TreeMap(java.util.TreeMap) HaplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch) VariantLocus(org.pharmgkb.pharmcat.definition.model.VariantLocus) Test(org.junit.jupiter.api.Test)

Example 3 with HaplotypeMatch

use of org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch in project PharmCAT by PharmGKB.

the class DiplotypeFactory method makeDiplotype.

/**
 * Make a Diplotype object based on a DiplotypeMatch, this will also populate function information from the match
 */
private Diplotype makeDiplotype(DiplotypeMatch diplotypeMatch) {
    HaplotypeMatch h1 = diplotypeMatch.getHaplotype1();
    HaplotypeMatch h2 = diplotypeMatch.getHaplotype2();
    Diplotype diplotype = new Diplotype(f_gene, makeHaplotype(h1), makeHaplotype(h2));
    fillDiplotype(diplotype);
    return diplotype;
}
Also used : HaplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch) Diplotype(org.pharmgkb.pharmcat.reporter.model.result.Diplotype)

Example 4 with HaplotypeMatch

use of org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch in project PharmCAT by PharmGKB.

the class DiplotypeMatcher method determineHeterozygousPairs.

/**
 * Determine possible diplotypes given a set of {@link HaplotypeMatch}'s when sample is heterozygous at at least one
 * position.
 *
 * @param haplotypeMatches the matches that were found via {@link #comparePermutations()}
 */
private List<DiplotypeMatch> determineHeterozygousPairs(SortedSet<HaplotypeMatch> haplotypeMatches) {
    SortedMap<NamedAllele, HaplotypeMatch> hapMap = new TreeMap<>();
    for (HaplotypeMatch hm : haplotypeMatches) {
        hapMap.put(hm.getHaplotype(), hm);
    }
    // possible pairs from what got matched
    List<List<NamedAllele>> pairs = CombinationUtil.generatePerfectPairs(hapMap.keySet());
    List<DiplotypeMatch> matches = new ArrayList<>();
    for (List<NamedAllele> pair : pairs) {
        NamedAllele hap1 = pair.get(0);
        HaplotypeMatch hm1 = hapMap.get(hap1);
        if (hm1 == null) {
            continue;
        }
        NamedAllele hap2 = pair.get(1);
        HaplotypeMatch hm2 = hapMap.get(hap2);
        if (hm2 == null) {
            continue;
        }
        if (hap1 == hap2 && hm1.getSequences().size() == 1) {
            // cannot call homozygous unless more than one sequence matches
            continue;
        }
        Set<String[]> sequencePairs = findSequencePairs(hm1, hm2);
        if (!sequencePairs.isEmpty()) {
            DiplotypeMatch dm = new DiplotypeMatch(hm1, hm2, m_dataset);
            sequencePairs.stream().forEach(dm::addSequencePair);
            matches.add(dm);
        }
    }
    Collections.sort(matches);
    return matches;
}
Also used : HaplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NamedAllele(org.pharmgkb.pharmcat.definition.model.NamedAllele) TreeMap(java.util.TreeMap) DiplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.DiplotypeMatch)

Example 5 with HaplotypeMatch

use of org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch in project PharmCAT by PharmGKB.

the class DiplotypeMatcher method determineHomozygousPairs.

/**
 * Determine possible diplotypes given a set of {@link HaplotypeMatch}'s when sample is homozygous at all positions.
 *
 * @param haplotypeMatches the matches that were found via {@link #comparePermutations()}
 */
private List<DiplotypeMatch> determineHomozygousPairs(SortedSet<HaplotypeMatch> haplotypeMatches) {
    String seq = m_dataset.getPermutations().iterator().next();
    List<DiplotypeMatch> matches = new ArrayList<>();
    if (haplotypeMatches.size() == 1) {
        // matched a single haplotype: need to return that as a diplotype
        HaplotypeMatch hm = haplotypeMatches.first();
        DiplotypeMatch dm = new DiplotypeMatch(hm, hm, m_dataset);
        dm.addSequencePair(new String[] { seq, seq });
        matches.add(dm);
    } else {
        // return all possible pairings of matched haplotypes
        List<List<HaplotypeMatch>> pairs = CombinationUtil.generatePerfectPairs(haplotypeMatches);
        for (List<HaplotypeMatch> pair : pairs) {
            DiplotypeMatch dm = new DiplotypeMatch(pair.get(0), pair.get(1), m_dataset);
            dm.addSequencePair(new String[] { seq, seq });
            matches.add(dm);
        }
    }
    return matches;
}
Also used : HaplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DiplotypeMatch(org.pharmgkb.pharmcat.haplotype.model.DiplotypeMatch)

Aggregations

HaplotypeMatch (org.pharmgkb.pharmcat.haplotype.model.HaplotypeMatch)5 NamedAllele (org.pharmgkb.pharmcat.definition.model.NamedAllele)3 DiplotypeMatch (org.pharmgkb.pharmcat.haplotype.model.DiplotypeMatch)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 VariantLocus (org.pharmgkb.pharmcat.definition.model.VariantLocus)2 PrintWriter (java.io.PrintWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 StringSubstitutor (org.apache.commons.text.StringSubstitutor)1 Test (org.junit.jupiter.api.Test)1 GeneCall (org.pharmgkb.pharmcat.haplotype.model.GeneCall)1 Variant (org.pharmgkb.pharmcat.haplotype.model.Variant)1 Diplotype (org.pharmgkb.pharmcat.reporter.model.result.Diplotype)1