Lesson 5 of 9
Module 6 · Genome assembly
Is the assembly good? Contiguity (how few, how long the pieces), completeness (how much of the genome is there) and correctness (how many errors) are three separate questions.
The three questions in the title need different evidence. Contiguity asks how many pieces the genome is in and how long they are; you can measure it from the contigs alone. Completeness asks how much of the real genome is present. Correctness asks whether the pieces are right, base by base and in the way they are joined. An assembly can score well on one and badly on another, so each needs its own number.
In Short-read assembly with SPAdes you produced five contigs and read their coverage from the headers. This lesson turns that first look into numbers you can report and compare with other assemblies.
N50, by hand
N50 is the length such that contigs of that length or longer contain half the assembly. It rewards a few long contigs over many short ones.
A way to picture it: line up all the contigs from longest to shortest and walk along them, adding up their lengths. The contig you are standing on when the running total first passes half of the whole assembly is the N50 contig, and its length is the N50. Why not use the average contig length instead? Because a handful of tiny contigs, a few hundred bases each, would drag the average down even if almost all the genome sits in a few long pieces. N50 asks where most of the sequence lives, which is the more useful question.
A second example shows the difference clearly. Assembly A has ten contigs of 3,000 bases each, 30,000 in all. Assembly B has one contig of 20,000 bases and ten of 1,000, also 30,000 in all. Both have eleven or fewer contigs and similar average lengths (3,000 and about 2,700), but half of A, 15,000 bases, is only reached at the fifth contig, so its N50 is 3,000, while B's first contig alone passes 15,000, so its N50 is 20,000. B has most of its sequence in one long piece, and N50 says so.
from Bio import SeqIO
lengths = sorted((len(r.seq) for r in SeqIO.parse("spades_out/contigs.fasta", "fasta")), reverse=True)
print(lengths, sum(lengths))
def n50(lengths):
"""Length of the contig at which half the assembly is in contigs this size or longer."""
lengths = sorted(lengths, reverse=True)
half, running = sum(lengths) / 2, 0
for length in lengths:
running += length
if running >= half:
return length
print("N50", n50(lengths))What this does
from Bio import SeqIOloads the sequence reader from Biopython.SeqIO.parse(..., "fasta")reads the contigs file one record at a time, andlen(r.seq)is the length of each contig.sorted(..., reverse=True)puts the lengths in order from longest to shortest. The firstprintshows the list and its sum.- Inside
n50,halfis half the total length andrunningstarts at 0. The loop adds each length in turn and returns the first length at whichrunningreaches or passeshalf. The function sorts again itself, so it gives the right answer even if it is handed an unsorted list.
Reading the output
The first line lists the five contig lengths, longest first, followed by their total, 28,266 bases. These are the same five contigs whose headers you saw in Short-read assembly with SPAdes. The second line is the answer: the N50 is 6,894 bases, the length of the second-longest contig.
Worked example · N50 step by step
7,294 → running 7,294 (not yet half)
+ 6,894 → running 14,188 ≥ 14,133 → N50 = 6,894
The assembly totals 28,266 bases, about 3,700 fewer than the genome: two of the three repeat copies are missing, because they were collapsed into one contig.
What the answer means. Half of this assembly lies in contigs at least 6,894 bases long. Notice how close the running total is to the halfway point: 14,188 − 14,133 = 55 bases. A slightly different assembly could tip N50 to the next contig, so treat small differences in N50 between two assemblies with caution.
A pitfall: the length cut-off. N50 depends on which contigs you count. Dropping very short contigs lowers the total, moves the halfway point and can raise N50 without changing any sequence. QUAST, used below, ignores contigs shorter than 500 bases by default for this reason. When you compare N50 values, make sure they were calculated with the same minimum contig length.
Two related numbers. L50 is how many contigs it took to reach half: here 2. A low L50 and a high N50 say the same thing from two sides. NG50 works like N50 but uses half the expected genome size, not half the assembly, so an assembly cannot improve its score by leaving sequence out. With the true size of 31,950, half is 15,975, which the running total passes at the third contig; the NG50 is therefore 6,189.
QUAST: compared with the truth
QUAST reports contiguity on its own, and correctness and completeness when you give it a reference. In a real project the reference would be a related genome, or you would rely on reference-free checks, described at the end of this lesson and in Genes on your assembly: a first count, and is it complete?.
With a reference, QUAST aligns every contig to it and counts what does not fit. That is how correctness becomes measurable: each disagreement between a contig and the reference is either a small difference at single bases or a large break in how the contig lines up.
quast.py spades_out/contigs.fasta -r data/practice_bacterium.fa -o quast_out grep -E "^# contigs |^Largest contig|^Total length |^N50|^# misassemblies|^Genome fraction|mismatches per" quast_out/report.txt
What this does
quast.pyevaluates the contigs file.-r data/practice_bacterium.fagives it the true genome to compare against, and-o quast_outis the output folder.- QUAST's full report.txt has many lines.
grep -Ekeeps only the lines that match any of the patterns separated by|. The^means "at the start of the line", and the extra spaces after# contigsandTotal lengthstop similar-looking lines from matching too.
Reading the output
- # contigs, Largest contig, Total length and N50 match what you calculated by hand. That agreement is a useful check that your function is right.
- # misassemblies counts places where a contig joins sequence that is not neighbouring in the reference: far apart, on the wrong strand, or on another chromosome. Zero means no contig is wrongly stitched together.
- Genome fraction (%) is the share of the reference covered by aligned contigs: 88.131% of 31,950 bases is about 28,160 bases.
- # mismatches per 100 kbp is the number of single-base disagreements, scaled to 100,000 bases so that genomes of different sizes can be compared. Across this assembly's roughly 28 kb, 7.10 per 100 kb works out to about 2 mismatches in total.
No misassemblies and very few mismatches: the assembly is correct but fragmented, and 88% complete because of the collapsed repeat. That is the typical profile of a short-read bacterial assembly.
Why does QUAST need to align the contigs first? A single mismatch only means something once you know which reference base a contig base corresponds to, and a misassembly only shows up when the two halves of one contig align to places far apart. The comparison is only as good as the reference, though. With a reference from a related strain rather than the true genome, some of the mismatches will be real differences between the strains, not errors in your assembly. Besides report.txt, QUAST writes an HTML report with plots, which is easier to read when you compare several assemblies side by side.
A common mistake is to read the genome fraction as a measure of how much was sequenced. Here every base was sequenced about 59 times; the missing 12% is sequence that was read but could not be placed, because it belongs to the repeat copies that were collapsed.
Go deeper: when a misassembly is not a mistake
By default QUAST calls a join a misassembly when the two sides of a contig align to the reference more than 1,000 bases apart, on opposite strands, or on different reference sequences; smaller shifts are reported separately as local misassemblies. The rule assumes the reference is the true genome, which for the practice data it is.
In a real project the reference is usually a related strain, and strains genuinely differ. An inversion, a stretch of DNA flipped in one strain, makes a perfectly correct contig look misjoined. Sequence present in your isolate but not in the reference, such as a plasmid, does not align and is listed as unaligned, while genes the reference has and your strain lacks lower the genome fraction. Before calling an assembly wrong, check whether long reads, or a second related genome, support the join.
Without a reference: completeness and base accuracy
Most real projects have no true genome to compare with. Two reference-free measures are now reported for almost every new genome, alongside N50.
Completeness with BUSCO. BUSCO (short for Benchmarking Universal Single-Copy Orthologs) searches the assembly for a set of genes that are expected exactly once in every genome of a group, such as bacteria, vertebrates or flowering plants. You choose the set that matches your organism. BUSCO then sorts every marker gene into one of the groups in the table below and prints a one-line summary.
The idea rests on orthologs: genes in different species that descend from a single gene in their common ancestor. Some orthologs are so essential, for example genes for parts of the ribosome, that almost every member of a group keeps exactly one copy. If one of these is missing from your assembly, the likelier explanation is that the assembly is incomplete, not that the organism lost an essential gene. The markers must match the organism, which is why you choose a lineage set. BUSCO would not mean much on the 32 kb practice genome: it is far smaller than a real bacterial genome and holds few of the markers, which is why the example below is made up. Genes on your assembly: a first count, and is it complete?, later in this module, returns to this question with the practice data.
How to read the table below
Each row is one letter of the BUSCO summary line. The middle column says what it counts; the right column says what a high value usually means. The example line under the table is made up for teaching, in the format BUSCO prints.
| Letter | What it counts | A high value suggests |
|---|---|---|
| C | Complete: markers found at full length (S + D) | Most of the gene content is present |
| S | Complete and single-copy | What a good assembly should show |
| D | Complete but duplicated | Contamination, or the same region kept twice, for example both copies of a chromosome left in one assembly |
| F | Fragmented: only part of the gene found | Genes broken by contig ends or by small indels |
| M | Missing | Sequence absent from the assembly |
| n | The number of marker genes searched | Not a score: it depends on the set you chose |
Example output, made up for teaching. Here 123 of 124 markers are complete and single-copy (123 ÷ 124 = 99.2%) and one is fragmented (0.8%).
Base accuracy as QV. The consensus quality, written QV, puts the assembly's error rate on the same Phred scale used for read quality scores in Module 5: QV = −10 × log10(error rate). QV 30 means about one wrong base in 1,000, QV 40 one in 10,000 and QV 50 one in 100,000; every 10 points is ten times fewer errors. Without a reference, tools such as Merqury estimate QV from k-mers. They count the k-mers of accurate reads from the same sample and check every k-mer of the assembly against them: an assembly k-mer that never appears in the reads probably contains an error, the same idea as the error k-mers of K-mers and genome size. Projects that build reference genomes commonly aim for QV 40 or higher.
Short-read and long-read assemblies tend to fail in different ways, which is why both QV and completeness are reported. Short reads are accurate base by base, so short-read assemblies usually have a high QV but break at repeats. Older long-read assemblies were the reverse: long contigs, but more small insertion and deletion errors, which lower the QV and can break genes into fragments that BUSCO then marks as F. Long reads and repeats, the next lesson, looks at this trade-off.
You can make a rough QV for the practice assembly from the QUAST numbers above. There were 7.10 mismatches per 100,000 bases and no small indels, so the error rate is 7.10 ÷ 100,000 = 0.000071, and QV = −10 × log10(0.000071) ≈ 41.5. That is a high base accuracy, which fits the picture of a short-read assembly that is correct but fragmented. For genomes assembled into scaffolds, report contig N50 and scaffold N50 separately: scaffolds can be joined across gaps of Ns, so their N50 says nothing about how much sequence is actually known.
QUAST is described by Gurevich, Saveliev, Vyahhi and Tesler (2013, Bioinformatics). BUSCO is Simão et al. (2015, Bioinformatics), with later versions described by Manni et al. (2021, Molecular Biology and Evolution). Merqury is Rhie, Walenz, Koren and Phillippy (2020, Genome Biology). BUSCO percentages depend on the lineage dataset, so a score against one set (for example bacteria_odb10) cannot be compared with a score against another set or another version of it. Report the BUSCO version, the lineage and its version, and the full summary line, not only the C value.
Key point
A high N50 is not proof of a good assembly: joining contigs wrongly also raises N50. Always report contiguity together with correctness and completeness.
Here is the same assembly with one wrong join. Suppose an assembler had stuck NODE_1 and NODE_2 together, although they are not neighbours in the genome. The lengths would become 14,188, 6,189, 5,888 and 2,001. The total is unchanged, so half is still 14,133, and the first contig alone passes it: the N50 jumps from 6,894 to 14,188, more than double. The assembly would look better while being worse, and only the misassembly count would reveal it.
Further reading · Module 6QUAST: the evaluation tool and its documentation. · The BUSCO user guide: lineage datasets and how to read the summary. · Merqury: reference-free QV and completeness from k-mers. · All reading for this course
Check your understanding
1. Contigs of 50, 30, 10 and 10 kb. What is the N50?
2. Why is the genome fraction 88% and not 100%?
3. Which measures correctness rather than contiguity?
Up next
Lesson 6
Long reads and repeats