import os
import shutil
import glob
import seqnado.utils as utils
import pandas as pd

ASSAY = "RNA"


configfile: "config_rna.yml"


container: "library://asmith151/seqnado/seqnado_pipeline:latest"


utils.format_config_dict(config)

# Get experiment design
if os.path.exists(config["design"]):
    # Expect columns - sample fq1 fq2
    FASTQ_SAMPLES = utils.GenericFastqSamples(
        pd.read_csv(config["design"], sep="[\s+,\t]", engine="python")
    )
    assert FASTQ_SAMPLES.design.shape[0] > 0, "No samples found in design file"
    for col in ["sample", "fq1", "fq2"]:
        assert (
            col in FASTQ_SAMPLES.design.columns
        ), f"Design file must contain columns sample, fq1, fq2. Columns found: {FASTQ_SAMPLES.design.columns}"
else:
    # Use pattern matching to get samples
    fq_files = list(utils.get_fastq_files("."))
    if fq_files:
        FASTQ_SAMPLES = utils.GenericFastqSamples.from_files(fq_files)
    else:
        raise ValueError("No FASTQ files found in the working directory")


DESIGN = FASTQ_SAMPLES.design
SAMPLE_NAMES = FASTQ_SAMPLES.sample_names_all
RUN_DESEQ2 = DESIGN.columns.str.contains("deseq2").any()


include: "rules/qc.smk"
include: "rules/fastq_trim.smk"
include: "rules/align_rna.smk"
include: "rules/alignment_post_processing.smk"
include: "rules/alignment_counts.smk"
include: "rules/pileup_rna.smk"
include: "rules/deseq2_rna.smk"
include: "rules/hub.smk"


# Define output files
ANALYSIS_OUTPUT = utils.define_output_files(
    sample_names=SAMPLE_NAMES, assay=ASSAY, run_deseq2=RUN_DESEQ2, **config
)


rule all:
    input:
        ANALYSIS_OUTPUT,


onsuccess:
    slurm_files = glob.glob("slurm-*.out")
    sps_files = glob.glob("sps-*")

    for fn in [*slurm_files, *sps_files]:
        try:
            if not os.path.isdir(fn):
                os.remove(fn)
            else:
                shutil.rmtree(fn)

        except Exception as e:
            print(e)


onerror:
    log_out = "seqnado_error.log"
    shutil.copyfile(log, log_out)
    print(
        f"An error occurred. Please check the log file {log_out} for more information."
    )
