-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_template
executable file
·164 lines (142 loc) · 6.96 KB
/
build_template
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
#!/bin/bash
usage(){
echo "The template creator is a group of shell scripts to build a population brian template. At this point, the scripts are able to build only DTI templates."
echo "Usage: $(basename $0) <Data main folder path>"
echo ""
echo "Data main folder path = The path where is a structured folder with all the subjects for the template creation. Please, set the folder as:"
echo "Data main folder path"
echo " -> subject 1 folder"
echo " -> DTI DICOM folder 1 (all the DICOM images that compose one single DTI acquisition)"
echo " -> DTI DICOM folder 2"
echo " -> ..."
echo " -> DTI DICOM folder N"
echo " -> subject 2 folder"
echo " -> DTI DICOM folder 1"
echo " -> DTI DICOM folder 2"
echo " -> ..."
echo " -> DTI DICOM folder N"
echo " -> subject 2 folder"
echo " -> DTI DICOM folder 1"
echo " -> DTI DICOM folder 2"
echo " -> ..."
echo " -> DTI DICOM folder N"
}
if [[ $# -eq 0 ]]; then
usage
exit
fi
#Checking if the input folder exist
if [[ ! -d $1 ]]; then
echo "ERROR: The input folder path does not exist!"
exit
fi
# Cheking the programs that these scripts depends
source config/dependecy_check
#Selecting and listing the folders passed to build the brain template.
source config/folder_list $1
#Turn the folders name in anonymous style
source config/anonym_subjects $1
# Saving configuration file in log folder
cat config/config_var.conf > $1/log/configuration_setup.log
NUM_SUBJ=0
for subj in `ls $1 | grep "subj*"`; do
echo "***********************************************************************"
echo "*********** DTI normalization from $subj initiated ************"
echo "***********************************************************************"
echo ""
echo "*** Initial process to create a brain template ***"
echo ""
# Apply dcm2nii to create the nii files from the DICOM FOLDERS.
# If you data is already in Nifti format, change the config_var.conf IS_NIFTI variable.
# Starting to select each subject to do all the process.
IS_NIFTI=`cat config/config_var.conf | grep IS_NIFTI= | cut -c10-11`
source config/dcm2nii $1 $subj $IS_NIFTI
# nii folder has all the images to process
NII_FOLDER=$1/$subj/nii
# Split the pre processing procedures to each acquisition per time. Less consumption of time per volume.
NUM_ACQ=0
for acq in `ls $NII_FOLDER | grep .nii.gz`; do
echo "Step 2: Pre processing DWIs volumes: Brain extraction and eddy correction - Folder: `echo ${acq}`"
# Show the parameters choosen: registration (flirt -> DOF (affine, 6 dof, 3 dof), fnirt (warpsize, fhwmin, fwhmref, subsamp, ))
# Read conf file with the variables values.
echo " --> Creating brain mask..."
BET_THR=`cat config/config_var.conf | grep BET_THR= | cut -c9-12`
NECK_RM=`cat config/config_var.conf | grep NECK_RM= | cut -c9-10`
source preprocessing/brain_extraction $NII_FOLDER $acq $BET_THR $NECK_RM
echo " --> Eddy current correction procedure..."
HIGH_PERF=`cat config/config_var.conf | grep HIGH_PERF= | cut -c11-12`
source preprocessing/eddy_correction $NII_FOLDER $acq $HIGH_PERF
((NUM_ACQ++))
done
echo "Step 3: Registration procedure:"
if [[ $NUM_ACQ -eq 1 ]]; then
mv $NII_FOLDER/*_eddy.nii.gz $NII_FOLDER/dti_ec_corr_mean.nii.gz
else
echo " --> Intrasubject registration"
DOF=`cat config/config_var.conf | grep DOF | cut -c5-6`
INTERP=`cat config/config_var.conf | grep INTERP | cut -c8-28`
source preprocessing/reg_intrasubj $NII_FOLDER $DOF $INTERP
fi
echo " --> Spatial normalization - Registration with MNI template"
MNI=`cat config/config_var.conf | grep MNI= | cut -c5-20`
BET_THR=`cat config/config_var.conf | grep BET_THR= | cut -c9-12`
WARP=`cat config/config_var.conf | grep WARP= | cut -c6-12`
INFWHM=`cat config/config_var.conf | grep INFWHM= | cut -c8-11`
REFFWHM=`cat config/config_var.conf | grep REFFWHM= | cut -c9-12`
SUBSAMP=`cat config/config_var.conf | grep SUBSAMP= | cut -c9-17`
INTERP=`cat config/config_var.conf | grep INTERP | cut -c8-28`
source preprocessing/reg_standard $NII_FOLDER $MNI $BET_THR $WARP $INFWHM $REFFWHM $SUBSAMP $INTERP
echo "***********************************************************************"
echo "*** DTI normalization from $subj terminated with success.***"
echo "***********************************************************************"
echo ""
echo ""
((NUM_SUBJ++))
done
echo "Step 4: Merge all subjects to build a final brain template"
REG_ADJUSTMENT=`cat config/config_var.conf | grep REG_ADJUSTMENT= | cut -c16-17`
# MNI=`cat config/config_var.conf | grep MNI= | cut -c5-20`
if [[ `ls $1 | grep template` == "" ]]; then
mkdir $1/template
fi
for subj in `ls $1 | grep subj`; do
cp $1/$subj/nii/dti_ec_corr_mean_nlin.nii.gz $1/template
# TODO Add vector_norm here to calculate the mean gradient file...USAR O SCRIPT PARA PELO MENOS FAZER A COPIA...DEPOIS PENSAR EM COMO TERMINAR
echo " --> Correcting gradient directions..."
source postprocessing/vector_norm $1/$subj/nii/ $subj
# STOP=0
# if [[ STOP -lt 1 ]]; then
# cp $1/$subj/nii/acq*1.bvec $1/template
# cp $1/$subj/nii/acq*1.bval $1/template
# mv $1/template/acq*1.bvec $1/template/gradient.bvec
# mv $1/template/acq*1.bval $1/template/bvalues.bval
# ((STOP++))
# fi
mv $1/template/dti_ec_corr_mean_nlin.nii.gz $1/template/dti_$subj.nii.gz
done
source postprocessing/merge_temp $1/template $NUM_SUBJ $REG_ADJUSTMENT
BUILD_MORE_MAPS=`cat config/config_var.conf | grep BUILD_MORE_MAPS= | cut -c17-18`
if [[ $BUILD_MORE_MAPS == "Y" ]]; then
DTI_MAP_VR=`cat config/config_var.conf | grep DTI_MAP_VR= | cut -c12-13`
DTI_MAP_RA=`cat config/config_var.conf | grep DTI_MAP_RA= | cut -c12-13`
DTI_MAP_PERPD=`cat config/config_var.conf | grep DTI_MAP_PERPD= | cut -c15-16`
echo " Appendix Step: Creating additional DTI maps namely Relative Anisotropy ($DTI_MAP_RA), Volume Ratio ($DTI_MAP_VR) and Perpendicular Diffusivity ($DTI_MAP_PERPD)"
source postprocessing/additional_maps $1/template $DTI_MAP_VR $DTI_MAP_RA $DTI_MAP_PERPD
fi
CLEAN_TRASH=`cat config/config_var.conf | grep CLEAN_TRASH= | cut -c13-14`
if [[ $CLEAN_TRASH == "Y" ]]; then
echo " Appendix Step: Cleaning subfile folders and trash files..."
for subj in `ls $1`; do
if [[ `ls $subj | grep nii` != "" ]]; then
echo " --> Cleaning folder: $subj"
rm -R $1/$subj/nii
fi
done
fi
echo "***********************************************************************"
echo "***********************************************************************"
echo "!!!!!!!!!!!!!!!!!!!! DTI template created !!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "***********************************************************************"
echo "***********************************************************************"
echo ""
echo ""