__sequana__alien_trimmer = [":fwd", ":rev"]

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


if len(cfg.DATASET) == 2:
    __alien_trimmer__output.append(prefix + 
        cfg.BASENAME[1].replace(".fastq.gz", ".alien_trimmer.fastq.gz"))


if "adapter_removal_input" in config.keys():
    if config["adapter_removal_input"] == "bwa_bam_to_fastq":
        __alien_trimmer__input = [cfg.PROJECT + "/bwa_bam_to_fastq/" + this for this in cfg.BASENAME]

    __alien_trimmer__input = [this.replace("_001.", ".unmapped.") for this in __alien_trimmer__input]

else:
    __alien_trimmer__input = cfg.DATASET[:]


if len(cfg.DATASET) == 2:
    rule alien_trimmer:
        input: 
            file1 = cfg.DATASET[0],
            file2 = cfg.DATASET[1]
        output:
            file1 = cfg.PROJECT + "/alien_trimmer/" + "at_" + cfg.BASENAME[0],
            file2 = cfg.PROJECT + "/alien_trimmer/" + "at_" + cfg.BASENAME[1],
            excluded = cfg.PROJECT+"/alien_trimmer/excluded.fastq",
            done=touch('alien.done')
        params:
            fwd= cfgca.fwd,
            rev= cfgca.rev,
        log: cfg.PROJECT + "/logs/alien_trimmer.txt"
        run:
            shell("java -jar AlienTrimmer.jar "
                    " -cf {params.fwd} -cr {params.rev} "
                    " -if {input.file1} "
                    " -ir {input.file2}"
                    " -of {output.file1}"
                    " -or {output.file2}"
                    " -os {output.excluded}")

            # to be fair we other tools, and gain space, we should also zip the
            # files. Should use pigz but not fair comparison. For the benchmark,
            # we will use the gzip option for now.
            shell("gzip {input.file1")
            shell("gzip {input.file2")
else:
    rule alien_trimmer:
        input: 
            file1 = cfg.DATASET[0],
        output:
            file1 = cfg.PROJECT + "/alien_trimmer/" + "at_" + cfg.BASENAME[0],
            excluded = cfg.PROJECT+"/alien_trimmer/excluded.fastq",
            done=touch('alien.done')
        params:
            fwd= cfgca.fwd,
        log: cfg.PROJECT + "/logs/alien_trimmer.txt"
        run:
            shell("java -jar AlienTrimmer.jar "
                    " -cf {params.fwd} -cr {params.rev} "
                    " -if {input.file1} "
                    " -of {output.file1}"
                    " -os {output.excluded}")

            # to be fair we other tools, and gain space, we should also zip the
            # files. Should use pigz but not fair comparison. For the benchmark,
            # we will use the gzip option for now.
            shell("gzip {input.file1")

