Calculate Sequence Length (fasta)

Sometimes it is essential to know the length distribution of your sequences. It may be your newly assembled scaffolds or it might be a genome, that you wish to know the size of chromosomes, or it could just be any multi fasta sequence file.

1. Using biopython

Save this as a script, make it an executable and run on a fasta file:

from Bio import SeqIO
import sys
cmdargs = str(sys.argv)
for seq_record in SeqIO.parse(str(sys.argv[1]), "fasta"):
 output_line = '%s\t%i' % \
(, len(seq_record))

To run:

chmod +x input_file.fasta

This will print length for all the sequences in that file.

2. Using bioawk

Bioawk is an extension of the awk written by Heng Li. It is available to donwload from this link. Installation is easy too. To get sequence length, run it as:

bioawk -c fastx '{print $name length($seq)}' input.fasta

Output will be similar to the above script and can be redicrected to any file if you want.

More information

Table of contents