Attachment 'Strings-BWT.rst'

Download

Strings and the Burrows-Wheeler Transform

Authors: Franco Saliola
License:CC BY-SA 3.0

Sage/Python includes a builtin datastructure for strings. There are several ways to input strings. You can input a string using single quotes ' or double quotes ":

sage: s = "This is a string!"
sage: s
sage: t = 'So is this!'
sage: print(t)

You can also input a string using three quotes (""" or '''). This is useful if you want to use both " and ' in your string, or you want your string to span multiple lines:

sage: s = """
sage: This is a multi-line
....:         string
....: that includes 'single quotes'
....:           and "double quotes".
sage: """
sage: print(s)

Exercise: Create and print the following string.

sage:   \ | ( | ) / /
....: _________________
....: |               |
....: |               |
....: |  I <3 Coffee! /--\
....: |               |  |
....:  \             /\--/
....:   \___________/

Exercise: Without using cut-and-paste(!) replace the substring I <3 Coffee! with the substring I <3 Tea!.

Exercise: Print a copy of your string with all the letters capitalized (uppercase).

Strings behave very much like lists. For example,

Operation Syntax for strings Syntax for lists
Accessing a letter string[3] list[3]
Slicing string[3:17:2] list[3:17:2]
Concatenation string1 \+ sting2 list1 \+ list2
A copy string[:] list[:]
A reversed copy string[::\-1] list[::\-1]
Length len(string) len(list)

Exercise: The factors of length 2 of 'rhubarb' are

Write a function called factors(s, l) that returns a list of the factors of length l of s.

Exercise: Using your factors function list all the factors of length 3 of 'rhubarb'.

Exercise: What happens if you apply your function factors to the list [0,1,1,0,1,0,0,1]? If it doesn't work for both lists and strings, go back and modify your function so that it does work for both.


Run-length encoding

The string

WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW

begins with W 12 times, then B once, then W 12 times, then B 3 times, then W 24 times, then B once and then W 14 times. Thus, it can be encoded as

(W, 12), (B, 1), (W, 12), (B, 3), (W, 24), (B, 1), (W, 14).

This is called the run-length encoding of the string.

Exercise: Write a function run_length_encoding(s) that returns the run-length encoding of a string s. Does your function work for lists as well as strings? If not, then can you make it so that it works for both strings and lists? Use your function to compute the run-length encoding of the following list.

sage: l = [0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,
....: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Rotations

The rotations of the string 'bananas' are:

  • ananasb
  • nanasba
  • anasban
  • nasbana
  • asbanan
  • sbanana

and if we sort these alphabetically, then we get:

  • anasban
  • asbanan
  • bananas
  • nanasba
  • nasbana
  • sbanana

Exercise: Define a function print_sorted_rotations(s) that sorts all the rotations of a string and prints them in an array as above. Print the sorted rotations of the strings 'ananas' and 'cocomero'.


The Burrows-Wheeler Transform

The Burrows-Wheeler Transform (BWT) of a string s sorts all the rotations of s and then returns the last column.

For example, if we sort the rotations of 'bananas':

  • ananasb
  • anasban
  • asbanan
  • bananas
  • nanasba
  • nasbana
  • sbanana

then the last column is bnnsaaa , so the BWT of bananas is bnnsaaa .

Exercise: Write a function bwt(s) that returns the BWT of the string s. Compute the BWT of bananas , ananas and cocomero . ( Hint: You can return your answer as a list, but if you want to return a string, then you might want to use the .join() method for strings.)


Exercise: Combine the functions you defined above to create an @interact object that takes a string s and prints:

  • the sorted rotations of s
  • the run-length encoding of s
  • the BWT of s
  • the run-length encoding of the BWT of s

(hint: String formatting can be done using the % operator. Here are some examples:

sage: print('The sum of %s and %s is %s.' % (3,2,3+2))

If you are familiar with the C language then you will notice that string formating is very similar to C 's printf statement.)

Use your interact object to explore this transformation, and then to answer the following questions below.

Exercise: What is the BWT of the three following strings?

sage: s1 = 'xxyxyxyxyxyxyxyxyxxyxyxyxyxyxyxyxyxy'
sage: s2 = '01101001100101101001011001101001100101100110100101'
sage: s3 = 'cdccdcdccdccdcdccdcdccdccdcdccdccdcdccdcdccdccdcdc'

Do you notice any patterns in the BWT of a string?

Can you think of an application for this transformation?

Find 3 other strings that have a 'nice' image under the BWT.

Exercise: Is the Burrows-Wheeler transformation invertible? (That is, can you find two strings that have the same BWT?)

Exercise: By comparing the BWT of a string with the first column of the array of sorted rotations of a string s, devise and implement an algorithm that reconstructs the first column of the array from the BWT of s.

Exercise: By examining the first two columns of the array, devise and implement an algorithm that reconstructs the first two columns of the array from the BWT of a string.

(hint: compare the last and first column with the first two columns.)

Exercise: By examining the first three columns of the array, devise and implement an algorithm that reconstructs the first three columns of the array from the BWT of a string.

Exercise: Write a function that reconstructs the entire array of sorted rotations of a string from the BWT of the string.

Exercise: A Lyndon word is a word w that comes first in alphabetical order among all its rotations. Is the BWT invertible on Lyndon words?

Exercise: Explain how one can modify the BWT to make it invertible on arbitrary words. Implement your modified transformation and the inverse transformation.

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-08-12 22:01:42, 8.3 KB) [[attachment:CollatzConjecture.ipynb]]
  • [get | view] (2018-08-12 22:01:45, 100.9 KB) [[attachment:CollatzConjecture.pdf]]
  • [get | view] (2018-08-12 22:01:49, 4.8 KB) [[attachment:CollatzConjecture.rst]]
  • [get | view] (2018-08-12 22:01:53, 12.3 KB) [[attachment:Dictionaries-GraphTheory.ipynb]]
  • [get | view] (2018-08-12 22:01:56, 202.6 KB) [[attachment:Dictionaries-GraphTheory_Solutions.pdf]]
  • [get | view] (2018-08-12 22:02:00, 9.1 KB) [[attachment:Dictionaries-GraphTheory_Solutions.rst]]
  • [get | view] (2018-08-15 12:32:58, 16.0 KB) [[attachment:Fields-2018-flatsurf_and_surface_dynamics_demo.ipynb]]
  • [get | view] (2018-08-11 19:25:37, 15.0 KB) [[attachment:S_2_1.svg]]
  • [get | view] (2018-08-12 22:02:10, 13.4 KB) [[attachment:Strings-BWT.ipynb]]
  • [get | view] (2018-08-12 22:02:14, 72.4 KB) [[attachment:Strings-BWT.pdf]]
  • [get | view] (2018-08-12 22:02:17, 7.0 KB) [[attachment:Strings-BWT.rst]]
  • [get | view] (2018-08-11 19:28:42, 16.7 KB) [[attachment:chap1-first_steps.ipynb]]
  • [get | view] (2018-08-11 19:28:46, 103.1 KB) [[attachment:chap1-first_steps.pdf]]
  • [get | view] (2018-08-11 19:28:53, 7.8 KB) [[attachment:chap1-first_steps.rst]]
  • [get | view] (2018-08-11 19:29:05, 24.6 KB) [[attachment:chap1-first_steps_solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:09, 108.7 KB) [[attachment:chap1-first_steps_solutions.pdf]]
  • [get | view] (2018-08-11 19:29:12, 12.0 KB) [[attachment:chap1-first_steps_solutions.rst]]
  • [get | view] (2018-08-11 19:29:17, 30.8 KB) [[attachment:chap2-list_and_for.ipynb]]
  • [get | view] (2018-08-11 19:29:23, 110.0 KB) [[attachment:chap2-list_and_for.pdf]]
  • [get | view] (2018-08-11 19:29:26, 17.6 KB) [[attachment:chap2-list_and_for.rst]]
  • [get | view] (2018-08-11 19:29:32, 42.0 KB) [[attachment:chap2-list_and_for_Solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:35, 116.1 KB) [[attachment:chap2-list_and_for_Solutions.pdf]]
  • [get | view] (2018-08-11 19:29:38, 25.6 KB) [[attachment:chap2-list_and_for_Solutions.rst]]
  • [get | view] (2018-08-11 19:29:51, 21.1 KB) [[attachment:chap3-if-solutions.ipynb]]
  • [get | view] (2018-08-11 19:29:58, 95.4 KB) [[attachment:chap3-if-solutions.pdf]]
  • [get | view] (2018-08-11 19:29:55, 9.0 KB) [[attachment:chap3-if-solutions.rst]]
  • [get | view] (2018-08-11 19:29:41, 11.8 KB) [[attachment:chap3-if.ipynb]]
  • [get | view] (2018-08-11 19:29:45, 91.6 KB) [[attachment:chap3-if.pdf]]
  • [get | view] (2018-08-11 19:29:48, 5.7 KB) [[attachment:chap3-if.rst]]
  • [get | view] (2018-08-11 19:30:02, 4.4 KB) [[attachment:chap4-functions.ipynb]]
  • [get | view] (2018-08-11 19:30:06, 83.1 KB) [[attachment:chap4-functions.pdf]]
  • [get | view] (2018-08-11 19:30:09, 2.3 KB) [[attachment:chap4-functions.rst]]
  • [get | view] (2018-08-11 19:30:20, 3.2 KB) [[attachment:chap5-while.ipynb]]
  • [get | view] (2018-08-11 19:30:14, 62.1 KB) [[attachment:chap5-while.pdf]]
  • [get | view] (2018-08-11 19:30:39, 1.5 KB) [[attachment:chap5-while.rst]]
  • [get | view] (2018-08-11 19:30:47, 4.0 KB) [[attachment:chap6-advanced_exercises.ipynb]]
  • [get | view] (2018-08-11 19:30:53, 69.7 KB) [[attachment:chap6-advanced_exercises.pdf]]
  • [get | view] (2018-08-11 19:31:00, 2.0 KB) [[attachment:chap6-advanced_exercises.rst]]
  • [get | view] (2018-08-12 22:02:04, 90.9 KB) [[attachment:euler.png]]
  • [get | view] (2018-08-13 21:52:57, 1.1 KB) [[attachment:flipper_nf_conversion.py]]
  • [get | view] (2018-08-15 12:18:55, 55.4 KB) [[attachment:flipper_tutorial.pdf]]
  • [get | view] (2018-08-12 21:56:57, 16.1 KB) [[attachment:floating_point_and_stability.ipynb]]
  • [get | view] (2018-08-12 21:57:01, 78.0 KB) [[attachment:floating_point_and_stability.pdf]]
  • [get | view] (2018-08-12 21:57:04, 6.6 KB) [[attachment:floating_point_and_stability.rst]]
  • [get | view] (2018-08-12 22:02:07, 17.6 KB) [[attachment:graph0.png]]
  • [get | view] (2018-08-11 23:44:38, 23.2 KB) [[attachment:intro.en.ipynb]]
  • [get | view] (2018-08-11 23:44:45, 117.5 KB) [[attachment:intro.en.pdf]]
  • [get | view] (2018-08-11 23:44:53, 12.5 KB) [[attachment:intro.en.rst]]
  • [get | view] (2018-08-12 23:25:35, 62.3 KB) [[attachment:logistic_orbit_interact.png]]
  • [get | view] (2018-08-11 19:48:34, 9.6 KB) [[attachment:random_walk.ipynb]]
  • [get | view] (2018-08-11 19:48:38, 87.9 KB) [[attachment:random_walk.pdf]]
  • [get | view] (2018-08-11 19:48:44, 5.7 KB) [[attachment:random_walk.rst]]
  • [get | view] (2018-08-15 19:25:46, 67.3 KB) [[attachment:real_and_complex_numbers.ipynb]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.