Skip to content

Commit

Permalink
Merge pull request #1046 from tmooney/merge_bams_cwltool_compatible
Browse files Browse the repository at this point in the history
Update merge_bams.cwl to work with both cwltool and cromwell.
  • Loading branch information
tmooney authored Aug 26, 2021
2 parents 1750cd5 + 9553f70 commit 43c790e
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions definitions/tools/merge_bams.cwl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)",
Expand All @@ -66,7 +66,7 @@ inputs:
position: 3
sorted:
type: boolean?
default: "false"
default: false
inputBinding:
prefix: "-s"
position: 2
Expand Down

0 comments on commit 43c790e

Please sign in to comment.