{"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":"# Introduction\n\nYou are given a list of proteins (with identifiers), their species (taxon ID), and for training proteins, their known GO term annotations. The Gene Ontology (go-basic.obo) provides the vocabulary of possible functions.\n\nOutput:\nFor each target (unannotated) protein we need to predict one or more GO term IDs that describe its biological function.","metadata":{}},{"cell_type":"code","source":"!pip install Bio\nfrom Bio import SeqIO\nimport pandas as pd\nimport plotly.express as px\nimport plotly.io as pio\n\n!pip install pronto\nfrom pronto import Ontology\n!pip install goatools\nimport matplotlib.pyplot as plt\nimport squarify  \nfrom goatools.obo_parser import GODag\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:14.104008Z","iopub.execute_input":"2025-10-20T18:58:14.104819Z","iopub.status.idle":"2025-10-20T18:58:26.386856Z","shell.execute_reply.started":"2025-10-20T18:58:14.104788Z","shell.execute_reply":"2025-10-20T18:58:26.385475Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_sequences = {}\nfor record in SeqIO.parse(\"/kaggle/input/cafa-6-protein-function-prediction/Train/train_sequences.fasta\", \"fasta\"):\n    train_sequences[record.id] = str(record.seq)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:26.388920Z","iopub.execute_input":"2025-10-20T18:58:26.389294Z","iopub.status.idle":"2025-10-20T18:58:26.908450Z","shell.execute_reply.started":"2025-10-20T18:58:26.389251Z","shell.execute_reply":"2025-10-20T18:58:26.907349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain_taxanomy = pd.read_csv(\"/kaggle/input/cafa-6-protein-function-prediction/Train/train_taxonomy.tsv\", sep=\"\\t\", names=[\"protein_id\", \"go_id\"])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:26.909354Z","iopub.execute_input":"2025-10-20T18:58:26.909599Z","iopub.status.idle":"2025-10-20T18:58:26.987271Z","shell.execute_reply.started":"2025-10-20T18:58:26.909580Z","shell.execute_reply":"2025-10-20T18:58:26.986329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_taxanomy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:26.989362Z","iopub.execute_input":"2025-10-20T18:58:26.989675Z","iopub.status.idle":"2025-10-20T18:58:26.998043Z","shell.execute_reply.started":"2025-10-20T18:58:26.989619Z","shell.execute_reply":"2025-10-20T18:58:26.996983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntrain_terms = pd.read_csv(\"/kaggle/input/cafa-6-protein-function-prediction/Train/train_terms.tsv\", sep=\"\\t\", names=[\"protein_id\", \"go_id\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:26.999127Z","iopub.execute_input":"2025-10-20T18:58:26.999462Z","iopub.status.idle":"2025-10-20T18:58:27.290895Z","shell.execute_reply.started":"2025-10-20T18:58:26.999431Z","shell.execute_reply":"2025-10-20T18:58:27.289881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_terms)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:27.292007Z","iopub.execute_input":"2025-10-20T18:58:27.292256Z","iopub.status.idle":"2025-10-20T18:58:27.299967Z","shell.execute_reply.started":"2025-10-20T18:58:27.292237Z","shell.execute_reply":"2025-10-20T18:58:27.298851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tax_counts = train_taxanomy[\"go_id\"].value_counts() \ntax_counts.head(10)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:27.301041Z","iopub.execute_input":"2025-10-20T18:58:27.301469Z","iopub.status.idle":"2025-10-20T18:58:27.322309Z","shell.execute_reply.started":"2025-10-20T18:58:27.301431Z","shell.execute_reply":"2025-10-20T18:58:27.321397Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"| Taxonomy ID | Scientific name                   | Common name               | Kingdom  |\n| ----------- | --------------------------------- | ------------------------- | -------- |\n| **9606**    | *Homo sapiens*                    | Human                     | Animalia |\n| **10090**   | *Mus musculus*                    | Mouse                     | Animalia |\n| **3702**    | *Arabidopsis thaliana*            | Thale cress (plant model) | Plantae  |\n| **559292**  | *Saccharomyces cerevisiae* S288C  | Baker’s yeast             | Fungi    |\n| **10116**   | *Rattus norvegicus*               | Rat                       | Animalia |\n| **284812**  | *Schizosaccharomyces pombe* 972h− | Fission yeast             | Fungi    |\n| **83333**   | *Escherichia coli* K-12           | E. coli (bacterium)       | Bacteria |\n| **7227**    | *Drosophila melanogaster*         | Fruit fly                 | Animalia |\n| **6239**    | *Caenorhabditis elegans*          | Nematode (worm)           | Animalia |\n| **83332**   | *Escherichia coli*                | E. coli (general)         | Bacteria |\n","metadata":{}},{"cell_type":"code","source":"\n\nfasta_file = \"/kaggle/input/cafa-6-protein-function-prediction/Train/train_sequences.fasta\"\nseq_records = list(SeqIO.parse(fasta_file, \"fasta\"))\n\nsequences_df = pd.DataFrame({\n    \"protein_id\": [rec.id for rec in seq_records],\n    \"sequence\": [str(rec.seq) for rec in seq_records]\n})\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:27.323435Z","iopub.execute_input":"2025-10-20T18:58:27.323763Z","iopub.status.idle":"2025-10-20T18:58:28.008524Z","shell.execute_reply.started":"2025-10-20T18:58:27.323737Z","shell.execute_reply":"2025-10-20T18:58:28.007401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tax_df = pd.read_csv(\"/kaggle/input/cafa-6-protein-function-prediction/Train/train_taxonomy.tsv\", sep=\"\\t\", names=[\"protein_id\", \"taxon_id\"])\nterms_df = pd.read_csv(\"/kaggle/input/cafa-6-protein-function-prediction/Train/train_terms.tsv\", sep=\"\\t\", names=[\"protein_id\", \"go_id\", \"aspect\"])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:28.009588Z","iopub.execute_input":"2025-10-20T18:58:28.009878Z","iopub.status.idle":"2025-10-20T18:58:28.340256Z","shell.execute_reply.started":"2025-10-20T18:58:28.009857Z","shell.execute_reply":"2025-10-20T18:58:28.339076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"go_agg = terms_df.groupby(\"protein_id\")[\"go_id\"].apply(list).reset_index()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:28.343859Z","iopub.execute_input":"2025-10-20T18:58:28.344163Z","iopub.status.idle":"2025-10-20T18:58:31.218504Z","shell.execute_reply.started":"2025-10-20T18:58:28.344132Z","shell.execute_reply":"2025-10-20T18:58:31.217675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"go_agg[\"go_id\"] = go_agg[\"go_id\"].apply(lambda x: \",\".join(x))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:31.219369Z","iopub.execute_input":"2025-10-20T18:58:31.219581Z","iopub.status.idle":"2025-10-20T18:58:31.288319Z","shell.execute_reply.started":"2025-10-20T18:58:31.219564Z","shell.execute_reply":"2025-10-20T18:58:31.287440Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sequences_df[\"protein_id\"] = sequences_df[\"protein_id\"].apply(lambda x: x.split(\"|\")[1])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:31.289315Z","iopub.execute_input":"2025-10-20T18:58:31.289571Z","iopub.status.idle":"2025-10-20T18:58:31.327872Z","shell.execute_reply.started":"2025-10-20T18:58:31.289552Z","shell.execute_reply":"2025-10-20T18:58:31.326878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df = sequences_df.merge(tax_df, on=\"protein_id\", how=\"left\") \\\n                        .merge(go_agg, on=\"protein_id\", how=\"left\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:31.329052Z","iopub.execute_input":"2025-10-20T18:58:31.329377Z","iopub.status.idle":"2025-10-20T18:58:31.496691Z","shell.execute_reply.started":"2025-10-20T18:58:31.329350Z","shell.execute_reply":"2025-10-20T18:58:31.495920Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:31.497529Z","iopub.execute_input":"2025-10-20T18:58:31.497894Z","iopub.status.idle":"2025-10-20T18:58:31.509590Z","shell.execute_reply.started":"2025-10-20T18:58:31.497863Z","shell.execute_reply":"2025-10-20T18:58:31.508551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_expl = merged_df.copy()\nmerged_expl = merged_expl.drop(\"sequence\", axis=1)  \n\nmerged_expl = merged_expl.assign(go_id=merged_expl[\"go_id\"].str.split(\",\")).explode(\"go_id\")\n\nmerged_expl = merged_expl.merge(terms_df[[\"protein_id\", \"go_id\", \"aspect\"]], \n                                on=[\"protein_id\", \"go_id\"], how=\"left\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:31.510609Z","iopub.execute_input":"2025-10-20T18:58:31.510941Z","iopub.status.idle":"2025-10-20T18:58:32.469843Z","shell.execute_reply.started":"2025-10-20T18:58:31.510920Z","shell.execute_reply":"2025-10-20T18:58:32.468848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\ntaxon_map = {\n    9606: \"Homo sapiens\",\n    10090: \"Mus musculus\",\n    3702: \"Arabidopsis thaliana\",\n    559292: \"Saccharomyces cerevisiae\",\n    284812: \"Schizosaccharomyces pombe\"\n}\n\nmerged_df[\"species\"] = merged_df[\"taxon_id\"].map(taxon_map)\nmerged_small = merged_df[merged_df[\"species\"].notna()]\nmerged_expl = merged_small.assign(go_id=merged_small[\"go_id\"].str.split(\",\")).explode(\"go_id\")\n\ngo = GODag(\"/kaggle/input/cafa-6-protein-function-prediction/Train/go-basic.obo\")\n\ndef go_name(go_id):\n    return go[go_id].name if go_id in go else go_id\n\nmerged_expl[\"go_name\"] = merged_expl[\"go_id\"].apply(go_name)\n\n# Aggregate counts\nagg_df = merged_expl.groupby([\"species\", \"go_name\"]).size().reset_index(name=\"count\")\n\n# Filter top 20 GO terms by total count\ntop_go = agg_df.groupby(\"go_name\")[\"count\"].sum().nlargest(20).index\nagg_df = agg_df[agg_df[\"go_name\"].isin(top_go)]\n\n# Create labels for the treemap\nagg_df[\"label\"] = agg_df[\"species\"] + \"\\n\" + agg_df[\"go_name\"]\n\n# Plot treemap\nplt.figure(figsize=(16, 10))\nsquarify.plot(\n    sizes=agg_df[\"count\"],\n    label=agg_df[\"label\"],\n    alpha=0.8\n)\nplt.axis('off')\nplt.title(\"Top 20 GO Terms by Species\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:32.470956Z","iopub.execute_input":"2025-10-20T18:58:32.471907Z","iopub.status.idle":"2025-10-20T18:58:34.534025Z","shell.execute_reply.started":"2025-10-20T18:58:32.471872Z","shell.execute_reply":"2025-10-20T18:58:34.533087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ngo = Ontology(\"/kaggle/input/cafa-6-protein-function-prediction/Train/go-basic.obo\")\n\nterm = go['GO:0000785']\nprint(term.id, term.name, term.namespace)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T18:58:34.534994Z","iopub.execute_input":"2025-10-20T18:58:34.535224Z","iopub.status.idle":"2025-10-20T18:58:41.994762Z","shell.execute_reply.started":"2025-10-20T18:58:34.535207Z","shell.execute_reply":"2025-10-20T18:58:41.993448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n\nmerged_df[\"species\"] = merged_df[\"taxon_id\"].map(taxon_map)\nmerged_small = merged_df[merged_df[\"species\"].notna()]\nmerged_expl = merged_small.assign(go_id=merged_small[\"go_id\"].str.split(\",\")).explode(\"go_id\")\n\n# Aggregate counts\nagg_df = merged_expl.groupby([\"species\", \"go_id\"]).size().reset_index(name=\"count\")\n\n# Select top 20 GO IDs\ntop_go = agg_df.groupby(\"go_id\")[\"count\"].sum().nlargest(20).index\nagg_df = agg_df[agg_df[\"go_id\"].isin(top_go)]\n\n# Sort for better plot\nagg_df = agg_df.sort_values(\"count\", ascending=False)\n\n# Plot bar chart\nplt.figure(figsize=(14, 8))\nsns.barplot(\n    data=agg_df,\n    x=\"count\",\n    y=\"go_id\",\n    hue=\"species\"\n)\nplt.title(\"Top 20 GO IDs by Species\")\nplt.xlabel(\"Count\")\nplt.ylabel(\"GO ID\")\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:00:33.085481Z","iopub.execute_input":"2025-10-20T19:00:33.089117Z","iopub.status.idle":"2025-10-20T19:00:34.440779Z","shell.execute_reply.started":"2025-10-20T19:00:33.088982Z","shell.execute_reply":"2025-10-20T19:00:34.439688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pio.renderers.default = 'notebook' \n\ngo = Ontology(\"/kaggle/input/cafa-6-protein-function-prediction/Train/go-basic.obo\")\n\nmerged_df[\"species\"] = merged_df[\"taxon_id\"].map(taxon_map)\n\nmerged_small = merged_df[merged_df[\"species\"].notna()]\n\nmerged_expl = merged_small.assign(go_id=merged_small[\"go_id\"].str.split(\",\")).explode(\"go_id\")\n\ndef go_name(go_id):\n    try:\n        return go[go_id].name\n    except KeyError:\n        return go_id  \n\nmerged_expl[\"go_name\"] = merged_expl[\"go_id\"].apply(go_name)\n\nagg_df = merged_expl.groupby([\"species\", \"go_name\"]).size().reset_index(name=\"count\")\n\ntop_go = agg_df.groupby(\"go_name\")[\"count\"].sum().nlargest(20).index\nagg_df = agg_df[agg_df[\"go_name\"].isin(top_go)]\n\n\nfig = px.sunburst(\n    agg_df,\n    path=[\"species\", \"go_name\"], \n    values=\"count\",\n    color=\"species\"\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-20T19:00:44.888099Z","iopub.execute_input":"2025-10-20T19:00:44.888979Z","iopub.status.idle":"2025-10-20T19:00:57.248822Z","shell.execute_reply.started":"2025-10-20T19:00:44.888944Z","shell.execute_reply":"2025-10-20T19:00:57.247739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}