Skip to content

Commit 43c790e

Browse files
authored
Merge pull request #1046 from tmooney/merge_bams_cwltool_compatible
Update merge_bams.cwl to work with both cwltool and cromwell.
2 parents 1750cd5 + 9553f70 commit 43c790e

1 file changed

Lines changed: 39 additions & 39 deletions

File tree

definitions/tools/merge_bams.cwl

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
cwlVersion: v1.0
44
class: CommandLineTool
55
label: "Sambamba: merge"
6-
baseCommand: ["/bin/bash", "merge.sh"]
6+
baseCommand: ["/usr/bin/perl", "merge.pl"]
77
requirements:
88
- class: ResourceRequirement
99
ramMin: 8000
@@ -12,49 +12,49 @@ requirements:
1212
dockerPull: "mgibio/bam-merge:0.1"
1313
- class: InitialWorkDirRequirement
1414
listing:
15-
- entryname: 'merge.sh'
15+
- entryname: 'merge.pl'
1616
entry: |
17-
#!/bin/bash
18-
set -o pipefail
19-
set -o errexit
20-
set -o nounset
17+
#!/usr/bin/perl
2118

22-
SORTED=false
19+
use strict;
20+
use warnings;
2321

24-
while getopts "t:n:s:" opt; do
25-
case "$opt" in
26-
t)
27-
NTHREADS="$OPTARG"
28-
;;
29-
n)
30-
OUTFILENAME="$OPTARG"
31-
;;
32-
s)
33-
SORTED=true
34-
;;
35-
esac
36-
done
22+
use Getopt::Std;
23+
use File::Copy;
3724

38-
BAMS=("${@:$OPTIND}")
39-
NUM_BAMS=${#BAMS[@]}
25+
my %opts;
26+
getopts('t:n:s', \%opts);
27+
my $nthreads = $opts{t} // die 'missing thread count';
28+
my $outfilename = $opts{n} // die 'missing output filename';
29+
my $sorted = $opts{s};
30+
31+
my @bams = @ARGV;
32+
die 'missing input bams' unless scalar(@bams);
4033

4134
#if there is only one bam, just copy it and index it
42-
if [[ $NUM_BAMS -eq 1 ]]; then
43-
cp "$BAMS" "$OUTFILENAME";
44-
else
45-
if [[ $SORTED == "true" ]];then
46-
/usr/bin/sambamba merge -t "$NTHREADS" "$OUTFILENAME" "${BAMS[@]}"
47-
else #unsorted bams, use picard
48-
args=(OUTPUT="$OUTFILENAME" ASSUME_SORTED=true USE_THREADING=true SORT_ORDER=unsorted VALIDATION_STRINGENCY=LENIENT)
49-
for i in "${BAMS[@]}";do
50-
args+=("INPUT=$i")
51-
done
52-
java -jar -Xmx6g /opt/picard/picard.jar MergeSamFiles "${args[@]}"
53-
fi
54-
fi
55-
if [[ $SORTED == "true" ]];then
56-
/usr/bin/sambamba index "$OUTFILENAME"
57-
fi
35+
if (scalar(@bams) == 1) {
36+
copy($bams[0], $outfilename) or die 'failed to copy file:' . $!;
37+
} else {
38+
if ($sorted) {
39+
my $rv = system((qw(/usr/bin/sambamba merge -t)), $nthreads, $outfilename, @bams);
40+
$rv == 0 or die 'failed to merge with sambamba';
41+
} else { #unsorted bams, use picard
42+
my @args = (
43+
'OUTPUT=' . $outfilename,
44+
'ASSUME_SORTED=true',
45+
'USE_THREADING=true',
46+
'SORT_ORDER=unsorted',
47+
'VALIDATION_STRINGENCY=LENIENT',
48+
map { 'INPUT=' . $_ } @bams
49+
);
50+
my $rv = system((qw(java -jar -Xmx6g /opt/picard/picard.jar MergeSamFiles)), @args);
51+
$rv == 0 or die 'failed to merge with picard';
52+
}
53+
}
54+
if ($sorted) {
55+
my $rv = system((qw(/usr/bin/sambamba index)), $outfilename);
56+
$rv == 0 or die 'failed to index';
57+
}
5858

5959
arguments: [
6060
"-t", "$(runtime.cores)",
@@ -66,7 +66,7 @@ inputs:
6666
position: 3
6767
sorted:
6868
type: boolean?
69-
default: "false"
69+
default: false
7070
inputBinding:
7171
prefix: "-s"
7272
position: 2

0 commit comments

Comments
 (0)