# Import libraries
import pandas as pd
from snakemake.utils import validate, min_version
from os import path
min_version("8") # Require at least Snakemake 8.0

# Load configuration
if not config:
    if path.exists("config/config.yaml"):
        configfile: "config/config.yaml"
    if not config:
        raise ValueError("No configuration file found.")
validate(config, schema="schemas/config.schema.yaml")

# Load samples
SAMPLES = (
    pd.read_csv(config["samples"], sep="\t", dtype={"accessionId": str})
        .set_index("accessionId", drop=False)
        .sort_index()
)
validate(SAMPLES, schema="schemas/samples.schema.yaml")

# Include modules
include: "rules/common.smk"
include: "rules/agat.smk"
include: "rules/1.assembly/all.smk"
include: "rules/2.annotation/all.smk"
include: "rules/3.quality_assessment/all.smk"

# Define report main page
report: "report/workflow.rst"

# Define global wildcard constraints
wildcard_constraints:
    minlen="\\d+",
    organelle="(mitochondrion|chloroplast|plastid)",

# Define output of all modules
rule all:
    input:
        "results/assembly.done",
        "results/annotation.done",
        "results/quality_assessment.done",

# Define messages for start, success and error
onstart:
    print("\033[1;34m"
    """
    ___  ___      _____   ___    ___    ___  ______
    |  \\/  |     |  __ \\ / _ \\  / _ \\  / _ \\ | ___ \\
    | .  . | ___ | |  \\// /_\\ \\/ /_\\ \\/ /_\\ \\| |_/ /
    | |\\/| |/ _ \\| | __ |  _  ||  _  ||  _  ||  __/
    | |  | | (_) | |_\\ \\| | | || | | || | | || |
    \\_|  |_/\\___/ \\____/\\_| |_/\\_| |_/\\_| |_/\\_|
    """
    "\033[0m"
    """
    Welcome to the MoGAAAP workflow.

    For citation information, see https://github.com/dirkjanvw/MoGAAAP
    """)

onsuccess:
    print("""
    MoGAAAP workflow finished successfully.

    For citation information, see https://github.com/dirkjanvw/MoGAAAP
    """)

onerror:
    print("""
    MoGAAAP workflow failed.
    Please check the provided error message and log file of the rule that failed above carefully before reporting any issues at https://github.com/dirkjanvw/MoGAAAP/issues for help.
    """)
