-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBIDS_averager.sh
243 lines (202 loc) · 8.96 KB
/
BIDS_averager.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
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
#!/bin/bash
# set -x
# Created by Alex Cohen to convert chosen files into BIDS formatted dirs.
# g_subjects="019"
# Cases with at least a T1
# g_subjects="001 002 003 004 009 010 011 012 014 015 016 017 018 019 021 022 027 028 029 030"
# Controls with at least a T1
g_subjects="001 002 003 004 005 006 007 008 009 010 011 013 014 015 016 017 018 019 020 021 022 023 024 025 028 029 030 032 033 035 037 038"
# Subjects with at least T1+T2
# g_subjects="Case001 Case002 Case003 Case004 Case009 Case010 Case011 Case012 Case014 Case015 Case016 Case017 Case018 Case019 Case021 Case022 Case027 Case028 Case029 Case030 \
# Control001 Control002 Control003 Control004 Control005 Control006 Control007 Control008 Control009 Control010 Control011 Control013 Control014 Control015 Control016 Control017 \
# Control018 Control019 Control020 Control021 Control022 Control023 Control024 Control025 Control028 Control029 Control030 Control032 Control033 Control035 Control037 Control038"
# Subjects with T1+T2+fMRI+fieldmaps
# g_subjects="Case001 Case002 Case003 Case009 Case010 Case011 Case014 Case015 Case027 Case029 \
# Control001 Control002 Control003 Control004 Control005 Control006 Control007 Control008 Control009 Control010 Control011 Control013 Control014 Control015 Control016 Control017 \
# Control018 Control019 Control021 Control022 Control023 Control024 Control025 Control028 Control032"
# sourcedir=/common/tsc/TSC-R01/Autism/RAW
# sourceprefix="Case"
# labeldir=/common/collections/Analyses/TSC-R01/HCP
# studydir=/common/collections/Analyses/TSC-R01-BIDS/Cases/sourcedata
# labelprefix="Case"
sourcedir=/common/tsc/TSC-R01/Controls/RAW
sourceprefix="case"
labeldir=/common/collections/Analyses/TSC-R01/HCP
studydir=/common/collections/Analyses/TSC-R01-BIDS/Controls/sourcedata
labelprefix="Control"
#################################################################
log=/tmp/ImportProcessing.log
echo "Logging will be output to: $log"
# for ad hoc testing/rerunning
if [ ! -z $1 ] ; then
echo -e "Bypassing hard coded subject list and using command_line input of SUBJECT = $1 instead\n"
g_subjects=$1
fi
# base script
dcm2niix_cmd="dcm2niix -b y -z y"
# dcm2niix_cmd="docker run --user=4135:1003 -ti --rm \
# -v ${session_sourcedir}:/input \
# -v ${session_targetdir}:/output \
# alex/dcm2niix -b y -z y"
dcm2niix_CMD()
{
sequence=`echo $sourcefile | awk -F[._] '{print $(NF-2)}'`
dcm2niix_command="${dcm2niix_cmd}"
if [ -n "${multiple_runs}" ]; then
run_tag="_run-0${run_number}"
else
run_tag=""
fi
target_filename="sub-${subject}_ses-0${session}${task_label}${acq_label}${run_tag}_${file_type}"
# dcm2niix_command="${dcm2niix_command} -o /output/anat"
# dcm2niix_command="${dcm2niix_command} /input/*/scan0${session}/DICOM/${dcmDIR}"
# run dcm2niix
if [ ! -e ${session_targetdir}/${folder_type}/${target_filename}.json ]; then
dcmDIR=`dcmdump --scan-directories --search 0020,0011 ${session_sourcedir}/*${sequence}* +Fs | grep -B 1 "\[${sequence}\]" | head -n 1 | awk -F[:/] '{print $(NF-1)}'`
dcm2niix_command="${dcm2niix_command} -f ${target_filename}"
dcm2niix_command="${dcm2niix_command} -o ${session_targetdir}/${folder_type}"
dcm2niix_command="${dcm2niix_command} ${session_sourcedir}/${dcmDIR}"
echo Running ${dcm2niix_command}
${dcm2niix_command} >> ${log}
echo -e "ses-${session}/${folder_type}/${target_filename}.nii.gz\t${dcmDIR}" >> ${studydir}/sub-${subject}/sub-${subject}_scans.tsv
if [ ${folder_type} == "func" ]; then
taskname=`echo ${task_label} | awk -F[-] '{print $(NF)}'`
jq '. + { "TaskName": "'"${taskname}"'" }' ${session_targetdir}/${folder_type}/${target_filename}.json > ${session_targetdir}/${folder_type}/temp.json
mv ${session_targetdir}/${folder_type}/temp.json ${session_targetdir}/${folder_type}/${target_filename}.json
fi
else
echo "Files already exist, moving on: ${session_targetdir}/${folder_type}/${target_filename}.json"
fi
}
run_dcm2niix()
{
subject=${1}
echo -e "Starting work on: $subject"
mkdir --parents ${studydir}/sub-${subject}
if [ ! -e ${studydir}/sub-${subject}/sub-${subject}_scans.tsv ]; then
echo -e "filename\tsource_data" > ${studydir}/sub-${subject}/sub-${subject}_scans.tsv
fi
# Determine which sessions are used
subject_labeldir=${labeldir}/${labelprefix}${subject}/unprocessed/source
sessions=`ls ${subject_labeldir} | awk -F[._] '{print $(NF-3)}' | sort | uniq`
for session in ${sessions} ; do
# set locations
# workaround for weird docker behavior:
# session_sourcedir=${sourcedir}/${sourceprefix}${subject}
session_sourcedir=${sourcedir}/${sourceprefix}${subject}/*/scan0${session}/DICOM
session_targetdir=${studydir}/sub-${subject}/ses-0${session}
mkdir --parents ${session_targetdir}/anat
mkdir --parents ${session_targetdir}/func
# get file lists
T1s_that_are_MEMPRAGE=`ls ${subject_labeldir}/${labelprefix}${subject}_T1w_MEMPRAGE_${session}* 2>/dev/null`
T1s_that_are_MPRAGE=`ls ${subject_labeldir}/${labelprefix}${subject}_T1w_${session}* 2>/dev/null`
T2s_that_are_Flair=`ls ${subject_labeldir}/${labelprefix}${subject}_T2w_Flair_${session}* 2>/dev/null`
T2s_that_are_not_Flair=`ls ${subject_labeldir}/${labelprefix}${subject}_T2w_${session}* 2>/dev/null`
fMRI_runs=`ls ${subject_labeldir}/${labelprefix}${subject}_fMRI_${session}* 2>/dev/null`
if [ ! -z "${T1s_that_are_MEMPRAGE}" ]; then
if [ `echo ${T1s_that_are_MEMPRAGE} | wc -w` -gt 1 ]; then
multiple_runs="True"
run_number=1
else
multiple_runs=""
fi
for sourcefile in ${T1s_that_are_MEMPRAGE} ; do
acq_label="_acq-memprage"
task_label=""
file_type="T1w"
folder_type="anat"
dcm2niix_CMD
((run_number++))
done
fi
if [ ! -z "${T1s_that_are_MPRAGE}" ]; then
if [ `echo ${T1s_that_are_MPRAGE} | wc -w` -gt 1 ]; then
multiple_runs="True"
run_number=1
else
multiple_runs=""
fi
for sourcefile in ${T1s_that_are_MPRAGE} ; do
acq_label="_acq-mprage"
task_label=""
file_type="T1w"
folder_type="anat"
dcm2niix_CMD
((run_number++))
done
fi
if [ ! -z "${T2s_that_are_Flair}" ]; then
if [ `echo ${T2s_that_are_Flair} | wc -w` -gt 1 ]; then
multiple_runs="True"
run_number=1
else
multiple_runs=""
fi
for sourcefile in ${T2s_that_are_Flair} ; do
acq_label=""
task_label=""
file_type="FLAIR"
folder_type="anat"
dcm2niix_CMD
((run_number++))
done
fi
if [ ! -z "${T2s_that_are_not_Flair}" ]; then
if [ `echo ${T2s_that_are_not_Flair} | wc -w` -gt 1 ]; then
multiple_runs="True"
run_number=1
else
multiple_runs=""
fi
for sourcefile in ${T2s_that_are_not_Flair} ; do
if [ ! -z `echo ${sourcefile} | grep coronal` ]; then
acq_label="_acq-coronal"
task_label=""
file_type="T2w"
folder_type="anat"
dcm2niix_CMD
((run_number++))
elif [ ! -z `echo ${sourcefile} | grep axial` ]; then
acq_label="_acq-axial"
task_label=""
file_type="T2w"
folder_type="anat"
dcm2niix_CMD
((run_number++))
else
acq_label=""
task_label=""
file_type="T2w"
folder_type="anat"
dcm2niix_CMD
((run_number++))
fi
done
fi
if [ ! -z "${fMRI_runs}" ]; then
if [ `echo ${fMRI_runs} | wc -w` -gt 1 ]; then
multiple_runs="True"
run_number=1
else
multiple_runs=""
fi
for sourcefile in ${fMRI_runs} ; do
acq_label=""
task_label="_task-rest"
file_type="bold"
folder_type="func"
dcm2niix_CMD
((run_number++))
done
fi
done
printf "\n"
}
main()
{
for subject in ${g_subjects} ; do
run_dcm2niix ${subject}
done
}
# invoke the main function to get things started
main $@