-
Notifications
You must be signed in to change notification settings - Fork 0
/
seqlen
58 lines (54 loc) · 1.4 KB
/
seqlen
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
#!/usr/bin/env bash
set -e
helpstr="SEQLEN - get length of specified sequence from FASTA file, or of all
sequences combined
Usage: headgrep FILE [SEQ_NUM]
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)
elif [[ -z "$seqnum" ]]; then
seqnum=${@:$OPTIND:1}
OPTIND=$(expr $OPTIND + 1)
fi
done # TODO: Fix so doesn't cycle if add extra options
shift $((OPTIND-1))
# Argument errors and warnings:
if [[ -z $file ]]; then
>&2 echo "Error: Input file required."
echo "$helpstr"
exit 1
fi
#if [[ -z $seqnum ]]; then
# >&2 echo "Error: Sequence number required."
# echo "$helpstr"
# exit 1
#fi
# Script:
headchar=$(awk '/./{print;exit}' $file | cut -c1)
if [[ "$headchar" != ">" ]]; then
>&2 echo "Error: input file must be in FASTA format."
exit 1
fi
if [[ -n "$seqnum" ]]; then
echo $(index -xs $seqnum $file 1 len | wc -c)
else
# Join sequence lines together and get total length
echo $(grep -v "$headchar" "$file" | paste -sd "" - | wc -c)
fi
exit