Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 127 additions & 26 deletions BINF_TP5.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO0SUJI6kaczFcOh8NoKcqb",
"include_colab_link": true
"provenance": []
},
"kernelspec": {
"name": "python3",
Expand All @@ -16,16 +14,6 @@
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/pparutto/BINF2025_TP5/blob/main/BINF_TP5.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
Expand Down Expand Up @@ -68,13 +56,36 @@
{
"cell_type": "code",
"source": [
"print(\"votre fonction ici !!\")"
"def BWT(s):\n",
" str = s + \"$\"\n",
" perm = []\n",
" for i in range(len(str)):\n",
" perm.append(str[i:] + str[:i])\n",
" perm.sort()\n",
" res = \"\"\n",
" for i in range(len(perm)):\n",
" res += perm[i][-1]\n",
" return res\n",
"\n",
"print(BWT(\"chien\"))"
],
"metadata": {
"id": "D5zJsnmQnajj"
"id": "D5zJsnmQnajj",
"outputId": "2418b079-26b6-452a-f932-d696ba2261d6",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": []
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"n$iche\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -135,13 +146,39 @@
{
"cell_type": "code",
"source": [
"print(\"Votre code ici !\")"
"def iBWT(t):\n",
"\n",
" n = len(t)\n",
" table = [\"\"] * n\n",
"\n",
" for _ in range(n):\n",
" for i in range(n):\n",
" table[i] = t[i] + table[i]\n",
" table.sort()\n",
"\n",
" for seq in table:\n",
" if seq.endswith(\"$\"):\n",
" return seq[:-1]\n",
"\n",
"print(iBWT(\"n$iche\"))\n"
],
"metadata": {
"id": "oH-bAVbgpOUe"
"id": "oH-bAVbgpOUe",
"outputId": "19609d92-190d-4726-d52e-2cc41153fca0",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": []
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"chien\n"
]
}
]
},
{
"cell_type": "markdown",
Expand All @@ -168,6 +205,7 @@
{
"cell_type": "markdown",
"source": [
"\n",
"### Exercice 3 : Recherche d’un mot dans la transformée\n",
"\n",
"Maintenant que l’on dispose de la transformée, on va développer un algorithme permettant de trouver efficacement si un mot se trouve dedans. Pour cela on va implémenter l’algorithme suivant qui provient de l’outil Bowtie base sur un principe similaire a la construction de table de l’exercice précédent. L’algorithme va reconstruire itérativement l’ensemble des lignes de la table contenant des fragments de plus en plus longs du mot recherche, jusqu’à obtenir l’ensemble des lignes contenant le mot entier. Vu que la table est toujours triée lexicographiquement, on sait que ces séquences vont se trouver cote a cote. L’algorithme est un peu plus complexe que la transformée inverse de l’exercice précédent car il ne reconstruit pas la table entière mais se base sur les propriétés de celle-ci pour trouver les indices :\n",
Expand All @@ -183,15 +221,78 @@
}
},
{
"cell_type": "code",
"source": [
"print(\"Votre code ici !\")"
"def build_C(bwt):\n",
" char_counts = {}\n",
" for char in sorted(set(bwt)):\n",
" char_counts[char] = bwt.count(char)\n",
"\n",
" C = {}\n",
" cumulative_count = 0\n",
" for char in sorted(char_counts.keys()):\n",
" C[char] = cumulative_count\n",
" cumulative_count += char_counts[char]\n",
" return C\n",
"\n",
"def build_Occ(bwt):\n",
" Occ = {}\n",
" for char in sorted(set(bwt)):\n",
" Occ[char] = [0] * (len(bwt) + 1)\n",
"\n",
" for i in range(len(bwt)):\n",
" for char in sorted(set(bwt)):\n",
" Occ[char][i+1] = Occ[char][i]\n",
" Occ[bwt[i]][i+1] += 1\n",
" return Occ\n",
"\n",
"def exactmatch(p, bwt):\n",
" n = len(bwt)\n",
" p_len = len(p)\n",
"\n",
" C = build_C(bwt)\n",
" Occ = build_Occ(bwt)\n",
" sp = 1\n",
" ep = n + 1\n",
"\n",
" for i in range(p_len - 1, -1, -1):\n",
" char = p[i]\n",
"\n",
" sp = C.get(char, 0) + Occ.get(char, [0] * (n + 1))[sp - 1] + 1\n",
" ep = C.get(char, 0) + Occ.get(char, [0] * (n + 1))[ep - 1] + 1\n",
"\n",
" if sp >= ep:\n",
" return (0, 0)\n",
"\n",
" return (sp, ep)\n",
"\n",
"bwt_example = \"BNANA$\"\n",
"p_example = \"ANA\"\n",
"sp, ep = exactmatch(p_example, bwt_example)\n",
"print(f\"Indices pour '{p_example}': ({sp}, {ep})\")\n",
"\n",
"p_example_not_found = \"XYZ\"\n",
"sp_nf, ep_nf = exactmatch(p_example_not_found, bwt_example)\n",
"print(f\"Indices pour '{p_example_not_found}': ({sp_nf}, {ep_nf})\")"
],
"cell_type": "code",
"metadata": {
"id": "C63yJjfjtUSQ"
"id": "SyhejN0NTMkq",
"outputId": "9b9c42fc-b801-4cff-c2a6-7203c9cf3bcc",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": []
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Indices pour 'ANA': (3, 4)\n",
"Indices pour 'XYZ': (0, 0)\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down