Skip to content

Instantly share code, notes, and snippets.

@klmr
Last active December 10, 2015 18:38
Show Gist options
  • Save klmr/4475683 to your computer and use it in GitHub Desktop.
Save klmr/4475683 to your computer and use it in GitHub Desktop.
Count nucleotides in a sequence (from file or standard input).
#!/usr/bin/env bash
set -e
set -u
input="${1-/dev/stdin}"
declare -A counts=(
[A]=0
[C]=0
[G]=0
[T]=0)
while read line < "$input"; do
for nuc in A C G T; do
counts[$nuc]=$((${counts[$nuc]} +
$(sed "s/[^$nuc]//g" <<< "$line" | xargs echo -n | wc -c)))
done
done
for nuc in ${!counts[@]}; do
echo "$nuc ${counts[$nuc]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment