{ "metadata": { }, "nbformat": 4, "nbformat_minor": 5, "cells": [ { "id": "metadata", "cell_type": "markdown", "source": "
Preclass prep: Chapters 5 and 7 from “Think Python”
\n\n\nThis material uses examples from notebooks developed by Ben Langmead
\n
An excellent way to illustrate the utility of lists is to implement a dynamic programming algorithm for sequence alignment. Suppose we have two sequences that deliberately have different lengths:
\n\n\\[\\texttt{G C T A T A C}$\\]\nand
\n\n\\[\\texttt{G C G T A T G C}$\\]\nLet’s represent them as the following matrix where the first character \\(\\epsilon\\) represents an empty string:
\n\n\\[\\begin{array}{ c | c | c | c | c | c | c}\n& \\epsilon & G & C & T & A & T & A & C\\\\\n\\hline\n\\epsilon \\\\\n\\hline\nG\\\\\n\\hline\n C\\\\\n \\hline\nG\\\\\n\\hline\nT\\\\\n\\hline\nA\\\\\n \\hline\nT\\\\\n\\hline\nG\\\\\n\\hline\nC\n\\end{array}\n\\\\\n\\textbf{Note}: sequence\\ \\texttt{X}\\ is\\ vertical\\ and\\ sequence\\ \\texttt{Y}\\ is\\ horizontal.\\]\nIn this matrix, the cells are addressed as shown below. They filled in using the following logic:
\n\n\\[D[i,j] = min\\begin{cases}\n \\color{green}{D[i-1,j] + 1} & \\\\\n \\color{blue}{D[i,j-1] + 1} & \\\\\n \\color{red}{D[i-1,j-1] + \\delta(x[i-1],y[j-1])}\n \\end{cases}\\]\nwhere \\(\\color{green}{green}\\) is upper cell, \\(\\color{blue}{blue}\\) is left cell, and \\(\\color{red}{red}\\) is upper-left cell:
\n\n\\[\\begin{array}{ c | c | c | c | c | c | c}\n& \\epsilon & G & C & T & A & T & A & C\\\\\n \\hline\n \\epsilon & & & & & & & & \\\\\n \\hline\n G & \\\\\n \\hline\n C & & & \\color{red}{D[2,2]} & \\color{green}{D[2,3]}\\\\\n \\hline\n G & & & \\color{blue}{D[3,2]} & D[3,3]\\\\\n \\hline\n T & \\\\\n \\hline\n A & \\\\\n \\hline\n T & \\\\\n \\hline\n G & \\\\\n \\hline\n C &\n\\end{array}\n\\\\\n\\textbf{Note}: sequence\\ \\texttt{X}\\ is\\ vertical\\ and\\ sequence\\ \\texttt{Y}\\ is\\ horizontal.\\]\nLet’s initialize the first column and first row of the matrix. Because the distance between a string and an empty string is equal to the length of the\nstring (e.g., a distance between, say, string \\(\\texttt{TCG}\\) and an empty string is 3) this resulting matrix will look like this:
\n\n\\[\\begin{array}{ c | c | c | c | c | c | c}\n& \\epsilon & G & C & T & A & T & A & C\\\\\n \\hline\n \\epsilon & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7\\\\\n \\hline\n G & 1\\\\\n \\hline\n C & 2\\\\\n \\hline\n G & 3\\\\\n \\hline\n T & 4\\\\\n \\hline\n A & 5\\\\\n \\hline\n T & 6\\\\\n \\hline\n G & 7\\\\\n \\hline\n C & 8\n\\end{array}\n\\\\\n\\textbf{Note}: sequence\\ \\texttt{X}\\ is\\ vertical\\ and\\ sequence\\ \\texttt{Y}\\ is\\ horizontal.\\]\nThis can be achieved with the following code:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-1", "source": [ "D = np.zeros((len(x)+1, len(y)+1), dtype=int)\n", "D[0, 1:] = range(1, len(y)+1)\n", "D[1:, 0] = range(1, len(x)+1)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-2", "source": "Now we can fill the entire matrix by using two nested loops: one iterating over \\(i\\) coordinate (sequence \\(x\\)) and the other iterating over \\(j\\) coordinate (sequence \\(y\\)):
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-3", "source": [ "for i in range(1, len(x)+1):\n", " for j in range(1, len(y)+1):\n", " delt = 1 if x[i-1] != y[j-1] else 0\n", " D[i, j] = min(D[i-1, j-1]+delt, D[i-1, j]+1, D[i, j-1]+1)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-4", "source": "Let’s start with the cell between \\(\\texttt{G}\\) and \\(\\texttt{G}\\). Recall that:
\n\n\\[D[i,j] = min\\begin{cases}\n \\color{green}{D[i-1,j] + 1} & \\\\\n \\color{blue}{D[i,j-1] + 1} & \\\\\n \\color{red}{D[i-1,j-1] + \\delta(x[i-1],y[j-1])}\n \\end{cases}\\]\nwhere \\(\\delta(x[i-1],y[j-1]) = 0\\) if \\(x[i-1] = y[j-1]\\) and \\(1\\) otherwise. And now let’s color each of the cells corresponding to each part of the above expression:
\n\n\\[\\begin{array}{ c | c | c | c | c | c | c}\n& \\epsilon & G & C & T & A & T & A & C\\\\\n\\hline\n\\epsilon & \\color{red}0 & \\color{green}1 & 2 & 3 & 4 & 5 & 6 & 7\\\\\n\\hline\nG & \\color{blue}1\\\\\n \\hline\n C & 2\\\\\n \\hline\n G & 3\\\\\n \\hline\n T & 4\\\\\n \\hline\n A & 5\\\\\n \\hline\n T & 6\\\\\n \\hline\n G & 7\\\\\n \\hline\n C & 8\n\\end{array}\n\\\\\n\\textbf{Note}: sequence\\ \\texttt{X}\\ is\\ vertical\\ and\\ sequence\\ \\texttt{Y}\\ is\\ horizontal.\\]\nIn this specific case:
\n\n\\[D[i,j] = min\\begin{cases}\n \\color{green}{D[i-1,j] + 1}\\ or\\ 0+0=0 & \\\\\n \\color{blue}{D[i,j-1] + 1}\\ or\\ 1+1=2 & \\\\\n \\color{red}{D[i-1,j-1] + \\delta(x[i-1],y[j-1])}\\ or\\ 1+1=2\n \\end{cases}\\]\nThe minimum of 0, 2, and 2 will be 0, so we are putting zero into that cell:
\n\n\\[\\begin{array}{ c | c | c | c | c | c | c}\n& \\epsilon & G & C & T & A & T & A & C\\\\\n\\hline\n\\epsilon & \\color{red}0 & \\color{green}1 & 2 & 3 & 4 & 5 & 6 & 7\\\\\n\\hline\nG & \\color{blue}1 & \\color{red}0\\\\\n \\hline\n C & 2\\\\\n \\hline\n G & 3\\\\\n \\hline\n T & 4\\\\\n \\hline\n A & 5\\\\\n \\hline\n T & 6\\\\\n \\hline\n G & 7\\\\\n \\hline\n C & 8\n\\end{array}\n\\\\\n\\textbf{Note}: sequence\\ \\texttt{X}\\ is\\ vertical\\ and\\ sequence\\ \\texttt{Y}\\ is\\ horizontal.\\]\nUsing this logic we can fill the entire matrix like this:
\n\n\\[\\begin{array}{ c | c | c | c | c | c | c}\n& \\epsilon & G & C & T & A & T & A & C\\\\\n\\hline\n\\epsilon & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7\\\\\n\\hline\n G & 1 & 0 & 1 & 2 & 3 & 4 & 5 & 6\\\\\n \\hline\n C & 2 & 1 & 0 & 1 & 2 & 3 & 4 & 5\\\\\n \\hline\n G & 3 & 2 & 1 & 1 & 2 & 3 & 4 & 5\\\\\n \\hline\n T & 4 & 3 & 2 & 1 & 2 & 2 & 3 & 4\\\\\n \\hline\n A & 5 & 4 & 3 & 2 & 1 & 2 & 2 & 3\\\\\n \\hline\n T & 6 & 5 & 4 & 3 & 2 & 1 & 2 & 3\\\\\n \\hline\n G & 7 & 6 & 5 & 4 & 3 & 2 & 2 & 3\\\\\n \\hline\n C & 8 & 7 & 6 & 5 & 4 & 3 & 3 & \\color{red}2\n\\end{array}\n\\\\\n\\textbf{Note}: sequence\\ \\texttt{X}\\ is\\ vertical\\ and\\ sequence\\ \\texttt{Y}\\ is\\ horizontal.\\]\nThe lower rightmost cell highlighted in red is special. It contains the value for the edit distance between the two strings.\nThe following Python script implements this idea. You can see that it is essentially instantaneous:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-5", "source": [ "import numpy as np\n", "\n", "def edDistDp(x, y):\n", " \"\"\" Calculate edit distance between sequences x and y using\n", " matrix dynamic programming. Return matrix and distance. \"\"\"\n", " D = np.zeros((len(x)+1, len(y)+1), dtype=int)\n", " D[0, 1:] = range(1, len(y)+1)\n", " D[1:, 0] = range(1, len(x)+1)\n", " for i in range(1, len(x)+1):\n", " for j in range(1, len(y)+1):\n", " delt = 1 if x[i-1] != y[j-1] else 0\n", " D[i, j] = min(D[i-1, j-1]+delt, D[i-1, j]+1, D[i, j-1]+1)\n", " return D,D[len(x),len(y)]" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-6", "source": "A graphical representation of the matrix between GCGTATGCACGC
and GCTATGCCACGC
looks like this:
This image is generated using Seaborn package using matrix directly:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-7", "source": [ "sns.heatmap(D,annot=True,cmap=\"crest\")" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-8", "source": "Perhaps the best way to demonstrate the utility of dictionaries is by using DNA-to-Protein translation as an example.
\nThe following dictionary maps codons to corresponding amino acid translations. In this case, codon is the key and amino acid is the value:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-9", "source": [ "table = {\n", " 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',\n", " 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',\n", " 'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',\n", " 'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',\n", " 'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',\n", " 'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',\n", " 'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',\n", " 'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',\n", " 'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',\n", " 'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',\n", " 'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',\n", " 'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',\n", " 'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',\n", " 'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',\n", " 'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',\n", " 'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W',\n", " }" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-10", "source": "Let’s generate a random DNA sequence:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-11", "source": [ "import random\n", "seq = \"\".join([random.choice('atcg') for x in range(100)])" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-12", "source": "\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-13", "source": [ "seq" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-14", "source": "‘agaccgtagcccaagtgcgtttgaatgtggctacttgggaggatttcattgcggtctgtctccgtacttgttattggtcttctttctgcattatgacgca’
\nTo translate this sequence we write a code that uses a for
loop that iterates over the DNA sequence in steps of 3, creating a codon at each iteration. If the codon is less than 3 letters long, the loop is broken. The resulting amino acid is then added to the translation
string:
Translation: RP_PKCV_MWLLGRISLRSVSVLVIGLLSAL_R
\nNote that the code uses the upper()
method to ensure the codon is in uppercase since the table dictionary is case-sensitive. Additionally, the code checks if the codon is in the table dictionary and if not, it adds the letter “X” to the translation. This is a common way to represent unknown or stop codons in a protein sequence.
‘RP_PKCV_MWLLGRISLRSVSVLVIGLLSAL_R’
\nNow we define a function that would perform translation so that we can reuse it later:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-19", "source": [ "def translate(seq):\n", " translation = ''\n", " table = {\n", " 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',\n", " 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',\n", " 'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',\n", " 'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',\n", " 'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',\n", " 'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',\n", " 'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',\n", " 'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',\n", " 'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',\n", " 'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',\n", " 'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',\n", " 'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',\n", " 'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',\n", " 'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',\n", " 'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',\n", " 'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W',\n", " }\n", " for i in range(0, len(seq), 3):\n", " codon = seq[i:i+3].upper()\n", " if len(codon) < 3: break\n", " if codon in table:\n", " translation += table[codon]\n", " else:\n", " translation += \"X\"\n", " return(translation)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-20", "source": "\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-21", "source": [ "translate(seq)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-22", "source": "‘RP_PKCV_MWLLGRISLRSVSVLVIGLLSAL_R’
\nWe can further modify the function by adding a phase
parameter that would allow translating in any of the three phases:
‘TVAQVRLNVATWEDFIAVCLRTCYWSSFCIMT’
\nTo translate in all six reading frames (three of the “+” strand and three of the “-“ strand) we need to be able to create a reverse complement of the sequence. Let’s write a simple function for that.\nThe cell below implements a function revcomp
that takes a DNA sequence as input and returns its reverse complement. It works by first reversing the sequence using the slice\nnotation seq[::-1]
, which returns the sequence in reverse order. Then, the translate
method is used with the str.maketrans
function to replace each occurrence\nof ‘a’, ‘t’, ‘g’, ‘c’, ‘A’, ‘T’, ‘G’, and ‘C’ in the reversed sequence with ‘t’, ‘a’, ‘c’, ‘g’, ‘T’, ‘A’, ‘C’, and ‘G’, respectively:
Now let’s use this function to create translation in all six reading frames. The cell below uses a for
loop that iterates over the range (0, 3)
, representing the different phases\n(or starting positions) of the translation. At each iteration, the translate_phase
function is called with the DNA sequence and the current phase, and the resulting protein sequence\nis appended to the translations
list along with the phase
and the strand
orientation (+ or -):
[(‘RP_PKCV_MWLLGRISLRSVSVLVIGLLSAL_R’, ‘0’, ‘+’),\n (‘SLIIDKQQE_ELATDRRIKWWELRRLKASSRSL’, ‘0’, ‘-‘),\n (‘DRSPSAFECGYLGGFHCGLSPYLLLVFFLHYDA’, ‘1’, ‘+’),\n (‘R__STNNRNKN_PQTGESNGGNYGD_KRVPVAC’, ‘1’, ‘-‘),\n (‘TVAQVRLNVATWEDFIAVCLRTCYWSSFCIMT’, ‘2’, ‘+’),\n (‘ADNRQTTGIRTSHRQANQMVGTTEIESEFP_P’, ‘2’, ‘-‘)]
\nThe translation we’ve generated above contains stops (e.g., _
symbols). The actual biologically relevant protein sequences are between stops.\nWe now need to split translation strings into meaningful peptide sequences and compute their coordinates. Let’s begin by splitting a string on _
and computing the start and end positions of each peptide:
[6, 11]
\nThe code above generates a list of split indices for a string. The list contains the indices of the characters in the string that match a specified character (in this case, the underscore _
character).
The enumerate
function is used to loop over the characters in the string, and at each iteration, the current index and character are stored in the variables i
and char
, respectively.\nIf the current character matches the specified character, the index i
is appended to the split_indices
list.
After the loop, the split_indices
list is printed to the console. For the input string \"aadsds_dsds_dsds\"
, the output would be [6, 11]
, indicating that the dashes are located at indices 6 and 11.
But we actually need coordinates of peptides bound by _
characters. To get to this let’s first modify split_indices
by adding the beginning and end:
[-1, 6, 11, 16]
\nNow, let’s convert these to ranges and also stick the peptide sequence in:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-37", "source": [ "string = \"aadsds_dsds_dsds\"\n", "\n", "split_indices = []\n", "for i, char in enumerate(string):\n", " if char == \"_\":\n", " split_indices.append(i)\n", "\n", "split_indices.insert(0, -1)\n", "split_indices.append(len(string))\n", "\n", "orfs = string.split('_')\n", "\n", "parts = []\n", "for i in range(len(split_indices)-1):\n", " parts.append((orfs[i],split_indices[i]+1, split_indices[i+1]))\n", "\n", "print(parts)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-38", "source": "[(‘aadsds’, 0, 6), (‘dsds’, 7, 11), (‘dsds’, 12, 16)]
\nNow let’s convert this to function:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-39", "source": [ "def extract_coords(translation):\n", " split_indices = []\n", " for i, char in enumerate(translation):\n", " if char == \"_\":\n", " split_indices.append(i)\n", "\n", " split_indices.insert(0, -1)\n", " split_indices.append(len(translation))\n", "\n", " parts = []\n", " for i in range(len(split_indices)-1):\n", " parts.append((translation.split('_')[i], split_indices[i] + 1, split_indices[i + 1]))\n", "\n", " return(parts)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-40", "source": "\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-41", "source": [ "extract_coords(string)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-42", "source": "[(‘aadsds’, 0, 6), (‘dsds’, 7, 11), (‘dsds’, 12, 16)]
\nAnd specify the right parameters to make it truly generic:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-43", "source": [ "def extract_coords_with_annotation(separator,translation,phase,strand):\n", " split_indices = []\n", " for i,char in enumerate(translation):\n", " if char == separator:\n", " split_indices.append(i)\n", "\n", " split_indices.insert(0,-1)\n", " split_indices.append(len(translation))\n", "\n", " parts = []\n", " for i in range(len(split_indices)-1):\n", " parts.append((translation.split(separator)[i], phase, strand, split_indices[i]+1, split_indices[i+1]))\n", "\n", " return(parts)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-44", "source": "\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-45", "source": [ "extract_coords_with_annotation('_', string, '0', '+')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-46", "source": "[(‘aadsds’, ‘0’, ‘+’, 0, 6),\n (‘dsds’, ‘0’, ‘+’, 7, 11),\n (‘dsds’, ‘0’, ‘+’, 12, 16)]
\nWe begin by parsing the translations
list we defined above:
[[(‘RP’, ‘0’, ‘+’, 0, 2),\n (‘PKCV’, ‘0’, ‘+’, 3, 7),\n (‘MWLLGRISLRSVSVLVIGLLSAL’, ‘0’, ‘+’, 8, 31),\n (‘R’, ‘0’, ‘+’, 32, 33)],\n [(‘SLIIDKQQE’, ‘0’, ‘-‘, 0, 9),\n (‘ELATDRRIKWWELRRLKASSRSL’, ‘0’, ‘-‘, 10, 33)],\n [(‘DRSPSAFECGYLGGFHCGLSPYLLLVFFLHYDA’, ‘1’, ‘+’, 0, 33)],\n [(‘R’, ‘1’, ‘-‘, 0, 1),\n (‘’, ‘1’, ‘-‘, 2, 2),\n (‘STNNRNKN’, ‘1’, ‘-‘, 3, 11),\n (‘PQTGESNGGNYGD’, ‘1’, ‘-‘, 12, 25),\n (‘KRVPVAC’, ‘1’, ‘-‘, 26, 33)],\n [(‘TVAQVRLNVATWEDFIAVCLRTCYWSSFCIMT’, ‘2’, ‘+’, 0, 32)],\n [(‘ADNRQTTGIRTSHRQANQMVGTTEIESEFP’, ‘2’, ‘-‘, 0, 30),\n (‘P’, ‘2’, ‘-‘, 31, 32)]]
\nNow the problem with this list is that it is nested. However, we need to make it flat:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-51", "source": [ "flat_list = []\n", "for sublist in all_translations:\n", " for item in sublist:\n", " flat_list.append(item)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-52", "source": "\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-53", "source": [ "flat_list" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-54", "source": "[(‘RP’, ‘0’, ‘+’, 0, 2),\n (‘PKCV’, ‘0’, ‘+’, 3, 7),\n (‘MWLLGRISLRSVSVLVIGLLSAL’, ‘0’, ‘+’, 8, 31),\n (‘R’, ‘0’, ‘+’, 32, 33),\n (‘SLIIDKQQE’, ‘0’, ‘-‘, 0, 9),\n (‘ELATDRRIKWWELRRLKASSRSL’, ‘0’, ‘-‘, 10, 33),\n (‘DRSPSAFECGYLGGFHCGLSPYLLLVFFLHYDA’, ‘1’, ‘+’, 0, 33),\n (‘R’, ‘1’, ‘-‘, 0, 1),\n (‘’, ‘1’, ‘-‘, 2, 2),\n (‘STNNRNKN’, ‘1’, ‘-‘, 3, 11),\n (‘PQTGESNGGNYGD’, ‘1’, ‘-‘, 12, 25),\n (‘KRVPVAC’, ‘1’, ‘-‘, 26, 33),\n (‘TVAQVRLNVATWEDFIAVCLRTCYWSSFCIMT’, ‘2’, ‘+’, 0, 32),\n (‘ADNRQTTGIRTSHRQANQMVGTTEIESEFP’, ‘2’, ‘-‘, 0, 30),\n (‘P’, ‘2’, ‘-‘, 31, 32)]
\nNow we can load the list into Pandas
and plot away:
\n | aa | \nframe | \nphase | \nstart | \nend | \n
---|---|---|---|---|---|
0 | \nRP | \n0 | \n+ | \n0 | \n2 | \n
1 | \nPKCV | \n0 | \n+ | \n3 | \n7 | \n
2 | \nMWLLGRISLRSVSVLVIGLLSAL | \n0 | \n+ | \n8 | \n31 | \n
3 | \nR | \n0 | \n+ | \n32 | \n33 | \n
4 | \nSLIIDKQQE | \n0 | \n- | \n0 | \n9 | \n
5 | \nELATDRRIKWWELRRLKASSRSL | \n0 | \n- | \n10 | \n33 | \n
6 | \nDRSPSAFECGYLGGFHCGLSPYLLLVFFLHYDA | \n1 | \n+ | \n0 | \n33 | \n
7 | \nR | \n1 | \n- | \n0 | \n1 | \n
8 | \n\n | 1 | \n- | \n2 | \n2 | \n
9 | \nSTNNRNKN | \n1 | \n- | \n3 | \n11 | \n
10 | \nPQTGESNGGNYGD | \n1 | \n- | \n12 | \n25 | \n
11 | \nKRVPVAC | \n1 | \n- | \n26 | \n33 | \n
12 | \nTVAQVRLNVATWEDFIAVCLRTCYWSSFCIMT | \n2 | \n+ | \n0 | \n32 | \n
13 | \nADNRQTTGIRTSHRQANQMVGTTEIESEFP | \n2 | \n- | \n0 | \n30 | \n
14 | \nP | \n2 | \n- | \n31 | \n32 | \n
Now let’s plot it:
\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-59", "source": [ "import altair as alt\n", "\n", "plus = alt.Chart(df[df['phase']=='+']).mark_rect().encode(\n", " x = alt.X('start:Q'),\n", " x2 = alt.X2('end:Q'),\n", " y = alt.Y('frame:N'),\n", " color='frame',\n", " tooltip='aa:N'\n", " ).properties(\n", " width=600,\n", " height=100)\n", "\n", "minus = alt.Chart(df[df['phase']=='-']).mark_rect().encode(\n", " x = alt.X('start:Q',sort=alt.EncodingSortField('start:Q', order='descending')),\n", " x2 = alt.X2('end:Q'),\n", " y = alt.Y('frame:N'),\n", " color='frame',\n", " tooltip='aa:N'\n", " ).properties(\n", " width=600,\n", " height=100)\n", "\n", "plus & minus" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "" ], "id": "" } } }, { "id": "cell-60", "source": "