Lesson 4 of 9
Module 6 · Genome assembly
SPAdes is a widely used assembler for bacterial genomes. It corrects read errors, builds graphs at several k values, and uses read pairs to join contigs. One command runs it all.
K-mers and genome size and Assembly graphs built the pieces of an assembler by hand: counting k-mers, building a de Bruijn graph and walking it. SPAdes does all of this and more in one run. It first corrects likely sequencing errors in the reads, so that fewer false k-mers enter the graph. It then builds graphs at several values of k, simplifies them by removing small side branches caused by remaining errors, and finally uses the read pairs to resolve some repeats and to place contigs in order. The command below runs the whole process on the practice reads and then looks at what it produced.
Why use a tool rather than the code from Assembly graphs? That code worked on eight perfect reads. Real data bring errors, uneven coverage, both strands and millions of reads, and SPAdes has years of rules for each of these. The reads still need to be clean in one respect: SPAdes' error correction fixes scattered wrong bases, but it does not remove adapter, so adapter should be trimmed first, as in Trimming and filtering. The practice reads here are already clean, which is why the command uses them directly.
spades.py --isolate -1 data/bact_R1.fastq.gz -2 data/bact_R2.fastq.gz -o spades_out -t 2 ls spades_out | head -n 8 grep ">" spades_out/contigs.fasta
What this does
spades.pystarts SPAdes.-1and-2name the two files of the read pairs, R1 and R2, so SPAdes knows which reads are partners.-o spades_outis the output folder and-t 2lets it use two processor threads.- SPAdes also takes
-m, a memory limit in gigabytes. On a laptop, set it below the memory the machine has, so that a run which needs too much stops with a clear message instead of slowing everything else down. Memory, not time, is usually what limits short-read assembly, because the graph holds every distinct k-mer. ls spades_out | head -n 8lists the output folder and shows only the first eight names. The folder holds more files than this, including the ones in the table below.- Before trusting any output, check that the run finished. The last lines of spades.log say whether the pipeline completed and list any warnings. A run that stops early, often because it ran out of memory, can leave partial files behind that look like results.
grep ">" spades_out/contigs.fastaprints only the header lines of the FASTA file. In FASTA format every sequence starts with a line beginning >, so this gives one line per contig without printing thousands of letters.
Reading the output
- The first eight lines are file and folder names. .fastg and .gfa are file formats for storing an assembly graph. In before_rr.fasta, "rr" stands for repeat resolution: these are the contigs before SPAdes used the read pairs to resolve repeats.
- Each header has the same pattern. Take NODE_1_length_7294_cov_30.961044: this is contig number 1, it is 7,294 bases long, and its k-mer coverage is about 31. SPAdes numbers contigs from longest to shortest.
- The k-mer coverage in the header is measured at SPAdes' largest k value, not in bases. For the reason you met in K-mers and genome size, it is lower than the base coverage: the longer the k-mer, the fewer complete ones fit in each read. Compare contigs with each other using this number, rather than with the 59× worked out in What genome assembly is.
- The names NODE_1, NODE_2 and so on only describe this run. Run SPAdes again with a different version or setting and the same piece of genome may get a different number, so do not use these names as permanent labels; later lessons rename contigs before sharing them.
--isolate is the setting for a single, pure, well-covered bacterial isolate. The folders K21, K33 and K55 are the three k values SPAdes chose from the read length. It took a few seconds here; a real 5 Mb bacterial genome takes much longer, depending on the coverage and the computer.
The reason for three values of k follows from Assembly graphs. A small k such as 21 makes the graph connected even where coverage is low, because short words are more likely to be shared by overlapping reads; a large k such as 55 keeps more repeats apart, because a longer word is more likely to be unique. SPAdes builds the graph at each value in turn and carries information from the smaller k into the larger, so the final graph has the benefits of both. The largest k is limited by the read length: with 125-base reads, a k of 55 still leaves room for many k-mers per read.
You can set the k values yourself with -k, but there is rarely a reason to with an isolate. Each value must be odd, for the reason given in K-mers and genome size, and smaller than the read length; SPAdes also has an upper limit on k, given in its manual. A common mistake is to choose a large k after trimming has shortened many reads: reads shorter than k contribute no k-mers at all, so the graph loses coverage exactly where you hoped to gain resolution.
SPAdes has other modes for other kinds of sample, for example metagenomes (many species mixed together) or RNA. Using the isolate setting on a mixed sample, or the reverse, gives poorer results, so check that the mode matches your sample before you start.
Older tutorials often use the option --careful, which adds an extra pass to reduce small errors in the contigs. Options and their combinations have changed between SPAdes versions, so follow the manual for the version you have installed rather than a command copied from an old guide.
How to read the table below
The table lists the four output files you will use most. The left column is the file name inside spades_out; the right column says what it holds. In practice you take contigs.fasta forward, use the graph file to see why the assembly broke where it did, and keep spades.log for your records.
| File | What it holds |
|---|---|
| contigs.fasta | The assembled contigs, longest first. Headers give length and k-mer coverage. |
| scaffolds.fasta | Contigs joined using read pairs, with Ns for unknown gaps. |
| assembly_graph_with_scaffolds.gfa | The graph itself: open it in the Bandage viewer to see how contigs connect. |
| spades.log | Everything SPAdes did, for your methods and for troubleshooting. |
Two terms in the table are new. A scaffold is a chain of contigs whose order and spacing are known from read pairs, even though the sequence between them is not. If one read of a pair lands near the end of contig A and its partner near the start of contig B, the two contigs must be neighbours, and the expected distance between the reads gives an estimate of the gap. The unknown bases in that gap are written as the letter N. Bandage is a free viewer that draws the assembly graph, so a repeat appears as one node with several connections, as in the figure in Assembly graphs.
Scaffolds come with a pitfall. The number of Ns in a gap is only an estimate from the read-pair distance, not a measured length, and any gene that crosses a gap is broken or mis-sized. For counting genes or comparing sequences, many people work from contigs.fasta and treat scaffolds as extra information about order. In this practice run the read pairs, about 400 bases apart, are too close to span the repeat, so the scaffolds cannot add much.
Worked example · spotting the repeat from coverage
89 ÷ 30 ≈ 3 → NODE_5 is a 2 kb sequence present three times, collapsed into one contig
four unique pieces + one triple repeat = the genome in five contigs
This is exactly the situation in Assembly graphs, at full scale. Read pairs 400 bases apart cannot span a 2,000-base repeat, so SPAdes cannot tell how the pieces connect around it.
Step by step. First find the coverage of a sequence present once. The four long contigs have 31.0, 30.9, 28.9 and 30.1; their average is (31.0 + 30.9 + 28.9 + 30.1) ÷ 4 = 120.9 ÷ 4 ≈ 30.2. Then divide NODE_5's coverage by it: 89.3 ÷ 30.2 ≈ 2.96, which rounds to 3. Reads from all three copies of the repeat land on the one collapsed contig, so it collects three times the reads.
What the answer means. The genome is about 32 kb, but the five contigs add up to less, because the repeat appears once in the assembly instead of three times. The coverage tells you how many copies are missing even though the sequence of each copy cannot be placed. Coverage near a whole-number multiple, such as 2× or 3×, points to copies; coverage at an awkward in-between value is more often noise or a mixture.
What it looks like in the graph. Opened in Bandage, this assembly would show the four long contigs as separate nodes, all joined through the short NODE_5, so that the repeat sits at the centre with many connections. That picture is the full-scale version of the toy graph from Assembly graphs, where two entrances and two exits met at one shared stretch.
Go deeper: converting k-mer coverage to base coverage
A read of length L contains L − k + 1 complete k-mers. So if the base coverage is C, the k-mer coverage is about C × (L − k + 1) ÷ L. This is the same reasoning as in K-mers and genome size, with SPAdes' largest k in place of 21.
For the practice data, with 125-base reads and k = 55: 125 − 55 + 1 = 71, and 58.7 × 71 ÷ 125 ≈ 33. The unique contigs report about 29 to 31, close to this. The formula is an idealisation, so expect the headers to sit near the estimate rather than exactly on it. Run backwards, 30 × 125 ÷ 71 ≈ 53, it turns any SPAdes header into an approximate base coverage, which is handy for comparing contigs with a target such as the 50–100× of What genome assembly is.
SPAdes is described by Bankevich et al. (2012, Journal of Computational Biology), and a practical guide to its current modes is Prjibelski et al. (2020, Current Protocols in Bioinformatics). The Bandage viewer is Wick, Schultz, Zobel and Holt (2015, Bioinformatics). SPAdes changes between releases, in its modes, defaults and output, so the same reads can give slightly different contigs with different versions. Report the version, the mode (here --isolate) and any k values you set by hand, and keep spades.log, which records them.
Key point
Check the contig coverages. Very low coverage contigs are often contamination or errors; multiples of the main coverage are repeats or plasmids with several copies.
A second example: many bacteria carry small plasmids present in several copies per cell. If a 4,000-base plasmid exists in five copies for every copy of the chromosome, its contig will show about five times the chromosome's coverage, even though it is a separate molecule rather than a repeat inside the chromosome. A short contig at a fraction of the main coverage, by contrast, such as 2 when the rest are at 30, is more likely to come from a small amount of contaminating DNA.
On a real bacterial genome, contigs.fasta often ends in a long tail of very short contigs, a few hundred bases or less, many of them at low coverage. Most are leftover errors or fragments of repeats, and many pipelines drop contigs below a few hundred bases before going further. Do this as a deliberate, reported step, with the threshold stated. The next lesson, Judging an assembly, gives the numbers to judge what is left.
Further reading · Module 6The SPAdes manual: modes, options and output files. · Bandage: the viewer for assembly graphs. · Why are de Bruijn graphs useful for genome assembly?: a short, open-access primer on assembly graphs. Read this one even if you read nothing else. · All reading for this course
Check your understanding
1. Why does SPAdes build graphs at several k values?
2. What do Ns in scaffolds.fasta mean?
3. A contig has coverage 60 when the rest are at 30. What is the first explanation to check?
Up next
Lesson 5
Judging an assembly