forked from flywheel-apps/fsl-feat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·266 lines (205 loc) · 7.9 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#! /bin/bash
#
#
CONTAINER="[flywheel/fsl-feat]"
echo -e "$CONTAINER Initiated"
###############################################################################
# Built to flywheel-v0 spec.
FLYWHEEL_BASE=/flywheel/v0
OUTPUT_DIR=$FLYWHEEL_BASE/output
INPUT_DIR=$FLYWHEEL_BASE/input/NIFTI
SLICE_FILE_DIR=$FLYWHEEL_BASE/input/SCLICE_FILE
MANIFEST=$FLYWHEEL_BASE/manifest.json
CONFIG_FILE=$FLYWHEEL_BASE/config.json
TEMPLATE=$FLYWHEEL_BASE/template.fsf
###############################################################################
# Configure the ENV
chmod +x /etc/fsl/5.0/fsl.sh
source /etc/fsl/5.0/fsl.sh
export USER=Flywheel
##############################################################################
# Parse configuration
function parse_config {
CONFIG_FILE=$FLYWHEEL_BASE/config.json
MANIFEST_FILE=$FLYWHEEL_BASE/manifest.json
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
}
FW_CONFIG_BB_THRESH="$(parse_config 'BB_THRESH')"
FW_CONFIG_Z_THRESH="$(parse_config 'Z_THRESH')"
FW_CONFIG_NOISE_LVL="$(parse_config 'NOISE_LVL')"
FW_CONFIG_T_SMOOTH="$(parse_config 'T_SMOOTH')"
FW_CONFIG_MC="$(parse_config 'MC')"
FW_CONFIG_STC="$(parse_config 'STC')"
FW_CONFIG_BET="$(parse_config 'BET')"
FW_CONFIG_FWHM="$(parse_config 'FW_CONFIG_FWHM')"
FW_CONFIG_INT_NORM="$(parse_config 'INT_NORM')"
FW_CONFIG_HPF_CUTOF="$(parse_config 'HPF_CUTOF')"
FW_CONFIG_HPF="$(parse_config 'HPF')"
FW_CONFIG_REG_STANDARD_IMAGE="$(parse_config 'REG_STANDARD_IMAGE')"
FW_CONFIG_LPF="$(parse_config 'LPF')"
###############################################################################
# INPUT File
input_file=`find $INPUT_DIR/* -maxdepth 0 -not -path '*/\.*' -type f -name "*.nii*" | head -1`
if [[ -z $input_file ]]; then
echo "$CONTAINER INPUT_DIR has no valid input files!"
exit 1
fi
####################################################################
# FillTemplate
# Credit goes to David Parker @UPENN
# VOLUME INFO
####################################################################
# THE INPUT FMRI VOLUME TO USE (CAN BE .NII OR .NII.GZ)
INPUT_DATA="${input_file}"
# USE 'FSLHD' TO FIND AND SET THE TR
#TR=` fslhd ${INPUT_DATA} | grep pixdim3 | tr -s ' ' | cut -d' ' -f2 `
TR=` fslhd ${INPUT_DATA} | grep pixdim4 | awk '{print $2}' `
# FOR SOME REASON IT LIKES TO KNOW THE NUMBER OF VOXELS. WE'LL
# use 'fslhd' again to do this
DXYZ=(` fslhd ${INPUT_DATA} | grep ^dim[1-4] `)
NX=${DXYZ[1]}
NY=${DXYZ[3]}
NZ=${DXYZ[5]}
NUM_VOL=${DXYZ[7]}
# As long as NX NY NZ and NUM_VOL are int, this works fine.
# Decimals will mess it up
NUM_VOX=$(( NX*NY*NZ*NUM_VOL ))
# SET THE NUMBER OF VOLUMES TO DELETE
DEL_VOL=0
# SET THE OUTPUT DIRECTORY
FEAT_OUTPUT_DIR="${OUTPUT_DIR}"/fsl_preproc
if [ ! -e ${FEAT_OUTPUT_DIR} ]; then
mkdir ${FEAT_OUTPUT_DIR}
fi
####################################################################
# STATISTICS
# Strictly speaking, these aren't important for the preprocessing
# and COULD be ignored
####################################################################
# SET THE BRAIN BACKGROUND THRESHOLD
# It is used in intensity normalization, brain mask
# generation and various other places in the analysis.
BB_THRESH=$FW_CONFIG_BB_THRESH
# SET THE Z THRESHOLD FOR DESIGN EFFICIENCY CALCULATION
# used to determine what level of activation would
# be statistically significant, to be used only in the design
# efficiency calculation. Increasing this will result in higher
# estimates of required effect.
Z_THRESH=$FW_CONFIG_Z_THRESH
# SET THE FMRI NOISE LEVEL
# the standard deviation (over time) for a
# typical voxel, expressed as a percentage of the baseline signal level.
NOISE_LVL=$FW_CONFIG_NOISE_LVL
# SET TNE TEMPORAL SMOOTHNESS
# is the smoothness coefficient in a simple
# AR(1) autocorrelation model (much simpler than that actually used in
# the FILM timeseries analysis but good enough for the efficiency
# calculation here).
T_SMOOTH=$FW_CONFIG_T_SMOOTH
####################################################################
# PREPROCESSING OPTIONS
####################################################################
# RUN MOTION CORRECTION
MC=$FW_CONFIG_MC
# RUN SLICE TIMING CORRECTION
# 0 : None
# 1 : Regular up (0, 1, 2, 3, ...)
# 2 : Regular down
# 3 : Use slice order file
# 4 : Use slice timings file
# 5 : Interleaved (0, 2, 4 ... 1, 3, 5 ... )
STC=$FW_CONFIG_STC
# SLICE ORDER/TIMING FILE
# If at slice order or timing file is chosen,
# This must also be set
if [[ $STC == 2 ]]; then
SLICE_FILE=$(find ${SLICE_FILE_DIR}/* -type f)
else
SLICE_FILE=''
fi
# RUN BRAIN EXTRACTION USING FSL's BET
BET=$FW_CONFIG_BET
# SET THE FWHM FOR SPATIAL SMOOTHING (mm)
FWHM=$FW_CONFIG_FWHM
# RUN INTENSITY NORMALIZATION
INT_NORM=$FW_CONFIG_INT_NORM
# HIGHPASS FILTER CUTOFF (seconds)
HPF_CUTOFF=$FW_CONFIG_HPF_CUTOFF
# RUN HIGHPASS FILTERING
HPF=$FW_CONFIG_HPF
# RUN LOWPASS FILTERING
LPF=$FW_CONFIG_LPF
REG_STANDARD_IMAGE=$FW_CONFIG_REG_STANDARD_IMAGE
####################################################################
# CREATE TEMPLATE
####################################################################
# Create a lost of all the variable names
# which match the place-holding text in the template
VAR_STRINGS=( INPUT_DATA TR NUM_VOL NUM_VOX DEL_VOL FEAT_OUTPUT_DIR BB_THRESH Z_THRESH NOISE_LVL T_SMOOTH MC STC SLICE_FILE BET FWHM INT_NORM HPF_CUTOFF HPF LPF REG_STANDARD_IMAGE )
DESIGN_FILE=${OUTPUT_DIR}/Design.fsf
cp ${TEMPLATE} ${DESIGN_FILE}
# loop through and preform substitution
for var_name in ${VAR_STRINGS[@]}; do
var_val=` eval 'echo $'$var_name `
#We need to replace and backslashes with "\/"
var_val=` echo ${var_val////"\/"} `
sed -i -e "s/\^${var_name}\^/${var_val}/g" ${DESIGN_FILE}
done
####################################################################
# RUN THE Algorithm with the .FSF FILE
####################################################################
echo -e "${CONTAINER} Running FEAT..."
time feat ${DESIGN_FILE}
FEAT_EXIT_STATUS=$?
if [[ $FEAT_EXIT_STATUS == 0 ]]; then
echo -e "${CONTAINER} FEAT completed successfully!"
fi
####################################################################
# Upon success, convert index to a webpage
####################################################################
if [[ $FEAT_EXIT_STATUS == 0 ]]; then
# Convert index to standalone index
echo "$CONTAINER generating output html..."
output_html_files=$(find ${OUTPUT_DIR} -type f -name "*.html")
for f in $output_html_files; do
web2htmloutput=${OUTPUT_DIR}/`basename $f`
python /opt/webpage2html/webpage2html.py -q -s "$f" > "$web2htmloutput"
rm -f $f # Don't keep the html around once the new file is generated.
done
fi
####################################################################
# CLEANUP THE OUTPUT DIRECTORIES
####################################################################
if [[ $FEAT_EXIT_STATUS == 0 ]]; then
echo -e "${CONTAINER} Compressing outputs..."
# FEAT will create an ${FEAT_OUTPUT_DIR}.feat directory anyways, so we can
# delete the old one and set the var to the actual output directory
rm -rf ${FEAT_OUTPUT_DIR}
FEAT_OUTPUT_DIR=${FEAT_OUTPUT_DIR}.feat
# Zip and move the relevant files to the output directory
pushd ${FEAT_OUTPUT_DIR} > /dev/null
zip -rq ${OUTPUT_DIR}/logs.zip logs && rm -rf logs
if [[ ${FW_CONFIG_MC} == 1 ]]; then
zip -rq ${OUTPUT_DIR}/mc.zip mc && rm -rf mc
fi
if [[ ${FW_CONFIG_REG_STANDARD_IMAGE} == 1 ]]; then
zip -rq ${OUTPUT_DIR}/reg.zip reg && rm -rf reg
fi
popd > /dev/null
mv ${FEAT_OUTPUT_DIR}/* ${OUTPUT_DIR} && rm -rf ${FEAT_OUTPUT_DIR}
fi
###############################################################################
# EXIT
###############################################################################
if [[ $FEAT_EXIT_STATUS == 0 ]]; then
echo -e "$CONTAINER Done!"
exit 0
else
echo "$CONTAINER Error while running FEAT... Exiting($FEAT_EXIT_STATUS)"
exit $FEAT_EXIT_STATUS
fi