{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1c325f87", "metadata": { "metadata": {} }, "outputs": [], "source": [ "# %pip install numpy pandas xgboost lightgbm scikit-learn openpyxl" ] }, { "cell_type": "code", "execution_count": 2, "id": "45696607", "metadata": { "metadata": {} }, "outputs": [], "source": [ "# %pip install cupy-cuda12x" ] }, { "cell_type": "code", "execution_count": 3, "id": "20398dff", "metadata": { "metadata": {} }, "outputs": [], "source": [ "# # MLP\n", "# %pip install torch torchvision torchaudio" ] }, { "cell_type": "code", "execution_count": 4, "id": "953471cc", "metadata": {}, "outputs": [], "source": [ "import os\n", "os.chdir('/Users/yuesir/Documents/PostGraduation/论文/AMVL/code')" ] }, { "cell_type": "code", "execution_count": 5, "id": "48c47e6a-0d56-4d22-a239-1a958fe436de", "metadata": { "metadata": {} }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from utils.utils import load_dataset\n", "from utils.workflow import train_eval, case_study, benchmark_all_cases\n", "\n", "from function.ITRPCA import ITRPCA_F\n", "from function.VDA_GKSBMF import A_VDA_GMSBMF\n", "from function.DRPADC import WeightImputeLogFactorization, GetProbability\n", "from function.MVL import MVL_F\n", "from function.BMC import BMC_F\n", "from function.MSBMF import MSBMF_F\n", "from function.HGIMC import fBMC, fGRB, fHGI\n", "from function.AMVL import AMVL" ] }, { "cell_type": "markdown", "id": "fbec543e-9da1-44bb-aa82-d71fb2b793b3", "metadata": {}, "source": [ "## HN-DREP Benchmark" ] }, { "cell_type": "code", "execution_count": 6, "id": "e5f89f0a-7c1e-4b5d-9f59-e6ec96c27902", "metadata": { "metadata": {} }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iddatasetaucauprf1time(s)memory(kb)overall(auc+aupr+f1)
0HINGRLFdataset0.94320.95150.878918.0655120.02.7736
1DRPADCiDrug0.97460.97310.70001177.01706124.02.6477
2VDA-GKSBMFYdataset0.96990.97510.6981111.0979672.02.6431
3MLMCCdataset0.97060.97470.694557.0889936.02.6398
4ITRPCACdataset0.96540.97300.696018.01328172.02.6344
\n", "
" ], "text/plain": [ " id dataset auc aupr f1 time(s) memory(kb) \\\n", "0 HINGRL Fdataset 0.9432 0.9515 0.8789 18.0 655120.0 \n", "1 DRPADC iDrug 0.9746 0.9731 0.7000 1177.0 1706124.0 \n", "2 VDA-GKSBMF Ydataset 0.9699 0.9751 0.6981 111.0 979672.0 \n", "3 MLMC Cdataset 0.9706 0.9747 0.6945 57.0 889936.0 \n", "4 ITRPCA Cdataset 0.9654 0.9730 0.6960 18.0 1328172.0 \n", "\n", " overall(auc+aupr+f1) \n", "0 2.7736 \n", "1 2.6477 \n", "2 2.6431 \n", "3 2.6398 \n", "4 2.6344 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "benchmark = pd.read_excel('data/Benchmark/evaluation-results.xlsx')\n", "benchmark.head()" ] }, { "cell_type": "markdown", "id": "fbafbde2-95b6-47c4-9413-fa24c0e61cd9", "metadata": {}, "source": [ "### Note: Explanation of High Average F1 Score in the HINGRL Algorithm\n", "\n", "The HINGRL algorithm achieved a relatively high average F1 score (0.8789) on the Fdataset dataset. However, it’s important to note that this high score may be influenced by specific strategies in generating negative samples and the model training methods used. Key points are outlined below:\n", "\n", "1. **Negative Sample Generation Strategy:**\n", " - The algorithm generates an equal number of negative samples by randomly selecting drug-disease pairs that are not labeled as positive in the existing dataset. While this ensures a balanced number of positive and negative samples, the generated negatives may not represent true non-associated pairs—they could be potential positives that are simply undiscovered.\n", " - This simple random sampling method does not account for biological similarities, functional annotations, or other prior knowledge, potentially making the negatives less representative of real-world scenarios. As a result, the model may perform well on the training set but have limited generalization capability in real applications.\n", "\n", "2. **Balanced Data Leading to Inflated F1 Scores:**\n", " - The training set is perfectly balanced with equal positive and negative samples, which makes it easier for the model to achieve high precision and recall, and thus a higher F1 score.\n", " - In real-world applications, negative samples usually vastly outnumber positive ones. This artificially balanced training data does not reflect real-world distributions, causing the F1 score to be overestimated.\n", "\n", "3. **Use of Random Forest for Training:**\n", " - The HINGRL algorithm utilizes a Random Forest model, which performs exceptionally well on balanced datasets by leveraging multiple decision trees to reduce overfitting and improve precision and recall.\n", " - While it achieves high F1 scores on balanced data, its performance may degrade on real, imbalanced datasets where the proportion of negative samples is much higher.\n", "\n", "**Conclusion:**\n", "\n", "The high average F1 score achieved by the HINGRL algorithm is partly due to the simplistic negative sample generation and training on a balanced dataset. While the results appear promising on the current dataset, its generalization to more complex and imbalanced real-world drug-disease prediction scenarios may be limited." ] }, { "cell_type": "markdown", "id": "e2a93bd1-73cf-489d-ad01-5fa517f40f39", "metadata": {}, "source": [ "## Data\n", "- **Fdataset**\n", "- **Cdataset**\n", "- **Ydataset**\n", "\n", "Each of the four algorithms achieved the highest score on the datasets mentioned above." ] }, { "cell_type": "code", "execution_count": 7, "id": "e42eb7b6", "metadata": {}, "outputs": [], "source": [ "datasets = ['Fdataset', 'Cdataset', 'Ydataset']" ] }, { "cell_type": "code", "execution_count": 8, "id": "e5b2db77-16a5-4c17-96c6-854063ff8177", "metadata": { "metadata": {} }, "outputs": [], "source": [ "# Test\n", "dataset_name = 'Fdataset'\n", "(drug_name, disease_name, \n", " Wrd, \n", " Wrr_eight, \n", " Wrr_seven_llms_kgs, Wrr_seven_llms_geps, Wrr_seven_kgs_geps, \n", " Wrr_six_geps, Wrr_six_kgs, Wrr_six_llms, \n", " Wrr_five, \n", " Wdd_three, \n", " Wdd_two, \n", " Trr_eight, \n", " Trr_seven_llms_kgs, Trr_seven_llms_geps, Trr_seven_kgs_geps, \n", " Trr_six_geps, Trr_six_kgs, Trr_six_llms, Trr_five, \n", " Tdd_three, \n", " Tdd_two, \n", " drug_embeddings, disease_embeddings) = load_dataset(dataset_name, embedding_type='llm')" ] }, { "cell_type": "code", "execution_count": 9, "id": "a2a7d229", "metadata": { "metadata": {} }, "outputs": [ { "data": { "text/plain": [ "(array(['DB00007', 'DB00010', 'DB00014'], dtype=' thresholdBMC)\n", " SR_MC, SD_MC, F_MC = MVL_F(Wrr_ML, Wdd_ML, Wdr0.T, 0.1, 0.1)\n", "\n", " return np.maximum(F, F_MC)" ] }, { "cell_type": "code", "execution_count": null, "id": "2f384a40", "metadata": {}, "outputs": [], "source": [ "mlmc_benchmark = benchmark_all_cases(MLMC, datasets)\n", "mlmc_benchmark" ] }, { "cell_type": "markdown", "id": "7991a2dd", "metadata": {}, "source": [ "## MSBMF" ] }, { "cell_type": "code", "execution_count": null, "id": "f6751708", "metadata": { "metadata": {} }, "outputs": [], "source": [ "def MSBMF(Wrr_list, Wdd_list, Wrd):\n", " lambda1 = 0.1\n", " lambda2 = 0.01\n", " lambda3 = lambda2\n", " k = int(min(Wrd.shape) * 0.7)\n", " maxiter = 300\n", " tol1 = 2 * 1e-3\n", " tol2 = 1 * 1e-4\n", "\n", " # Wrr = [Wrr1, Wrr2, Wrr3, Wrr4, Wrr5];\n", " Wrr = np.hstack(Wrr_list)\n", " # Wdd = [Wdd1, Wdd2];\n", " Wdd = np.hstack(Wdd_list)\n", "\n", " U, V, iter = MSBMF_F(Wrd.T, Wdd, Wrr, lambda1, lambda2, lambda3, k, tol1, tol2, maxiter);\n", " M_recovery = U @ V.T\n", "\n", " return M_recovery.T" ] }, { "cell_type": "code", "execution_count": null, "id": "6ea00e8c", "metadata": {}, "outputs": [], "source": [ "msbmf_benchmark = benchmark_all_cases(MSBMF, datasets)\n", "msbmf_benchmark" ] }, { "cell_type": "markdown", "id": "2f0a9325-45f4-4d35-b42e-da81c4404431", "metadata": {}, "source": [ "## ITRPCA" ] }, { "cell_type": "code", "execution_count": null, "id": "51aea5c9-fc69-4503-b32a-9403c2010b93", "metadata": { "metadata": {} }, "outputs": [], "source": [ "def ITRPCA(Wrd, Trr, Tdd):\n", " p = 0.9\n", " K = 30\n", " rat1 = 0.1\n", " rat2 = 0.2\n", "\n", " F_ITRPCA = ITRPCA_F(Trr, Tdd, Wrd.T, p, K, rat1, rat2).T\n", "\n", " return F_ITRPCA" ] }, { "cell_type": "code", "execution_count": null, "id": "1b2f7521", "metadata": {}, "outputs": [], "source": [ "itrpca_benchmark = benchmark_all_cases(ITRPCA, datasets, folds=10)\n", "itrpca_benchmark" ] }, { "cell_type": "markdown", "id": "916f2014", "metadata": {}, "source": [ "## HGIMC" ] }, { "cell_type": "code", "execution_count": null, "id": "cd65e866", "metadata": { "metadata": {} }, "outputs": [], "source": [ "def HGIMC(Wrr_list, Wdd_list, Wrd):\n", " A_DR = Wrd.T\n", "\n", " # # Base\n", " # R = Wrr_list[0]\n", " # D = Wdd_list[1]\n", "\n", " # Average\n", " R = np.mean(Wrr_list, axis=0)\n", " D = np.mean(Wdd_list, axis=0)\n", "\n", " alpha = 10\n", " beta = 10\n", " gamma = 0.1\n", " threshold = 0.1\n", " maxiter = 300\n", " tol1 = 2 * 1e-3\n", " tol2 = 1 * 1e-5\n", "\n", " trIndex = (A_DR != 0).astype(float)\n", " A_bmc, iter = fBMC(alpha, beta, A_DR, trIndex, tol1, tol2, maxiter, 0, 1)\n", " A_DR0 = A_bmc * (A_bmc > threshold)\n", "\n", " A_RR = fGRB(R, 0.5)\n", " A_DD = fGRB(D, 0.5)\n", "\n", " A_recovery = fHGI(gamma, A_DD, A_RR, A_DR0)\n", "\n", " return A_recovery.T\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fdc640ec", "metadata": {}, "outputs": [], "source": [ "hgimc_benchmark = benchmark_all_cases(HGIMC, datasets)\n", "hgimc_benchmark" ] }, { "cell_type": "markdown", "id": "d28ded0a", "metadata": {}, "source": [ "## DRPADC" ] }, { "cell_type": "code", "execution_count": null, "id": "fbd88801", "metadata": { "metadata": {} }, "outputs": [], "source": [ "def DRPADC(Wrd, Wrr_list, Wdd_list):\n", " rank = 150\n", " rnseed = 0\n", " lR = 0.1\n", " lM = 1.0\n", " lN = 0.1\n", " num_iter = 550\n", " learn_rate = 0.09\n", "\n", " W = np.maximum(1, Wrd)\n", "\n", " # # Paper Setting\n", " # Wdd = Wdd_list[1] # semantic sim\n", " # Wrr = Wrr_list[0] # chemical sim\n", " \n", " # Benchmark\n", " Wdd = np.mean(Wdd_list, axis=0)\n", " Wrr = np.mean(Wrr_list, axis=0)\n", "\n", " F, G = WeightImputeLogFactorization(Wrd, Wrr, Wdd, W, rank, lR, lM, lN, num_iter, learn_rate, rnseed)\n", " \n", " PROB = GetProbability(np.dot(F, G.T))\n", " \n", " return PROB" ] }, { "cell_type": "code", "execution_count": null, "id": "105aa1dc", "metadata": {}, "outputs": [], "source": [ "drpadc_benchmark = benchmark_all_cases(DRPADC, datasets)\n", "drpadc_benchmark" ] }, { "cell_type": "markdown", "id": "bd88b91b-8a49-4d29-9129-616526e02ec7", "metadata": {}, "source": [ "## VDA-GKSBMF" ] }, { "cell_type": "code", "execution_count": null, "id": "b2eacd15", "metadata": { "metadata": {} }, "outputs": [], "source": [ "def VDA_GKSBMF(Wrd, Wrr_list, Wdd_list):\n", " gm = 0.5\n", " w = 0.3\n", " lambda1 = 1\n", " lambda2 = lambda1\n", " lambda3 = lambda2\n", " tol1 = 2 * 1e-30\n", " tol2 = 2 * 1e-40\n", " maxiter = 400\n", "\n", " k = int(min(Wrd.shape) * 0.7)\n", "\n", " # # Paper Setting\n", " # Wdd = Wdd_list[1]\n", " # Wrr = Wrr_list[0]\n", "\n", " # Benchmark\n", " Wdd = np.mean(Wdd_list, axis=0)\n", " Wrr = np.mean(Wrr_list, axis=0)\n", "\n", " M_recovery = A_VDA_GMSBMF(Wrd.T, Wdd, Wrr, gm, w, lambda1, lambda2, lambda3, k, tol1, tol2, maxiter)\n", " \n", " return M_recovery.T" ] }, { "cell_type": "code", "execution_count": null, "id": "4ee8eb24", "metadata": {}, "outputs": [], "source": [ "vda_gksbmf_benchmark = benchmark_all_cases(VDA_GKSBMF, datasets)\n", "vda_gksbmf_benchmark" ] }, { "cell_type": "markdown", "id": "6537a467", "metadata": {}, "source": [ "## Machine Learning" ] }, { "cell_type": "code", "execution_count": null, "id": "7dfe1e99", "metadata": { "metadata": {} }, "outputs": [], "source": [ "train_eval(\n", " drug_embeddings=drug_embeddings,\n", " disease_embeddings=disease_embeddings,\n", " Wrd=Wrd,\n", " Wrr_list=Wrr,\n", " Wdd_list=Wdd,\n", " Trr=Trr,\n", " Tdd=Tdd,\n", " ml_benchmark=True\n", ")" ] }, { "cell_type": "markdown", "id": "038bf799", "metadata": {}, "source": [ "## New Drug Prediction" ] }, { "cell_type": "code", "execution_count": 14, "id": "c824a7fa", "metadata": {}, "outputs": [], "source": [ "from function.AMVL import predict_new_drug_from_mat" ] }, { "cell_type": "code", "execution_count": 16, "id": "fqtqvlh62ci", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">>> Loading data from: data/new_drug_prediction/Fdataset_new_drug.mat\n", ">>> Using 8 drug similarity matrices: ['drug_AtcS', 'drug_ChemS', 'drug_DDIS', 'drug_GepS', 'drug_KgS', 'drug_LlmS', 'drug_SideS', 'drug_TargetS']\n", ">>> Using 4 disease similarity matrices: ['disease_DoS', 'disease_KgS', 'disease_LlmS', 'disease_PhS']\n", ">>> Dataset dimensions:\n", " Total drugs (with new drug): 594\n", " Known drugs (training): 593\n", " Diseases: 313\n", " Drug similarity matrices: [(594, 594), (594, 594), (594, 594), (594, 594), (594, 594), (594, 594), (594, 594), (594, 594)]\n", " Disease similarity matrices: [(313, 313), (313, 313), (313, 313), (313, 313)]\n", ">>> Predicting associations for 1 new drug with 313 diseases...\n", ">>> Training set: 593 drugs × 313 diseases\n", ">>> Extended drug similarity matrices: 594 × 594\n", ">>> Extended association matrix shape: (594, 313)\n", ">>> Running AMVL algorithm for new drug prediction...\n", ">>> Step 1: Starting BMC matrix completion...\n", ">>> BMC converged in 166 iterations.\n", ">>> Calculating GIP similarity matrices for disease-disease and drug-drug interactions...\n", ">>> Removing diagonal elements to eliminate self-interactions...\n", ">>> Step 2: Performing Matrix Factorization with similarity regularization...\n", ">>> Matrix factorization completed in 256 iterations.\n", ">>> Thresholding the recovered matrix based on the threshold value...\n", ">>> Step 3: Starting multi-view learning...\n", ">>> Performing multi-view learning with 8 drug views and 4 disease views...\n", ">>> Iteration stops at 16 step.\n", ">>> Step 4: Combining results and ensuring values are in [0, 1] range...\n", ">>> AdaMVL training complete. Returning final prediction matrix.\n", ">>> Prediction complete. Shape of predictions: (313,)\n", ">>> Prediction statistics - Min: 0.0000, Max: 0.0016, Mean: 0.0002\n", ">>> Top 10 predicted disease associations:\n", " 1. Disease 86: 0.001636\n", " 2. Disease 142: 0.001593\n", " 3. Disease 102: 0.001567\n", " 4. Disease 241: 0.001545\n", " 5. Disease 3: 0.001509\n", " 6. Disease 284: 0.001483\n", " 7. Disease 211: 0.001455\n", " 8. Disease 154: 0.001373\n", " 9. Disease 12: 0.001364\n", " 10. Disease 23: 0.001288\n" ] } ], "source": [ "predictions, ranked_results, drug_names, disease_names = predict_new_drug_from_mat(\n", " mat_file_path='data/new_drug_prediction/Fdataset_new_drug.mat',\n", " use_cupy=False # 如果有 GPU 和 CuPy,可以设为 True 加速\n", ")" ] }, { "cell_type": "code", "execution_count": 22, "id": "058af7ca", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "================================================================================\n", "预测结果总结\n", "================================================================================\n", "新药物: DB_NEW_001\n", "预测的疾病数量: 313\n", "预测概率范围: [0.000001, 0.001636]\n", "预测概率均值: 0.000226\n", "预测概率中位数: 0.000029\n", "\n", "--------------------------------------------------------------------------------\n", "排名 疾病ID 疾病名称 预测概率 \n", "--------------------------------------------------------------------------------\n", "1 D147530 D147530 0.00163630\n", "2 D188050 D188050 0.00159291\n", "3 D157300 D157300 0.00156730\n", "4 D276300 D276300 0.00154464\n", "5 D102500 D102500 0.00150873\n", "6 D604416 D604416 0.00148295\n", "7 D254200 D254200 0.00145530\n", "8 D194200 D194200 0.00137313\n", "9 D109100 D109100 0.00136382\n", "10 D115000 D115000 0.00128793\n" ] } ], "source": [ "top_k = 10\n", "\n", "print(\"\\n\" + \"=\" * 80)\n", "print(\"预测结果总结\")\n", "print(\"=\" * 80)\n", "print(f\"新药物: {drug_names[-1]}\")\n", "print(f\"预测的疾病数量: {len(predictions)}\")\n", "print(f\"预测概率范围: [{predictions.min():.6f}, {predictions.max():.6f}]\")\n", "print(f\"预测概率均值: {predictions.mean():.6f}\")\n", "print(f\"预测概率中位数: {np.median(predictions):.6f}\")\n", "\n", "print(\"\\n\" + \"-\" * 80)\n", "print(f\"{'排名':<6} {'疾病ID':<15} {'疾病名称':<20} {'预测概率':<12}\")\n", "print(\"-\" * 80)\n", "for rank, (idx, prob, disease_name) in enumerate(ranked_results[:top_k], 1):\n", " print(f\"{rank:<6} {disease_name:<15} {disease_name:<20} {prob:.8f}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "236d43cf", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "bio", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }