-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathKUL_multisubjects_dcm2bids.sh
executable file
·239 lines (177 loc) · 6.01 KB
/
KUL_multisubjects_dcm2bids.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
#!/bin/bash
# Bash shell script wrapper for calling KUL_dcm2bids for multiple subjects
#
# Requires KUL_dcm2bids
#
# @ Stefan Sunaert - UZ/KUL - [email protected]
#
# v0.1 - dd 26/10/2018 - alpha version
version="v0.1 - dd 26/10/2018"
# ----------------------------------- MAIN ---------------------------------------------
# this script defines a few functions:
# - Usage (for information to the novice user)
# - kul_e2cl from KUL_main_functions (for logging)
# source general functions
kul_main_dir=$(dirname "$0")
script=$(basename "$0")
source $kul_main_dir/KUL_main_functions.sh
# $cwd, mrtrix3new & $log_dir is made in main_functions
# BEGIN LOCAL FUNCTIONS --------------
# --- function Usage ---
function Usage {
cat <<USAGE
`basename $0` wrapper for calling KUL_dcm2bids for multiple subjects
Usage:
`basename $0` -d dicom_dir -c config_file -o bids_dir <OPT_ARGS>
Depends on a config file that defines parameters with subjects, dicom_zip_files, study_sequence_descriptions e.g.
BIDS_participant;EAD;dicom_zip;config_file;session
P001;70386396;John_Lennon.zip;study_sequences_2parts.txt;tp01
P002;60423284;Curt_Cobain.zip;study_sequences_3parts.txt;tp01
P003;77503712;Stefan_Sunaert.zip;study_sequences_3parts.txt;tp01
explains that we scanned 3 subjects in this study.
BIDS_participant will be the anonymised participant name in the BIDS subfolder
EAD = an internal reference number
dicom_zip_file = name of the zipfile containing all dicoms
config_file = description of the subjects (see above)
session = e.g. tp01 (timepoint 1) in a longitudinal study with multiple timepoints
Example:
`basename $0` -d DICOM -c Study_config/Subjects_for_dcm2bids_conversion.csv -o BIDS
Required arguments:
-d: directory where all dicom_zip_file (the zip or tar.gz containing all your dicoms) are stored
-c: config_file
-o: bids directory
Optional arguments:
-t: temporary directory (default = /tmp)
-e: copy task-*_events.tsv from config to BIDS dir
-v: verbose
USAGE
exit 1
}
# END LOCAL FUNCTIONS --------------
# CHECK COMMAND LINE OPTIONS -------------
#
# Set defaults
silent=1
tmp=/tmp
# Set flags
dcmdir_flag=0
conf_flag=0
bids_flag=0
tmp_flag=0
events_flag=0
if [ "$#" -lt 3 ]; then
Usage >&2
exit 1
else
while getopts "d:c:o:veth" OPT; do
case $OPT in
d) #dicom_dir
dcmdir_flag=1
dcmdir=$OPTARG
;;
c) #config_file
conf_flag=1
conf=$OPTARG
;;
o) #bids output directory
bids_flag=1
bids_output=$OPTARG
;;
v) #verbose
silent=0
;;
e) #events for task based fmri
events_flag=1
;;
t) #temporary directory
tmp_flag=1
tmp=$OPTARG
;;
h) #help
Usage >&2
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo
Usage >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
echo
Usage >&2
exit 1
;;
esac
done
fi
# check for required options
if [ $dcmdir_flag -eq 0 ] ; then
echo
echo "Option -d is required: give the directory with your dicoms zip files" >&2
echo
exit 2
fi
if [ $conf_flag -eq 0 ] ; then
echo
echo "Option -c is required: give the path to the file that describes the subjects" >&2
echo
exit 2
elif [ ! -f $conf ] ; then
echo
echo "The config file $conf does not exist"
echo
exit 2
fi
if [ $bids_flag -eq 0 ] ; then
echo
echo "Option -o is required: give the path to the BIDS directory" >&2
echo
exit 2
fi
# INITIATE ---
# ----------- SAY HELLO ----------------------------------------------------------------------------------
if [ $silent -eq 0 ]; then
echo " The script you are running has basename `basename "$0"`, located in dirname $kul_main_dir"
echo " The present working directory is `pwd`"
fi
echo " reading $conf"
# we read the config file (and it may be csv, tsv or ;-seperated)
while IFS=$'\t,;' read -r BIDS_participant EAD dicom_zip config_file session comment; do
if [ "$dicom_zip" = "dicom_zip" ]; then
echo "first line" > /dev/null 2>&1
else
if [ $silent -eq 0 ]; then
echo " Information about the config file:"
echo " BIDS_participant = $BIDS_participant"
echo " EAD = $EAD"
echo " dicom_zip_file = $dicom_zip"
echo " sequences_config_file = $config_file"
echo " session = $session"
echo " bids_output = $bids_output"
fi
# check if already converted
bids_dir_to_check=${bids_output}/sub-${BIDS_participant}/ses-${session}
#echo $BIDS_participant
#echo $bids_dir_to_check
#if [ ! -d $bids_dir_to_check ]; then
kul_e2cl "Performing KUL_dcm2bids.sh -d $dcmdir/$dicom_zip -p $BIDS_participant -c $config_file -o $bids_output -s "${session}" " $log
KUL_dcm2bids_new.sh -d $dcmdir/$dicom_zip -p $BIDS_participant -c $config_file -o $bids_output -s $session -x
#else
# echo " BIDS conversion of participant $BIDS_participant already done, skipping..."
#fi
fi
done < $conf
# copying task based events.tsv to BIDS directory
if [ $events_flag -eq 1 ]; then
kul_e2cl " Copying task based events.tsv to BIDS directory" $log
cp Study_config/task-*_events.tsv $bids_output &
fi
# make a full .cvs file with final dicom tag info of all subjects
kul_e2cl " Making a full .cvs file with final dicom tag info of all subjects" $log
csv_all_subjects=$log_dir/ALL_subjects_dicom_info.csv
csv_single_subject=${preproc}/log/KUL_dcm2bids.sh/\*_final_dicom_info.csv
#echo "cat $csv_single_subject > $csv_all_subjects"
cat $csv_single_subject > $csv_all_subjects &
kul_e2cl "Finished $script" $log