See More

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Quick recap\n", "\n", "- code blocks and conditional execution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# code blocks\n", "if condition:\n", " # do this\n", "else\n", " # do that" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_value = 12\n", "if my_value > 5:\n", " print('value bigger than 5:', my_value)\n", "else:\n", " print('value lower than 5:', my_value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Session 2.2\n", "- for and while loops" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# for loops\n", "my_values = [1, 3, 5, 7, 3, 9]\n", "for v in my_values:\n", " print(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# combine with previous condition\n", "for v in my_values:\n", " if v > 5:\n", " print(v, 'is > than 5')\n", " else:\n", " print(v, 'is =< than 5')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "help(range)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_numbers = list(range(1, 10))\n", "print(my_numbers)\n", "\n", "for i in range(1, 10, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# example with string\n", "prot_seq = 'SYLYC'\n", "for s in seq:\n", " print(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# example with dictionary\n", "rna_dict = {\"G\":345.21, \"C\":305.18, \"A\":329.21, \"U\":302.16}\n", "for r in rna_dict:\n", " print(r, rna_dict[r])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(r)\n", "for r in rna_dict.values():\n", " print(r)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.2.1\n", "\n", "1. Create a 15 base long DNA sequence.\n", "2. Print its length.\n", "3. Use a `for` loop to print each individual base of the sequence on a new line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### While loop\n", "There is a fundamental difference between the for and while loops:\n", "- with a for loop, you need to know beforehand how often the loop body will be executed\n", "- with a while loop, you need to repeat something until a given condition is true" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# while loops\n", "my_value = 0.25\n", "while my_value < 8:\n", " my_value += 1\n", " print (my_value)\n", "print('my final value is', my_value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.2.2\n", "\n", "1. Reuse the 15 base long sequence created.\n", "2. Create a while loop that starts at the third base in the sequence and outputs every third base until the 12th." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# solution with while loop\n", "dna = 'TCATCGTCTTCCTCA'\n", "i = 2\n", "while i < 12:\n", " print(dna[i])\n", " i += 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# solution with slicing and step\n", "list(dna[2:12:3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# solution with range\n", "for i in range(2, 12, 3):\n", " print(dna[i])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# regular expression for checking all occurances within a string\n", "my_text = 'Check occurences within this string'\n", "import re \n", "for m in re.finditer('e', my_text):\n", " print(m.start(), m.end())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(re.finditer)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# skip an iteration in loops\n", "values = [10, -5, 3, -1, 7]\n", "total = 0\n", "for v in values:\n", " if v < 0:\n", " continue # Skip this iteration\n", " total += v\n", "print(total)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# break the loop completely\n", "sequence = ['CAG','CAA', 'TAC','CAA']\n", "for codon in sequence:\n", " if codon == 'TAC':\n", " break # Quit looping at this point\n", " else:\n", " print(codon)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# do not delete while looping" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# range\n", "?range" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "squares = []\n", "for x in range(8):\n", " s = x*x\n", " squares.append(s)\n", " \n", "print(squares)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# using list comprehension\n", "squares = [x*x for x in range(8)]\n", "print(squares)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# enumerate\n", "letters = ['A','C','G','T']\n", "for index, letter in enumerate(letters):\n", " print(index, letter)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# filtering\n", "city_pops = {\n", " 'London': 8200000,\n", " 'Cambridge': 130000,\n", " 'Edinburgh': 420000,\n", " 'Glasgow': 1200000\n", "}\n", "\n", "big_cities = []\n", "for city in city_pops:\n", " if city_pops[city] >= 1000000:\n", " big_cities.append(city)\n", "\n", "print(big_cities)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# total population\n", "# using a variable in the loop, give the example above\n", "# or using sum of all values\n", "sum(city_pops.values())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# string formating: '{}' is a place holder for all values given to the format() function\n", "rna_dict = {\"G\": 345.21, \"C\": 305.18, \"A\": 329.21, \"U\": 302.16}\n", "for rna in rna_dict:\n", " print(\"This base {} has this mass {:.3f}\".format(rna, rna_dict[rna]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2.2.3\n", "\n", "1. Calculate the GC content of a DNA sequence. \n", "2. Output every base of the sequence alongside its index on a new line.\n", "3. Loop over the bases in your sequence to count the Gs and the Cs.\n", "4. Print the % of GC for this sequence and format the result to display only 2 decimal place. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "seq = 'AAAAAAAATTTTTTCCCCCGGGG'\n", "gc = 0\n", "for i, s in enumerate(seq):\n", " #print(i, s)\n", " if (s == 'C') or (s == 'G'):\n", " gc += 1\n", "#print(gc)\n", "print('%GC is {:.2f} for this sequence {}'.format((gc/len(seq))*100, seq))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }