From 695d0fcb32004105c8325568b9c43ac714316b25 Mon Sep 17 00:00:00 2001 From: Laurane Deckers Date: Wed, 12 Mar 2025 16:49:33 +0100 Subject: [PATCH] rendu TP4 --- BINF2025_TP4.ipynb | 68 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/BINF2025_TP4.ipynb b/BINF2025_TP4.ipynb index be4f132..be5b265 100644 --- a/BINF2025_TP4.ipynb +++ b/BINF2025_TP4.ipynb @@ -3,9 +3,7 @@ "nbformat_minor": 0, "metadata": { "colab": { - "provenance": [], - "authorship_tag": "ABX9TyOkaYHMV5kAI2JXjqDKbiVa", - "include_colab_link": true + "provenance": [] }, "kernelspec": { "name": "python3", @@ -19,8 +17,7 @@ { "cell_type": "markdown", "metadata": { - "id": "view-in-github", - "colab_type": "text" + "id": "view-in-github" }, "source": [ "\"Open" @@ -60,7 +57,7 @@ "metadata": { "id": "A8LhiAAeP9u-" }, - "execution_count": null, + "execution_count": 1, "outputs": [] }, { @@ -75,6 +72,8 @@ { "cell_type": "code", "source": [ + "import numpy as np\n", + "\n", "def read_fasta(fname):\n", " #Read a fasta file with multiple entries.\n", " #Returns a list of sequences.\n", @@ -123,7 +122,7 @@ "metadata": { "id": "30qlDWTmQ47E" }, - "execution_count": null, + "execution_count": 3, "outputs": [] }, { @@ -184,12 +183,20 @@ { "cell_type": "code", "source": [ - "#Votre code ici" + "def mark_columns(A: list, alpha: float) -> list :\n", + " marked = []\n", + " for i in range(len(A)) :\n", + " count = 0\n", + " for char in len(A[i]) :\n", + " count += char == \"-\"\n", + " if count / len(A[i]) < alpha :\n", + " marked.append(i)\n", + " return marked" ], "metadata": { "id": "ikf5pcUtRYvV" }, - "execution_count": null, + "execution_count": 4, "outputs": [] }, { @@ -222,7 +229,48 @@ { "cell_type": "code", "source": [ - "#Votre code ici" + "def build_HMM(A: list, mark: list) :\n", + " # Step 1: init matrixes\n", + " N = len(mark)\n", + " T = np.ones(shape=(N+1, 9))\n", + " eM = np.ones(shape=(N+1, 20))\n", + " eI = np.ones(shape=(N+1, 20))\n", + " T[:,0] = 0\n", + " T[:,T.shape[1] - 1] = 0\n", + " eM[:,0] = 0\n", + "\n", + " # Step 2: fill matrixes\n", + " pi = []\n", + " for k in range(1, len(A)) :\n", + " for l in range(1, len(A[k])):\n", + " if l in mark :\n", + " if A[k][l] == \"-\" :\n", + " pi[l] = \"D\"\n", + " else :\n", + " pi[l] = \"M\"\n", + " else :\n", + " if A[k][l] == \"-\" :\n", + " pi[l] = \"0\"\n", + " else :\n", + " pi[l] = \"I\"\n", + "\n", + " l0 = 0\n", + " while l0 < len(pi) and A[k][l0] == \"-\" :\n", + " l0 += 1\n", + " if l0 == len(pi) :\n", + " eI[0][A[k][l0]]\n", + " T[0][M] += A[k][l0]\n", + "\n", + " u = 0\n", + " for l in range(l0, len(pi)) :\n", + " if l in mark :\n", + " u += 1\n", + " elif pi[l] == \"0\" :\n", + " continue\n", + " elif pi[l] == \"M\" and A[k][l] != \"-\" :\n", + " eM[u][A[k][l]] += 1\n", + " elif pi[l] == \"I\" and A[k][l] != \"-\" :\n", + " eI[u][A[k][l]] += 1" ], "metadata": { "id": "Y3TXPqNhTwNo"