-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline2.sh
266 lines (222 loc) · 8.92 KB
/
pipeline2.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
#! /bin/tcsh -fex
# Setup
set OD = JJ
mkdir $OD
# Source NIFTI files
set WATER_NIFTI = waterimage_noir_hires_lowte_adiabfast_1.nii.gz
set T1_ECHO_NIFTI = (t1_memprage_nib_?.nii.gz)
set FLAIR_NIFTI = flair_sag_vfl_1.nii.gz
set CALC_RAGE_NIFTI = Calc_MPRAGE.nii.gz
set MTC_NIFTI = mtc_on_{1.2,4.0,98.0}khz_500deg_10us_1.nii.gz
#set PD_NIFTI = pd_t2_ax_tse_1.nii.gz
#set T2_NIFTI = pd_t2_ax_tse_2.nii.gz
# Destination AFNI files
set WATER = wt
set T1_ECHO = t1_
set FLAIR = fl
set CALC_RAGE = t1_avg
set MTC = mtc_
#set PD = pd
#set T2 = t2
# Various suffixes for resulting files
set THRESH = _th
set ALIGN = _alw
set MASK = _mask
set ZERO = _z
set LOC_STAT = _ls
set SCALE = _s
set RESAMPLE = _rs
set STRIP = _ss
set SMOOTH = _sm
set SEG = _seg
set EDGE = _edge
# Specific measure variables
set DILATE = 5
set RAD = 45
set WT_TH = 78
# A) Copy everything from NIFTI to AFNI format
3dcopy $WATER_NIFTI $OD/$WATER
3dcopy $FLAIR_NIFTI $OD/$FLAIR
3dcopy $CALC_RAGE_NIFTI $OD/$CALC_RAGE
#3dcopy $PD_NIFTI $OD/$PD
#3dcopy $T2_NIFTI $OD/$T2
set i = 1
while ($i <= $#T1_ECHO_NIFTI)
3dcopy $T1_ECHO_NIFTI[$i] ${OD}/${T1_ECHO}${i}
@ i ++
end
set i = 1
while ($i <= $#MTC_NIFTI)
3dcopy $MTC_NIFTI[$i] ${OD}/${MTC}${i}
@ i ++
end
# Change output directory for calculations
cd ${OD}
# B) Threshold the water image. This number is context-dependent
3dcalc -a ${WATER}+orig -expr "step(a-${WT_TH})*a" -prefix ${WATER}${THRESH}
# 3dcalc -a all_us+orig'[5]' -expr "step(a - ${WT_TH})" -prefix ${WATER}${THRESH}
# C) Align Calc_MPRAGE to thresholded water image, then do the same
# transformation to T1 echo images, using same "box frame" as Calc_MPRAGE
align_epi_anat.py -dset1 ${CALC_RAGE}+orig -dset2 ${WATER}${THRESH}+orig \
-dset1_strip None -dset2_strip None -suffix $ALIGN \
-master_dset1 ${CALC_RAGE}+orig -child_anat ${T1_ECHO}?+orig*
# Cont'd) Align MTC images to thresholded water image
set i = 1
while ($i <= $#MTC_NIFTI)
align_epi_anat.py -dset1 ${MTC}${i}+orig -dset2 ${WATER}${THRESH}+orig \
-dset1_strip None -dset2_strip None -suffix ${ALIGN} \
-master_dset1 ${CALC_RAGE}+orig
@ i ++
end
# Had to align one of them separately to a different MTC that did align
# set i = 1
# align_epi_anat.py -dset1 ${MTC}${i}+orig -dset2 ${MTC}2_alw+orig \
# -dset1_strip None -dset2_strip None -suffix ${ALIGN}3 \
# -master_dset1 ${CALC_RAGE}+orig
# Cont'd) Align FLAIR to thresholded water image
align_epi_anat.py -dset1 ${FLAIR}+orig -dset2 ${WATER}${THRESH}+orig \
-dset1_strip None -dset2_strip None -suffix ${ALIGN} \
-master_dset1 ${CALC_RAGE}+orig
# 3dallineate -base ${CALC_RAGE}${ALIGN}+orig -master ${CALC_RAGE}${ALIGN}+orig -input ${PD}+orig -cost ls -prefix ${PD}${ALIGN} -1Dmatrix_save ${PD}${ALIGN}
# 3dallineate -base ${CALC_RAGE}${ALIGN}+orig -master ${CALC_RAGE}${ALIGN}+orig -input ${T2}+orig -cost ls -prefix ${T2}${ALIGN}
# TEST TO SEE IF ALIGNMENT WENT CORRECTLY
3dresample -input ${WATER}+orig -master ${CALC_RAGE}${ALIGN}+orig \
-rmode Li -prefix ${WATER}${RESAMPLE}_tmp
set cl = (${FLAIR}${ALIGN}+orig.HEAD \
${T1_ECHO}?${ALIGN}+orig.HEAD \
${WATER}${RESAMPLE}_tmp+orig.HEAD \
${MTC}?${ALIGN}+orig.HEAD)
3dTcat -relabel -prefix all_us $cl
# D) Create a head mask (then manually draw to fill in gaps):
3dAutomask -prefix ${CALC_RAGE}${ALIGN}${MASK} ${CALC_RAGE}${ALIGN}+orig
# E) Dilate mask to make sure everything is covered (in the head):
3dmask_tool -input ${CALC_RAGE}${ALIGN}${MASK}+orig \
-prefix d${DILATE}${ALIGN}${MASK} -dilate_input $DILATE
# F) Zero out everything away from head in all images
foreach IMAGE (${T1_ECHO}?${ALIGN}+orig.HEAD ${FLAIR}${ALIGN}+orig.HEAD \
${CALC_RAGE}${ALIGN}+orig.HEAD ${MTC}?${ALIGN}+orig.HEAD)
set p = `ParseName -out Prefix $IMAGE`
3dcalc -a $IMAGE -b d${DILATE}${ALIGN}${MASK}+orig \
-expr 'step(b)*a' -prefix ${p}${ZERO}
end
# G) Calculate local stats (percentiles) on all the images except water
foreach IMAGE (${T1_ECHO}?${ALIGN}+orig.HEAD ${MTC}?${ALIGN}+orig.HEAD \
${FLAIR}${ALIGN}+orig.HEAD ${CALC_RAGE}${ALIGN}+orig.HEAD)
set p = `ParseName -out Prefix $IMAGE`
3dLocalstat -nbhd "SPHERE($RAD)" -stat perc:65:95:5 \
-datum short -reduce_max_vox 5 \
-prefix ${p}${LOC_STAT} ${p}${ZERO}+orig
end
# Cont'd) ...and the same for PD and T2
foreach IMAGE (${PD}${ALIGN}+orig.HEAD ${T2}${ALIGN}+orig.HEAD)
set p = `ParseName -out Prefix $IMAGE`
3dLocalstat -nbhd "SPHERE($RAD)" -stat perc:90 \
-datum short -reduce_max_vox 5 \
-prefix ${p}${LOC_STAT} ${p}+orig
end
# Cont'd) ...and the same for water, since it doesn't have a "_z"
3dLocalstat -nbhd "SPHERE($RAD)" -stat perc:65:95:5 \
-datum short -reduce_max_vox 5 \
-prefix ${WATER}${LOC_STAT} ${WATER}+orig.HEAD
# H) Scale T1 echoes by Calc_MPRAGE percentiles
foreach IMAGE (${T1_ECHO}?+orig.HEAD)
set p = `ParseName -out Prefix $IMAGE`
3dcalc -a ${CALC_RAGE}${ALIGN}${LOC_STAT}+orig'[perc:90.00]' \
-b ${p}${ALIGN}${ZERO}+orig \
-expr 'step(a)*b/a' -prefix ${p}${SCALE}
end
# ALTERNATIVE: Scale by T1 echoes' own percentiles
#foreach vv (${T1_ECHO}?+orig.HEAD)
# set p = `ParseName -out Prefix $vv`
# 3dcalc -a ${p}${ALIGN}+orig -b ${p}${ALIGN}${LOC_STAT}+orig'[perc:90.00]' \
# -expr 'step(b)*a/b' \
# -prefix ${p}${SCALE}
#end
# Cont'd) Scale MTC images by themselves
foreach IMAGE (${MTC}?+orig.HEAD)
set p = `ParseName -out Prefix $IMAGE`
3dcalc -a ${p}${ALIGN}${LOC_STAT}+orig'[perc:90.00]' \
-b ${p}${ALIGN}${ZERO}+orig \
-expr 'step(a)*b/a' -prefix ${p}${SCALE}
end
# Cont'd) Scale Calc_MPRAGE by CALC_MPRAGE percentiles
3dcalc -a ${CALC_RAGE}${ALIGN}${LOC_STAT}+orig'[perc:90.00]' \
-b ${CALC_RAGE}${ALIGN}${ZERO}+orig \
-expr 'step(a)*b/a' -prefix ${CALC_RAGE}${SCALE}
# Cont'd) ...and the same for FLAIR, scale by iteself
3dcalc -a ${FLAIR}${ALIGN}${LOC_STAT}+orig'[perc:90.00]' \
-b ${FLAIR}${ALIGN}${ZERO}+orig \
-expr 'step(a)*b/a' -prefix ${FLAIR}${SCALE}
# Cont'd) ...and the same for the water image, scale by itself
3dcalc -a ${WATER}${LOC_STAT}+orig'[perc:70.00]' -b ${WATER}+orig \
-expr 'step(a)*b/a' \
-prefix ${WATER}${SCALE}
# I) Resample the scaled water image so it is the same size as the rest and
# mask out the rest
3dresample -input ${WATER}${SCALE}+orig -master ${CALC_RAGE}${ALIGN}+orig \
-rmode Li -prefix ${WATER}${RESAMPLE}${SCALE}_tmp
3dcalc -a ${WATER}${RESAMPLE}${SCALE}_tmp+orig \
-b d${DILATE}${ALIGN}${MASK}+orig \
-expr 'step(b)*a' -prefix ${WATER}${RESAMPLE}${SCALE}
# Cont'd) Resample the unscaled water image and mask it
3dresample -input ${WATER}+orig -master ${CALC_RAGE}${ALIGN}+orig \
-rmode Li -prefix ${WATER}${RESAMPLE}_tmp
3dcalc -a ${WATER}${RESAMPLE}_tmp+orig \
-b d${DILATE}${ALIGN}${MASK}+orig \
-expr 'step(b)*a' -prefix ${WATER}${RESAMPLE}${ZERO}
# J) Join them all together, first the scaled, then the unscaled
set cl_s = (${FLAIR}${SCALE}+orig.HEAD ${T1_ECHO}?${SCALE}+orig.HEAD \
${WATER}${RESAMPLE}${SCALE}+orig.HEAD ${MTC}?${SCALE}+orig.HEAD)
3dTcat -relabel -prefix all_s $cl_s
if ( -f feats.txt ) rm -f feats.txt
foreach l ($cl_s)
echo $l | sed "s/${SCALE}+orig.HEAD//g" >> feats.txt
end
3drefit -relabel_all feats.txt all_s+orig.HEAD
# Cont'd) ...unscaled
set cl_us = (${FLAIR}${ALIGN}+orig.HEAD \
${T1_ECHO}?${ALIGN}+orig.HEAD \
${WATER}${RESAMPLE}_tmp+orig.HEAD \
${MTC}?${ALIGN}+orig.HEAD)
3dTcat -relabel -prefix all_us $cl_us
3drefit -relabel_all feats.txt all_us+orig.HEAD
# GET MORE STATS
set nbhds = {2,4,6}
foreach i ( $nbhds )
3dLocalstat -nbhd "SPHERE($i)" -stat stdev -prefix ls_${i}_stdev all_s+orig
3dLocalstat -nbhd "SPHERE($i)" -stat MAD -prefix ls_${i}_mad all_s+orig
3dLocalstat -nbhd "SPHERE($i)" -stat P2skew -prefix ls_${i}_skew all_s+orig
end
set stat_type = {stdev,mad,skew}
set subbrick = {0,1,2,3,4,5,6,7,8}
foreach i ( $nbhds )
foreach j ( $stat_type )
foreach k ( $subbrick )
echo "${j}_${i}[${k}]" >> ${j}${i}.txt
end
3drefit -relabel_all ${j}${i}.txt ls_${i}_${j}+orig
end
end
set cl_ls = (ls_?_*HEAD)
3dTcat -prefix all_ls $cl_ls
echo Done
cd ../../a20130213/JJ
set nbhds = {2,4,6}
foreach i ( $nbhds )
3dLocalstat -nbhd "SPHERE($i)" -stat stdev -prefix ls_${i}_stdev all_s+orig
3dLocalstat -nbhd "SPHERE($i)" -stat MAD -prefix ls_${i}_mad all_s+orig
3dLocalstat -nbhd "SPHERE($i)" -stat P2skew -prefix ls_${i}_skew all_s+orig
end
set stat_type = {stdev,mad,skew}
set subbrick = {0,1,2,3,4,5,6,7,8}
foreach i ( $nbhds )
foreach j ( $stat_type )
foreach k ( $subbrick )
echo "${j}_${i}[${k}]" >> ${j}${i}.txt
end
3drefit -relabel_all ${j}${i}.txt ls_${i}_${j}+orig
end
end
set cl_ls = (ls_?_*HEAD)
3dTcat -prefix all_ls $cl_ls
echo Done