rule sambamba_markdup:
    """ 
    This rule marks or removes PCR duplicate reads. For determining whether a
    read is a duplicate or not, the same criteria as in Picard are used.

    Required input:
        - __sambamba_markdup__input: Sorted BAM file.

    Required output:
        - __sambamba_markdup__output: BAM file with duplicates marked or removed.

    Required log:
        - __sambamba_markdup__log_std: Log file with stdout.
        - __sambamba_markdup__log_err: Log file with stderr.

    Required configuration:
        .. code-block:: yaml

            sambamba_markdup:
                remove: no # Remove or just mark duplicate reads.
                tmp_directory: /tmp # Temporary directory.

    Reference:
        - http://lomereiter.github.io/sambamba/docs/sambamba-markdup.html
    """
    input:
        bam = __sambamba_markdup__input
    output:
        bam = __sambamba_markdup__output
    log:
        out = __sambamba_markdup__log_std,
        err = __sambamba_markdup__log_err
    params:
        remove = config["sambamba_markdup"]["remove"],
        tmp = config["sambamba_markdup"]["tmp_directory"]
    run:
        cmd = "sambamba markdup %s %s --tmpdir=%s" % (
            input['bam'], output['bam'], params['tmp']
        )

        if params['remove']:
            cmd += " --remove-duplicates"
        
        shell('%s > %s 2> %s' % (cmd, log['out'], log['err']))
