-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-group-analysis-bayes.sh
executable file
·352 lines (292 loc) · 17.2 KB
/
generate-group-analysis-bayes.sh
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
#!/bin/bash
# ------------------------------------------------------------------ #
# GENERATE-GROUP-ANALYSIS
# ------------------------------------------------------------------ #
# Generates an SPM batch script for performing group analysis.
# ------------------------------------------------------------------ #
J=0
DIR=`pwd`
M="${DIR}/group-analysis.m"
echo "%% Starting... " > ${M}
L1_RESULTS_FOLDER=$1
L2_RESULTS_FOLDER=$2
CONTRAST_FILE=$3 # Contrast file
GROUP_FILE=$4 # Group file
GMS=3 # 1 = No GMS, 2 = proportional, 3 = ANCOVA
VAR=1 # 0 = Equal variance, 1 = unequal variance
#GROUPS=`awk '{print $2}' ${GROUP_FILE} | sort | uniq`
#echo $GROUPS, $GROUP_FILE
# --------------------------------------------------------------------
# Part 1 --- Full analysis with all subjects
# --------------------------------------------------------------------
# Reset the contrast counter
C=0
while read contrast; do
# Generates a different subfolder for each contrast
C=$((C+1))
contrast_name=`echo $contrast | cut -f1 -d':'`
contrast_name=`echo ${contrast_name}`
contrast_dir=${contrast_name// /_} # Subtitute spaces with '_'
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.factorial_design.dir = {'${DIR}/${L2_RESULTS_FOLDER}/all/${contrast_dir}/'};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t1.scans = {" >> ${M}
while read subject; do
subject_folder=`echo $subject | awk '{print $1}'`
contrast_file=con_`printf "%04d" ${C}`.img
if [ ! -e ${DIR}/${subject_folder}/do-not-include.txt ]; then
if [ -e ${DIR}/${subject_folder}/${L1_RESULTS_FOLDER}/${contrast_file} ]; then
echo "'${DIR}/${subject_folder}/${L1_RESULTS_FOLDER}/${contrast_file},1'" >> ${M}
else
echo "No contrast file ${contrast_file} for ${subject_folder}: Subject excluded"
fi
else
echo "Excluding subject ${subject_folder} (do-not-include file found)"
fi
done < ${GROUP_FILE}
echo "};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.cov = struct('c', {}, 'cname', {}, 'iCFI', {}, 'iCC', {});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.tm.tm_none = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.im = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.em = {''};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalc.g_omit = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalm.gmsca.gmsca_no = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalm.glonorm = ${GMS};" >> ${M}
# ----------------------------------------------------------------
# Estimate the Group-Level Contrast
# ----------------------------------------------------------------
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1) = cfg_dep;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tname = 'Select SPM.mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(1).name = 'filter';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(1).value = 'mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(2).name = 'strtype';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(2).value = 'e';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).sname = 'Factorial design specification: SPM.mat File';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).src_exbranch = substruct('.','val', '{}',{$((J-1))}, '.','val', '{}',{1}, '.','val', '{}',{1});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).src_output = substruct('.','spmmat');" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.method.Classical = 1;" >> ${M}
# ----------------------------------------------------------------
# Create the "Contrast" (A simple '1' vector)
# ----------------------------------------------------------------
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1) = cfg_dep;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tname = 'Select SPM.mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(1).name = 'filter';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(1).value = 'mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(2).name = 'strtype';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(2).value = 'e';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).sname = 'Model estimation: SPM.mat File';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).src_exbranch = substruct('.','val', '{}',{$((J-1))}, '.','val', '{}',{1}, '.','val', '{}',{1});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).src_output = substruct('.','spmmat');" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.name = '${contrast_name}';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.convec = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.sessrep = 'none';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.delete = 1;" >> ${M}
done < ${CONTRAST_FILE}
# --------------------------------------------------------------------
# Part 2 --- Analysis of the two groups, separately
# --------------------------------------------------------------------
# This part of the analysis is performed IFF there are at least two
# groups in the 'groups' file.
# --------------------------------------------------------------------
# Calculate the number of groups. There should only be two.
# (this is contrieved but I couldn't make it work with the classic
# ${#array[@]} trick...)
N=`awk '{print $2}' groups.txt | sort | uniq | wc | awk '{print $1}'`
if [ $N == 2 ]; then
# If we have at least two groups, we need to create individual
# analysis
for group in `awk '{print $2}' ${GROUP_FILE} | sort | uniq`; do
if [ ! -d ${DIR}/${L2_RESULTS_FOLDER}/${group} ]; then
echo "Creating folder for group '${group}'"
cd ${L2_RESULTS_FOLDER}
mkdir ${group}
cd ..
fi
C=0
while read contrast; do
# Generates a different subfolder for each contrast
C=$((C+1))
contrast_name=`echo $contrast | cut -f1 -d':'`
contrast_name=`echo ${contrast_name}`
contrast_dir=${contrast_name// /_} # Subtitute spaces with '_'
contrast_file=con_`printf "%04d" ${C}`.img
if [ ! -d ${DIR}/${L2_RESULTS_FOLDER}/${group}/${contrast_dir} ]; then
echo "Warning: Creating '${contrast_dir}' folder"
cd ${L2_RESULTS_FOLDER}/${group}
mkdir ${contrast_dir}
cd ../..
fi
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.factorial_design.dir = {'${DIR}/${L2_RESULTS_FOLDER}/${group}/${contrast_dir}/'};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t1.scans = {" >> ${M}
for subject in `grep ${group} ${GROUP_FILE} | awk '{print $1}'`; do
#echo " $subject"
if [ ! -e ${DIR}/${subject}/do-not-include.txt ]; then
if [ -e ${DIR}/${subject}/${L1_RESULTS_FOLDER}/${contrast_file} ]; then
echo "'${DIR}/${subject}/${L1_RESULTS_FOLDER}/${contrast_file},1'" >> ${M}
else
echo "No contrast file ${contrast_file} for ${subject}: Subject excluded"
fi
else
echo "Excluding subject ${subject} (do-not-include file found)"
fi
done
echo "};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.cov = struct('c', {}, 'cname', {}, 'iCFI', {}, 'iCC', {});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.tm.tm_none = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.im = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.em = {''};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalc.g_omit = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalm.gmsca.gmsca_no = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalm.glonorm = ${GMS};" >> ${M}
# ----------------------------------------------------------------
# Estimate the Group-Level Contrast
# ----------------------------------------------------------------
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1) = cfg_dep;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tname = 'Select SPM.mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(1).name = 'filter';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(1).value = 'mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(2).name = 'strtype';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(2).value = 'e';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).sname = 'Factorial design specification: SPM.mat File';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).src_exbranch = substruct('.','val', '{}',{$((J-1))}, '.','val', '{}',{1}, '.','val', '{}',{1});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).src_output = substruct('.','spmmat');" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.method.Classical = 1;" >> ${M}
# ----------------------------------------------------------------
# Create the "Contrast" (A simple '1' vector)
# ----------------------------------------------------------------
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1) = cfg_dep;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tname = 'Select SPM.mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(1).name = 'filter';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(1).value = 'mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(2).name = 'strtype';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(2).value = 'e';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).sname = 'Model estimation: SPM.mat File';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).src_exbranch = substruct('.','val', '{}',{$((J-1))}, '.','val', '{}',{1}, '.','val', '{}',{1});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).src_output = substruct('.','spmmat');" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.name = '${contrast_name}';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.convec = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.sessrep = 'none';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.delete = 1;" >> ${M}
done < ${CONTRAST_FILE}
done
fi
## -------------------------------------------------------------------
## PART 3: GROUP COMPARISONS
## -------------------------------------------------------------------
if [ $N == 2 ]; then
if [ ! -d ${DIR}/${L2_RESULTS_FOLDER}/comparisons ]; then
echo "Warning: Creating 'comparisons' directory"
cd ${L2_RESULTS_FOLDER}
mkdir comparisons
cd ..
fi
# Ideally, these two should be identified at the very beginning of the
# file to avoid inconsistencies if the file is rewritten during
# the execution of the script.
group1=`awk '{print $2}' groups.txt | sort | uniq | head -1`
group2=`awk '{print $2}' groups.txt | sort | uniq | tail -1`
# Reset contrast counter
C=0
while read contrast; do
# Generates a different subfolder for each contrast
C=$((C+1))
contrast_name=`echo $contrast | cut -f1 -d':'`
contrast_name=`echo ${contrast_name}`
contrast_dir=${contrast_name// /_} # Subtitute spaces with '_'
contrast_file=con_`printf "%04d" ${C}`.img
if [ ! -d ${DIR}/${L2_RESULTS_FOLDER}/comparisons/${contrast_dir} ]; then
echo "Warning: Creating '${contrast_dir}' folder"
cd ${L2_RESULTS_FOLDER}/comparisons
mkdir ${contrast_dir}
cd ../..
fi
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.factorial_design.dir = {'${DIR}/${L2_RESULTS_FOLDER}/comparisons/${contrast_dir}'};" >> ${M}
# ------------------------------------------------------------
# Group 1
# ------------------------------------------------------------
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t2.scans1 = {" >> ${M}
for subject in `grep ${group1} ${GROUP_FILE} | awk '{print $1}'`; do
if [ ! -e ${DIR}/${subject}/do-not-include.txt ]; then
if [ -e ${DIR}/${subject}/${L1_RESULTS_FOLDER}/${contrast_file} ]; then
echo "'${DIR}/${subject}/${L1_RESULTS_FOLDER}/${contrast_file},1'" >> ${M}
else
echo "No contrast file ${contrast_file} for ${subject}: Subject excluded"
fi
else
echo "Excluding subject ${subject} (do-not-include file found)"
fi
done
echo "};" >> ${M}
# ------------------------------------------------------------
# Group 2
# ------------------------------------------------------------
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t2.scans2 = {" >> ${M}
for subject in `grep ${group2} ${GROUP_FILE} | awk '{print $1}'`; do
if [ ! -e ${DIR}/${subject}/do-not-include.txt ]; then
if [ -e ${DIR}/${subject}/${L1_RESULTS_FOLDER}/${contrast_file} ]; then
echo "'${DIR}/${subject}/${L1_RESULTS_FOLDER}/${contrast_file},1'" >> ${M}
else
echo "No contrast file ${contrast_file} for ${subject}: Subject excluded"
fi
else
echo "Excluding subject ${subject} (do-not-include file found)"
fi
done
echo "};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t2.dept = 0;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t2.variance = ${VAR};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t2.gmsca = 0;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.des.t2.ancova = 0;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.cov = struct('c', {}, 'cname', {}, 'iCFI', {}, 'iCC', {});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.tm.tm_none = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.im = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.masking.em = {''};" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalc.g_omit = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalm.gmsca.gmsca_no = 1;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.factorial_design.globalm.glonorm = ${GMS};" >> ${M}
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1) = cfg_dep;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tname = 'Select SPM.mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(1).name = 'filter';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(1).value = 'mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(2).name = 'strtype';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).tgt_spec{1}(2).value = 'e';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).sname = 'Factorial design specification: SPM.mat File';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).src_exbranch = substruct('.','val', '{}',{$((J-1))}, '.','val', '{}',{1}, '.','val', '{}',{1});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.spmmat(1).src_output = substruct('.','spmmat');" >> ${M}
echo "matlabbatch{${J}}.spm.stats.fmri_est.method.Classical = 1;" >> ${M}
J=$((J+1))
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1) = cfg_dep;" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tname = 'Select SPM.mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(1).name = 'filter';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(1).value = 'mat';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(2).name = 'strtype';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).tgt_spec{1}(2).value = 'e';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).sname = 'Model estimation: SPM.mat File';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).src_exbranch = substruct('.','val', '{}',{$((J-1))}, '.','val', '{}',{1}, '.','val', '{}',{1});" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.spmmat(1).src_output = substruct('.','spmmat');" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.name = '${group1} > ${group2}';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.convec = [1 -1];" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{1}.tcon.sessrep = 'none';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{2}.tcon.name = '${group2} > ${group1}';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{2}.tcon.convec = [-1 1];" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.consess{2}.tcon.sessrep = 'none';" >> ${M}
echo "matlabbatch{${J}}.spm.stats.con.delete = 0;" >> ${M}
done < ${CONTRAST_FILE}
fi
if [ $N != 2 ]; then
# If there were more than two groups, then we have only two options:
# Either we don't have between-group designs (in which case, we exit)
# or we have more than two groups (in which case, right now, we
# cannot really do much)
if [ $N == 1 ]; then
echo "No groups found, ending here"
else
echo "More than three groups found. Cannot do analysis (${GROUPS})."
fi
fi