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
267 changes: 234 additions & 33 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,42 @@
{
"cell_type": "code",
"source": [
"print(\"votre fonction ici !!\")"
"def BWT(s):\n",
" s += \"$\"\n",
"\n",
" n = len(s)\n",
" perm = [s[-i:] + s[:-i] for i in range(n)]\n",
"\n",
" indices = list(range(n))\n",
"\n",
" sorted_perm_indices = sorted(zip(perm, indices))\n",
" perm_sorted = [p for p, _ in sorted_perm_indices]\n",
" indices_sorted = [i for _, i in sorted_perm_indices]\n",
"\n",
" last_column = ''.join(p[-1] for p in perm_sorted)\n",
"\n",
" return last_column, indices_sorted\n",
"\n",
"bwt = BWT('chien')\n",
"print(bwt)"
],
"metadata": {
"id": "D5zJsnmQnajj"
"id": "D5zJsnmQnajj",
"outputId": "b72a5830-50a7-4c74-dc8f-f12880823132",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": []
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"('n$iche', [1, 0, 3, 5, 4, 2])\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -135,13 +152,39 @@
{
"cell_type": "code",
"source": [
"print(\"Votre code ici !\")"
"def iBWT(t):\n",
" n = len(t)\n",
"\n",
" table = [\"\" for _ in range(n)]\n",
"\n",
" for _ in range(n):\n",
" table = [t[i] + table[i] for i in range(n)]\n",
" table.sort()\n",
"\n",
" for seq in table:\n",
" if seq.endswith(\"$\"):\n",
" return seq[:-1]\n",
"\n",
"ibwt = iBWT(bwt[0])\n",
"print(ibwt)"
],
"metadata": {
"id": "oH-bAVbgpOUe"
"id": "oH-bAVbgpOUe",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d29a1d77-11b1-4da1-869a-4bcd2d8b1fef"
},
"execution_count": null,
"outputs": []
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"chien\n"
]
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -185,13 +228,69 @@
{
"cell_type": "code",
"source": [
"print(\"Votre code ici !\")"
"from collections import Counter\n",
"\n",
"def calculate_C(bwt):\n",
" freq = Counter(bwt)\n",
" sorted_chars = sorted(freq)\n",
" C = {}\n",
" total = 0\n",
" for char in sorted_chars:\n",
" C[char] = total\n",
" total += freq[char]\n",
" return C\n",
"\n",
"def calculate_occ(bwt):\n",
" occ = {}\n",
" for char in set(bwt):\n",
" occ[char] = [0] * (len(bwt) + 1)\n",
" for i, char in enumerate(bwt):\n",
" for c in occ:\n",
" occ[c][i + 1] = occ[c][i]\n",
" occ[char][i + 1] += 1\n",
" return occ\n",
"\n",
"def exactmatch(p, bwt):\n",
" C = calculate_C(bwt)\n",
" occ = calculate_occ(bwt)\n",
" p = list(p)\n",
" if p[-1] not in C:\n",
" return -1, -1\n",
" c = p[-1]\n",
" sp = C[c] + 1\n",
" ep = C[c] + occ[c][-1]\n",
" for i in range(len(p) - 2, -1, -1):\n",
" c = p[i]\n",
" if c not in C:\n",
" return -1, -1\n",
" sp = C[c] + occ[c][sp - 1] + 1\n",
" ep = C[c] + occ[c][ep]\n",
" if sp > ep:\n",
" return -1, -1\n",
" return sp, ep\n",
"\n",
"bwt = \"annb$aa\"\n",
"p = \"ana\"\n",
"sp, ep = exactmatch(p, bwt)\n",
"print(f\"Indices sp: {sp}, ep: {ep}\")\n"
],
"metadata": {
"id": "C63yJjfjtUSQ"
"id": "C63yJjfjtUSQ",
"outputId": "819b0940-d7c9-4674-d266-beee0ec002de",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": []
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Indices sp: 3, ep: 4\n"
]
}
]
},
{
"cell_type": "markdown",
Expand All @@ -217,13 +316,63 @@
{
"cell_type": "code",
"source": [
"print(\"Votre code ici !\")"
"def stepleft(k, bwt, C, occ):\n",
" char = bwt[k]\n",
" return C[char] + 1 + occ[char][k]\n",
"\n",
"def index_from_match(k, idxs, bwt, C, occ):\n",
" cnt = 0\n",
" while k not in idxs:\n",
" k = stepleft(k, bwt, C, occ)\n",
" cnt += 1\n",
" return (idxs[k] + cnt) % len(bwt)\n",
"\n",
"text = \"chien\"\n",
"bwt_str, idxs, sorted_perms = BWT(text)\n",
"C = calculate_C(bwt_str)\n",
"occ = calculate_occ(bwt_str)\n",
"\n",
"p = \"ien\"\n",
"sp, ep = exactmatch(p, bwt_str)\n",
"\n",
"positions = []\n",
"for k in range(sp - 1, ep):\n",
" pos = index_from_match(k, idxs, bwt_str, C, occ)\n",
" positions.append(pos)\n",
"\n",
"print(positions)\n",
"\n",
"text = \"chien\"\n",
"bwt_str, idxs, sorted_perms = BWT(text)\n",
"\n",
"print(\"Permutations triées :\")\n",
"for i, p in enumerate(sorted_perms):\n",
" print(f\"{i}: {p} (rotation depuis index {idxs[i]})\")"
],
"metadata": {
"id": "3RzUM2FJ91u_"
"id": "3RzUM2FJ91u_",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "53c09e84-6e6f-4e32-a216-76bbd618b57e"
},
"execution_count": null,
"outputs": []
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[4]\n",
"Permutations triées :\n",
"0: $chien (rotation depuis index 1)\n",
"1: chien$ (rotation depuis index 0)\n",
"2: en$chi (rotation depuis index 3)\n",
"3: hien$c (rotation depuis index 5)\n",
"4: ien$ch (rotation depuis index 4)\n",
"5: n$chie (rotation depuis index 2)\n"
]
}
]
},
{
"cell_type": "markdown",
Expand All @@ -241,13 +390,56 @@
{
"cell_type": "code",
"source": [
"print(\"Votre code ici !\")"
"import random\n",
"\n",
"def BWT_prog(s, k):\n",
" n = len(s)\n",
" seeds = sorted(random.sample(range(1, n), k - 1))\n",
" seeds = [0] + seeds + [n]\n",
"\n",
" print(\"Seeds:\", seeds)\n",
" print(\"Blocks:\", [s[seeds[i]:seeds[i + 1]] for i in range(len(seeds)-1)])\n",
"\n",
" bwt_result = \"\"\n",
" for i in range(len(seeds) - 1):\n",
" block = s[seeds[i]:seeds[i + 1]]\n",
" bwt_result += BWT(block)[0]\n",
"\n",
" return bwt_result\n",
"\n",
"random.seed(42)\n",
"\n",
"text = \"chien\"\n",
"bwt_prog_result_1 = BWT_prog(text, k=1)\n",
"bwt_prog_result = BWT_prog(text, k=3)\n",
"btw = BWT(text)\n",
"print(\"BWT prog result with 1 seed:\", bwt_prog_result_1)\n",
"print(\"BWT full string:\", btw[0])\n",
"print(\"BWT prog result:\", bwt_prog_result)"
],
"metadata": {
"id": "aoWF2thh8zEn"
"id": "aoWF2thh8zEn",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "1d791ee1-d263-40be-8d3c-d744a778d293"
},
"execution_count": null,
"outputs": []
"execution_count": 48,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Seeds: [0, 5]\n",
"Blocks: ['chien']\n",
"Seeds: [0, 1, 4, 5]\n",
"Blocks: ['c', 'hie', 'n']\n",
"BWT prog result with 1 seed: n$iche\n",
"BWT full string: n$iche\n",
"BWT prog result: c$ei$hn$\n"
]
}
]
},
{
"cell_type": "markdown",
Expand All @@ -264,6 +456,15 @@
"metadata": {
"id": "RsY4KN-x83cM"
}
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "EEpdjqEGCR6t"
},
"execution_count": null,
"outputs": []
}
]
}