#!/bin/bash

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

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


set -euo pipefail
genomeRefFolder=`dirname /cromwell_root/broad-institute-gdac/reference/genome/GRCh37.d1.vd1__by_chromosome/chr1.txt`
Rscript /src/SignatureAnalyzer.Broad.R \
--libdir /src \
 \
 \
--n.iter 20 \
--Kcol 96 \
--tol 1.e-07 \
--a0 10 \
--tumor.type BRCA-freeze-v5 \
--maffile /cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/87e93152-c62b-420d-8832-883ed21d1cc4/mutation_mutsig2cv_hg19/20396aa5-9704-4384-92d5-8f87909c9572/call-tool_mutsig2cv_hg19/BRCA-freeze-v5.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][a-zA-Z0-9][a-zA-Z0-9]/*" \
		exec.sh
fi
)  > "$out1a7a6192" 2> "$err1a7a6192"
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
# make the directory which will keep the matching files
mkdir /cromwell_root/glob-92c4d1a96a01c252fde1566ed8fd16d0

# create the glob control file that will allow for the globbing to succeed even if there is 0 match
echo "This file is used by Cromwell to allow for globs that would not match any file.
By its presence it works around the limitation of some backends that do not allow empty globs.
Regardless of the outcome of the glob, this file will not be part of the final list of globbed files." > /cromwell_root/glob-92c4d1a96a01c252fde1566ed8fd16d0/cromwell_glob_control_file

# hardlink or symlink all the files into the glob directory
( ln -L /cromwell_root/OUTPUT_lego96/BRCA-freeze-v5_solution_*.maf /cromwell_root/glob-92c4d1a96a01c252fde1566ed8fd16d0 2> /dev/null ) || ( ln /cromwell_root/OUTPUT_lego96/BRCA-freeze-v5_solution_*.maf /cromwell_root/glob-92c4d1a96a01c252fde1566ed8fd16d0 )

# list all the files (except the control file) that match the glob into a file called glob-[md5 of glob].list
ls -1 /cromwell_root/glob-92c4d1a96a01c252fde1566ed8fd16d0 | grep -v cromwell_glob_control_file > /cromwell_root/glob-92c4d1a96a01c252fde1566ed8fd16d0.list


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