-
Notifications
You must be signed in to change notification settings - Fork 5
/
cat_batch_long.sh
executable file
·398 lines (332 loc) · 10.3 KB
/
cat_batch_long.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
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
#! /bin/bash
# Call CAT12 longitudinal pipeline from shell
# ______________________________________________________________________
#
# Christian Gaser, Robert Dahnke
# Structural Brain Mapping Group (https://neuro-jena.github.io)
# Departments of Neurology and Psychiatry
# Jena University Hospital
# ______________________________________________________________________
# $Id$
########################################################
# global parameters
########################################################
matlab=matlab # you can use other matlab versions by changing the matlab parameter
cwd=$(dirname "$0")
# if a relative path was given add current folder to name
if [ "$cwd" == "." ]; then
cwd=$(pwd);
fi
cat12_dir=$cwd
spm12=$(dirname "$cwd")
spm12=$(dirname "$spm12")
spm12dir=spm12
LOGDIR=$PWD
export_dartel=0
output_surface=1
long_model=3
printlong=2
time=`date "+%Y%b%d_%H%M"`
defaults_tmp=/tmp/defaults$$.m
fg=0
bids=0
bids_folder=
########################################################
# run main
########################################################
main ()
{
parse_args ${1+"$@"}
check_matlab
check_files
modifiy_defaults
run_cat12
exit 0
}
########################################################
# check arguments and files
########################################################
parse_args ()
{
local optname optarg
count=0
if [ $# -lt 1 ]; then
help
exit 1
fi
while [ $# -gt 0 ]; do
optname="`echo $1 | sed 's,=.*,,'`"
optarg="`echo $2 | sed 's,^[^=]*=,,'`"
paras="$paras $optname $optarg"
case "$1" in
--defaults* | -d*)
exit_if_empty "$optname" "$optarg"
defaults=$optarg
shift
;;
--model* | -model*)
exit_if_empty "$optname" "$optarg"
long_model=$optarg
shift
;;
--print* | longlong*)
exit_if_empty "$optname" "$optarg"
printlong=$optarg
shift
;;
--matlab* | -m*)
exit_if_empty "$optname" "$optarg"
matlab=$optarg
shift
;;
--export-dartel* | -e*)
export_dartel=1
;;
--no-surf* | -ns*)
output_surface=0
;;
--nojvm | -nj*)
nojvm=" -nojvm "
;;
--fg* | -fg*)
fg=1
;;
--bids_folder* | --bids-folder* | -bf*)
exit_if_empty "$optname" "$optarg"
bids_folder=$optarg
shift
;;
--b* | -b*)
exit_if_empty "$optname" "$optarg"
bids=1
;;
--logdir* | -log*)
exit_if_empty "$optname" "$optarg"
LOGDIR=$optarg
if [ ! -d $LOGDIR ]; then
mkdir -p $LOGDIR
fi
shift
;;
--small* | -l*)
long_model=1
;;
--large* | -l*)
long_model=2
;;
-h | --help | -v | --version | -V)
help
exit 1
;;
-*)
echo "`basename $0`: ERROR: Unrecognized option \"$1\"" >&2
;;
*)
ARRAY[$count]=$1
((count++))
;;
esac
shift
done
}
########################################################
# check arguments
########################################################
exit_if_empty ()
{
local desc val
desc="$1"
shift
val="$*"
if [ ! -n "$val" ]; then
echo 'ERROR: No argument given with \"$desc\" command line argument!' >&2
exit 1
fi
}
########################################################
# check files
########################################################
check_files ()
{
SIZE_OF_ARRAY="${#ARRAY[@]}"
if [ "$SIZE_OF_ARRAY" -eq 0 ]; then
echo 'ERROR: No files given!' >&2
help
exit 1
fi
if [ "$SIZE_OF_ARRAY" -lt 2 ]; then
echo 'ERROR: You have to define at least two files for longitudinal processing!' >&2
help
exit 1
fi
i=0
while [ "$i" -lt "$SIZE_OF_ARRAY" ]; do
if [ ! -f "${ARRAY[$i]}" ]; then
if [ ! -L "${ARRAY[$i]}" ]; then
echo ERROR: File ${ARRAY[$i]} not found
help
exit 1
fi
fi
((i++))
done
}
########################################################
# modify defaults
########################################################
modifiy_defaults ()
{
pwd=$PWD
# argument empty?
if [ -n "${defaults}" ]; then
# check whether absolute or relative names were given
if [ -f "${pwd}/${defaults}" ]; then
defaults="${pwd}/${defaults}"
fi
# check whether defaults file exist
if [ ! -f "${defaults}" ]; then
echo Default file "$defaults" not found.
exit
fi
else
defaults=${cat12_dir}/cat_defaults.m
fi
# modifiy defaults if needed
cp ${defaults} ${defaults_tmp}
if [ -n "$bids_folder" ]; then
echo "cat.extopts.bids_folder = '${bids_folder}';" >> ${defaults_tmp}
echo "cat.extopts.bids_yes = 1;" >> ${defaults_tmp}
fi
if [ "$bids" -eq 1 ]; then
echo "cat.extopts.bids_yes = 1;" >> ${defaults_tmp}
fi
}
########################################################
# run cat12 long pipeline
########################################################
run_cat12 ()
{
pwd=$PWD
# we have to go into toolbox folder to find matlab files
cd $cwd
if [ ! -n "${LOGDIR}" ]; then
LOGDIR=$(dirname "${ARRAY[0]}")
fi
# we have to add current path if cat_batch_cat.sh was called from relative path
if [ -d ${pwd}/${spm12} ]; then
spm12=${pwd}/${spm12}
fi
export MATLABPATH=$spm12
SIZE_OF_ARRAY="${#ARRAY[@]}"
TMP=/tmp/cat_$$
i=0
while [ "$i" -lt "$SIZE_OF_ARRAY" ]; do
# check whether absolute or relative names were given
if [ ! -f ${ARRAY[$i]} ]; then
if [ -f "${pwd}/${ARRAY[$i]}" ]; then
FILE="${pwd}/${ARRAY[$i]}"
fi
else
FILE=${ARRAY[$i]}
fi
# replace white spaces
FILE=$(echo "$FILE" | sed -e 's/ /\\ /g')
if [ ! -n "${ARG_LIST}" ]; then
ARG_LIST="$FILE"
else
ARG_LIST="${ARG_LIST} $FILE"
fi
((i++))
done
echo ${ARG_LIST} >> ${TMP}
time=`date "+%Y%b%d_%H%M"`
vbmlog=${LOGDIR}/cat_${HOSTNAME}_${time}.log
# if relative foldername were given we have to add the data folder because we change into cat12 folder
if [ ! -d ${LOGDIR} ]; then
vbmlog=${pwd}/${vbmlog}
fi
echo Check $vbmlog for logging information
echo
COMMAND="addpath('${spm12}'); cat_batch_long('${TMP}','${output_surface}','${long_model}','${defaults_tmp}','${export_dartel}','{printlong}')"
echo Running ${ARG_LIST}
echo > $vbmlog
echo ---------------------------------- >> $vbmlog
date >> $vbmlog
echo ---------------------------------- >> $vbmlog
echo >> $vbmlog
echo $0 $ARG_LIST >> $vbmlog
echo >> $vbmlog
if [ "$fg" -eq 0 ]; then
nohup ${matlab} "$nojvm" -nodisplay -nosplash -r "$COMMAND" >> $vbmlog 2>&1 &
else
nohup ${matlab} "$nojvm" -nodisplay -nosplash -r "$COMMAND" >> $vbmlog 2>&1
fi
exit 0
}
########################################################
# check if matlab exist
########################################################
check_matlab ()
{
found=`which ${matlab} 2>/dev/null`
if [ ! -n "$found" ]; then
echo $matlab not found.
exit 1
fi
}
########################################################
# help
########################################################
help ()
{
cat <<__EOM__
USAGE:
cat_batch_long.sh filenames|filepattern [-d default_file] [-m matlabcommand]
[-log logdir] [-ns] [-large] [-model longmodel] [-printlong printlong] [-e] [-nj]
-m <FILE> | --matlab <FILE> matlab command (default $matlab)
-d <FILE> | --defaults <FILE> optional default file (default ${cat12_dir}/cat_defaults.m)
-log <FILE> | --logdir directory for log-file (default $LOGDIR)
-fg | --fg do not run matlab process in background
-ns | --no-surf disable surface and thickness estimation
-e | --export-dartel export affine registered segmentations for Dartel
-large | --large use longitudinal model for detecting large changes (i.e. ageing)
This option is only thought for compatibility with older scripts. Do not use that option together with the model flag.
-small | --small use longitudinal model for detecting smaller changes (i.e. plasticity)
This option is only thought for compatibility with older scripts. Do not use that option together with the model flag.
-nj | --nojvm supress call of jvm using the -nojvm flag
-model | --model longitudinal model:
0 - detect large changes with brain/head growth (i.e. developmental effects)
1 - detect small changes (i.e. due to plasticity)
2 - detect large changes (i.e. ageing)
3 - save results for both models 1 and 2 (default)
-printlong | --printlong print longitudinal report
0 - no printing
1 - print report but only volume results
2 - print full report (default)
-b | --bids use default BIDS path (i.e. '../derivatives/CAT12.x_rxxxx')
-bf <STRING>| --bids_folder <STRING> define BIDS path
Processing is only supported for one subject.
Optionally you can set the matlab command with the "-m" option. As default no display
is used (via the -nodisplay option in matlab). However sometimes the batch file needs
a graphical output and the display should be enabled with the option "-d".
PURPOSE:
Command line call of longitudinal segmentation pipeline
EXAMPLE
cat_batch_long.sh all_files*.nii -m /Volumes/UltraMax/MATLAB_R2010b.app/bin/matlab
This command will process all given files in the longitudinal pipeline. As matlab command
/Volumes/UltraMax/MATLAB_R2010b.app/bin/matlab will be used.
INPUT:
filenames
OUTPUT:
${LOGDIR}/spm_${HOSTNAME}_${time}.log for log information
USED FUNCTIONS:
SPM12
SETTINGS
matlab command: $matlab
This script was written by Christian Gaser ([email protected]).
__EOM__
}
########################################################
# call main program
########################################################
main ${1+"$@"}