# Burrows-Wheeler transform: Wikis

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

# Encyclopedia

Updated live from Wikipedia, last check: May 21, 2013 00:04 UTC (41 seconds ago)
(Redirected to Burrows–Wheeler transform article)

The Burrows–Wheeler transform (BWT, also called block-sorting compression), is an algorithm used in data compression techniques such as bzip2. It was invented by Michael Burrows and David Wheeler in 1994 while working at DEC Systems Research Center in Palo Alto, California.[1] It is based on a previously unpublished transformation discovered by Wheeler in 1983.

When a character string is transformed by the BWT, none of its characters change value. The transformation permutes the order of the characters. If the original string had several substrings that occurred often, then the transformed string will have several places where a single character is repeated multiple times in a row. This is useful for compression, since it tends to be easy to compress a string that has runs of repeated characters by techniques such as move-to-front transform and run-length encoding.

For example:

Input SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES TEXYDST.E.IXIXIXXSSMPPS.B..E.S.EUSFXDIIOIIIT

The output is easier to compress because it has many repeated characters. In fact, in the transformed string, there are a total of six runs of identical characters: XX, SS, PP, .., II, and III, which together make 13 out of the 44 characters in it.

## Example

The transform is done by sorting all rotations of the text, then taking the last column. For example, the text "^BANANA@" is transformed into "BNN^AA@A" through these steps (the red @ character indicates the 'EOF' pointer):

Transformation
Input All
Rotations
Sort the
Rows
Output
```^BANANA@
```
```^BANANA@
@^BANANA
A@^BANAN
NA@^BANA
ANA@^BAN
NANA@^BA
ANANA@^B
BANANA@^
```
```ANANA@^B
ANA@^BAN
A@^BANAN
BANANA@^
NANA@^BA
NA@^BANA
^BANANA@
@^BANANA
```
```BNN^AA@A
```

The following pseudocode gives a simple, but inefficient, way to calculate the BWT and its inverse. It assumes that the input string `s` contains a special character 'EOF' which is the last character, occurs nowhere else in the text, and is ignored during sorting.

``` function BWT (string s)
create a table, rows are all possible rotations of s
sort rows alphabetically
return (last column of the table)

function inverseBWT (string s)
create empty table

repeat length(s) times
insert s as a column of table before first column of the table   // first insert creates first column
sort rows of the table alphabetically
return (row that ends with the 'EOF' character)
```

To understand why this creates more-easily-compressible data, let's consider transforming a long English text frequently containing the word "the". Sorting the rotations of this text will often group rotations starting with "he " together, and the last character of that rotation (which is also the character before the "he ") will usually be "t", so the result of the transform would contain a number of "t" characters along with the perhaps less-common exceptions (such as if it contains "Brahe ") mixed in. So it can be seen that the success of this transform depends upon one value having a high probability of occurring before a sequence, so that in general it needs fairly long samples (a few kilobytes at least) of appropriate data (such as text).

The remarkable thing about the BWT is not that it generates a more easily encoded output—an ordinary sort would do that—but that it is reversible, allowing the original document to be re-generated from the last column data.

The inverse can be understood this way. Take the final table in the BWT algorithm, and erase all but the last column. Given only this information, you can easily reconstruct the first column. The last column tells you all the characters in the text, so just sort these characters to get the first column. Then, the first and last columns together give you all pairs of successive characters in the document, where pairs are taken cyclically so that the last and first character form a pair. Sorting the list of pairs gives the first and second columns. Continuing in this manner, you can reconstruct the entire list. Then, the row with the "end of file" character at the end is the original text. Reversing the example above is done like this:

Inverse Transformation
Input
```BNN^AA@A
```
```B
N
N
^
A
A
@
A
```
```A
A
A
B
N
N
^
@
```
```BA
NA
NA
^B
AN
AN
@^
A@
```
```AN
AN
A@
BA
NA
NA
^B
@^
```
```BAN
NAN
NA@
^BA
ANA
ANA
@^B
A@^
```
```ANA
ANA
A@^
BAN
NAN
NA@
^BA
@^B
```
```BANA
NANA
NA@^
^BAN
ANAN
ANA@
@^BA
A@^B
```
```ANAN
ANA@
A@^B
BANA
NANA
NA@^
^BAN
@^BA
```
```BANAN
NANA@
NA@^B
^BANA
ANANA
ANA@^
@^BAN
A@^BA
```
```ANANA
ANA@^
A@^BA
BANAN
NANA@
NA@^B
^BANA
@^BAN
```
```BANANA
NANA@^
NA@^BA
^BANAN
ANANA@
ANA@^B
@^BANA
A@^BAN
```
```ANANA@
ANA@^B
A@^BAN
BANANA
NANA@^
NA@^BA
^BANAN
@^BANA
```
```BANANA@
NANA@^B
NA@^BAN
^BANANA
ANANA@^
ANA@^BA
@^BANAN
A@^BANA
```
```ANANA@^
ANA@^BA
A@^BANA
BANANA@
NANA@^B
NA@^BAN
^BANANA
@^BANAN
```
```BANANA@^
NANA@^BA
NA@^BANA
^BANANA@
ANANA@^B
ANA@^BAN
@^BANANA
A@^BANAN
```
```ANANA@^B
ANA@^BAN
A@^BANAN
BANANA@^
NANA@^BA
NA@^BANA
^BANANA@
@^BANANA
```
Output
```^BANANA@
```

## Optimization

A number of optimizations can make these algorithms run more efficiently without changing the output. In BWT, there is no need to represent the table in either the encoder or decoder. In the encoder, each row of the table can be represented by a single pointer into the strings, and the sort performed using the indices. Some care must be taken to ensure that the sort does not exhibit bad worst-case behavior: Standard library sort functions are unlikely to be appropriate. In the decoder, there is also no need to store the table, and in fact no sort is needed at all. In time proportional to the alphabet size and string length, the decoded string may be generated one character at a time from right to left. The example code below demonstrates efficient decoding. A "character" in the algorithm can be a byte, or a bit, or any other convenient size.

There is no need to have an actual 'EOF' character. Instead, a pointer can be used that remembers where in a string the 'EOF' would be if it existed. In this approach, the output of the BWT must include both the transformed string, and the final value of the pointer. That means the BWT does expand its input slightly. The inverse transform then shrinks it back down to the original size: it is given a string and a pointer, and returns just a string.

A complete description of the algorithms can be found in Burrows and Wheeler's paper, or in a number of online sources.

## Bijective variant

Since any rotation of the input string will lead to the same transformed string, the BWT cannot be inverted without adding an 'EOF' marker to the input or, augmenting the output with information, such as an index, that makes it possible to identify the input string from the class of all of its rotations.

There is a bijective version of the transform, by which the transformed string uniquely identifies the original. In this version, every string has a unique inverse of the same length.

The bijective transform is computed by first factoring the input into Lyndon words, and then sorting together all rotations of these words. The transformed string is then obtained by picking the last character of the strings in this sorted list.

For example, applying the bijective transform gives:

Input SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES STEYDST.E.IXXIIXXSMPPXS.B..EE..SUSFXDIOIIIIT

The bijective transform includes eight runs of identical characters. These runs are, in order: XX, II, XX, PP, .., EE, .., and IIII. In total, 18 characters take part in these runs.

A complete description of the algorithm can be found in an article due to Gil and Scott.

## Dynamic Burrows-Wheeler Transform

Instead of reconstructing the Burrows-Wheeler Transform of an edited text, Salson et al.[2] propose an algorithm that deduces the new Burrows-Wheeler Transform from the original one, doing a limited number of local reorderings in the original Burrows-Wheeler Transform.

## Sample implementation

This Python implementation sacrifices speed for simplicity: the program is short, but takes more than the linear time that would be desired in a practical implementation.

Using the null character as the end of file marker, and using ```s[i:] + s[:i]``` to construct the ith rotation of `s`, the forward transform takes the last character of each of the sorted rows:

```def bwt(s):
"""Apply Burrows-Wheeler transform to input string."""
assert "\0" not in s, "Input string cannot contain null character ('\0')"
s = s + "\0"  # Add end of file marker
table = [s[i:] + s[:i] for i in range(len(s))]  # Table of rotations of string
table.sort()
last_column = [row[-1] for row in table]  # Last characters of each row
return "".join(last_column)  # Convert list of characters into string
```

The inverse transform repeatedly inserts `r` as the left column of the table and sorts the table. After the whole table is built, it returns the row that ends with null, minus the null.

```def ibwt(r):
"""Apply inverse Burrow-Wheeler transform."""
table = [""] * len(r)  # Make empty table
for i in range(len(r)):
table = [r[i] + table[i] for i in range(len(r))]  # Add a column of r
table.sort()
s = [row for row in table if row.endswith("\0")][0]  # Find the correct row (ending in "\0")
return s.strip("\0")  # Get rid of trailing null character
```

## BWT in bioinformatics

The advent of high-throughput sequencing (HTS) techniques at the end of the 2000 decade has led to another application of the Burrows–Wheeler transformation. In HTS, DNA is fragmented into small pieces, of which the first few bases are sequenced, yielding several millions of "reads", each 20 to 100 base pairs ("DNA characters") long. In many experiments, e.g., in ChIP-Seq, the task is now to align these reads to a reference genome, i.e., to the known, nearly complete sequence of the organism in question (which may be up to several billion base pairs long). A number of alignment programs, specialized for this task, were published, which initially relied on hashing (e.g., Eland, SOAP[1][3], or Maq[4]). Here, the enormous memory requirements for the hash table was a problem, and so, the next generation of alignment programs, notably Bowtie[5] , BWA[6], and SOAP2[7], uses the Burrows–Wheeler transform of the reference genome as the basis for very efficiently aligning the reads.

## References

1. ^ Burrows M and Wheeler D (1994), A block sorting lossless data compression algorithm, Technical Report 124, Digital Equipment Corporation
2. ^ Salson M, Lecroq T, Léonard M and Mouchard L (2009). "A Four-Stage Algorithm for Updating a Burrows-Wheeler Transform". Theoretical Computer Science. doi:10.1016/j.tcs.2009.07.016.
3. ^ Li R, et al (2008). "SOAP: short oligonucleotide alignment program". Bioinformatics 24 (5): 713–714. PMID 18227114.
4. ^ Li H, Ruan J, Durbin R (2008-08-19). "Mapping short DNA sequencing reads and calling variants using mapping quality scores". Genome Research 18 (11): 1851–1858. doi:10.1101/gr.078212.108. PMID 18714091.
5. ^ Langmead B, Trapnell C, Pop M, Salzberg SL (2009). "Ultrafast and memory-efficient alignment of short DNA sequences to the human genome". Genome Biology 10 (3): R25. doi:10.1186/gb-2009-10-3-r25. PMID 19261174.
6. ^ Li H, Durbin R (2009). "Fast and accurate short read alignment with Burrows–Wheeler Transform". Bioinformatics. PMID 19451168.
7. ^ Li R, et al (2009). "SOAP2: an improved ultrafast tool for short read alignment". Bioinformatics. PMID 19497933.

The Burrows-Wheeler transform (BWT, also called block-sorting compression), is an algorithm used in data compression techniques such as bzip2. It was invented by Michael Burrows and David Wheeler in 1994 while working at DEC Systems Research Center in Palo Alto, California.[1] It is based on a previously unpublished transformation discovered by Wheeler in 1983.

When a character string is transformed by the BWT, none of its characters change value. The transformation permutes the order of the characters. If the original string had several substrings that occurred often, then the transformed string will have several places where a single character is repeated multiple times in a row. This is useful for compression, since it tends to be easy to compress a string that has runs of repeated characters by techniques such as move-to-front transform and run-length encoding.

For example:

 Input SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES TEXYDST.E.IXIXIXXSSMPPS.B..E.S.EUSFXDIIOIIIT

The output is easier to compress because it has many repeated characters. In fact, in the transformed string, there are a total of six runs of identical characters: XX, SS, PP, .., II, and III, which together make 13 out of the 44 characters in it.

## Example

The transform is done by sorting all rotations of the text, then taking the last column. For example, the text "^BANANA@" is transformed into "BNN^AA@A" through these steps (the red @ character indicates the 'EOF' pointer):

Transformation
Input All
Rotations
Sort the
Rows
Output
```^BANANA@
```
```^BANANA@
@^BANANA
A@^BANAN
NA@^BANA
ANA@^BAN
NANA@^BA
ANANA@^B
BANANA@^
```
```ANANA@^B
ANA@^BAN
A@^BANAN
BANANA@^
NANA@^BA
NA@^BANA
^BANANA@
@^BANANA
```
```BNN^AA@A
```

The following pseudocode gives a simple, but inefficient, way to calculate the BWT and its inverse. It assumes that the input string `s` contains a special character 'EOF' which is the last character, occurs nowhere else in the text, and is ignored during sorting.

``` function BWT (string s)
create a table, rows are all possible rotations of s
sort rows alphabetically
return (last column of the table)

function inverseBWT (string s)
create empty table

repeat length(s) times
insert s as a column of table before first column of the table   // first insert creates first column
sort rows of the table alphabetically
return (row that ends with the 'EOF' character)
```

To understand why this creates more-easily-compressible data, let's consider transforming a long English text frequently containing the word "the". Sorting the rotations of this text will often group rotations starting with "he " together, and the last character of that rotation (which is also the character before the "he ") will usually be "t", so the result of the transform would contain a number of "t" characters along with the perhaps less-common exceptions (such as if it contains "Brahe ") mixed in. So it can be seen that the success of this transform depends upon one value having a high probability of occurring before a sequence, so that in general it needs fairly long samples (a few kilobytes at least) of appropriate data (such as text).

The remarkable thing about the BWT is not that it generates a more easily encoded output—an ordinary sort would do that—but that it is reversible, allowing the original document to be re-generated from the last column data.

The inverse can be understood this way. Take the final table in the BWT algorithm, and erase all but the last column. Given only this information, you can easily reconstruct the first column. The last column tells you all the characters in the text, so just sort these characters to get the first column. Then, the first and last columns together give you all pairs of successive characters in the document, where pairs are taken cyclically so that the last and first character form a pair. Sorting the list of pairs gives the first and second columns. Continuing in this manner, you can reconstruct the entire list. Then, the row with the "end of file" character at the end is the original text. Reversing the example above is done like this:

Inverse Transformation
Input
```BNN^AA@A
```
```B
N
N
^
A
A
@
A
```
```A
A
A
B
N
N
^
@
```
```BA
NA
NA
^B
AN
AN
@^
A@
```
```AN
AN
A@
BA
NA
NA
^B
@^
```
```BAN
NAN
NA@
^BA
ANA
ANA
@^B
A@^
```
```ANA
ANA
A@^
BAN
NAN
NA@
^BA
@^B
```
```BANA
NANA
NA@^
^BAN
ANAN
ANA@
@^BA
A@^B
```
```ANAN
ANA@
A@^B
BANA
NANA
NA@^
^BAN
@^BA
```
```BANAN
NANA@
NA@^B
^BANA
ANANA
ANA@^
@^BAN
A@^BA
```
```ANANA
ANA@^
A@^BA
BANAN
NANA@
NA@^B
^BANA
@^BAN
```
```BANANA
NANA@^
NA@^BA
^BANAN
ANANA@
ANA@^B
@^BANA
A@^BAN
```
```ANANA@
ANA@^B
A@^BAN
BANANA
NANA@^
NA@^BA
^BANAN
@^BANA
```
```BANANA@
NANA@^B
NA@^BAN
^BANANA
ANANA@^
ANA@^BA
@^BANAN
A@^BANA
```
```ANANA@^
ANA@^BA
A@^BANA
BANANA@
NANA@^B
NA@^BAN
^BANANA
@^BANAN
```
```BANANA@^
NANA@^BA
NA@^BANA
^BANANA@
ANANA@^B
ANA@^BAN
@^BANANA
A@^BANAN
```
```ANANA@^B
ANA@^BAN
A@^BANAN
BANANA@^
NANA@^BA
NA@^BANA
^BANANA@
@^BANANA
```
Output
```^BANANA@
```

## Optimization

A number of optimizations can make these algorithms run more efficiently without changing the output. In BWT, there is no need to represent the table in either the encoder or decoder. In the encoder, each row of the table can be represented by a single pointer into the strings, and the sort performed using the indices. Some care must be taken to ensure that the sort does not exhibit bad worst-case behavior: Standard library sort functions are unlikely to be appropriate. In the decoder, there is also no need to store the table, and in fact no sort is needed at all. In time proportional to the alphabet size and string length, the decoded string may be generated one character at a time from right to left. The example code below demonstrates efficient decoding. A "character" in the algorithm can be a byte, or a bit, or any other convenient size.

There is no need to have an actual 'EOF' character. Instead, a pointer can be used that remembers where in a string the 'EOF' would be if it existed. In this approach, the output of the BWT must include both the transformed string, and the final value of the pointer. That means the BWT does expand its input slightly. The inverse transform then shrinks it back down to the original size: it is given a string and a pointer, and returns just a string.

A complete description of the algorithms can be found in Burrows and Wheeler's paper, or in a number of online sources.

## Bijective Variant

Since any rotation of the input string will lead to the same transformed string, the BWT cannot be inverted without adding an 'EOF' marker to the input or, augmenting the output with information, such as an index, that makes it possible to identity the input string from the class of all of its rotations.

There is a bijective version of the transform, by which the transform string uniquely identified the original. In this version, any string as an inverse of the same length.

The bijective transform is computed by first factoring the input into Lyndon words, and then sorting together all rotations of these words. The transformed string is then obtained by picking the last character of the strings in this sorted list.

For example, applying the bijective transform gives:

 Input SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES STEYDST.E.IXXIIXXSMPPXS.B..EE..SUSFXDIOIIIIT

The bijective transform includes eight runs of identical characters. These runs are, in order: XX, II, XX, PP, .., EE, .., and IIII. In total, 18 characters take part in these runs.

A complete description of the algorithm can be found in an article due to Gil and Scott.

## Sample implementations

### Python language

This implementation sacrifices speed for simplicity: the program is short, but takes more than the linear time that would be desired in a practical implementation.

Using the null character as the end of file marker, and using `s[i:] + s[:i]` to construct the ith rotation of `s`, the forward transform takes the last character of each of the sorted rows:

import string

def bwt(s):

```   """Apply Burrows-Wheeler transform to input string."""
assert "\0" not in s, "Input string cannot contain null character ('\0')"
s = s + "\0"  # Add end of file marker
table = [s[i:] + s[:i] for i in range(len(s))]  # Table of rotations of string
table.sort()
last_column = [row[-1] for row in table]  # Last characters of each row
return string.join(last_column, "")  # Convert list of characters into string
```

The inverse transform repeatedly inserts `r` as the left column of the table and sorts the table. After the whole table is built, it returns the row that ends with null, minus the null.

def ibwt(r):

```   """Apply inverse Burrow-Wheeler transform."""
table = [""] * len(r)  # Make empty table
for i in range(len(r)):
table = [r[i] + table[i] for i in range(len(r))]  # Add a column of r
table.sort()
s = [row for row in table if row.endswith("\0")][0]  # Find the correct row (ending in "\0")
return s.strip("\0")  # Get rid of trailing null character
```

## BWT in bioinformatics

The advent of high-throughput sequencing (HTS) techniques at the end of the 2000 decade has led to another application of the Burrows-Wheeler transformation. In HTS, DNA is fragmented into small pieces, of which the first few bases are sequenced, yielding several millions of "reads", each 20 to 100 base pairs ("DNA characters") long. In many experiments, e.g., in ChIP-Seq, the task is now to align these reads to a reference genome, i.e., to the known, nearly complete sequence of the organism in question (which may be up to several billion base pairs long). A number of alignment programs, specialized for this task, were published, which initially relied on hashing (e.g., Eland, SOAP, or Maq[2]). Here, the enormous memory requirements for the hash table was a problem, and so, the next generation of alignment programs, notably Bowtie[3] and BWA[4], uses the Burrows-Wheeler transform of the reference genome as the basis for very efficiently aligning the reads.

## References

1. ^ Burrows M and Wheeler D (1994), A block sorting lossless data compression algorithm, Technical Report 124, Digital Equipment Corporation
2. ^ Li H, Ruan J, Durbin R (2008-08-19). "Mapping short DNA sequencing reads and calling variants using mapping quality scores". Genome Research 18 (11): 1851-1858. doi:10.1101/gr.078212.108. PMID 18714091.
3. ^ Langmead B, Trapnell C, Pop M, Salzberg SL (2009). "Ultrafast and memory-efficient alignment of short DNA sequences to the human genome". Genome Biology 10 (3): R25. doi:10.1186/gb-2009-10-3-r25. PMID 19261174.
4. ^ Li H, Durbin R (2009). "Fast and accurate short read alignment with Burrows-Wheeler Transform". Bioinformatics. PMID 19451168.