{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading Annotations from a _GO Association File_ (GAF)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Download a GAF file\n", "2. Load the GAF file into the GafReader\n", "3. Get Annotations\n", "\n", "**Bonus: Each line in the GAF file is stored in a namedtuple**:\n", " * Namedtuple fields\n", " * Print a subset of the namedtuple fields" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1) Download a GAF file" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "if not os.path.exists('goa_human.gaf.gz'):\n", " !wget http://current.geneontology.org/annotations/goa_human.gaf.gz\n", " !gunzip goa_human.gaf.gz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2) Load the GAF file into the GafReader" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HMS:0:00:13.490551 424,966 annotations READ: goa_human.gaf \n" ] } ], "source": [ "from goatools.anno.gaf_reader import GafReader\n", "\n", "ogaf = GafReader(\"goa_human.gaf\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3) Get Annotations\n", "The annotations will be stored in three dicts, one for each GODAG branch, where:\n", " * the key is the protein ID and \n", " * the value is a list of GO IDs associated with the protein." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "ns2assc = ogaf.get_ns2assc()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BP A0A075B6H9 : GO:0002250\n", "BP A0A075B6I0 : GO:0002250\n", "BP A0A075B6I1 : GO:0002250\n", "MF A0A024RBG1 : GO:0003723 GO:0008486 GO:0046872 GO:0052840 GO:0052842\n", "MF A0A075B6H9 : GO:0003823\n", "MF A0A075B6I0 : GO:0003823\n", "CC A0A024RBG1 : GO:0005829\n", "CC A0A075B6H9 : GO:0005886\n", "CC A0A075B6I0 : GO:0005886\n" ] } ], "source": [ "for namespace, associations in ns2assc.items():\n", " for protein_id, go_ids in sorted(associations.items())[:3]:\n", " print(\"{NS} {PROT:7} : {GOs}\".format(\n", " NS=namespace,\n", " PROT=protein_id,\n", " GOs=' '.join(sorted(go_ids))))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus: The GAF is stored as a list of named tuples\n", "The list of namedtuples is stored in the **GafReader** data member named **_associations_**.\n", "\n", "Each namedtuple stores data for one line in the GAF file." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ntgafobj(DB='UniProtKB', DB_ID='A0A024RBG1', DB_Symbol='NUDT4B', Qualifier=set(), GO_ID='GO:0003723', DB_Reference={'GO_REF:0000037'}, Evidence_Code='IEA', With_From={'UniProtKB-KW:KW-0694'}, NS='MF', DB_Name={'Diphosphoinositol polyphosphate phosphohydrolase NUDT4B'}, DB_Synonym={'NUDT4B'}, DB_Type='protein', Taxon=[9606], Date=datetime.date(2019, 4, 6), Assigned_By='UniProt', Extension=None, Gene_Product_Form_ID=set())\n" ] } ], "source": [ "# Sort the list of GAF namedtuples by ID\n", "nts = sorted(ogaf.associations, key=lambda nt:nt.DB_ID)\n", "\n", "# Print one namedtuple\n", "print(nts[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Namedtuple fields\n", "```\n", "DB # 0 required 1 UniProtKB\n", "DB_ID # 1 required 1 P12345\n", "DB_Symbol # 2 required 1 PHO3\n", "Qualifier # 3 optional 0 or greater NOT\n", "GO_ID # 4 required 1 GO:0003993\n", "DB_Reference # 5 required 1 or greater PMID:2676709\n", "Evidence_Code # 6 required 1 IMP\n", "With_From # 7 optional 0 or greater GO:0000346\n", "Aspect # 8 required 1 F\n", "DB_Name # 9 optional 0 or 1 Toll-like receptor 4\n", "DB_Synonym # 10 optional 0 or greater hToll|Tollbooth\n", "DB_Type # 11 required 1 protein\n", "Taxon # 12 required 1 or 2 taxon:9606\n", "Date # 13 required 1 20090118\n", "Assigned_By # 14 required 1 SGD\n", "Annotation_Extension # 15 optional 0 or greater part_of(CL:0000576)\n", "Gene_Product_Form_ID # 16 optional 0 or 1 UniProtKB:P12345-2\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Print a subset of the namedtuple fields" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A0A024RBG1 NUDT4B GO:0003723 IEA 2019-04-06 UniProt\n", "A0A024RBG1 NUDT4B GO:0005829 IDA 2016-12-04 HPA\n", "A0A024RBG1 NUDT4B GO:0008486 IEA 2019-04-06 UniProt\n", "A0A024RBG1 NUDT4B GO:0046872 IEA 2019-04-06 UniProt\n", "A0A024RBG1 NUDT4B GO:0052840 IEA 2019-04-06 UniProt\n", "A0A024RBG1 NUDT4B GO:0052842 IEA 2019-04-06 UniProt\n", "A0A075B6H9 IGLV4-69 GO:0002250 IEA 2019-04-06 UniProt\n", "A0A075B6H9 IGLV4-69 GO:0003823 IEA 2019-04-06 UniProt\n", "A0A075B6H9 IGLV4-69 GO:0005886 IEA 2019-04-06 UniProt\n", "A0A075B6I0 IGLV8-61 GO:0002250 IEA 2019-04-06 UniProt\n" ] } ], "source": [ "fmtpat = '{DB_ID} {DB_Symbol:13} {GO_ID} {Evidence_Code} {Date} {Assigned_By}'\n", "for nt_line in nts[:10]:\n", " print(fmtpat.format(**nt_line._asdict()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (C) 2010-2019, DV Klopfenstein, Haibao Tang. All rights reserved." ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }