NONPAREIL PLOT

https://img.shields.io/badge/wrapper_version-v9.2.0-10785b https://img.shields.io/github/issues-pr/snakemake/snakemake-wrappers/bio/nonpareil/plot?label=version%20update%20pull%20requests&color=1cb481

Plot Nonpareil results.

URL: https://nonpareil.readthedocs.io/en/latest/

Example

This wrapper can be used in the following way:

rule test_nonpareil_plot:
    input:
        npo="{sample}.npo",
    output:
        pdf="results/{sample}.pdf",
        tsv="results/{sample}.tsv",
        json="results/{sample}.json",
    threads: 1
    log:
        "logs/{sample}.log",
    params:
        extra=lambda w: f"--labels {w.sample} --col blue",
    wrapper:
        "v9.2.0/bio/nonpareil/plot"


use rule test_nonpareil_plot as test_nonpareil_plot_multiple with:
    input:
        npo=["a.npo", "b.npo", "c.npo", "d.npo"],
    output:
        pdf="results/samples.pdf",
        tsv="results/samples.tsv",
        json="results/samples.json",
    log:
        "logs/samples.log",
    params:
        extra="--labels 'Model A,Model B,Model C,Model D' --col blue,red,green,yellow",


use rule test_nonpareil_plot as test_nonpareil_plot_nomodel with:
    output:
        pdf="results/{sample}.nomodel.pdf",
        tsv="results/{sample}.nomodel.tsv",
        json="results/{sample}.nomodel.json",
    log:
        "logs/{sample}.nomodel.log",
    params:
        extra=lambda w: f"--labels {w.sample} --col blue --no-model",

Note that input, output and log file paths can be chosen freely.

When running with

snakemake --use-conda

the software dependencies will be automatically deployed into an isolated environment before execution.

Software dependencies

  • nonpareil=3.5.5

Input/Output

Input:

  • NPO file(s)

Output:

  • PDF file with plot (optional)

  • TSV file with summary stats (optional)

  • JSON file with detailed info (optional)

Params

  • extra: additional program arguments (not –pdf, –tsv, or –json).

Authors

  • Filipe G. Vieira

Code

__author__ = "Filipe G. Vieira"
__copyright__ = "Copyright 2024, Filipe G. Vieira"
__license__ = "MIT"


import tempfile
from pathlib import Path
from snakemake.shell import shell

extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell(stdout=True, stderr=True)


with tempfile.NamedTemporaryFile() as tmp:

    for output in snakemake.output:
        ext = Path(output).suffix.lstrip(".")
        if ext in ["json", "tsv", "pdf"]:
            if ext == "json":
                extra += f" --{ext} {tmp.name}"
            else:
                extra += f" --{ext} {output}"

    shell("NonpareilCurves.R {extra} {snakemake.input} {log}")

    # Fix JSON output file
    # https://github.com/lmrodriguezr/nonpareil/issues/70
    json_out = snakemake.output.get("json")
    if json_out:
        import json
        import numpy as np

        with open(tmp.name, "rt") as json_f:
            json_data = json.load(json_f)
            for key, val in json_data.items():
                val["x.adj"] = [np.nan if x == "NaN" else x for x in val["x.adj"]]
        with open(json_out, "wt") as json_f:
            json.dump(json_data, json_f, indent=4)