I have a multifasta file containing several genes from a genomic island. I want to know the total length of this island. How can I do this?

  • 3
    $\begingroup$ What have you tried? How many sequences do you have? How are the position of each sequence stored in your multifasta or elsewhere? $\endgroup$
    – llrs
    Commented Nov 15, 2017 at 14:47
  • $\begingroup$ Most tools that calculate genome assembly summary stats will report total length. I use assembly-stats $\endgroup$ Commented Nov 15, 2017 at 15:06

2 Answers 2

grep -v ">" multi.fasta | tr -d '\n' | wc -c
  • grep -v ">" find all non header lines (i.e. without ">")
  • tr -d '\n' delete all newlines
  • wc -c count all characters

Here’s a function that I keep around for calculating the individual lengths of fasta sequences

awk '/^>/ {if (seqlen){print seqlen}; print ;seqlen=0;next; } { seqlen += length($0)}END{print seqlen}' $1

And one I use for summing a column of numbers

awk '{ sum += $1 } END { print sum}'

You can combine these by piping the output to do the sum for you:

falens myseqs.fa | grep -v “>” | awk '{ sum += $1 } END { print sum}'

(Can’t test this myself currently as I’m not at my laptop, but it’s based on 2 functions that work normally)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.