# coding: utf-8
"""Read mapping for either single or paired-end data using BWA and samtools.

input:
    fastq -> list with your fastq.gz (R1, R2)
    ref -> reference's fastq file
"""


def bwa_mem_dynamic(name, project):
    metadata = {'name':name}

    from easydev import cmd_exists
    ret = cmd_exists("pbwa") # for IP cluster
    if ret:
        metadata['executable'] = "pbwa"
    else:
        metadata['executable'] = "bwa"

    bwa_mem_code = '''
rule bwa_index_%(name)s:
    input:
        reference = __bwa_mem_%(name)s__reference
    output:
        fai = __bwa_mem_%(name)s__fai
    params:
        index = config["bwa_mem_%(name)s"]["index_algorithm"]
    log:
        __bwa_index_%(name)s__log
    shell:
        """
        bwa index -a {params.index} {input.reference} 2> {log}
        samtools faidx {input.reference}
        """

rule bwa_mem_%(name)s:
    """Read mapping with BWA-mem

    BWA mem rule aligns single or paired end FASTQ files on a reference genome.
    This rule is a **dynamic** rule (see `developers` section). This means
    that it can  be included multiple times with different names in the same
    pipeline.
    The reference file is indexed by BWA index and FASTQ files are mapped on it.
    The sorting is done with sambamba by reading the stdin. Moreover, Sambamba
    generates automatically the BAM index.

    Required input:
        - __bwa_mem_%(name)s__fastq: FASTQ files single or paired end.
        - __bwa_mem_%(name)s__reference: FASTA file of the reference.
        - __bwa_mem_%(name)s__fai: Index file of the reference.

    Required output:
        - __bwa_mem_%(name)s__bam: Sorted BAM file.

    Log:
        - __bwa_index_%(name)s__log: Log file of BWA index.
        - __bwa_mem_%(name)s__log: Log file of BWA mem.

    Required configuration:
        .. code-block:: yaml

            bwa_mem_%(name)s:
                index_algorithm: 'is' # BWA index algorithm (is or bwtsw)
                threads: 4         # Number of threads used by BWA mem.
                options:           # Any options recognised by BWA mem tool.
                tmp_directory: '/tmp' # Temporary directory.

    Note:
        When a dynamic rule is included, template variables must be replaced.

    References:
        - http://bio-bwa.sourceforge.net/
        - http://lomereiter.github.io/sambamba/docs/sambamba-sort.html
    """
    input:
        fai = __bwa_mem_%(name)s__fai,
        fastq = __bwa_mem_%(name)s__fastq,
        reference = __bwa_mem_%(name)s__reference
    output:
        bam = __bwa_mem_%(name)s__bam
    log:
        __bwa_mem_%(name)s__log
    params:
        options = config['bwa_mem_%(name)s']['options'],
        bwa_exe = '%(executable)s',
        tmp = config['bwa_mem_%(name)s']['tmp_directory']
    threads:
        int(config['bwa_mem_%(name)s']['threads'])
    shell:
        """
        ({params.bwa_exe} mem -t {threads} {params.options} \
        {input.reference} {input.fastq} | \
        samtools view -Sbu - | \
        sambamba sort /dev/stdin -o {output.bam} -t {threads} \
        --tmpdir={params.tmp}) 2> {log}
        """
    '''
    from sequana.snaketools import build_dynamic_rule
    return build_dynamic_rule(bwa_mem_code % metadata, project.pipeline_dir)

