

rule cutadapt:
    """Cutadapt (adapter removal)

    Required input:
        - __cutadapt__input_fastq

    Required output:
        - __cutadapt__output

    Required parameters:
        - __cutadapt__fwd: forward adapters as a file, or string
        - __cutadapt__rev: reverse adapters as a file, or string
        - __cutadapt__options, 
        - __cutadapt__mode, # g for 5' adapter, a for 3' and b 
          for both 5'/3' (see cutadapt doc for details)
        - __cutadapt__wkdir,
        - __cutadapt__design,
        - __cutadapt__design_adapter,
        - __cutadapt__sample

    Other requirements:
        - __cutadapt__log

    Required configuration:
        .. code-block:: yaml

            cutadapt:
                do: yes
                tool_choice: cutadapt
                design: "%(adapter_design)s"
                adapter_choice: "%(adapter_type)s"
                fwd: "%(adapter_fwd)s"
                rev: "%(adapter_rev)s"
                m: 20   # cutoff
                mode: "g"   # g for 5' adapter, a for 3' and b for both 5'/3'
                quality: "30"
                options: "-O 6 --trim-n"

    References:
        http://cutadapt.readthedocs.io/en/stable/index.html
    """
    input:
        fastq = __cutadapt__input_fastq
    output:
        fastq = __cutadapt__output
    params:
        fwd = __cutadapt__fwd,
        rev = __cutadapt__rev,
        m = config['cutadapt']['m'],   # cutoff
        quality = config['cutadapt']['quality'],
        kwargs = __cutadapt__options,
        mode = __cutadapt__mode,
        wkdir = __cutadapt__wkdir,
        design = __cutadapt__design,
        design_adapter = __cutadapt__design_adapter,
        sample = __cutadapt__sample,
        thread = config['cutadapt']['threads']
    log: __cutadapt__log
    run:
        executable = config['cutadapt']['tool_choice'] # could be cutadapt or atropos

        if config['cutadapt']['design_file']:
            from sequana import FindAdaptersFromDesign
            # FIXME how important is this that the sample name contains a _
            # character ?
            sample = params.sample.split("_")[0]
            fa = FindAdaptersFromDesign(params.design, params.design_adapter)
            fwd, rev = fa.save_adapters_to_fasta(sample, output_dir=params.wkdir)
            fwd = "file:%s" % fwd
            rev = "file:%s" % rev
        elif params.design_adapter:
            from sequana.adapters import get_sequana_adapters
            fwd = "file:%s" % get_sequana_adapters(params.design_adapter, "fwd")
            rev = "file:%s" % get_sequana_adapters(params.design_adapter, "rev")
        else:
            fwd = params.fwd
            rev = params.rev

        if len(input.fastq) == 2:
            if executable == "atropos":
                cmd = "%s trim -p {output.fastq[1]}" % executable
                cmd += " -pe1 {input.fastq[0]} -pe2 {input.fastq[1]} --progress bar -T {params.thread}"
            else:
                cmd = "%s -p {output.fastq[1]}" % executable
                cmd += " {input.fastq[0]} {input.fastq[1]} "

            mode2 = params.mode.upper()

            if fwd:
                cmd += " -{params.mode} %s " % fwd
            else:
                cmd += " -{params.mode} XXXX "

            if rev:
                cmd += " -%s %s " % (mode2, rev)
            else:
                cmd += " -%s XXXX " % mode2

            cmd += " -m {params.m} -q {params.quality} "
            cmd += " {params.kwargs} -o {output.fastq[0]} > {log}"
            shell(cmd)
        else:
            if executable == "atropos":
                cmd = "%s trim -se {input.fastq[0]}" % executable
            else:
                cmd = "%s {input.fastq[0]}" % executable
            if fwd:
                cmd += "    -{params.mode} %s " % fwd
            else:
                cmd += "    -{params.mode} XXXX "
            cmd += " -m {params.m} -q {params.quality} "
            cmd += " {params.kwargs} -o {output.fastq[0]} > {log}"
            shell(cmd)
