{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Like the naive algorithm but we break out of the inner loop as soon as our\n", "# mismatch budget exceeds the maximum allowed Hamming distance.\n", "def naive_approx_hamming(p, t, maxHammingDistance=1):\n", " occurrences = []\n", " for i in range(0, len(t) - len(p) + 1): # for all alignments\n", " nmm = 0\n", " for j in range(0, len(p)): # for all characters\n", " if t[i+j] != p[j]: # does it match?\n", " nmm += 1 # mismatch\n", " if nmm > maxHammingDistance:\n", " break # exceeded maximum distance\n", " if nmm <= maxHammingDistance:\n", " # approximate match; return pair where first element is the\n", " # offset of the match and second is the Hamming distance\n", " occurrences.append((i, nmm))\n", " return occurrences" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (7, 2)]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "naive_approx_hamming('needle', 'needle noodle nargle', maxHammingDistance=2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }