-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumseq
45 lines (41 loc) · 987 Bytes
/
sumseq
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
#!/usr/bin/env bash
set -e
helpstr="SUMSEQ - print total length of all sequences in FASTA file
Usage: headgrep FILE
Options:
-h: Print this help message and exit."
unset file
while [ $OPTIND -le $# ]; do
while getopts "h" opt; do
case "$opt" in
h)
echo "$helpstr"
exit
;;
*)
echo "Invalid argument:" $opt >&2
echo "$helpstr"
exit 1
esac
done
if [[ -z "$file" ]]; then
file=${@:$OPTIND:1}
OPTIND=$(expr $OPTIND + 1)
fi
done
shift $((OPTIND-1))
# Argument errors and warnings:
if [[ -z $file ]]; then
>&2 echo "Error: Input file required."
echo "$helpstr"
exit 1
fi
# Script:
headchar=$(awk '/./{print;exit}' $file | cut -c1)
if [[ "$headchar" == ">" ]]; then
grep -v ">" $file | awk '{sum+=length($1)}END{print sum}'
else
>&2 echo "Error: input file must be in FASTA format."
exit 1
fi
exit