SAMTOOLS SORT

Sort bam file with samtools.

URL:

Example

This wrapper can be used in the following way:

rule samtools_sort:
    input:
        "mapped/{sample}.bam",
    output:
        "mapped/{sample}.sorted.bam",
    log:
        "{sample}.log",
    params:
        extra="-m 4G",
    threads: 8
    wrapper:
        "v1.2.1/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.14
  • snakemake-wrapper-utils=0.3

Notes

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 os
import tempfile
from pathlib import Path
from snakemake.shell import shell
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)

out_name, out_ext = os.path.splitext(snakemake.output[0])

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

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