-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreesurfer.py
More file actions
369 lines (290 loc) · 15.6 KB
/
freesurfer.py
File metadata and controls
369 lines (290 loc) · 15.6 KB
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
import os
import subprocess
import json
import argparse
import shutil
class Multiscaleatlas:
def __init__(self, t1, subjectID, output_dir=None):
self.t1 = t1
self.subjectID = subjectID
self.output_dir = output_dir
self.setup_environment() # Call setup_environment during initialization
def setup_environment(self):
with open('setup_environment.json', 'r') as f:
env_vars = json.load(f)
for key, value in env_vars.items():
os.environ[key] = value
FREESURFER_HOME = os.environ['FREESURFER_HOME']
freesurferdirmpath = f'{FREESURFER_HOME}/SetUpFreeSurfer.sh'
# Execute the setup script
subprocess.run(['sh', freesurferdirmpath], check=True)
def recon_all(self):
'''
Run recon-all command to generate the freesurfer reconstructions
'''
cmd = ['recon-all',
'-s', self.subjectID,
'-i', self.t1,
'-threads', '8', '-all']
subprocess.run(cmd, check=True)
def lausanne2018scale1(self):
'''Run Lausanne 2018 Scale 1 parcellation'''
sub_dir = os.environ.get('SUBJECTS_DIR')
# Surface-to-Surface Mapping for Left Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'lh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/lh.lausanne2018.scale1.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/lh.lausanne2018.scale1.annot')]
subprocess.run(cmd, check=True)
# Surface-to-Surface Mapping for Right Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'rh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/rh.lausanne2018.scale1.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/rh.lausanne2018.scale1.annot')]
subprocess.run(cmd, check=True)
# Anatomical Statistics for Left Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'lh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'lh.lausanne2018.scale1.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'lh.lausanne2018.scale1.annot'),
self.subjectID, 'lh']
subprocess.run(cmd, check=True)
# Anatomical Statistics for Right Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'rh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'rh.lausanne2018.scale1.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'rh.lausanne2018.scale1.annot'),
self.subjectID, 'rh']
subprocess.run(cmd, check=True)
# Convert Aparc to Aseg
cmd = ['mri_aparc2aseg',
'--old-ribbon',
'--s', self.subjectID,
'--annot', 'lausanne2018.scale1',
'--threads', '8',
'--o', os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale1.mgz')]
subprocess.run(cmd, check=True)
# Convert MGZ to NII format
cmd = ['mri_convert',
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale1.mgz'),
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale1.nii.gz')]
subprocess.run(cmd, check=True)
def lausanne2018scale2(self):
'''Run Lausanne 2018 Scale 2 parcellation'''
sub_dir = os.environ.get('SUBJECTS_DIR')
# Surface-to-Surface Mapping for Left Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'lh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/lh.lausanne2018.scale2.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/lh.lausanne2018.scale2.annot')]
subprocess.run(cmd, check=True)
# Surface-to-Surface Mapping for Right Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'rh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/rh.lausanne2018.scale2.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/rh.lausanne2018.scale2.annot')]
subprocess.run(cmd, check=True)
# Anatomical Statistics for Left Hemisphere
cmd = ['mris_anatomical_stats', '-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'lh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'lh.lausanne2018.scale2.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'lh.lausanne2018.scale2.annot'),
self.subjectID, 'lh']
subprocess.run(cmd, check=True)
# Anatomical Statistics for Right Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'rh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'rh.lausanne2018.scale2.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'rh.lausanne2018.scale2.annot'),
self.subjectID, 'rh']
subprocess.run(cmd, check=True)
# Convert Aparc to Aseg
cmd = ['mri_aparc2aseg',
'--old-ribbon',
'--s', self.subjectID,
'--annot', 'lausanne2018.scale2',
'--threads', '8',
'--o', os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale2.mgz')]
subprocess.run(cmd, check=True)
# Convert MGZ to NII format
cmd = ['mri_convert',
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale2.mgz'),
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale2.nii.gz')]
subprocess.run(cmd, check=True)
def lausanne2018scale3(self):
'''Run Lausanne 2018 Scale 3 parcellation'''
sub_dir = os.environ.get('SUBJECTS_DIR')
# Surface-to-Surface Mapping for Left Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'lh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/lh.lausanne2018.scale3.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/lh.lausanne2018.scale3.annot')]
subprocess.run(cmd, check=True)
# Surface-to-Surface Mapping for Right Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'rh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/rh.lausanne2018.scale3.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/rh.lausanne2018.scale3.annot')]
subprocess.run(cmd, check=True)
# Anatomical Statistics for Left Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'lh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'lh.lausanne2018.scale3.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'lh.lausanne2018.scale3.annot'),
self.subjectID, 'lh']
subprocess.run(cmd, check=True)
# Anatomical Statistics for Right Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'rh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'rh.lausanne2018.scale3.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'rh.lausanne2018.scale3.annot'),
self.subjectID, 'rh']
subprocess.run(cmd, check=True)
# Convert Aparc to Aseg
cmd = ['mri_aparc2aseg',
'--old-ribbon',
'--s', self.subjectID,
'--annot', 'lausanne2018.scale3',
'--threads', '8',
'--o', os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale3.mgz')]
subprocess.run(cmd, check=True)
# Convert MGZ to NII format
cmd = ['mri_convert',
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale3.mgz'),
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale3.nii.gz')]
subprocess.run(cmd, check=True)
def lausanne2018scale4(self):
'''Run Lausanne 2018 Scale 4 parcellation'''
sub_dir = os.environ.get('SUBJECTS_DIR')
# Surface-to-Surface Mapping for Left Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'lh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/lh.lausanne2018.scale4.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/lh.lausanne2018.scale4.annot')]
subprocess.run(cmd, check=True)
# Surface-to-Surface Mapping for Right Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'rh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/rh.lausanne2018.scale4.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/rh.lausanne2018.scale4.annot')]
subprocess.run(cmd, check=True)
# Anatomical Statistics for Left Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'lh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'lh.lausanne2018.scale4.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'lh.lausanne2018.scale4.annot'),
self.subjectID, 'lh']
subprocess.run(cmd, check=True)
# Anatomical Statistics for Right Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'rh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'rh.lausanne2018.scale4.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'rh.lausanne2018.scale4.annot'),
self.subjectID, 'rh']
subprocess.run(cmd, check=True)
# Convert Aparc to Aseg
cmd = ['mri_aparc2aseg',
'--old-ribbon',
'--s', self.subjectID,
'--annot', 'lausanne2018.scale4',
'--threads', '8',
'--o', os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale4.mgz')]
subprocess.run(cmd, check=True)
# Convert MGZ to NII format
cmd = ['mri_convert',
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale4.mgz'),
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale4.nii.gz')]
subprocess.run(cmd, check=True)
def lausanne2018scale5(self):
'''Run Lausanne 2018 Scale 5 parcellation'''
sub_dir = os.environ.get('SUBJECTS_DIR')
# Surface-to-Surface Mapping for Left Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'lh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/lh.lausanne2018.scale5.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/lh.lausanne2018.scale5.annot')]
subprocess.run(cmd, check=True)
# Surface-to-Surface Mapping for Right Hemisphere
cmd = ['mri_surf2surf',
'--srcsubject', 'fsaverage',
'--trgsubject', self.subjectID,
'--hemi', 'rh',
'--sval-annot', os.path.join(sub_dir, 'fsaverage/label/rh.lausanne2018.scale5.annot'),
'--tval', os.path.join(sub_dir, self.subjectID, 'label/rh.lausanne2018.scale5.annot')]
subprocess.run(cmd, check=True)
# Anatomical Statistics for Left Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'lh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'lh.lausanne2018.scale5.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'lh.lausanne2018.scale5.annot'),
self.subjectID, 'lh']
subprocess.run(cmd, check=True)
# Anatomical Statistics for Right Hemisphere
cmd = ['mris_anatomical_stats',
'-th3', '-mgz', '-noglobal',
'-cortex', os.path.join(sub_dir, self.subjectID, 'label', 'rh.cortex.label'),
'-f', os.path.join(sub_dir, self.subjectID, 'stats', 'rh.lausanne2018.scale5.stats'),
'-b', '-a', os.path.join(sub_dir, self.subjectID, 'label', 'rh.lausanne2018.scale5.annot'),
self.subjectID, 'rh']
subprocess.run(cmd, check=True)
# Convert Aparc to Aseg
cmd = ['mri_aparc2aseg',
'--old-ribbon',
'--s', self.subjectID,
'--annot', 'lausanne2018.scale5',
'--threads', '8',
'--o', os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale5.mgz')]
subprocess.run(cmd, check=True)
# Convert MGZ to NII format
cmd = ['mri_convert',
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale5.mgz'),
os.path.join(sub_dir, self.subjectID, 'mri', 'lausanne2018.scale5.nii.gz')]
subprocess.run(cmd, check=True)
def move_output(self):
'''move the output directory to the desired location'''
if self.output_dir:
source_dir = os.path.join(os.environ.get('SUBJECTS_DIR'), self.subjectID)
output_dir = self.output_dir
shutil.move(source_dir, output_dir)
def main():
parser = argparse.ArgumentParser(description='Run Freesurfer recon-all.')
parser.add_argument('-t1', type=str, required=True, help='Path to the T1 image (required)', metavar='')
parser.add_argument('-s', type=str, required=True, help='Subject ID (required)',metavar='')
parser.add_argument('-o', type=str, help='Output directory (optional)', metavar='')
args = parser.parse_args()
recon = Multiscaleatlas(args.t1, args.s, args.o)
recon.recon_all()
recon.lausanne2018scale1()
recon.lausanne2018scale2()
recon.lausanne2018scale3()
recon.lausanne2018scale4()
recon.lausanne2018scale5()
recon.move_output()
if __name__ == "__main__":
main()