SAMTOOLS SORT

https://img.shields.io/github/issues-pr/snakemake/snakemake-wrappers/bio/samtools/sort?label=version%20update%20pull%20requests

Sort bam file with samtools.

URL: http://www.htslib.org/doc/samtools-sort.html

Example

This wrapper can be used in the following way:

rule samtools_sort:
    input:
        "mapped/{sample}.bam",
    output:
        "mapped/{sample}.sorted.bam",
    log:
        "logs/{sample}.log",
    params:
        extra="-m 4G",
    threads: 8
    wrapper:
        "v3.10.2-32-gf4e5b66/bio/samtools/sort"

Note that input, output and log file paths can be chosen freely.

When running with

snakemake --use-conda

the software dependencies will be automatically deployed into an isolated environment before execution.

Software dependencies

  • samtools=1.20

  • snakemake-wrapper-utils=0.6.2

Input/Output

Input:

  • SAM/BAM/CRAM file

Output:

  • SAM/BAM/CRAM file

  • SAM/BAM/CRAM index file (optional)

Params

  • extra: additional program arguments (not -@/–threads, –write-index, -m, -o or -O/–output-fmt).

Authors

  • Johannes Köster

  • Filipe G. Vieira

Code

__author__ = "Johannes Köster"
__copyright__ = "Copyright 2016, Johannes Köster"
__email__ = "koester@jimmy.harvard.edu"
__license__ = "MIT"


import tempfile
from pathlib import Path
from snakemake.shell import shell
from snakemake_wrapper_utils.snakemake import get_mem
from snakemake_wrapper_utils.samtools import get_samtools_opts


samtools_opts = get_samtools_opts(snakemake)
extra = snakemake.params.get("extra", "")
log = snakemake.log_fmt_shell(stdout=True, stderr=True)

# `-m`: Approximately the maximum required memory per thread [...]
# samtools sort can use more memory than specified (e.g. https://github.com/samtools/samtools/issues/831)
mem_overhead_factor = snakemake.params.get("mem_overhead_factor", 0.1)
mem_per_thread_mb = int(
    get_mem(snakemake) / snakemake.threads * (1.0 - mem_overhead_factor)
)

with tempfile.TemporaryDirectory() as tmpdir:
    tmp_prefix = Path(tmpdir) / "samtools_sort"

    shell(
        "samtools sort {samtools_opts} -m {mem_per_thread_mb}M {extra} -T {tmp_prefix} {snakemake.input[0]} {log}"
    )