-
Notifications
You must be signed in to change notification settings - Fork 21
/
abcdbids.py
363 lines (311 loc) · 14.4 KB
/
abcdbids.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
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
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Functions for converting ABCD-BIDS-format derivatives to fMRIPrep format.
These functions are specifically designed to work with abcd-hcp-pipeline version 0.1.3.
https://github.com/DCAN-Labs/abcd-hcp-pipeline/releases/tag/v0.1.3
"""
import glob
import os
import re
import nibabel as nb
import pandas as pd
from nipype import logging
from xcp_d.data import load as load_data
from xcp_d.ingression.utils import (
collect_anatomical_files,
collect_hcp_confounds,
collect_meshes,
collect_morphs,
copy_files_in_dict,
plot_bbreg,
write_json,
)
from xcp_d.utils.filemanip import ensure_list
LOGGER = logging.getLogger("nipype.utils")
def convert_dcan2bids(in_dir, out_dir, participant_ids=None):
"""Convert DCAN derivatives to BIDS-compliant derivatives.
Parameters
----------
in_dir : str
Path to DCAN derivatives.
out_dir : str
Path to the output BIDS-compliant derivatives folder.
participant_ids : None or list of str
List of participant IDs to run conversion on.
The participant IDs must not have the "sub-" prefix.
If None, the function will search for all subjects in ``in_dir`` and convert all of them.
Returns
-------
participant_ids : list of str
The list of subjects whose derivatives were converted.
Notes
-----
Since the T1w is in standard space already, we use identity transforms instead of the
individual transforms available in the DCAN derivatives.
"""
LOGGER.warning("convert_dcan2bids is an experimental function.")
in_dir = os.path.abspath(in_dir)
out_dir = os.path.abspath(out_dir)
if participant_ids is None:
subject_folders = sorted(glob.glob(os.path.join(in_dir, "sub*")))
subject_folders = [
subject_folder for subject_folder in subject_folders if os.path.isdir(subject_folder)
]
participant_ids = [os.path.basename(subject_folder) for subject_folder in subject_folders]
if not participant_ids:
raise ValueError(f"No subject found in {in_dir}")
else:
participant_ids = ensure_list(participant_ids)
for subject_id in participant_ids:
LOGGER.info(f"Processing {subject_id}")
convert_dcan_to_bids_single_subject(
in_dir=in_dir,
out_dir=out_dir,
sub_ent=subject_id,
)
return participant_ids
def convert_dcan_to_bids_single_subject(in_dir, out_dir, sub_ent):
"""Convert DCAN derivatives to BIDS-compliant derivatives for a single subject.
Parameters
----------
in_dir : str
Path to the subject's DCAN derivatives.
out_dir : str
Path to the output BIDS-compliant derivatives folder.
sub_ent : str
Subject identifier, with "sub-" prefix.
Notes
-----
Since the T1w is in standard space already, we use identity transforms instead of the
individual transforms available in the DCAN derivatives.
.. code-block::
sub-<sub_id>
└── ses-<ses_id>
└── files
└── MNINonLinear
├── Results
│ ├── ses-<ses_id>_task-<task_id>_run-<run_id>
│ │ ├── ses-<ses_id>_task-<task_id>_run-<run_id>_SBRef.nii.gz
│ │ ├── ses-<ses_id>_task-<task_id>_run-<run_id>.nii.gz
│ │ ├── ses-<ses_id>_task-<task_id>_run-<run_id>_Atlas.dtseries.nii
│ │ ├── Movement_Regressors.txt
│ │ ├── Movement_AbsoluteRMS.txt
│ │ └── brainmask_fs.2.0.nii.gz
├── fsaverage_LR32k
│ ├── L.pial.32k_fs_LR.surf.gii
│ ├── R.pial.32k_fs_LR.surf.gii
│ ├── L.white.32k_fs_LR.surf.gii
│ ├── R.white.32k_fs_LR.surf.gii
│ ├── <sub_id>.L.thickness.32k_fs_LR.shape.gii
│ ├── <sub_id>.R.thickness.32k_fs_LR.shape.gii
│ ├── <sub_id>.L.corrThickness.32k_fs_LR.shape.gii
│ ├── <sub_id>.R.corrThickness.32k_fs_LR.shape.gii
│ ├── <sub_id>.L.curvature.32k_fs_LR.shape.gii
│ ├── <sub_id>.R.curvature.32k_fs_LR.shape.gii
│ ├── <sub_id>.L.sulc.32k_fs_LR.shape.gii
│ ├── <sub_id>.R.sulc.32k_fs_LR.shape.gii
│ ├── <sub_id>.L.MyelinMap.32k_fs_LR.func.gii
│ ├── <sub_id>.R.MyelinMap.32k_fs_LR.func.gii
│ ├── <sub_id>.L.SmoothedMyelinMap.32k_fs_LR.func.gii
│ └── <sub_id>.R.SmoothedMyelinMap.32k_fs_LR.func.gii
├── T1w.nii.gz
├── aparc+aseg.nii.gz
├── brainmask_fs.nii.gz
├── ribbon.nii.gz
├── vent_2mm_<sub_id>_mask_eroded.nii.gz
└── wm_2mm_<sub_id>_mask_eroded.nii.gz
"""
assert isinstance(in_dir, str)
assert os.path.isdir(in_dir), f"Folder DNE: {in_dir}"
assert isinstance(out_dir, str)
assert isinstance(sub_ent, str)
sub_id = sub_ent.replace("sub-", "")
# Reset the subject entity in case the sub- prefix wasn't included originally.
sub_ent = f"sub-{sub_id}"
VOLSPACE = "MNI152NLin6Asym"
volspace_ent = f"space-{VOLSPACE}"
RES_ENT = "res-2"
subject_dir_bids = os.path.join(out_dir, sub_ent)
os.makedirs(subject_dir_bids, exist_ok=True)
# get session ids
session_folders = sorted(glob.glob(os.path.join(in_dir, sub_ent, "s*")))
ses_entities = [
os.path.basename(ses_dir) for ses_dir in session_folders if os.path.isdir(ses_dir)
]
if not ses_entities:
raise FileNotFoundError(f"No session volumes found in {os.path.join(in_dir, sub_ent)}")
dataset_description_fmriprep = os.path.join(out_dir, "dataset_description.json")
if os.path.isfile(dataset_description_fmriprep):
LOGGER.info("Converted dataset folder already exists. Skipping conversion.")
return
# A dictionary of mappings from HCP derivatives to fMRIPrep derivatives.
# Values will be lists, to allow one-to-many mappings.
copy_dictionary = {}
# The identity xform is used in place of any actual ones.
identity_xfm = str(load_data("transform/itkIdentityTransform.txt"))
copy_dictionary[identity_xfm] = []
morph_dict_all_ses = {}
for ses_ent in ses_entities:
LOGGER.info(f"Processing {ses_ent}")
subses_ents = f"{sub_ent}_{ses_ent}"
session_dir_fmriprep = os.path.join(subject_dir_bids, ses_ent)
anat_dir_orig = os.path.join(in_dir, sub_ent, ses_ent, "files", "MNINonLinear")
anat_dir_bids = os.path.join(session_dir_fmriprep, "anat")
func_dir_orig = os.path.join(anat_dir_orig, "Results")
func_dir_bids = os.path.join(session_dir_fmriprep, "func")
work_dir = os.path.join(subject_dir_bids, "work")
os.makedirs(anat_dir_bids, exist_ok=True)
os.makedirs(func_dir_bids, exist_ok=True)
os.makedirs(work_dir, exist_ok=True)
# Create identity-based transforms
t1w_to_template_fmriprep = os.path.join(
anat_dir_bids,
f"{subses_ents}_from-T1w_to-{VOLSPACE}_mode-image_xfm.txt",
)
copy_dictionary[identity_xfm].append(t1w_to_template_fmriprep)
template_to_t1w_fmriprep = os.path.join(
anat_dir_bids,
f"{subses_ents}_from-{VOLSPACE}_to-T1w_mode-image_xfm.txt",
)
copy_dictionary[identity_xfm].append(template_to_t1w_fmriprep)
# Collect anatomical files to copy
base_anatomical_ents = f"{subses_ents}_{volspace_ent}_{RES_ENT}"
anat_dict = collect_anatomical_files(
anat_dir_orig,
anat_dir_bids,
base_anatomical_ents,
)
copy_dictionary = {**copy_dictionary, **anat_dict}
# Collect surface files to copy
mesh_dict = collect_meshes(anat_dir_orig, anat_dir_bids, sub_id, subses_ents)
copy_dictionary = {**copy_dictionary, **mesh_dict}
# Convert morphometry files
morphometry_dict = collect_morphs(anat_dir_orig, anat_dir_bids, sub_id, subses_ents)
morph_dict_all_ses = {**morph_dict_all_ses, **morphometry_dict}
LOGGER.info("Finished collecting anatomical files")
# Get masks to be used to extract confounds
wm_mask = os.path.join(anat_dir_orig, f"wm_2mm_{sub_id}_mask_eroded.nii.gz")
csf_mask = os.path.join(anat_dir_orig, f"vent_2mm_{sub_id}_mask_eroded.nii.gz")
# Collect functional files to copy
task_dirs_orig = sorted(glob.glob(os.path.join(func_dir_orig, f"{ses_ent}_task-*")))
task_names = [os.path.basename(f) for f in task_dirs_orig if os.path.isdir(f)]
for base_task_name in task_names:
LOGGER.info(f"Processing {base_task_name}")
# Names seem to follow ses-X_task-Y_run-Z format.
found_task_info = re.findall(
r".*_task-([0-9a-zA-Z]+[a-zA-Z]+)_run-(\d+)",
base_task_name,
)
if len(found_task_info) != 1:
LOGGER.warning(
f"Task name and run number could not be inferred for {base_task_name}. "
"Skipping."
)
continue
task_id, run_id = found_task_info[0]
task_ent = f"task-{task_id}"
run_ent = f"run-{run_id}"
task_dir_orig = os.path.join(func_dir_orig, base_task_name)
func_prefix = f"{subses_ents}_{task_ent}_{run_ent}"
# Find original task files
sbref_orig = os.path.join(task_dir_orig, f"{base_task_name}_SBRef.nii.gz")
boldref_fmriprep = os.path.join(
func_dir_bids,
f"{func_prefix}_{volspace_ent}_{RES_ENT}_boldref.nii.gz",
)
copy_dictionary[sbref_orig] = [boldref_fmriprep]
bold_nifti_orig = os.path.join(task_dir_orig, f"{base_task_name}.nii.gz")
bold_nifti_fmriprep = os.path.join(
func_dir_bids,
f"{func_prefix}_{volspace_ent}_{RES_ENT}_desc-preproc_bold.nii.gz",
)
copy_dictionary[bold_nifti_orig] = [bold_nifti_fmriprep]
bold_cifti_orig = os.path.join(task_dir_orig, f"{base_task_name}_Atlas.dtseries.nii")
bold_cifti_fmriprep = os.path.join(
func_dir_bids,
f"{func_prefix}_space-fsLR_den-91k_bold.dtseries.nii",
)
copy_dictionary[bold_cifti_orig] = [bold_cifti_fmriprep]
# Extract metadata for JSON files
bold_metadata = {
"RepetitionTime": float(nb.load(bold_nifti_orig).header.get_zooms()[-1]),
"TaskName": task_id,
}
bold_nifti_json_fmriprep = os.path.join(
func_dir_bids,
f"{func_prefix}_{volspace_ent}_{RES_ENT}_desc-preproc_bold.json",
)
write_json(bold_metadata, bold_nifti_json_fmriprep)
bold_metadata.update(
{
"grayordinates": "91k",
"space": "HCP grayordinates",
"surface": "fsLR",
"surface_density": "32k",
"volume": "MNI152NLin6Asym",
},
)
bold_cifti_json_fmriprep = os.path.join(
func_dir_bids,
f"{func_prefix}_space-fsLR_den-91k_bold.dtseries.json",
)
write_json(bold_metadata, bold_cifti_json_fmriprep)
# Create confound regressors
collect_hcp_confounds(
task_dir_orig=task_dir_orig,
out_dir=func_dir_bids,
prefix=func_prefix,
work_dir=work_dir,
bold_file=bold_nifti_orig,
# This file is the anatomical brain mask downsampled to 2 mm3.
brainmask_file=os.path.join(task_dir_orig, "brainmask_fs.2.0.nii.gz"),
csf_mask_file=csf_mask,
wm_mask_file=wm_mask,
)
# Make figures
figdir = os.path.join(subject_dir_bids, "figures")
os.makedirs(figdir, exist_ok=True)
bbref_fig_fmriprep = os.path.join(
figdir,
f"{func_prefix}_desc-bbregister_bold.svg",
)
t1w = os.path.join(anat_dir_orig, "T1w.nii.gz")
ribbon = os.path.join(anat_dir_orig, "ribbon.nii.gz")
bbref_fig_fmriprep = plot_bbreg(
fixed_image=t1w,
moving_image=sbref_orig,
out_file=bbref_fig_fmriprep,
contour=ribbon,
)
LOGGER.info(f"Finished {base_task_name}")
LOGGER.info("Finished collecting functional files")
# Copy ABCD files to fMRIPrep folder
LOGGER.info("Copying files")
copy_files_in_dict(copy_dictionary)
LOGGER.info("Finished copying files")
# Write the dataset description out last
dataset_description_dict = {
"Name": "ABCD-DCAN",
"BIDSVersion": "1.9.0",
"DatasetType": "derivative",
"GeneratedBy": [
{
"Name": "DCAN",
"Version": "0.0.4",
"CodeURL": "https://github.com/DCAN-Labs/abcd-hcp-pipeline",
},
],
}
if not os.path.isfile(dataset_description_fmriprep):
write_json(dataset_description_dict, dataset_description_fmriprep)
# Write out the mapping from DCAN to fMRIPrep
copy_dictionary = {**copy_dictionary, **morph_dict_all_ses}
scans_dict = {}
for key, values in copy_dictionary.items():
for item in values:
scans_dict[item] = key
scans_tuple = tuple(scans_dict.items())
scans_df = pd.DataFrame(scans_tuple, columns=["filename", "source_file"])
scans_tsv = os.path.join(subject_dir_bids, f"{subses_ents}_scans.tsv")
scans_df.to_csv(scans_tsv, sep="\t", index=False)
LOGGER.info("Conversion completed")