-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·115 lines (99 loc) · 3.84 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
FLYWHEEL_BASE=/flywheel/v0
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
CONFIG_FILE=$FLYWHEEL_BASE/config.json
ANALYSIS_ID=$(jq -r '.destination.id' $CONFIG_FILE)
GEAR_INPUT_DIR=$FLYWHEEL_BASE/input
GEAR_OUTPUT_DIR=$FLYWHEEL_BASE/output
ANTS_OUTPUT_DIR=$GEAR_OUTPUT_DIR/"$ANALYSIS_ID"
#WORKING_DIR=$GEAR_OUTPUT_DIR/"$ANALYSIS_ID"_work
BIDS_DIR=$GEAR_INPUT_DIR/bids_dataset
CONTAINER='[flywheel/antsct]'
EXE_SCRIPT=$GEAR_OUTPUT_DIR/antsct_run.sh
# CRITICAL: re-create the environment
cat ${FLYWHEEL_BASE}/docker-env.sh
# shellcheck source=/flywheel/v0/docker-env.sh
source ${FLYWHEEL_BASE}/docker-env.sh
function error_exit()
{
echo "$@" 1>&2
exit 1
}
function parse_config {
# Parse the config file
if [[ -f $CONFIG_FILE ]]; then
echo "$(cat $CONFIG_FILE | jq -r '.config.'"$1")"
else
CONFIG_FILE=$MANIFEST_FILE
echo "$(cat $MANIFEST_FILE | jq -r '.config.'"$1"'.default')"
fi
}
# determine the input format
inputFormat='bids' #default to BIDS
isManualInput=$(jq '.inputs.t1_anatomy' "$CONFIG_FILE")
if [[ ! $isManualInput = null ]]; then
inputFormat='manual'
else
inputFormat='bids'
fi
# Download BIDS and write command
# Do the download no matter the input format because BIDS dataset needed to determine the prefix
if [[ ! -d ${BIDS_DIR} ]]; then
timeout 30m /usr/local/miniconda/bin/python /flywheel/v0/prepare_run.py
fi
if [[ ! -f $EXE_SCRIPT ]]; then
echo "$CONTAINER Unable to create ANTsCT run environment! Exiting"
error_exit 1
fi
bash -x ${FLYWHEEL_BASE}/output/antsct_run.sh
ANTS_EXITSTATUS=$?
if [[ $ANTS_EXITSTATUS == 0 ]]; then
# parse the run script to get the file-root (aka prefix)
fileRoot=$(cat ${FLYWHEEL_BASE}/output/antsct_run.sh | grep -o -P '(?<=file-root ).*(?=_ --denoise)')
subjectName=$(echo "$fileRoot" | cut -d '_' -f 1)
sessionName=$(echo "$fileRoot" | cut -d '_' -f 2)
LOOSE_DIR=$GEAR_OUTPUT_DIR/loose
mkdir $LOOSE_DIR
if [[ $inputFormat = 'bids' ]]; then
# make a BIDS derivatives folder and zip
BIDS_OUTPUT_DIR="$GEAR_OUTPUT_DIR/ants-ct"
mkdir -p "$BIDS_OUTPUT_DIR"
# arrange derivatives
bash ${FLYWHEEL_BASE}/BIDsDerivative.sh "$GEAR_OUTPUT_DIR" "$BIDS_OUTPUT_DIR" "$fileRoot" #$subjectName" "$sessionName"
# zip folder (cd there is easier)
pushd "$GEAR_OUTPUT_DIR"
# zip -r "${GEAR_OUTPUT_DIR}/antsct_${ANALYSIS_ID}_bids.zip" ants-ct
zip -r "${GEAR_OUTPUT_DIR}/antsct_${subjectName}_${sessionName}_bids.zip" ants-ct
popd
mv $(find "$BIDS_OUTPUT_DIR" -type f | grep csv) "$LOOSE_DIR"
mv $(find "$BIDS_OUTPUT_DIR" -type f | grep png) "$LOOSE_DIR"
mv $(find "$BIDS_OUTPUT_DIR" -type f | grep desc-thickness) "$LOOSE_DIR"
rm -rf "$BIDS_OUTPUT_DIR"
elif [[ $inputFormat = 'manual' ]]; then
# move everything in outputs folder to a single directory and zip
LEGACY_OUTPUT_DIR="$GEAR_OUTPUT_DIR/antsct_${ANALYSIS_ID}"
mkdir -p "$LEGACY_OUTPUT_DIR"
find "$GEAR_OUTPUT_DIR" -type f -maxdepth 1 | grep -v antsct | while read line
do
mv $line $LEGACY_OUTPUT_DIR/
done
# zip folder (cd there is easier)
pushd "$LEGACY_OUTPUT_DIR"
#zip -r "${GEAR_OUTPUT_DIR}/antsct_${ANALYSIS_ID}.zip" ./*
zip -r "${GEAR_OUTPUT_DIR}/antsct_${subjectName}_${subjectName}.zip" ./*
popd
mv $(find "$LEGACY_OUTPUT_DIR" -type f | grep csv) "$LOOSE_DIR"
mv $(find "$LEGACY_OUTPUT_DIR" -type f | grep png) "$LOOSE_DIR"
mv $(find "$LEGACY_OUTPUT_DIR" -type f | grep CorticalThickness) "$LOOSE_DIR"
rm -rf "$LEGACY_OUTPUT_DIR"
else
error_exit "Unable to determine input format..."
fi
rm $(find "$GEAR_OUTPUT_DIR" -maxdepth 1 -type f | grep -v antsct | grep -v loose) || echo "No loose files in output folder."
mv $LOOSE_DIR/* $GEAR_OUTPUT_DIR
rmdir $LOOSE_DIR
fi
# Clean up
rm -rf "$ANTS_OUTPUT_DIR"
echo -e "Wrote: $(ls -lh $GEAR_OUTPUT_DIR)"
exit $ANTS_EXITSTATUS