#!/bin/bash

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

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


set -euo pipefail
unzip /cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/report_only_files.zip -d `dirname /cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/gisticInputs.txt`

RunR.sh -f main /src/Gistic2Report.R main \
	-g/src/geneList.txt \
	 \
	-a/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/amp_genes.txt \
	-d/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/del_genes.txt \
	-A/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/amp_qplot.png \
	-D/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/del_qplot.png \
	-S/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/raw_copy_number.png \
	-V/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/gisticVersion.txt \
	-T/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/arraylistfile.txt \
	-b/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/broad_significance_results.txt \
	-I/cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/gisticInputs.txt

zip -D report_gistic2 * -x exec.sh

if true; then
	mv /cromwell_root/fc-secure-823808d0-5404-49c9-990f-b3d9e353e468/f3dd464d-79fd-48b3-81a3-ed641311fe5a/copy_number_gistic2/b66ac4b3-51b2-4724-8311-445ea38bdee6/call-tool_gistic2/copy_number_gistic2.zip .
	zip -r copy_number_gistic2.zip . -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 \
		report_gistic2.zip
fi
)  > "$outb66ac4b3" 2> "$errb66ac4b3"
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
