{ "metadata": { "name": "", "signature": "sha256:8cf072e68aaa1678b84eb3f3f438ee4f6df35f204f8c9daa9f9b08bc9f0d2eb9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## [Python Programming for Biologists, Tel-Aviv University / 0411-3122 / Spring 2015](http://py4life.github.io/TAU2015/)\n", "# Homework 3\n", "\n", "## 1) Translating DNA\n", "In the code below there is a dictionary (named `codon_table`) in which keys represent codons and values represent corresponding amino acids. \n", "\n", "Write a program that will translate a DNA sequence into an amino acid sequence using the codons disctionary. Print out the result. Note that `*` are stop codons. \n", "\n", "If you want to know more about how the codons dictionary was created, read the documentation for [list comprehension](https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions) and the built-in [zip-function](https://docs.python.org/3.4/library/functions.html#zip)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create codons dictionary\n", "bases = ['t', 'c', 'a', 'g']\n", "codons = [a+b+c for a in bases for b in bases for c in bases]\n", "amino_acids = 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG'\n", "codon_table = dict(zip(codons, amino_acids))\n", "print(codon_table)\n", "\n", "DNA = 'atgattccaacgcgaaggtcaagtacgtacagctctcagtgtgtgctactcaccgactccgtcatagcaaccggcgtcgtggtcgttaccattgcataa'\n", "\n", "# translate the sequence\n", "\n", "\n", "\n", "print(_________________)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2) Protein contents\n", "\n", "**a)** Write a function that __receives__ an amino acid sequence as string and __returns__ a dictionary where the keys are the amino acid residues and the values are the number of times each residue appeared in the protein. For example, the expected result for the peptide `LLTDSGT` is: `{'L': 2, 'T': 2, 'D': 1, 'S': 1, 'G': 1}`. \n", "Test your function on the provided sequences, and print the results in the following format: \n", "L - 2 \n", "T - 2 \n", "D - 1 \n", "S - 1 \n", "G - 1\n", "\n", "Remember: `dict` is unordered." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def count_residues(protein_seq):\n", " # your code goes here. remove the pass statement.\n", " pass\n", " \n", " \n", "protein_sequence = 'DQHTWMYAEGYLNHVYRCDKQRAEDKECNGLYAWALALESHGKGSYYCQGFKTFPNPWPMHMMTFVMADLYQYMEI'\n", "aa_counts_dict = count_residues(protein_sequence)\n", "# print results\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b)** Write a function that receives an amino acid sequence as a string and returns a dictionary with the _frequencies_ of hydrophobic, posituvely-charged, negatively-charged, polar an other amino acids. Use the strings provided in the code below. \n", "\n", "For example,\n", "```\n", "residues_type_frequencies('LLTDSGT')\n", "{'hydrophobic': 0.286, 'positive': 0, 'negative': 0.143, 'polar': 0.428, 'other': 0.143}\n", "```\n", "\n", "Test your function on the provided amino acid sequence, and print the results in the following format:\n", "```\n", "hydrophobic - 0.286 \n", "positive - 0 \n", "negative - 0.143 \n", "polar - 0.428 \n", "other - 0.143\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "hydrophobic = 'AVILMFYW'\n", "pos_charged = 'RHK'\n", "neg_charged = 'DE'\n", "polar = 'STNQ'\n", "other = 'CUGP'\n", "def residues_type_frequencies(protein_seq):\n", " # your code goes here. remove the pass statement.\n", " pass\n", " \n", " \n", "aa_sequence = 'DQHTWMYAEGYLNHVYRCDKQRAEDKECNGLYAWALALESHGKGSYYCQGFKTFPNPWPMHMMTFVMADLYQYMEI'\n", "aa_types_freq_dict = residues_type_frequencies(protein_sequence)\n", "# print results\n", "\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3) Palindromic sequences\n", "A palindromic sequence is a DNA sequence that is the same whether read 5' to 3' on one strand or 5' to 3' on the complementary strand. For example, the sequence 5' GAATTC 3' is palindromic, since the complement strand is 3' CTTAAG 5', or 5' GAATTC 3'. \n", "Palindromic sequences are biologically interesting because they can form special structural motifs, such as hairpins, and often are cutting sites for restriction enzymes. \n", "\n", "**a)** Write a function `is_palindrome` that receives a DNA sequence as a string and returns `True` (boolean) if it is palindromic and `False` (boolean) otherwise. You may use the function defined in the lecture to find the complement strand. \n", "The assertions test your function on the provided sequences. If you don't get any error messages, that means your function works fine." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def is_palindrome(seq):\n", " # your code goes here. remove the pass statement.\n", " pass\n", " \n", "\n", "assert(is_palindrome('GAATTC'))\n", "assert(is_palindrome('GATATC'))\n", "assert(is_palindrome('AGCTTCTAGTCGACTAGAAGCT'))\n", "assert(not is_palindrome('GAACTC'))\n", "assert(not is_palindrome('GATATG'))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b)** Now use the function `is_palindrome` to look for palindromic subsequences within a given DNA sequence. \n", "\n", "Write a function `find_palindromes` that receives two parameters: a sequence `seq` (string) and an integer `n`. The function searches `seq` for n bases long palindromic subsequences. It returns a list of all the palindromic subsequences found. If none were found, it returns an empty list. Implement the function using a __for__ loop." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def find_palindromes(seq, n):\n", " palindromes = []\n", " \n", " for _________________:\n", " \n", " \n", " if _________________:\n", " palindromes.append(___________)\n", " \n", " \n", " \n", " \n", " return palindromes\n", "\n", "DNA_seq = 'GGAGCTCCCAAAGCCATCAATATTCATCAAAACGAATTCAACGGAGCTCGATATCGCATCGCAAAAGACACC'\n", "palindromic_sequences = find_palindromes(DNA_seq,6)\n", "assert palindromic_sequences == ['GAGCTC', 'AATATT', 'GAATTC', 'GAGCTC', 'GATATC']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**c)** Implement the same function using a __while__ loop." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def find_palindromes(seq, n):\n", " palindromes = []\n", " \n", " while _________________:\n", " \n", " \n", " if _________________:\n", " palindromes.append(___________)\n", " \n", " \n", " \n", " \n", " return palindromes\n", "\n", "DNA_seq = 'GGAGCTCCCAAAGCCATCAATATTCATCAAAACGAATTCAACGGAGCTCGATATCGCATCGCAAAAGACACC'\n", "palindromic_sequences = find_palindromes(DNA_seq,6)\n", "assert palindromic_sequences == ['GAGCTC', 'AATATT', 'GAATTC', 'GAGCTC', 'GATATC']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A major caveat of the functions created so far is that they will return all palindromic sequences, even if they are overlapping, which makes no biological sense. For example, if we search the sequence `GAATTCGAACAT` for 6-bases long palindromes, we will get both `GAATTC` and `TTCGAA`, although they are overlapping. \n", "\n", "**d)** Choose one of the implementations from parts **b** and **c**, and change it so that no overlapping palindromes will be found. The function should return the upstream palindromes where there is an overlap." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def find_palindromes_no_overlap(S, n):\n", " palindromes = []\n", " # your code here\n", " \n", " \n", " \n", " \n", " \n", " return palindromes\n", "\n", "# test\n", "DNA_seq = 'GGAGCTCCCAAAGCCATCAATATTCATCAAAACGAATTCAACGGAGCTCGATATCGCATCGCAAAAGACACC'\n", "palindromic_sequences = find_palindromes_no_overlap(DNA_seq,6)\n", "assert palindromic_sequences == ['GAGCTC', 'AATATT', 'GAATTC', 'GAGCTC', 'GATATC']\n", "DNA_seq = 'GGAGCTCCCAAAGCCATCAGAATTCGAACATATCGCAAAAGACACC'\n", "palindromic_sequences = find_palindromes(DNA_seq,6)\n", "assert palindromic_sequences == ['GAGCTC', 'GAATTC', 'TTCGAA']\n", "DNA_seq = 'GGAGCTCCCAAAGCCATCAGAATTCGAACATATCGCAAAAGACACC'\n", "palindromic_sequences = find_palindromes_no_overlap(DNA_seq,6)\n", "assert palindromic_sequences == ['GAGCTC', 'GAATTC']" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }