__sequana__cutadapt = [":fwd", ":rev", ":m", ":q"]

from sequana import SequanaConfig
cfg = SequanaConfig(config)
cfgca = cfg.config.adapter_removal


#DOC AdapterRemoval: https://github.com/MikkelSchubert/adapterremoval

__adapter_removal__output = [prefix + cfg.BASENAME[0].replace(".fastq.gz", ".adapter_removal.fastq.gz")]


# NOTE: "adapter_removal_input" is used by alien_trimmer, cutadapt, and
# adapter_removal rules to cite a few. Note that the word
# "adapter_removal_input" is confusing since it is alsmost the same as the
# name of the rule in this snakefile... 
# 
if "adapter_removal_input" in config.keys():
    if config["adapter_removal_input"] == "bwa_bam_to_fastq":
        __adapter_removal__input = [cfg.PROJECT 
            + "/bwa_bam_to_fastq/" + this for this in cfg.BASENAME]
    __adapter_removal__input = [this.replace("_001.", ".unmapped.") 
            for this in __adapter_removal__input]
else:
    __adapter_removal__input = cfg.DATASET[:]


if len(cfg.DATASET) == 2:
    rule adapter_removal:
        input: 
            file1 = __adapter_removal__input[0],
            file2 = __adapter_removal__input[1]
        output:
            file1 = __adapter_removal__output[0],
            file2 = __adapter_removal__output[1]
            discarded = cfg.PROJECT+"/adapter_removal/discarded.fastq",
            done=touch('adapter_removal.done')
        params:
            fwd= cfgca.fwd,
            rev= cfgca.rev,
        log: cfg.PROJECT + "/logs/adapter_removal.txt"
        threads: 4
        run:
            shell("/home/cokelaer/Work/github/adapter_removal/build/AdapterRemoval "
                    " --file1 {input.file1}"
                    " --file2 {input.file2}"
                    " --output1 {output.file1}"
                    " --output2 {output.file2}"
                    " --discarded {output.discarded}"
                    "--threads {threads]"
                    " --gzip")
else:
    rule adapter_removal:
        input:
            file1 = __adapter_removal__input[0],
        output:
            file1 = __adapter_removal__output[0],
        params:
            fwd= cfgca.fwd,
            m= cfgca.m,
            quality= cfgca.quality,
            kwargs= cfgca.kwargs
        log: cfg.PROJECT + "/logs/adapter_removal.txt"
        run:
            shell("/home/cokelaer/Work/github/adapter_removal/build/AdapterRemoval "
                    " --file1 {input.file1}"
                    " --output1 {output.file1}"
                    " --discarded {output.discarded}"
                    "--threads {threads]"
                    " --gzip")
