Skip to content

Commit

Permalink
feat: parse mem for samtools sort (#442)
Browse files Browse the repository at this point in the history
* Fixed tmp prefix

* Parsing memory

* Removed redundant random number on prefix

* Code clean up

* Added missing import

* Fixed bug with int and str

* Tweaked mem format

* FIxed typo
  • Loading branch information
fgvieira committed Jan 26, 2022
1 parent 7febbb0 commit 37c2d08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions bio/samtools/fastq/separate/environment.yaml
Expand Up @@ -3,3 +3,4 @@ channels:
- bioconda
dependencies:
- samtools =1.14
- snakemake-wrapper-utils =0.3
36 changes: 21 additions & 15 deletions bio/samtools/fastq/separate/wrapper.py
Expand Up @@ -6,35 +6,41 @@

import os
import tempfile
from pathlib import Path
from snakemake.shell import shell
from snakemake_wrapper_utils.snakemake import get_mem

params_sort = snakemake.params.get("sort", "")
params_fastq = snakemake.params.get("fastq", "")
log = snakemake.log_fmt_shell(stdout=True, stderr=True)

prefix = os.path.splitext(snakemake.output[0])[0]

# Samtools takes additional threads through its option -@
# One thread is used bu Samtools sort
# One thread is used by Samtools fastq
# So snakemake.threads has to take them into account
# before allowing additional threads through samtools sort -@
threads = "" if snakemake.threads <= 2 else " -@ {} ".format(snakemake.threads - 2)
threads = 0 if snakemake.threads <= 2 else snakemake.threads - 2

mem = get_mem(snakemake, "MiB")
mem = "-m {0:.0f}M".format(mem / threads) if mem and threads else ""

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

with tempfile.NamedTemporaryFile() as tmpfile:
shell(
"(samtools sort -n "
" {threads} "
" -T {tmpfile.name} "
" {params_sort} "
"(samtools sort -n"
" --threads {threads}"
" {mem}"
" -T {tmp_prefix}"
" {params_sort}"
" {snakemake.input[0]} | "
"samtools fastq "
" {params_fastq} "
" -1 {snakemake.output[0]} "
" -2 {snakemake.output[1]} "
" -0 /dev/null "
" -s /dev/null "
" -F 0x900 "
"samtools fastq"
" {params_fastq}"
" -1 {snakemake.output[0]}"
" -2 {snakemake.output[1]}"
" -0 /dev/null"
" -s /dev/null"
" -F 0x900"
" - "
") {log}"
)

0 comments on commit 37c2d08

Please sign in to comment.