

from sequana.snaketools import SequanaConfig, FileFactory
cfg = SequanaConfig(config)

__identify_adapters__output = [
    "identify_adapters/adapters.txt",
    "identify_adapters/identify_adapters.done"]


if "adapter_removal_input" in config.keys():
    if config["adapter_removal_input"] == "bwa_bam_to_fastq":
        __identify_adapters__input = [x for x in __bwa_bam_to_fastq__output if "unmapped" in x]
else:
    __identify_adapters__input = cfg.DATASET[:]


if cfg.paired:
    rule identify_adapters:
        input:
            file1= __identify_adapters__input[0],
            file2= __identify_adapters__input[1]
        output:
            results=__identify_adapters__output[0],
            results_html= "identify_adapters.html",
            done=touch(__identify_adapters__output[1])
        params:
            # Not yet in bioconda so must be installed manually
            executable= cfg.config['adapter_removal']['executable']
        run:
            shell("{params.executable} --file1 {input.file1}"
            " --file2 {input.file2} --identify-adapters > {output.results}")
            shell("cp {output.results} {output.results_html}")
            with open(output.results_html, "w") as fout:
                with open(output.results, "r") as fin:
                    fout.write("<pre>\n")
                    fout.write(fin.read())
                    fout.write("</pre>")
else:
    rule identify_adapters:
        input:
            file1= cfg.DATASET[0],
        output:
            results=__identify_adapters__output[0],
            results_html= "report/identify_adapters.html",
            done=touch(__identify_adapters__output[1])
        params:
            # Not yet in bioconda so must be installed manually
            path_to_executable=cfg.config['adapter_removal']['executable']
        run:
            # Works only for paired so we trick the software to feed file2 (but
            # it is file1) so adapter1 and adapter2 should be identical !
            shell("{params.path_to_executable} "
            " --file1 {input.file1}"
            " --file2 {input.file1}"
            " --identify-adapters > {output.results}")
            with open(output.results_html, "w") as fout:
                with open(output.results, "r") as fin:
                    fout.write("<pre>\n")
                    fout.write(fin.read())
                    fout.write("</pre>")
