rule sambamba_filter:
    """ 
    This rule uses sambamba view to filter reads with a mapping quality lower
    than a threshold. It also removes reads with multiple occurrence.

    Required input:
        - __sambamba_filter__input: bam file

    Required output:
        - __sambamba_filter__output: bam file

    Required log:
        - __sambamba_filter__log: Log file with stdout and stderr of sambamba.

    Required configuration:
        .. code-block:: yaml

            sambamba_filter:
                threshold: 30 # Mapping quality score threshold

    Reference:
        - http://lomereiter.github.io/sambamba/docs/sambamba-view.html
    """
    input:
        __sambamba_filter__input
    output:
        __sambamba_filter__output
    log:
        __sambamba_filter__log
    params:
        threshold = config['sambamba_filter']['threshold']
    shell:
        """
        sambamba view \
            --format=bam \
            --filter="mapping_quality >= {params.threshold}" \
            -o {output} {input} > {log} 2>&1
        """
