-
Notifications
You must be signed in to change notification settings - Fork 0
/
T1_parcellation_catalogue_v1.py
270 lines (240 loc) · 11.1 KB
/
T1_parcellation_catalogue_v1.py
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
from pydra import Workflow, mark, ShellCommandTask
import os
from pydra.engine.specs import SpecInfo, BaseSpec, ShellSpec, ShellOutSpec
from pydra.tasks.mrtrix3.v3_0 import labelconvert, labelsgmfix
# Define some filepaths
freesurfer_home='/Applications/freesurfer/'
mrtrix_lut_dir='/Users/arkievdsouza/git/mrtrix3/share/mrtrix3/labelconvert/'
output_path = '/Users/arkievdsouza/git/t1-pipeline/working-dir/catalogue_testing_v1/'
os.environ["SUBJECTS_DIR"] = ''
# Define the input values using input_spec
input_spec = {"FS_dir": str, "parcellation": str}
wf = Workflow(name='parcellation_catalogue', input_spec=input_spec, cache_dir=output_path)
@mark.task
@mark.annotate({
"parcellation": str,
"FS_dir": str,
"freesurfer_home": str,
"return": {
"fsavg_dir": str,
"parc_lut_file": str,
"mrtrix_lut_file": str,
"output_parcellation_filename": str,
"lh_annotation": str,
"rh_annotation": str,
"source_annotation_file_lh": str,
"source_annotation_file_rh": str,
"node_image": str,
"output_parcellation_filename": str,
"normimg_path": str,
"final_parc_image": str,
}
})
def join_task_catalogue(parcellation: str, FS_dir: str, freesurfer_home: str):
node_image=(parcellation + "_nodes.mif")
final_parc_image=('parcellation_image_' + parcellation + '.mif' )
normimg_path=os.path.join(FS_dir, 'mri', "norm.mgz")
if parcellation == 'desikan':
# DESIKAN definitions
fsavg_dir=''
parc_lut_file = os.path.join(freesurfer_home,'FreeSurferColorLUT.txt')
mrtrix_lut_file = os.path.join(mrtrix_lut_dir,'fs_default.txt')
output_parcellation_filename=os.path.join(FS_dir,"mri","aparc+aseg.mgz")
lh_annotation= ''
rh_annotation= ''
source_annotation_file_lh=''
source_annotation_file_rh=''
return fsavg_dir, parc_lut_file, mrtrix_lut_file, output_parcellation_filename, lh_annotation, rh_annotation, source_annotation_file_lh, source_annotation_file_rh, node_image,normimg_path,final_parc_image
elif parcellation == 'destrieux':
# DESTRIEUX definitions
fsavg_dir=''
parc_lut_file = os.path.join(freesurfer_home,'FreeSurferColorLUT.txt')
mrtrix_lut_file = os.path.join(mrtrix_lut_dir,'fs_a2009s.txt')
output_parcellation_filename=os.path.join(FS_dir,"mri","aparc.a2009s+aseg.mgz")
lh_annotation= ''
rh_annotation= ''
source_annotation_file_lh=''
source_annotation_file_rh=''
return fsavg_dir, parc_lut_file, mrtrix_lut_file, output_parcellation_filename, lh_annotation, rh_annotation, source_annotation_file_lh, source_annotation_file_rh, node_image,normimg_path,final_parc_image
if parcellation == 'hcpmmp1':
# HCPMMP1 definitions
fsavg_dir=os.path.join(freesurfer_home,"subjects","fsaverage")
parc_lut_file = os.path.join(mrtrix_lut_dir,'hcpmmp1_original.txt')
mrtrix_lut_file = os.path.join(mrtrix_lut_dir,'hcpmmp1_ordered.txt')
output_parcellation_filename = os.path.join(FS_dir,"mri","aparc.HCPMMP1+aseg.mgz")
lh_annotation= os.path.join(FS_dir,"label","lh.HCPMMP1.annot")
rh_annotation= os.path.join(FS_dir,"label","rh.HCPMMP1.annot")
source_annotation_file_lh=os.path.join(fsavg_dir,'label','lh.HCPMMP1.annot')
source_annotation_file_rh=os.path.join(fsavg_dir,'label','rh.HCPMMP1.annot')
return fsavg_dir, parc_lut_file, mrtrix_lut_file, output_parcellation_filename, lh_annotation, rh_annotation, source_annotation_file_lh, source_annotation_file_rh, node_image,normimg_path,final_parc_image
elif parcellation == 'yeo7fs':
# yeo7fs definitions
fsavg_dir= os.path.join(freesurfer_home,"subjects","fsaverage5")
parc_lut_file = os.path.join(freesurfer_home,"Yeo2011",'Yeo2011_7networks_Split_Components_LUT.txt')
mrtrix_lut_file = os.path.join(mrtrix_lut_dir,'Yeo2011_7N_split.txt')
output_parcellation_filename = os.path.join(FS_dir,"mri",'aparc.Yeo7+aseg.mgz')
lh_annotation= os.path.join(FS_dir,"label","lh.Yeo7.annot")
rh_annotation= os.path.join(FS_dir,"label","rh.Yeo7.annot")
source_annotation_file_lh=os.path.join(fsavg_dir,'label','lh.Yeo2011_7Networks_N1000.split_components.annot')
source_annotation_file_rh=os.path.join(fsavg_dir,'label','rh.Yeo2011_7Networks_N1000.split_components.annot')
return fsavg_dir, parc_lut_file, mrtrix_lut_file, output_parcellation_filename, lh_annotation, rh_annotation, source_annotation_file_lh, source_annotation_file_rh, node_image,normimg_path,final_parc_image
elif parcellation == 'yeo17fs':
# yeo17fs definitions
fsavg_dir= os.path.join(freesurfer_home,"subjects","fsaverage5")
parc_lut_file = os.path.join(freesurfer_home,"Yeo2011",'Yeo2011_17networks_Split_Components_LUT.txt')
mrtrix_lut_file = os.path.join(mrtrix_lut_dir,'Yeo2011_17N_split.txt')
output_parcellation_filename = os.path.join(FS_dir,"mri",'aparc.Yeo17+aseg.mgz')
lh_annotation= os.path.join(FS_dir,"label","lh.Yeo17fs.annot")
rh_annotation= os.path.join(FS_dir,"label","rh.Yeo17fs.annot")
source_annotation_file_lh=os.path.join(fsavg_dir,'label','lh.Yeo2011_17Networks_N1000.split_components.annot')
source_annotation_file_rh=os.path.join(fsavg_dir,'label','rh.Yeo2011_17Networks_N1000.split_components.annot')
return fsavg_dir, parc_lut_file, mrtrix_lut_file, output_parcellation_filename, lh_annotation, rh_annotation, source_annotation_file_lh, source_annotation_file_rh, node_image,normimg_path,final_parc_image
wf.add(join_task_catalogue(FS_dir=wf.lzin.FS_dir, parcellation=wf.lzin.parcellation, freesurfer_home=freesurfer_home, name="join_task"))
###########################
# mri_surf2surf spec info #
###########################
mri_s2s_input_spec = SpecInfo(
name="Input",
fields=[
( "source_subject_id", str,
{ "help_string": "source subject",
"argstr": "--srcsubject",
"mandatory": True } ),
( "target_subject_id", str,
{ "help_string": "target subject",
"argstr": "--trgsubject",
"mandatory": True } ),
( "source_annotation_file", str,
{ "help_string": "annotfile : map annotation",
"argstr": "--sval-annot",
"mandatory": True } ),
( "target_annotation_file", str,
{ "help_string": "path of file in which to store output values",
"argstr": "--tval",
"mandatory": True } ),
( "hemisphere", str,
{ "help_string": "hemisphere : (lh or rh) for both source and targ",
"argstr": "--hemi",
"mandatory": True } ),
],
bases=(ShellSpec,)
)
mri_s2s_output_spec=SpecInfo(
name="Output",
fields=[
( "target_annotation_file", str,
{ "help_string": "path of file in which to store output values",
"argstr": "--tval",
"mandatory": True } ),
],
bases=(ShellOutSpec,)
)
##################################
# mri_surf2surf task - lh and rh #
##################################
if wf.inputs.parcellation in ['hcpmmp1', 'yeo17fs', 'yeo7fs']:
hemispheres = ['lh', 'rh']
for hemi in hemispheres:
wf.add(
ShellCommandTask(
name=f"mri_s2s_task_{hemi}",
executable="mri_surf2surf",
input_spec=mri_s2s_input_spec,
output_spec=mri_s2s_output_spec,
cache_dir=output_path,
source_subject_id=wf.join_task.lzout.fsavg_dir,
target_subject_id=wf.lzin.FS_dir,
source_annotation_file=getattr(wf.join_task.lzout, f"source_annotation_file_{hemi}"),
target_annotation_file=getattr(wf.join_task.lzout, f"{hemi}_annotation"),
hemisphere=hemi,
)
)
# ############################
# # mri_aparc2aseg spec info #
# ############################
mri_a2a_input_spec = SpecInfo(
name="Input",
fields=[
( "subject", str,
{ "help_string": "Name of the subject as found in the SUBJECTS_DIR",
"argstr": "--s",
"mandatory": True } ),
( "old_ribbon", bool,
{ "help_string": "use mri/hemi.ribbon.mgz as a mask for the cortex",
"argstr": "--old-ribbon",
"mandatory": True } ),
( "annotname", str,
{ "help_string": "Use annotname surface annotation. By default, uses ?h.aparc.annot. With this option, it will load ?h.annotname.annot. The output file will be set to annotname+aseg.mgz, but this can be changed with --o. Note: running --annot aparc.a2009s is NOT the same as running --a2009s. The index numbers will be different.",
"argstr": "--annot",
"mandatory": True } ),
( "volfile", str,
{ "help_string": "Full path of file to save the output segmentation in. Default is mri/aparc+aseg.mgz",
"argstr": "--o",
"mandatory": True } ),
],
bases=(ShellSpec,)
)
mri_a2a_output_spec=SpecInfo(
name="Output",
fields=[
( "volfile", str,
{ "help_string": "Full path of file to save the output segmentation in. Default is mri/aparc+aseg.mgz",
"argstr": "--o",
"mandatory": True } ),
],
bases=(ShellOutSpec,)
)
# ########################
# # mri_aparc2aseg task #
# ########################
if wf.inputs.parcellation in ['hcpmmp1', 'yeo17fs', 'yeo7fs']:
wf.add(
ShellCommandTask(
name="mri_a2a_task",
executable="mri_aparc2aseg",
input_spec=mri_a2a_input_spec,
output_spec=mri_a2a_output_spec,
cache_dir=output_path,
subject=wf.lzin.FS_dir,
old_ribbon=True,
annotname=wf.lzin.parcellation,
volfile=wf.join_task.lzout.output_parcellation_filename,
)
)
##########################################################
# PARCELLATION EDITS - applies to all parcellation types #
##########################################################
# relabel segmenetation to integers
wf.add(
labelconvert(
path_in=wf.join_task.lzout.output_parcellation_filename,
lut_in=wf.join_task.lzout.parc_lut_file,
lut_out=wf.join_task.lzout.mrtrix_lut_file,
path_out=wf.join_task.lzout.node_image,
name="LabelConvert_task"
)
)
# # # # Replace FreeSurfer’s estimates of sub-cortical grey matter structures with estimates from FSL’s FIRST tool
wf.add(
labelsgmfix(
parc=wf.LabelConvert_task.lzout.path_out,
t1=wf.join_task.lzout.normimg_path,
lut=wf.join_task.lzout.mrtrix_lut_file,
output=wf.join_task.lzout.final_parc_image,
name="SGMfix_task",
nocleanup=True,
premasked=True,
sgm_amyg_hipp=True
)
)
########################
# Execute the workflow #
########################
# Set the workflow output as the result of the join_task
# wf.set_output(("parcellation_image", wf.aparc2aseg_task.lzout.out_file))
wf.set_output(("parc_img", wf.SGMfix_task.lzout.output))
result = wf(
FS_dir="/Users/arkievdsouza/git/t1-pipeline/working-dir/catalogue_testing_v1/sub-01", # 100307_orig
parcellation="destrieux", # , hcpmmp1 desikan hcpmmp1 yeo17fs destrieux
plugin="serial",
)