-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare_MPI_registration.sh
executable file
·220 lines (142 loc) · 4.59 KB
/
prepare_MPI_registration.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
#!/bin/bash
# subjects with bad brain segmentations from BET
# 10045 10047 10092 10244 10272
clear
resolution=2mm
MaximumThreads=8 # Maximum number of parallel threads to use
threads=0
Study=MPI
#mkdir /home/andek/Research_projects/fMRI_GAN/data/${Study}
startDir=/home/andek/Research_projects/fMRI_GAN
dataDir=/home/andek/Research_projects/fMRI_GAN/data/${Study}
# Get one fMRI volume from each fMRI dataset
Subject=1
threads=0
echo "Extracting single fMRI volumes "
#for i in ${dataDir}/* ; do
#echo "Processing Subject $Subject"
#if ls $i/ses-01/func/*.nii.gz 1> /dev/null 2>&1; then
# fslroi $i/ses-01/func/*.nii.gz $i/ses-01/func/fMRI.nii.gz 0 1
#else
# echo "No fMRI found for Subject $Subject"
#fi
#((threads++))
#if [ $threads -eq "$MaximumThreads" ]; then
# wait
# threads=0
#fi
#((Subject++))
#done
wait
echo "Registering fMRI to T1 "
# First mask T1 volume
Subject=1
for i in ${dataDir}/* ; do
echo "Processing Subject $Subject"
if ls $i/ses-01/anat/*inv-2_mp2rage.nii.gz 1> /dev/null 2>&1; then
echo "Found inv-2-mp2rage found for Subject $Subject"
#fslmaths $i/ses-01/anat/*inv-2_mp2rage.nii.gz -thr 100 $i/ses-01/anat/mask.nii.gz
#fslmaths $i/ses-01/anat/*acq-mp2rage_T1w.nii.gz -mul $i/ses-01/anat/mask.nii.gz $i/ses-01/anat/T1w.nii.gz
else
echo "No inv-2-mp2rage found for Subject $Subject"
fi
((Subject++))
done
# Do T1 segmentations
Subject=1
threads=0
for i in ${dataDir}/* ; do
echo "Processing Subject $Subject"
#if [ -f "$i/ses-01/anat/T1w.nii.gz" ]; then
# bet $i/ses-01/anat/T1w.nii.gz $i/ses-01/anat/T1w_brain.nii.gz -R -f 0.25 &
#fi
((threads++))
if [ $threads -eq "$MaximumThreads" ]; then
wait
threads=0
fi
((Subject++))
done
wait
Subject=1
for i in ${dataDir}/* ; do
# Go to current directory
cd $i
# Get subject name
SubjectID=${PWD##*/}
echo "Processing" $SubjectID
# Go back to original directory
cd ${startDir}
#if [ -f "$i/ses-01/anat/T1w.nii.gz" ]; then
# cp $i/ses-01/anat/T1w.nii.gz ${dataDir}/brains/T1w_${SubjectID}.nii.gz
#
# cp $i/ses-01/anat/T1w_brain.nii.gz ${dataDir}/brains/T1w_brain_${SubjectID}.nii.gz
#fi
((Subject++))
done
# Now do EPI T1 registrations
Subject=1
threads=0
for i in ${dataDir}/* ; do
echo "Processing Subject $Subject"
if [ -f "$i/ses-01/func/fMRI.nii.gz" ]; then
epi_reg --epi=$i/ses-01/func/fMRI.nii.gz --t1=$i/ses-01/anat/T1w.nii.gz --t1brain=$i/ses-01/anat/T1w_brain.nii.gz --out=$i/ses-01/func/epi_to_T1 &
else
echo "No fMRI found for Subject $Subject"
fi
((threads++))
if [ $threads -eq "$MaximumThreads" ]; then
wait
threads=0
fi
((Subject++))
done
wait
# Now do T1 MNI registrations
Subject=1
threads=0
for i in ${dataDir}/* ; do
echo "Processing Subject $Subject"
if [ -f "$i/ses-01/anat/T1w.nii.gz" ]; then
flirt -interp sinc -in $i/ses-01/anat/T1w_brain.nii.gz -ref MNI_${resolution}_crop.nii.gz -out $i/ses-01/anat/T1_MNI_${resolution}.nii.gz -omat $i/ses-01/anat/T1_to_MNI_${resolution}.mat &
fi
((threads++))
if [ $threads -eq "$MaximumThreads" ]; then
wait
threads=0
fi
((Subject++))
done
wait
# Now combine transformations to MNI
Subject=1
threads=0
for i in ${dataDir}/* ; do
echo "Processing Subject $Subject"
if [ -f "$i/ses-01/func/fMRI.nii.gz" ]; then
# A = fMRI, B = T1, C = MNI
convert_xfm -omat $i/ses-01/func/epi_to_MNI_${resolution}.mat -concat $i/ses-01/anat/T1_to_MNI_${resolution}.mat $i/ses-01/func/epi_to_T1.mat
flirt -interp sinc -in $i/ses-01/func/fMRI.nii.gz -ref MNI_${resolution}_crop.nii.gz -applyxfm -init $i/ses-01/func/epi_to_MNI_${resolution}.mat -out $i/ses-01/func/fMRI_MNI_${resolution}.nii.gz &
fi
((threads++))
if [ $threads -eq "$MaximumThreads" ]; then
wait
threads=0
fi
((Subject++))
done
wait
echo "Copying data "
Subject=1
for i in ${dataDir}/* ; do
if [ -f "$i/ses-01/func/fMRI.nii.gz" ]; then
cp $i/ses-01/func/fMRI_MNI_${resolution}.nii.gz ${dataDir}/MNI_${resolution}/${Study}_fMRI_${Subject}.nii.gz
fi
if [ -f "$i/ses-01/anat/T1w.nii.gz" ]; then
cp $i/ses-01/anat/T1_MNI_${resolution}.nii.gz ${dataDir}/MNI_${resolution}/${Study}_T1_${Subject}.nii.gz
fi
((Subject++))
done
# How to create cropped MNI volumes
# fslroi MNI152_T1_2mm_brain.nii.gz MNI_2mm_crop.nii.gz 8 76 12 88 18 60
# fslroi MNI152_T1_1mm_brain.nii.gz MNI_1mm_crop.nii.gz 15 152 21 180 36 120