-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter_ids
executable file
·198 lines (188 loc) · 5.29 KB
/
filter_ids
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
#!/bin/bash
set -e
# ------------------------------------------------------------------------------
# usage
# ------------------------------------------------------------------------------
function usage() {
echo -e "\n\tusage:\t\tfilter_ids <opts>"
echo -e "\n\tdescription:\tfilter ids from a csv file and return the row indices"
echo -e "\n\toptions:\t"
echo -e "\t\t--study <study>\t\tstudy name"
echo -e "\t\t--sub <sub>\t\tsubject id"
echo -e "\t\t--ses <ses>\t\tsession id"
echo -e "\t\t--scan <scan>\t\tscan id"
echo -e "\t\t--airc-id <airc-id>\tairc id"
echo -e "\t\t--idx-only <0|1>\treturn row indices only (default: 0)"
echo -e "\t\t-h|--help\t\ttdisplay help message"
echo ""
}
# ------------------------------------------------------------------------------
# args
# ------------------------------------------------------------------------------
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
# defaults
idx_only=0
vars=""
while [[ $# -gt 0 ]]; do
case "$1" in
--study)
study=$2
if [[ -z ${study} ]]; then
usage
error_msg "study value not specified"
exit 1
elif [[ ${study} == "all" ]]; then
study=""
fi
vars="${vars} study"
shift 2
;;
--sub)
sub=$2
if [[ -z ${sub} ]]; then
usage
error_msg "sub value not specified"
exit 1
elif [[ ${sub} == "all" ]]; then
sub=""
fi
vars="${vars} sub"
shift 2
;;
--ses)
ses=$2
if [[ -z ${ses} ]]; then
usage
error_msg "ses value not specified"
exit 1
elif [[ ${ses} == "all" ]]; then
ses=""
fi
vars="${vars} ses"
shift 2
;;
--scan)
scan=$2
if [[ -z ${scan} ]]; then
usage
error_msg "scan value not specified"
exit 1
elif [[ ${scan} == "all" ]]; then
scan=""
fi
vars="${vars} scan"
shift 2
;;
--airc-id)
airc_id=$2
if [[ -z ${airc_id} ]]; then
usage
error_msg "airc_id value not specified"
exit 1
elif [[ ${airc_id} == "all" ]]; then
airc_id=""
fi
vars="${vars} airc_id"
shift 2
;;
--idx-only)
idx_only=$2
if [[ -z ${idx_only} ]]; then
usage
error_msg "idx_only value not specified"
exit 1
fi
if [[ ${idx_only} -ne 0 ]] && [[ ${idx_only} -ne 1 ]]; then
# echo -e "\n\tinvalid idx_only value: ${idx_only}"
usage
error_msg "invalid idx_only value (${idx_only}), must be 0 or 1"
exit 1
fi
vars="${vars} idx_only"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo -e "\n\tunknown option: $1"
usage
exit 1
;;
esac
done
# ------------------------------------------------------------------------------
# paths
# ------------------------------------------------------------------------------
root_dir=`get_root_dir kenrod`
in_ids="${root_dir}/software/scripts/eep170030/ids_long-format_study-all.csv"
# ------------------------------------------------------------------------------
# check paths
# ------------------------------------------------------------------------------
check_in_paths ${in_ids}
# ------------------------------------------------------------------------------
# main
# ------------------------------------------------------------------------------
# Read the column names from the header line
header=$(head -n 1 "$in_ids")
# Extract the desired column names dynamically
study_col=$(echo "${header}" | awk -F, '{ for (i = 1; i <= NF; i++) if ($i == "study") print i }')
subj_col=$(echo "${header}" | awk -F, '{ for (i = 1; i <= NF; i++) if ($i == "sub") print i }')
ses_col=$(echo "${header}" | awk -F, '{ for (i = 1; i <= NF; i++) if ($i == "ses") print i }')
scan_col=$(echo "${header}" | awk -F, '{ for (i = 1; i <= NF; i++) if ($i == "scan") print i }')
airc_id_col=$(echo "${header}" | awk -F, '{ for (i = 1; i <= NF; i++) if ($i == "airc_id") print i }')
# Read the CSV file and filter lines based on arguments
if [[ ${idx_only} == 0 ]]; then
awk \
-F, \
-v study=${study} \
-v subj=${sub} \
-v ses=${ses} \
-v scan=${scan} \
-v airc_id=${airc_id} \
-v study_col=${study_col} \
-v subj_col=${subj_col} \
-v ses_col=${ses_col} \
-v scan_col=${scan_col} \
-v airc_id_col=${airc_id_col} \
' \
NR > 1 && \
($study_col == study || study == "") && \
($subj_col == subj || subj == "") && \
($ses_col == ses || ses == "") && \
($scan_col == scan || scan == "") && \
($airc_id_col == airc_id || airc_id == "") \
{
print FNR - 1 "," $0;
}
' "${in_ids}"
elif [[ ${idx_only} -eq 1 ]]; then
row_numbers=`awk \
-F, \
-v study="${study}" \
-v subj="${sub}" \
-v ses="${ses}" \
-v scan="${scan}" \
-v airc_id="${airc_id}" \
-v study_col="${study_col}" \
-v subj_col="${subj_col}" \
-v ses_col="${ses_col}" \
-v scan_col="${scan_col}" \
-v airc_id_col="${airc_id_col}" \
' \
NR > 1 && \
($study_col == study || study == "") && \
($subj_col == subj || subj == "") && \
($ses_col == ses || ses == "") && \
($scan_col == scan || scan == "") && \
($airc_id_col == airc_id || airc_id == "") \
{
print FNR - 1;
}
' "$in_ids" | tr '\n' ' ' | sed 's/ /,/g' | sed 's/,$//g'`
echo "${row_numbers}"
fi