-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·197 lines (131 loc) · 6.29 KB
/
run
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
#! /bin/bash
# Long Qian, Peking University
# Email:[email protected]
# Run functional-preprocessing Gear
# Built to flywheel-v0 spec.
#
CONTAINER="[mricenter/functional-preprocessing]"
echo -e "$CONTAINER Initiated"
set -e
# Configure the ENV
export FSLOUTPUTTYPE=NIFTI_GZ
# Configure paths
FLYWHEEL_BASE=/flywheel/v0
OUTPUT_DIR=$FLYWHEEL_BASE/output
INPUT_DIR=$FLYWHEEL_BASE/input
WORK_DIR=$INPUT_DIR/work
CONFIG_FILE=$FLYWHEEL_BASE/manifest.json
TR=$(jq <$CONFIG_FILE -r '.config.TR')
MIDSLICE=$(jq <$CONFIG_FILE -r '.config.MIDSLICE')
ACQ=$(jq <$CONFIG_FILE -r '.config.ACQ')
# Ensure there is an output directory
if [[ ! -d "$OUTPUT_DIR" ]]
then
echo "$CONTAINER $OUTPUT_DIR not found!"
exit 1
fi
# Make the directories
mkdir $WORK_DIR
####################You shold change this path############
TEMPLATE_1mm=/tmp/ch2_1mm.nii
TEMPLATE_2mm=/tmp/T1_bet_2mm.nii
TEMPLATE_3mm=/tmp/T1_3mm.nii
#################################
t1_file=t1_anat.nii.gz
# reorient T1
3dcopy $INPUT_DIR/structural/* $OUTPUT_DIR/rm_${t1_file}
3drefit -deoblique $OUTPUT_DIR/rm_${t1_file}
3dresample -orient RPI -prefix $OUTPUT_DIR/${t1_file} -inset $OUTPUT_DIR/rm_${t1_file}
# bet skull
ss_t1_file=ss${t1_file}
bet $OUTPUT_DIR/${t1_file} $OUTPUT_DIR/ss${t1_file} -F -f 0.25 -g 0
# use flirt to normalize T1 to template
warp_file=t1_template.nii.gz
flirt -in $OUTPUT_DIR/${ss_t1_file} -ref ${TEMPLATE_2mm} -omat $OUTPUT_DIR/rm_affine_transf.mat
fnirt --in=$OUTPUT_DIR/${t1_file} --aff=$OUTPUT_DIR/rm_affine_transf.mat \
--cout=$OUTPUT_DIR/t1_template --config=T1_2_MNI152_2mm.cnf
norm_t1_file=w${t1_file}
applywarp --ref=${TEMPLATE_1mm} --in=$OUTPUT_DIR/${t1_file} \
--warp=$OUTPUT_DIR/t1_template --out=$OUTPUT_DIR/${norm_t1_file}
norm_ss_t1_file=w${ss_t1_file}
applywarp --ref=${TEMPLATE_1mm} --in=$OUTPUT_DIR/${ss_t1_file} \
--warp=$OUTPUT_DIR/t1_template --out=$OUTPUT_DIR/${norm_ss_t1_file}
## create csf and wm masks in template space
csf_mask=w${ss_t1_file%%.nii.gz}_csf.nii.gz
wm_mask=w${ss_t1_file%%.nii.gz}_wm.nii.gz
gm_mask=sw${ss_t1_file%%.nii.gz}_gm.nii.gz
in_csf_file=rm_${ss_t1_file%%.nii.gz}_pve_0.nii.gz
in_wm_file=rm_${ss_t1_file%%.nii.gz}_pve_2.nii.gz
in_gm_file=rm_${ss_t1_file%%.nii.gz}_pve_1.nii.gz
fast --channels=1 --type=1 --class=3 --out=$OUTPUT_DIR/rm_${ss_t1_file} $OUTPUT_DIR/${ss_t1_file}
# create csf mask in template space
applywarp --ref=${TEMPLATE_1mm} --in=$OUTPUT_DIR/${in_csf_file} \
--warp=$OUTPUT_DIR/t1_template --out=$OUTPUT_DIR/rm_w${in_csf_file##rm_}
3dcalc -a $OUTPUT_DIR/rm_w${in_csf_file##rm_} -expr 'step(a-.99)' -prefix $OUTPUT_DIR/${csf_mask} -datum short
# create white matter mask in template space
applywarp --ref=${TEMPLATE_1mm} --in=$OUTPUT_DIR/${in_wm_file} \
--warp=$OUTPUT_DIR/t1_template --out=$OUTPUT_DIR/rm_w${in_wm_file##rm_}
3dcalc -a $OUTPUT_DIR/rm_w${in_wm_file##rm_} -expr 'step(a-.99)' -prefix $OUTPUT_DIR/${wm_mask} -datum short
# gray matter mask in norm space
applywarp --ref=${TEMPLATE_1mm} --in=$OUTPUT_DIR/${in_gm_file} \
--warp=$OUTPUT_DIR/t1_template --out=$OUTPUT_DIR/${gm_mask##s}
3dmerge -1blur_fwhm 6 -doall -prefix $OUTPUT_DIR/${gm_mask} $OUTPUT_DIR/${gm_mask##s}
#----- BASIC resting-state fMRI preprocessing
subname_rest=rest_fmri
bold_file=rest_fmri.nii.gz
orig_bold=rm_nmrda${bold_file}
mni_preproc_bold_data=snwmrda${bold_file}
norm_filtered_denoised_bold=sfnwmrda${bold_file}
epi_mean_template=mean_mrda${bold_file}
mni_mask_file=mask_w${epi_mean_template}
# coregister all EPI data to the first image of REST1
coreg_base=rm_da${bold_file}'[0]'
# remove first 10 images
#--------------------------------------------#####
FUN_DIR=$INPUT_DIR/functional
sub_rest='find ${FUN_DIR}/* -name "*.nii*"'
3dcalc -prefix $OUTPUT_DIR/rm_${bold_file} -a ${sub_rest}'[1..$]' -expr 'a'
prev_step=$OUTPUT_DIR/rm_${bold_file}
# time shift dataset
3dTshift -TR ${TR}s -slice ${MIDSLICE} -tpattern ${ACQ} -prefix $OUTPUT_DIR/rm_a${prev_step##rm_} ${prev_step}
prev_step=$OUTPUT_DIR/rm_a${prev_step##rm_}
# deoblique dataset, and convert to RPI
3drefit -deoblique ${prev_step}
3dresample -orient RPI -prefix $OUTPUT_DIR/rm_d${prev_step##rm_} -inset ${prev_step}
prev_step=$OUTPUT_DIR/rm_d${prev_step##rm_}
# motion correct data
motion_file=rp_${bold_file%%.nii.gz}.1D
3dvolreg -Fourier -prefix $OUTPUT_DIR/rm_r${prev_step##rm_} -base $OUTPUT_DIR/${coreg_base} \
-1Dfile $OUTPUT_DIR/rp_${bold_file%%.nii.gz}.1D ${prev_step}
prev_step1=rm_r${prev_step##rm_}
prev_step=$OUTPUT_DIR/rm_r${prev_step##rm_}
# create a mask for the dataset
mask_file=mask_${prev_step1##rm_}
3dAutomask -prefix $OUTPUT_DIR/mask_${prev_step##rm_} ${prev_step}
# mask the dataset
3dcalc -a ${prev_step} -b $OUTPUT_DIR/${mask_file} -expr 'ispositive(b)*a' -prefix $OUTPUT_DIR/rm_m${prev_step##rm_}
prev_step=$OUTPUT_DIR/rm_m${prev_step##rm_}
# create an average of this file to use for coregistering to T1 at later stage
3dTstat -prefix $OUTPUT_DIR/${epi_mean_template} ${prev_step}
#----- Transfrom BOLD data to template space
epi_mni_xform=${subname_rest}_epi_2_template_3mm
# calculate the EPI-template transform
# register coreg_base to anatomical
flirt -ref $OUTPUT_DIR/${ss_t1_file} -in $OUTPUT_DIR/${epi_mean_template} -dof 7 -omat $OUTPUT_DIR/rm_${subname_rest}_epi_2_T1.mat
# copy mean template into T1 space for debugging
flirt -in $OUTPUT_DIR/${epi_mean_template} -ref $OUTPUT_DIR/${ss_t1_file} -out $OUTPUT_DIR/t1_${epi_mean_template} \
-init $OUTPUT_DIR/rm_${subname_rest}_epi_2_T1.mat -applyxfm
# combine xforms
convertwarp --ref=${TEMPLATE_3mm} --warp1=$OUTPUT_DIR/${warp_file} \
--premat=$OUTPUT_DIR/rm_${subname_rest}_epi_2_T1.mat --out=$OUTPUT_DIR/${epi_mni_xform} --relout
# copy mean image into template space for debugging
applywarp --ref=${TEMPLATE_3mm} \
--in=$OUTPUT_DIR/${epi_mean_template} --warp=$OUTPUT_DIR/${epi_mni_xform} --rel \
--out=$OUTPUT_DIR/w${epi_mean_template}
# create mask in template space
3dAutomask -prefix $OUTPUT_DIR/${mni_mask_file} $OUTPUT_DIR/w${epi_mean_template}
# copy data into template space
applywarp --ref=${TEMPLATE_3mm} \
--in=${prev_step} --warp=$OUTPUT_DIR/${epi_mni_xform} --rel \
--out=$OUTPUT_DIR/rm_w${prev_step##rm_}
#prev_step=$OUTPUT_DIR/rm_w${prev_step##rm_}