Count missing characters in fasta files with a shell one-liner

One of the best things about working with fasta and phylip files is they are relatively simple file formats and thus are easy to parse with command-line tools. There are certainly a lot of negatives to these file types but it is handy for certain types of tasks.

I needed to find out how much missing data were in our fasta and phylip multiple sequence alignments. As an alpha programmer, writing a one-off Python program is extremely lame and would certainly not give me any cred for being “teh hardc0rez”. Instead, I decided to do it all in the Terminal. Join me, as I build up a small shell pipeline to count missing characters.

To count the total number of characters in a file:

wc -c file.fasta

Count the total number of sequence characters in a file:

grep -v "^>" file.fasta | wc -c

Do this for all sequence files:

find . -name "*.fasta" -exec sh -c 'grep -v "^>" "$1" | wc -c' -- {} \;

…and add them up:

find . -name "*.fasta" -exec sh -c 'grep -v "^>" "$1" | wc -c' -- {} \; | paste -s -d+ - | bc

Count the number of gap characters (-) in a file:

fgrep -o - file.fasta | wc -c

Get the proportion of missing data (gaps divided by total number of characters) in a file:

(fgrep -o - file.fasta | wc -c && grep -v "^>" file.fasta | wc -c) | paste -s -d/ - | bc -

Do the previous, but for all files in the current directory:

find . -name "*.fasta" -exec sh -c '(fgrep -o - "$1" | wc -c && grep -v "^>" "$1" | wc -c) | paste -s -d/ - | bc -l' -- {} \;

Add up the number of missing characters for all files in a certain directory:

find folder/ -name "*.fasta" -exec sh -c 'fgrep -o - "$1" | wc -c' -- {} \; | paste -s -d+ - | bc

Add up the number of missing characters for all files in all directories in the current directory, in comma-separated format:

while read f; do echo -n $f,; find $f -name "*.fasta" -exec sh -c 'fgrep -o - "$1" | wc -c' -- {} \; | paste -s -d+ - | bc; done < <(find . -depth 1 -type d)

Other variations are left as an exercise to the reader. I hope this has been an enjoyable journey through the wonderful world of shell pipelines.