{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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":14875579,"sourceType":"competition"},{"sourceId":14503500,"sourceType":"datasetVersion","datasetId":9263259}],"dockerImageVersionId":31259,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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)\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":"2026-01-26T05:53:09.329280Z","iopub.execute_input":"2026-01-26T05:53:09.329707Z","iopub.status.idle":"2026-01-26T05:53:09.349543Z","shell.execute_reply.started":"2026-01-26T05:53:09.329615Z","shell.execute_reply":"2026-01-26T05:53:09.348602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom collections import defaultdict\nfrom tqdm import tqdm\nfrom functools import lru_cache\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:53:09.351316Z","iopub.execute_input":"2026-01-26T05:53:09.351688Z","iopub.status.idle":"2026-01-26T05:53:09.366273Z","shell.execute_reply.started":"2026-01-26T05:53:09.351660Z","shell.execute_reply":"2026-01-26T05:53:09.365189Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Input submission from your similarity model\nSUB_PATH = \"/kaggle/input/similaritysubmission/submission (3).tsv\"\n# Example:\n# SUB_PATH = \"/kaggle/input/my-submission-dataset/similarity_submission.tsv\"\n\n# Gene Ontology file\nOBO_PATH = \"/kaggle/input/cafa-6-protein-function-prediction/Train/go-basic.obo\"\n\n# Output boosted submission\nOUT_PATH = \"/kaggle/working/submission.tsv\"\n\nprint(\"Paths ready\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:53:09.367708Z","iopub.execute_input":"2026-01-26T05:53:09.368018Z","iopub.status.idle":"2026-01-26T05:53:09.392935Z","shell.execute_reply.started":"2026-01-26T05:53:09.367990Z","shell.execute_reply":"2026-01-26T05:53:09.391852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def parse_go_obo(path):\n    parents = defaultdict(list)\n    current = None\n\n    with open(path) as f:\n        for line in f:\n            line = line.strip()\n            if line == \"[Term]\":\n                current = None\n            elif line.startswith(\"id:\"):\n                current = line.split(\"id:\")[1].strip()\n            elif line.startswith(\"is_a:\") and current:\n                parent = line.split(\"is_a:\")[1].split()[0]\n                parents[current].append(parent)\n    return parents\n\ngo_parents = parse_go_obo(OBO_PATH)\nprint(\"GO terms loaded:\", len(go_parents))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:53:09.394150Z","iopub.execute_input":"2026-01-26T05:53:09.394657Z","iopub.status.idle":"2026-01-26T05:53:09.829160Z","shell.execute_reply.started":"2026-01-26T05:53:09.394616Z","shell.execute_reply":"2026-01-26T05:53:09.828156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"@lru_cache(maxsize=None)\ndef get_ancestors(go):\n    res = set()\n    for p in go_parents.get(go, []):\n        res.add(p)\n        res |= get_ancestors(p)\n    return res\n\nprint(\"Ancestor cache ready\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:53:09.831629Z","iopub.execute_input":"2026-01-26T05:53:09.831963Z","iopub.status.idle":"2026-01-26T05:53:09.838065Z","shell.execute_reply.started":"2026-01-26T05:53:09.831935Z","shell.execute_reply":"2026-01-26T05:53:09.837148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"protein_scores = defaultdict(dict)\n\nwith open(SUB_PATH) as f:\n    for line in tqdm(f):\n        pid, go, score = line.strip().split(\"\\t\")\n        score = float(score)\n        protein_scores[pid][go] = max(\n            protein_scores[pid].get(go, 0),\n            score\n        )\n\nprint(\"Proteins loaded:\", len(protein_scores))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:54:05.627822Z","iopub.execute_input":"2026-01-26T05:54:05.628181Z","iopub.status.idle":"2026-01-26T05:54:23.191882Z","shell.execute_reply.started":"2026-01-26T05:54:05.628153Z","shell.execute_reply":"2026-01-26T05:54:23.190890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for pid in protein_scores:\n    max_sc = max(protein_scores[pid].values())\n    if max_sc < 0.95:\n        scale = 0.95 / max_sc\n        for go in protein_scores[pid]:\n            protein_scores[pid][go] *= scale\n\nprint(\"Score calibration completed\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:54:54.120314Z","iopub.execute_input":"2026-01-26T05:54:54.120681Z","iopub.status.idle":"2026-01-26T05:54:56.416397Z","shell.execute_reply.started":"2026-01-26T05:54:54.120645Z","shell.execute_reply":"2026-01-26T05:54:56.414852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# MIN_SCORE = 0.001\n\n# with open(OUT_PATH, \"w\") as f:\n#     for pid, gos in protein_scores.items():\n#         for go, sc in gos.items():\n#             if sc >= MIN_SCORE:\n#                 f.write(f\"{pid}\\t{go}\\t{round(sc,3)}\\n\")\n\n# print(\"Saved:\", OUT_PATH)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:53:19.860687Z","iopub.status.idle":"2026-01-26T05:53:19.861099Z","shell.execute_reply.started":"2026-01-26T05:53:19.860920Z","shell.execute_reply":"2026-01-26T05:53:19.860943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import defaultdict\nfrom tqdm import tqdm\n\nSUB_PATH = \"/kaggle/input/similaritysubmission/submission (3).tsv\"\nOUT_PATH = \"/kaggle/working/submission.tsv\"\n\nprotein_scores = defaultdict(dict)\n\nwith open(SUB_PATH) as f:\n    for line in tqdm(f):\n        pid, go, score = line.strip().split(\"\\t\")\n        protein_scores[pid][go] = float(score)\n\nprint(\"Proteins loaded:\", len(protein_scores))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:54:59.863457Z","iopub.execute_input":"2026-01-26T05:54:59.863830Z","iopub.status.idle":"2026-01-26T05:55:13.752109Z","shell.execute_reply.started":"2026-01-26T05:54:59.863798Z","shell.execute_reply":"2026-01-26T05:55:13.750969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"PROP_THRESHOLD = 0.15\nMAX_PARENTS = 50\n\nfor pid in tqdm(protein_scores):\n    original = dict(protein_scores[pid])\n    parent_scores = defaultdict(float)\n\n    for go, sc in original.items():\n        if sc < PROP_THRESHOLD:\n            continue\n\n        for parent in go_parents.get(go, []):\n            parent_scores[parent] = max(parent_scores[parent], sc * 0.8)\n\n    # Keep only top parents\n    sorted_parents = sorted(\n        parent_scores.items(), key=lambda x: -x[1]\n    )[:MAX_PARENTS]\n\n    for go, sc in sorted_parents:\n        protein_scores[pid][go] = max(\n            protein_scores[pid].get(go, 0),\n            sc\n        )\n\nprint(\"Smart propagation done\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:55:29.178980Z","iopub.execute_input":"2026-01-26T05:55:29.179333Z","iopub.status.idle":"2026-01-26T05:55:30.934599Z","shell.execute_reply.started":"2026-01-26T05:55:29.179303Z","shell.execute_reply":"2026-01-26T05:55:30.933235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TARGET_MAX = 0.85\n\nfor pid in protein_scores:\n    max_sc = max(protein_scores[pid].values())\n    if max_sc < TARGET_MAX:\n        scale = TARGET_MAX / max_sc\n        for go in protein_scores[pid]:\n            protein_scores[pid][go] *= scale\n\nprint(\"Soft calibration done\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:55:34.945490Z","iopub.execute_input":"2026-01-26T05:55:34.945909Z","iopub.status.idle":"2026-01-26T05:55:37.156307Z","shell.execute_reply.started":"2026-01-26T05:55:34.945881Z","shell.execute_reply":"2026-01-26T05:55:37.155178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MIN_SCORE = 0.01\n\nwith open(OUT_PATH, \"w\") as f:\n    for pid, gos in protein_scores.items():\n        for go, sc in gos.items():\n            if sc >= MIN_SCORE:\n                f.write(f\"{pid}\\t{go}\\t{round(sc,3)}\\n\")\n\nprint(\"Saved:\", OUT_PATH)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-26T05:55:42.754075Z","iopub.execute_input":"2026-01-26T05:55:42.754962Z","iopub.status.idle":"2026-01-26T05:55:56.616589Z","shell.execute_reply.started":"2026-01-26T05:55:42.754928Z","shell.execute_reply":"2026-01-26T05:55:56.615543Z"}},"outputs":[],"execution_count":null}]}