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;
}
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());
}
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;
}
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;
}
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;
}
Aggregations