# Snakefile
# Annotation script for feature retrieval
# CADD-SV

# this container defines the underlying OS for each job when using the workflow
# # with --use-conda --use-singularity
container: "docker://continuumio/miniconda3"
def as_list(x):
    if isinstance(x, (list, tuple)):
        return list(x)
    if x is None:
        return []
    if isinstance(x, str) and "," in x:
        return x.split(",")
    return [x]

DATASETS = as_list(config.get("dataset", []))
FORMAT = as_list(config.get("format", ["bed"]))[0]
FLANK = int(as_list(config.get("flank", [100]))[0])
MODE = config.get("mode", "scoring")
ANNOT_DIR = config.get("annotations_dir", "annotations")


CELLS = ["A549", "Caki2"]
TAD = ["nested", "tad"]

# set of encode datasets in annotation/
ENCODES = [
    "DNase-seq",
    "H2AFZ",
    "H3K27ac",
    "H3K27me3",
    "H3K36me3",
    "H3K4me1",
    "H3K4me2",
    "H3K4me3",
    "H3K79me2",
    "H3K9ac",
    "H3K9me3",
    "H4K20me1",
    "totalRNA-seq",
]

# set of genomegitar datasets for HIC directionality index
genomegitars = [
    "GSM1055800_DI",
    "GSM1055805_DI",
    "GSM1081530_DI",
    "GSM1267196_DI",
    "GSM1267200_DI",
    "GSM1294038_DI",
    "GSM1294039_DI",
    "GSM1551599_DI",
    "GSM1551629_DI",
    "GSM1608505_DI",
    "GSM1718021_DI",
    "GSM1906332_DI",
    "GSM1906333_DI",
    "GSM1906334_DI",
    "GSM1909121_DI",
    "GSM455133_DI",
    "GSM862723_DI",
    "GSM862724_DI",
    "GSM927075_DI",
]

# Cell lines from Encode for HIC datasets
CL = ["gm12878", "msc", "mes", "imr90", "h1"]

# Supported Formats
wildcard_constraints:
    format="bed|vcf"


include: "rules/fileprep.smk"
include: "rules/preprocessing.smk"
include: "rules/annotations.smk"
include: "rules/seqonly.smk"


def targets(wildcards):
    if config.get("mode", "training") == "scoring":
        return expand(
            "beds/{sets}/output/{sets}{formats}_score{flanks}.bed",
            sets=config["dataset"], formats=config["format"], flanks=config["flank"]
        )
    elif config.get("mode", "training") == "training":
        return expand(
            "beds/{sets}/{sets}{formats}_generated_models{flanks}.txt",
            sets=config["dataset"], formats=config["format"], flanks=config["flank"]
        )
    elif config.get("mode", "training") == "seqonly":
        # Sequence-only mode: score using seqonlymodel with SB, SBref, DB features
        return expand(
            "beds/{sets}/output/{sets}_seqonly_score.tsv",
            sets=config["dataset"]
        )
    else:
        return expand(
            "beds/{sets}/output/{sets}{formats}_score{flanks}.bed",
            sets=config["dataset"], formats=config["format"], flanks=config["flank"]
        )

rule all:
    input:
        matrix=targets