-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·194 lines (155 loc) · 5.18 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /bin/bash
#
#
CONTAINER="[scitran/fslmerge]"
echo -e "$CONTAINER Initiated"
###############################################################################
# Built to flywheel-v0 spec.
FLYWHEEL_BASE=/flywheel/v0
OUTPUT_DIR=$FLYWHEEL_BASE/output
INPUT_DIR=$FLYWHEEL_BASE/input
MANIFEST=$FLYWHEEL_BASE/manifest.json
CONFIG_FILE=$FLYWHEEL_BASE/config.json
###############################################################################
# Configure the ENV
chmod +x /etc/fsl/5.0/fsl.sh
source /etc/fsl/5.0/fsl.sh
###############################################################################
# Initialize config parameters
catTime=' '
catX=' '
catY=' '
catX=' '
auto=' '
catTR=' '
###############################################################################
# Generate flags from the manifest
catTime_flag=$(jq -r <$MANIFEST '''.config.catTime.id')
catX_flag=$(jq -r <$MANIFEST '''.config.catX.id')
catY_flag=$(jq -r <$MANIFEST '''.config.catY.id')
catZ_flag=$(jq -r <$MANIFEST '''.config.catZ.id')
auto_flag=$(jq -r <$MANIFEST '''.config.auto.id')
catTR_flag=$(jq -r <$MANIFEST '''.config.catTR.id')
###############################################################################
# Parse config options from CONFIG file or MANIFEST
function parse_config {
CONFIG_FILE=$FLYWHEEL_BASE/config.json
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
if [[ -f $CONFIG_FILE ]]; then
echo -e "$(cat $CONFIG_FILE | jq -r '.config.'$1)"
else
CONFIG_FILE=$MANIFEST_FILE
echo -e "$(cat $MANIFEST_FILE | jq -r '.config.'$1'.default')"
fi
}
# String parsing
config_catTime="$(parse_config 'catTime')"
config_catX="$(parse_config 'catX')"
config_catY="$(parse_config 'catY')"
config_catZ="$(parse_config 'catZ')"
config_auto="$(parse_config 'auto')"
config_catTR="$(parse_config 'catTR')"
config_base_outname="$(parse_config 'base_outname')"
# Boolean parsing
if [[ $config_catTime == 'true' ]]; then
catTime=$catTime_flag
fi
if [[ $config_catX == 'true' ]]; then
catX=$catX_flag
fi
if [[ $config_catY == 'true' ]]; then
catY=$catY_flag
fi
if [[ $config_catZ == 'true' ]]; then
catZ=$catZ_flag
fi
if [[ $config_auto == 'true' ]]; then
auto=$auto_flag
fi
if [[ $config_base_outname != 'none' ]]; then
output_file_base="$config_base_outname"
fi
if [[ $config_catTR == 0 ]]; then
TR=' '
else
catTR=$catTR_flag
TR=$config_catTR
fi
###############################################################################
# Compile options string
OPTIONS="$catTime $catX $catY $catZ $auto $catTR"
###############################################################################
# Generate paths to input data
NIFTI_1=$(find $INPUT_DIR/NIFTI_1/* -type f -name "*.nii*" | head -1)
NIFTI_2=$(find $INPUT_DIR/NIFTI_2/* -type f -name "*.nii*" | head -1)
if [[ -d $INPUT_DIR/BVAL_1 ]] && [[ -d $INPUT_DIR/BVAL_2 ]]; then
BVAL_1=$(find $INPUT_DIR/BVAL_1/* -type f -name "*.bval*" | head -1)
BVAL_2=$(find $INPUT_DIR/BVAL_2/* -type f -name "*.bval*" | head -1)
else
BVAL_1=''
BVAL_2=''
fi
if [[ -d $INPUT_DIR/BVEC_1 ]] && [[ -d $INPUT_DIR/BVEC_2 ]]; then
BVEC_1=$(find $INPUT_DIR/BVEC_1/* -type f -name "*.bvec*" | head -1)
BVEC_2=$(find $INPUT_DIR/BVEC_2/* -type f -name "*.bvec*" | head -1)
else
BVEC_1=''
BVEC_2=''
fi
###############################################################################
# Merge functions for diffusion data
merge_bvec () {
for L in $(seq 3); do
linea=$(sed -n "$L"p "$1" | tr -d "\n")
lineb=$(sed -n "$L"p "$2")
echo -e "$linea\t$lineb" >> "$3"
done
}
merge_bval () {
bvala=$(cat "$1" | tr -d "\n")
bvalb=$(cat "$2")
echo -e "$bvala\t$bvalb" >> "$3"
}
###############################################################################
# RUN the MERGE Algorithm
if [[ -n "$NIFTI_1" ]] && [[ -n "$NIFTI_2" ]]; then
echo "$CONTAINER Running fslmerge on $NIFTI_1 & $NIFTI_2..."
# Set the output file basename if not already defined by config
if [[ -z $output_file_base ]]; then
output_file_base=$OUTPUT_DIR/`basename "$NIFTI_1" .nii.gz`_`basename "$NIFTI_2" .nii.gz`
output_file_base=$(echo -e "${output_file_base}" | tr -d '[:space:]')
fi
# Merge the images
fslmerge ${OPTIONS} "$output_file_base".nii.gz "$NIFTI_1" "$NIFTI_2" $TR
exit_status=$?
if [[ $exit_status != 0 ]]; then
echo "$CONTAINER fslmerge exited with failures ($exit_status)"
exit 1
fi
else
echo "$CONTAINER $INPUT_DIR has no valid input files!"
exit 1
fi
if [[ -n "$BVAL_1" ]] && [[ -n "$BVAL_2" ]]; then
echo "$CONTAINER Running merge_bval on $BVAL_1 & $BVAL_2..."
merge_bval "$BVAL_1" "$BVAL_2" "$output_file_base".bval
merge_bval_exit=$?
else
merge_bval_exit=0
fi
if [[ -n "$BVEC_1" ]] && [[ -n "$BVEC_2" ]]; then
echo "$CONTAINER Running merge_bvec on $BVEC_1 & $BVEC_2..."
merge_bvec "$BVEC_1" "$BVEC_2" "$output_file_base".bvec
merge_bvec_exit=$?
else
merge_bvec_exit=0
fi
###############################################################################
# Check status and exit accordingly
if [[ $exit_status == 0 ]] && [[ $merge_bval_exit == 0 ]] && [[ $merge_bvec_exit == 0 ]]; then
echo -e "$CONTAINER Done!"
exit 0
else
echo -e "$CONTAINER Exiting with failures! (fslmerge=$exit_status, merge_bval=$merge_bval_exit, merge_bvec=$merge_bvec_exit)"
exit 1
fi