{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def rotations(t):\n", " # Return list of rotations of input string t\n", " tt = t * 2\n", " return [ tt[i:i+len(t)] for i in range(len(t)) ]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['cat', 'atc', 'tca']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rotations('cat')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def bwm(t):\n", " # Return lexicographically sorted list of t’s rotations\n", " return sorted(rotations(t))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['$abaaba', 'a$abaab', 'aaba$ab', 'aba$aba', 'abaaba$', 'ba$abaa', 'baaba$a']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bwm('abaaba$')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "$abaaba\n", "a$abaab\n", "aaba$ab\n", "aba$aba\n", "abaaba$\n", "ba$abaa\n", "baaba$a\n" ] } ], "source": [ "print('\\n'.join(bwm('abaaba$')))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def bwtViaBwm(t):\n", " # Given T, returns BWT(T) by way of the BWM\n", " return ''.join(map(lambda x: x[-1], bwm(t)))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abba$aa'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bwtViaBwm('abaaba$') # we can see the result equals the last column of the matrix above" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def suffixArray(s):\n", " satups = sorted([(s[i:], i) for i in range(len(s))])\n", " return map(lambda x: x[1], satups)\n", "\n", "def bwtViaSa(t):\n", " # Given T, returns BWT(T) by way of the suffix array\n", " bw = []\n", " for si in suffixArray(t):\n", " if si == 0:\n", " bw.append('$')\n", " else:\n", " bw.append(t[si-1])\n", " return ''.join(bw) # return string-ized version of list bw" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('abba$aa', 'abba$aa')" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bwtViaBwm('abaaba$'), bwtViaSa('abaaba$') # same result" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }