From 78fd89082baf553264127853a5bab2d85229e0e9 Mon Sep 17 00:00:00 2001
From: Emilio Palumbo <emiliopalumbo@gmail.com>
Date: Thu, 20 Jul 2023 14:58:23 +0200
Subject: [PATCH] Reduce memory usage in RSEM quantification process

---
 modules/quantification/rsem/main.nf | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/modules/quantification/rsem/main.nf b/modules/quantification/rsem/main.nf
index 9e43e69..24c5d43 100644
--- a/modules/quantification/rsem/main.nf
+++ b/modules/quantification/rsem/main.nf
@@ -61,6 +61,8 @@ process quantify {
     viewTx = "TranscriptQuantifications"
     viewGn = "GeneQuantifications"
     def memory = (task.memory ?: 1.GB).toMega()
+    def sortMemFrac = params.rsemSkipCi ? 0.75 : 0.5
+    def sortMemory = Math.max(1024, (memory * sortMemFrac) as long)
     def forwardProb = null
 
     switch (readStrand) {
@@ -73,7 +75,7 @@ process quantify {
     }
     
     def cmd = []
-    cmd << "sambamba sort -t ${task.cpus} -m ${memory}MB -N -M -l 0 -o - ${bam} \\"
+    cmd << "sambamba sort -t ${task.cpus} -m ${sortMemory}MB -N -M -l 0 -o - ${bam} \\"
     cmd << """\
         | rsem-calculate-expression -p ${task.cpus} \\
                                     --bam \\
@@ -89,9 +91,10 @@ process quantify {
                             --forward-prob ${forwardProb} \\"""
     }
     if ( ! params.rsemSkipCi ) {
+        def ciMemory = Math.max(1024, (memory / 4) as long)
         cmd << """\
                             --calc-ci \\
-                            --ci-memory ${memory} \\"""
+                            --ci-memory ${ciMemory} \\"""
     }
     cmd << """\
                             - \\