-
Notifications
You must be signed in to change notification settings - Fork 0
/
UKBwarp_tfMRI.sh
executable file
·205 lines (164 loc) · 4.78 KB
/
UKBwarp_tfMRI.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
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
#!/bin/bash
#
# Script: UKBwarpfMRI
# Purpose: Warp subject-level fMRI results to MNI space
# Author: T. Nichols, F. Alfaro Almagro
# Version: 1.0
#
###############################################################################
#
# Environment set up
#
###############################################################################
shopt -s nullglob # No-match globbing expands to null
Tmp=/tmp/`basename $0`-${$}-
trap CleanUp INT
# Check for essential env var
if [[ "$FSLDIR" == "" || "$UKB_SUBJECTS" == "" ]] ; then
echo "ERROR: Key env vars, FSLDIR, UKB_SUBJECTS not all set"
exit 1
fi
# Default interpolation
Interp="spline"
###############################################################################
#
# Functions
#
###############################################################################
Usage() {
cat <<EOF
Usage: `basename $0` [options] <SubjIds> <FeatImg> <DestDir> <JobFile>
Creates a job file, to be used with fsl_sub -t, to convert UK Biobank task fMRI
from subject-space to MNI space.
SubjIds Plain text file listing the subject Ids warp
FeatImg File in a feat directory; e.g. mask or stats/cope1
DestDir Location of where MNI space files should be put; files named:
XXXXXX_tfMRI_<FeatFileNm>_MNI
where FeatFileNm is FeatImg with any directory removed.
JobFile Text file with one applywarp command per line.
Options
-i <Interp> Specify interpolation. Default is "spline"; use "nn" for
masks.
-d SrcDir Source directory where UKB image data is found; defaults to
$UKB_SUBJECTS/subjectsAll
-v <Visit> Imaging visit; defaults to visit 1 (ID prefix 2).
Specify -1 to indicate that SubjIds alredy have visit
prefix.
_________________________________________________________________________
Version 1.0
EOF
exit
}
# Unused usage
# -S SubjUsed.txt
# Search for all possible subjects (as by default) but write
# out list of ID's to SubjUsed.txt; saves time on subsequent runs.
CleanUp () {
/bin/rm -f /tmp/`basename $0`-${$}-*
exit 0
}
ApplyWarpJob() {
local SrcDir="$1"
local Subj="$2"
local FeatFile="$3"
local Interp="$4"
local DestDir="$5"
local FileNm="$(basename "$FeatFile")"
echo applywarp \
-i $SrcDir/$Subj/fMRI/tfMRI.feat/"$FeatFile" \
-r $FSLDIR/data/standard/MNI152_T1_2mm \
-o "$DestDir"/${Subj}_tfMRI_"${FileNm}"_MNI \
-w $SrcDir/$Subj/fMRI/tfMRI.feat/reg/example_func2standard_warp \
--interp="$Interp"
}
###############################################################################
#
# Parse arguments
#
###############################################################################
Vis=1
while (( $# > 1 )) ; do
case "$1" in
"-help")
Usage
;;
"-i")
shift
Interp="$1"
shift
;;
"-v")
shift
Vis="$1"
shift
;;
"-d")
shift
SrcDir="$1"
shift
;;
-*)
echo "ERROR: Unknown option '$1'"
exit 1
break
;;
*)
break
;;
esac
done
if (( $# != 4 )) ; then
Usage
fi
if (( Vis == -1 )) ; then
VisID=""
else
VisID=$((Vis+1))
fi
if [ "$SrcDir" == "" ] ; then
if [ "$UKB_SUBJECTS" == "" ] ; then
echo "ERROR: UKB_SUBJECTS not defined & SrcDir not set with -d"
exit 1
fi
SrcDir="$UKB_SUBJECTS/subjectsAll"
fi
# Make sure no trailing slash
SrcDir="${SrcDir%/}"
if [ 0 == 1 ] ; then
# Code to find all... just not practical
SubjIds=${Tmp}SubjId
# find all subjects with task fMRI
find -L "$SrcDir" \
-maxdepth 3 \
\! -readable -prune -o \
-path "*/fMRI/tfMRI.feat" -print \
| sed "s@${SrcDir}/@@;s@/fMRI/tfMRI.feat@@" \
> $SubjIds
# When generalising to other filters:
# For a filter "*/fMRI/tfMRI.feat" also set maxdepth to correspond to the
# depth of the filter (this isn't vital, but speeds it up dramatically.
if [ "$SubjIdsSv" != "" ] ; then
cp "$SubjIds" "$SubjIdsSv"
fi
else
SubjIds="$1"
fi
nSubj=$(cat $SubjIds | wc -l)
FeatFile="${2%.nii.gz}"
DestDir="$3"
JobFile="$4"
cp /dev/null "$JobFile"
for ((i=1;i<=nSubj;i++)) ; do
Subj=$(sed -n ${i}p $SubjIds)
if [ -f $SrcDir/${VisID}$Subj/fMRI/tfMRI.feat/"$FeatFile".nii.gz ] ; then
if [ -f $SrcDir/${VisID}$Subj/fMRI/tfMRI.feat/reg/example_func2standard_warp.nii.gz ] ; then
ApplyWarpJob "$SrcDir" "${VisID}$Subj" "$FeatFile" "$Interp" "$DestDir" >> "$JobFile"
fi
fi
done
###############################################################################
#
# Exit & Clean up
#
###############################################################################
CleanUp