{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# An introduction to solving biological problems with Python\n", "\n", "## Session 1.4: Collections Sets and dictionaries\n", "\n", "- [Sets](#Sets) | [Exercise 1.4.1](#Exercise-1.4.1)\n", "- [Dictionaries](#Dictionaries) | [Exercises 1.4.2](#Exercises-1.4.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sets\n", "\n", "- Sets contain unique elements, i.e. no repeats are allowed\n", "- The elements in a set do not have an order\n", "- Sets cannot contain elements which can be internally modified (e.g. lists and dictionaries)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 2, 3] # list of 5 values\n", "s = set(l) # set of 3 unique values\n", "print(s)\n", "e = set() # empty set\n", "print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sets are very similar to lists and tuples and you can use many of the same operators and functions, except they are **inherently unordered**, so they don't have an index, and can only contain _unique_ values, so adding a value already in the set will have no effect" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = set([1, 2, 3, 2, 3])\n", "print(s)\n", "print(\"number in set:\", len(s))\n", "s.add(4)\n", "print(s)\n", "s.add(3)\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can remove specific elements from the set." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = set([1, 2, 3, 2, 3])\n", "print(s)\n", "s.remove(3)\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can do all the expected logical operations on sets, such as taking the union or intersection of 2 sets with the | _or_ and & _and_ operators " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s1 = set([2, 4, 6, 8, 10])\n", "s2 = set([4, 5, 6, 7])\n", "\n", "print(\"Union:\", s1 | s2)\n", "print(\"Intersection:\", s1 & s2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.4.1\n", "\n", "1. Given the protein sequence \"MPISEPTFFEIF\", split the sequence into its component amino acid codes and use a set to establish the unique amino acids in the protein and print out the result." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries\n", "\n", "Lists are useful in many contexts, but often we have some data that has no inherent order and that we want to access by some useful name rather than an index. For example, as a result of some experiment we may have a set of genes and corresponding expression values. We could put the expression values in a list, but then we'd have to remember which index in the list corresponded to which gene and this would quickly get complicated.\n", "\n", "For these situations a _dictionary_ is a very useful data structure.\n", "\n", "Dictionaries:\n", "\n", "- Contain a mapping of keys to values (like a word and its corresponding definition in a dictionary)\n", "- The keys of a dictionary are unique, i.e. they cannot repeat\n", "- The values of a dictionary can be of any data type\n", "- The keys of a dictionary cannot be an internally modifiable type (e.g. lists, but you can use tuples)\n", "- Dictionaries do not store data in any particular order" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "print(dna)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access values in a dictionary using the key inside square brackets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "print(\"A represents\", dna[\"A\"])\n", "print(\"G represents\", dna[\"G\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An error is triggered if a key is absent from the dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "print(\"What about N?\", dna[\"N\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access values safely with the get method, which gives back None if the key is absent and you can also supply a default values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "print(\"What about N?\", dna.get(\"N\"))\n", "print(\"With a default value:\", dna.get(\"N\", \"unknown\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can check if a key is in a dictionary with the in operator, and you can negate this with not" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "\"T\" in dna" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "\"Y\" not in dna" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The len() function gives back the number of (key, value) pairs in the dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "print(len(dna))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can introduce new entries in the dictionary by assigning a value with a new key:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n", "dna['Y'] = 'Pyrimidine'\n", "print(dna)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can change the value for an existing key by reassigning it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {'A': 'Adenine', 'C': 'Cytosine', 'T': 'Thymine', 'G': 'Guanine', 'Y': 'Pyrimidine'}\n", "dna['Y'] = 'Cytosine or Thymine'\n", "print(dna)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can delete entries from the dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {'A': 'Adenine', 'C': 'Cytosine', 'T': 'Thymine', 'G': 'Guanine', 'Y': 'Pyrimidine'}\n", "del dna['Y']\n", "print(dna)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can get a list of all the keys (in arbitrary order) using the inbuilt .keys() function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {'A': 'Adenine', 'C': 'Cytosine', 'T': 'Thymine', 'G': 'Guanine', 'Y': 'Pyrimidine'}\n", "print(list(dna.keys()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And equivalently get a list of the values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {'A': 'Adenine', 'C': 'Cytosine', 'T': 'Thymine', 'G': 'Guanine', 'Y': 'Pyrimidine'}\n", "print(list(dna.values()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And a list of tuples containing (key, value) pairs:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dna = {'A': 'Adenine', 'C': 'Cytosine', 'T': 'Thymine', 'G': 'Guanine', 'Y': 'Pyrimidine'}\n", "print(list(dna.items()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises 1.4.2\n", "\n", "1. Print out the names of the amino acids that would be produced by the DNA sequence \"GTT GCA CCA CAA CCG\" ([See the DNA codon table](https://en.wikipedia.org/wiki/DNA_codon_table)). Split this string into the individual codons and then use a dictionary to map between codon sequences and the amino acids they encode.\n", "2. Print each codon and its corresponding amino acid.\n", "3. Why couldn't we build a dictionary where the keys are names of amino acids and the values are the DNA codons?\n", "\n", "### Advanced exercise 1.4.3\n", "\n", "- Starting with an empty dictionary, count the abundance of different residue types present in the 1-letter lysozyme protein sequence (http://www.uniprot.org/uniprot/B2R4C5.fasta) and print the results to the screen in alphabetical key order." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Congratulation! You reached the end of day 1!\n", "\n", "Go to our next notebook: [python_basic_2_intro](python_basic_2_intro.ipynb)" ] } ], "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": 1 }