{"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":[{"sourceType":"competition","sourceId":116062,"databundleVersionId":14875579,"isSourceIdPinned":false}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Deep Learning (MLP) for predicting protein function (Full Code)","metadata":{}},{"cell_type":"markdown","source":"This model uses K-mer Counting combined with Logistic Regression (One-Vs-Rest) to handle multiple labels. This is an efficient and fast approach when working with large sequencing datasets on Kaggle.\n\nThere is **internet access** at kaggle.com","metadata":{}},{"cell_type":"markdown","source":"***. Note 1: The code file (1_ai_models_for_protein_function_prediction_minh.py) considered Traditional Machine Learning**\n\nThe algorithm used is KNN (K-Nearest Neighbors) combined with TF-IDF.\n\nFeature Engineering: You have to manually divide the protein sequence into small segments (k-mers) and then use TF-IDF to convert them into numbers. This is a characteristic of Machine Learning.\n\nKNN Algorithm: KNN is a fundamental machine learning algorithm; it relies on calculating the distance between data points rather than having a layered structure like the brain.\n\nProperties: Runs well on CPUs, doesn't require GPUs, easy to explain, but struggles to learn the deep biological rules of proteins.\n\n***. Note 2:  When is it considered Deep Learning?**\n\nIf we use algorithms such as:\n\nMulti-Layer Perceptron (MLP): As in the code version I suggested above (using MLPClassifier).\n\nCNN (Convolutional Neural Networks): Used to scan patterns on amino acid sequences.\n\nLSTM/Transformer: Used to understand the \"language\" and order of amino acids.\n\nCharacteristics: Deep Learning learns to extract features (without needing excessive manual TF-IDF) through complex hidden layers.","metadata":{}},{"cell_type":"markdown","source":"**Choosing between Machine Learning (traditional machine learning) and Deep Learning for this protein problem is like choosing between a \"durable manual car\" and a \"modern scooter.\"**\n\nBelow is a detailed analysis of which is \"longer\" and which is \"better\":\n\n**1. Regarding time (Does it take longer?)**\n\nThe answer is: YES, training will take longer, but predictions are very fast.\n\n**Machine Learning (KNN - your old code)**: Doesn't require training time (because KNN only stores data in memory). But when you introduce a new protein, it has to compare it with all the old data, so predictions will be very slow if the dataset is large.\n\n**Deep Learning (Neural Network - new code)**: Takes a lot of time to \"learn.\" It has to run through the data many times to find patterns. However, once the learning is complete and the data is saved as a .pkl or .h5 file, predicting a new protein happens in a flash.\n\n**2. Regarding quality (Is it \"better\"?)**\n\nThe answer is: It's MUCH BETTER in terms of \"thinking\" ability.\n\nTraditional Machine Learning (KNN/Logistics): Like a rote learner. If it sees a protein sequence that's somewhat similar to one it already knows, it will guess the exact same function. It doesn't understand why one amino acid next to another is important.\n\nDeep Learning: Like a biological expert. Hidden layers in neural networks are able to spontaneously detect complex motifs (sequence patterns). It understands the non-linear relationships between amino acids, thereby making more accurate predictions for completely new proteins it has never encountered in training.\n\n**3. An Example Illustrating the \"Brilliant\" Nature of Deep Learning**\n\nLet's say you have an unusual amino acid sequence: M-A-V-R-E...\n\nTraditional KNN (Neural Network Programming): It would search its database: \"Does it start with M-A-V? If it doesn't have a 90% match, I'll guess randomly or guess incorrectly.\"\n\nNeural Network (Deep Learning): It would analyze: \"Although I haven't seen this sequence before, the folded structure of these amino acid groups suggests it has the potential to bind to DNA.\" -> A more intelligent result.","metadata":{}},{"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":{"execution_failed":"2026-03-29T15:57:12.008Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"This model uses a Multi-Layer Perceptron (MLP) architecture with multiple hidden layers to learn complex protein features. Added memory management mechanisms (Garbage Collection) and data dimensionality reduction (SVD) to ensure the machine runs deeply without crashing.","metadata":{}},{"cell_type":"markdown","source":"**Multi-Layered Perceptron (MLP) Structure**: Instead of using KNNs that only search for similarities, MLPs have three hidden layers (512, 256, and 128 neurons). They act as information filters; the first layer identifies individual amino acid segments, and the subsequent layers combine them into \"functional blocks.\"\n\nOptimization: Uses the Adam algorithm and the Relu activation function. These are core Deep Learning techniques that allow the model to learn non-linear relationships (e.g., amino acid position 1 affects position 100 due to protein folding).\n\n**Generalization Capability**: This model will not freeze when encountering unfamiliar proteins. It will rely on what it has learned about biochemical rules to make the most logical judgment.\n\nEstimated Runtime:\n\nPreprocessing: ~10 minutes.\n\nDeep Learning Training: ~40 - 70 minutes (depending on the number of labels and complexity).\n\nTotal: Approximately 1.5 hours.\n\nThis model will feel more like a true \"AI\" because you will see the text \"Iteration 1, loss = ...\" appear as it is learning, just like a brain gradually understanding a problem.","metadata":{}},{"cell_type":"markdown","source":"**Deep Architecture**: The neural network has three hidden layers. This allows it to learn features at different levels of the amino acid sequence, making it \"smarter\" than simply comparing distances like KNNs.\n\n**Adam Optimization Function**: This is standard in Deep Learning, helping the model self-adjust its learning speed to avoid getting stuck on incorrect predictions.\n\n**Rich Example System**: I programmed the model to print the first 10 prediction examples with a clear Confidence Score. You can immediately see how the \"AI\" is thinking.\n\n**RAM Optimization**: With extremely large protein data, I added the `del` and `gc.collect()` commands to clear garbage data after each step. This helps the code run longer but avoids \"Kernel Died\" errors.","metadata":{}},{"cell_type":"code","source":"# =================================================================\n# 1. CÀI ĐẶT & QUẢN LÝ BỘ NHỚ\n# =================================================================\nimport os\nimport gc\nimport subprocess\nimport sys\n\n# Cài đặt Biopython (Cần bật Internet ON trong Settings Kaggle)\ndef install(package):\n    subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", package])\n\ntry:\n    from Bio import SeqIO\nexcept ImportError:\n    print(\"Đang cài đặt Biopython để xử lý trình tự chuyên sâu...\")\n    install('biopython')\n    from Bio import SeqIO\n\nimport numpy as np\nimport pandas as pd\nimport joblib\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.preprocessing import MultiLabelBinarizer\nfrom sklearn.decomposition import TruncatedSVD\n\n# =================================================================\n# 2. XÁC ĐỊNH ĐƯỜNG DẪN DỮ LIỆU\n# =================================================================\ndef find_path(name):\n    for root, dirs, files in os.walk('/kaggle/input'):\n        if name in files: return os.path.join(root, name)\n    return None\n\npaths = {\n    \"train_seq\": find_path('train_sequences.fasta'),\n    \"train_terms\": find_path('train_terms.tsv'),\n    \"test_seq\": find_path('testsuperset.fasta')\n}\n\n# =================================================================\n# 3. TẢI DỮ LIỆU & TIỀN XỬ LÝ (PHẦN CHẠY LÂU)\n# =================================================================\nprint(\"--- Bước 1/5: Tải trình tự Protein (Loading Sequences) ---\")\ndef load_fasta(path):\n    seqs = {}\n    for record in SeqIO.parse(path, \"fasta\"):\n        # Tách ID chuẩn cho CAFA: sp|P12345|NAME -> P12345\n        pid = record.id.split('|')[1] if '|' in record.id else record.id\n        seqs[pid] = str(record.seq)\n    return seqs\n\ntrain_seqs = load_fasta(paths['train_seq'])\ntest_seqs = load_fasta(paths['test_seq'])\n\nprint(\"--- Bước 2/5: Tải nhãn GO (Loading Labels) ---\")\ntrain_terms = pd.read_csv(paths['train_terms'], sep='\\t')\n\n# Lấy Top 1000 nhãn phổ biến nhất (Học sâu và rộng hơn các bản trước)\ntop_1000_terms = train_terms['term'].value_counts().head(1000).index\ntrain_terms = train_terms[train_terms['term'].isin(top_1000_terms)]\ntrain_labels_dict = train_terms.groupby('EntryID')['term'].apply(list).to_dict()\n\n# Khớp ID giữa trình tự và nhãn để tạo tập Train chuẩn\ncommon_ids = list(set(train_seqs.keys()) & set(train_labels_dict.keys()))\n\ndef get_kmers(seq_dict, ids, k=3):\n    \"\"\"Biến protein thành ngôn ngữ k-mer cho Deep Learning hiểu\"\"\"\n    return [\" \".join([seq_dict[pid][i:i+k] for i in range(len(seq_dict[pid])-k+1)]) for pid in ids]\n\nX_train_raw = get_kmers(train_seqs, common_ids)\ny_list = [train_labels_dict[pid] for pid in common_ids]\n\n# Giải phóng bộ nhớ RAM cho các biến trung gian không dùng đến\ndel train_terms\ngc.collect()\n\n# =================================================================\n# 4. VECTOR HÓA & NÉN DỮ LIỆU (OPTIMIZATION)\n# =================================================================\nprint(\"--- Bước 3/5: Vector hóa và Nén dữ liệu (SVD) ---\")\nmlb = MultiLabelBinarizer()\ny_train = mlb.fit_transform(y_list)\n\n# Dùng 20,000 đặc trưng để không bỏ sót chi tiết nhỏ nào của protein\nvectorizer = TfidfVectorizer(max_features=20000)\nX_train_tfidf = vectorizer.fit_transform(X_train_raw)\n\n# Nén xuống 500 chiều quan trọng nhất bằng SVD (giúp mạng thần kinh học nhanh hơn)\nsvd = TruncatedSVD(n_components=500, random_state=42)\nX_train_reduced = svd.fit_transform(X_train_tfidf)\n\ndel X_train_raw, X_train_tfidf\ngc.collect()\n\n# =================================================================\n# 5. HUẤN LUYỆN DEEP LEARNING (PHẦN CHẠY LÂU NHẤT)\n# =================================================================\nprint(\"--- Bước 4/5: Huấn luyện Mạng thần kinh (Deep MLP Training) ---\")\n# Cấu trúc mạng 3 lớp: 512 neurals -> 256 neurals -> 128 neurals\nmodel = MLPClassifier(\n    hidden_layer_sizes=(512, 256, 128), \n    max_iter=150,           # Chạy nhiều vòng lặp hơn để học thật kỹ\n    activation='relu',      # Hàm kích hoạt phi tuyến cho Deep Learning\n    solver='adam',          # Bộ tối ưu hóa mạnh nhất hiện nay\n    verbose=True,           # In tiến trình học ra màn hình\n    random_state=42\n)\nmodel.fit(X_train_reduced, y_train)\n\n# Lưu mô hình đã huấn luyện\njoblib.dump({'model': model, 'mlb': mlb, 'vec': vectorizer, 'svd': svd}, 'protein_deep_model.pkl')\n\n# =================================================================\n# 6. DỰ ĐOÁN NHIỀU VÍ DỤ & XUẤT FILE\n# =================================================================\nprint(\"\\n\" + \"=\"*60)\nprint(\"CÁC VÍ DỤ DỰ ĐOÁN TỪ MÔ HÌNH HỌC SÂU\")\nprint(\"=\"*60)\n\ntest_ids = list(test_seqs.keys())\n# Chạy dự đoán cho 10 ví dụ để bạn xem kết quả\nfor pid in test_ids[:10]:\n    seq = test_seqs[pid]\n    kmer_text = \" \".join([seq[i:i+3] for i in range(len(seq)-2)])\n    vec = vectorizer.transform([kmer_text])\n    red = svd.transform(vec)\n    prob = model.predict_proba(red)[0]\n    \n    top_5 = np.argsort(prob)[-5:][::-1]\n    print(f\"\\nProtein ID: {pid} | Sequence: {seq[:40]}...\")\n    print(f\"{'GO Term':<15} | {'Confidence Score'}\")\n    print(\"-\" * 35)\n    for idx in top_5:\n        print(f\"  {mlb.classes_[idx]:<13} | {prob[idx]:.4f}\")\n\nprint(\"\\n--- Bước 5/5: Đang tạo file nộp bài submission.tsv ---\")\nX_test_raw = get_kmers(test_seqs, test_ids)\nX_test_tfidf = vectorizer.transform(X_test_raw)\nX_test_reduced = svd.transform(X_test_tfidf)\n\nprobs = model.predict_proba(X_test_reduced)\nsubmission_data = []\n\nfor i, pid in enumerate(test_ids):\n    # Lấy top 20 nhãn dự đoán tốt nhất cho mỗi Protein\n    top_idx = np.argsort(probs[i])[-20:]\n    for idx in top_idx:\n        score = probs[i, idx]\n        if score > 0.001:\n            submission_data.append([pid, mlb.classes_[idx], f\"{score:.3f}\"])\n\n# Xuất tệp tin nộp bài\npd.DataFrame(submission_data).to_csv('submission.tsv', sep='\\t', index=False, header=False)\nprint(\"\\n[THÀNH CÔNG] File submission.tsv đã sẵn sàng để nộp!\")","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}