# __author__ = "Charlie Pauvert"
# __copyright__ = "Copyright 2020, Charlie Pauvert"
# __license__ = "MIT"

include: "rules/common.smk"

rule all:
    input:
        expand("results/PLN/plots/plot_network_{criteria}.png", criteria=config["criteria"]),
        "results/report_PLNnetwork.html"


rule pln_prepare_data:
    input:
        counts=config["counts"],
        covariates=config["covariates"]
    output:
        "results/PLN/RDS/prepared_data.RDS"
    # Optional arguments for `prepare_data()`
    # Please provide them as python `key=value` pairs
    #params:
    #    offset="none"
    log:
        "results/logs/prepare_data.log"
    conda:
        "envs/PLNnetwork.yaml"
    script:
        "scripts/prepare_data.R"

rule pln_infer_networks:
    input:
        "results/PLN/RDS/prepared_data.RDS"
    output:
        models="results/PLN/RDS/networks.RDS",
        plot=report("results/PLN/plots/plot_models.png",
         caption="report/models.rst", category="Fit network models"),
        table="results/PLN/RDS/table_models.RDS"
    # Optional arguments for ``PLNnetwork()``
    # Please provide them as python ``key=value`` pairs
    params:
        # The formula, as character here, is interpreted by R
        formula=config["formula"]
    log:
        "results/logs/infer_networks.log"
    conda:
        "envs/PLNnetwork.yaml"
    threads: 1 # set desired number of threads here
    script:
        "scripts/infer_networks.R"

rule pln_stability_selection:
# Dedicated rule for stability selection to be adapt to threads number
    input:
        "results/PLN/RDS/networks.RDS"
    output:
        models="results/PLN/RDS/networks_stars.RDS",
        plot=report("results/PLN/plots/plot_stars.png",
         caption="report/stars.rst", category="StARS network selection")
    # Optional arguments for ``stability_selection()``
    # Please provide them as python ``key=value`` pairs
    #params:
    log:
        "results/logs/stability_selection.log"
    conda:
        "envs/PLNnetwork.yaml"
    threads: 1 # set desired number of threads here
    script:
        "scripts/stability_selection.R"

rule pln_pick_network:
    input:
        expand("results/PLN/RDS/networks{suffix}.RDS",
                suffix="_stars" if "StARS" in config["criteria"] else "")
    output:
        pln="results/PLN/RDS/network_{criteria}.RDS",
        igraph="results/PLN/RDS/igraph_{criteria}.RDS"
    # Optional arguments for ``getBestModel()``
    # Please provide them as python ``key=value`` pairs
    params:
        stability=config["stability"] if "StARS" in config["criteria"] else ""
    log:
        "results/logs/pick_network_{criteria}.log"
    conda:
        "envs/PLNnetwork.yaml"
    threads: 1 # set desired number of threads here
    script:
        "scripts/pick_network.R"

rule pln_plot_network:
    input:
       "results/PLN/RDS/igraph_{criteria}.RDS"
    output:
        report("results/PLN/plots/plot_network_{criteria}.png",
         caption="report/network.rst", category="Plot network", subcategory="{criteria}")
    log:
        "results/logs/plot_network_{criteria}.log"
    conda:
        "envs/ggraph.yaml"
    threads: 1 # set desired number of threads here
    script:
        "scripts/plot_network.R"

rule report_pln:
    input:
        table_models="results/PLN/RDS/table_models.RDS",
        network=expand("results/PLN/RDS/igraph_{criteria}.RDS", criteria=config["criteria"])
    output:
        "results/report_PLNnetwork.html"
    log:
        "results/logs/report.log"
    conda:
        "envs/report.yaml"
    threads: 1 # set desired number of threads here
    script:
        "report/report_PLNnetwork.Rmd"
