Introduction to Bioinformatics

Lesson 4 of 16 · 10 min

The FASTA format: how sequences are stored

In bioinformatics, a sequence is the exact order of the building blocks inside a molecule of DNA, RNA, or protein, written out as a string of letters. DNA, for example, is built from four building blocks called nucleotides, each shown as a single letter: A, C, G, or T. Before a computer can work with a sequence, it has to be saved in a file, and the most common way to do that is a format called FASTA.

The parts of a record

A FASTA file is plain text, which means it contains nothing but ordinary keyboard characters and can be opened almost anywhere. Its basic unit is a record: a single header line that begins with a greater-than sign, written ">", followed by one or more lines that hold the sequence itself. To look inside a file we will use the terminal, the window where you type commands to your computer, and run cat, a command that prints a file's contents to the screen.

bash
cat dna.fasta
>gene1 hypothetical protein, partial cds
ATGGCACGTGATTACAAGGATGACGACGATAAGGGTTTACCCGATACCGGAAGCTAGCTA
GCTAGCTAGCATCGATCGGATCCGGAATTCAAATGA

The first line is the header, because it starts with ">"; here the identifier is gene1 and the words after it are a free-text description. Every line below the header is sequence. When a sequence is long it is usually wrapped across several lines of 60 to 80 characters so it fits on screen, but reading the lines one after another gives one continuous sequence.

Try it yourself: Look at the dna.fasta record above. Which single line is the header, and which lines are the sequence? What is the identifier written right after the ">"? (Answer: the header is the one line that starts with ">", its identifier is gene1, and the two lines beneath it are the sequence.)

Many records in one file

Most FASTA files hold many records stacked one after another, which is called a multi-FASTA file. The same format works for nucleotide sequences like DNA and for protein sequences, whose letters stand for amino acids, the building blocks of proteins. You usually get these files by downloading them from public databases such as NCBI or UniProt, straight off a DNA sequencing machine, or from a colleague who shares one. Because such files can be long, we will peek at just the top of one using head, a command that prints only the first few lines of a file; the -n 4 option asks it for the first four lines, which here is exactly two records.

bash
head -n 4 proteins.fasta
>sp|P69905|HBA_HUMAN Hemoglobin subunit alpha
MVLSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHFDLS
>sp|P68871|HBB_HUMAN Hemoglobin subunit beta
MVHLTPEEKSAVTALWGKVNVDEVGGEALGRLLVVYPWTQRFFESFGDLS

Warning: never open or edit a FASTA file in a word processor like Microsoft Word. Word processors silently insert invisible formatting characters and curly quotation marks that break the programs that read FASTA. Always use a plain text editor instead, such as Notepad, TextEdit in plain-text mode, VS Code, or nano.