-
Notifications
You must be signed in to change notification settings - Fork 266
/
hcp_fix_multi_run
executable file
·401 lines (333 loc) · 15.7 KB
/
hcp_fix_multi_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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
# hcp_fix_multi_run - wrapper script for FIX, for HCP pipelines to clean multiple fMRI runs at a time (useful for cleaning shorter runs)
#
# Requires FIX version 1.065 or later and FSL 6.0 melodic version
#
# Stephen Smith and Matthew F. Glasser
#
# SHCOPYRIGHT
#
# Changes by Timothy B. Brown <tbbrown@wustl.edu>
#
# 1) Changes to support 7T ICAFIX processing based on changes originally made by Keith Jamison <kjamison@umn.edu>
# 2) Changes to log output messages to aid in debugging
#
DEBUG="TRUE"
g_script_name=$(basename "${0}")
log_Debug() {
local msg="${1}"
local date_time=$(date)
local output_msg
if [ "${DEBUG}" = "TRUE" ]; then
output_msg="${date_time} - ${g_script_name} - DEBUG - ${msg}"
echo "${output_msg}"
>&2 echo "${output_msg}"
fi
}
log_Inform() {
local msg="${1}"
local date_time=$(date)
local output_msg
output_msg="${date_time} - ${g_script_name} - INFORM - ${msg}"
echo "${output_msg}"
>&2 echo "${output_msg}"
}
log_Error() {
local msg="${1}"
local date_time=$(date)
local output_msg
output_msg="${date_time} - ${g_script_name} - ERROR - ${msg}"
echo ${output_msg}
>&2 echo "${output_msg}"
}
# Set this before calling hcp_fix instead, to make it more flexible
#FSL_FIXDIR=$( cd $(dirname $0) ; pwd)
#export FSL_FIXDIR
log_Inform "FSL_FIXDIR: ${FSL_FIXDIR}"
# All fix settings are held in the settings.sh file - edit this file to suit your setup
source ${FSL_FIXDIR}/settings.sh
#############################################################
Usage() {
cat <<EOF
hcp_fix <4D_FMRI_data> <highpass> <concat_name> [<TrainingFile>]
with <highpass> being the temporal highpass full-width (2*sigma) in seconds
e.g. hcp_fix BOLD_REST1_RL/BOLD_REST1_RL.nii.gz 200
for detrending-like behaviour, set <highpass> to 2000
EOF
exit 1
}
[ "$2" = "" ] && Usage
unset POSIXLY_CORRECT
demeanMovementRegressors() {
In=${1}
log_Debug "demeanMovementRegressors: In: ${In}"
Out=${2}
log_Debug "demeanMovementRegressors: Out: ${Out}"
log_Debug "demeanMovementRegressors: getting nCols"
nCols=$(head -1 ${In} | wc -w)
log_Debug "demeanMovementRegressors: nCols: ${nCols}"
log_Debug "demeanMovementRegressors: getting nRows"
nRows=$(wc -l < ${In})
log_Debug "demeanMovementRegressors: nRows: ${nRows}"
AllOut=""
c=1
while [ ${c} -le ${nCols} ] ; do
ColIn=`cat ${In} | sed 's/ / /g' | sed 's/ / /g' | sed 's/^ //g'| cut -d " " -f ${c}`
valsum=0
r=1
while [ ${r} -le ${nRows} ] ; do
val=`echo "${ColIn}" | head -${r} | tail -1`
valsum=`echo ${valsum} + ${val} | bc -l`
r=$((${r}+1))
done
valmean=`echo ${valsum} / ${r} | bc -l`
ColOut=""
r=1
while [ ${r} -le ${nRows} ] ; do
val=`echo "${ColIn}" | head -${r} | tail -1`
newval=`echo "${val} - ${valmean}" | bc -l`
ColOut=`echo ${ColOut} $(printf "%10.6f" $newval)`
r=$((${r}+1))
done
ColOut=`echo ${ColOut} | tr ' ' '\n'`
AllOut=`paste <(echo "${AllOut}") <(echo "${ColOut}")` #Figure out how to do this paste
c=$((${c}+1))
done
echo "${AllOut}" > ${Out}
}
hp=$2
log_Inform "hp: ${hp}"
unset TrainingData
if [ $# -ge 4 ] ; then
TrainingData=$4
fi
fmris=`echo ${1} | sed 's/@/ /g'` # replaces the @ that combines the filenames with ' '
log_Inform "fmris: ${fmris}"
ConcatName="${3}"
log_Inform "ConcatName: ${ConcatName}"
DIR=`pwd`
echo $fmris | tr ' ' '\n' #separates paths separated by ' '
#Loops over the files and does highpass to each of them
log_Inform "Looping over files and doing highpass to each of them"
NIFTIvolMergeSTRING=""
NIFTIvolhpMergeSTRING=""
CIFTIMergeSTRING=""
CIFTIhpMergeSTRING=""
MovementTXTMergeSTRING=""
MovementNIFTIMergeSTRING=""
MovementNIFTIhpMergeSTRING=""
SBRefVolSTRING=""
MeanVolSTRING=""
MeanCIFTISTRING=""
for fmri in $fmris ; do
log_Inform "Top of loop through fmris: fmri: ${fmri}"
NIFTIvolMergeSTRING=`echo "${NIFTIvolMergeSTRING}$($FSLDIR/bin/remove_ext $fmri)_demean "`
NIFTIvolhpMergeSTRING=`echo "${NIFTIvolhpMergeSTRING}$($FSLDIR/bin/remove_ext $fmri)_hp$hp "`
SBRefVolSTRING=`echo "${SBRefVolSTRING}$($FSLDIR/bin/remove_ext $fmri)_SBRef "`
MeanVolSTRING=`echo "${MeanVolSTRING}$($FSLDIR/bin/remove_ext $fmri)_mean "`
CIFTIMergeSTRING=`echo "${CIFTIMergeSTRING} -cifti $($FSLDIR/bin/remove_ext $fmri)_Atlas_demean.dtseries.nii"`
CIFTIhpMergeSTRING=`echo "${CIFTIhpMergeSTRING} -cifti $($FSLDIR/bin/remove_ext $fmri)_Atlas_hp$hp.dtseries.nii"`
MeanCIFTISTRING=`echo "${MeanCIFTISTRING} -cifti $($FSLDIR/bin/remove_ext $fmri)_Atlas_mean.dscalar.nii "`
cd `dirname $fmri`
log_Debug "pwd: "$(pwd)
fmri=`basename $fmri`
fmri=`$FSLDIR/bin/imglob $fmri`
[ `imtest $fmri` != 1 ] && echo No valid 4D_FMRI input file specified && exit 1
fmri_orig=$fmri
tr=`$FSLDIR/bin/fslval $fmri pixdim4`
log_Inform "tr: $tr"
log_Inform "processing FMRI file $fmri with highpass $hp"
${FSLDIR}/bin/fslmaths $fmri -Tmean ${fmri}_mean
${FSLDIR}/bin/fslmaths $fmri -sub ${fmri}_mean ${fmri}_demean
if [ $hp -gt 0 ] ; then
log_Inform "running highpass"
hptr=`echo "10 k $hp 2 / $tr / p" | dc -`
log_Inform "hptr $hptr"
${FSLDIR}/bin/fslmaths $fmri -sub ${fmri}_mean -bptf $hptr -1 ${fmri}_hp$hp
fi
demeanMovementRegressors Movement_Regressors.txt Movement_Regressors_demean.txt
MovementTXTMergeSTRING=`echo "${MovementTXTMergeSTRING}$(pwd)/Movement_Regressors_demean.txt "`
mkdir -p ${fmri}_hp$hp.ica/mc
if [ -f Movement_Regressors.txt ] ; then
log_Debug "About to create ${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf.par file"
cat Movement_Regressors.txt | awk '{ print $4 " " $5 " " $6 " " $1 " " $2 " " $3}' > ${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf.par
else
log_Error "Movement_Regressors.txt not retrieved properly."
exit -1
fi
MovementNIFTIMergeSTRING=`echo "${MovementNIFTIMergeSTRING}$(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf.nii.gz "`
MovementNIFTIhpMergeSTRING=`echo "${MovementNIFTIhpMergeSTRING}$(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf_hp.nii.gz "`
log_Inform "functionmotionconfounds log file is to be named: .fix.functionmotionconfounds.log instead of .fix.log"
cd ${fmri}_hp$hp.ica
${FSL_FIXDIR}/call_matlab.sh -l .fix.functionmotionconfounds.log -f functionmotionconfounds $tr $hp
cd ..
fslmaths $(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf.nii.gz -Tmean $(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf_mean.nii.gz
fslmaths $(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf.nii.gz -sub $(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf_mean.nii.gz $(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf.nii.gz
$FSLDIR/bin/imrm $(pwd)/${fmri}_hp$hp.ica/mc/prefiltered_func_data_mcf_conf_mean.nii.gz
${FSL_FIX_WBC} -cifti-reduce $($FSLDIR/bin/remove_ext $fmri)_Atlas.dtseries.nii MEAN $($FSLDIR/bin/remove_ext $fmri)_Atlas_mean.dscalar.nii
${FSL_FIX_WBC} -cifti-math "TCS - MEAN" $($FSLDIR/bin/remove_ext $fmri)_Atlas_demean.dtseries.nii -var TCS $($FSLDIR/bin/remove_ext $fmri)_Atlas.dtseries.nii -var MEAN $($FSLDIR/bin/remove_ext $fmri)_Atlas_mean.dscalar.nii -select 1 1 -repeat
${FSL_FIX_WBC} -cifti-convert -to-nifti ${fmri}_Atlas.dtseries.nii ${fmri}_Atlas_FAKENIFTI.nii.gz
${FSLDIR}/bin/fslmaths ${fmri}_Atlas_FAKENIFTI.nii.gz -bptf $hptr -1 ${fmri}_Atlas_hp$hp_FAKENIFTI.nii.gz
${FSL_FIX_WBC} -cifti-convert -from-nifti ${fmri}_Atlas_hp$hp_FAKENIFTI.nii.gz ${fmri}_Atlas.dtseries.nii ${fmri}_Atlas_hp$hp.dtseries.nii
$FSLDIR/bin/imrm ${fmri}_Atlas_FAKENIFTI ${fmri}_Atlas_hp$hp_FAKENIFTI
fmri=${fmri}_hp$hp
cd ${fmri}.ica
$FSLDIR/bin/imln ../$fmri filtered_func_data
cd ..
log_Inform "Bottom of loop through fmris: fmri: ${fmri}"
done
AlreadyHP="-1" #Don't run highpass on concatenated data
#Make Concatenated Folder
ConcatFolder=`dirname ${ConcatName}`
log_Inform "Making concatenated folder: ${ConcatFolder}"
if [ ! -e ${ConcatFolder} ] ; then
mkdir ${ConcatFolder}
else
rm -r ${ConcatFolder}
mkdir ${ConcatFolder}
fi
fslmerge -tr `remove_ext ${ConcatName}`_demean ${NIFTIvolMergeSTRING} $tr
fslmerge -tr `remove_ext ${ConcatName}`_hp$hp ${NIFTIvolhpMergeSTRING} $tr
fslmerge -t `remove_ext ${ConcatName}`_SBRef ${SBRefVolSTRING}
fslmerge -t `remove_ext ${ConcatName}`_mean ${MeanVolSTRING}
fslmaths `remove_ext ${ConcatName}`_SBRef -Tmean `remove_ext ${ConcatName}`_SBRef
fslmaths `remove_ext ${ConcatName}`_mean -Tmean `remove_ext ${ConcatName}`_mean
fslmaths `remove_ext ${ConcatName}`_hp$hp -add `remove_ext ${ConcatName}`_mean `remove_ext ${ConcatName}`_hp$hp
fslmaths `remove_ext ${ConcatName}`_demean -add `remove_ext ${ConcatName}`_mean `remove_ext ${ConcatName}`
$FSLDIR/bin/imrm `remove_ext ${ConcatName}`_mean
${FSL_FIX_WBC} -cifti-merge `remove_ext ${ConcatName}`_Atlas_demean.dtseries.nii ${CIFTIMergeSTRING}
${FSL_FIX_WBC} -cifti-average `remove_ext ${ConcatName}`_Atlas_mean.dscalar.nii ${MeanCIFTISTRING}
${FSL_FIX_WBC} -cifti-math "TCS + MEAN" `remove_ext ${ConcatName}`_Atlas.dtseries.nii -var TCS `remove_ext ${ConcatName}`_Atlas_demean.dtseries.nii -var MEAN `remove_ext ${ConcatName}`_Atlas_mean.dscalar.nii -select 1 1 -repeat
${FSL_FIX_WBC} -cifti-merge `remove_ext ${ConcatName}`_Atlas_hp$hp.dtseries.nii ${CIFTIhpMergeSTRING}
cd ${ConcatFolder}
concatfmri=`basename $(remove_ext ${ConcatName})`_hp$hp
concat_fmri_orig=`basename $(remove_ext ${ConcatName})`
mkdir -p ${concatfmri}.ica
log_Debug "About to put contents of ${MovementTXTMergeSTRING} in Movement_Regressors_demean.txt file"
cat ${MovementTXTMergeSTRING} > Movement_Regressors_demean.txt
mkdir ${concatfmri}.ica/mc
fslmerge -tr ${concatfmri}.ica/mc/prefiltered_func_data_mcf_conf_hp ${MovementNIFTIhpMergeSTRING} $tr
fslmerge -tr ${concatfmri}.ica/mc/prefiltered_func_data_mcf_conf ${MovementNIFTIMergeSTRING} $tr
log_Inform "running MELODIC"
log_Debug "About to run melodic: Contents of ${concatfmri}.ica follow"
if [ "${DEBUG}" = "TRUE" ] ; then
ls -lRa `remove_ext ${concatfmri}`.ica
fi
log_Inform "Running melodic located at: ${FSLDIR}/bin/melodic"
log_Debug "Beginning of melodic version log, help, and checksum"
if [ "${DEBUG}" = "TRUE" ] ; then
log_Debug "${FSLDIR}/bin/melodic --version"
${FSLDIR}/bin/melodic --version
log_Debug "${FSLDIR}/bin/melodic --help"
${FSLDIR}/bin/melodic --help
log_Debug "md5sum ${FSLDIR}/bin/melodic"
md5sum ${FSLDIR}/bin/melodic
fi
log_Debug "End of melodic version log, help, and checksum"
###Run on concatenated file --
# Original melodic command without logging of command
# ${FSLDIR}/bin/melodic -i $concatfmri -o ${concatfmri}.ica/filtered_func_data.ica -d -250 --nobet --report --Oall --tr=$tr
#
# melodic command with verbose output and debugging output, used when trying to determine why melodic was crashing
# melodic_cmd="${FSLDIR}/bin/melodic -i $concatfmri -o ${concatfmri}.ica/filtered_func_data.ica --nobet --report --Oall --tr=$tr --verbose --debug"
melodic_cmd="${FSLDIR}/bin/melodic -i $concatfmri -o ${concatfmri}.ica/filtered_func_data.ica --nobet --report --Oall --tr=$tr"
log_Inform "melodic_cmd: ${melodic_cmd}"
${melodic_cmd}
return_code=$?
log_Inform "melodic has been run: return_code = ${return_code}"
log_Debug "melodic has been run: Contents of ${concatfmri}.ica follow"
if [ "${DEBUG}" = "TRUE" ] ; then
ls -lRa `remove_ext ${concatfmri}`.ica
fi
if [ "${return_code}" -ne "0" ] ; then
log_Error "melodic has returned a non-zero code"
log_Error "Exiting this script with -1 return value."
exit -1
fi
cd `remove_ext ${concatfmri}`.ica
$FSLDIR/bin/imln ../$concatfmri filtered_func_data
$FSLDIR/bin/imln filtered_func_data.ica/mask mask
if [ `$FSLDIR/bin/imtest ../${concat_fmri_orig}_SBRef` = 1 ] ; then
$FSLDIR/bin/imln ../${concat_fmri_orig}_SBRef mean_func
else
$FSLDIR/bin/imln filtered_func_data.ica/mean mean_func
fi
if [ -f ../${concat_fmri_orig}_Atlas_hp$hp.dtseries.nii ] ; then
$FSLDIR/bin/imln ../${concat_fmri_orig}_Atlas_hp$hp.dtseries.nii Atlas.dtseries.nii
fi
mkdir -p reg
cd reg
i_am_at=`pwd`
log_Debug "current folder ${i_am_at}"
$FSLDIR/bin/imln ../../../../T1w_restore_brain highres
$FSLDIR/bin/imln ../../../../wmparc wmparc
$FSLDIR/bin/imln ../mean_func example_func
$FSLDIR/bin/makerot --theta=0 > highres2example_func.mat
if [ `$FSLDIR/bin/imtest ../../../../T2w` = 1 ] ; then
$FSLDIR/bin/fslmaths ../../../../T1w -div ../../../../T2w veins -odt float
$FSLDIR/bin/flirt -in ${FSL_FIXDIR}/mask_files/hcp_0.7mm_brain_mask -ref veins -out veinbrainmask -applyxfm
$FSLDIR/bin/fslmaths veinbrainmask -bin veinbrainmask
$FSLDIR/bin/fslmaths veins -div `$FSLDIR/bin/fslstats veins -k veinbrainmask -P 50` -mul 2.18 -thr 10 -min 50 -div 50 veins
$FSLDIR/bin/flirt -in veins -ref example_func -applyxfm -init highres2example_func.mat -out veins_exf
$FSLDIR/bin/fslmaths veins_exf -mas example_func veins_exf
fi
cd ../..
log_Inform "running FIX"
# Changes to handle user specified training data file
if [ "X${TrainingData}" != X ]; then
# User has specified a training data file
# add .RData suffix if not already there
if [[ "${TrainingData}" != *.RData ]]; then
TrainingData=${TrainingData}.RData
fi
# if the specified TrainingData is not a full path to an existing file,
# assume that the user is specifying the name of a file in the training_files folder in FSL_FIXDIR
if [ ! -f "${TrainingData}" ]; then
TrainingData=${FSL_FIXDIR}/training_files/${TrainingData}
fi
log_Inform "User has specified a training data file: ${TrainingData}"
# finally, if the TrainingData file is not found, report an error and get out of here
if [ ! -f "${TrainingData}" ]; then
log_Error "FIX training data not found: ${TrainingData}"
exit -1
fi
# now run fix
fix_cmd="${FSL_FIXDIR}/fix ${concatfmri}.ica ${TrainingData} 10 -m -h ${AlreadyHP}"
log_Inform "fix_cmd: ${fix_cmd}"
${fix_cmd}
return_code=$?
log_Msg "return_code from fix_cmd: ${return_code}"
if [ "${return_code}" -ne "0" ]; then
exit ${return_code}
fi
else
log_Inform "User has NOT specified a training data file"
log_Inform "using training data ${FSL_FIXDIR}/training_files/HCP_hp2000.RData"
${FSL_FIXDIR}/fix ${concatfmri}.ica ${FSL_FIXDIR}/training_files/HCP_hp2000.RData 10 -m -h ${AlreadyHP}
fi
log_Inform "Done running FIX"
log_Debug "Moving ${concatfmri}.ica/filtered_func_data_clean to ${concatfmri}_clean"
$FSLDIR/bin/immv ${concatfmri}.ica/filtered_func_data_clean ${concatfmri}_clean
log_Debug "Checking for existence of ${concatfmri}.ica/Atlas_clean.dtseries.nii"
if [ -f ${concatfmri}.ica/Atlas_clean.dtseries.nii ] ; then
log_Debug "Moving ${concatfmri}.ica to ${concat_fmri_orig}_Atlas_hp${hp}_clean.dtseries.nii"
/bin/mv ${concatfmri}.ica/Atlas_clean.dtseries.nii ${concat_fmri_orig}_Atlas_hp${hp}_clean.dtseries.nii
fi
Start="1"
for fmri in $fmris ; do
NumTPS=`${FSL_FIX_WBC} -file-information $(remove_ext ${fmri})_Atlas.dtseries.nii -no-map-info -only-number-of-maps`
Stop=`echo "${NumTPS} + ${Start} -1" | bc -l`
log_Inform "Start=${Start} Stop=${Stop}"
log_Debug "cifti merging"
${FSL_FIX_WBC} -cifti-merge `remove_ext ${fmri}`_Atlas_hp${hp}_clean.dtseries.nii -cifti ${concat_fmri_orig}_Atlas_hp${hp}_clean.dtseries.nii -column ${Start} -up-to ${Stop}
log_Debug "volume merging"
${FSL_FIX_WBC} -volume-merge `remove_ext ${fmri}`_hp${hp}_clean.nii.gz -volume ${concatfmri}_clean.nii.gz -subvolume ${Start} -up-to ${Stop}
fslmaths `remove_ext ${fmri}`_hp${hp}_clean.nii.gz -Tmean `remove_ext ${fmri}`_hp${hp}_clean_mean
fslmaths `remove_ext ${fmri}`_hp${hp}_clean.nii.gz -sub `remove_ext ${fmri}`_hp${hp}_clean_mean `remove_ext ${fmri}`_hp${hp}_clean.nii.gz ###Remove the run average mean again if added above
fslmaths `remove_ext ${fmri}`_hp${hp}_clean.nii.gz -add `remove_ext ${fmri}`_mean `remove_ext ${fmri}`_hp${hp}_clean.nii.gz
rm `remove_ext ${fmri}`_mean.nii.gz `remove_ext ${fmri}`_hp${hp}_clean_mean.nii.gz
${FSL_FIX_WBC} -cifti-math "TCS + Mean" `remove_ext ${fmri}`_Atlas_hp${hp}_clean.dtseries.nii -var TCS `remove_ext ${fmri}`_Atlas_hp${hp}_clean.dtseries.nii -var Mean `remove_ext ${fmri}`_Atlas_mean.dscalar.nii -select 1 1 -repeat
rm `remove_ext ${fmri}`_Atlas_mean.dscalar.nii
Start=`echo "${Start} + ${NumTPS}" | bc -l`
done
cd ${DIR}