forked from BrightLab-ANVIL/PreProc_BRAIN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.PreProc_VolReg-4D
executable file
·118 lines (104 loc) · 4.55 KB
/
x.PreProc_VolReg-4D
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
#!/bin/sh
# x.PreProc_VolReg-4D
# Uses 3dvolreg to register a 4D image to a reference image and saves motion parameters
if [ $# -ne 5 ]
then
echo "**************************************************************************************"
echo "Insufficient arguments supplied"
echo "Input 1 should be the full path to the 4D input scan*"
echo "Input 2 should be the full path to reference scan* (this can be the same as input 1)"
echo "Input 3 should be the volume number of reference scan"
echo "Input 4 should be the full path to the output directory"
echo "Input 5 should be 1 (yes) or 0 (no) to output motion derivatives and quadratic terms"
echo "*Do not include file extension - assumes nii.gz"
echo "**************************************************************************************"
exit
fi
#define inputs
input_file=${1}
ref_file=${2}
ref_vol=${3}
output_dir=${4}
quad_deriv=${5}
#Check input 5 to output motion derivatives and quadratic terms
if [ ${quad_deriv} != 0 ] && [ ${quad_deriv} != 1 ]; then
echo "*****************************"
echo "Input 5 is unknown... exiting"
echo "*****************************"
exit
fi
#Check the input file exists
echo "***************************"
echo "Input file is ${input_file}"
echo "***************************"
if [ ! -f "${input_file}.nii.gz" ]
then
echo "************************************************"
echo "Cannot locate the NIFTI input file ${input_file}"
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 "********************************"
#Get the input filename without the path
input_prefix="$(basename -- $input_file)"
#Create a symbolic link of the original dataset in the output directory
if [ ! -f "${output_dir}/${input_prefix}_ORIG.nii.gz" ]
then
ln -s "${input_file}.nii.gz" "${output_dir}/${input_prefix}_ORIG.nii.gz"
fi
# Volume Registration
if [ ! -f ${output_dir}/${input_prefix}_mc.nii.gz ]
then
echo "****************************************************"
echo "Running volume registration on ${input_prefix}.nii.gz"
echo "****************************************************"
#Run volume registration (AFNI)
3dvolreg -dfile ${output_dir}/${input_prefix}_mc.1D \
-base ${ref_file}.nii.gz[${ref_vol}] \
-maxdisp1D ${output_dir}/${input_prefix}_mc_maxdisp.1D \
-prefix ${output_dir}/${input_prefix}_mc.nii.gz \
${input_file}.nii.gz
#Demean the saved motion parameters
# 0: roll; 1: pitch; 2: yaw; 3: dS (z); 4: dL (x); 5: dP (y)
if [ ! -f ${output_dir}/${input_prefix}_mc_demean.1D ]
then
echo "*******************************************"
echo "Demeaning and saving the motion parameters!"
echo "*******************************************"
1d_tool.py -infile ${output_dir}/${input_prefix}_mc.1D[1..6] \
-demean -write ${output_dir}/${input_prefix}_mc_demean.1D
fi
#Calculate the motion derivatives and quadratic terms, if requested
if [ "${quad_deriv}" -eq 1 ]; then
echo "*************************************************"
echo "Making derivative and quadratic motion regressors"
echo "*************************************************"
# Output temporal derivatives of motion regressor
1d_tool.py -infile ${output_dir}/${input_prefix}_mc.1D[1..6] \
-derivative -write ${output_dir}/${input_prefix}_mc_deriv.1D
# Output quadratics (squared) of motion regressor
1dcat `1deval -1D: -a ${output_dir}/${input_prefix}_mc.1D[1] -expr 'a*a'` \
`1deval -1D: -a ${output_dir}/${input_prefix}_mc.1D[2] -expr 'a*a'` \
`1deval -1D: -a ${output_dir}/${input_prefix}_mc.1D[3] -expr 'a*a'` \
`1deval -1D: -a ${output_dir}/${input_prefix}_mc.1D[4] -expr 'a*a'` \
`1deval -1D: -a ${output_dir}/${input_prefix}_mc.1D[5] -expr 'a*a'` \
`1deval -1D: -a ${output_dir}/${input_prefix}_mc.1D[6] -expr 'a*a'` > ${output_dir}/${input_prefix}_mc_quad.1D
# Demean the derivative and quadratic motion regressor
1d_tool.py -infile ${output_dir}/${input_prefix}_mc_deriv.1D \
-demean -write ${output_dir}/${input_prefix}_mc_deriv_demean.1D
1d_tool.py -infile ${output_dir}/${input_prefix}_mc_quad.1D \
-demean -write ${output_dir}/${input_prefix}_mc_quad_demean.1D
fi
else
echo "**************************************************************"
echo "Volume registration already complete for ${input_prefix}.nii.gz"
echo "**************************************************************"
fi