{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to get GO IDs up both the '_is a_' and '_part of_' relationships\n", "\n", " 1. Download Ontologies, if necessary\n", " 2. Choose a set of GO IDs to print\n", " 3. GO colors for plotting\n", " 4. Traverse up or down the GO DAG with no, all, or some optional relationships(a):\n", " * 4a) No optional relationships, like **part_of** and **regulates**\n", " * 4b) All relationships\n", " * 4c) Relationship: **part_of**\n", " * 4d) Relationship: **regulates**\n", " * 4e) Relationship: **positively_regulates**\n", " * 4f) Relationship: **negatively_regulates**\n", " \n", " \n", "a) The *is_a* relationships will always be traversed to find the children or parents of a GO term\n", "\n", "## Plots with and without relationships\n", "\n", "Arrows denote the relationships, *is_a* (solid black arrows) and *part_of* (dashed orange arrows), in the plots below: \n", "\n", "| With no optional relationships | With the optional *part_of* relationship\n", "|---------------------------------|-------------------------------\n", "| ![is_a](images/fig1_r0.png) | ![part_of](images/fig1_r1.png) \n", "\n", "Notice that the text in the GO term box is different if you are plotting using optional relationships. The bottom GO term text is:\n", "\n", "* Left plot: **GO:0043231 L3 D3 d0**\n", "* Right plot: **GO:0043231 D3 R4 d0**\n", "\n", "**L (Level)** is the minimum distance from root term, *cellular_component*, to the bottom term \n", "**D (Depth)**, is the maximum distance from root term, *cellular_component*, to the bottom term using only *is_a* relationships \n", "**R (Depth using optional relationships)**, is the maximum distance from root term, *cellular_component*, to the bottom term using both *is_a* and optional relationships\n", "\n", "In this example **D (Depth)** from the root to the bottom term is 3 (light blue lines). \n", "\n", "And **R (Depth using optional relationships)** from the root to the bottom is 4 (dark purple lines) because it traverses the *part_of* relationships\n", "\n", "\"depth\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Download Ontologies, if necessary\n", "Load all relationships into a GODag object. \n", "Specific relationships, like **part_of** or **regulates**, will be chosen using GoSubDag. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "requests.get(http://purl.obolibrary.org/obo/go/go-basic.obo, stream=True)\n", " WROTE: go-basic.obo\n", "\n", "go-basic.obo: fmt(1.2) rel(2020-12-08) 47,295 GO Terms; optional_attrs(relationship)\n", " EXISTS: ../tests/data/i126/viral_gene_silence.obo\n", "../tests/data/i126/viral_gene_silence.obo: fmt(1.2) rel(2019-04-17) 79 GO Terms; optional_attrs(relationship)\n" ] } ], "source": [ "from goatools.base import get_godag\n", "godag = get_godag(\"go-basic.obo\", optional_attrs={'relationship'})\n", "\n", "# We are going to use a GO DAG subset for this example\n", "godag = get_godag(\"../tests/data/i126/viral_gene_silence.obo\", optional_attrs={'relationship'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Choose a set of GO IDs to print\n", "This example will use all GO IDs above GO:0060150, *viral triggering of virus induced gene silencing*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "virus_trigger_silence = 'GO:0060150'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. GO colors\n", "The GO DAG and go-color files for this example are located in: **./tests/data/i126** \n", "\n", "GO colors are stored in **viral_gene_silence.txt** as 6-digit hax numbers, like #ffe5b4. \n", "The color file looks like this:\n", "```\n", "Relationship, part_of, shall be orange (#ffe5b4):\n", "#ffe5b4 GO:0002376 immune system process\n", "#ffe5b4 GO:0050896 response to stimulus\n", "#ffe5b4 GO:0051704 multi-organism process\n", "...\n", "```" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from goatools.cli.gos_get import get_go2color\n", "\n", "go2hexcolor = get_go2color('../tests/data/i126/viral_gene_silence.txt')\n", "\n", "# For printing GO terms and their color names to the print\n", "hex2color = {\n", " '#e6fad2': 'green',\n", " '#ffe5b4': 'orange',\n", " '#d2d2fa': 'purple',\n", " '#d2fafa': 'cyan',\n", " '#fad2fa': 'magenta',\n", " '#d8dcd6': 'grey',\n", "}\n", "GREY = '#d8dcd6'\n", "GO2COLORNAME = {go: hex2color[rgb] for go, rgb in go2hexcolor.items()}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Traversals up the GO DAG\n", "### 4a. No optional relationships loaded\n", "Do not traverse any optional relationships, like **part_of** and **regulates**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " GoSubDag: 1 sources in 13 GOs rcnt(True). 0 alt GO IDs\n", " GoSubDag: namedtuple fields: NS level depth GO alt GO_name dcnt D1 id\n", " GoSubDag: relationships: set()\n" ] } ], "source": [ "from goatools.gosubdag.gosubdag import GoSubDag\n", "\n", "gosubdag_r0 = GoSubDag({virus_trigger_silence}, godag)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NtGo(NS='BP', level=5, depth=8, GO='GO:0060148', alt='', GO_name='positive regulation of posttranscriptional gene silencing', dcnt=1, D1='A', id='GO:0060148')\n" ] } ], "source": [ "# Decide which GO fields to print. Save in prtfmt\n", "\n", "# Print one namedtuple\n", "print(next(iter(gosubdag_r0.go2nt.values())))\n", "\n", "# Use some namedtuple fields in print of GO terms\n", "PRTFMT = '{NS} {GO} D-{depth:02} {color:7} {dcnt:2} {GO_name}'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BP GO:0008150 D-00 green 50 biological_process\n", "BP GO:0065007 D-01 green 19 biological regulation\n", "BP GO:0050789 D-02 green 18 regulation of biological process\n" ] } ], "source": [ "# Report all ancestors of term, GO:001922 (colored grey)\n", "grey_go = 'GO:0019222'\n", "ancestors = gosubdag_r0.rcntobj.go2ancestors[grey_go]\n", "\n", "def prt_goids(goids, gosubdag):\n", " \"\"\"Print GO IDs\"\"\"\n", " nts = [gosubdag.go2nt[go] for go in goids]\n", " for nt_go in sorted(nts, key=lambda nt: [nt.depth, nt.dcnt]):\n", " color = GO2COLORNAME[nt_go.GO]\n", " print(PRTFMT.format(color=color, **nt_go._asdict()))\n", "\n", "prt_goids(ancestors, gosubdag_r0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot no optional relationships, just using ***is_a*** (runtime script):\n", "Color the term, GO:0019222 *regulation of metabolic process*, with xkcd's *light grey* (#d8dcd6)\n", "\n", "```\n", "$ go_plot.py -o viral_r0.png GO:0060150 GO:0019222#d8dcd6 --obo=viral_gene_silence.obo --go_color_file=viral_gene_silence.txt\n", "\n", "```\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot no optional relationships, just using ***is_a*** (code):" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 usr 13 GOs WROTE: viral_r0.png\n" ] } ], "source": [ "from goatools.gosubdag.plot.gosubdag_plot import GoSubDagPlot\n", "\n", "# Color the GO of interest in a copy of the color dictionary\n", "go2col = dict(go2hexcolor)\n", "go2col[grey_go] = GREY\n", "\n", "pltobj = GoSubDagPlot(gosubdag_r0, go2color=go2col)\n", "pltobj.plt_dag('viral_r0.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4b. All relationships\n", "Traverse all optional relationships, like **part_of** and **regulates**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " GoSubDag: 1 sources in 51 GOs rcnt(True). 0 alt GO IDs\n", " GoSubDag: namedtuple fields: NS level depth reldepth GO alt GO_name dcnt D1 childcnt REL REL_short rel id\n", " GoSubDag: relationships: {'negatively_regulates', 'regulates', 'part_of', 'positively_regulates'}\n", "BP GO:0008150 D-00 green 50 biological_process\n", "BP GO:0008152 D-01 cyan 17 metabolic process\n", "BP GO:0065007 D-01 green 19 biological regulation\n", "BP GO:0071704 D-02 purple 14 organic substance metabolic process\n", "BP GO:0050789 D-02 green 18 regulation of biological process\n", "BP GO:0019222 D-03 green 13 regulation of metabolic process\n", "BP GO:0043170 D-03 cyan 13 macromolecule metabolic process\n", "BP GO:0010467 D-04 cyan 10 gene expression\n", "BP GO:0060255 D-04 green 11 regulation of macromolecule metabolic process\n" ] } ], "source": [ "gosubdag_r1 = GoSubDag({virus_trigger_silence}, godag, relationships=True)\n", "\n", "# Report all ancestors of grey term\n", "grey_go = 'GO:0010468' # regulation of gene expression\n", "ancestors = gosubdag_r1.rcntobj.go2ancestors[grey_go]\n", "prt_goids(ancestors, gosubdag_r1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot **all** relationships (runtime script):\n", "\n", "```\n", "$ go_plot.py -o viral_r1.png -r GO:0060150 GO:0010468#d8dcd6 --obo=viral_gene_silence.obo --go_color_file=viral_gene_silence.txt\n", "```\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot **all** relationships (code):" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 usr 51 GOs WROTE: viral_r1.png\n" ] } ], "source": [ "# Color the GO of interest in a copy of the color dictionary\n", "go2col = dict(go2hexcolor)\n", "go2col[grey_go] = GREY\n", "\n", "pltobj = GoSubDagPlot(gosubdag_r1, go2color=go2col)\n", "pltobj.plt_dag('viral_r1.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4c. Relationship: *part_of*" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " GoSubDag: 1 sources in 38 GOs rcnt(True). 0 alt GO IDs\n", " GoSubDag: namedtuple fields: NS level depth reldepth GO alt GO_name dcnt D1 childcnt REL REL_short rel id\n", " GoSubDag: relationships: {'part_of'}\n", "BP GO:0008150 D-00 green 50 biological_process\n", "BP GO:0065007 D-01 green 19 biological regulation\n", "BP GO:0050789 D-02 green 18 regulation of biological process\n", "BP GO:0019222 D-03 green 13 regulation of metabolic process\n", "BP GO:0060255 D-04 green 11 regulation of macromolecule metabolic process\n" ] } ], "source": [ "gosubdag_partof = GoSubDag({virus_trigger_silence}, godag, relationships={'part_of',})\n", "\n", "# Report all ancestors of grey term\n", "grey_go = 'GO:0010468' # regulation of gene expression\n", "ancestors = gosubdag_partof.rcntobj.go2ancestors[grey_go]\n", "prt_goids(ancestors, gosubdag_partof)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***part_of*** relationships (runtime script): \n", "Orange dashed lines represent *part_of* relationships:\n", "\n", "```\n", "$ go_plot.py -o viral_r_partof.png --relationships=part_of GO:0010468#d8dcd6 GO:0060150 --obo=viral_gene_silence.obo --go_color_file=viral_gene_silence.txt \n", "```\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***part_of*** relationships (code)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 usr 38 GOs WROTE: viral_r_partof.png\n" ] } ], "source": [ "# Color the GO of interest in a copy of the color dictionary\n", "go2col = dict(go2hexcolor)\n", "go2col[grey_go] = GREY\n", "\n", "pltobj = GoSubDagPlot(gosubdag_partof, go2color=go2col)\n", "pltobj.plt_dag('viral_r_partof.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4d. Relationship: *regulates*" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " GoSubDag: 1 sources in 26 GOs rcnt(True). 0 alt GO IDs\n", " GoSubDag: namedtuple fields: NS level depth reldepth GO alt GO_name dcnt D1 childcnt REL REL_short rel id\n", " GoSubDag: relationships: {'regulates'}\n", "BP GO:0008150 D-00 green 50 biological_process\n", "BP GO:0009987 D-01 magenta 8 cellular process\n", "BP GO:0065007 D-01 green 19 biological regulation\n", "BP GO:0050789 D-02 green 18 regulation of biological process\n" ] } ], "source": [ "gosubdag_reg = GoSubDag({virus_trigger_silence}, godag, relationships={'regulates',})\n", "\n", "# Report all ancestors of grey term\n", "grey_go = 'GO:0050794' # regulation of cellular process\n", "ancestors = gosubdag_reg.rcntobj.go2ancestors[grey_go]\n", "prt_goids(ancestors, gosubdag_reg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***regulates*** relationships (runtime script): \n", "Purple dashed lines represent *regulates* relationships:\n", "\n", "```\n", "$ go_plot.py -o viral_reg.png --relationships=regulates GO:0050794#d8dcd6 GO:0060150 --obo=viral_gene_silence.obo --go_color_file=viral_gene_silence.txt \n", "```\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***regulates*** relationships (code): " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 usr 26 GOs WROTE: viral_reg.png\n" ] } ], "source": [ "# Color the GO of interest in a copy of the color dictionary\n", "go2col = dict(go2hexcolor)\n", "go2col[grey_go] = GREY\n", "\n", "pltobj = GoSubDagPlot(gosubdag_reg, go2color=go2col)\n", "pltobj.plt_dag('viral_reg.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4e. Relationship: *positively_regulates*" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " GoSubDag: 1 sources in 22 GOs rcnt(True). 0 alt GO IDs\n", " GoSubDag: namedtuple fields: NS level depth reldepth GO alt GO_name dcnt D1 childcnt REL REL_short rel id\n", " GoSubDag: relationships: {'positively_regulates'}\n" ] } ], "source": [ "gosubdag_regp = GoSubDag({virus_trigger_silence}, godag, relationships={'positively_regulates',})" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BP GO:0008150 D-00 green 50 biological_process\n", "BP GO:0009987 D-01 magenta 5 cellular process\n", "BP GO:0065007 D-01 green 19 biological regulation\n", "BP GO:0050789 D-02 green 18 regulation of biological process\n", "BP GO:0048518 D-03 green 3 positive regulation of biological process\n", "BP GO:0050794 D-03 green 5 regulation of cellular process\n" ] } ], "source": [ "# Report all ancestors of grey term\n", "grey_go = 'GO:0048522' # positive regulation of cellular process\n", "ancestors = gosubdag_regp.rcntobj.go2ancestors[grey_go]\n", "prt_goids(ancestors, gosubdag_regp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***positively_regulates*** relationships (runtime script): \n", "Magenta dashed lines represent *positively regulates* relationships: \n", "\n", "```\n", "$ go_plot.py -o viral_rp.png --relationships=positively_regulates GO:0048522#d8dcd6 GO:0060150 --obo=viral_gene_silence.obo --go_color_file=viral_gene_silence.txt \n", "```\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***positively_regulates*** relationships (code): " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 usr 22 GOs WROTE: viral_rp.png\n" ] } ], "source": [ "# Color the GO of interest in a copy of the color dictionary\n", "go2col = dict(go2hexcolor)\n", "go2col[grey_go] = GREY\n", "\n", "pltobj = GoSubDagPlot(gosubdag_regp, go2color=go2col)\n", "pltobj.plt_dag('viral_rp.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4f. Relationship: *negatively_regulates*" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " GoSubDag: 1 sources in 26 GOs rcnt(True). 0 alt GO IDs\n", " GoSubDag: namedtuple fields: NS level depth reldepth GO alt GO_name dcnt D1 childcnt REL REL_short rel id\n", " GoSubDag: relationships: {'negatively_regulates', 'regulates'}\n", "BP GO:0008150 D-00 green 50 biological_process\n", "BP GO:0009987 D-01 magenta 8 cellular process\n", "BP GO:0065007 D-01 green 19 biological regulation\n", "BP GO:0050789 D-02 green 18 regulation of biological process\n" ] } ], "source": [ "gosubdag_regn = GoSubDag({virus_trigger_silence}, godag, relationships={'regulates', 'negatively_regulates',})\n", "\n", "# Report all ancestors of grey term\n", "grey_go = 'GO:0050794' # regulation of cellular process\n", "ancestors = gosubdag_regn.rcntobj.go2ancestors[grey_go]\n", "prt_goids(ancestors, gosubdag_regn)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***negatively_regulates*** relationships (runtime script): \n", "Cyan dashed lines represent *negatively regulates* relationships \n", "\n", "**NOTE:** In this example, the following *relationships* argument will have **no affect** because **no green GO IDs** have the relationship, *negatively_regulates*: \n", "**--relationships=*negatively_regulates***\n", "\n", "\n", "To see *negatively_regulates*, specify both *regulates* and *negatively_regulates*: \n", "**--relationships=*regulates,negatively_regulates***\n", "\n", "Note that specifying both *regulates* and *negatively_regulates* will case the magenta GO IDs to be traversed. The magenta GO IDs are accessed by both the *regulates* and the *positively_regulates* relationships.\n", "\n", "```\n", "$ go_plot.py -o viral_rn.png --relationships=regulates,negatively_regulates GO:0050794#d8dcd6 GO:0060150 --obo=viral_gene_silence.obo --go_color_file=viral_gene_silence.txt\n", "```\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot ***negatively_regulates*** relationships (code): " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 usr 26 GOs WROTE: viral_rn.png\n" ] } ], "source": [ "# Color the GO of interest in a copy of the color dictionary\n", "go2col = dict(go2hexcolor)\n", "go2col[grey_go] = GREY\n", "\n", "pltobj = GoSubDagPlot(gosubdag_regn, go2color=go2col)\n", "pltobj.plt_dag('viral_rn.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (C) 2016-present, DV Klopfenstein et al. 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 }