Skip to content

Commit

Permalink
Added BWT naive implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinyu95 committed Jun 20, 2011
1 parent e498a75 commit 583aae2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
14 changes: 13 additions & 1 deletion transform/BWT/ref/memo.txt
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
http://marknelson.us/1996/09/01/bwt/

Michael Burrows and David Wheele: ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/SRC-124.pdf

Michael Dipperstein
Burrows-Wheeler Transform Discussion and Implementation
http://michael.dipperstein.com/bwt/

Mark Nelson
Data Compression with the Burrows-Wheeler Transform
http://marknelson.us/1996/09/01/bwt/

WIKI
http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform
29 changes: 29 additions & 0 deletions transform/BWT/src/bwt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/python

# Algorithm 1, Naive version from [1]

# using \0 as the special EOF char
def bwt(s):
s += "\0"
table = sorted(s[i:] + s[:i] for i in range(len(s)))
last_colum = [row[-1:] for row in table]
return "".join(last_colum)

def ibwt(r):
table = [""] * len(r)
for i in range(len(r)):
table = sorted(r[i] + table[i] for i in range(len(r)))
s = [row for row in table if row.endswith("\0")][0]
return s.rstrip("\0")

def test():
s = "this is the demo program of bwt transform in the real programming code"
r = bwt(s)
print "bwt(", s, ") ==>", r
print "ibwt(", r, ") ==>", ibwt(r)

if __name__ == "__main__":
test()

# reference
# [1] WIKI. http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform

0 comments on commit 583aae2

Please sign in to comment.