-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBIDSto3col.sh
More file actions
executable file
·307 lines (256 loc) · 7.74 KB
/
BIDSto3col.sh
File metadata and controls
executable file
·307 lines (256 loc) · 7.74 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
#
# Script: BIDSto3col.sh
# Purpose: Convert a BIDS event TSV file to a 3 column FSL file
# Author: T Nichols t.e.nichols@warwick.ac.uk
# Version: 1.2 5 September 2016
#
# Extension to give created 3 column files
ThreeColExt="txt"
# Replace slashes in event names with this character.
SlashReplace=""
# To avoid headaches with spaces in filenames, replace spaces in event names
# with this character.
SpaceReplace="_"
# Event type column
TypeNm="trial_type"
# Awk header; a command to protect against DOS/Windows carriage returns
AwkHd='{sub(/\r$/,"")};'
# Temporal shift; value to be *subtracted* from each onset
ShiftSec=0
###############################################################################
#
# Environment set up
#
###############################################################################
shopt -s nullglob # No-match globbing expands to null
Tmp=/tmp/`basename $0`-${$}-
trap CleanUp INT
###############################################################################
#
# Functions
#
###############################################################################
Usage() {
cat <<EOF
Usage: `basename $0` [options] BidsTSV OutBase
Reads BidsTSV and then creates 3 column event files, one per event
type if a "$TypeNm" column is found. Files are named as OutBase
and, if "$TypeNm" is present, appended with the event name.
By default, all rows and event types are used, and the height value
(3rd column) is 1.0.
Options
-s Even if "$TypeNm" column is found, ignore and process
all rows as a single event type.
-e EventName Instead of all event types, only use the given event
type (no event name appended to OutBase).
-h HtColName Instead of using 1.0, get height value from given
column; two files are written, the unmodulated (with
1.0 in 3rd column) and the modulated one, having a
"_pmod" suffix.
-d DurColName Instead of getting duration from the "duration"
column, take it from this named column.
-t TypeColName Instead of getting trial type from "trial_type"
column, use this column.
-b Sec Shift onset times backwards, subtracting specified
value (in seconds) from each onset. (Useful when
initial acquistions are discarded).
-N By default, when creating 3 column files any spaces
in the event name are replaced with "$SpaceReplace";
use this option to suppress this replacement.
EOF
exit
}
CleanUp () {
/bin/rm -f /tmp/`basename $0`-${$}-*
exit 0
}
FailIfEmpty() {
local Val="$1" ValNm="$2"
if [ "$Val" = "" ] ; then
echo "ERROR: Empty value given for $ValNm"
CleanUp
fi
}
###############################################################################
#
# Parse arguments
#
###############################################################################
while (( $# > 1 )) ; do
case "$1" in
"-help")
Usage
;;
"-s")
shift
AllEvents=1
;;
"-e")
shift
EventNm="$1"
NoAppend=1
shift
FailIfEmpty "$EventNm" "event name (-e option)"
;;
"-h")
shift
HeightNm="$1"
shift
FailIfEmpty "$HeightNm" "height column name (-h option)"
;;
"-d")
shift
DurNm="$1"
shift
FailIfEmpty "$DurNm" "duration column name (-d option)"
;;
"-t")
shift
TypeNm="$1"
shift
FailIfEmpty "$TypeNm" "trial type column name (-t option)"
;;
"-N")
shift
NoSpaceRepl=1
;;
"-b")
shift
ShiftSec="$1"
FailIfEmpty "$ShiftSec" "seconds shift (-b option)"
shift
;;
-*)
echo "ERROR: Unknown option '$1'"
exit 1
break
;;
*)
break
;;
esac
done
if (( $# < 2 )) ; then
Usage
fi
TSV="$1"
OutBase="$2"
###############################################################################
#
# Script Body
#
###############################################################################
# Validate TSV
if [ ! -f "$TSV" ] ; then
echo "ERROR: Cannot find '$TSV'."
CleanUp
fi
# Validate shift
if [ $(echo "$ShiftSec" | grep -E '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' | wc -l ) = 0 ] ; then
echo "ERROR: Shift in seconds '$ShiftSec' is not a valid number."
CleanUp
fi
# Duration column
if [ "$DurNm" = "" ] ; then
DurCol=2
else
# Validate duration column name
DurCol=$( awk -F'\t' "$AwkHd"'(NR==1){for(i=1;i<=NF;i++){if($i=="'"$DurNm"'")print i}}' "$TSV")
if [ "$DurCol" = "" ] ; then
echo "ERROR: Column '$DurNm' not found in TSV file."
CleanUp
fi
fi
# Validate trial_type column name
if [ "$AllEvents" = 1 ] ; then
NoAppend=1
else
TypeCol=$( awk -F'\t' "$AwkHd"'(NR==1){for(i=1;i<=NF;i++){if($i=="'"$TypeNm"'")print i}}' "$TSV")
if [ "$TypeCol" = "" ] ; then
echo "WARNING: Column '$TypeNm' not found in TSV file; using all rows."
AllEvents=1
NoAppend=1
fi
fi
if [[ "$AllEvents" = "1" ]] ; then
EventNms=("*")
else
# Get all event names (need to loop to handle spaces)
awk -F'\t' "$AwkHd"'(NR>1){print $'"$TypeCol"'}' "$TSV" | sort | uniq > ${Tmp}AllEv
nEV=$(cat ${Tmp}AllEv | wc -l)
EventNms=()
for ((i=1;i<=nEV;i++)); do
EventNms[i-1]="$(sed -n ${i}p ${Tmp}AllEv)"
done
# Validate requested event name
if [ "$EventNm" != "" ] ; then
Fail=1
for ((i=0;i<${#EventNms[*]};i++)) ; do
if [ "${EventNms[i]}" = "$EventNm" ] ; then
Fail=0
break
fi
done
if [ $Fail = 1 ] ; then
echo "ERROR: Event type '$EventNm' not found in TSV file."
CleanUp
fi
EventNms=("$EventNm")
fi
fi
# Validate height column name
if [ "$HeightNm" != "" ] ; then
HeightCol=$( awk -F'\t' "$AwkHd"'(NR==1){for(i=1;i<=NF;i++){if($i=="'"$HeightNm"'")print i}}' "$TSV")
if [ "$HeightCol" = "" ] ; then
echo "ERROR: Column '$HeightNm' not found in TSV file."
CleanUp
fi
fi
for E in "${EventNms[@]}" ; do
if [ "$NoAppend" = 1 ] ; then
App=""
else
E="$(echo "$E" | sed 's@/@'$SlashReplace'@g')"
if [ "$NoSpaceRepl" = 1 ] ; then
App="_${E}"
else
App="_$(echo "$E" | sed 's/ /'$SpaceReplace'/g')"
fi
fi
Out="${OutBase}${App}.${ThreeColExt}"
OutHt="${OutBase}${App}_pmod.${ThreeColExt}"
echo -n "Creating '$Out'"
if [ "$AllEvents" = "1" ] ; then
AwkSel="(NR>1)"
else
AwkSel='$'"$TypeCol"'~/^'"$E"'$/'
fi
awk -F'\t' "$AwkHd""$AwkSel"'{printf("%s %s 1.0\n",$1-('"$ShiftSec"'),$'"$DurCol"')}' "$TSV" > "$Out"
# Validate duration values (if non-standard duration used)
if [ "$DurNm" != "" ] ; then
Nev="$(cat "$Out" | wc -l)"
Nnum="$(awk '{print $2}' "$Out" | grep -E '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' | wc -l )"
if [ "$Nev" != "$Nnum" ] ; then
echo " WARNING: Event '$E' has non-numeric durations from '$DurNm'"
fi
fi
if [ "$HeightNm" != "" ] ; then
echo " & '$OutHt'"
awk -F'\t' "$AwkHd""$AwkSel"'{printf("%s %s %s\n",$1-('"$ShiftSec"'),$'"$DurCol"',$'"$HeightCol"')}' "$TSV" > "$OutHt"
# Validate height values
Nev="$(cat "$OutHt" | wc -l)"
Nnum="$(awk '{print $3}' "$OutHt" | grep -E '^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$' | wc -l )"
if [ "$Nev" != "$Nnum" ] ; then
echo " WARNING: Event '$E' has non-numeric heights from '$HeightNm'"
fi
else
echo
fi
done
###############################################################################
#
# Exit & Clean up
#
###############################################################################
CleanUp