This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Count the number of sequences in fasta file using perl

I want to write a code that takes file input from the user in the command line and gives an output of the number of sequences in the input file, Total Length of all the sequence, Max sequence length, Min sequence length and Average length of the sequence.

fasta perl bioperl

Hello koushikayaluri ,

and what have you done so far? Where you run in trouble and need our help?

fin swimmer

1 answer

You can do many things in UNIX command line using Perl and "wc".

Counting the number of sequences:

grep '>' INPUT.fasta | wc -l

Counting the total length of all sequences:

grep -v '>' INPUT.fasta | perl -pe 's/[\n\r]//g' | wc -c

List of all sequence lengths:

cat INPUT.fasta |\
perl -pe '/^>/ ? s/.*// : s/[\n\r]//g' |\
tail -n +2 |\
perl -pe 's/.*/length($&)/e'

Log in to answer this question.