-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperform_group_ica2.sh
More file actions
261 lines (183 loc) · 9.37 KB
/
Copy pathperform_group_ica2.sh
File metadata and controls
261 lines (183 loc) · 9.37 KB
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
#!/bin/bash
# This script performs dual regression of group ICA results and
# computes the dice coefficient between each subject's ICs and
# each of the standard RSNs
#
# The following scripts are called:
# settings_pipeline: assigns, for each pipeline, input functional data and output directory
#
# Analysis inputs, specified in the begining of the script:
# pe_dir: phase enconding direction
# subj_list: list of subjects to be analysed
# cleanup_list: cleanup pipeline list (from a predefined list)
#
# The cleanup pipelines suported in this version are the following:
# all: ica-fix + nuisance regression (WM, CSF, RP, MO)
# all_gs: ica-fix + nuisance regression (WM, CSF, RP, MO, GS)
# nuisance: nuisance regression (WM, CSF, RP, MO)
# nuisance_gs: nuisance regression (WM, CSF, RP, MO, GS)
# icafix: ica-fix
# icafix_physio: ica-fix + nuisance regression (WM, CSF)
# icafix_motion: ica-fix + nuisance regression (MO, RP)
# nocleanup: none
#
path=/home/mxavier/eeg-fmri/
cd $path
#--------------------------------------------- Declare analysis settings ---------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------#
# Declare analysis settings
dataset=NODDI
pe_dir="y-" # phase encoding direction
task="task-rest" # "task-rest" "task-calib"
# Run list
#run_list=("run-1" "run-2" "run-3")
run_list=("run-1")
# Cleanup list:
cleanup_list=("ica_mo_csf_wm_reg")
# Declare reference RSN template ("smith" - Smith, 2009; "yeo" - Yeo, 2011)
rsn_template="smith"
if [[ $pe_dir == y- ]]; then pedir_dir="minusy"; else pedir_dir="plusy"; fi
#---------------------------------------- Read RSNs list from input .txt file ----------------------------------------#
#---------------------------------------------------------------------------------------------------------------------#
# Specify which .txt file containing the rsn names is going to be read
if [ $rsn_template == "smith" ]; then
input_list="list_smith_rsns.txt"
ref_dir=STANDARD
ref=PNAS_Smith09_rsn10_bin.nii.gz
elif [ $rsn_template == "yeo" ]; then
input_list="list_yeo_rsns.txt"
ref_dir=STANDARD
ref=Yeo11_rsn7_bin.nii.gz
fi
#--------------------------------------------------- Read dataset settings -------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------#
# Create unique temporary directory
#if find $path/tmp -mindepth 1 -maxdepth 1 | read; then rm $path/tmp/*; fi
tmpdir=$path/tmp
rm -rf $tmpdir; mkdir $tmpdir
# Read dataset settings
. settings_dataset.sh
read subj_list < $tmpdir/subj_list.txt
#------------------------------------------------- Assign RSN list --------------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------#
# List of RSNs
rsn_num=`fslval "${ref_dir}/${ref}" dim4`
rsn_num=$(echo "$rsn_num - 1"| bc -l)
declare -a rsn_list=()
c=0
while [ $c -le 9 ] && [ $c -le $rsn_num ]; do
rsn_list=( "${rsn_list[@]}" "000${c}" )
c=$(echo "$c + 1" | bc -l)
done
while [ $c -le $rsn_num ]; do
rsn_list=( "${rsn_list[@]}" "00${c}" )
c=$(echo "$c + 1" | bc -l)
done
#------------------------------------------- Go through cleanup pipelines -------------------------------------------#
#---------------------------------------------------------------------------------------------------------------------#
# Iterate through cleanup pipelines
for cleanup in "${cleanup_list[@]}"; do
cd $path
# Assign flags and inputs for pipeline
. settings_pipeline.sh
# Read variables created in child process, in temporary directory
read func_data < $tmpdir/func_data_final.txt
read gica_dir < $tmpdir/gica_dir.txt;
#---------------------------------------------- Run dual regression ------------------------------------------------#
#-------------------------------------------------------------------------------------------------------------------#
# Create inputlist_4groupICA .txt file
cd $dataset/PREPROCESS/$task
# Write input list for melodic ica
rm inputlist_4groupICA.txt
for run in "${run_list[@]}"; do
for i in "${subj_list[@]}"; do
if [[ $run == "run-2" ]] && [[ $i == "sub-02" ]]; then continue; fi
if [[ $run == "run-3" ]] && [[ $i == "sub-03" ]]; then continue; fi
if [[ $run == "run-3" ]] && [[ $i == "sub-28" ]]; then continue; fi
echo $path/$dataset/PREPROCESS/$task/$i/$run/$pedir_dir/${func_data}2standard >> inputlist_4groupICA.txt
done
done
# Perform dual regression, turn on variance normalisation of the timecourses used as stage-2 regressors
dual_regression groupICA/$gica_dir/$rsn_template/melodic_IC 1 -1 0 groupICA.dr `cat inputlist_4groupICA.txt`
echo "Performed dual regression"
#------------------------------------------- Obtain dice coefficient for -------------------------------------------#
#------------------------------------------------ each subject ICs -------------------------------------------------#
s=0
# Split binarized reference images into separate images
fslsplit $path/$ref_dir/$ref groupICA.stats/STD
for run in "${run_list[@]}"; do
for i in "${subj_list[@]}"; do
if [[ $run == "run-2" ]] && [[ $i == "sub-02" ]]; then continue; fi
if [[ $run == "run-3" ]] && [[ $i == "sub-03" ]]; then continue; fi
if [[ $run == "run-3" ]] && [[ $i == "sub-28" ]]; then continue; fi
if [ $s -le 9 ]; then
sub_num="0$s"
else
sub_num=$s
fi
# Binarize subject IC image, thresholding at Z=3
fslmaths "groupICA.dr/dr_stage2_subject000${sub_num}_Z" -thr 3 -bin "groupICA.stats/$i"
# Split binarized subject IC maps into separate images
fslsplit "groupICA.stats/$i" groupICA.stats/subj
cd groupICA.stats
# Assign IC list for current patient
ic_num=`fslval "$i" dim4`
ic_num=$(echo "$ic_num -1"| bc -l)
unset ic_list
declare -a ic_list=()
c=0
while [ $c -le 9 ] && [ $c -le $ic_num ]; do
ic_list=( "${ic_list[@]}" "000${c}" )
c=$(echo "$c + 1" | bc -l)
done
while [ $c -le 99 ] && [ $c -le $ic_num ]; do
ic_list=( "${ic_list[@]}" "00${c}" )
c=$(echo "$c + 1" | bc -l)
done
while [ $c -le $ic_num ]; do
ic_list=( "${ic_list[@]}" "0${c}" )
c=$(echo "$c + 1" | bc -l)
done
# Iterate through subject's ICs
for k in "${ic_list[@]}"; do
# Iterate through RSNs
for j in "${rsn_list[@]}"; do
# Create an image of the intersection between the two volumes
fslmaths "STD${j}" -mul "subj${k}" STD_subj
# Compute the number of nonzero voxels in each of the volumes
STD_vox=`fslstats "STD${j}" -V`; STD_vox=`awk '{ print $1}' <<< "$STD_vox"`
subj_vox=`fslstats "subj${k}" -V`; subj_vox=`awk '{ print $1}' <<< "$subj_vox"`
STD_subj_vox=`fslstats "STD_subj" -V`; STD_subj_vox=`awk '{ print $1}' <<< "$STD_subj_vox"`
# Compute the dice coefficient (only 4 decimal digits) between the two volumes
dice=$(echo "scale=4; 2 * $STD_subj_vox / ($STD_vox + $subj_vox)"| bc -l)
# Input the dice coefficient into the .txt file
# It will add this dice coef to the next line of the file
echo $dice >> "dice_${i}_ic${k}.txt"
# Remove the intersection volume
rm STD_subj*
done #rsns
done #ics
# Append subject's IC file to main file (at the end)
paste dice* | column -s $'\t' -t >> "${i}_dice.txt"
# Save file in subjects PREPROCESS directory
if [[ ! -d $path/$dataset/PREPROCESS/$task/$i/$run/$pedir_dir/$gica_dir ]]; then mkdir $path/$dataset/PREPROCESS/$task/$i/$run/$pedir_dir/$gica_dir; fi;
if [[ ! -d $path/$dataset/PREPROCESS/$task/$i/$run/$pedir_dir/$gica_dir/$rsn_template ]]; then mkdir $path/$dataset/PREPROCESS/$task/$i/$run/$pedir_dir/$gica_dir/$rsn_template; fi;
cp "${i}_dice.txt" $path/$dataset/PREPROCESS/$task/$i/$run/$pedir_dir/$gica_dir/$rsn_template
echo "Computed dice coefficients for subject $i"
# Remove files of current subject
imrm sub*
rm dice*
# Update subject number
s=$(echo "$s + 1" | bc -l)
cd ..
done # subjs
# Move all files to analysis directory
if [[ ! -d $path/$dataset/PREPROCESS/$task/groupICA.stats/$gica_dir/$rsn_template/$run ]]; then mkdir $path/$dataset/PREPROCESS/$task/groupICA.stats/$gica_dir/$rsn_template/$run; fi;
mv $path/$dataset/PREPROCESS/$task/groupICA.stats/sub* $path/$dataset/PREPROCESS/$task/groupICA.stats/$gica_dir/$rsn_template/$run
done # runs
# Move all dual regression files to current analysis directory
if [[ ! -d groupICA.dr/$gica_dir ]]; then mkdir groupICA.dr/$gica_dir; fi;
if [[ ! -d groupICA.dr/$gica_dir/$rsn_template ]]; then mkdir groupICA.dr/$gica_dir/$rsn_template; fi;
mv -f groupICA.dr/mas* groupICA.dr/dr* groupICA.dr/script* groupICA.dr/$gica_dir/$rsn_template
cd groupICA.stats; rm STD00*
done # cleanup