{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":116062,"databundleVersionId":14084779,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on October 15, 2025. By Marília Prata. mpwolke","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-15T22:42:23.186985Z","iopub.execute_input":"2025-10-15T22:42:23.187354Z","iopub.status.idle":"2025-10-15T22:42:24.595573Z","shell.execute_reply.started":"2025-10-15T22:42:23.187328Z","shell.execute_reply":"2025-10-15T22:42:24.594576Z"},"_kg_hide-input":true,"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"![](https://i.ytimg.com/vi/ONiWugVEf2s/hq720.jpg?sqp=-oaymwEhCK4FEIIDSFryq4qpAxMIARUAAAAAGAElAADIQj0AgKJD&rs=AOn4CLD0jFw0JEZbTyutoSeUBgU4L4zLBw)Youtube","metadata":{}},{"cell_type":"markdown","source":"## Competition Citation:\n\n@misc{cafa-6-protein-function-prediction,\n\n    author = {Iddo Friedberg and Predrag Radivojac and Paul D Thomas and An Phan and M. Clara De Paolis Kaluza and Damiano Piovesan and Parnal Joshi and Chris Mungall and Martyna Plomecka and Walter Reade and María Cruz},\n    \n    title = {CAFA 6 Protein Function Prediction},\n    year = {2025},\n    \n    howpublished = {\\url{https://kaggle.com/competitions/cafa-6-protein-function-prediction}},\n    note = {Kaggle}\n}","metadata":{}},{"cell_type":"markdown","source":"## Prospective data Competition \n\n\"This is a prospective (i.e., future) data competition. Many proteins in the Test data do not currently have any assigned functions. Proteins having one or more of their functions published by researchers during the curation phase of the competition will comprise the future test set. Final leaderboard scores will be calculated after the curation phase of the competition.\"\n\n\"The organizers provide a set of protein sequences on which the participants are asked to predict **Gene Ontology (GO)** terms in each of the three subontologies: **Molecular Function (MF)**, **Biological Process (BP), and Cellular Component (CC)**. This set of sequences is referred to as the test superset.\"\n\nhttps://www.kaggle.com/competitions/cafa-6-protein-function-prediction/overview","metadata":{}},{"cell_type":"markdown","source":"## GOATOOLS: A Python library for Gene Ontology analyses","metadata":{}},{"cell_type":"code","source":"!pip install goatools","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:29:06.872138Z","iopub.execute_input":"2025-10-15T23:29:06.873126Z","iopub.status.idle":"2025-10-15T23:29:49.878581Z","shell.execute_reply.started":"2025-10-15T23:29:06.873092Z","shell.execute_reply":"2025-10-15T23:29:49.877470Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## OBO parser","metadata":{}},{"cell_type":"code","source":"# Import the OBO parser from GOATools\nfrom goatools import obo_parser","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:29:55.665303Z","iopub.execute_input":"2025-10-15T23:29:55.665672Z","iopub.status.idle":"2025-10-15T23:29:56.012673Z","shell.execute_reply.started":"2025-10-15T23:29:55.665641Z","shell.execute_reply":"2025-10-15T23:29:56.011575Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install wget","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:30:00.284908Z","iopub.execute_input":"2025-10-15T23:30:00.285552Z","iopub.status.idle":"2025-10-15T23:30:06.456091Z","shell.execute_reply.started":"2025-10-15T23:30:00.285523Z","shell.execute_reply":"2025-10-15T23:30:06.454775Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import wget","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:30:21.281143Z","iopub.execute_input":"2025-10-15T23:30:21.281968Z","iopub.status.idle":"2025-10-15T23:30:21.292077Z","shell.execute_reply.started":"2025-10-15T23:30:21.281932Z","shell.execute_reply":"2025-10-15T23:30:21.291151Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## OBO path","metadata":{}},{"cell_type":"code","source":"#By Alexander Chervov https://www.kaggle.com/code/alexandervc/gene-ontology-python-tutorial\n\nobo_path = \"../input/cafa-6-protein-function-prediction/Train/go-basic.obo\"\n\ngo_obo_url = 'http://purl.obolibrary.org/obo/go/go-basic.obo'\ndata_folder = os.getcwd() + '/data'\n\n# Check if we have the ./data directory already\nif(not os.path.isfile(data_folder)):\n    # Emulate mkdir -p (no error if folder exists)\n    try:\n        os.mkdir(data_folder)\n    except OSError as e:\n        if(e.errno != 17):\n            raise e\nelse:\n    raise Exception('Data path (' + data_folder + ') exists as a file. '\n                   'Please rename, remove or change the desired location of the data path.')\n\n# Check if the file exists already\nif(not os.path.isfile(data_folder+'/go-basic.obo')):\n    go_obo = wget.download(go_obo_url, data_folder+'/go-basic.obo')\nelse:\n    go_obo = data_folder+'/go-basic.obo'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:32:22.492816Z","iopub.execute_input":"2025-10-15T23:32:22.493186Z","iopub.status.idle":"2025-10-15T23:32:23.303409Z","shell.execute_reply.started":"2025-10-15T23:32:22.493159Z","shell.execute_reply":"2025-10-15T23:32:23.302476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install Bio","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:39:09.441163Z","iopub.execute_input":"2025-10-15T23:39:09.441519Z","iopub.status.idle":"2025-10-15T23:39:15.901814Z","shell.execute_reply.started":"2025-10-15T23:39:09.441497Z","shell.execute_reply":"2025-10-15T23:39:15.900526Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Fasta file\n\nThat's not fast. It simply stopped my Notebook AGAIN! I wrote that two years ago.\n\n## **It seems I'm Homo inhabilis.**","metadata":{}},{"cell_type":"code","source":"#https://stackoverflow.com/questions/29805642/learning-to-parse-a-fasta-file-with-python\n\ndef read_fasta(fp):\n        name, seq = None, []\n        for line in fp:\n            line = line.rstrip()\n            if line.startswith(\">\"):\n                if name: yield (name, ''.join(seq))\n                name, seq = line, []\n            else:\n                seq.append(line)\n        if name: yield (name, ''.join(seq))\n\nwith open('../input/cafa-6-protein-function-prediction/Train/train_sequences.fasta') as fp:\n    for name, seq in read_fasta(fp):\n        print(name, seq)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:44:22.356757Z","iopub.execute_input":"2025-10-15T23:44:22.357623Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### I simply forgot what I done 2y ago 🥴 awkward :)","metadata":{}},{"cell_type":"markdown","source":"## Load IA tsv\n\n\"Information accretion: **IA.tsv** contains the information accretion (weights) for each GO term. These weights are used to compute weighted precision and recall, as described in the Evaluation section.\"\n\nhttps://www.kaggle.com/competitions/cafa-6-protein-function-prediction/data","metadata":{}},{"cell_type":"code","source":"IA = pd.read_csv('../input/cafa-6-protein-function-prediction/IA.tsv', sep='\\t')\nIA.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T22:45:59.958908Z","iopub.execute_input":"2025-10-15T22:45:59.959835Z","iopub.status.idle":"2025-10-15T22:46:00.037283Z","shell.execute_reply.started":"2025-10-15T22:45:59.959798Z","shell.execute_reply":"2025-10-15T22:46:00.036503Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load Train_taxonomy\n\n\"Taxonomy: **train_taxonomy.tsv** contains the list of proteins and the species to which they belong, represented by a taxonomic identifier (taxon ID). The first column is the protein UniProt accession ID and the second is the taxon ID.\"\n\nhttps://www.kaggle.com/competitions/cafa-6-protein-function-prediction/data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/cafa-6-protein-function-prediction/Train/train_taxonomy.tsv', sep='\\t')\ntrain.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T22:49:55.214521Z","iopub.execute_input":"2025-10-15T22:49:55.214863Z","iopub.status.idle":"2025-10-15T22:49:55.290224Z","shell.execute_reply.started":"2025-10-15T22:49:55.214837Z","shell.execute_reply":"2025-10-15T22:49:55.289203Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 9606 is Homo sapiens on testsuperset-taxon-list","metadata":{}},{"cell_type":"code","source":"train['9606'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:19:25.708304Z","iopub.execute_input":"2025-10-15T23:19:25.708674Z","iopub.status.idle":"2025-10-15T23:19:25.720131Z","shell.execute_reply.started":"2025-10-15T23:19:25.708650Z","shell.execute_reply":"2025-10-15T23:19:25.719110Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load Taxon-list","metadata":{}},{"cell_type":"code","source":"lis = pd.read_csv('../input/cafa-6-protein-function-prediction/Test/testsuperset-taxon-list.tsv', sep='\\t')\nlis.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:57:47.581821Z","iopub.execute_input":"2025-10-15T23:57:47.582182Z","iopub.status.idle":"2025-10-15T23:57:47.613428Z","shell.execute_reply.started":"2025-10-15T23:57:47.582157Z","shell.execute_reply":"2025-10-15T23:57:47.612447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lis['Species'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:17:00.263620Z","iopub.execute_input":"2025-10-15T23:17:00.263994Z","iopub.status.idle":"2025-10-15T23:17:00.279092Z","shell.execute_reply.started":"2025-10-15T23:17:00.263969Z","shell.execute_reply":"2025-10-15T23:17:00.278082Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Code by Taha07  https://www.kaggle.com/taha07/data-scientists-jobs-analysis-visualization/notebook\n\nfrom wordcloud import WordCloud\nfrom wordcloud import STOPWORDS\nstopwords = set(STOPWORDS)\nwordcloud = WordCloud(background_color = 'purple',\n                      height =2000,\n                      width = 2000\n                     ).generate(str(lis[\"Species\"]))\nplt.rcParams['figure.figsize'] = (12,12)\nplt.axis(\"off\")\nplt.imshow(wordcloud)\nplt.title(\"Taxonomy List\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:06:02.502370Z","iopub.execute_input":"2025-10-15T23:06:02.502739Z","iopub.status.idle":"2025-10-15T23:06:07.293049Z","shell.execute_reply.started":"2025-10-15T23:06:02.502714Z","shell.execute_reply":"2025-10-15T23:06:07.291892Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### That Rattus is this close to the sapiens.\n\n![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ1f2LX4-Auoyu5UllVObn0ChIAf4--GKGq8Q&s)\n\n#### Fine, that's just a squirrel.","metadata":{}},{"cell_type":"markdown","source":"## Load train_terms","metadata":{}},{"cell_type":"code","source":"ter = pd.read_csv('../input/cafa-6-protein-function-prediction/Train/train_terms.tsv', sep='\\t' )\nter.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:01:48.221758Z","iopub.execute_input":"2025-10-15T23:01:48.223307Z","iopub.status.idle":"2025-10-15T23:01:48.658822Z","shell.execute_reply.started":"2025-10-15T23:01:48.223203Z","shell.execute_reply":"2025-10-15T23:01:48.657566Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Load sample_submission (on_bad_lines='skip')","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('../input/cafa-6-protein-function-prediction/sample_submission.tsv', sep='\\t', on_bad_lines='skip')\nsub.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T22:59:18.478939Z","iopub.execute_input":"2025-10-15T22:59:18.479299Z","iopub.status.idle":"2025-10-15T22:59:18.510369Z","shell.execute_reply.started":"2025-10-15T22:59:18.479274Z","shell.execute_reply":"2025-10-15T22:59:18.509445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"homo_sapiens = lis[(lis['Species']=='Homo sapiens')].reset_index(drop=True)\nhomo_sapiens.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-15T23:58:01.833845Z","iopub.execute_input":"2025-10-15T23:58:01.834167Z","iopub.status.idle":"2025-10-15T23:58:01.845732Z","shell.execute_reply.started":"2025-10-15T23:58:01.834147Z","shell.execute_reply":"2025-10-15T23:58:01.844793Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Draft Session: 1h:16m.\n\n### Time to Homo inhabilis clock out.","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgements:\n\nAlexander Chervov https://www.kaggle.com/code/alexandervc/gene-ontology-python-tutorial\n\nMarília Prata https://www.kaggle.com/code/mpwolke/cafa-5-protein-prediction/notebook\n\nMarília Prata https://www.kaggle.com/code/mpwolke/danio-rerio/notebook","metadata":{}}]}