from snakemake.io import load_configfile


# setup workflow
def load_config(path):
    config = load_configfile(path)
    config["subworkflow_run"] = True
    return config


onstart:
    shell("Rscript -e 'devtools::install(quick = TRUE, dependencies = FALSE)'")


# import modules
module dispersion_fitting:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_dispersion_fitting/config.yaml")
    replace_prefix:
        {"results/": "results/dispersion_fitting/"}


use rule * from dispersion_fitting as dispersion_fitting_*


module response_distribution:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_response_distribution/config.yaml")
    replace_prefix:
        {"results/": "results/response_distribution/"}


use rule * from response_distribution as response_distribution_*


module regularization_parameter:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_regularization_parameter/config.yaml")
    replace_prefix:
        {"results/": "results/regularization_parameter/"}


use rule * from regularization_parameter as regularization_parameter_*


module noise:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_noise/config.yaml")
    replace_prefix:
        {"results/": "results/noise/"}


use rule * from noise as noise_*


module regularization_effect:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_regularization_effect/config.yaml")
    replace_prefix:
        {"results/": "results/regularization_effect/"}


use rule * from regularization_effect as regularization_effect_*


module real_datasets:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_real_datasets/config.yaml")
    replace_prefix:
        {"results/": "results/real_datasets/"}


use rule * from real_datasets as real_datasets_*


module similarity_measures:
    snakefile:
        "../../../workflow/Snakefile"
    config:
        load_config("config_similarity_measures/config.yaml")
    replace_prefix:
        {"results/": "results/similarity_measures/"}


use rule * from similarity_measures as similarity_measures_*


# custom logic
rule compare_rocauc_vs_loss:
    input:
        fname_aucs="results/regularization_parameter/aggregated_plots/roc_aucs.csv",
        fname_enr_list=expand(
            f"results/regularization_parameter/enrichments/{regularization_parameter.paramspace.wildcard_pattern}/replicates/{{replicate}}/{{method}}/enrichment_result.csv",
            replicate=range(regularization_parameter.config["replicate_count"]),
            method=regularization_parameter.method_list,
            allow_missing=True,
        ),
        fname_study_list=[
            f"results/regularization_parameter/studies/{regularization_parameter.paramspace.wildcard_pattern}/replicates/{replicate}/study.rds"
            for replicate in [
                f"{r}_testsamples"
                for r in range(regularization_parameter.config["replicate_count"])
            ]
            for method in regularization_parameter.method_list
        ],
    output:
        outdir=directory(
            f"results/regularization_parameter/rocauc_vs_loss/{regularization_parameter.paramspace.wildcard_pattern}/"
        ),
    resources:
        mem_mb=20_000,
    script:
        "scripts/compare_rocauc_vs_loss.R"


# define required output files
rule all:
    input:
        # rules.dispersion_fitting_all.input,
        rules.response_distribution_all.input,
        # rules.regularization_effect_all.input,
        # rules.regularization_parameter_all.input,
        # expand(
        #     "results/regularization_parameter/rocauc_vs_loss/{params}/",
        #     params=regularization_parameter.paramspace.instance_patterns,
        # ),
        # rules.noise_all.input,
        # rules.real_datasets_all.input,
        # rules.similarity_measures_all.input,
    default_target: True
