rule all:
    input:
        "figs/citations+development.svg",
        "plots/benchmark-example.svg",


rule clone_repo:
    output:
        directory("data/snakemake-git-repo")
    conda:
        "envs/git.yaml"
    shell:
        "git clone https://github.com/snakemake/snakemake data/snakemake-git-repo"


rule collect_commits_and_releases:
    input:
        "data/snakemake-git-repo"
    output:
        "tables/git-log.csv"
    conda:
        "envs/git.yaml"
    shell:
        "(cd {input}; git --no-pager log --pretty='format:%as;%d') > {output}"


rule plot_commits_and_releases:
    input:
        "tables/git-log.csv"
    output:
        report("plots/commits+releases.svg", category="Plots", caption="report/commits+releases.rst")
    conda:
        "envs/stats.yaml"
    notebook:
        "notebooks/commits+releases.py.ipynb"


rule plot_citations:
    input:
        "data/citations.tsv"
    output:
        report("plots/citations.svg", category="Plots", caption="report/citations.rst")
    conda:
        "envs/stats.yaml"
    notebook:
        "notebooks/citations.py.ipynb"


rule plot_citations_per_category:
    input:
        "data/citations-per-category.tsv"
    output:
        report("plots/citations-per-category.svg", category="Plots", caption="report/citations-per-category.rst")
    conda:
        "envs/stats.yaml"
    notebook:
        "notebooks/citations-per-category.py.ipynb"


rule fig_citations_and_development:
    input:
        a="plots/citations.svg",
        b="plots/citations-per-category.svg",
        c="plots/commits+releases.svg",
    output:
        "figs/citations+development.svg"
    conda:
        "envs/svgutils.yaml"
    notebook:
        "notebooks/fig-citations+development.py.ipynb"


rule benchmark_example:
    input:
        "resources/hello-world"
    output:
        touch("profiles/example/{replicates}.profile={profile}.txt")
    benchmark:
        "benchmarks/example/{replicates}.profile={profile}.tsv"
    params:
        profile=lambda w, output: "--runtime-profile ../../{}".format(output) if w.profile == "true" else ""
    shell:
        "(cd {input}; snakemake {params.profile} --config replicates={wildcards.replicates} -n) > /dev/null"


country_replicates = [1] + list(range(1000, 10001, 1000))


rule plot_benchmark_results:
    input:
        benchmarks=expand("benchmarks/example/{replicates}.profile=false.tsv", replicates=country_replicates),
    output:
        report("plots/benchmark-example.svg", category="Plots", caption="report/benchmark.rst")
    params:
        replicates=country_replicates
    conda:
        "envs/stats.yaml"
    notebook:
        "notebooks/plot-benchmark-example.py.ipynb"


rule plot_profile_results:
    input:
        profiles=expand("profiles/example/{replicates}.profile=true.txt", replicates=[100, 1000, 5000])
    output:
        "plots/profiles.svg"
    params:
        replicates=[100, 500, 1000]
    conda:
        "envs/stats.yaml"
    notebook:
        "notebooks/plot-profiles.py.ipynb"
