-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnifmri_util_epireg
executable file
·418 lines (379 loc) · 15.7 KB
/
lnifmri_util_epireg
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/bin/bash
#######################################################################
#######################################################################
# LNiF MRI Lab Pipeline for preprocessing of
# structural and functional MRI dataset
# Based on fsl_anat and edited to be compliant with local and remote
# processing on mri-server.cimec.unitn.it
# [email protected] - 20210514
#######################################################################
#######################################################################
# Copyright (C) 2012 University of Oxford
#
# Part of FSL - FMRIB's Software Library
# http://www.fmrib.ox.ac.uk/fsl
#
# Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
# Imaging of the Brain), Department of Clinical Neurology, Oxford
# University, Oxford, UK
#
#
# LICENCE
#
# FMRIB Software Library, Release 6.0 (c) 2018, The University of
# Oxford (the "Software")
#
# The Software remains the property of the Oxford University Innovation
# ("the University").
#
# The Software is distributed "AS IS" under this Licence solely for
# non-commercial use in the hope that it will be useful, but in order
# that the University as a charitable foundation protects its assets for
# the benefit of its educational and research purposes, the University
# makes clear that no condition is made or to be implied, nor is any
# warranty given or to be implied, as to the accuracy of the Software,
# or that it will be suitable for any particular purpose or for use
# under any specific conditions. Furthermore, the University disclaims
# all responsibility for the use which is made of the Software. It
# further disclaims any liability for the outcomes arising from using
# the Software.
#
# The Licensee agrees to indemnify the University and hold the
# University harmless from and against any and all claims, damages and
# liabilities asserted by third parties (including claims for
# negligence) which arise directly or indirectly from the use of the
# Software or the sale of any products based on the Software.
#
# No part of the Software may be reproduced, modified, transmitted or
# transferred in any form or by any means, electronic or mechanical,
# without the express permission of the University. The permission of
# the University is not required if the said reproduction, modification,
# transmission or transference is done without financial return, the
# conditions of this Licence are imposed upon the receiver of the
# product, and all original and amended source code is included in any
# transmitted product. You may be held legally responsible for any
# copyright infringement that is caused or encouraged by your failure to
# abide by these terms and conditions.
#
# You are not permitted under this Licence to use this Software
# commercially. Use for which any financial return is received shall be
# defined as commercial use, and includes (1) integration of all or part
# of the source code or the Software into a product for sale or license
# by or on behalf of Licensee to third parties or (2) use of the
# Software or any derivative of it for research with the final aim of
# developing software products for sale or license to a third party or
# (3) use of the Software or any derivative of it for research with the
# final aim of developing non-software products for sale or license to a
# third party, or (4) use of the Software to provide any service to an
# external organisation for which payment is received. If you are
# interested in using the Software commercially, please contact Oxford
# University Innovation ("OUI"), the technology transfer company of the
# University, to negotiate a licence. Contact details are:
# [email protected] quoting Reference Project 9564, FSL.
export LC_ALL=C
Usage() {
echo ""
echo "Usage: `basename $0` [options] --epi=<EPI image> --t1=<wholehead T1 image> --t1brain=<brain extracted T1 image> --out=<output name>"
echo " "
echo "Optional arguments"
echo " --fmap=<image> : fieldmap image (in rad/s)"
echo " --fmapmag=<image> : fieldmap magnitude image - wholehead extracted"
echo " --fmapmagbrain=<image> : fieldmap magnitude image - brain extracted"
echo " --gdc=<image> : Gradient-distortion corection warpfield"
echo " --wmseg=<image> : white matter segmentation of T1 image"
echo " --echospacing=<val> : Effective EPI echo spacing (sometimes called dwell time) - in seconds"
echo " --pedir=<dir> : phase encoding direction, dir = x/y/z/-x/-y/-z"
echo " --weight=<image> : weighting image (in T1 space)"
echo " --nofmapreg : do not perform registration of fmap to T1 (use if fmap already registered) "
echo " --noclean : do not clean up intermediate files"
echo " -v : verbose output"
echo " -h : display this help message"
echo " "
echo "e.g.: `basename $0` --epi=example_func --t1=struct --t1brain=struct_brain --out=epi2struct --fmap=fmap_rads --fmapmag=fmap_mag --fmapmagbrain=fmap_mag_brain --echospacing=0.0005 --pedir=-y"
echo " "
echo "Note that if parallel acceleration is used in the EPI acquisition then the *effective* echo spacing is the actual echo spacing between acquired lines in k-space divided by the acceleration factor."
echo " "
exit 1
}
get_opt1() {
arg=`echo $1 | sed 's/=.*//'`
echo $arg
}
get_arg1() {
if [ X`echo $1 | grep '='` = X ] ; then
echo "Option $1 requires an argument" 1>&2
exit 1
else
arg=`echo $1 | sed 's/.*=//'`
if [ X$arg = X ] ; then
echo "Option $1 requires an argument" 1>&2
exit 1
fi
echo $arg
fi
}
get_imarg1() {
arg=`get_arg1 $1`;
arg=`$FSLDIR/bin/remove_ext $arg`;
echo $arg
}
get_arg2() {
if [ X$2 = X ] ; then
echo "Option $1 requires an argument" 1>&2
exit 1
fi
echo $2
}
# list of variables to be set via the options
vepi="";
vrefhead="";
vrefbrain="";
vout="";
use_fmap=no;
use_weighting=no;
verbose=no
cleanup=yes;
fmaprads="";
fmapmaghead="";
fmapmagbrain="";
gdcwarp="";
wmseg="";
dwell="";
pe_dir="";
fdir="y";
fmapreg=yes;
# Parse them baby
if [ $# -lt 4 ] ; then Usage; exit 0; fi
while [ $# -ge 1 ] ; do
iarg=`get_opt1 $1`;
case "$iarg"
in
--epi)
vepi=`get_imarg1 $1`;
shift;;
--t1)
vrefhead=`get_imarg1 $1`;
shift;;
--t1brain)
vrefbrain=`get_imarg1 $1`;
shift;;
--fmap)
fmaprads=`get_imarg1 $1`;
use_fmap=yes;
shift;;
--fmapmag)
fmapmaghead=`get_imarg1 $1`;
shift;;
--fmapmagbrain)
fmapmagbrain=`get_imarg1 $1`;
shift;;
--gdc)
gdcwarp=`get_imarg1 $1`;
shift;;
--wmseg)
wmseg=`get_imarg1 $1`;
shift;;
--out)
vout=`get_imarg1 $1`;
shift;;
--echospacing)
dwell=`get_arg1 $1`;
if [ `echo "if ( $dwell > 0.2 ) {1}; if ( $dwell <= 0.2 ) {0}" | bc -l` = 1 ] ; then
msdwell=`echo "scale=6; $dwell / 1000.0" | bc -l`;
echo "Echo spacing should be specified in seconds, not milliseconds. Value of $dwell appears to be incorrectly specified in milliseconds. Try using the value $msdwell instead.";
exit 1;
fi
shift;;
--pedir)
pearg=`get_arg1 $1`;
# These are consistent with the ones used in FUGUE (this has been checked)
if [ $pearg = "x" ] ; then pe_dir=1; fdir="x"; fi
if [ $pearg = "y" ] ; then pe_dir=2; fdir="y"; fi
if [ $pearg = "z" ] ; then pe_dir=3; fdir="z"; fi
if [ $pearg = "-x" ] ; then pe_dir=-1; fdir="x-"; fi
if [ $pearg = "-y" ] ; then pe_dir=-2; fdir="y-"; fi
if [ $pearg = "-z" ] ; then pe_dir=-3; fdir="z-"; fi
if [ $pearg = "x-" ] ; then pe_dir=-1; fdir="x-"; fi
if [ $pearg = "y-" ] ; then pe_dir=-2; fdir="y-"; fi
if [ $pearg = "z-" ] ; then pe_dir=-3; fdir="z-"; fi
if [ X${pe_dir} = X ] ; then
echo "Error: invalid phase encode direction specified";
exit 2;
fi
shift;;
--weight)
refweight=`get_imarg1 $1`;
use_weighting=yes;
echo REFWEIGHT = $refweight;
shift;;
--nofmapreg)
fmapreg=no;
shift;;
--noclean)
cleanup=no;
shift;;
--init)
initmat=`get_arg1 $1`;
shift;;
-v)
verbose=yes;
shift;;
-h)
Usage;
exit 0;;
*)
#if [ `echo $1 | sed 's/^\(.\).*/\1/'` = "-" ] ; then
echo "Unrecognised option $1" 1>&2
exit 1
#fi
#shift;;
esac
done
### Sanity checking of arguments
if [ X$vout = X ] ; then
echo "The compulsory argument --out MUST be used"
exit 1;
fi
if [ X$vepi = X ] ; then
echo "The compulsory argument --epi MUST be used"
exit 1;
fi
if [ X$vrefhead = X ] ; then
echo "The compulsory argument --t1 MUST be used"
exit 1;
fi
if [ X$vrefbrain = X ] ; then
echo "The compulsory argument --t1brain MUST be used"
exit 1;
fi
if [ $use_fmap = yes ] ; then
if [ X$fmaprads = X ] ; then
echo "The argument --fmap MUST be usspecifieded if using fieldmaps"
exit 1;
fi
if [ X$fmapmaghead = X ] ; then
echo "The argument --fmapmag MUST be specified if using fieldmaps"
exit 1;
fi
if [ X$fmapmagbrain = X ] ; then
echo "The argument --fmapmagbrain MUST be specified if using fieldmaps"
exit 1;
fi
if [ X$pe_dir = X ] ; then
echo "The argument --pedir MUST be specified if using fieldmaps"
exit 1;
fi
if [ X$dwell = X ] ; then
echo "The argument --echospacing MUST be specified if using fieldmaps"
exit 1;
fi
fi
if [ $verbose = yes ] ; then
echo "Arguments are:"
echo " vepi = $vepi"
echo " vrefhead = $vrefhead"
echo " vrefbrain = $vrefbrain"
echo " vout = $vout"
echo " fmaprads = $fmaprads"
echo " fmapmaghead = $fmapmaghead"
echo " fmapmagbrain = $fmapmagbrain"
echo " wmseg = $wmseg"
echo " dwell = $dwell"
echo " pe_dir = $pe_dir"
echo " fdir = $fdir"
echo " use_fmap = $use_fmap"
echo " use_weighting = $use_weighting"
fi
##########################################################################################
# create the WM segmentation
if [ X$wmseg = X ] ; then
if [ `$FSLDIR/bin/imtest ${vrefbrain}_wmseg` = 0 ] ; then
echo "Running FAST segmentation"
$FSLDIR/bin/fast -o ${vout}_fast ${vrefbrain}
$FSLDIR/bin/fslmaths ${vout}_fast_pve_2 -thr 0.5 -bin ${vout}_fast_wmseg
else
for file in ${vrefbrain}_wmseg*; do
absfile=`$FSLDIR/bin/fsl_abspath $file`;
cp ${absfile} ${file/${vrefbrain}_wmseg/${vout}_fast_wmseg} #To link the correct files with extensions
done
fi
else
# copy specified wmseg file(s)
for file in `ls ${wmseg}*.nii.gz`; do
absfile=`$FSLDIR/bin/fsl_abspath $file`;
cp ${absfile} ${file/${wmseg}/${vout}_fast_wmseg} #To link the correct files with extensions
done
fi
# make a WM edge map for visualisation (good to overlay in FSLView)
if [ `$FSLDIR/bin/imtest ${vrefbrain}_wmedge` = 0 ] ; then
$FSLDIR/bin/fslmaths ${vout}_fast_wmseg -edge -bin -mas ${vout}_fast_wmseg ${vout}_fast_wmedge
else
for file in ${vrefbrain}_wmedge*; do
absfile=`$FSLDIR/bin/fsl_abspath $file`;
cp ${absfile} ${file/${vrefbrain}_wmedge/${vout}_fast_wmedge} #To link the correct files with extensions
done
fi
if [ X$initmat = X ]; then
# do a standard flirt pre-alignment
echo "FLIRT pre-alignment"
$FSLDIR/bin/flirt -ref ${vrefbrain} -in ${vepi} -dof 6 -omat ${vout}_init.mat
else
# user has supplied an intial alignment
cp $initmat ${vout}_init.mat
fi
####################
if [ $use_fmap = no ] ; then
# NO FIELDMAP
# now run the bbr
echo "Running BBR"
$FSLDIR/bin/flirt -ref ${vrefhead} -in ${vepi} -dof 6 -cost bbr -wmseg ${vout}_fast_wmseg -init ${vout}_init.mat -omat ${vout}.mat -out ${vout} -schedule ${FSLDIR}/etc/flirtsch/bbr.sch
$FSLDIR/bin/applywarp -i ${vepi} -r ${vrefhead} -o ${vout} --premat=${vout}.mat --interp=spline
####################
else
# WITH FIELDMAP
if [ $fmapreg = yes ] ; then
# register fmap to structural image
echo "Registering fieldmap to structural"
$FSLDIR/bin/flirt -in ${fmapmagbrain} -ref ${vrefbrain} -dof 6 -omat ${vout}_fieldmap2str_init.mat
$FSLDIR/bin/flirt -in ${fmapmaghead} -ref ${vrefhead} -dof 6 -init ${vout}_fieldmap2str_init.mat -omat ${vout}_fieldmap2str.mat -out ${vout}_fieldmap2str -nosearch
else
cp ${fmapmaghead}.nii.gz ${vout}_fieldmap2str.nii.gz
cp $FSLDIR/etc/flirtsch/ident.mat ${vout}_fieldmap2str.mat
fi
# unmask the fieldmap (necessary to avoid edge effects)
$FSLDIR/bin/fslmaths ${fmapmagbrain} -abs -bin ${vout}_fieldmaprads_mask
$FSLDIR/bin/fslmaths ${fmaprads} -abs -bin -mul ${vout}_fieldmaprads_mask ${vout}_fieldmaprads_mask
$FSLDIR/bin/fugue --loadfmap=${fmaprads} --mask=${vout}_fieldmaprads_mask --unmaskfmap --savefmap=${vout}_fieldmaprads_unmasked --unwarpdir=${fdir} # the direction here should take into account the initial affine (it needs to be the direction in the EPI)
# the following is a NEW HACK to fix extrapolation when fieldmap is too small
$FSLDIR/bin/applywarp -i ${vout}_fieldmaprads_unmasked -r ${vrefhead} --premat=${vout}_fieldmap2str.mat -o ${vout}_fieldmaprads2str_pad0
$FSLDIR/bin/fslmaths ${vout}_fieldmaprads2str_pad0 -abs -bin ${vout}_fieldmaprads2str_innermask
$FSLDIR/bin/fugue --loadfmap=${vout}_fieldmaprads2str_pad0 --mask=${vout}_fieldmaprads2str_innermask --unmaskfmap --unwarpdir=${fdir} --savefmap=${vout}_fieldmaprads2str_dilated
$FSLDIR/bin/fslmaths ${vout}_fieldmaprads2str_dilated ${vout}_fieldmaprads2str
# run bbr with fieldmap
echo "Running BBR with fieldmap"
if [ $use_weighting = yes ] ; then wopt="-refweight $refweight"; else wopt=""; fi
$FSLDIR/bin/flirt -ref ${vrefhead} -in ${vepi} -dof 6 -cost bbr -wmseg ${vout}_fast_wmseg -init ${vout}_init.mat -omat ${vout}.mat -out ${vout}_1vol -schedule ${FSLDIR}/etc/flirtsch/bbr.sch -echospacing ${dwell} -pedir ${pe_dir} -fieldmap ${vout}_fieldmaprads2str $wopt
# make equivalent warp fields
echo "Making warp fields and applying registration to EPI series"
$FSLDIR/bin/convert_xfm -omat ${vout}_inv.mat -inverse ${vout}.mat
$FSLDIR/bin/convert_xfm -omat ${vout}_fieldmaprads2epi.mat -concat ${vout}_inv.mat ${vout}_fieldmap2str.mat
$FSLDIR/bin/applywarp -i ${vout}_fieldmaprads_unmasked -r ${vepi} --premat=${vout}_fieldmaprads2epi.mat -o ${vout}_fieldmaprads2epi
$FSLDIR/bin/fslmaths ${vout}_fieldmaprads2epi -abs -bin ${vout}_fieldmaprads2epi_mask
$FSLDIR/bin/fugue --loadfmap=${vout}_fieldmaprads2epi --mask=${vout}_fieldmaprads2epi_mask --saveshift=${vout}_fieldmaprads2epi_shift --unmaskshift --dwell=${dwell} --unwarpdir=${fdir}
$FSLDIR/bin/convertwarp -r ${vrefhead} -s ${vout}_fieldmaprads2epi_shift --postmat=${vout}.mat -o ${vout}_warp --shiftdir=${fdir} --relout
$FSLDIR/bin/applywarp -i ${vepi} -r ${vrefhead} -o ${vout} -w ${vout}_warp --interp=spline --rel
if [ X$gdcwarp != X ]; then
${FSLDIR}/bin/convertwarp --ref=${vepi} --warp1=${vout}_warp --midmat=${vout}_inv.mat --warp2=${gdcwarp} --out=${vepi}2${vepi}_undistorted_warp --relout
${FSLDIR}/bin/applywarp -i ${vepi} -r ${vepi} -o ${vepi}_fully_undistorted -w ${vepi}2${vepi}_undistorted_warp --interp=spline --rel
${FSLDIR}/bin/flirt -ref ${vrefhead} -in ${vepi}_fully_undistorted -dof 6 -cost bbr -wmseg ${vout}_fast_wmseg -init ${vout}.mat -omat ${vout}.mat -out ${vout} -schedule ${FSLDIR}/etc/flirtsch/bbr.sch
${FSLDIR}/bin/convertwarp --ref=${vrefhead} --warp1=${vout}_warp --midmat=${vout}_inv.mat --warp2=${gdcwarp} --postmat=${vout}.mat --out=${vout}_warp --relout
${FSLDIR}/bin/applywarp -i ${vepi} -r ${vrefhead} -o ${vout} -w ${vout}_warp --interp=spline --rel
fi
fi
####################
# CLEAN UP UNNECESSARY FILES
if [ $cleanup = yes ] ; then
rm ${vout}_fast_mixeltype.nii.gz ${vout}_fast_pve*.nii.gz ${vout}_fast_seg.nii.gz
rm ${vout}_fieldmap*mask*.nii.gz ${vout}_fieldmap2str_pad0.nii.gz
fi