From 9553f70bda5d470a423638d2b84abc175b64f77b Mon Sep 17 00:00:00 2001 From: "Thomas B. Mooney" Date: Thu, 19 Aug 2021 15:14:01 -0500 Subject: [PATCH] Replace bash script with perl script for merge wrapper. The ${...} syntax requires different escaping for cwltool than for cromwell, so it's impossible to get it right. This rewrite avoids the need to use that syntax. --- definitions/tools/merge_bams.cwl | 76 ++++++++++++++++---------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/definitions/tools/merge_bams.cwl b/definitions/tools/merge_bams.cwl index b8f66dd9..437fd419 100644 --- a/definitions/tools/merge_bams.cwl +++ b/definitions/tools/merge_bams.cwl @@ -3,7 +3,7 @@ cwlVersion: v1.0 class: CommandLineTool label: "Sambamba: merge" -baseCommand: ["/bin/bash", "merge.sh"] +baseCommand: ["/usr/bin/perl", "merge.pl"] requirements: - class: ResourceRequirement ramMin: 8000 @@ -12,49 +12,49 @@ requirements: dockerPull: "mgibio/bam-merge:0.1" - class: InitialWorkDirRequirement listing: - - entryname: 'merge.sh' + - entryname: 'merge.pl' entry: | - #!/bin/bash - set -o pipefail - set -o errexit - set -o nounset + #!/usr/bin/perl - SORTED=false + use strict; + use warnings; - while getopts "t:n:s:" opt; do - case "$opt" in - t) - NTHREADS="$OPTARG" - ;; - n) - OUTFILENAME="$OPTARG" - ;; - s) - SORTED=true - ;; - esac - done + use Getopt::Std; + use File::Copy; - BAMS=("${@:$OPTIND}") - NUM_BAMS=${#BAMS[@]} + my %opts; + getopts('t:n:s', \%opts); + my $nthreads = $opts{t} // die 'missing thread count'; + my $outfilename = $opts{n} // die 'missing output filename'; + my $sorted = $opts{s}; + + my @bams = @ARGV; + die 'missing input bams' unless scalar(@bams); #if there is only one bam, just copy it and index it - if [[ $NUM_BAMS -eq 1 ]]; then - cp "$BAMS" "$OUTFILENAME"; - else - if [[ $SORTED == "true" ]];then - /usr/bin/sambamba merge -t "$NTHREADS" "$OUTFILENAME" "${BAMS[@]}" - else #unsorted bams, use picard - args=(OUTPUT="$OUTFILENAME" ASSUME_SORTED=true USE_THREADING=true SORT_ORDER=unsorted VALIDATION_STRINGENCY=LENIENT) - for i in "${BAMS[@]}";do - args+=("INPUT=$i") - done - java -jar -Xmx6g /opt/picard/picard.jar MergeSamFiles "${args[@]}" - fi - fi - if [[ $SORTED == "true" ]];then - /usr/bin/sambamba index "$OUTFILENAME" - fi + if (scalar(@bams) == 1) { + copy($bams[0], $outfilename) or die 'failed to copy file:' . $!; + } else { + if ($sorted) { + my $rv = system((qw(/usr/bin/sambamba merge -t)), $nthreads, $outfilename, @bams); + $rv == 0 or die 'failed to merge with sambamba'; + } else { #unsorted bams, use picard + my @args = ( + 'OUTPUT=' . $outfilename, + 'ASSUME_SORTED=true', + 'USE_THREADING=true', + 'SORT_ORDER=unsorted', + 'VALIDATION_STRINGENCY=LENIENT', + map { 'INPUT=' . $_ } @bams + ); + my $rv = system((qw(java -jar -Xmx6g /opt/picard/picard.jar MergeSamFiles)), @args); + $rv == 0 or die 'failed to merge with picard'; + } + } + if ($sorted) { + my $rv = system((qw(/usr/bin/sambamba index)), $outfilename); + $rv == 0 or die 'failed to index'; + } arguments: [ "-t", "$(runtime.cores)",