-
Notifications
You must be signed in to change notification settings - Fork 0
/
uf-sort
executable file
·74 lines (63 loc) · 2.32 KB
/
uf-sort
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
#!/bin/sh
#
# uf-sort - Sort sequences in an unfasta file
# 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] [FILE ...]
Sort the sequences in unfasta FILEs in order of decreasing length, and
write to standard output. If no FILE is present or when FILE is '-',
read standard input. Length ties are broken alphabetically.
OPTIONS
-r, --reverse Reverse the order of the sort
" >&2
exit ${1:-1}
}
# Parse options
unset REVERSE
while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do
case $1 in
-r|--reverse) REVERSE="-r" ;;
-h|--help) usage_exit 0 ;;
*) usage_exit ;;
esac
shift || usage_exit
done
# Do the work
# We need to sort on length, then alphabet, and also need to keep each header
# together with its sequence. The solution is to first turn every sequence
# into a single line having fields length <> sequence data <> header (where
# header really is anything from field 3 to the end).
# Then we sort, and then we unpack everything again.
# Pre-process into single records
awk -v OFS='\t' '
NR % 2 == 1 { HDR = $0; }
NR % 2 == 0 { print length(), $0, HDR }
' "$@" |
# Sort in order
#LC_ALL=C sort $REVERSE --buffer-size=1G --key='1rn,2' -t ' ' - |
LC_ALL=C sort $REVERSE --key='1rn,2' -t "$(printf '\t')" - |
# And unpack again - let's hope there are no tabs in headers ...
awk -F '\t' '{ print $3; print $2; }'
# vim: sts=4:sw=4:et:si:ai