{"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":"gpu","dataSources":[{"sourceType":"competition","sourceId":41875,"databundleVersionId":5521661}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-02-23T21:29:30.414324Z","iopub.execute_input":"2026-02-23T21:29:30.415039Z","iopub.status.idle":"2026-02-23T21:29:30.422691Z","shell.execute_reply.started":"2026-02-23T21:29:30.415008Z","shell.execute_reply":"2026-02-23T21:29:30.422005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# قراءة الـ Labels\ntrain_terms = pd.read_csv('/kaggle/input/competitions/cafa-5-protein-function-prediction/Train/train_terms.tsv', sep='\\t')\nprint(train_terms.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:31:24.105160Z","iopub.execute_input":"2026-02-23T21:31:24.105463Z","iopub.status.idle":"2026-02-23T21:31:26.317859Z","shell.execute_reply.started":"2026-02-23T21:31:24.105435Z","shell.execute_reply":"2026-02-23T21:31:26.317190Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# قراءة ملف الـ IA\nia_data = pd.read_csv('/kaggle/input/competitions/cafa-5-protein-function-prediction/IA.txt', sep='\\t', names=['term', 'ia'])\n\n# دمجها مع بيانات التدريب لرؤية قيمة كل وظيفة\ntrain_with_ia = train_terms.merge(ia_data, on='term', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:32:37.772100Z","iopub.execute_input":"2026-02-23T21:32:37.772742Z","iopub.status.idle":"2026-02-23T21:32:38.757884Z","shell.execute_reply.started":"2026-02-23T21:32:37.772712Z","shell.execute_reply":"2026-02-23T21:32:38.757331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Count how many GO terms each protein is associated with\nprotein_counts = train_terms.groupby('EntryID')['term'].count()\n\nprint(f\"Average number of functions per protein: {protein_counts.mean():.2f}\")\nprint(f\"Maximum functions for one protein: {protein_counts.max()}\")\n\n# Visualize the distribution\nimport matplotlib.pyplot as plt\nprotein_counts.hist(bins=50, color='skyblue', edgecolor='black')\nplt.title('Distribution of Number of Functions per Protein')\nplt.xlabel('Number of GO Terms')\nplt.ylabel('Number of Proteins')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:36:04.235933Z","iopub.execute_input":"2026-02-23T21:36:04.236664Z","iopub.status.idle":"2026-02-23T21:36:05.297401Z","shell.execute_reply.started":"2026-02-23T21:36:04.236637Z","shell.execute_reply":"2026-02-23T21:36:05.296790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Merge train_terms with ia_data to see the weights of our training labels\ntrain_with_ia = train_terms.merge(ia_data, on='term', how='left')\n\n# Show the 10 most specific (highest IA) functions in our training set\nprint(\"Top 10 most specific functions (Highest IA):\")\nprint(train_with_ia.sort_values(by='ia', ascending=False).head(10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:36:23.577558Z","iopub.execute_input":"2026-02-23T21:36:23.578259Z","iopub.status.idle":"2026-02-23T21:36:26.577186Z","shell.execute_reply.started":"2026-02-23T21:36:23.578234Z","shell.execute_reply":"2026-02-23T21:36:26.576607Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Count functions by Aspect\naspect_counts = train_terms['aspect'].value_counts()\nprint(\"Distribution of GO Aspects:\")\nprint(aspect_counts)\n\naspect_counts.plot(kind='pie', autopct='%1.1f%%', colors=['#ff9999','#66b3ff','#99ff99'])\nplt.title('Percentage of Each GO Aspect')\nplt.ylabel('')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:37:46.501705Z","iopub.execute_input":"2026-02-23T21:37:46.502432Z","iopub.status.idle":"2026-02-23T21:37:46.926932Z","shell.execute_reply.started":"2026-02-23T21:37:46.502401Z","shell.execute_reply":"2026-02-23T21:37:46.926333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers\n\n# 1. Define the number of features (input_dim)\n# If you are using ProtBERT embeddings, this is usually 1024\n# If you are using ESM2 embeddings, it could be 1280\ninput_dim = 1024  \n\n# 2. Define the number of GO Terms you want to predict\n# For now, let's use a placeholder number like 1500\nnum_labels = 1500 \n\n# 3. Build the Model\nmodel = tf.keras.Sequential([\n    # First layer: needs the input_dim to know the size of the protein data\n    layers.Dense(512, activation='relu', input_shape=(input_dim,)),\n    \n    # Dropout: prevents the model from \"memorizing\" and helps it \"learn\"\n    layers.Dropout(0.3), \n    \n    # Hidden layer\n    layers.Dense(256, activation='relu'),\n    \n    # Output layer: sigmoid is used for multi-label classification\n    layers.Dense(num_labels, activation='sigmoid') \n])\n\n# 4. Compile the model\nmodel.compile(\n    optimizer='adam',\n    loss='binary_crossentropy', # Standard loss for multi-label problems\n    metrics=['accuracy']\n)\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:50:09.899649Z","iopub.execute_input":"2026-02-23T21:50:09.900603Z","iopub.status.idle":"2026-02-23T21:50:12.898837Z","shell.execute_reply.started":"2026-02-23T21:50:09.900539Z","shell.execute_reply":"2026-02-23T21:50:12.898219Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Select the top 1500 most frequent GO terms as our targets\ntop_terms = train_terms['term'].value_counts().head(1500).index\n\n# Filter the training data to include only these terms\ntrain_terms_filtered = train_terms[train_terms['term'].isin(top_terms)]\n\n# Pivot the table so each row is a protein and columns are GO terms (1 or 0)\n# This will be your 'y_train'\ntrain_labels_pivot = train_terms_filtered.pivot_table(index='EntryID', columns='term', aggfunc='size', fill_value=0)\ny_train = train_labels_pivot.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-23T21:50:53.156971Z","iopub.execute_input":"2026-02-23T21:50:53.157674Z","iopub.status.idle":"2026-02-23T21:51:00.189952Z","shell.execute_reply.started":"2026-02-23T21:50:53.157634Z","shell.execute_reply":"2026-02-23T21:51:00.189165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}