-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathx.PreProc_TissueReg_func2stand.sh
executable file
·139 lines (120 loc) · 6.07 KB
/
x.PreProc_TissueReg_func2stand.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
#!/bin/sh
# x.PreProc_TissueReg_func2stand.sh
# Registers an image in functional space to standard space
# Outputs are transformation matrices for anat2stand, stand2anat, func2stand, and stand2func
# Uses FNIRT in FSL (nonlinear)
# Must have already run x.PreProc_TissueReg_func2anat.sh to get func2anat transformation matrix
# Currently works only for 2mm MNI space. To use with a different standard, will have to find a different --config file to use with FNIRT
if [ $# -ne 8 ]
then
echo "********************************************************************************************************"
echo "Insufficient arguments supplied"
echo "Input 1 should be the full path to the func2anat transformation matrix"
echo "Input 2 should be the full path to the anat input scan (brain extracted)"
echo "Input 3 should be the full path to the anat input scan (NOT brain extracted)"
echo "Input 4 should be the full path to the standard input scan, ex. MNI152_T1_2mm_brain (brain extracted)"
echo "Input 5 should be the full path to the standard input scan, ex. MNI152_T1_2mm (NOT brain extracted)"
echo "Input 6 should be the full path to the standard brain mask, ex. MNI152_T1_2mm_brain_mask_dil"
echo "Input 7 should be the full path to the output directory"
echo "Input 8 should be the subject ID"
echo "*Note: do not include file extension - assumes nii.gz and .mat"
echo "********************************************************************************************************"
exit
fi
#define inputs
input_file_func2anat=${1}
input_file_anat_brain=${2}
input_file_anat=${3}
input_file_stand_brain=${4}
input_file_stand=${5}
input_file_stand_mask=${6}
output_dir=${7}
subject=${8}
#Check the input files exist
echo "*******************************************************"
echo "Input files are ${input_file_func2anat} and ${input_file_anat_brain} and ${input_file_anat} and ${input_file_stand_brain} and ${input_file_stand} and ${input_file_stand_mask}"
echo "*******************************************************"
if [ ! -f "${input_file_func2anat}.mat" ]
then
echo "***************************************************"
echo "Cannot locate the transformation matrix ${input_file_func2anat}"
echo "...exiting!"
echo "***************************************************"
exit
fi
if [ ! -f "${input_file_anat_brain}.nii.gz" ]
then
echo "***************************************************"
echo "Cannot locate the NIFTI input file ${input_file_anat_brain}"
echo "...exiting!"
echo "***************************************************"
exit
fi
if [ ! -f "${input_file_anat}.nii.gz" ]
then
echo "***************************************************"
echo "Cannot locate the NIFTI input file ${input_file_anat}"
echo "...exiting!"
echo "***************************************************"
exit
fi
if [ ! -f "${input_file_stand_brain}.nii.gz" ]
then
echo "***************************************************"
echo "Cannot locate the NIFTI input file ${input_file_stand_brain}"
echo "...exiting!"
echo "***************************************************"
exit
fi
if [ ! -f "${input_file_stand}.nii.gz" ]
then
echo "***************************************************"
echo "Cannot locate the NIFTI input file ${input_file_stand}"
echo "...exiting!"
echo "***************************************************"
exit
fi
if [ ! -f "${input_file_stand_mask}.nii.gz" ]
then
echo "***************************************************"
echo "Cannot locate the NIFTI input file ${input_file_stand_mask}"
echo "...exiting!"
echo "***************************************************"
exit
fi
#If output directory is not present, make it
if [ ! -d ${output_dir} ]
then
mkdir ${output_dir}
fi
echo "********************************"
echo "Output directory is ${output_dir}"
echo "********************************"
#Registration from func to stand
if [ ! -f ${output_dir}/${subject}_func2stand_warp.nii.gz ]
then
echo "*******************************************************************"
echo "Registering ${input_file_anat_brain} to ${input_file_stand_brain} with FSL FNIRT"
echo "*******************************************************************"
# stuctural <-> standard
flirt -in ${input_file_anat_brain}.nii.gz -ref ${input_file_stand_brain}.nii.gz -out ${output_dir}/${subject}_anat2stand -omat ${output_dir}/${subject}_anat2stand.mat -cost corratio -dof 12 \
-searchrx -90 90 -searchry -90 90 -searchrz -90 90 -interp trilinear
fnirt --iout=${output_dir}/${subject}_anat2stand_head --in=${input_file_anat}.nii.gz --aff=${output_dir}/${subject}_anat2stand.mat --cout=${output_dir}/${subject}_anat2stand_warp \
--iout=${output_dir}/${subject}_anat2stand --jout=${output_dir}/${subject}_anat2anat_jac --config=T1_2_MNI152_2mm --ref=${input_file_stand}.nii.gz --refmask=${input_file_stand_mask}.nii.gz --warpres=5,5,5
applywarp -i ${input_file_anat_brain}.nii.gz -r ${input_file_stand_brain}.nii.gz -o ${output_dir}/${subject}_anat2stand.nii.gz -w ${output_dir}/${subject}_anat2stand_warp
convert_xfm -inverse -omat ${output_dir}/${subject}_stand2anat.mat ${output_dir}/${subject}_anat2stand.mat
echo "*****************************"
echo "Concatenating with func2anat"
echo "*****************************"
# concatenate: functional <-> MNI standard
convert_xfm -omat ${output_dir}/${subject}_func2stand.mat -concat ${output_dir}/${subject}_anat2stand.mat ${input_file_func2anat}.mat
convertwarp --ref=${input_file_stand_brain}.nii.gz --premat=${output_dir}/${subject}_func2anat.mat --warp1=${output_dir}/${subject}_anat2stand_warp --out=${output_dir}/${subject}_func2stand_warp
echo "*******************************"
echo "Inverting transformation matrix"
echo "*******************************"
convert_xfm -inverse -omat ${output_dir}/${subject}_stand2func.mat ${output_dir}/${subject}_func2stand.mat
else
echo "***********************************************************************"
echo "Registration from func to ${input_file_stand_brain} already completed"
echo "***********************************************************************"
fi