rule bamCoverage:
    """
    Bam coverage file in bigwig format. Is part of deeptools http://deeptools.readthedocs.io/en/latest/index.html

    Required input:
        __bamCoverage__input: list of bam files

    Required output:
        __bamCoverage__output: normalized bigwig file
    """
    input:
        __bamCoverage__input
    params:
        bs = config['coverage']['binSize'],
        gs = config['coverage']['genomeSize'],
        minFL = config['coverage']['minFragmentLength'],
        maxFL = config['coverage']['maxFragmentLength'],
        exRead = config['coverage']['extendReads']

    log:
        __bamCoverage__log
    output:
        __bamCoverage__output
    threads:
        config['coverage']['threads']
    shell:
        """
        if [ ! -e {input}.bai ] ; then
            samtools index {input}
        fi
        bamCoverage --bam {input} -o {output} --numberOfProcessors 4 \
        --binSize {params.bs} --normalizeTo1x {params.gs} \
        --extendReads {params.exRead} --minFragmentLength {params.minFL}  \
        --maxFragmentLength {params.maxFL} 2> {log}
        """