From bca6efcaa923fbd93c05420e95b31c01f3c6e085 Mon Sep 17 00:00:00 2001 From: Harry Hung <4848896+HarryHung@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:27:28 +0000 Subject: [PATCH] Add thread limitation to samtools sort to avoid excessive memory request --- bin/convert_sam_to_sorted_bam.sh | 8 ++++++-- modules/mapping.nf | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/convert_sam_to_sorted_bam.sh b/bin/convert_sam_to_sorted_bam.sh index c730a5f..f3278a2 100755 --- a/bin/convert_sam_to_sorted_bam.sh +++ b/bin/convert_sam_to_sorted_bam.sh @@ -1,9 +1,13 @@ # Convet SAM to sorted BAM file # Remove source SAM file if $LITE is true -samtools view -@ "$(nproc)" -b "$SAM" > "$BAM" +# Thread usage is capped as memory usage is per thread, and the speed gain level-off as thread count increases +AVAILABLE_THREAD=$(nproc) +THREAD=$(( AVAILABLE_THREAD > MAX_THREAD ? MAX_THREAD : AVAILABLE_THREAD )) -samtools sort -@ "$(nproc)" -o "$SORTED_BAM" "$BAM" +samtools view -@ "$THREAD" -b "$SAM" > "$BAM" + +samtools sort -@ "$THREAD" -o "$SORTED_BAM" "$BAM" rm "$BAM" if [ "$LITE" = true ]; then diff --git a/modules/mapping.nf b/modules/mapping.nf index 08c298b..42f0ace 100644 --- a/modules/mapping.nf +++ b/modules/mapping.nf @@ -67,11 +67,13 @@ process SAM_TO_SORTED_BAM { script: sorted_bam="${sample_id}_mapped_sorted.bam" + max_thread="8" """ SAM="$sam" BAM="mapped.bam" SORTED_BAM="$sorted_bam" LITE="$lite" + MAX_THREAD="$max_thread" source convert_sam_to_sorted_bam.sh source get_ref_coverage.sh