Esempio n. 1
0
def download_hpo_genes(institute_id, case_name, category):
    """Download the genes contained in a case dynamic gene list

    Args:
        institute_id(str):  id for current institute
        case_name(str):     name for current case
        category(str):      variant category - "clinical" or "research"
                            "research" retrieves all gene symbols for each HPO term on the dynamic phenotype panel
                            "clinical" limits dynamic phenotype panel retrieval to genes present on case clinical genes
    """

    institute_obj, case_obj = institute_and_case(store, institute_id,
                                                 case_name)
    # Create export object consisting of dynamic phenotypes with associated genes as a dictionary
    is_clinical = category != "research"
    phenotype_terms_with_genes = controllers.phenotypes_genes(
        store, case_obj, is_clinical)
    html_content = ""
    for term_id, term in phenotype_terms_with_genes.items():
        html_content += f"<hr><strong>{term_id} - {term.get('description')}</strong><br><br><font style='font-size:16px;'><i>{term.get('genes')}</i></font><br><br>"

    bytes_file = html_to_pdf_file(html_content, "portrait", 300)
    file_name = "_".join([
        case_obj["display_name"],
        datetime.datetime.now().strftime("%Y-%m-%d"),
        category,
        "dynamic_phenotypes.pdf",
    ])
    return send_file(
        bytes_file,
        download_name=file_name,
        mimetype="application/pdf",
        as_attachment=True,
    )
Esempio n. 2
0
def delivery_report(institute_id, case_name):
    """Display delivery report."""
    institute_obj, case_obj = institute_and_case(store, institute_id,
                                                 case_name)
    if case_obj.get("delivery_report") is None:
        return abort(404)

    date_str = request.args.get("date")
    if date_str is not None:
        delivery_report = None
        for analysis_data in case_obj.get("analyses", []):
            if str(analysis_data["date"].date()) == date_str:
                delivery_report = analysis_data["delivery_report"]
        if delivery_report is None:
            return abort(404)
    else:
        delivery_report = case_obj["delivery_report"]

    out_dir = os.path.abspath(os.path.dirname(delivery_report))
    filename = os.path.basename(delivery_report)

    report_format = request.args.get("format", "html")
    if report_format == "pdf":
        try:  # file could not be available
            html_file = open(delivery_report, "r")
            source_code = html_file.read()

            bytes_file = html_to_pdf_file(source_code, "portrait", 300)
            file_name = "_".join([
                case_obj["display_name"],
                datetime.datetime.now().strftime("%Y-%m-%d"),
                "scout_delivery.pdf",
            ])
            return send_file(
                bytes_file,
                download_name=file_name,
                mimetype="application/pdf",
                as_attachment=True,
            )
        except Exception as ex:
            flash(
                "An error occurred while downloading delivery report {} -- {}".
                format(delivery_report, ex),
                "warning",
            )
            LOG.error(
                f"An error occurred while downloading delivery report {delivery_report} -- {ex}"
            )

    return send_from_directory(out_dir, filename)
Esempio n. 3
0
def pdf_case_report(institute_id, case_name):
    """Download a pdf report for a case"""

    institute_obj, case_obj = institute_and_case(store, institute_id,
                                                 case_name)
    data = controllers.case_report_content(store, institute_id, case_name)

    # add coverage report on the bottom of this report
    if (current_app.config.get("SQLALCHEMY_DATABASE_URI")
            and case_obj.get("track", "rare") != "cancer"):
        data["coverage_report"] = controllers.coverage_report_contents(
            request.url_root, institute_obj, case_obj)

    # Workaround to be able to print the case pedigree to pdf
    if case_obj.get("madeline_info") and case_obj.get("madeline_info") != "":
        try:
            write_to = os.path.join(cases_bp.static_folder, "madeline.png")
            svg2png(
                bytestring=case_obj["madeline_info"],
                write_to=write_to,
            )  # Transform to png, since PDFkit can't render svg images
            data["case"]["madeline_path"] = write_to
        except Exception as ex:
            LOG.error(
                f"Could not convert SVG pedigree figure {case_obj['madeline_info']} to PNG: {ex}"
            )

    html_report = render_template("cases/case_report.html",
                                  format="pdf",
                                  **data)

    bytes_file = html_to_pdf_file(html_string=html_report,
                                  orientation="portrait",
                                  dpi=300,
                                  zoom=0.6)
    file_name = "_".join([
        case_obj["display_name"],
        datetime.datetime.now().strftime("%Y-%m-%d"),
        "scout_report.pdf",
    ])
    return send_file(
        bytes_file,
        download_name=file_name,
        mimetype="application/pdf",
        as_attachment=True,
    )
def test_html_to_pdf_file():
    """Test function that converts HTML file into pdf file using PDFKit"""

    test_content = """
        <!DOCTYPE html>
        <html>
        <head>
        <title>A demo html page</title>
        </head>
        <body>
        <p>Hello world!</p>
        </body>
        </html>
    """

    # GIVEN an HTML report to be converted to PDF:
    bytes_file = html_to_pdf_file(test_content, "landscape", 300)
    assert isinstance(bytes_file, BytesIO)
Esempio n. 5
0
def panel_export(panel_id):
    """Export panel to PDF file"""
    panel_obj = store.panel(panel_id)
    data = controllers.panel_export(store, panel_obj)
    data["report_created_at"] = datetime.datetime.now().strftime("%Y-%m-%d")
    html_report = render_template("panels/panel_pdf_simple.html", **data)

    bytes_file = html_to_pdf_file(html_report, "portrait", 300)
    file_name = "_".join([
        data["panel"]["panel_name"],
        str(data["panel"]["version"]),
        datetime.datetime.now().strftime("%Y-%m-%d"),
        "scout.pdf",
    ])
    return send_file(
        bytes_file,
        download_name=file_name,
        mimetype="application/pdf",
        as_attachment=True,
    )
Esempio n. 6
0
def coverage_qc_report(institute_id, case_name):
    """Display coverage and qc report."""
    _, case_obj = institute_and_case(store, institute_id, case_name)
    coverage_qc_report = case_obj.get("coverage_qc_report")

    if coverage_qc_report is None:
        return abort(404)

    report_format = request.args.get("format", "html")

    out_dir = os.path.abspath(os.path.dirname(coverage_qc_report))
    filename = os.path.basename(coverage_qc_report)

    if report_format == "pdf":
        try:
            with open(os.path.abspath(coverage_qc_report), "r") as html_file:
                source_code = html_file.read()
                bytes_file = html_to_pdf_file(source_code, "landscape", 300)
                file_name = "_".join([
                    case_obj["display_name"],
                    datetime.datetime.now().strftime("%Y-%m-%d"),
                    "coverage_qc_report.pdf",
                ])
                return send_file(
                    bytes_file,
                    download_name=file_name,
                    mimetype="application/pdf",
                    as_attachment=True,
                )
        except Exception as ex:
            flash(
                "An error occurred while converting report to PDF: {} -- {}".
                format(coverage_qc_report, ex),
                "warning",
            )
            LOG.error(ex)

    return send_from_directory(out_dir, filename)