-
Notifications
You must be signed in to change notification settings - Fork 0
/
uf-map
executable file
·110 lines (89 loc) · 3.57 KB
/
uf-map
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
#!/bin/sh
#
# uf-map - Apply an operation to all sequences in an unfasta stream.
# Copyright (C) 2016 Marco van Zwetselaar <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This utility is part of http://io.zwets.it/unfasta
# Function to exit this script with an error message on stderr
err_exit() {
echo "$(basename "$0"): $*" >&2
exit 1
}
# Function to show usage information and exit
usage_exit() {
echo "
Usage: $(basename $0) [OPTIONS] OPERATION [FILE ...]
Feed every sequence in FILE to OPERATION's standard input. When no FILE
is present or FILE is '-', read from standard input. Write to standard
output the output of OPERATION applied to each sequence, interspersed
with every header from the input. With option -t apply OPERATION to the
sequence titles instead, passing the sequence data straight through.
OPTIONS
-t, --titles map OPERATION over the sequence headers instead of of data
-h, --help this help
Note that OPERATION is started once, not invoked separately for each line.
It must process every line coming in on its standard input separately.
Example
\$ uf-map 'tr [:upper:] [:lower:]' # convert all seqs to lower case
\$ uf-map 'sed -re s,^.{12},,' # drop initial 12 elements from each
" >&2
exit ${1:-1}
}
# Parse options
unset TITLES
while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do
case $1 in
-t|--title*) TITLES=1 ;;
-h|--help) usage_exit 0 ;;
*) usage_exit ;;
esac
shift || usage_exit
done
# Parse arguments
[ $# -ge 1 ] || usage_exit
OPERATION="$1"
shift
# Making this work produced two nice false starts: a race and a deadlock
# FAIL 1: using pipe inside gawk
# Fails because of race: external OPERATION is buffering its output,
# so lines do not come out in the right order, not even with flush.
# gawk -bO -v OPER="sh -c '$OPERATION'" '
# NR%2==1 { print }
# NR%2==0 { print | OPER }
# { fflush() }' "$@"
# FAIL 2: using a FIFO between uf-bare and uf-dress
# Fails because of deadlock: uf-bare blocks on FIFO until uf-dress consumes,
# but uf-dress is waiting for standard input before consuming from FIFO.
# FIFO="/tmp/$(basename "$0").$$" && mkfifo "$FIFO"
# ./uf-bare -f -w "$FIFO" | ./uf-dress -r "$FIFO"
# ATTEMPT 3: using paste to merge OPERATION output and FIFO
# Set pipefail to have the errorcode of the rightmost failing filter (OPERATION)
[ -z "$BASH" ] || set +o pipefail
# Make a FIFO to hold the straight-through lines, from which paste will merge them with OPERATION output
FIFO="/tmp/$(basename "$0").$$"
mkfifo "$FIFO"
# The pipeline splits the lines between FIFO and OPERATION, then paste merges hem
if [ -z "$TITLES" ]; then LHS="$FIFO"; RHS="-"; else LHS="-"; RHS="$FIFO"; fi
gawk -bO -v FIFO="$FIFO" -v ONE="${TITLES:-0}" '
(NR+ONE)%2==1 { print > FIFO };
(NR+ONE)%2==0' "$@" \
| $OPERATION \
| paste -d '\n' "$LHS" "$RHS"
# Hold on to RETVAL as rm will reset exit status
RETVAL=$?
rm -f $FIFO
exit $RETVAL
# vim: sw=4:ts=4:ai:si:et