#!/bin/bash

cd /cromwell_root
tmpDir=$(mkdir -p "/cromwell_root/tmp.40fb3f60" && echo "/cromwell_root/tmp.40fb3f60")
chmod 777 "$tmpDir"
export _JAVA_OPTIONS=-Djava.io.tmpdir="$tmpDir"
export TMPDIR="$tmpDir"
export HOME="$HOME"
(
cd /cromwell_root

)
outb0797441="${tmpDir}/out.$$" errb0797441="${tmpDir}/err.$$"
mkfifo "$outb0797441" "$errb0797441"
trap 'rm "$outb0797441" "$errb0797441"' EXIT
tee '/cromwell_root/stdout' < "$outb0797441" &
tee '/cromwell_root/stderr' < "$errb0797441" >&2 &
(
cd /cromwell_root


set -euo pipefail
genomeRefFolder=`dirname /cromwell_root/broad-institute-gdac/reference/genome/GRCh38.d1.vd1__by_chromosome/chr1.txt`
Rscript /src/SignatureAnalyzer.Broad.R \
 \
 \
--n.iter 10 \
--Kcol 96 \
--tol 1.e-07 \
--a0 10 \
--tumor.type CPTAC3-LUAD-TP \
--maffile /cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/9e0520c9-8e20-4cc6-84e9-7a0dbcc09d77/mutation_mutsig2cv/feac43f0-aca5-408c-8251-fa2fda0291a8/call-tool_mutsig2cv/CPTAC3-LUAD-TP.final_analysis_set.maf \
--genomeRefFolder $genomeRefFolder \
--prior L1KL


if true
	then zip -r mutation_signature_analyzer . -x \
		"fc-[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]/*" \
		lost+found/\* \
		broad-institute-gdac/\* \
		"tmp.[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]/*" \
		exec.sh
fi
)  > "$outb0797441" 2> "$errb0797441"
echo $? > /cromwell_root/rc.tmp
(
# add a .file in every empty directory to facilitate directory delocalization on the cloud
cd /cromwell_root
find . -type d -exec sh -c '[ -z "$(ls -A '"'"'{}'"'"')" ] && touch '"'"'{}'"'"'/.file' \;
)
(
cd /cromwell_root
sync


)
mv /cromwell_root/rc.tmp /cromwell_root/rc
