-
Notifications
You must be signed in to change notification settings - Fork 0
/
uf
executable file
·93 lines (79 loc) · 3.18 KB
/
uf
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
#!/bin/bash
#
# uf - Convert FASTA to unfasta format
# 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
# "Hardened bash"
set -euo pipefail
# 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 ...]
Convert FILEs to unfasta format and write to standard output. FILE must
contain optionally gzipped FASTA data. With no FILE or when FILE is '-',
read standard input. Unfasta is FASTA without the pesky line breaks.
OPTIONS
-r, --revert Revert unfasta to FASTA (see below)
-w, --width=W Revert unfasta to FASTA with line length W (see below)
-l, --lower Write lowercase
-u, --upper Write uppercase
Apart from checking that every sequence is preceded by a header, uf performs
no validation of the input data. See uf-valid.
Options -r and -w revert sequences to classic FASTA by breaking lines at W
(default 60) characters. Note though that strictly there is no need to do
this, as every unfasta file is also a valid FASTA file. The 60 char line
length is a recommendation, not a requirement.
More information about the unfasta suite at http://io.zwets.it/unfasta.
" >&2
exit ${1:-1}
}
# Parse options
REVERT=0 WIDTH=60 LOWER=0 UPPER=0 RET=0
#while (( $# )) && "$(expr "$1" : '\(.\)..*')" = "-" ]; do
while (( $# )) && [[ -z "${1%%-?*}" ]]; do
case $1 in
-l|--l*) LOWER=1 ;;
-u|--u*) UPPER=1 ;;
-r|--r*) REVERT=1 ;;
--w*=*) WIDTH=${1##*=}; REVERT=1 ;;
-w|--w*) shift && WIDTH=$1; REVERT=1 ;;
-h|--h*) usage_exit 0 ;;
*) usage_exit ;;
esac
shift || usage_exit
done
[ $# -eq 0 ] && DASH="-" || DASH=""
for F in $DASH "$@"; do
gzip -dfc "$F" |
if (( $REVERT )); then
gawk -bO -v WIDTH=$WIDTH -v L=$LOWER -v U=$UPPER '
/^>/
/^[^>]/ { for (i=1;i<=length();i+=WIDTH) {
S=substr($0,i,WIDTH); print (L ? tolower(S) : U ? toupper(S) : S)
} }' '-'
else
gawk -bO -v L=$LOWER -v U=$UPPER 'BEGIN { ORS=""; D=0; H=0 }
/^>/ && (H||D) { print "\n" }
/^>/ { print $0 "\n"; D=0; H=1 }
/^[^>]/ && !(H||D) { print "uf: input is not valid FASTA\n" >"/dev/stderr"; exit 1 }
/^[^>]/ { gsub(/[[:space:]]/,""); print (L ? tolower($0) : U ? toupper($0) : $0); D=1; H=0 }
END { if (D||H) print "\n" }' '-'
fi
done | tr -d '\r'
# vim: sts=4:sw=4:et:si:ai