{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install obonet","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:54:01.598514Z","iopub.execute_input":"2023-05-12T13:54:01.599295Z","iopub.status.idle":"2023-05-12T13:54:20.206658Z","shell.execute_reply.started":"2023-05-12T13:54:01.599243Z","shell.execute_reply":"2023-05-12T13:54:20.204989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pygraphviz","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:00.550944Z","iopub.execute_input":"2023-05-12T13:57:00.551577Z","iopub.status.idle":"2023-05-12T13:57:21.937279Z","shell.execute_reply.started":"2023-05-12T13:57:00.551523Z","shell.execute_reply":"2023-05-12T13:57:21.935067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# networkx for graph stuff\nimport networkx\n\n# to plot subgraphs\nfrom networkx.drawing.nx_agraph import graphviz_layout\n\n# to plot graphs\nimport matplotlib.pyplot as plt\n\n# use obonet to load the ontology in a networkx graph\nimport obonet","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:23.977397Z","iopub.execute_input":"2023-05-12T13:57:23.977994Z","iopub.status.idle":"2023-05-12T13:57:23.987727Z","shell.execute_reply.started":"2023-05-12T13:57:23.977944Z","shell.execute_reply":"2023-05-12T13:57:23.985265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"go_path = \"/kaggle/input/cafa-5-protein-function-prediction/Train/go-basic.obo\"","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:24.829374Z","iopub.execute_input":"2023-05-12T13:57:24.830618Z","iopub.status.idle":"2023-05-12T13:57:24.83568Z","shell.execute_reply.started":"2023-05-12T13:57:24.83057Z","shell.execute_reply":"2023-05-12T13:57:24.834062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"go_path","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:25.569529Z","iopub.execute_input":"2023-05-12T13:57:25.570409Z","iopub.status.idle":"2023-05-12T13:57:25.578631Z","shell.execute_reply.started":"2023-05-12T13:57:25.570368Z","shell.execute_reply":"2023-05-12T13:57:25.577063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knowledge_graph = obonet.read_obo(go_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:26.25858Z","iopub.execute_input":"2023-05-12T13:57:26.259087Z","iopub.status.idle":"2023-05-12T13:57:48.138466Z","shell.execute_reply.started":"2023-05-12T13:57:26.259047Z","shell.execute_reply":"2023-05-12T13:57:48.136839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print the number of nodes and edges in the graph\nprint(\"Nodes: {}\".format(len(knowledge_graph.nodes)))\nprint(\"Edges: {}\".format(len(knowledge_graph.edges)))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:48.141055Z","iopub.execute_input":"2023-05-12T13:57:48.141439Z","iopub.status.idle":"2023-05-12T13:57:48.189693Z","shell.execute_reply.started":"2023-05-12T13:57:48.141407Z","shell.execute_reply":"2023-05-12T13:57:48.187425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check if the graph is a DAG\nprint(\"Is DAG: {}\".format(networkx.is_directed_acyclic_graph(knowledge_graph)))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:48.191961Z","iopub.execute_input":"2023-05-12T13:57:48.19241Z","iopub.status.idle":"2023-05-12T13:57:48.751817Z","shell.execute_reply.started":"2023-05-12T13:57:48.192373Z","shell.execute_reply":"2023-05-12T13:57:48.750399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# using networkx we can get the decendants of a node\nterm = \"GO:0003700\"\nprint(\"Descendants of {}: {}\".format(term, len(networkx.descendants(knowledge_graph, term))))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:48.754674Z","iopub.execute_input":"2023-05-12T13:57:48.755215Z","iopub.status.idle":"2023-05-12T13:57:48.762611Z","shell.execute_reply.started":"2023-05-12T13:57:48.755181Z","shell.execute_reply":"2023-05-12T13:57:48.760828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# using the above we can get a subgraph for a given term containing the term and all its descendants\ndef get_subgraph(graph, term):\n    descendants = networkx.descendants(graph, term)\n    return networkx.subgraph(graph, [term] + list(descendants))","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:48.764755Z","iopub.execute_input":"2023-05-12T13:57:48.765344Z","iopub.status.idle":"2023-05-12T13:57:48.776196Z","shell.execute_reply.started":"2023-05-12T13:57:48.765296Z","shell.execute_reply":"2023-05-12T13:57:48.774509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get node atrributes\nknowledge_graph.nodes[term]","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:48.777962Z","iopub.execute_input":"2023-05-12T13:57:48.779444Z","iopub.status.idle":"2023-05-12T13:57:48.795462Z","shell.execute_reply.started":"2023-05-12T13:57:48.779388Z","shell.execute_reply":"2023-05-12T13:57:48.793062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# integrate the name attribute into our plots. (similar other attributes could be added)\n\ndef get_node_labels(graph):\n    labels = {}\n    for node in graph.nodes:\n        id = node\n        namespace = graph.nodes[node].get('name', '')  # Default to empty string if 'name' is not found\n        labels[node] = f'{id}\\n{namespace}'\n    return labels\n\ndef plot_subgraph(graph, term, width=10, height=10):\n    sg = get_subgraph(graph, term)\n    pos = graphviz_layout(sg, prog='dot')\n    labels = get_node_labels(sg)\n    plt.figure(figsize=(width, height))\n    networkx.draw_networkx(sg, pos, labels=labels, with_labels=True, node_color = 'lightblue', node_size=1000)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:48.797855Z","iopub.execute_input":"2023-05-12T13:57:48.798367Z","iopub.status.idle":"2023-05-12T13:57:48.810632Z","shell.execute_reply.started":"2023-05-12T13:57:48.798327Z","shell.execute_reply":"2023-05-12T13:57:48.809231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test\nplot_subgraph(knowledge_graph, term, 15, 15)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:55.831464Z","iopub.execute_input":"2023-05-12T13:57:55.831903Z","iopub.status.idle":"2023-05-12T13:57:56.329128Z","shell.execute_reply.started":"2023-05-12T13:57:55.83187Z","shell.execute_reply":"2023-05-12T13:57:56.327593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mapping from GO ID to name\nid_to_name = {id_: data.get('name') for id_, data in knowledge_graph.nodes(data=True)}\n# mapping from name to GO ID\nname_to_id = {data['name']: id_ for id_, data in knowledge_graph.nodes(data=True) if 'name' in data}","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:56.893578Z","iopub.execute_input":"2023-05-12T13:57:56.894025Z","iopub.status.idle":"2023-05-12T13:57:56.97051Z","shell.execute_reply.started":"2023-05-12T13:57:56.893991Z","shell.execute_reply":"2023-05-12T13:57:56.969327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test\nid_to_name[\"GO:0006302\"], name_to_id['double-strand break repair']","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:57.814179Z","iopub.execute_input":"2023-05-12T13:57:57.814885Z","iopub.status.idle":"2023-05-12T13:57:57.823951Z","shell.execute_reply.started":"2023-05-12T13:57:57.814836Z","shell.execute_reply":"2023-05-12T13:57:57.822892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the name of the roots.\nid_to_name['GO:0008150'], id_to_name['GO:0005575'], id_to_name['GO:0003674']","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:57:58.613813Z","iopub.execute_input":"2023-05-12T13:57:58.614724Z","iopub.status.idle":"2023-05-12T13:57:58.623275Z","shell.execute_reply.started":"2023-05-12T13:57:58.614683Z","shell.execute_reply":"2023-05-12T13:57:58.621801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get the corresponding graph\nplot_subgraph(knowledge_graph, 'GO:0003899', 15, 15)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we can reverse the graph as well to get the ancestors of a given term\nreversed_graph = knowledge_graph.reverse(copy=True)\n# plot the ancestors of GO:0003899\nplot_subgraph(reversed_graph, 'GO:0003899', 30, 30)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T13:58:00.744198Z","iopub.execute_input":"2023-05-12T13:58:00.744886Z","iopub.status.idle":"2023-05-12T13:58:05.80372Z","shell.execute_reply.started":"2023-05-12T13:58:00.74483Z","shell.execute_reply":"2023-05-12T13:58:05.801822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from here: https://www.ebi.ac.uk/QuickGO/annotations?geneProductId=P9WHI7\n# the GO annotations for the protein P9WHI7\nannotations = [\n\"GO:0009274\",\n\"GO:0000724\",\n\"GO:0009314\",\n\"GO:0009432\",\n\"GO:0005524\",\n\"GO:0006281\",\n\"GO:0006310\",\n\"GO:0005524\",\n\"GO:0006281\",\n\"GO:0006974\",\n\"GO:0000166\"\n]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lets check what we can learn from our knowledge graph\n# about these annotations\nfor annotation in annotations:\n    print(annotation, id_to_name[annotation])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot all the annotations\nfor annotation in annotations:\n    plot_subgraph(knowledge_graph, annotation, 25, 25)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we can see that a protein can have a variety of different jobs","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}