{"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":31192,"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":"2025-11-15T17:50:59.663069Z","iopub.execute_input":"2025-11-15T17:50:59.663388Z","iopub.status.idle":"2025-11-15T17:51:00.026611Z","shell.execute_reply.started":"2025-11-15T17:50:59.663363Z","shell.execute_reply":"2025-11-15T17:51:00.025468Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install biopython obonet","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:51:03.882763Z","iopub.execute_input":"2025-11-15T17:51:03.883538Z","iopub.status.idle":"2025-11-15T17:51:11.537386Z","shell.execute_reply.started":"2025-11-15T17:51:03.883508Z","shell.execute_reply":"2025-11-15T17:51:11.536074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom collections import Counter, defaultdict\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom Bio import SeqIO\nimport networkx as nx\nfrom typing import Dict, List, Tuple, Set\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Set plotting style\nsns.set_style(\"whitegrid\")\nplt.rcParams['figure.figsize'] = (12, 6)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:51:13.793258Z","iopub.execute_input":"2025-11-15T17:51:13.794290Z","iopub.status.idle":"2025-11-15T17:51:15.225168Z","shell.execute_reply.started":"2025-11-15T17:51:13.794252Z","shell.execute_reply":"2025-11-15T17:51:15.224102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dir = \"/kaggle/input/cafa-6-protein-function-prediction/\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:51:19.695417Z","iopub.execute_input":"2025-11-15T17:51:19.696042Z","iopub.status.idle":"2025-11-15T17:51:19.701191Z","shell.execute_reply.started":"2025-11-15T17:51:19.696012Z","shell.execute_reply":"2025-11-15T17:51:19.699890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_seq_file = f\"{data_dir}Train/train_sequences.fasta\"\ntest_seq_file = f\"{data_dir}Test/testsuperset.fasta\"\nterms_file = f\"{data_dir}Train/train_terms.tsv\"\ntaxonomy_file = f\"{data_dir}Train/train_taxonomy.tsv\"\nia_file = f\"{data_dir}IA.tsv\"\nobo_file = f\"{data_dir}Train/go-basic.obo\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:51:40.051982Z","iopub.execute_input":"2025-11-15T17:51:40.052354Z","iopub.status.idle":"2025-11-15T17:51:40.057903Z","shell.execute_reply.started":"2025-11-15T17:51:40.052330Z","shell.execute_reply":"2025-11-15T17:51:40.056796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def parse_obo(path):\n    terms = []\n    term = None\n\n    with open(path, \"r\", encoding=\"utf-8\") as f:\n        for line in f:\n            line = line.strip()\n\n            if line == \"[Term]\":\n                # Bắt đầu term mới\n                term = {}\n                continue\n            \n            if line == \"\" and term is not None:\n                # Kết thúc term\n                if \"id\" in term:  # bỏ qua các entry không có id\n                    terms.append(term)\n                term = None\n                continue\n\n            if term is None:\n                continue\n\n            # Xử lý từng field\n            if \": \" in line:\n                key, value = line.split(\": \", 1)\n\n                # Field đặc biệt có nhiều giá trị\n                if key in [\"synonym\", \"is_a\"]:\n                    term.setdefault(key, []).append(value)\n                else:\n                    # Field bình thường\n                    term[key] = value\n\n    return terms\n\n\ndef get_ids_numpy(terms):\n    return np.array([t[\"id\"] for t in terms])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:51:46.868528Z","iopub.execute_input":"2025-11-15T17:51:46.869524Z","iopub.status.idle":"2025-11-15T17:51:46.877451Z","shell.execute_reply.started":"2025-11-15T17:51:46.869492Z","shell.execute_reply":"2025-11-15T17:51:46.876217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nterms = parse_obo(\"/kaggle/input/cafa-6-protein-function-prediction/Train/go-basic.obo\")\nterms_random = terms[:1000]\n# Lấy mảng ID numpy\nids_np = get_ids_numpy(terms)\nprint(ids_np)\n\n# Ví dụ xem term đầu tiên\nprint(terms[0])\nprint(len(terms_random))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:51:56.575107Z","iopub.execute_input":"2025-11-15T17:51:56.575435Z","iopub.status.idle":"2025-11-15T17:51:57.360698Z","shell.execute_reply.started":"2025-11-15T17:51:56.575409Z","shell.execute_reply":"2025-11-15T17:51:57.359724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/cafa-6-protein-function-prediction/Train/train_terms.tsv\", sep=\"\\t\")\n\nentry_ids = df[\"EntryID\"].unique()     # loại bỏ trùng lặp\nentry_ids_np = np.array(entry_ids)\n\nprint(len(entry_ids_np))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T17:52:01.717498Z","iopub.execute_input":"2025-11-15T17:52:01.718567Z","iopub.status.idle":"2025-11-15T17:52:02.220648Z","shell.execute_reply.started":"2025-11-15T17:52:01.718524Z","shell.execute_reply":"2025-11-15T17:52:02.219529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"entry_ids_all = df['EntryID'].unique()\n\nterms_random_ids = [t['id'] for t in terms_random]\n\ndf_filtered = df[df['term'].isin(terms_random_ids)]\n# binary_matrix = pd.crosstab(df_filtered['EntryID'], df_filtered['term'])\n# binary_matrix = binary_matrix.reindex(columns=terms_random_ids, fill_value=0)\n\n# matrix_np = binary_matrix.to_numpy()\n# entry_ids = binary_matrix.index.to_numpy()\n\n# print(matrix_np.shape)\n\nbinary_matrix = pd.crosstab(df_filtered['EntryID'], df_filtered['term'])\nbinary_matrix = binary_matrix.reindex(columns=terms_random_ids, fill_value=0)\nbinary_matrix = binary_matrix.reindex(index=entry_ids_all, fill_value=0)\nmatrix_np = binary_matrix.to_numpy()\nentry_ids = binary_matrix.index.to_numpy()\n\nprint(\"Shape:\", matrix_np.shape)   # (số EntryID, 1000)\nprint(\"EntryIDs:\", entry_ids[:10])\nprint(\"Matrix sample:\\n\", matrix_np[:5, :100])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T18:04:11.237219Z","iopub.execute_input":"2025-11-15T18:04:11.237585Z","iopub.status.idle":"2025-11-15T18:04:12.741581Z","shell.execute_reply.started":"2025-11-15T18:04:11.237559Z","shell.execute_reply":"2025-11-15T18:04:12.740339Z"}},"outputs":[],"execution_count":null}]}