# ruleorder: skip_long_assembly > get_reads_list_ref > link_reads > short_only
# ruleorder: filtlong_no_reference > link_reads

onsuccess:
    import os
    log_list = log if isinstance(log, list) else [log]
    flat_logs = [item for sublist in log_list for item in (sublist if isinstance(sublist, list) else [sublist])]
    for l in flat_logs:
        if isinstance(l, str) and os.path.exists(l):
            shell("chmod g+w " + l)
    print("Aviary finished, no error")

onerror:
    import os
    log_list = log if isinstance(log, list) else [log]
    flat_logs = [item for sublist in log_list for item in (sublist if isinstance(sublist, list) else [sublist])]
    for l in flat_logs:
        if isinstance(l, str) and os.path.exists(l):
            shell("chmod g+w " + l)
    print("An error occurred")

onstart:
    import os
    import sys

    from snakemake.utils import logger, min_version

    # minimum required snakemake version
    min_version("6.0")
    long_reads = config["long_reads"]
    fasta = config["fasta"]
    short_reads_1 = config["short_reads_1"]
    short_reads_2 = config["short_reads_2"]
    min_contig_size = config["min_contig_size"]
    min_bin_size = config["min_bin_size"]
    gtdbtk_folder = config["gtdbtk_folder"]
    busco_folder = config["busco_folder"]
    threads = config["max_threads"]
    ## pplacer deadlocks on too many threads
    pplacer_threads = min(48, int(config["pplacer_threads"]))

    if long_reads == "none" and short_reads_1 == "none" and not config.get("skip_reads_check", False):
        sys.exit("Need at least one of long_reads or short_reads_1")
    if long_reads != "none" and not os.path.exists(long_reads[0]):
        sys.exit("long_reads does not point to a file")
    if short_reads_1 != "none" and not os.path.exists(short_reads_1[0]):
        sys.exit("short_reads_1 does not point to a file")
    if short_reads_2 != "none" and not os.path.exists(short_reads_2[0]):
        sys.exit("short_reads_2 does not point to a file")
    if gtdbtk_folder != "none" and not os.path.exists(gtdbtk_folder):
        sys.stderr.write("gtdbtk_folder does not point to a folder\n")
    if busco_folder != "none" and not os.path.exists(busco_folder):
        sys.stderr.write("busco_folder does not point to a folder\n")


from snakemake.utils import min_version
import os
min_version("6.0")
os.umask(0o002)

module qc:
    snakefile: "quality_control/qc.smk"
    config: config

module cluster:
    snakefile: "cluster/clustering.smk"
    config: config

module assembly:
    snakefile: "assembly/assembly.smk"
    config: config

module isolate:
    snakefile: "assembly/isolate.smk"
    config: config

module binning:
    snakefile: "binning/binning.smk"
    config: config

module annotation:
    snakefile: "annotation/annotation.smk"
    config: config

use rule * from qc as *

use rule * from cluster as *

use rule * from assembly as *

use rule * from isolate as *

use rule * from binning as *

use rule * from annotation as *

if config["fasta"] == "none":
    config["fasta"] = "assembly/final_contigs.fasta"
