{"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":"- This notebook was forked from https://www.kaggle.com/code/pauljef/data-analysis-cafa-6-protein-function-prediction.\n- The officially provided data were loaded in DataFrame format and combined.\n\nI hope this helps improve the understanding and analysis of the data. Please let me know if you have any feedback. Thank you!","metadata":{}},{"cell_type":"code","source":"!pip install Bio\nfrom Bio import SeqIO\nimport pandas as pd\nimport re\n\n!pip install goatools\nfrom goatools.obo_parser import GODag","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2025-10-22T05:50:07.818003Z","iopub.execute_input":"2025-10-22T05:50:07.818225Z","iopub.status.idle":"2025-10-22T05:50:15.803471Z","shell.execute_reply.started":"2025-10-22T05:50:07.818207Z","shell.execute_reply":"2025-10-22T05:50:15.802359Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load each data","metadata":{}},{"cell_type":"markdown","source":"## `train_sequences.fasta`","metadata":{}},{"cell_type":"code","source":"fasta_path = \"/kaggle/input/cafa-6-protein-function-prediction/Train/train_sequences.fasta\"\nseq_records = list(SeqIO.parse(fasta_path, \"fasta\"))\nseq_records[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:15.804775Z","iopub.execute_input":"2025-10-22T05:50:15.805146Z","iopub.status.idle":"2025-10-22T05:50:17.036092Z","shell.execute_reply.started":"2025-10-22T05:50:15.805106Z","shell.execute_reply":"2025-10-22T05:50:17.035236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_meta(description, key):\n    if key == \"OS\":\n        match = re.search(r\"OS=(\\S+\\s\\S+)\", description)\n        if match:\n             return match.group(1).strip()\n    match = re.search(rf\"{key}=([^ ]+)\", description)\n    return match.group(1) if match else None\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    \"sequence_length\": [len(rec.seq) for rec in seq_records],\n    \"OS\": [extract_meta(rec.description, \"OS\") for rec in seq_records],\n    \"OX\": [extract_meta(rec.description, \"OX\") for rec in seq_records],\n    \"GN\": [extract_meta(rec.description, \"GN\") for rec in seq_records],\n    \"PE\": [extract_meta(rec.description, \"PE\") for rec in seq_records],\n    \"SV\": [extract_meta(rec.description, \"SV\") for rec in seq_records],\n})\nsequences_df[\"protein_id\"] = sequences_df[\"protein_id\"].apply(lambda x: x.split(\"|\")[1])\nsequences_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:17.036970Z","iopub.execute_input":"2025-10-22T05:50:17.037342Z","iopub.status.idle":"2025-10-22T05:50:17.715615Z","shell.execute_reply.started":"2025-10-22T05:50:17.037320Z","shell.execute_reply":"2025-10-22T05:50:17.714744Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"↑ The three elements — `taxon_id` in `train_taxonomy.tsv`, and `OS` and `OZ` in `train_sequences.fasta` — have a one-to-one correspondence.","metadata":{}},{"cell_type":"markdown","source":"## `train_taxonomy.tsv`","metadata":{}},{"cell_type":"code","source":"tax_path = \"/kaggle/input/cafa-6-protein-function-prediction/Train/train_taxonomy.tsv\"\ntax_df = pd.read_csv(tax_path, sep=\"\\t\", names=[\"protein_id\", \"taxon_id\"])\ntax_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:17.716640Z","iopub.execute_input":"2025-10-22T05:50:17.716953Z","iopub.status.idle":"2025-10-22T05:50:17.763522Z","shell.execute_reply.started":"2025-10-22T05:50:17.716925Z","shell.execute_reply":"2025-10-22T05:50:17.762494Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"↑ The three elements — `taxon_id` in `train_taxonomy.tsv`, and `OS` and `OZ` in `train_sequences.fasta` — have a one-to-one correspondence.","metadata":{}},{"cell_type":"markdown","source":"## `train_terms.tsv`","metadata":{}},{"cell_type":"code","source":"go_path = \"/kaggle/input/cafa-6-protein-function-prediction/Train/train_terms.tsv\"\ngo_df = pd.read_csv(go_path, sep=\"\\t\", header=0, names=[\"protein_id\", \"go_id\", \"aspect\"])\ngo_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:17.764561Z","iopub.execute_input":"2025-10-22T05:50:17.764783Z","iopub.status.idle":"2025-10-22T05:50:18.008202Z","shell.execute_reply.started":"2025-10-22T05:50:17.764764Z","shell.execute_reply":"2025-10-22T05:50:18.007240Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"↑ `namespace` in `go-basic.obo` and `aspect` in `train_terms.tsv` have a one-to-one correspondence.","metadata":{}},{"cell_type":"code","source":"go_agg_df = go_df.groupby(\"protein_id\")[\"go_id\"].apply(list).reset_index()\n\ngo_f_df = go_df[go_df[\"aspect\"] == \"F\"]\ngo_agg_f_df = go_f_df.groupby(\"protein_id\")[\"go_id\"].apply(list).reset_index()\ngo_agg_df = go_agg_df.merge(go_agg_f_df.rename(columns={\"go_id\": \"go_id_f\"}), on=\"protein_id\", how=\"left\")\n\ngo_p_df = go_df[go_df[\"aspect\"] == \"P\"]\ngo_agg_p_df = go_p_df.groupby(\"protein_id\")[\"go_id\"].apply(list).reset_index()\ngo_agg_df = go_agg_df.merge(go_agg_p_df.rename(columns={\"go_id\": \"go_id_p\"}), on=\"protein_id\", how=\"left\")\n\ngo_c_df = go_df[go_df[\"aspect\"] == \"C\"]\ngo_agg_c_df = go_c_df.groupby(\"protein_id\")[\"go_id\"].apply(list).reset_index()\ngo_agg_df = go_agg_df.merge(go_agg_c_df.rename(columns={\"go_id\": \"go_id_c\"}), on=\"protein_id\", how=\"left\")\n\ngo_agg_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:18.009250Z","iopub.execute_input":"2025-10-22T05:50:18.009653Z","iopub.status.idle":"2025-10-22T05:50:23.015646Z","shell.execute_reply.started":"2025-10-22T05:50:18.009622Z","shell.execute_reply":"2025-10-22T05:50:23.014854Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"↑ `go_id` = `go_id_f` + `go_id_p` + `go_id_c`","metadata":{}},{"cell_type":"markdown","source":"## `IA.tsv`","metadata":{}},{"cell_type":"code","source":"ia_path = \"/kaggle/input/cafa-6-protein-function-prediction/IA.tsv\"\nia_df = pd.read_csv(ia_path, sep=\"\\t\", names=[\"go_id\", \"weight\"])\nia_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:23.016429Z","iopub.execute_input":"2025-10-22T05:50:23.016636Z","iopub.status.idle":"2025-10-22T05:50:23.051215Z","shell.execute_reply.started":"2025-10-22T05:50:23.016618Z","shell.execute_reply":"2025-10-22T05:50:23.050324Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## `go-basic.obo`","metadata":{}},{"cell_type":"code","source":"go = GODag(\"/kaggle/input/cafa-6-protein-function-prediction/Train/go-basic.obo\")\nfirst_term_obj = go[next(iter(go))]\nkeys = list(first_term_obj.__dict__.keys())\nkeys","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:23.052267Z","iopub.execute_input":"2025-10-22T05:50:23.052585Z","iopub.status.idle":"2025-10-22T05:50:23.824080Z","shell.execute_reply.started":"2025-10-22T05:50:23.052555Z","shell.execute_reply":"2025-10-22T05:50:23.823272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"go_data = []\n\nfor go_id, term_obj in go.items():\n    data = {\n        \"go_id\": go_id,\n        \"name\": term_obj.name,\n        \"namespace\": term_obj.namespace,\n        \"level\": term_obj.level,\n        \"depth\": term_obj.depth,\n        \"is_obsolete\": term_obj.is_obsolete,\n        \"parents_ids\": \",\".join(term_obj._parents),\n    }\n    go_data.append(data)\n\nobo_df = pd.DataFrame(go_data)\nobo_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:23.825035Z","iopub.execute_input":"2025-10-22T05:50:23.825322Z","iopub.status.idle":"2025-10-22T05:50:23.995341Z","shell.execute_reply.started":"2025-10-22T05:50:23.825303Z","shell.execute_reply":"2025-10-22T05:50:23.994540Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"↑ `namespace` in `go-basic.obo` and `aspect` in `train_terms.tsv` have a one-to-one correspondence.","metadata":{}},{"cell_type":"markdown","source":"# Merge dataframe","metadata":{}},{"cell_type":"markdown","source":"## on `protein_id`","metadata":{}},{"cell_type":"code","source":"merge_on_protein_df = sequences_df.merge(tax_df, on=\"protein_id\", how=\"left\") \\\n                        .merge(go_agg_df, on=\"protein_id\", how=\"left\")\nmerge_on_protein_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:23.996233Z","iopub.execute_input":"2025-10-22T05:50:23.996501Z","iopub.status.idle":"2025-10-22T05:50:24.190153Z","shell.execute_reply.started":"2025-10-22T05:50:23.996477Z","shell.execute_reply":"2025-10-22T05:50:24.189376Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- This includes data from `train_sequences.fasta`, `train_taxonomy.tsv` and `train_terms.tsv`.\n- `taxon_id`, `OS` and `OZ` have a one-to-one correspondence.","metadata":{}},{"cell_type":"markdown","source":"## on `go_id`","metadata":{}},{"cell_type":"code","source":"merge_on_go_df = go_df.merge(ia_df, on=\"go_id\", how=\"left\") \\\n                    .merge(obo_df, on=\"go_id\", how=\"left\") \\\n                    .merge(sequences_df, on=\"protein_id\", how=\"left\") \\\n                    .merge(tax_df, on=\"protein_id\", how=\"left\")\n\ncol = merge_on_go_df.pop(\"protein_id\")\nmerge_on_go_df.insert(9, \"protein_id\", col)\n\nmerge_on_go_df ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-22T05:50:24.191041Z","iopub.execute_input":"2025-10-22T05:50:24.191397Z","iopub.status.idle":"2025-10-22T05:50:25.465222Z","shell.execute_reply.started":"2025-10-22T05:50:24.191377Z","shell.execute_reply":"2025-10-22T05:50:25.464363Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- This includes data from `train_terms.tsv`, `IA.tsv`, `go-basic.obo`, `train_sequences.fasta` and `train_taxonomy.tsv`.\n- `aspect` and `namespace` have a one-to-one correspondence.\n- `taxon_id`, `OS` and `OZ` have a one-to-one correspondence.","metadata":{}}]}