-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqueezebricate.sh
More file actions
215 lines (174 loc) · 5.1 KB
/
Copy pathsqueezebricate.sh
File metadata and controls
215 lines (174 loc) · 5.1 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
#!/usr/bin/env bash
set -euo pipefail
### defaults
IN_ABR="card.tsv"
OUT="squeezebricate_out.tsv"
SQUEEZE_DIR="."
UNMATCHED_FILE="squeezebricate_unmatched.tsv"
usage() {
cat >&2 <<'EOF'
Welcome to SqueezeBricate :)
This wrapper adds a TAXON column (derived from SqueezeMeta) to Abricate TSV output.
Usage: squeezebricate_cached.sh -i <abricate.tsv> -o <out.tsv> -s </path/to/your/SqueezeMeta/working/dir> [-u <unmatched_file>]
Options:
-i Abricate results TSV (default: card.tsv)
-o Output TSV (default: squeezebricate_out.tsv)
-s Path to SqueezeMeta working directory (default: ".")
-u File to write unmatched hits (default: squeezebricate_unmatched.tsv)
-h Show help
EOF
exit 2
}
while getopts ":i:o:s:u:h" opt; do
case "${opt}" in
i) IN_ABR="${OPTARG}" ;;
o) OUT="${OPTARG}" ;;
s) SQUEEZE_DIR="${OPTARG}" ;;
u) UNMATCHED_FILE="${OPTARG}" ;;
h) usage ;;
\?) echo "ERROR: Invalid option -${OPTARG}" >&2; usage ;;
:) echo "ERROR: Option -${OPTARG} requires an argument." >&2; usage ;;
esac
done
if [[ ! -r "${IN_ABR}" ]]; then
echo "ERROR: ${IN_ABR} not found or not readable." >&2
exit 1
fi
SQUEEZE_DIR="$(printf '%s' "$SQUEEZE_DIR" | sed 's#//*#/#g')"
if [[ ! -d "${SQUEEZE_DIR}" ]]; then
echo "ERROR: Input directory '${SQUEEZE_DIR}' not found." >&2
exit 1
fi
command -v gawk >/dev/null 2>&1 || {
echo "ERROR: gawk is required for this optimized wrapper." >&2
exit 1
}
mkdir -p "$(dirname "$OUT")" 2>/dev/null || true
: > "$OUT"
: > "$UNMATCHED_FILE"
SQUEEZE_BASE="$(dirname "$(dirname "$SQUEEZE_DIR")")"
awk -v FS='\t' -v OFS='\t' '
NR==1 || $0 ~ /^#/ {
line=$0
print line
next
}
{ exit }
' "$IN_ABR" >> "$OUT"
printf "SAMPLE\tCONTIG\tSTART\tEND\tGENE\tREASON\tORIGINAL_ROW\n" >> "$UNMATCHED_FILE"
TMP_SORTED="$(mktemp)"
awk -v FS='\t' -v OFS='\t' '
NR==1 || $0 ~ /^#/ { next }
{
s=$1
sub(/\/.*/,"",s)
print s, $0
}
' "$IN_ABR" | sort -t $'\t' -k1,1 > "$TMP_SORTED"
gawk -v FS='\t' -v OFS='\t' \
-v out="$OUT" \
-v unmatched="$UNMATCHED_FILE" \
-v squeeze_dir="$SQUEEZE_DIR" \
-v squeeze_base="$SQUEEZE_BASE" '
function pick_taxfile(sample,f1,f2,probe) {
f1 = squeeze_dir "/06." sample ".fun3.tax.wranks"
f2 = squeeze_base "/" sample "/results/06." sample ".fun3.tax.wranks"
if ((getline probe < f1) > 0) { close(f1); return f1 }
close(f1)
if ((getline probe < f2) > 0) { close(f2); return f2 }
close(f2)
return ""
}
function reset_cache() {
delete cnt
delete st
delete en
delete tx
}
function load_taxfile(sample,taxfile,p,ps,id,parts,n_parts,c,range_part,r,rs,re,n_tax,taxbits,tax) {
taxfile = pick_taxfile(sample)
if (taxfile == "") {
# Warn once per sample
print "WARN: tax file not found for sample=" sample " (tried under " squeeze_dir " and " squeeze_base ")" > "/dev/stderr"
return 0
}
while ((getline p < taxfile) > 0) {
split(p, ps, FS)
id = ps[1]
# contig parsing matches original: first two '_' parts
n_parts = split(id, parts, "_")
if (n_parts < 3) continue
c = parts[1] "_" parts[2]
# range is last '_' part like "123-456"
range_part = parts[n_parts]
split(range_part, r, "-")
rs = r[1] + 0
re = r[2] + 0
# taxon is last '_' part of column 2
n_tax = split(ps[2], taxbits, "_")
tax = taxbits[n_tax]
cnt[c]++
st[c, cnt[c]] = rs
en[c, cnt[c]] = re
tx[c, cnt[c]] = tax
}
close(taxfile)
return 1
}
function write_unmatched(sample, contig, start, end, gene, reason, orig, j) {
orig = $2
for (j = 3; j <= NF; j++) orig = orig OFS $j
print sample, contig, start, end, gene, reason, orig >> unmatched
}
BEGIN {
cur_sample = ""
loaded = 0
n_missing_tax = 0
n_no_match = 0
}
{
sample_key = $1
if (sample_key != cur_sample) {
reset_cache()
loaded = load_taxfile(sample_key)
cur_sample = sample_key
}
contig = $3
start = $4 + 0
end = $5 + 0
gene = $7 # original $6
if (!loaded) {
n_missing_tax++
write_unmatched(cur_sample, contig, start, end, gene, "missing_taxfile")
next
}
# tolerance is <=50 bp on either end
done = 0
n = cnt[contig]
for (i = 1; i <= n; i++) {
rs = st[contig, i]
re = en[contig, i]
if (rs <= start || (rs - start) <= 50) {
if (end <= re || (end - re) <= 50) {
tax = tx[contig, i]
out_line = $2 OFS $3 OFS $4 OFS $5 OFS $6
out_line = out_line OFS (tax ";" $7)
for (f = 8; f <= NF; f++) out_line = out_line OFS $f
print out_line >> out
done = 1
break
}
}
}
if (!done) {
n_no_match++
write_unmatched(cur_sample, contig, start, end, gene, "no_match")
}
}
END {
print "INFO: unmatched (missing_taxfile)=" n_missing_tax ", unmatched (no_match)=" n_no_match > "/dev/stderr"
}
' "$TMP_SORTED"
rm -f "$TMP_SORTED"
echo "Wrote: $OUT" >&2
echo "Wrote: $UNMATCHED_FILE" >&2