#!/bin/bash

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

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


set -euo pipefail
unzip /cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/report_only_files.zip -d `dirname /cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/gisticInputs.txt`

RunR.sh -f main /src/Gistic2Report.R main \
	-g/src/geneList.txt \
	 \
	-a/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/amp_genes.txt \
	-d/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/del_genes.txt \
	-A/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/amp_qplot.png \
	-D/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/del_qplot.png \
	-S/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/raw_copy_number.png \
	-V/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/gisticVersion.txt \
	-T/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/arraylistfile.txt \
	-b/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/broad_significance_results.txt \
	-I/cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/call-tool_gistic2/gisticInputs.txt

zip -D report_gistic2 * -x exec.sh

if true; then
	mv /cromwell_root/fc-e7058367-eaa6-44b5-aab5-1ec08acf146a/b6064b6c-6bfd-4c61-986b-9864aac7a226/copy_number_gistic2/dabe62b3-9ee9-4644-b01c-ae86d498dd18/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]/*" \
		exec.sh \
		report_gistic2.zip
fi
)  > "$outdabe62b3" 2> "$errdabe62b3"
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
