<commit_before>package org.pharmgkb.pharmcat.reporter.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.pharmgkb.pharmcat.reporter.model.Annotation;
import org.pharmgkb.pharmcat.reporter.model.CPICException;
import org.pharmgkb.pharmcat.reporter.model.Group;
import org.pharmgkb.pharmcat.reporter.resultsJSON.GeneReport;
import org.pharmgkb.pharmcat.reporter.resultsJSON.Interaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ReporterWriter {
  private static final Logger sf_logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  private static final String sf_outputFileName = "reporter.output.md";

  public static void printResults(Path outputDir, List<Interaction> guidelineResultList, Map<String, GeneReport> symbolToGeneReportMap) throws IOException {
    Path reportPath = outputDir.resolve(sf_outputFileName);
    sf_logger.info("Writing report to {}", reportPath);

    try (BufferedWriter writer = Files.newBufferedWriter(reportPath)) {
      writer.write("# PharmCAT Report\n\n\n");

      writer.write("## Haplotype Calls\n\n");
      for (String gene : symbolToGeneReportMap.keySet()) {
        writer.write("### Gene: " + gene + "\n");
        GeneReport geneReport = symbolToGeneReportMap.get(gene);

        if (geneReport.getDips().size() == 1) {
          writer.write("call: `" + geneReport.getDips().iterator().next() + "`\n");
        } else {
          writer.write("__possible calls__:\n\n");
          for (String dip : geneReport.getDips()) {
            writer.write(" * `" + dip + "`\n");
          }
          writer.write("\n");
        }

        writer.write("__warnings__: ");
        if (geneReport.getExceptionList() == null || geneReport.getExceptionList().size() == 0) {
          writer.write("none applicable\n");
        } else {
          writer.write("\n\n");
          for (CPICException exception : geneReport.getExceptionList()) {
            writer.write(" * "+exception.getMessage()+"\n");
          }
        }
        writer.write("\n");
      }

      writer.write("\n\n");

      writer.write("## Guidelines\n\n");

      for (Interaction guideline : guidelineResultList) {
        writer.write("### Guideline: " + guideline.getName() + "\n");
        writer.write("[guideline on PharmGKB]("+guideline.getUrl()+")\n");

        for (Group group : guideline.getMatchingGroups()) {
          writer.write("_");
          writer.write(group.getName());
          writer.write("_ ");
          writer.write(guideline.getMatchedDiplotypes().get(group.getId()).stream()
              .map(d -> "`"+d+"`")
              .collect(Collectors.joining(", ")));
          writer.write("\n\n");

          writer.write("|Type|Annotation|\n");
          writer.write("|---|---|\n");
          for (Annotation ann : group.getAnnotations()) {<commit_msg>Fix NPE when no annotation groups match.<commit_after>package org.pharmgkb.pharmcat.reporter.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.pharmgkb.pharmcat.reporter.model.Annotation;
import org.pharmgkb.pharmcat.reporter.model.CPICException;
import org.pharmgkb.pharmcat.reporter.model.Group;
import org.pharmgkb.pharmcat.reporter.resultsJSON.GeneReport;
import org.pharmgkb.pharmcat.reporter.resultsJSON.Interaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ReporterWriter {
  private static final Logger sf_logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  private static final String sf_outputFileName = "reporter.output.md";

  public static void printResults(Path outputDir, List<Interaction> guidelineResultList, Map<String, GeneReport> symbolToGeneReportMap) throws IOException {
    Path reportPath = outputDir.resolve(sf_outputFileName);
    sf_logger.info("Writing report to {}", reportPath);

    try (BufferedWriter writer = Files.newBufferedWriter(reportPath)) {
      writer.write("# PharmCAT Report\n\n\n");

      writer.write("## Haplotype Calls\n\n");
      for (String gene : symbolToGeneReportMap.keySet()) {
        writer.write("### Gene: " + gene + "\n");
        GeneReport geneReport = symbolToGeneReportMap.get(gene);

        if (geneReport.getDips().size() == 1) {
          writer.write("call: `" + geneReport.getDips().iterator().next() + "`\n");
        } else {
          writer.write("__possible calls__:\n\n");
          for (String dip : geneReport.getDips()) {
            writer.write(" * `" + dip + "`\n");
          }
          writer.write("\n");
        }

        writer.write("__warnings__: ");
        if (geneReport.getExceptionList() == null || geneReport.getExceptionList().size() == 0) {
          writer.write("none applicable\n");
        } else {
          writer.write("\n\n");
          for (CPICException exception : geneReport.getExceptionList()) {
            writer.write(" * "+exception.getMessage()+"\n");
          }
        }
        writer.write("\n");
      }

      writer.write("\n\n");

      writer.write("## Guidelines\n\n");

      for (Interaction guideline : guidelineResultList) {
        writer.write("### Guideline: " + guideline.getName() + "\n");
        writer.write("[guideline on PharmGKB]("+guideline.getUrl()+")\n");

        if (guideline.getMatchingGroups() == null) {
          writer.write("_no matching annotations found_\n");
          continue;
        }

        for (Group group : guideline.getMatchingGroups()) {
          writer.write("_");
          writer.write(group.getName());
          writer.write("_ ");
          writer.write(guideline.getMatchedDiplotypes().get(group.getId()).stream()
              .map(d -> "`"+d+"`")
              .collect(Collectors.joining(", ")));
          writer.write("\n\n");

          writer.write("|Type|Annotation|\n");
          writer.write("|---|---|\n");
          for (Annotation ann : group.getAnnotations()) {
{'fraction_non_alphanumeric': 0.15653008962868117, 'fraction_numerical': 0.0012804097311139564, 'mean_word_length': 3.0238248551191242, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2015527', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1824, 'n_words': 320}