The Burrows–Wheeler Transform & FM-Index
Compression and search in one structure, behind genome alignment.
Sort all rotations of a string and keep only the last column. The result looks scrambled, yet it can be inverted exactly, it compresses better than the original, and it can count the occurrences of any pattern without being decompressed. The Burrows–Wheeler transform (1994) does all three; it sits inside bzip2 and inside the read aligners that map short DNA sequences to the human genome.
What you’ll learn
- Compute the Burrows–Wheeler transform of a short string by sorting rotations, and relate it to the suffix array: .
- Prove the LF-mapping property and invert the transform in time.
- Explain why the transform helps compression, and trace bzip2's pipeline.
- Count a pattern's occurrences by backward search with rank queries, locate them with a sampled suffix array, and describe the FM-index's space and its use in genome read alignment.
Sort the rotations, keep the last column
Append a unique end marker $, smaller than every character, to , and write down all rotations of . Sort them. The first column holds the characters in sorted order; the last column is the Burrows–Wheeler transform (BWT) of .
| Row | Sorted rotation | ||
|---|---|---|---|
| 0 | $banana | $ | a |
| 1 | a$banan | a | n |
| 2 | ana$ban | a | n |
| 3 | anana$b | a | b |
| 4 | banana$ | b | $ |
| 5 | na$bana | n | a |
| 6 | nana$ba | n | a |
annb$aa. Row 4 is the original string, the only rotation that ends with the marker.Why it compresses. Row 's last character is the one that precedes the context -row starts with, and rows with similar contexts sort next to each other. In English, the rows starting with "he " are mostly preceded by t, so the last column grows long runs of t. On the 87-character tongue twister "she sells sea shells by the sea shore and the shells she sells are sea shells for sure", the number of runs of equal characters falls from 82 in the text to 39 in its BWT, and after move-to-front coding (replace each character by its position in a list of recently used characters, then move it to the front) 48 of the 87 values are 0, against 5 for the raw text. A stream dominated by small numbers is easy prey for an entropy coder.
Think first In the sorted rotation matrix of , which row holds itself? (Not row 0: that one starts with the marker.)
The row whose last character is $: is the only rotation that ends with the marker, so it is row 4 for banana. Inverting the transform does not need to know this row in advance, because it can start from row 0, the rotation that begins with $.
Inverting with the LF-mapping
The rows ending with are rotations of the form . Their order in the matrix is the order of the strings , because rotations are distinct and are decided before the final .
Rotating each of them by one position gives , and these are exactly the rows starting with . Among themselves they are ordered by again, since they share the first character:
So the -th row ending with and the -th row starting with are rotations of each other by one position: the same occurrence of in the text. The rows starting with occupy the block that begins at , which gives the formula.
| Row | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| a | n | n | b | $ | a | a | |
| 1 | 5 | 6 | 4 | 0 | 2 | 3 |
annb$aa. Inverting walks backward through the text: start at row 0, whose last character a is the last character of banana, and repeat "output , then move to ": rows 0, 1, 5, 2, 6, 3 give a, n, a, n, a, b, which is banana read backward, and the walk ends at row 4.def bwt(s): # s ends with a unique smallest "$"
sa = sorted(range(len(s)), key=lambda i: s[i:])
return ''.join(s[i - 1] for i in sa) # s[-1] is "$" when i == 0
def lf_tables(L):
C, total = {}, 0
for c in sorted(set(L)): # C[c]: number of characters smaller than c
C[c] = total
total += L.count(c)
rank, seen = [], {}
for c in L: # rank[i]: occurrences of L[i] in L[0..i)
rank.append(seen.get(c, 0))
seen[c] = seen.get(c, 0) + 1
return C, rank
def inverse_bwt(L):
C, rank = lf_tables(L)
row, out = 0, [] # row 0 starts with "$": its last character ends s
for _ in range(len(L) - 1):
out.append(L[row])
row = C[L[row]] + rank[row] # LF(row)
return ''.join(reversed(out)) + '$'bwt uses the slow suffix sort for clarity. The inversion is linear once the ranks are tabulated in one pass (for a constant alphabet): inverse_bwt("annb$aa") returns "banana$".
Inverting annb$aa, the walk is at row 1, where . Which row does it visit next? (.)
Show the answer
5
: this n is the first n in column , so it is the first n in column , at row 5 (na$bana).
Backward search: the FM-index
The rows of the matrix that start with a string form a contiguous range , exactly like a block of the suffix array. Ferragina and Manzini (2000) noticed that the LF-mapping turns the range for into the range for : of the rows starting with , those whose last character is are the rotations , and LF maps them, in order, onto the rows starting with . So a pattern is matched from its last character to its first, with two rank queries per character: backward search.
Start with all rows, the range of the empty string. Within , the rows ending with are its occurrences of in column , the ranks to among all 's. LF maps them, keeping their order, to consecutive rows of the block that starts at :
After processing all of from right to left, the range holds the rows that start with ; its size is the number of occurrences, and an empty range means none:
| Step | Character | Range of rows | Rows start with |
|---|---|---|---|
| start | — | (anything) | |
| 1 | a | a | |
| 2 | n | na | |
| 3 | a | ana |
annb$aa: two rows, so two occurrences. Rows 2 and 3 hold the suffixes starting at and , the positions of "ana" in banana.def count(L, p): # backward search: number of occurrences of p
C, _ = lf_tables(L)
sp, ep = 0, len(L) # rows [sp, ep) start with the part of p read so far
for c in reversed(p):
if c not in C:
return 0
sp = C[c] + L[:sp].count(c) # Occ(c, sp): a rank query
ep = C[c] + L[:ep].count(c)
if sp >= ep:
return 0
return ep - spHere each rank query scans a prefix of for clarity. An FM-index answers in for small alphabets with stored counts plus rank structures on bit vectors, or in with a wavelet tree (Grossi, Gupta & Vitter, 2003), so counting takes or time, independent of .
Locating needs suffix-array values, which the index does not store in full. Keep every -th text position's suffix-array entry (a sampled suffix array). For a row in the final range, apply LF until reaching a sampled row; after steps the answer is that sample plus . Each occurrence then costs steps, with samples.
Space. The column is as compressible as the text: Ferragina and Manzini showed that their FM-index fits in space bounded in terms of the -th order empirical entropy of the text, and later refinements surveyed by Navarro and Mäkinen (2007) bring it to about bits plus lower-order terms. A self-index of this kind replaces the text: it can also extract any substring.
Why does the FM-index match a pattern from its last character to its first?
- Because the BWT stores the text reversed
- Because LF prepends a character: from the rows starting with it reaches the rows starting with
- Because the first column is not stored
- Because right-to-left matching skips characters, as in Boyer–Moore
Show the answer
B. Because LF prepends a character: from the rows starting with it reaches the rows starting with
LF moves from a row to the row of the rotation that starts one character earlier in the text, so ranges grow by one character on the left. Matching therefore starts with its last character and prepends the others.
Genomes, and the frontier
A sequencing machine produces millions of short DNA reads, each to be placed where it best matches a reference genome of about 3 billion bases. Bowtie (Langmead, Trapnell, Pop & Salzberg, 2009) and BWA (Li & Durbin, 2009) index the genome with an FM-index small enough for an ordinary computer's memory, a few gigabytes instead of the tens of gigabytes a suffix tree would need, and match each read by backward search. Mismatches are handled by backtracking: at a position of the read, try the other three bases as well, extending each surviving range to the left, with limits on how many such branches a read may take.
Key takeaways
- The BWT is the last column of the sorted rotations of , equivalently , so a linear-time suffix array gives a linear-time BWT.
- Characters are grouped by the context that follows them, so the BWT has long runs that move-to-front and entropy coding exploit, as in bzip2.
- The LF-mapping, , follows the text backward and inverts the transform in .
- Backward search counts a pattern with rank queries, independent of ; a sampled suffix array locates occurrences, and the whole FM-index fits in about bits plus lower-order terms.
- FM-indexes run genome read aligners such as Bowtie and BWA; run-length variants (the r-index) index highly repetitive collections in space proportional to the number of BWT runs.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
The suffix array of banana$ is . What is the BWT character at row 2?
- a
- b
- $
- n
Show the answer
D. n
n: the suffix at row 2, ana$, starts at position 3 and is preceded by the n at position 2.
What makes the Burrows–Wheeler transform invertible?
- It stores the original string's row and the sorted first column
- The -th occurrence of a character in the last column and its -th occurrence in the first column are the same character of the text
- Each character's frequency is preserved, so the text can be reassembled by counting
- Sorting is a permutation, and every permutation of a string is invertible
Show the answer
B. The -th occurrence of a character in the last column and its -th occurrence in the first column are the same character of the text
The LF property links each character to the one before it in the text, so walking LF from the row that starts with the marker spells the text backward. The first column needs no storage: it is sorted.
An FM-index of a 3-billion-character genome counts the occurrences of a 100-character read. How many rank queries does backward search make, at most?
- About
- About
- 200
Show the answer
C. 200
Two rank queries, and , per character of the read: at most 200, and fewer if the range empties early. The length of the genome does not appear, which is the point of the index.
Why is the BWT of an English text more compressible than the text itself?
- It removes repeated characters
- It sorts the characters, like column
- Characters followed by similar contexts become adjacent, so the last column has long runs of a few characters
- It replaces frequent words by short codes
Show the answer
C. Characters followed by similar contexts become adjacent, so the last column has long runs of a few characters
Row ends with the character that precedes the context the row starts with. Sorting groups similar contexts, and similar contexts tend to be preceded by the same characters (t before "he "), so runs appear, which move-to-front turns into small numbers for an entropy coder.
Further reading
- M. Burrows & D. J. Wheeler, "A block-sorting lossless data compression algorithm," SRC Research Report 124, Digital Equipment Corporation (1994).
- P. Ferragina & G. Manzini, "Indexing compressed text," Journal of the ACM 52(4) (2005), 552–581; first presented as "Opportunistic data structures with applications," FOCS 2000.
- G. Navarro & V. Mäkinen, "Compressed full-text indexes," ACM Computing Surveys 39(1) (2007), article 2.
- B. Langmead, C. Trapnell, M. Pop & S. L. Salzberg, "Ultrafast and memory-efficient alignment of short DNA sequences to the human genome," Genome Biology 10(3) (2009), R25.
- H. Li & R. Durbin, "Fast and accurate short read alignment with Burrows–Wheeler transform," Bioinformatics 25(14) (2009), 1754–1760.