{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from kerasy.utils import generateSeq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 60\n",
    "seed = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequence = \"\".join(generateSeq(size=n, nucleic_acid=\"RNA\", seed=seed))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AUGGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG\n"
     ]
    }
   ],
   "source": [
    "print(sequence)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from kerasy.bio.string import StringSearch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = StringSearch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Building Suffix Array...\n",
      "Building Auxiliary data structure for BWT...\n",
      "Building Longest-Common-Prefix Array...\n"
     ]
    }
   ],
   "source": [
    "model.build(sequence)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Suffix Array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " i SA  H Suffix\n",
      "---------------\n",
      " 0 60  0 $\n",
      " 1 54  2 AAAUUG$\n",
      " 2 14  2 AAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      " 3 20  3 AAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      " 4 55  1 AAUUG$\n",
      " 5 32  4 AGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      " 6 15  2 AGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      " 7 34  2 AGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      " 8 17  1 AGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      " 9 21  6 AUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "10 25  2 AUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "11 51  3 AUGAAAUUG$\n",
      "12  0  2 AUGGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "13 29  3 AUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "14 56  0 AUUG$\n",
      "15 10  4 CCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "16 41  2 CCCGCUGCCUAUGAAAUUG$\n",
      "17 11  3 CCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "18 38  4 CCGCCCGCUGCCUAUGAAAUUG$\n",
      "19 42  2 CCGCUGCCUAUGAAAUUG$\n",
      "20 48  1 CCUAUGAAAUUG$\n",
      "21 12  2 CGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "22 39  3 CGCCCGCUGCCUAUGAAAUUG$\n",
      "23  5  7 CGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "24 43  1 CGCUGCCUAUGAAAUUG$\n",
      "25 23  4 CUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "26 49  4 CUAUGAAAUUG$\n",
      "27 27  2 CUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "28  7  5 CUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "29 45  0 CUGCCUAUGAAAUUG$\n",
      "30 59  1 G$\n",
      "31 53  3 GAAAUUG$\n",
      "32 13  2 GAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "33 33  3 GAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "34 16  1 GAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "35  9  5 GCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "36 40  3 GCCCGCUGCCUAUGAAAUUG$\n",
      "37 37  3 GCCGCCCGCUGCCUAUGAAAUUG$\n",
      "38 47  2 GCCUAUGAAAUUG$\n",
      "39  6  6 GCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "40 44  1 GCUGCCUAUGAAAUUG$\n",
      "41 36  2 GGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "42 35  2 GGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "43  2  1 GGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "44 18  2 GUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "45  3  0 GUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "46 19  2 UAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "47 31  2 UAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "48 24  3 UAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "49 50  3 UAUGAAAUUG$\n",
      "50 28  1 UAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "51  4  2 UCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "52 22  5 UCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "53 26  1 UCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "54 58  2 UG$\n",
      "55 52  2 UGAAAUUG$\n",
      "56  8  4 UGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "57 46  2 UGCCUAUGAAAUUG$\n",
      "58  1  1 UGGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "59 30  2 UUAGAGGGCCGCCCGCUGCCUAUGAAAUUG$\n",
      "60 57 -1 UUG$\n"
     ]
    }
   ],
   "source": [
    "model.SuffixArray()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "Met = \"AUG\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([51,  0])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.search(Met)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \u001b[01mSuffix Array\u001b[0m\n",
      "Number of matches: \u001b[31m2\u001b[0m\n",
      "\n",
      "=========================================================================\n",
      "S: [00] AUGGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG [59]\n",
      " : [00] ***                                                ***       [59]\n",
      "=========================================================================\n"
     ]
    }
   ],
   "source": [
    "model.where(Met)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Burrows-Wheeler Transform (BWT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "bwt = \"\".join(model.BWT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GGGUAUAGGAUU$UAGGCGCGCCUCUCUGGUUCAAUCGUCCGAUAGGUCCCGAAUACCAAA\n"
     ]
    }
   ],
   "source": [
    "print(bwt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Reverse BWT without Suffix Array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from kerasy.bio.string import reverseBWT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = reverseBWT(bwt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "original               : AUGGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG\n",
      "original→SA→BWT→reverse: AUGGUCGCUGCCCGAAGAGUAAUCUAUCUAUUAGAGGGCCGCCCGCUGCCUAUGAAAUUG\n"
     ]
    }
   ],
   "source": [
    "print(f\"original               : {sequence}\")\n",
    "print(f\"original→SA→BWT→reverse: {string}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Perfectly restored from BWT !!**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compression "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to check the function `getAlltext` deeply, please visit [\"Python-utils\" repository](https://github.com/iwasakishuto/Python-utils/blob/22e0a1e7041f61bf63882a9db535bf777ae655bb/Scraping/scrap_utils.py#L5)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Scraping.scrap_utils import getAlltext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "# get All text in 'https://en.wikipedia.org/wiki/Keras'.\n",
    "text = getAlltext(\"https://en.wikipedia.org/wiki/Keras\", joint=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from kerasy.bio.string import SAIS, mkBWT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "SA = SAIS(text)\n",
    "BWT = mkBWT(text, SA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "bwt = \"\".join(BWT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from kerasy.bio.string import simple_compression\n",
    "from kerasy.bio.string import simple_decompression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "bwt_compressed = simple_compression(bwt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BWT Compression rate: 76.962%\n"
     ]
    }
   ],
   "source": [
    "print(f\"BWT Compression rate: {len(bwt_compressed)/len(bwt)*100:.3f}%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
