-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcm2bids.sh
77 lines (56 loc) · 2.4 KB
/
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
#!/bin/bash
#---------------- HELP ----------------#
help() {
echo "
1st argument = top level directory
This script will convert your structural DICOM images into NIFTI and organize them into the BIDS. It requires you to specify the folder where your 'DICOM' folder is, and that every directory within that folder is named by the ID of the corresponding subject (e.g. 01, 02, 03, [...]). It further expects a standard DICOM outcome, like:
> DICOM
>> 01
>>> 1800 (or whatever)
>>> DICOM
>>>>> * .dcm files*
It should also be mentioned that it uses the dcm2niix pipeline, so you should have that installed.
### This script is based on: https://github.com/franklin-feingold/BIDS-Example-NKI-RS-Multiband-Imaging ###
"
}
# # # # # # # # # # # # Variables # # # # # # # # # # # # #
toplvl=$1
dcmdir=${toplvl}/DICOM
niidir=${toplvl}/NIFTI
# -----------------------------------------------------------------------------------------------#
## checking if the input is valid
if [[ -d $1 ]]; then
echo "$1 is a directory"
else
echo "$1 is not valid"
help
exit 1
fi
## create NIFTI dir
cd $toplvl
mkdir NIFTI
## Labeling
for direcs in $(ls ${dcmdir}); do
id=`echo $direcs`;
echo "[INFO] ID $id created at `date`" >> $toplvl/dcm2bids_log.txt
## Create NIFTI directories
mkdir -p ${niidir}/sub-$id/anat;
echo "[INFO] Directory sub-$id/anat created in $niidir at `date` " >> $toplvl/dcm2bids_log.txt
## Convert atatomical Dicoms 2 NIFTI and put them in the corresponding folders
dcm2niix -o ${niidir}/sub-$id/anat -f sub-$id_%f_%p ${dcmdir}/*${id}*/**/DICOM;
echo "[INFO] DICOM images of sub-$id (stored in $dcmdir/*${id}*/**/DICOM) converted into NIFTI and stored in $niidir/sub-$id/anat, at `date` " >> $toplvl/dcm2bids_log.txt
#change direc
cd ${niidir}/sub-$id
# Rename stuctural NIFTI files
anatfiles=$(ls -1 ${niidir}/**/**/*FSPGR_BRAVO* | wc -l)
for ((i=1;i<=${anatfiles};i++)); do
Anat=$(ls ${niidir}/**/**/*FSPGR_BRAVO*)
tempanat=$(ls -1 $Anat | sed '1q;d')
tempanatext="${tempanat##*.}"
tempanatfile="${tempanat%.*}"
mv ${tempanatfile}.${tempanatext} sub-${id}_ses-1_T1w.nii.${tempanatext}
echo -e "[INFO] ${tempanat} changed to sub-${id}_T1w.${tempanatext}" >> $toplvl/dcm2bids_log.txt
done
mv ${niidir}/sub-$id/*_T1w* ${niidir}/sub-$id/anat
echo "[INFO] ${niidir}/sub-$id/*_T1w* moved to ${niidir}/sub-$id/anat" >> $toplvl/dcm2bids_log.txt
done