-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmotionScrub.sh
More file actions
executable file
·181 lines (138 loc) · 4.53 KB
/
motionScrub.sh
File metadata and controls
executable file
·181 lines (138 loc) · 4.53 KB
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
#!/bin/bash
##################################################################################################################
# Motion Scrubbing (Censoring TRs with too much movement (Power 2012 Neuroimage)
# 1. Scrubbing
# a. 0=No Scrubbing
# b. 1=Motion Scrubbing
# c. 2=No Scrubbing, Motion Scrubbing in parallel
##################################################################################################################
scriptPath=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$0")
scriptDir=$(dirname "$scriptPath")
filename=run_motionscrub.m
function Usage {
echo "Usage: motionScrub.sh -f restingStateImage"
echo " where"
echo " -E resting state image"
echo " *Top-level RestingState.nii.gz image"
echo " -h help"
exit 1
}
########## FSL's arg parsing functions ###################
get_opt1() {
arg=${1//=*/}
echo "$arg"
}
get_imarg1() {
arg=$(get_arg1 "$1");
arg=$("$FSLDIR"/bin/remove_ext "$arg");
echo "$arg"
}
get_arg1() {
if [ X"$(echo "$1" | grep '=')" = X ] ; then
echo "Option $1 requires an argument" 1>&2
exit 1
else
arg=${1//*=/}
if [ X"$arg" = X ] ; then
echo "Option $1 requires an argument" 1>&2
exit 1
fi
echo "$arg"
fi
}
# Parse Command line arguments
if [ $# -lt 1 ] ; then Usage; exit 0; fi
while [ $# -ge 1 ] ; do
iarg=$(get_opt1 "$1");
case "$iarg"
in
--epi)
epiData=$(get_arg1 "$1");
export epiData;
indir=$(dirname "$epiData");
export indir;
rawEpiDir=$(dirname "$(x=$indir; while [ "$x" != "/" ] ; do x=$(dirname "$x"); find "$x" -maxdepth 1 -type f -name "mcImg.nii.gz"; done 2>/dev/null)");
export rawEpiDir
if [ "$epiData" == "" ]; then
echo "Error: The restingStateImage (-E) is a required option"
exit 1
fi
shift;;
-h)
Usage;
exit 0;;
*)
echo "Unrecognised option $1" 1>&2
exit 1
esac
done
##Echo out all input parameters into a log
logDir=$rawEpiDir
{
echo "$scriptPath"
echo "------------------------------------"
echo "-E $epiData"
date
echo ""
echo ""
} >> "$logDir"/rsParams_log
cd "$indir" || exit
gunzip "${epiData}"
echo "Running $0 ..."
cd "$rawEpiDir" || exit
# Extract image dimensions from the NIFTI File
numXdim=$(fslinfo "$epiData" | grep ^dim1 | awk '{print $2}')
numYdim=$(fslinfo "$epiData" | grep ^dim2 | awk '{print $2}')
numZdim=$(fslinfo "$epiData" | grep ^dim3 | awk '{print $2}')
numtimepoint=$(fslinfo "$epiData" | grep ^dim4 | awk '{print $2}')
#### Motion scrubbing ############
echo "...Scrubbing data"
cat > $filename << EOF
% It is matlab script
addpath('${scriptDir}')
niftiScripts=['${scriptDir}','/Octave/nifti'];
addpath(niftiScripts)
funcvoldim=[${numXdim} ${numYdim} ${numZdim} ${numtimepoint}];
motionscrub('${rawEpiDir}','${epiData//.nii.gz/.nii}',funcvoldim)
quit
EOF
# Run script using Matlab or Octave
haveMatlab=$(which matlab)
if [ "$haveMatlab" == "" ]; then
octave --no-window-system "$rawEpiDir/$filename"
else
matlab -nodisplay -r "run $rawEpiDir/$filename"
fi
#################################
cd "${rawEpiDir}"/motionScrub || exit
#### Process Summary ############
echo "...Summarizing Results"
##Want to summarize motion-scrubbing output
echo "ID,total_volumes,deleted_volumes,prop_deleted,resid_vols" > motion_scrubbing_info.txt
fsl_tsplot -i fd.txt -t "FD (mm)" -w 800 -h 300 -u 1 --start=1 -o fd.png
fsl_tsplot -i dvars.txt -t "DVARS" -w 800 -h 300 -u 1 --start=1 -o dvars.png
##Echo out the pertinent info for the motion-scrubbed/processed subjects
numvols=$(fslinfo "${epiData}" | grep ^dim4 | awk '{print $2}')
delvols=$(wc < deleted_vols.txt | awk '{print $2}')
propdel=$(echo "${numvols} ${delvols}" | awk '{print ($2/$1)}')
residvols=$(echo "${numvols} ${delvols}" | awk '{print ($1-$2)}')
echo "${indir},${numvols},${delvols},${propdel},${residvols}" >> motion_scrubbing_info.txt
#Echo out motionscrub info to rsParams file
echo "epiNormMS=${epiData//.nii/_ms.nii}" >> "$rawEpiDir"/rsParams
{
echo "<hr>"
echo "<h2>Motion Scrubbing</h2>"
echo "<b>Total Volumes</b>: $numvols<br>"
echo "<b>Deleted Volumes</b>: $delvols<br>"
echo "<b>Remaining Volumes</b>: $residvols<br>"
} >> "${rawEpiDir}"/analysisResults.html
scrubDataCheck=$(head -1 deleted_vols.txt)
if [[ $scrubDataCheck != "" ]]; then
echo "<b>Scrubbed TR</b>: $(awk '{$1=$1}1' deleted_vols.txt)<br>" >> "${rawEpiDir}"/analysisResults.html
fi
#################################
# clean up gunzipped nifti
gzip "${epiData//.nii.gz/.nii}"
echo "$0 Complete"
echo ""
echo ""