Skip to content

Commit

Permalink
Add thread limitation to samtools sort to avoid excessive memory request
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryHung committed Jan 6, 2025
1 parent 57b107c commit bca6efc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions bin/convert_sam_to_sorted_bam.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions modules/mapping.nf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bca6efc

Please sign in to comment.