-
Notifications
You must be signed in to change notification settings - Fork 0
/
uf-freqs
executable file
·89 lines (80 loc) · 2.58 KB
/
uf-freqs
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
#!/bin/sh
#
# uf-freqs - Count frequencies in unfasta files
# 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 ...]
Count frequencies of elements in each sequence in each unfasta FILE and write
to standard output. If no FILE is present or when FILE is '-', read standard
input.
Options
-b, --bare Do not output the headers, only the data
-l, --length-only Length of sequence only, no frequencies per element
-t, --totals Write totals across all sequences at bottom
-i, --ignore-case Count all lower case letters as upper case
" >&2
exit ${1:-1}
}
# Parse options
BARE=0
LENGTH_ONLY=0
TOTALS=0
IGNORE_CASE=0
while [ $# -ne 0 -a "$(expr "$1" : '\(.\)..*')" = "-" ]; do
case $1 in
-b|--b*) BARE=1 ;;
-l|--l*) LENGTH_ONLY=1 ;;
-t|--t*) TOTALS=1 ;;
-i|--i*) IGNORE_CASE=1 ;;
-h|--h*) usage_exit 0 ;;
*) usage_exit ;;
esac
shift || usage_exit
done
gawk -bO -v P="$(basename "$0")" -v BARE=$BARE -v LENGTH_ONLY=$LENGTH_ONLY -v TOTALS=$TOTALS -v IGNORE_CASE=$IGNORE_CASE '
BEGIN { PROCINFO["sorted_in"] = "@ind_str_asc" }
NR%2==1 && !BARE
NR%2==0 {
if (IGNORE_CASE) $0 = toupper($0)
for (i=1; i<=length($0); ++i) freqs[substr($0,i,1)] += 1
printf length($0)
if (!LENGTH_ONLY) for (j in freqs) printf " " j "=" freqs[j]
printf "\n"
if (TOTALS) {
for (j in freqs) totals[j] += freqs[j]
grand_total += length($0)
}
for (j in freqs) freqs[j] = 0
}
END {
if (TOTALS) {
if (!BARE) print ">TOTALS"
printf grand_total;
if (!LENGTH_ONLY) for (j in totals) printf " " j "=" totals[j]
printf "\n"
}
}
' "$@"
# vim: sts=4:sw=4:et:si:ai