-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnifmri_prep00_organize
executable file
·209 lines (189 loc) · 6.6 KB
/
lnifmri_prep00_organize
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
#!/bin/bash
#
# LNiF MRI Lab Pipeline for preprocessing of
# structural and functional MRI dataset
# [email protected] - 20200310
#
# Organize raw data in a convenient directory structure.
# We recommend dcm2niix for DICOM to NIFTI conversion with
# the following flags:
#
# dcm2niix -f %f_%p_%i_%t_%s -b y -ba n -z y <FOLDER NAME>
#
########################################################################
export LC_ALL=C
shopt -s extglob
Usage() {
printf "\n***************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "\tRequires:\n\t\t--\n"
printf "\tUsage:\n\t\tlnifmri_prep00_organize -o outdir\n"
printf "\tArgs:\n"
printf "\t\t-o:\tspecify name of main output directory;\n\n"
printf "\te.g.:\tlnif_preproc_00_organize -o my_project_root"
printf "\n\n(c) [email protected], 2020.\n"
printf "***************************************************\n\n"
exit 1
}
Start() {
stamp=`date +%Y%m%d_%H%M%S`
printf "\n***************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "Create directory structure\n"
printf "\n\tOutput Directory: $OUTDIR\n\n"
}
NextStep() {
printf "\n****************************************************\n"
printf "LNiF MRI Lab Preprocessing Pipeline\n"
printf "CIMeC - Center for Mind/Brain Science\n\n"
printf "Complete. Exec. Time: ${exectime}\nNow cd to $OUTDIR\nand run lnifmri_prep01_anat\n"
printf "\n++++++++++++++++ [ HINT ] ++++++++++++++++++++\n"
printf "To save time, run:\n\tcd <my_project_root>\n\tlnifmri_prep01_anat &\n\tlnifmri_prep02_preproc"
printf "\n\n****************************************************\n\n"
exit 1
}
[ "$1" = "" ] && Usage
# Set default directories
SUBDIR=""
SUBPATH=""
DIRNAME=""
OUTDIR=""
ANATDIR="anat"
FUNCDIR="func"
BOLDDIR="bold"
RESTDIR="rest"
DWIDIR="dwi"
SDCDIR="sdc"
TOFDIR="tof"
ASLDIR="asl"
SWIDIR="swi"
MISCDIR="misc"
QCDIR="qc"
subcount=0
DATAPATH=`pwd`
#echo $DATAPATH
#Get name of output directory
while getopts o: option
do
case "${option}" in
o) DIRNAME=${OPTARG};;
esac
done
#Set fullpath of DIRNAME
OUTDIR=$DATAPATH/$DIRNAME
LOGDIR=$DATAPATH/$DIRNAME/logs
exectime=0
SECONDS=0
stamp=`date +%Y%m%d_%H%M%S`
Start
#DICOM conversion
for DCMDIR in `cat lnifmri_prep_dcmlist.txt` ; do
printf "${DCMDIR}: DICOM to NIFTI conversion\n"
#check if current directory is an output dir from lnifmri_prep;
#if not, run the DCM to NII conversion of image files
if [ -f "${DCMDIR}/lnifmri_prep_output.txt" ]; then
printf "\t${DCMDIR}: not a raw DICOM folder.\n"
else
dcm2niix -f %f_%p_%t_%s_%d -b y -ba n -z y ${DCMDIR}
sleep 1
printf "LNiF MRI Lab Preprocessing Pipeline\nDo not remove this file!\nCreated: ${stamp}" >> ${DCMDIR}/lnifmri_prep_output.txt
mv ${DCMDIR} ${DCMDIR}.sub
printf "%s\n" "${DCMDIR}.sub" >> lnifmri_prep_sublist.txt
fi
done
printf "\n\nConversion complete. Setting up the environment \n\n\n"
#Prepare the output directory tree
#for SUBRAW in *.sub ; do
#for SUBRAW in `cat lnifmri_prep_sublist.txt` ; do
for SUBPATH in `cat lnifmri_prep_sublist.txt` ; do
#SUBPATH=$DATAPATH/$SUBRAW
#echo $SUBPATH
printf -v SUBDIR -- 'sub-%02d' "$(( ++subcount ))"
#echo $SUBDIR
mkdir -p -m 777 \
$OUTDIR/$SUBDIR \
$OUTDIR/$SUBDIR/$ANATDIR \
$OUTDIR/$SUBDIR/$FUNCDIR \
$OUTDIR/$SUBDIR/$DWIDIR \
$OUTDIR/$SUBDIR/$SDCDIR \
$OUTDIR/$SUBDIR/$TOFDIR \
$OUTDIR/$SUBDIR/$ASLDIR \
$OUTDIR/$SUBDIR/$SWIDIR \
$OUTDIR/$SUBDIR/$MISCDIR;
#copy dataset into the corresponding directory
for f in $SUBPATH/*.{nii,nii.gz,json,bval,bvec} ; do
# for f in $SUBPATH/*.* ; do
#extract basename
u=`basename $f`
# fix invalid characters
d=`echo ${u} | sed -e 's/[^A-Za-z0-9._-]/_/g'`
#printf "$f\n"
#printf "$d\n\n"
case "${d}" in
*MEmprage*_e[1-9]*)
printf "Copy echo data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$MISCDIR/$d
;;
*bold*)
printf "Copy func data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$FUNCDIR/$d
;;
*fMRI*) #GE specific
printf "Copy func data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$FUNCDIR/$d
;;
*t1*)
printf "Copy anat data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$ANATDIR/$d
;;
*mask*)
printf "Copy mask data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$ANATDIR/$d
;;
*T1*) #GE specific 2022-06-07
printf "Copy anat data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$ANATDIR/$d
;;
*t2*)
printf "Copy T2w data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$MISCDIR/$d
;;
*T2*)
printf "Copy T2w data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$MISCDIR/$d
;;
*MSHELL*)
printf "Copy diff data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$DWIDIR/$d
;;
*mshell*)
printf "Copy diff data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$DWIDIR/$d
;;
*distcorr*)
printf "Copy sdc data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$SDCDIR/$d
;;
*field_mapping*)
printf "Copy sdc data\t${d}\n"
cp $f $OUTDIR/$SUBDIR/$SDCDIR/$d
;;
esac
done
done
mkdir -p -m 777 $OUTDIR/$QCDIR
duration=$SECONDS
printf -v exectime "$(($duration / 60))m:$(($duration % 60))s."
rm -rf dcmlist.txt
# Create filelist for functional processing
ls -d1 ${OUTDIR}/sub-*/${FUNCDIR}/*.nii.gz > ${LOGDIR}/lnifmri_prep_ep2dlist.txt
# Create filelist for anatomical processing
ls -d1 ${OUTDIR}/sub-*/${ANATDIR}/*.nii.gz | grep -v 'mask' > ${LOGDIR}/lnifmri_prep_anatlist.txt
# ls -d1 ${OUTDIR}/sub-*/${ANATDIR}/*mp2rage*.nii.gz > ${LOGDIR}/lnifmri_prep_mp2ragelist.txt
# Create filelist for diffusion processing
# ls -d1 ${OUTDIR}/sub-*/${DWIDIR}/*.nii.gz > ${OUTDIR}/lnifmri_prep_dwilist.txt
# Create filelist for VBM analysis
ls -d1 ${OUTDIR}/sub-*/${ANATDIR}/*.nii.gz | grep -v 'mask' > ${OUTDIR}/filelist_VBM_template.txt
NextStep