{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to extract information from parent GO terms\n",
"\n",
"* **1) Load the GO DAG**\n",
"* **2) Pick a GO term and visualize**\n",
" * **2a) Print GO information**\n",
" * **2b) Plot GO term**\n",
"* **3. Find GO parents for numerous GO IDs**\n",
" * **3a. Find GO parents up all relationships**\n",
" * **3b. Find GO parents up *is_a* relationships only**\n",
"* **4. Find GO parents by traversing explicit relationships**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 1. Load the GO DAG\n",
"Required relationship between GO terms, *is_a*, is always loaded.\n",
"\n",
"Load optional relationships, like *part_of*, so we have the option of finding GO parents with or without relationships."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" EXISTS: go-basic.obo\n",
"go-basic.obo: fmt(1.2) rel(2019-10-07) 47,285 GO Terms; optional_attrs(relationship)\n"
]
}
],
"source": [
"from goatools.base import get_godag\n",
"godag = get_godag('go-basic.obo', optional_attrs='relationship')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 2. Pick a GO term and visualize"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"goid = 'GO:0050807'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### 2a. Print GO information"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def prt_flds(gosubdag):\n",
" \"\"\"Print the available printing fields\"\"\"\n",
" print('Print fields:')\n",
" for fld in sorted(gosubdag.prt_attr['flds']):\n",
" print(' {F}'.format(F=fld))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BP GO:0050807 D05 regulation of synapse organization\n"
]
}
],
"source": [
"from goatools.gosubdag.gosubdag import GoSubDag\n",
"\n",
"# Create a subset of the GO DAG which contains:\n",
"# * The selected GO term and\n",
"# * All the GO terms above it\n",
"gosubdag = GoSubDag(goid, godag, relationships=True, prt=False)\n",
"\n",
"# Get additional information for chosen GO\n",
"ntgo = gosubdag.go2nt[goid]\n",
"\n",
"# Choose fields and custom printing format\n",
"# prt_flds(gosubdag) # Uncomment to see the available print fields\n",
"prtfmt = '{NS} {GO} D{depth:02} {GO_name}'\n",
"\n",
"# Print detailed information for GO\n",
"print(prtfmt.format(**ntgo._asdict()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### 2b. Plot GO term"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1 usr 12 GOs WROTE: reg_synapse_org.png\n"
]
}
],
"source": [
"from goatools.gosubdag.plot.gosubdag_plot import GoSubDagPlot\n",
"GoSubDagPlot(gosubdag).plt_dag('reg_synapse_org.png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 3. Find GO parents for numerous GO IDs\n",
"### 3a. Find GO parents up all relationships"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BP GO:0051128 D04 regulation of cellular component organization\n",
"BP GO:0050803 D03 regulation of synapse structure or activity\n",
"BP GO:0050808 D03 synapse organization\n"
]
}
],
"source": [
"from goatools.godag.go_tasks import get_go2parents\n",
"\n",
"go2parents = get_go2parents(gosubdag.go2obj, gosubdag.relationships)\n",
"for goid_parent in go2parents[goid]:\n",
" print(prtfmt.format(**gosubdag.go2nt[goid_parent]._asdict()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### 3b. Find GO parents up *is_a* relationships only"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BP GO:0051128 D04 regulation of cellular component organization\n"
]
}
],
"source": [
"from goatools.godag.go_tasks import get_go2parents_isa\n",
"\n",
"go2parents = get_go2parents_isa(gosubdag.go2obj)\n",
"for goid_parent in go2parents[goid]:\n",
" print(prtfmt.format(**gosubdag.go2nt[goid_parent]._asdict()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 4. Find GO parents by traversing explicit relationships"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Parents up \"is_a\": required relationship\n",
"BP GO:0051128 D04 regulation of cellular component organization\n",
"\n",
"Parents up \"part_of\" optional relationship:\n",
"BP GO:0050803 D03 regulation of synapse structure or activity\n",
"\n",
"Parents up \"regulates\" optional relationship:\n",
"BP GO:0050808 D03 synapse organization\n",
"\n",
"Ancestors up all loaded relationships:\n",
"BP GO:0050789 D02 regulation of biological process\n",
"BP GO:0050794 D03 regulation of cellular process\n",
"BP GO:0050803 D03 regulation of synapse structure or activity\n",
"BP GO:0016043 D02 cellular component organization\n",
"BP GO:0065008 D02 regulation of biological quality\n",
"BP GO:0050808 D03 synapse organization\n",
"BP GO:0008150 D00 biological_process\n",
"BP GO:0009987 D01 cellular process\n",
"BP GO:0065007 D01 biological regulation\n",
"BP GO:0051128 D04 regulation of cellular component organization\n",
"BP GO:0071840 D01 cellular component organization or biogenesis\n"
]
}
],
"source": [
"goterm = godag[goid]\n",
"\n",
"print('Parents up \"is_a\": required relationship')\n",
"for p_term in goterm.parents:\n",
" print(prtfmt.format(**gosubdag.go2nt[p_term.item_id]._asdict()))\n",
" \n",
"if 'part_of' in goterm.relationship:\n",
" print('\\nParents up \"part_of\" optional relationship:')\n",
" for p_go in goterm.relationship['part_of']:\n",
" print(prtfmt.format(**gosubdag.go2nt[p_go.item_id]._asdict()))\n",
" \n",
"if 'regulates' in goterm.relationship:\n",
" print('\\nParents up \"regulates\" optional relationship:')\n",
" for p_go in goterm.relationship['regulates']:\n",
" print(prtfmt.format(**gosubdag.go2nt[p_go.item_id]._asdict()))\n",
"\n",
"# godag must be loaded with: optional_attrs='relationship'\n",
"# gosubdag must be loaded with: relationships=True\n",
"print('\\nAncestors up all loaded relationships:')\n",
"for p_go in gosubdag.rcntobj.go2ancestors[goid]:\n",
" print(prtfmt.format(**gosubdag.go2nt[p_go]._asdict()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright (C) 2019-present, DV Klopfenstein, H 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
}