{ "cells": [ { "cell_type": "markdown", "id": "cff1ab76", "metadata": {}, "source": [ "# Assigment 1 - Python lists, loops, and conditionals\n", "\n", "### Overview\n", "------------\n", "\n", "Genomic annotations are often represented in the browser extensible data (BED) file format. This consists of genomic location data and may include labels, scores, strandedness, and other features for each entry. In this exercise, you will be looking at data from a basic BED file containing gene boundaries and expression levels to calculate some basic descriptive statistics.\n", "\n", "Biological Learning Objectives\n", "\n", "- Understand how interval data is represented in the BED file format\n", "- Use descriptive statistics to characterize a set of gene annotations\n", "\n", "Computational Learning Objectives\n", "\n", "- Create, modify, and access data from lists\n", "- Use built-in string functions to manipulate text\n", "- Convert between different data types\n", "- Step through list items using a `for` loop\n", "- Condition execution of code based on an `if` statement\n", "\n", "### Instructions\n", "----------------\n", "\n", "- Save a copy of this notebook as `~/qbXX-answers/python_basics.ipynb`.\n", "- Fill in answers in the available code cells below.\n", "- Remember to comment your code to help yourself and us know what each part is intended to do.\n", "- You may find the functions `sorted()`, `sum()`, and `len()`, which all accept a list as their argument, useful for completing this exercise" ] }, { "cell_type": "code", "execution_count": null, "id": "6c7f1741", "metadata": {}, "outputs": [], "source": [ "# Use this as your input data\n", "\n", "text_data = \"chrI\\t8377406\\t8390027\\tNM_059873.7\\t6\\t-\\nchrI\\t8377598\\t8392758\\tNM_182066.7\\t502\\t-\\nchrI\\t8377600\\t8392768\\tNM_001129046.3\\t7\\t-\\nchrMt\\t1041473\\t1049600\\tNM_058410.5\\t476\\t+\\nchrI\\t3144409\\t3147793\\tNM_058707.5\\t531\\t-\\nchrI\\t4193240\\t4203303\\tNM_058924.6\\t673\\t+\\nchrI\\t6284972\\t6294057\\tNM_059412.6\\t532\\t+\\nchrI\\t6289432\\t6294068\\tNM_001374900.1\\t615\\t+\\nchrI\\t6290315\\t6293988\\tNM_001374901.1\\t709\\t+\\nchrMt\\t7339231\\t7345684\\tNM_001136303.4\\t7\\t+\\nchrI\\t9431248\\t9441017\\tNM_060105.7\\t481\\t+\\nchrI\\t9435464\\t9441029\\tNM_060106.7\\t3\\t+\\nchrMt\\t11526917\\t11557854\\tNM_060545.6\\t498\\t-\\nchrI\\t11526922\\t11552160\\tNM_001383731.1\\t536\\t-\\nchrI\\t11527027\\t11557792\\tNM_001306313.4\\t6\\t-\\nchrI\\t11527076\\t11541127\\tNM_001306312.3\\t7\\t-\\nchrI\\t11527076\\t11546567\\tNM_001306311.3\\t694\\t-\\nchrI\\t11527076\\t11552106\\tNM_001306310.3\\t2\\t-\"\n", "\n", "# This is what the data would look like in a file\n", "# chrI 8377406 8390027 NM_059873.7 6 -\n", "# chrI 8377598 8392758 NM_182066.7 502 -\n", "# chrI 8377600 8392768 NM_001129046.3 7 -\n", "# chrMt 1041473 1049600 NM_058410.5 476 +\n", "# chrI 3144409 3147793 NM_058707.5 531 -\n", "# chrI 4193240 4203303 NM_058924.6 673 +\n", "# chrI 6284972 6294057 NM_059412.6 532 +\n", "# chrI 6289432 6294068 NM_001374900.1 615 +\n", "# chrI 6290315 6293988 NM_001374901.1 709 +\n", "# chrMt 7339231 7345684 NM_001136303.4 7 +\n", "# chrI 9431248 9441017 NM_060105.7 481 +\n", "# chrI 9435464 9441029 NM_060106.7 3 +\n", "# chrMt 11526917 11557854 NM_060545.6 498 -\n", "# chrI 11526922 11552160 NM_001383731.1 536 -\n", "# chrI 11527027 11557792 NM_001306313.4 6 -\n", "# chrI 11527076 11541127 NM_001306312.3 7 -\n", "# chrI 11527076 11546567 NM_001306311.3 694 -\n", "# chrI 11527076 11552106 NM_001306310.3 2 -" ] }, { "cell_type": "markdown", "id": "7bc62460", "metadata": {}, "source": [ "\n", "### Excercises\n", "--------------\n", "\n", "1. Parse the data from the data string, converting values to appropriate data types as necessary. The column values are chromosome, start position, end position, gene name, expression value, and strand\n", " \n", " - Split the string by the newline character (`\\n`) to create a list containing one line per list-entry\n", " - Use a `for` loop to step through the data, line by line\n", " - Split each line by the tab characer (`\\t`) to get values for each column\n", " - Convert position values to integers and expression values to floats" ] }, { "cell_type": "code", "execution_count": null, "id": "db06c36a", "metadata": {}, "outputs": [], "source": [ "# Split the data string into a list of individual lines\n", "\n", "# Step through each line in the list\n", "\n", " # Split the line into a list of individual fields\n", "\n", " # Assign each field to a variable (chrom, start, stop, etc.), converting the data type if necessary\n" ] }, { "cell_type": "markdown", "id": "5802f9e7", "metadata": {}, "source": [ "2. Create and populate lists with the parsed data using one list to record expression values, one to record gene lengths, and one to record strandedness, skipping any genes from the mitochondrial chromosome\n", "\n", " - Use your above code as a starting point for this, adding lines inside the `for` loop for recording relevant values\n", " - It will be much easier to analyze the strandedness data if you convert it into ones and zeros (you can use an `if` statement for this)" ] }, { "cell_type": "code", "execution_count": null, "id": "fefd971b", "metadata": {}, "outputs": [], "source": [ "# Split the data string into a list of individual lines\n", "\n", "# Create a list for recording expression data, one for recording gene lengths, and one for recording strandedness\n", "\n", "# Step through each line in the list\n", "\n", " # Split the line into a list of individual fields\n", "\n", " # Assign each field to a variable (chrom, start, stop, etc.), converting the data type if necessary\n", "\n", " # Check if gene is from \"chrMt\" and if so, skip\n", "\n", " # Add the expression value, gene size, and strandedness to their corresponding lists\n" ] }, { "cell_type": "markdown", "id": "f48d12a4", "metadata": {}, "source": [ "3. Calculate and report the 1st, 2nd, and 3rd quartiles of gene expression\n", "\n", " - Your expression values will need to be in order to determine quartiles\n", " - The first quartile is the 0.25 * (n + 1)th item, the second quartile is the 0.5 * (n + 1)th item, etc.\n", " - Because python starts counting at zero, the ith item is at index i - 1\n", " - Remember that the get a value from the list at specific index, you can only use intergers, not floats\n", " - Use a `print` statement to report your answer" ] }, { "cell_type": "code", "execution_count": null, "id": "ab23a729", "metadata": {}, "outputs": [], "source": [ "# Sort your expression values from lowest to highest\n", "\n", "# Determine which position in the list corresponds to the first, second, and third quartiles\n", "\n", "# Find the value from those positions in your expression list\n", "\n", "# Report your results\n" ] }, { "cell_type": "markdown", "id": "e51b9643", "metadata": {}, "source": [ "4. Calculate and report the minimum and maximum gene size" ] }, { "cell_type": "code", "execution_count": null, "id": "2e8a5e0d", "metadata": {}, "outputs": [], "source": [ "# Sort your gene sizes values from lowest to highest\n", "\n", "# Find the largest and smallest gene sizes\n", "\n", "# Report your results\n" ] }, { "cell_type": "markdown", "id": "aef852fd", "metadata": {}, "source": [ "5. Calculate and report the percentage of genes that are on the positive strand" ] }, { "cell_type": "code", "execution_count": null, "id": "1e95280e", "metadata": {}, "outputs": [], "source": [ "# Calculate the percentage of positive strand genes\n", "\n", "# Report your results\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }