Skip to content

Commit

Permalink
fixes that make it a bit more consistent with what's shown in the pra…
Browse files Browse the repository at this point in the history
…ctical, comments make more sense
  • Loading branch information
BenLangmead committed Jul 24, 2015
1 parent b7d789c commit 1e20b36
Showing 1 changed file with 113 additions and 22 deletions.
135 changes: 113 additions & 22 deletions 3.02_GlobalAlignment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,114 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"alphabet = ['A', 'C', 'G', 'T']\n",
"score = [[0, 4, 2, 4, 8], \\\n",
" [4, 0, 4, 2, 8], \\\n",
" [2, 4, 0, 4, 8], \\\n",
" [4, 2, 4, 0, 8], \\\n",
"score = [[0, 4, 2, 4, 8],\n",
" [4, 0, 4, 2, 8],\n",
" [2, 4, 0, 4, 8],\n",
" [4, 2, 4, 0, 8],\n",
" [8, 8, 8, 8, 8]]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# converts from character to its offset in list alphabet\n",
"alphabet.index('A')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"alphabet.index('G')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# penalty associated with A (from X) mismatching with T (from Y)\n",
"score[alphabet.index('A')][alphabet.index('T')]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# penalty associated with C (from X) being deleted in Y\n",
"score[alphabet.index('C')][-1]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
Expand All @@ -30,28 +121,28 @@
" for i in range(len(x)+1):\n",
" D.append([0] * (len(y)+1))\n",
" \n",
" # Initialize first row and column of matrix\n",
" # Initialize first column\n",
" for i in range(1, len(x)+1):\n",
" D[i][0] = D[i-1][0] + score[alphabet.index(x[i-1])][-1] #!!!!!!!!!\n",
" D[i][0] = D[i-1][0] + score[alphabet.index(x[i-1])][-1]\n",
"\n",
" # Initialize first row\n",
" for i in range(1,len(y)+1):\n",
" D[0][i] = D[0][i-1] + score[-1][alphabet.index(y[i-1])] #!!!!!!!!!\n",
" D[0][i] = D[0][i-1] + score[-1][alphabet.index(y[i-1])]\n",
" \n",
" # Fill in the rest of the matrix\n",
" # Fill rest of the matrix\n",
" for i in range(1, len(x)+1):\n",
" for j in range(1, len(y)+1):\n",
" distHor = D[i][j-1] + score[4][alphabet.index(y[j-1])] #!!!!!!!!!\n",
" distVer = D[i-1][j] + score[alphabet.index(x[i-1])][4] #!!!!!!!\n",
" distDiag = D[i-1][j-1] + score[alphabet.index(x[i-1])][alphabet.index(y[j-1])] #!!\n",
" \n",
" distHor = D[i][j-1] + score[-1][alphabet.index(y[j-1])]\n",
" distVer = D[i-1][j] + score[alphabet.index(x[i-1])][-1]\n",
" distDiag = D[i-1][j-1] + score[alphabet.index(x[i-1])][alphabet.index(y[j-1])]\n",
" D[i][j] = min(distHor, distVer, distDiag)\n",
" \n",
" # Edit distance is the value in the bottom right corner of the matrix\n",
" return D[-1][-1]"
" return D[-1][-1] # return value in bottom right corner"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {
"collapsed": false
},
Expand All @@ -73,21 +164,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 1e20b36

Please sign in to comment.