Skip to contentSkip to navigationFull graphics

Converting GenBank files to FASTA format with Biopython

This page follows on from dealing with GenBank files in BioPython and shows how to use the GenBank parser to convert a GenBank file into a FASTA format file. See also this example of dealing with Fasta Nucelotide files.

As before, I'm going to use a small bacterial genome, Nanoarchaeum equitans Kin4-M (RefSeq NC_005213, GI:38349555, GenBank AE017199) which can be downloaded from the NCBI here:

As you can see, the NCBI provide this file in GenBank format, and preconverted into assorted FASTA formats. This document shows how to do this for yourself, which has the advantage that you can choose what to put into the FASTA title lines.

Note that the NCBI's genomic GenBank files (like the one above) only contain a single large record. However, for maximum flexibility, the example code should cope with multi-record GenBank files.

Record by Record : GenBank to FASTA Nucleotides (*.gbk to *.fna)

Simple sequence file format between supported file formats is very easy using Bio.SeqIO - assuming you are happy with its default choices! This bit of code will record the full DNA nucleotide sequence for each record in the GenBank file as a fasta record:

from Bio import SeqIO
SeqIO.convert("NC_005213.gbk", "genbank", "NC_005213_converted.fna", "fasta")

For comparison, in this next version ( [Python Code]) we construct the FASTA file "by hand" giving full control:

from Bio import SeqIO
gbk_filename = "NC_005213.gbk"
faa_filename = "NC_005213_converted.fna"
input_handle  = open(gbk_filename, "r")
output_handle = open(faa_filename, "w")

for seq_record in SeqIO.parse(input_handle, "genbank") :
    print "Dealing with GenBank record %s" %
    output_handle.write(">%s %s\n%s\n" % (


These examples should all give basically the same output, with a single FASTA record for each GenBank record. In this case NC_005213.gbk only has one very large record, so you only get one FASTA record. Note that I have added the line breaks shown here:

>NC_005213 Nanoarchaeum equitans Kin4-M, complete genome.

Gene by Gene : GenBank to FASTA Amino Acids (*.gbk to *.faa)

As before, the suggested input file only has a single GenBank record (as a SeqRecord object), but the code below will cope with multiple GenBank records - each with multiple features (as SeqFeature objects). You can download this example too: [Python Code].

from Bio import SeqIO
gbk_filename = "NC_005213.gbk"
faa_filename = "NC_005213_converted.faa"
input_handle  = open(gbk_filename, "r")
output_handle = open(faa_filename, "w")

for seq_record in SeqIO.parse(input_handle, "genbank") :
    print "Dealing with GenBank record %s" %
    for seq_feature in seq_record.features :
        if seq_feature.type=="CDS" :
            assert len(seq_feature.qualifiers['translation'])==1
            output_handle.write(">%s from %s\n%s\n" % (


This results in one FASTA record for each CDS feature in each GenBank record. I have added line breaks to the sequence below to display this better:

>NEQ001 from NC_005213
>NEQ003 from NC_005213
>NEQ550 from NC_005213

Its easy enough to adapt the code if you wanted to output the protein names for example, or GI numbers.

Note that not all GenBank CDS features include the translation. In a related example, for viruses, the mat_peptide (mature peptide) features don't include the protein sequence either. In such cases, you can first extract the nucleotide sequence (see below) and then translate it to get the amino acids.

Gene by Gene : GenBank to FASTA Nucleotides (*.gbk to *.ffn)

I've saved this one till last, because it was the hardest. However, as described in the preceding document, Biopython 1.53 adds a new extract method to the SeqFeature object. This means you don't have to deal with anything complicated like "joins" or "fuzzy" positions explicitly.

I still need to find the time to sit down and expand this example to use the new method...

[Python logo]

[Biopython logo]

[NCBI logo]

Back to the top of the page


Full graphics | Sign in

© MMXVIII  |  Privacy  |  Cookies  |  Accessibility