# Pipeline

##############################
# MODULES
import os
from tempfile import TemporaryDirectory

##############################
# CONFIG
# can be overwritten by using --configfile <path to config> when calling snakemake
configfile: "config/CONFIG.yaml"

# Paths
SRC_DIR = srcdir("workflow/scripts")
ENV_DIR = srcdir("workflow/envs")
DATA_DIR = config["data_dir"]
RESULTS_DIR = config["results_dir"]
DB_DIR = config["db_dir"]

# Steps
STEPS = config['steps']
ANALYSIS_STEPS = config["analysis_steps"]

# Input
BARCODES = config["barcodes"]
RUNS = [
    config["runs"]["first"],
    config["runs"]["second"],
#    config["runs"]["third"]
]
SAMPLES = config["samples"]
BINNING_SAMPLES = config["binning_samples"]

# References
IGC_URI = config["igc"]["uri"]
HG38_URI = config["hg38"]["uri"]
REFERENCES = ["igc", "hg38"]

# Tools
ASSEMBLERS = config["assemblers"]
HYBRID_ASSEMBLER = config["hybrid_assembler"]
MAPPERS = ["bwa", "mmi"]

# default executable for snakmake
shell.executable("bash")

# working directory
workdir:
    config["work_dir"]

##############################
# TARGETS & RULES
# List of targets to be created
TARGETS = []

# Include rules and add targets based on the config file

# Assembly annotation
if 'assembly_annotation' in STEPS:
    include:
        "workflow/steps/assembly_annotation.smk"
    TARGETS += [
        "assemble_and_coverage.done",
        "annotate.done",
        "basecall_merge_qc.done",
        "coverage_of_references.done",
        "prodigal_gene_call.done",
        "diamond_proteins.done"
    ]

if 'checkpoint_assembly_annotation' in STEPS:
    include:
        "workflow/steps/checkpoint_assembly_annotation.smk"
    TARGETS += [
        "assemble_and_coverage.done",
        "annotate.done",
        "basecall_merge_qc.done",
        "basecall_merge_qc_NO_MOD.done",
        "coverage_of_references.done",
        "prodigal_gene_call.done",
        "diamond_proteins.done",
        "dummy_folders_created.done"
    ]

# MMSEQ2
if 'mmseq' in STEPS:
    include:
        "workflow/steps/mmseq.smk"
    TARGETS += [
        "mmseq_comparison_for_ont.done"
    ]

# MetaT
if 'metaT' in STEPS:
    include:
        "workflow/steps/metat.smk"
    TARGETS += ["metaT_mapping_for_ONT.done"]

# Mapping
if 'mapping' in STEPS:
    include:
        "workflow/steps/mapping.smk"
    TARGETS += ["mapping_for_binning.done"]

# Binning
if 'binning' in STEPS:
    include:
        "workflow/steps/binning.smk"
    TARGETS += ["binning_for_ont.done"]

# Taxonomy
if 'taxonomy' in STEPS:
    include:
        "workflow/steps/taxonomy.smk"
    TARGETS += ["taxonomy_for_ont.done"]

# Analysis
if 'analysis' in STEPS:
    include:
        "workflow/steps/analysis.smk"
    # CD-HIT
    if "cdhit" in ANALYSIS_STEPS:
        TARGETS.append("cdhit_analysis.done")
    # Mappability
    if "mappability" in ANALYSIS_STEPS:
        TARGETS.append("mappability_index.done")
    # CRISPR
    if "crispr" in ANALYSIS_STEPS:
        TARGETS.append("crispr_analysis.done")
    # Plasmid prediction
    if "plasmids" in ANALYSIS_STEPS:
        TARGETS.append("plasmids_analysis.done")
    # AMR prediction
    if "amr" in ANALYSIS_STEPS:
        TARGETS.append("amr_analysis.done")

# No targets
if len(TARGETS) == 0:
   raise Exception('You are not serious. Nothing to be done? Really?')

rule all:
    input:
        TARGETS