forked from DCAN-Labs/abcd-dicom2bids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subset_data.py
executable file
·341 lines (297 loc) · 11.5 KB
/
subset_data.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
#! /usr/bin/env python3
"""
A tool to subset the ABCD full data set to a smaller subset of datatypes.
Created 2/15/2022 by Eric Earl <[email protected]>
"""
import argparse # For command line arguments
import csv # For CSV file handling
import json # For BIDS sidecar JSON file handling
import os # For file system operations
import pandas # For text-file reading and dataframes
import pickle # For extracting pickle file data
import subprocess # For calling external programs
from datetime import datetime # For timestamping print statements
from glob import glob # For "globbing" file names
POSSIBLES = [
"Diffusion-FM",
"Diffusion-FM-AP",
"Diffusion-FM-PA",
"DTI",
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA",
"MID-fMRI",
"nBack-fMRI",
"rsfMRI",
"SST-fMRI",
"T1",
"T1-NORM",
"T2",
"T2-NORM"
]
DATATYPES = {
"anat": [
"T1",
"T1-NORM",
"T2",
"T2-NORM"
],
"dwi": [
"Diffusion-FM",
"Diffusion-FM-AP",
"Diffusion-FM-PA",
"DTI",
],
"dwi_fmap": [
"Diffusion-FM",
"Diffusion-FM-AP",
"Diffusion-FM-PA"
],
"func_fmap": [
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA"
],
"fmap": [
"Diffusion-FM",
"Diffusion-FM-AP",
"Diffusion-FM-PA",
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA"
],
"func": [
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA",
"MID-fMRI",
"nBack-fMRI",
"rsfMRI",
"SST-fMRI"
],
"task-MID": [
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA",
"MID-fMRI"
],
"task-nback": [
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA",
"nBack-fMRI"
],
"task-rest": [
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA",
"rsfMRI"
],
"task-SST": [
"fMRI-FM",
"fMRI-FM-AP",
"fMRI-FM-PA",
"SST-fMRI"
],
"T1w-asacquired": [
"T1"
],
"T2w-asacquired": [
"T2"
],
"T1w-normalized": [
"T1-NORM"
],
"T2w-normalized": [
"T2-NORM"
]
}
HERE = os.path.dirname(os.path.realpath(__file__))
__doc__ = """
This command-line tool allows the user to easily subset a directory of ABCD-BIDS
input data by datatype (T1, T2, fMRI, field maps, etc).
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-a', '--abcd', metavar='FILE' , required=True,
help='Input abcd_fastqc01.txt file')
# parser.add_argument('-i', '--input-dir', metavar='DIRECTORY', required=True,
# help='Input directory to pull BIDS data from')
parser.add_argument('-p', '--pickle-file', metavar='FILE', required=True,
help='Input path to the "ftq_map_mapping.pkl" file')
parser.add_argument('-o', '--output-dir', metavar='DIRECTORY', required=True,
help='Output directory to deposit BIDS hierarchy into')
parser.add_argument('-t', '--types', metavar='TYPE', required=True,
nargs='+', choices=DATATYPES.keys(),
help="""
Space-separated list of data types to subset. Pick
one or more: """ + ', '.join(DATATYPES.keys())
)
parser.add_argument('-g', '--good-qc', action='store_true',
help='Only keep data flagged as "good" (ftq_usable==1).')
parser.add_argument('-f', '--intended-for', action='store_true',
help='Only keep fmaps with non-empty IntendedFor fields.')
args = parser.parse_args()
# only for testing
# input_dir = os.path.abspath(args.input_dir)
input_dir = os.path.abspath('/data/ABCD_DSST/ABCD_BIDS/fast_track')
rawdata = os.path.join(input_dir, 'rawdata')
sourcedata = os.path.join(input_dir, 'sourcedata')
input_txt = os.path.abspath(args.abcd)
pickle_file = os.path.abspath(args.pickle_file)
output_dir = os.path.abspath(args.output_dir)
with open(pickle_file, "rb") as p:
ftq_map_mapping = pickle.load(p)
# parse datatypes
datatypes = set()
for t in args.types:
for d in DATATYPES[t]:
datatypes.add(d)
subsets = sorted(list(datatypes))
with open(args.abcd, 'r') as f:
abcd_lines = f.readlines()
for series_idx, series_col in enumerate(abcd_lines[0].split('\t')):
if 'ftq_series_id' in series_col:
print('ftq_series_id is in the 1-indexed column: ' + str(series_idx+1))
break
for recall_idx, recall_col in enumerate(abcd_lines[0].split('\t')):
if 'ftq_recall_reason' in recall_col:
print('ftq_recall_reason is in the 1-indexed column: ' + str(recall_idx+1))
break
for usable_idx, usable_col in enumerate(abcd_lines[0].split('\t')):
if 'ftq_usable' in usable_col:
print('ftq_usable is in the 1-indexed column: ' + str(usable_idx+1))
break
ftq_series_id_dict = {}
for line in abcd_lines[2:]:
ftq_series_id = line.split('\t')[series_idx].strip('"')
ftq_recall_reason = line.split('\t')[recall_idx].strip('"')
ftq_usable = line.split('\t')[usable_idx].strip('"')
if ftq_recall_reason == '':
if args.good_qc:
if ftq_usable == "1":
pass
else:
continue
ftq_series_id_dict[ftq_series_id] = line
# create output directories
print(datetime.now(), 'Creating subset directory:', output_dir)
os.makedirs(output_dir, exist_ok=True)
# match ftq_series_ids to subsets
print(datetime.now(), 'Matching ftq_series_ids to subsets:', subsets)
ftq_series_ids = []
for subset in subsets:
print(datetime.now(), '\t', subset)
for ftq_series_id in ftq_map_mapping.keys():
if "_ABCD-" + subset + "_" in ftq_series_id:
ftq_series_ids.append(ftq_series_id)
subset_ftq_series_ids = set(ftq_series_ids)
all_ftq_series_ids = set(ftq_series_id_dict.keys())
final_subset = subset_ftq_series_ids & all_ftq_series_ids
# create subset QC file
subset_qc_file = output_dir + '.abcd_fastqc01.txt'
print(datetime.now(), 'Creating subset QC file:', subset_qc_file)
with open(subset_qc_file, 'w') as f:
f.write('"related"\t' + abcd_lines[0])
f.write('"Related BIDS NIfTI file"\t' + abcd_lines[1])
for ftq_series_id in list(final_subset):
related_nifti = [f for f in ftq_map_mapping[ftq_series_id] if f.endswith('.nii.gz')][0]
f.write('"' + related_nifti + '"\t' + ftq_series_id_dict[ftq_series_id])
# get the fmaps
print(datetime.now(), 'Collecting relevant field maps, if any')
dwi_fmap_jsons = []
func_fmap_jsons = []
for subset in subsets:
if 'Diffusion-FM' in subset and dwi_fmap_jsons == []:
dwi_fmap_jsons = glob(os.path.join(rawdata, 'sub-*', 'ses-*', 'fmap', '*_acq-dwi_*.json'))
if 'fMRI-FM' in subset and func_fmap_jsons == []:
func_fmap_jsons = glob(os.path.join(rawdata, 'sub-*', 'ses-*', 'fmap', '*_acq-func_*.json'))
# all fmap JSONs
fmap_jsons = dwi_fmap_jsons + func_fmap_jsons
fmap_jsons_len = len(fmap_jsons)
# in the case of IntendedFor...
final_fmap_jsons = []
if args.intended_for:
print(datetime.now(), 'Selecting only fmaps with non-empty IntendedFor fields')
print(datetime.now(), 'This might take a while so please be patient...')
for i, fmap_json in enumerate(fmap_jsons):
if i % (round(fmap_jsons_len*0.05)) == 0:
print(datetime.now(), 'Progress:', str(round(100*i/fmap_jsons_len)), '%')
with open(fmap_json, 'r') as f:
fmap_dict = json.load(f)
if 'IntendedFor' in fmap_dict and not fmap_dict['IntendedFor'] == []:
final_fmap_jsons.append(fmap_json)
# if no args.intended_for flag was provided then final_fmap_jsons is still empty
if final_fmap_jsons == []:
# so use all fmap_jsons
final_fmap_jsons = fmap_jsons
# get the sourcedata
print(datetime.now(), 'Collecting relevant task-based fMRI E-Prime files, if any')
sourcedata_txts = []
for subset in subsets:
if subset == 'MID-fMRI':
sourcedata_txts += glob(os.path.join(sourcedata, 'sub-*', 'ses-*', 'func', '*_task-MID_*.txt'))
if subset == 'nBack-fMRI':
sourcedata_txts += glob(os.path.join(sourcedata, 'sub-*', 'ses-*', 'func', '*_task-nback_*.txt'))
if subset == 'SST-fMRI':
sourcedata_txts += glob(os.path.join(sourcedata, 'sub-*', 'ses-*', 'func', '*_task-SST_*.txt'))
# symlink the ftq_series_id mapped files
print(datetime.now(), 'Symbolically linking func, dwi, and anat files, if any')
for ftq_series_id in list(final_subset):
if '-fMRI_' in ftq_series_id or '_ABCD-rsfMRI_' in ftq_series_id:
modality = 'func'
elif '_ABCD-DTI_' in ftq_series_id:
modality = 'dwi'
elif '_ABCD-T1' in ftq_series_id or '_ABCD-T2' in ftq_series_id:
modality = 'anat'
mapped_file_list = ftq_map_mapping[ftq_series_id]
subses_underscore_split = mapped_file_list[0].split('_')
subject = subses_underscore_split[0]
session = subses_underscore_split[1]
for mapped_file in mapped_file_list:
mapped_path = os.path.join(rawdata, subject, session, modality, mapped_file)
mapped_file_relpath = os.path.relpath(mapped_path, input_dir)
output_path = os.path.join(output_dir, mapped_file_relpath)
output_subdir = os.path.dirname(output_path)
if os.path.exists(mapped_path):
os.makedirs(output_subdir, exist_ok=True)
os.symlink(mapped_path, output_path)
else:
print(datetime.now(), f'Does not exist so not symlinking: {mapped_path}')
# symlink the fmaps, if any
print(datetime.now(), 'Symbolically linking fmap files, if any')
for fmap_json in final_fmap_jsons:
fmap_nifti = fmap_json.replace('.json', '.nii.gz')
fmap_json_relpath = os.path.relpath(fmap_json, input_dir)
output_json = os.path.join(output_dir, fmap_json_relpath)
output_nifti = os.path.join(output_dir, fmap_json_relpath.replace('.json', '.nii.gz'))
output_subdir = os.path.dirname(output_json)
os.makedirs(output_subdir, exist_ok=True)
if os.path.exists(fmap_json) and os.path.exists(fmap_nifti):
os.symlink(fmap_json, output_json)
os.symlink(fmap_nifti, output_nifti)
else:
fmap_print = fmap_nifti.replace('nii.gz','{json,nii.gz}')
print(datetime.now(), f'One of JSON or NII.GZ do not exist so not symlinking either: {fmap_print}')
if '_acq-dwi_' in fmap_json:
fmap_bval = fmap_json.replace('.json', '.bval')
fmap_bvec = fmap_json.replace('.json', '.bvec')
output_bval = os.path.join(output_dir, fmap_json_relpath.replace('.json', '.bval'))
output_bvec = os.path.join(output_dir, fmap_json_relpath.replace('.json', '.bvec'))
if os.path.exists(fmap_bval) and os.path.exists(fmap_bvec):
os.symlink(fmap_bval, output_bval)
os.symlink(fmap_bvec, output_bvec)
else:
bvxx_print = fmap_bval.replace('bval','{bval,bvec}')
print(datetime.now(), f'One of BVAL or BVEC do not exist so not symlinking either: {bvxx_print}')
# symlink the sourcedata, if any
print(datetime.now(), 'Symbolically linking task-based fMRI E-Prime files, if any')
for sourcedata_txt in sourcedata_txts:
sourcedata_txt_relpath = os.path.relpath(sourcedata_txt, input_dir)
output_txt = os.path.join(output_dir, sourcedata_txt_relpath)
output_subdir = os.path.dirname(output_txt)
if os.path.exists(sourcedata_txt):
os.makedirs(output_subdir, exist_ok=True)
os.symlink(sourcedata_txt, output_txt)
else:
print(datetime.now(), f'Does not exist so not symlinking: {sourcedata_txt}')
print(datetime.now(), 'All done!')