From 60c48b64ab592288e9727de11a68d9a7b3cafc16 Mon Sep 17 00:00:00 2001
From: Ruben Vorderman <r.h.p.vorderman@lumc.nl>
Date: Mon, 28 Oct 2019 14:28:21 +0100
Subject: [PATCH] fix samtools index

---
 samtools.wdl | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/samtools.wdl b/samtools.wdl
index 7c1143c..871110d 100644
--- a/samtools.wdl
+++ b/samtools.wdl
@@ -31,28 +31,29 @@ task BgzipAndIndex {
 task Index {
     input {
         File bamFile
-        String outputBamPath = basename(bamFile)
+        String? outputBamPath
         String dockerImage = "quay.io/biocontainers/samtools:1.8--h46bd0b3_5"
     }
 
     # Select_first is needed, otherwise womtool validate fails.
-    String bamIndexPath = sub(select_first([outputBamPath]), "\.bam$", ".bai")
+    String outputPath = select_first([outputBamPath, basename(bamFile)])
+    String bamIndexPath = sub(outputPath, "\.bam$", ".bai")
 
     command {
         bash -c '
         set -e
         # Make sure outputBamPath does not exist.
-        if [ ! -f ~{outputBamPath} ]
+        if [ ! -f ~{outputPath} ]
         then
-            mkdir -p $(dirname ~{outputBamPath})
-            ln ~{bamFile} ~{outputBamPath}
+            mkdir -p $(dirname ~{outputPath})
+            ln ~{bamFile} ~{outputPath}
         fi
-        samtools index ~{outputBamPath} ~{bamIndexPath}
+        samtools index ~{outputPath} ~{bamIndexPath}
         '
     }
 
     output {
-        File indexedBam = outputBamPath
+        File indexedBam = outputPath
         File index =  bamIndexPath
     }
 
-- 
GitLab