{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 数据处理:SMILES信息在Gene里,CP里面没有,所以首先在这里实现映射和对齐" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import os\n", "import h5py\n", "\n", "def load_from_HDF(fname):\n", " \"\"\"Load data from a HDF5 file to a dictionary.\"\"\"\n", " data = dict()\n", " with h5py.File(fname, 'r') as f:\n", " for key in f:\n", " data[key] = np.asarray(f[key])\n", " if isinstance(data[key][0], np.bytes_):\n", " data[key] = data[key].astype(str)\n", " return data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(113416, 896) (68120, 979)\n" ] }, { "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", " \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", " \n", " \n", " \n", " \n", " \n", "
det_plateCPD_SMILES221227_x_at212345_s_at218597_s_at217140_s_at209253_at214404_x_at219888_at201225_s_at...218397_at202996_at204608_at211071_s_at203341_at202801_at206414_s_at204978_at205379_at203897_at
0PAC001_U2OS_6H_X1_B1_UNI4445LCC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C...-0.118505-0.293445-0.2940880.292745-0.1458960.973580.247915-0.159028...-0.5299050.8049920.430269-0.97559-1.7406050.3403820.4987780.3521520.535910.52484
1PAC001_U2OS_6H_X1_B1_UNI4445LIc1cccc(CSc2nnc(o2)-c2ccncc2)c10.043264-0.264945-0.0006330.0009350.177604-0.504720.2143150.144271...0.228895-0.3813080.5569690.25051-0.025625-0.024428-0.2251220.533052-0.19329-0.25436
2PAC001_U2OS_6H_X1_B1_UNI4445LClc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1-0.0708050.1957550.004606-0.040855-0.067346-0.19872-0.0531950.167971...-0.031795-0.051728-0.0177410.056810.1306950.005919-0.408722-0.057177-0.256990.31004
3PAC001_U2OS_6H_X1_B1_UNI4445LCOc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc10.027165-0.1495450.173113-0.100695-0.309296-0.20782-0.3239850.294872...-0.013787-0.1776080.219869-0.161290.365195-0.040848-0.2511220.0407320.17091-0.05144
4PAC001_U2OS_6H_X1_B1_UNI4445LOC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c230.3032940.254455-0.055418-0.0536350.000455-0.109080.0314250.044482...-0.052585-0.1961080.2454690.007840.093595-0.3034180.474878-0.022947-0.285890.06423
\n", "

5 rows × 979 columns

\n", "
" ], "text/plain": [ " det_plate \\\n", "0 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "1 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "2 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "3 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "4 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "\n", " CPD_SMILES 221227_x_at \\\n", "0 CC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C... -0.118505 \n", "1 Ic1cccc(CSc2nnc(o2)-c2ccncc2)c1 0.043264 \n", "2 Clc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1 -0.070805 \n", "3 COc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc1 0.027165 \n", "4 OC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c23 0.303294 \n", "\n", " 212345_s_at 218597_s_at 217140_s_at 209253_at 214404_x_at 219888_at \\\n", "0 -0.293445 -0.294088 0.292745 -0.145896 0.97358 0.247915 \n", "1 -0.264945 -0.000633 0.000935 0.177604 -0.50472 0.214315 \n", "2 0.195755 0.004606 -0.040855 -0.067346 -0.19872 -0.053195 \n", "3 -0.149545 0.173113 -0.100695 -0.309296 -0.20782 -0.323985 \n", "4 0.254455 -0.055418 -0.053635 0.000455 -0.10908 0.031425 \n", "\n", " 201225_s_at ... 218397_at 202996_at 204608_at 211071_s_at 203341_at \\\n", "0 -0.159028 ... -0.529905 0.804992 0.430269 -0.97559 -1.740605 \n", "1 0.144271 ... 0.228895 -0.381308 0.556969 0.25051 -0.025625 \n", "2 0.167971 ... -0.031795 -0.051728 -0.017741 0.05681 0.130695 \n", "3 0.294872 ... -0.013787 -0.177608 0.219869 -0.16129 0.365195 \n", "4 0.044482 ... -0.052585 -0.196108 0.245469 0.00784 0.093595 \n", "\n", " 202801_at 206414_s_at 204978_at 205379_at 203897_at \n", "0 0.340382 0.498778 0.352152 0.53591 0.52484 \n", "1 -0.024428 -0.225122 0.533052 -0.19329 -0.25436 \n", "2 0.005919 -0.408722 -0.057177 -0.25699 0.31004 \n", "3 -0.040848 -0.251122 0.040732 0.17091 -0.05144 \n", "4 -0.303418 0.474878 -0.022947 -0.28589 0.06423 \n", "\n", "[5 rows x 979 columns]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "root = '/home/bob/boom/VCBench/data/MVC/CDRP-BBBC047-Bray/'\n", "\n", "CP_path = root + './CP_data.parquet'\n", "GE_path = root + './GE_data.parquet'\n", "\n", "CP_CSV = pd.read_parquet(CP_path)\n", "GE_CSV = pd.read_parquet(GE_path)\n", "print(CP_CSV.shape, GE_CSV.shape)\n", "GE_CSV.head()" ] }, { "cell_type": "code", "execution_count": 12, "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", " \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", " \n", " \n", " \n", " \n", " \n", "
SMILESMetadata_PlateCells_AreaShape_CompactnessCells_AreaShape_EulerNumberCells_AreaShape_ExtentCells_AreaShape_FormFactorCells_AreaShape_MaxFeretDiameterCells_AreaShape_MaximumRadiusCells_AreaShape_SolidityCells_AreaShape_Zernike_0_0...Nuclei_Texture_SumEntropy_DNA_5_0Nuclei_Texture_SumEntropy_ER_3_0Nuclei_Texture_SumEntropy_Mito_3_0Nuclei_Texture_SumEntropy_RNA_5_0Nuclei_Texture_SumVariance_ER_3_0Nuclei_Texture_SumVariance_Mito_5_0Nuclei_Texture_SumVariance_RNA_10_0Nuclei_Texture_Variance_AGP_5_0Nuclei_Texture_Variance_ER_5_0Nuclei_Texture_Variance_RNA_5_0
0CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC242770.256710NaN-0.114990-0.2370900.6155940.5504340.072795-0.210908...0.0900210.375747-0.0104270.118997-0.156442-0.018014-0.024573-0.383890-0.199967-0.075658
1COc1cc(C)cc(OC)c1[C@@H]1C=C(C)CC[C@H]1C(C)=C242770.050962NaN0.051053-0.0337330.2719530.1727140.145941-0.039057...-0.0303380.390687-0.0585600.065822-0.113721-0.089807-0.041392-0.248967-0.1261830.018563
2COc1cc(O)cc(\\C=C\\c2ccccc2)c124277-0.098185NaN0.0442120.1646680.0119850.1270980.0947360.078257...0.1029460.2791480.0298910.079777-0.0079790.081155-0.0896330.043140-0.0343550.052914
3COc1c(O)cc2C(=O)O[C@H]3[C@@H](O)[C@H](O)[C@@H]...242770.410722NaN-0.236208-0.0550840.6946480.5167850.154239-0.347572...0.4705160.2147920.0154080.339100-0.034251-0.0475970.103611-0.139579-0.0422410.249977
4CCOc1ccc2ccccc2c1C(=O)N[C@H]1[C@H]2SC(C)(C)[C@...242770.248559NaN-0.0200580.0857720.6787420.6718970.214587-0.221396...0.6717900.4241860.1188730.4156230.2575980.1933700.238471-0.0337970.0800180.335890
\n", "

5 rows × 896 columns

\n", "
" ], "text/plain": [ " SMILES Metadata_Plate \\\n", "0 CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC 24277 \n", "1 COc1cc(C)cc(OC)c1[C@@H]1C=C(C)CC[C@H]1C(C)=C 24277 \n", "2 COc1cc(O)cc(\\C=C\\c2ccccc2)c1 24277 \n", "3 COc1c(O)cc2C(=O)O[C@H]3[C@@H](O)[C@H](O)[C@@H]... 24277 \n", "4 CCOc1ccc2ccccc2c1C(=O)N[C@H]1[C@H]2SC(C)(C)[C@... 24277 \n", "\n", " Cells_AreaShape_Compactness Cells_AreaShape_EulerNumber \\\n", "0 0.256710 NaN \n", "1 0.050962 NaN \n", "2 -0.098185 NaN \n", "3 0.410722 NaN \n", "4 0.248559 NaN \n", "\n", " Cells_AreaShape_Extent Cells_AreaShape_FormFactor \\\n", "0 -0.114990 -0.237090 \n", "1 0.051053 -0.033733 \n", "2 0.044212 0.164668 \n", "3 -0.236208 -0.055084 \n", "4 -0.020058 0.085772 \n", "\n", " Cells_AreaShape_MaxFeretDiameter Cells_AreaShape_MaximumRadius \\\n", "0 0.615594 0.550434 \n", "1 0.271953 0.172714 \n", "2 0.011985 0.127098 \n", "3 0.694648 0.516785 \n", "4 0.678742 0.671897 \n", "\n", " Cells_AreaShape_Solidity Cells_AreaShape_Zernike_0_0 ... \\\n", "0 0.072795 -0.210908 ... \n", "1 0.145941 -0.039057 ... \n", "2 0.094736 0.078257 ... \n", "3 0.154239 -0.347572 ... \n", "4 0.214587 -0.221396 ... \n", "\n", " Nuclei_Texture_SumEntropy_DNA_5_0 Nuclei_Texture_SumEntropy_ER_3_0 \\\n", "0 0.090021 0.375747 \n", "1 -0.030338 0.390687 \n", "2 0.102946 0.279148 \n", "3 0.470516 0.214792 \n", "4 0.671790 0.424186 \n", "\n", " Nuclei_Texture_SumEntropy_Mito_3_0 Nuclei_Texture_SumEntropy_RNA_5_0 \\\n", "0 -0.010427 0.118997 \n", "1 -0.058560 0.065822 \n", "2 0.029891 0.079777 \n", "3 0.015408 0.339100 \n", "4 0.118873 0.415623 \n", "\n", " Nuclei_Texture_SumVariance_ER_3_0 Nuclei_Texture_SumVariance_Mito_5_0 \\\n", "0 -0.156442 -0.018014 \n", "1 -0.113721 -0.089807 \n", "2 -0.007979 0.081155 \n", "3 -0.034251 -0.047597 \n", "4 0.257598 0.193370 \n", "\n", " Nuclei_Texture_SumVariance_RNA_10_0 Nuclei_Texture_Variance_AGP_5_0 \\\n", "0 -0.024573 -0.383890 \n", "1 -0.041392 -0.248967 \n", "2 -0.089633 0.043140 \n", "3 0.103611 -0.139579 \n", "4 0.238471 -0.033797 \n", "\n", " Nuclei_Texture_Variance_ER_5_0 Nuclei_Texture_Variance_RNA_5_0 \n", "0 -0.199967 -0.075658 \n", "1 -0.126183 0.018563 \n", "2 -0.034355 0.052914 \n", "3 -0.042241 0.249977 \n", "4 0.080018 0.335890 \n", "\n", "[5 rows x 896 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "CP_CSV.head()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((20342,), (21782,))" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "CP_CSV['SMILES'].unique().shape, GE_CSV['CPD_SMILES'].unique().shape # 包含 NaN的" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((20342,), (21782,))" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 填充nan为 DMSO\n", "CP_CSV['SMILES'] = CP_CSV['SMILES'].fillna('DMSO')\n", "GE_CSV['CPD_SMILES'] = GE_CSV['CPD_SMILES'].fillna('DMSO')\n", "\n", "CP_CSV['SMILES'].unique().shape, GE_CSV['CPD_SMILES'].unique().shape # 包含 NaN的" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 确保这两个CSV包含相同的SMILES" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "交集数量: 20342\n", "示例交集: ['Cc1ccccc1S(=O)(=O)N1C[C@H](O)COC[C@@H]2O[C@H](CC(=O)NCc3ccccc3Cl)CC[C@@H]12', 'CCCC#Cc1cnc2O[C@@H](CN(C)S(=O)(=O)c3cn(C)cn3)[C@H](C)CN([C@@H](C)CO)C(=O)c2c1', 'O[C@H]1COC[C@H]2O[C@@H](CC(=O)N3CCCCC3)CC[C@@H]2N(Cc2ccc3OCOc3c2)C1', 'CCC(=O)N1C[C@H](C)[C@H](CN(C)C(=O)c2cc(NC(=O)C3CC3)ccc2OC[C@H]1C)OC', 'Brc1ccc2NC(=O)C3(NNc4ccccc4O3)c2c1', 'CNC[C@@H]1OCCCC[C@@H](C)Oc2ccc(NS(C)(=O)=O)cc2C(=O)N(C[C@@H]1C)[C@H](C)CO', 'OC[C@H]1O[C@H](CC(=O)N2CCc3ccccc3C2)C[C@H]2[C@@H]1Oc1ccc(NS(=O)(=O)c3ccc(F)cc3)cc21', 'C[C@@H](CO)N1C[C@H](C)[C@@H](CN(C)C(=O)Nc2cccc3ccccc23)Oc2ccc(cc2C1=O)N(C)C', 'CN(C)Cc1c(nnn1-c1nonc1N)C(=O)NN=Cc1ccccc1OCc1ccc(F)cc1', 'CCC(C)C(OC(C)=O)C(=O)O[C@H]1[C@H](OC=O)C(C(=C)C2(O)C(=O)C[C@@H](c3ccoc3)[C@]12C)[C@@]1(C)[C@H](CC(=O)O[C@](C)(COC(C)=O)C1CC(=O)OC)OC(C)=O']\n" ] } ], "source": [ "# 转换成集合\n", "set_cp = set(CP_CSV['SMILES'].unique())\n", "set_ge = set(GE_CSV['CPD_SMILES'].dropna().unique())\n", "\n", "# 求交集\n", "common_smiles = set_cp & set_ge\n", "\n", "print(f\"交集数量: {len(common_smiles)}\")\n", "print(\"示例交集:\", list(common_smiles)[:10]) # 只看前10个" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(61313, 979) (113416, 896)\n" ] } ], "source": [ "# 删除Gene中不在交集的行\n", "GE_CSV_matched = GE_CSV[GE_CSV['CPD_SMILES'].isin(common_smiles)].copy()\n", "# 删除CellPainting中不在交集的行\n", "CP_CSV_matched = CP_CSV[CP_CSV['SMILES'].isin(common_smiles)].copy()\n", "print(GE_CSV_matched.shape, CP_CSV_matched.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 数据对齐\n", "\n", "### 对齐逻辑:数据清洗,各自生成对齐数据即可" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DataFrame 中存在 NaN 值\n", "包含 NaN 的列: 193\n", "包含 NaN 的行:\n", " 113416\n", "整个 DataFrame 中 NaN 的总数: 10578278\n" ] } ], "source": [ "# 检查整个 DataFrame 中是否存在 NaN\n", "if CP_CSV_matched.isna().any().any():\n", " print(\"DataFrame 中存在 NaN 值\")\n", "else:\n", " print(\"DataFrame 中不存在 NaN 值\")\n", "\n", "# 检查每一列是否存在 NaN\n", "nan_columns = CP_CSV_matched.columns[CP_CSV_matched.isna().any()].tolist()\n", "print(\"包含 NaN 的列:\", len(nan_columns))\n", "\n", "# 检查每一行是否存在 NaN\n", "nan_rows = CP_CSV_matched[CP_CSV_matched.isna().any(axis=1)]\n", "print(\"包含 NaN 的行:\\n\", len(nan_rows))\n", "\n", "\n", "# 检查整个 DataFrame 中 NaN 的总数\n", "total_nans = CP_CSV_matched.isna().sum().sum()\n", "print(f\"整个 DataFrame 中 NaN 的总数: {total_nans}\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "清洗后 DataFrame 形状: (113416, 747)\n", "NaN 剩余数量: 158163\n" ] } ], "source": [ "import pandas as pd\n", "\n", "# 假设 CP_CSV_matched 已经加载好了\n", "df = CP_CSV_matched.copy()\n", "\n", "# 1. 删除 NaN 占比 > 20% 的列\n", "col_thresh = 0.1\n", "row_thresh = 0.2\n", "\n", "# 列筛选\n", "valid_cols = df.columns[df.isnull().mean() <= col_thresh]\n", "\n", "# 行筛选\n", "valid_rows = df.index[df.isnull().mean(axis=1) <= row_thresh]\n", "\n", "# 保留行和列\n", "df = df.loc[valid_rows, valid_cols]\n", "\n", "print(f\"清洗后 DataFrame 形状: {df.shape}\")\n", "print(\"NaN 剩余数量:\", df.isnull().sum().sum())" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(113416, 747) (113416, 896)\n" ] } ], "source": [ "print(df.shape, CP_CSV_matched.shape)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NaN 剩余数量: 158163\n" ] } ], "source": [ "print(\"NaN 剩余数量:\", df.isnull().sum().sum())" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "清洗后 DataFrame 形状: (113416, 747)\n", "NaN 剩余数量 (第一轮清洗后): 158163\n", "DataFrame 最终填充完成。\n", "NaN 剩余数量 (最终): 0\n", "最终 DataFrame 形状: (113416, 747)\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# 你的初始数据清洗步骤\n", "# 假设 CP_CSV_matched 已经加载好了\n", "df = CP_CSV_matched.copy()\n", "\n", "# 1. 删除 NaN 占比 > 20% 的列和行\n", "col_thresh = 0.1\n", "row_thresh = 0.2\n", "\n", "# 列筛选\n", "valid_cols = df.columns[df.isnull().mean() <= col_thresh]\n", "df = df[valid_cols]\n", "\n", "# 行筛选\n", "valid_rows = df.index[df.isnull().mean(axis=1) <= row_thresh]\n", "df = df.loc[valid_rows]\n", "\n", "print(f\"清洗后 DataFrame 形状: {df.shape}\")\n", "print(\"NaN 剩余数量 (第一轮清洗后):\", df.isnull().sum().sum())\n", "\n", "# --- 最终填充方案 ---\n", "\n", "# 2. 强制转换所有列为数值类型\n", "# `errors='coerce'` 会将任何非数值数据(如空字符串、'-')强制转换为 NaN\n", "# 这一步是关键,它将所有需要填充的数据统一为 NaN\n", "# 我们需要保留 SMILES 列,因为它不是数值\n", "smiles_col = 'SMILES' # 假设 SMILES 列名为 canonical_smiles\n", "if smiles_col in df.columns:\n", " df_numeric = df.drop(columns=[smiles_col]).apply(pd.to_numeric, errors='coerce')\n", " df_numeric[smiles_col] = df[smiles_col]\n", "else:\n", " df_numeric = df.apply(pd.to_numeric, errors='coerce')\n", "\n", "# 3. 用中位数填充剩余的 NaN\n", "# 中位数对异常值不敏感,是填充数据的好选择\n", "# fillna() 会用 Series 的中位数来填充其 NaN 值\n", "# .median() 会自动跳过 NaN 值进行计算\n", "df_filled = df_numeric.fillna(df_numeric.median(numeric_only=True))\n", "\n", "# 4. 再次检查,确保没有剩余的 NaN\n", "# 如果有某一列在转换后全变成了 NaN(比如该列原本都是非数值),那么它的中位数也会是 NaN\n", "# 我们可以将这些剩余的 NaN 填充为 0\n", "df_filled = df_filled.fillna(0)\n", "\n", "print(\"DataFrame 最终填充完成。\")\n", "print(\"NaN 剩余数量 (最终):\", df_filled.isnull().sum().sum())\n", "print(\"最终 DataFrame 形状:\", df_filled.shape)" ] }, { "cell_type": "code", "execution_count": 71, "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", " \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", " \n", " \n", " \n", " \n", " \n", "
Metadata_PlateCells_AreaShape_CompactnessCells_AreaShape_ExtentCells_AreaShape_FormFactorCells_AreaShape_MaxFeretDiameterCells_AreaShape_MaximumRadiusCells_AreaShape_SolidityCells_AreaShape_Zernike_0_0Cells_AreaShape_Zernike_1_1Cells_AreaShape_Zernike_2_0...Nuclei_Texture_SumEntropy_ER_3_0Nuclei_Texture_SumEntropy_Mito_3_0Nuclei_Texture_SumEntropy_RNA_5_0Nuclei_Texture_SumVariance_ER_3_0Nuclei_Texture_SumVariance_Mito_5_0Nuclei_Texture_SumVariance_RNA_10_0Nuclei_Texture_Variance_AGP_5_0Nuclei_Texture_Variance_ER_5_0Nuclei_Texture_Variance_RNA_5_0SMILES
0242770.256710-0.114990-0.2370900.6155940.5504340.072795-0.2109080.008488-0.228257...0.375747-0.0104270.118997-0.156442-0.018014-0.024573-0.383890-0.199967-0.075658CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC
1242770.0509620.051053-0.0337330.2719530.1727140.145941-0.039057-0.0708260.050313...0.390687-0.0585600.065822-0.113721-0.089807-0.041392-0.248967-0.1261830.018563COc1cc(C)cc(OC)c1[C@@H]1C=C(C)CC[C@H]1C(C)=C
224277-0.0981850.0442120.1646680.0119850.1270980.0947360.0782570.119333-0.033444...0.2791480.0298910.079777-0.0079790.081155-0.0896330.043140-0.0343550.052914COc1cc(O)cc(\\C=C\\c2ccccc2)c1
3242770.410722-0.236208-0.0550840.6946480.5167850.154239-0.347572-0.172400-0.215598...0.2147920.0154080.339100-0.034251-0.0475970.103611-0.139579-0.0422410.249977COc1c(O)cc2C(=O)O[C@H]3[C@@H](O)[C@H](O)[C@@H]...
4242770.248559-0.0200580.0857720.6787420.6718970.214587-0.221396-0.086168-0.090049...0.4241860.1188730.4156230.2575980.1933700.238471-0.0337970.0800180.335890CCOc1ccc2ccccc2c1C(=O)N[C@H]1[C@H]2SC(C)(C)[C@...
\n", "

5 rows × 747 columns

\n", "
" ], "text/plain": [ " Metadata_Plate Cells_AreaShape_Compactness Cells_AreaShape_Extent \\\n", "0 24277 0.256710 -0.114990 \n", "1 24277 0.050962 0.051053 \n", "2 24277 -0.098185 0.044212 \n", "3 24277 0.410722 -0.236208 \n", "4 24277 0.248559 -0.020058 \n", "\n", " Cells_AreaShape_FormFactor Cells_AreaShape_MaxFeretDiameter \\\n", "0 -0.237090 0.615594 \n", "1 -0.033733 0.271953 \n", "2 0.164668 0.011985 \n", "3 -0.055084 0.694648 \n", "4 0.085772 0.678742 \n", "\n", " Cells_AreaShape_MaximumRadius Cells_AreaShape_Solidity \\\n", "0 0.550434 0.072795 \n", "1 0.172714 0.145941 \n", "2 0.127098 0.094736 \n", "3 0.516785 0.154239 \n", "4 0.671897 0.214587 \n", "\n", " Cells_AreaShape_Zernike_0_0 Cells_AreaShape_Zernike_1_1 \\\n", "0 -0.210908 0.008488 \n", "1 -0.039057 -0.070826 \n", "2 0.078257 0.119333 \n", "3 -0.347572 -0.172400 \n", "4 -0.221396 -0.086168 \n", "\n", " Cells_AreaShape_Zernike_2_0 ... Nuclei_Texture_SumEntropy_ER_3_0 \\\n", "0 -0.228257 ... 0.375747 \n", "1 0.050313 ... 0.390687 \n", "2 -0.033444 ... 0.279148 \n", "3 -0.215598 ... 0.214792 \n", "4 -0.090049 ... 0.424186 \n", "\n", " Nuclei_Texture_SumEntropy_Mito_3_0 Nuclei_Texture_SumEntropy_RNA_5_0 \\\n", "0 -0.010427 0.118997 \n", "1 -0.058560 0.065822 \n", "2 0.029891 0.079777 \n", "3 0.015408 0.339100 \n", "4 0.118873 0.415623 \n", "\n", " Nuclei_Texture_SumVariance_ER_3_0 Nuclei_Texture_SumVariance_Mito_5_0 \\\n", "0 -0.156442 -0.018014 \n", "1 -0.113721 -0.089807 \n", "2 -0.007979 0.081155 \n", "3 -0.034251 -0.047597 \n", "4 0.257598 0.193370 \n", "\n", " Nuclei_Texture_SumVariance_RNA_10_0 Nuclei_Texture_Variance_AGP_5_0 \\\n", "0 -0.024573 -0.383890 \n", "1 -0.041392 -0.248967 \n", "2 -0.089633 0.043140 \n", "3 0.103611 -0.139579 \n", "4 0.238471 -0.033797 \n", "\n", " Nuclei_Texture_Variance_ER_5_0 Nuclei_Texture_Variance_RNA_5_0 \\\n", "0 -0.199967 -0.075658 \n", "1 -0.126183 0.018563 \n", "2 -0.034355 0.052914 \n", "3 -0.042241 0.249977 \n", "4 0.080018 0.335890 \n", "\n", " SMILES \n", "0 CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC \n", "1 COc1cc(C)cc(OC)c1[C@@H]1C=C(C)CC[C@H]1C(C)=C \n", "2 COc1cc(O)cc(\\C=C\\c2ccccc2)c1 \n", "3 COc1c(O)cc2C(=O)O[C@H]3[C@@H](O)[C@H](O)[C@@H]... \n", "4 CCOc1ccc2ccccc2c1C(=O)N[C@H]1[C@H]2SC(C)(C)[C@... \n", "\n", "[5 rows x 747 columns]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_filled.head()" ] }, { "cell_type": "code", "execution_count": 20, "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", " \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", " \n", " \n", " \n", " \n", " \n", "
SMILESMetadata_PlateCells_AreaShape_CompactnessCells_AreaShape_ExtentCells_AreaShape_FormFactorCells_AreaShape_MaxFeretDiameterCells_AreaShape_MaximumRadiusCells_AreaShape_SolidityCells_AreaShape_Zernike_0_0Cells_AreaShape_Zernike_1_1...Nuclei_Texture_SumEntropy_DNA_5_0Nuclei_Texture_SumEntropy_ER_3_0Nuclei_Texture_SumEntropy_Mito_3_0Nuclei_Texture_SumEntropy_RNA_5_0Nuclei_Texture_SumVariance_ER_3_0Nuclei_Texture_SumVariance_Mito_5_0Nuclei_Texture_SumVariance_RNA_10_0Nuclei_Texture_Variance_AGP_5_0Nuclei_Texture_Variance_ER_5_0Nuclei_Texture_Variance_RNA_5_0
0CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC242770.256710-0.114990-0.2370900.6155940.5504340.072795-0.2109080.008488...0.0900210.375747-0.0104270.118997-0.156442-0.018014-0.024573-0.383890-0.199967-0.075658
1COc1cc(C)cc(OC)c1[C@@H]1C=C(C)CC[C@H]1C(C)=C242770.0509620.051053-0.0337330.2719530.1727140.145941-0.039057-0.070826...-0.0303380.390687-0.0585600.065822-0.113721-0.089807-0.041392-0.248967-0.1261830.018563
2COc1cc(O)cc(\\C=C\\c2ccccc2)c124277-0.0981850.0442120.1646680.0119850.1270980.0947360.0782570.119333...0.1029460.2791480.0298910.079777-0.0079790.081155-0.0896330.043140-0.0343550.052914
3COc1c(O)cc2C(=O)O[C@H]3[C@@H](O)[C@H](O)[C@@H]...242770.410722-0.236208-0.0550840.6946480.5167850.154239-0.347572-0.172400...0.4705160.2147920.0154080.339100-0.034251-0.0475970.103611-0.139579-0.0422410.249977
4CCOc1ccc2ccccc2c1C(=O)N[C@H]1[C@H]2SC(C)(C)[C@...242770.248559-0.0200580.0857720.6787420.6718970.214587-0.221396-0.086168...0.6717900.4241860.1188730.4156230.2575980.1933700.238471-0.0337970.0800180.335890
\n", "

5 rows × 747 columns

\n", "
" ], "text/plain": [ " SMILES Metadata_Plate \\\n", "0 CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC 24277 \n", "1 COc1cc(C)cc(OC)c1[C@@H]1C=C(C)CC[C@H]1C(C)=C 24277 \n", "2 COc1cc(O)cc(\\C=C\\c2ccccc2)c1 24277 \n", "3 COc1c(O)cc2C(=O)O[C@H]3[C@@H](O)[C@H](O)[C@@H]... 24277 \n", "4 CCOc1ccc2ccccc2c1C(=O)N[C@H]1[C@H]2SC(C)(C)[C@... 24277 \n", "\n", " Cells_AreaShape_Compactness Cells_AreaShape_Extent \\\n", "0 0.256710 -0.114990 \n", "1 0.050962 0.051053 \n", "2 -0.098185 0.044212 \n", "3 0.410722 -0.236208 \n", "4 0.248559 -0.020058 \n", "\n", " Cells_AreaShape_FormFactor Cells_AreaShape_MaxFeretDiameter \\\n", "0 -0.237090 0.615594 \n", "1 -0.033733 0.271953 \n", "2 0.164668 0.011985 \n", "3 -0.055084 0.694648 \n", "4 0.085772 0.678742 \n", "\n", " Cells_AreaShape_MaximumRadius Cells_AreaShape_Solidity \\\n", "0 0.550434 0.072795 \n", "1 0.172714 0.145941 \n", "2 0.127098 0.094736 \n", "3 0.516785 0.154239 \n", "4 0.671897 0.214587 \n", "\n", " Cells_AreaShape_Zernike_0_0 Cells_AreaShape_Zernike_1_1 ... \\\n", "0 -0.210908 0.008488 ... \n", "1 -0.039057 -0.070826 ... \n", "2 0.078257 0.119333 ... \n", "3 -0.347572 -0.172400 ... \n", "4 -0.221396 -0.086168 ... \n", "\n", " Nuclei_Texture_SumEntropy_DNA_5_0 Nuclei_Texture_SumEntropy_ER_3_0 \\\n", "0 0.090021 0.375747 \n", "1 -0.030338 0.390687 \n", "2 0.102946 0.279148 \n", "3 0.470516 0.214792 \n", "4 0.671790 0.424186 \n", "\n", " Nuclei_Texture_SumEntropy_Mito_3_0 Nuclei_Texture_SumEntropy_RNA_5_0 \\\n", "0 -0.010427 0.118997 \n", "1 -0.058560 0.065822 \n", "2 0.029891 0.079777 \n", "3 0.015408 0.339100 \n", "4 0.118873 0.415623 \n", "\n", " Nuclei_Texture_SumVariance_ER_3_0 Nuclei_Texture_SumVariance_Mito_5_0 \\\n", "0 -0.156442 -0.018014 \n", "1 -0.113721 -0.089807 \n", "2 -0.007979 0.081155 \n", "3 -0.034251 -0.047597 \n", "4 0.257598 0.193370 \n", "\n", " Nuclei_Texture_SumVariance_RNA_10_0 Nuclei_Texture_Variance_AGP_5_0 \\\n", "0 -0.024573 -0.383890 \n", "1 -0.041392 -0.248967 \n", "2 -0.089633 0.043140 \n", "3 0.103611 -0.139579 \n", "4 0.238471 -0.033797 \n", "\n", " Nuclei_Texture_Variance_ER_5_0 Nuclei_Texture_Variance_RNA_5_0 \n", "0 -0.199967 -0.075658 \n", "1 -0.126183 0.018563 \n", "2 -0.034355 0.052914 \n", "3 -0.042241 0.249977 \n", "4 0.080018 0.335890 \n", "\n", "[5 rows x 747 columns]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# SMILES列移动到第一列\n", "\n", "if 'SMILES' in df_filled.columns:\n", " cols = df_filled.columns.tolist()\n", " cols.insert(0, cols.pop(cols.index('SMILES')))\n", " df_filled = df_filled[cols]\n", "\n", "df_filled.head()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# save to parquet\n", "output_path = root + 'Step2_CP_cleaned.parquet'\n", "# df_filled.to_parquet(output_path, index=False)\n", "\n", "df_filled = pd.read_parquet(output_path)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "数据已成功处理并保存到 ./Processed_Paired_CP.h5 文件中。\n", "共有 86796 个样本。\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import h5py\n", "\n", "# 1. 确保数值列为 float32 类型,这能减少内存使用并提高计算效率\n", "data_cols = [col for col in df_filled.columns if col not in ['Metadata_Plate', 'SMILES']]\n", "df_filled[data_cols] = df_filled[data_cols].astype(np.float32)\n", "\n", "# 2. 对每个 Plate 中的相同 SMILES 进行求均值,一步到位\n", "# 这是一个高效的 Pandas 操作,能将数据处理流程大大简化\n", "processed_df = df_filled.groupby(['Metadata_Plate', 'SMILES']).mean().reset_index()\n", "\n", "# 3. 初始化列表来存储数据。这种方式比在循环中频繁创建和合并 NumPy 数组更高效\n", "all_smiles = []\n", "all_control = []\n", "all_target = []\n", "\n", "# 4. 按 Plate 分组,遍历每个 Plate\n", "# `grouped` 是一个迭代器,可以逐个处理每个 Plate\n", "grouped = processed_df.groupby('Metadata_Plate')\n", "for plate_name, plate_data in grouped:\n", " # 5. 分离 DMSO 和 Target 数据\n", " dmso_data = plate_data[plate_data['SMILES'] == 'DMSO']\n", " target_data = plate_data[plate_data['SMILES'] != 'DMSO']\n", "\n", " # 6. 处理 DMSO 行(Control)\n", " if not dmso_data.empty:\n", " # 提取数值列并转换为 NumPy 数组\n", " control = dmso_data[data_cols].values.flatten().astype(np.float32)\n", " else:\n", " print(f\"警告:Plate '{plate_name}' 没有 DMSO 数据。将用全零填充。\")\n", " control = np.zeros(len(data_cols), dtype=np.float32)\n", "\n", " # 7. 处理非 DMSO 行(Target)\n", " if not target_data.empty:\n", " # 提取 SMILES 和数值数据\n", " smiles = target_data['SMILES'].tolist()\n", " target = target_data[data_cols].values.astype(np.float32)\n", "\n", " # 重复 control,使其与 target 的行数匹配\n", " control_aligned = np.tile(control, (target.shape[0], 1))\n", "\n", " # 将处理后的数据追加到列表中\n", " all_smiles.extend(smiles)\n", " all_control.append(control_aligned)\n", " all_target.append(target)\n", "\n", "# 8. 一次性将列表合并为 NumPy 数组\n", "# 使用 astype('S') 转换为字节串,这是 HDF5 存储字符串的最佳方式\n", "final_smiles = np.array(all_smiles, dtype='S')\n", "# 使用三元表达式防止列表为空时报错\n", "final_control = np.vstack(all_control) if all_control else np.array([], dtype=np.float32).reshape(0, len(data_cols))\n", "final_target = np.vstack(all_target) if all_target else np.array([], dtype=np.float32).reshape(0, len(data_cols))\n", "\n", "# 9. 保存到 HDF5 文件\n", "# output_path = './Processed_Paired_CP.h5'\n", "# with h5py.File(output_path, 'w') as f:\n", "# f.create_dataset('canonical_smiles', data=final_smiles)\n", "# f.create_dataset('control', data=final_control, dtype=np.float32)\n", "# f.create_dataset('target', data=final_target, dtype=np.float32)\n", "\n", "print(f\"数据已成功处理并保存到 {output_path} 文件中。\")\n", "print(f\"共有 {len(final_smiles)} 个样本。\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 规范化SMILES" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "final_smiles" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "规范化后的 SMILES 数组:\n", "86796 86796\n" ] } ], "source": [ "import numpy as np\n", "from rdkit import Chem\n", "\n", "# 假设你的 results_CP['smiles'] 数组如你所示\n", "smiles_array = final_smiles\n", "\n", "# 存储规范化后的 SMILES\n", "canonical_smiles_list = []\n", "\n", "# 遍历数组中的每一个 SMILES\n", "for byte_smiles in smiles_array:\n", " # 1. 解码字节字符串\n", " # smiles_str = byte_smiles.decode('utf-8')\n", "\n", " # 2. 尝试将 SMILES 转换为分子对象\n", " # Chem.MolFromSmiles() 会处理非规范的 SMILES,并返回一个分子对象\n", " mol = Chem.MolFromSmiles(byte_smiles)\n", "\n", " # 3. 如果解析成功,生成规范 SMILES\n", " if mol is not None:\n", " # Chem.MolToSmiles() 默认生成规范 SMILES\n", " canonical_smiles = Chem.MolToSmiles(mol)\n", " canonical_smiles_list.append(canonical_smiles)\n", " else:\n", " # 如果 SMILES 无效,可以添加 None 或原始字符串\n", " print(f\"警告: 无法解析 SMILES: {byte_smiles}\")\n", " canonical_smiles_list.append(None) # 或者 smiles_str\n", "\n", "# 将列表转换回 NumPy 数组\n", "# dtype='O' 可以存储不同长度的字符串\n", "canonical_smiles_array = np.array(canonical_smiles_list, dtype='O')\n", "\n", "print(\"规范化后的 SMILES 数组:\")\n", "print(len(smiles_array), len(canonical_smiles_array))" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['O=S(=O)(NCCNC/C=C/c1ccc(Br)cc1)c1cccc2cnccc12',\n", " 'c1nc(CC2CCNCC2)c[nH]1',\n", " 'C(=Cc1ccccc1)CN1CCN(CCOC(c2ccccc2)c2ccccc2)CC1', ...,\n", " 'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)C(=O)c2cc(-c3ccccc3)on2)OCc2cnnn2CCCC1=O',\n", " 'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)C(=O)c2ccc(N3CCN(C)CC3)cc2)OCc2cnnn2CCCC1=O',\n", " 'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)S(=O)(=O)c2ccc3ccccc3c2)OCc2cnnn2CCCC1=O'],\n", " dtype=object)" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "canonical_smiles_array" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "数据已保存到 Processed_Paired_CP.h5 文件中。\n" ] } ], "source": [ "# 保存到 HDF5 文件\n", "with h5py.File('./Processed_Paired_CP.h5', 'w') as f:\n", " f.create_dataset('canonical_smiles', data=canonical_smiles_array)\n", " f.create_dataset('control', data=final_control, dtype=np.float32)\n", " f.create_dataset('target', data=final_target, dtype=np.float32)\n", "\n", "print(\"数据已保存到 Processed_Paired_CP.h5 文件中。\")" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': array([[ 0.01272539, -0.00142729, 0.00117443, ..., 0.00213188,\n", " 0.00776369, 0.02431507],\n", " [ 0.01272539, -0.00142729, 0.00117443, ..., 0.00213188,\n", " 0.00776369, 0.02431507],\n", " [ 0.01272539, -0.00142729, 0.00117443, ..., 0.00213188,\n", " 0.00776369, 0.02431507],\n", " ...,\n", " [ 0.01929056, -0.00750519, -0.00410514, ..., -0.00124201,\n", " 0.01009735, 0.01925787],\n", " [ 0.01929056, -0.00750519, -0.00410514, ..., -0.00124201,\n", " 0.01009735, 0.01925787],\n", " [ 0.01929056, -0.00750519, -0.00410514, ..., -0.00124201,\n", " 0.01009735, 0.01925787]], dtype=float32),\n", " 'smiles': array([b'O=S(=O)(NCCNC/C=C/c1ccc(Br)cc1)c1cccc2cnccc12',\n", " b'c1nc(CC2CCNCC2)c[nH]1',\n", " b'C(=Cc1ccccc1)CN1CCN(CCOC(c2ccccc2)c2ccccc2)CC1', ...,\n", " b'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)C(=O)c2cc(-c3ccccc3)on2)OCc2cnnn2CCCC1=O',\n", " b'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)C(=O)c2ccc(N3CCN(C)CC3)cc2)OCc2cnnn2CCCC1=O',\n", " b'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)S(=O)(=O)c2ccc3ccccc3c2)OCc2cnnn2CCCC1=O'],\n", " dtype=object),\n", " 'target': array([[ 0.13452166, -0.10539893, -0.46075574, ..., -0.24802667,\n", " -0.28574485, -0.06428494],\n", " [ 0.1773164 , -0.15054591, 0.01029928, ..., -0.23876335,\n", " -0.2282957 , -0.13433062],\n", " [ 0.11017302, 0.01138897, 0.44465727, ..., -0.04879403,\n", " 0.09316072, 0.18261081],\n", " ...,\n", " [-0.06364447, 0.09350243, 0.25299442, ..., 0.07714102,\n", " 0.03842843, -0.24475378],\n", " [ 0.16646433, -0.00153079, 0.26423883, ..., 0.01276418,\n", " 0.01655833, 0.07614829],\n", " [ 0.16382717, -0.0129601 , 0.23206687, ..., -0.05227156,\n", " -0.06377184, -0.0376341 ]], dtype=float32)}" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load Processed_Paired_CP.h5\n", "data = load_from_HDF('./Processed_Paired_CP.h5')\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 配对基因表达" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(68120, 979) (61313, 979)\n" ] } ], "source": [ "print(GE_CSV.shape, GE_CSV_matched.shape)" ] }, { "cell_type": "code", "execution_count": 24, "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", " \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", " \n", " \n", " \n", " \n", " \n", "
det_plateCPD_SMILES221227_x_at212345_s_at218597_s_at217140_s_at209253_at214404_x_at219888_at201225_s_at...218397_at202996_at204608_at211071_s_at203341_at202801_at206414_s_at204978_at205379_at203897_at
0PAC001_U2OS_6H_X1_B1_UNI4445LCC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C...-0.118505-0.293445-0.2940880.292745-0.1458960.973580.247915-0.159028...-0.5299050.8049920.430269-0.97559-1.7406050.3403820.4987780.3521520.535910.52484
1PAC001_U2OS_6H_X1_B1_UNI4445LIc1cccc(CSc2nnc(o2)-c2ccncc2)c10.043264-0.264945-0.0006330.0009350.177604-0.504720.2143150.144271...0.228895-0.3813080.5569690.25051-0.025625-0.024428-0.2251220.533052-0.19329-0.25436
2PAC001_U2OS_6H_X1_B1_UNI4445LClc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1-0.0708050.1957550.004606-0.040855-0.067346-0.19872-0.0531950.167971...-0.031795-0.051728-0.0177410.056810.1306950.005919-0.408722-0.057177-0.256990.31004
3PAC001_U2OS_6H_X1_B1_UNI4445LCOc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc10.027165-0.1495450.173113-0.100695-0.309296-0.20782-0.3239850.294872...-0.013787-0.1776080.219869-0.161290.365195-0.040848-0.2511220.0407320.17091-0.05144
4PAC001_U2OS_6H_X1_B1_UNI4445LOC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c230.3032940.254455-0.055418-0.0536350.000455-0.109080.0314250.044482...-0.052585-0.1961080.2454690.007840.093595-0.3034180.474878-0.022947-0.285890.06423
\n", "

5 rows × 979 columns

\n", "
" ], "text/plain": [ " det_plate \\\n", "0 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "1 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "2 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "3 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "4 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "\n", " CPD_SMILES 221227_x_at \\\n", "0 CC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C... -0.118505 \n", "1 Ic1cccc(CSc2nnc(o2)-c2ccncc2)c1 0.043264 \n", "2 Clc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1 -0.070805 \n", "3 COc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc1 0.027165 \n", "4 OC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c23 0.303294 \n", "\n", " 212345_s_at 218597_s_at 217140_s_at 209253_at 214404_x_at 219888_at \\\n", "0 -0.293445 -0.294088 0.292745 -0.145896 0.97358 0.247915 \n", "1 -0.264945 -0.000633 0.000935 0.177604 -0.50472 0.214315 \n", "2 0.195755 0.004606 -0.040855 -0.067346 -0.19872 -0.053195 \n", "3 -0.149545 0.173113 -0.100695 -0.309296 -0.20782 -0.323985 \n", "4 0.254455 -0.055418 -0.053635 0.000455 -0.10908 0.031425 \n", "\n", " 201225_s_at ... 218397_at 202996_at 204608_at 211071_s_at 203341_at \\\n", "0 -0.159028 ... -0.529905 0.804992 0.430269 -0.97559 -1.740605 \n", "1 0.144271 ... 0.228895 -0.381308 0.556969 0.25051 -0.025625 \n", "2 0.167971 ... -0.031795 -0.051728 -0.017741 0.05681 0.130695 \n", "3 0.294872 ... -0.013787 -0.177608 0.219869 -0.16129 0.365195 \n", "4 0.044482 ... -0.052585 -0.196108 0.245469 0.00784 0.093595 \n", "\n", " 202801_at 206414_s_at 204978_at 205379_at 203897_at \n", "0 0.340382 0.498778 0.352152 0.53591 0.52484 \n", "1 -0.024428 -0.225122 0.533052 -0.19329 -0.25436 \n", "2 0.005919 -0.408722 -0.057177 -0.25699 0.31004 \n", "3 -0.040848 -0.251122 0.040732 0.17091 -0.05144 \n", "4 -0.303418 0.474878 -0.022947 -0.28589 0.06423 \n", "\n", "[5 rows x 979 columns]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GE_CSV_matched.head()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "太棒了!整个 DataFrame 都不存在 NaN 值。\n" ] } ], "source": [ "# 检查整个 DataFrame 那存在NaN的列\n", "nan_columns = GE_CSV_matched.columns[GE_CSV_matched.isnull().any()].tolist()\n", "\n", "if nan_columns:\n", " print(\"以下列仍包含 NaN 值:\")\n", " for col in nan_columns:\n", " # 打印出具体列及其 NaN 值的数量\n", " print(f\"- {col}: {GE_CSV_matched[col].isnull().sum()} 个 NaN\")\n", "else:\n", " print(\"太棒了!整个 DataFrame 都不存在 NaN 值。\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "太棒了!整个 DataFrame 都不存在 NaN 值。\n" ] } ], "source": [ "# 删除CPD_NAME列\n", "# GE_CSV_matched = GE_CSV_matched.drop(columns=['CPD_NAME'])\n", "\n", "# 检查整个 DataFrame 那存在NaN的列\n", "nan_columns = GE_CSV_matched.columns[GE_CSV_matched.isnull().any()].tolist()\n", "\n", "if nan_columns:\n", " print(\"以下列仍包含 NaN 值:\")\n", " for col in nan_columns:\n", " # 打印出具体列及其 NaN 值的数量\n", " print(f\"- {col}: {GE_CSV_matched[col].isnull().sum()} 个 NaN\")\n", "else:\n", " print(\"太棒了!整个 DataFrame 都不存在 NaN 值。\")" ] }, { "cell_type": "code", "execution_count": 27, "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", " \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", " \n", " \n", " \n", " \n", " \n", "
det_plateCPD_SMILES221227_x_at212345_s_at218597_s_at217140_s_at209253_at214404_x_at219888_at201225_s_at...218397_at202996_at204608_at211071_s_at203341_at202801_at206414_s_at204978_at205379_at203897_at
0PAC001_U2OS_6H_X1_B1_UNI4445LCC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C...-0.118505-0.293445-0.2940880.292745-0.1458960.973580.247915-0.159028...-0.5299050.8049920.430269-0.97559-1.7406050.3403820.4987780.3521520.535910.52484
1PAC001_U2OS_6H_X1_B1_UNI4445LIc1cccc(CSc2nnc(o2)-c2ccncc2)c10.043264-0.264945-0.0006330.0009350.177604-0.504720.2143150.144271...0.228895-0.3813080.5569690.25051-0.025625-0.024428-0.2251220.533052-0.19329-0.25436
2PAC001_U2OS_6H_X1_B1_UNI4445LClc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1-0.0708050.1957550.004606-0.040855-0.067346-0.19872-0.0531950.167971...-0.031795-0.051728-0.0177410.056810.1306950.005919-0.408722-0.057177-0.256990.31004
3PAC001_U2OS_6H_X1_B1_UNI4445LCOc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc10.027165-0.1495450.173113-0.100695-0.309296-0.20782-0.3239850.294872...-0.013787-0.1776080.219869-0.161290.365195-0.040848-0.2511220.0407320.17091-0.05144
4PAC001_U2OS_6H_X1_B1_UNI4445LOC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c230.3032940.254455-0.055418-0.0536350.000455-0.109080.0314250.044482...-0.052585-0.1961080.2454690.007840.093595-0.3034180.474878-0.022947-0.285890.06423
\n", "

5 rows × 979 columns

\n", "
" ], "text/plain": [ " det_plate \\\n", "0 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "1 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "2 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "3 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "4 PAC001_U2OS_6H_X1_B1_UNI4445L \n", "\n", " CPD_SMILES 221227_x_at \\\n", "0 CC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C... -0.118505 \n", "1 Ic1cccc(CSc2nnc(o2)-c2ccncc2)c1 0.043264 \n", "2 Clc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1 -0.070805 \n", "3 COc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc1 0.027165 \n", "4 OC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c23 0.303294 \n", "\n", " 212345_s_at 218597_s_at 217140_s_at 209253_at 214404_x_at 219888_at \\\n", "0 -0.293445 -0.294088 0.292745 -0.145896 0.97358 0.247915 \n", "1 -0.264945 -0.000633 0.000935 0.177604 -0.50472 0.214315 \n", "2 0.195755 0.004606 -0.040855 -0.067346 -0.19872 -0.053195 \n", "3 -0.149545 0.173113 -0.100695 -0.309296 -0.20782 -0.323985 \n", "4 0.254455 -0.055418 -0.053635 0.000455 -0.10908 0.031425 \n", "\n", " 201225_s_at ... 218397_at 202996_at 204608_at 211071_s_at 203341_at \\\n", "0 -0.159028 ... -0.529905 0.804992 0.430269 -0.97559 -1.740605 \n", "1 0.144271 ... 0.228895 -0.381308 0.556969 0.25051 -0.025625 \n", "2 0.167971 ... -0.031795 -0.051728 -0.017741 0.05681 0.130695 \n", "3 0.294872 ... -0.013787 -0.177608 0.219869 -0.16129 0.365195 \n", "4 0.044482 ... -0.052585 -0.196108 0.245469 0.00784 0.093595 \n", "\n", " 202801_at 206414_s_at 204978_at 205379_at 203897_at \n", "0 0.340382 0.498778 0.352152 0.53591 0.52484 \n", "1 -0.024428 -0.225122 0.533052 -0.19329 -0.25436 \n", "2 0.005919 -0.408722 -0.057177 -0.25699 0.31004 \n", "3 -0.040848 -0.251122 0.040732 0.17091 -0.05144 \n", "4 -0.303418 0.474878 -0.022947 -0.28589 0.06423 \n", "\n", "[5 rows x 979 columns]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GE_CSV_matched.head()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DMSO 3478\n", "C[C@]12CC[C@H]3[C@@H](CCc4cc(O)ccc34)[C@@H]1CC[C@@H]2O 193\n", "Oc1ccc(cc1)-c1coc2cc(O)cc(O)c2c1=O 193\n", "O=c1cc(oc2c(cccc12)-c1ccccc1)N1CCOCC1 188\n", "OCCNCCNc1ccc(NCCNCCO)c2C(=O)c3c(O)ccc(O)c3C(=O)c12 184\n", " ... \n", "CN(C)CC\\C=C1/c2ccccc2Sc2ccc(Cl)cc12 1\n", "CC(C)=CCc1c(O)cc2occ(-c3ccc4OCOc4c3)c(=O)c2c1O 1\n", "O=C1N(c2ccccc2C1(Cc1ccncc1)Cc1ccncc1)c1ccccc1 1\n", "O=S(=O)(NC1CC1)c1ccc(cc1)S(=O)(=O)N1CCc2ccccc12 1\n", "C[C@@H]1C[C@H]2[C@@H]3CCC4=CC(=O)C=C[C@]4(C)[C@@]3(Cl)[C@@H](O)C[C@]2(C)[C@@]1(OC(=O)c1ccco1)C(=O)CCl 1\n", "Name: CPD_SMILES, Length: 20342, dtype: int64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GE_CSV_matched['CPD_SMILES'].value_counts()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# save to parquet\n", "output_path = root + 'Step2_GE_cleaned.parquet'\n", "GE_CSV_matched.to_parquet(output_path, index=False)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "GE_CSV_matched = pd.read_parquet('./Step2_GE_cleaned.parquet')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(61313, 979)\n" ] }, { "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", " \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", " \n", " \n", " \n", " \n", " \n", "
BROAD_CPD_IDCPD_SMILES221227_x_at212345_s_at218597_s_at217140_s_at209253_at214404_x_at219888_at201225_s_at...218397_at202996_at204608_at211071_s_at203341_at202801_at206414_s_at204978_at205379_at203897_at
0BRD-K07762753CC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C...-0.118505-0.293445-0.2940880.292745-0.1458960.973580.247915-0.159028...-0.5299050.8049920.430269-0.97559-1.7406050.3403820.4987780.3521520.535910.52484
1BRD-K09991945Ic1cccc(CSc2nnc(o2)-c2ccncc2)c10.043264-0.264945-0.0006330.0009350.177604-0.504720.2143150.144271...0.228895-0.3813080.5569690.25051-0.025625-0.024428-0.2251220.533052-0.19329-0.25436
2BRD-K46678324Clc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1-0.0708050.1957550.004606-0.040855-0.067346-0.19872-0.0531950.167971...-0.031795-0.051728-0.0177410.056810.1306950.005919-0.408722-0.057177-0.256990.31004
3BRD-K67860401COc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc10.027165-0.1495450.173113-0.100695-0.309296-0.20782-0.3239850.294872...-0.013787-0.1776080.219869-0.161290.365195-0.040848-0.2511220.0407320.17091-0.05144
4BRD-K52620403OC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c230.3032940.254455-0.055418-0.0536350.000455-0.109080.0314250.044482...-0.052585-0.1961080.2454690.007840.093595-0.3034180.474878-0.022947-0.285890.06423
\n", "

5 rows × 979 columns

\n", "
" ], "text/plain": [ " BROAD_CPD_ID CPD_SMILES \\\n", "0 BRD-K07762753 CC(C)[C@H](CO)Nc1nc(Nc2cc(N)cc(Cl)c2)c2ncn(C(C... \n", "1 BRD-K09991945 Ic1cccc(CSc2nnc(o2)-c2ccncc2)c1 \n", "2 BRD-K46678324 Clc1cc(Cl)c(NC(=O)Nc2ccncc2)c(Cl)c1 \n", "3 BRD-K67860401 COc1ccc(CNC(=O)Nc2ncc(s2)[N+]([O-])=O)cc1 \n", "4 BRD-K52620403 OC(=O)c1ccc2c3nc4ccccc4n3c(=O)c3cccc1c23 \n", "\n", " 221227_x_at 212345_s_at 218597_s_at 217140_s_at 209253_at 214404_x_at \\\n", "0 -0.118505 -0.293445 -0.294088 0.292745 -0.145896 0.97358 \n", "1 0.043264 -0.264945 -0.000633 0.000935 0.177604 -0.50472 \n", "2 -0.070805 0.195755 0.004606 -0.040855 -0.067346 -0.19872 \n", "3 0.027165 -0.149545 0.173113 -0.100695 -0.309296 -0.20782 \n", "4 0.303294 0.254455 -0.055418 -0.053635 0.000455 -0.10908 \n", "\n", " 219888_at 201225_s_at ... 218397_at 202996_at 204608_at 211071_s_at \\\n", "0 0.247915 -0.159028 ... -0.529905 0.804992 0.430269 -0.97559 \n", "1 0.214315 0.144271 ... 0.228895 -0.381308 0.556969 0.25051 \n", "2 -0.053195 0.167971 ... -0.031795 -0.051728 -0.017741 0.05681 \n", "3 -0.323985 0.294872 ... -0.013787 -0.177608 0.219869 -0.16129 \n", "4 0.031425 0.044482 ... -0.052585 -0.196108 0.245469 0.00784 \n", "\n", " 203341_at 202801_at 206414_s_at 204978_at 205379_at 203897_at \n", "0 -1.740605 0.340382 0.498778 0.352152 0.53591 0.52484 \n", "1 -0.025625 -0.024428 -0.225122 0.533052 -0.19329 -0.25436 \n", "2 0.130695 0.005919 -0.408722 -0.057177 -0.25699 0.31004 \n", "3 0.365195 -0.040848 -0.251122 0.040732 0.17091 -0.05144 \n", "4 0.093595 -0.303418 0.474878 -0.022947 -0.28589 0.06423 \n", "\n", "[5 rows x 979 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(GE_CSV_matched.shape)\n", "\n", "GE_CSV_matched.head()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PAC061_U2OS_6H_X1_B1_UNI4445R 177\n", "PAC063_U2OS_6H_X3_B1_UNI4445R 177\n", "PAC068_U2OS_6H_X3_B1_UNI4445R 177\n", "PAC068_U2OS_6H_X1_B1_UNI4445R 176\n", "PAC068_U2OS_6H_X2_B1_UNI4445R 176\n", " ... \n", "PAC008_U2OS_6H_X3_B1_UNI4445R 9\n", "PAC007_U2OS_6H_X2_B1_UNI4445R 9\n", "PAC008_U2OS_6H_X2_B1_UNI4445R 9\n", "PAC035_U2OS_6H_X3_B1_UNI4445L 8\n", "PAC035_U2OS_6H_X1_B1_UNI4445L 8\n", "Name: det_plate, Length: 389, dtype: int64" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GE_CSV_matched['det_plate'].value_counts()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# # read GE_CSV_matched\n", "# import pandas as pd\n", "# GE_CSV_matched = pd.read_parquet('./Step2_GE_cleaned.parquet')\n", "# GE_CSV_matched.head()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "警告: Plate PAC035_U2OS_6H_X1_B1_UNI4445L 中没有其他化合物数据。跳过。\n", "警告: Plate PAC035_U2OS_6H_X2_B1_UNI4445L 中没有其他化合物数据。跳过。\n", "警告: Plate PAC035_U2OS_6H_X3_B1_UNI4445L 中没有其他化合物数据。跳过。\n", "警告: Plate PAC062_U2OS_6H_X1_B1_UNI4445L 中没有 DMSO 行,使用其他 Plate 的 DMSO 均值填充。\n", "警告: Plate PAC062_U2OS_6H_X2_B1_UNI4445L 中没有 DMSO 行,使用其他 Plate 的 DMSO 均值填充。\n", "警告: Plate PAC062_U2OS_6H_X3_B1_UNI4445L 中没有 DMSO 行,使用其他 Plate 的 DMSO 均值填充。\n", "smiles: [b'Brc1c(NC2=NCCN2)ccc2nccnc12', b'C(C1=NCCN1)c1cccc2ccccc12',\n", " b'C(CC1CCNCC1)Cc1c[nH]cn1', ...,\n", " b'[O-][N+](=O)c1ccc(C=NNC(=O)CNc2cccc3ccccc23)o1',\n", " b'[O-][N+](=O)c1ccc(o1)C(=O)OCc1nnc(o1)-c1ccccc1',\n", " b'c1ccc(cc1)-c1cnc(nn1)-c1ccccn1']\n", "control: [[-0.0064055 , -0.084825 , -0.04988559, ..., -0.00964386, -0.04108364,\n", " 0.05288727],\n", " [-0.0064055 , -0.084825 , -0.04988559, ..., -0.00964386, -0.04108364,\n", " 0.05288727],\n", " [-0.0064055 , -0.084825 , -0.04988559, ..., -0.00964386, -0.04108364,\n", " 0.05288727],\n", " ...,\n", " [-0.03671667, -0.0018625 , 0.11883333, ..., -0.02260833, -0.38597083,\n", " -0.05919583],\n", " [-0.03671667, -0.0018625 , 0.11883333, ..., -0.02260833, -0.38597083,\n", " -0.05919583],\n", " [-0.03671667, -0.0018625 , 0.11883333, ..., -0.02260833, -0.38597083,\n", " -0.05919583]]\n", "target: [[-0.2927055, 0.174255 , -0.0271475, ..., 0.1775525, -0.32239 ,\n", " -0.04767 ],\n", " [ 0.0540945, -0.205745 , 0.0111595, ..., 0.0287825, -0.29249 ,\n", " -0.09885 ],\n", " [ 0.0193845, 0.174755 , 0.1224125, ..., -0.1546475, 0.43921 ,\n", " -0.37556 ],\n", " ...,\n", " [ 0.215 , 0.0721 , -0.3513 , ..., 0.2826 , -0.194 ,\n", " 0.3589 ],\n", " [ 0.2546 , 0.2264 , -0.1818 , ..., 0.1765 , -0.1461 ,\n", " 0.2113 ],\n", " [ 0.0762 , 0.2918 , -0.2467 , ..., -0.295 , -0.1428 ,\n", " 0.4439 ]]\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import h5py\n", "\n", "# 初始化存储结果的字典\n", "results_CP = {\n", " 'smiles': [], # 存储非 DMSO 的 smiles\n", " 'control': [], # 存储 DMSO 行对应的 672 维数据\n", " 'target': [] # 存储非 DMSO 行对应的 672 维数据\n", "}\n", "\n", "def process_plate(df):\n", " # 对当前 Plate 中的相同 CPD_SMILES 进行求均值\n", " # 显式添加 numeric_only=True 来消除警告\n", " return df.groupby('CPD_SMILES', as_index=False).mean(numeric_only=True)\n", "\n", "df = GE_CSV_matched\n", "# 按 det_plate 分组,对每个组应用 process_plate 函数\n", "processed_data_list = []\n", "for plate_name, plate_df in df.groupby('det_plate'):\n", " processed_plate_df = process_plate(plate_df)\n", " # 为处理后的数据添加分组ID,以便后续使用\n", " processed_plate_df['det_plate'] = plate_name\n", " processed_data_list.append(processed_plate_df)\n", "\n", "if processed_data_list:\n", " processed_data = pd.concat(processed_data_list, ignore_index=True)\n", "else:\n", " print(\"没有数据可供处理。\")\n", " exit()\n", "\n", "# 获取数值列的列名列表\n", "# 这将排除 det_plate 和 CPD_SMILES\n", "numeric_cols = [col for col in processed_data.columns if col not in ['det_plate', 'CPD_SMILES']]\n", "\n", "# 按 Plate 分组\n", "grouped = processed_data.groupby('det_plate')\n", "\n", "# 遍历每个 Plate\n", "for plate_name, plate_data in grouped:\n", " # 分离 DMSO 和非 DMSO 行\n", " na_mask = plate_data['CPD_SMILES'] == 'DMSO'\n", " na_data = plate_data[na_mask]\n", " non_na_data = plate_data[~na_mask]\n", "\n", " # 处理 DMSO 行(control)\n", " if not na_data.empty:\n", " # 使用显式列名列表来提取数据\n", " control = na_data[numeric_cols].values\n", " control = control.flatten()\n", " else:\n", " # 这里的警告提示了你数据中可能存在的问题\n", " print(f\"警告: Plate {plate_name} 中没有 DMSO 行,使用其他 Plate 的 DMSO 均值填充。\")\n", " # 可以用所有 DMSO 的平均值作为代替\n", " all_dmso_data = processed_data[processed_data['CPD_SMILES'] == 'DMSO']\n", " if not all_dmso_data.empty:\n", " control = all_dmso_data[numeric_cols].mean().values.flatten()\n", " else:\n", " # 如果整个数据集都没有 DMSO,用全零填充\n", " print(f\"警告: 整个数据集中都没有 DMSO,用全零填充 control。\")\n", " control = np.zeros(len(numeric_cols))\n", "\n", " # 处理非 DMSO 行(smiles 和 target)\n", " if not non_na_data.empty:\n", " smiles = non_na_data['CPD_SMILES'].tolist()\n", " # 使用显式列名列表来提取数据\n", " target = non_na_data[numeric_cols].values\n", "\n", " # 对齐 control 和 target:通过重复 control 使其长度与 target 一致\n", " control_aligned = np.tile(control, (target.shape[0], 1))\n", "\n", " # 存储结果\n", " results_CP['smiles'].extend(smiles)\n", " results_CP['control'].append(control_aligned)\n", " results_CP['target'].append(target)\n", " else:\n", " print(f\"警告: Plate {plate_name} 中没有其他化合物数据。跳过。\")\n", "\n", "# 将结果转换为 NumPy 数组并保存\n", "if results_CP['smiles']:\n", " results_CP['smiles'] = np.array(results_CP['smiles'], dtype='S256') # 使用 S256 来存储字符串\n", " results_CP['control'] = np.vstack(results_CP['control'])\n", " results_CP['target'] = np.vstack(results_CP['target'])\n", "\n", " # 输出结果,用逗号分隔元素\n", " print(\"smiles:\", np.array2string(results_CP['smiles'], separator=', '))\n", " print(\"control:\", np.array2string(results_CP['control'], separator=', '))\n", " print(\"target:\", np.array2string(results_CP['target'], separator=', '))\n", "\n", " # 保存到 HDF5 文件\n", " # with h5py.File('./Processed_Paired_GE.h5', 'w') as f:\n", " # f.create_dataset('smiles', data=results_CP['smiles'])\n", " # f.create_dataset('control', data=results_CP['control'])\n", " # f.create_dataset('target', data=results_CP['target'])\n", "\n", " # print(\"数据已保存到 Processed_Paired_GE.h5 文件中。\")\n", "else:\n", " print(\"没有找到任何可用的数据来保存。\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 规范化 SMILES" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([b'Brc1c(NC2=NCCN2)ccc2nccnc12', b'C(C1=NCCN1)c1cccc2ccccc12',\n", " b'C(CC1CCNCC1)Cc1c[nH]cn1', ...,\n", " b'[O-][N+](=O)c1ccc(C=NNC(=O)CNc2cccc3ccccc23)o1',\n", " b'[O-][N+](=O)c1ccc(o1)C(=O)OCc1nnc(o1)-c1ccccc1',\n", " b'c1ccc(cc1)-c1cnc(nn1)-c1ccccn1'], dtype='|S256')" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results_CP['smiles']" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "规范化后的 SMILES 数组:\n", "57797 57797\n" ] } ], "source": [ "import numpy as np\n", "from rdkit import Chem\n", "\n", "# 假设你的 results_CP['smiles'] 数组如你所示\n", "smiles_array = np.array(results_CP['smiles'])\n", "\n", "# 存储规范化后的 SMILES\n", "canonical_smiles_list = []\n", "\n", "# 遍历数组中的每一个 SMILES\n", "for byte_smiles in smiles_array:\n", " # 1. 解码字节字符串\n", " smiles_str = byte_smiles.decode('utf-8')\n", "\n", " # 2. 尝试将 SMILES 转换为分子对象\n", " # Chem.MolFromSmiles() 会处理非规范的 SMILES,并返回一个分子对象\n", " mol = Chem.MolFromSmiles(smiles_str)\n", "\n", " # 3. 如果解析成功,生成规范 SMILES\n", " if mol is not None:\n", " # Chem.MolToSmiles() 默认生成规范 SMILES\n", " canonical_smiles = Chem.MolToSmiles(mol)\n", " canonical_smiles_list.append(canonical_smiles)\n", " else:\n", " # 如果 SMILES 无效,可以添加 None 或原始字符串\n", " print(f\"警告: 无法解析 SMILES: {smiles_str}\")\n", " canonical_smiles_list.append(None) # 或者 smiles_str\n", "\n", "# 将列表转换回 NumPy 数组\n", "# dtype='O' 可以存储不同长度的字符串\n", "canonical_smiles_array = np.array(canonical_smiles_list, dtype='O')\n", "\n", "print(\"规范化后的 SMILES 数组:\")\n", "print(len(smiles_array), len(canonical_smiles_array))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[b'Brc1c(NC2=NCCN2)ccc2nccnc12' b'C(C1=NCCN1)c1cccc2ccccc12'\n", " b'C(CC1CCNCC1)Cc1c[nH]cn1' b'C(COCc1ccccc1)Cc1cnc[nH]1'\n", " b'C1CCC(CC1)n1cnc2c(Nc3ccc(cc3)N3CCOCC3)nc(Oc3cccc4ccccc34)nc12']\n" ] }, { "data": { "text/plain": [ "array(['Brc1c(NC2=NCCN2)ccc2nccnc12', 'c1ccc2c(CC3=NCCN3)cccc2c1',\n", " 'c1nc(CCCC2CCNCC2)c[nH]1', 'c1ccc(COCCCc2cnc[nH]2)cc1',\n", " 'c1ccc2c(Oc3nc(Nc4ccc(N5CCOCC5)cc4)c4ncn(C5CCCCC5)c4n3)cccc2c1'],\n", " dtype=object)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(smiles_array[:5])\n", "canonical_smiles_array[:5]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-3.01315000e-01, 2.78822000e-01, -1.71820000e-03, ...,\n", " 5.91480000e-01, -6.37050000e-02, 3.87160000e-01],\n", " [ 1.12985000e-01, -1.49480000e-02, -1.90191500e-01, ...,\n", " 4.85880000e-01, 7.97650000e-02, -8.35200000e-02],\n", " [-4.05000000e-04, -6.98980000e-02, -7.65150000e-03, ...,\n", " 2.34180000e-01, 1.89735000e-01, 1.55560000e-01],\n", " ...,\n", " [-2.73184000e-01, 1.35683125e-01, -3.48305000e-02, ...,\n", " 8.96000000e-02, -3.95982500e-01, -4.78477500e-02],\n", " [ 4.49702000e-02, -1.57217000e-01, 2.03263333e-02, ...,\n", " 1.31445000e-01, -1.57280167e-01, 5.00203333e-02],\n", " [-1.18556467e-01, -3.71366667e-03, -1.04697000e-01, ...,\n", " -2.55181667e-01, -8.59601667e-02, -1.04776333e-01]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results_CP['target']" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "# 保存到 HDF5 文件\n", "with h5py.File('./Processed_Paired_GE.h5', 'w') as f:\n", " f.create_dataset('canonical_smiles', data=canonical_smiles_array)\n", " f.create_dataset('control', data=results_CP['control'])\n", " f.create_dataset('target', data=results_CP['target'])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n", "\u001b[1;31m请查看单元格中的代码,以确定故障的可能原因。\n", "\u001b[1;31m单击此处了解详细信息。\n", "\u001b[1;31m有关更多详细信息,请查看 Jupyter log。" ] } ], "source": [ "# load './Processed_Paired_CP.h5' \n", "with h5py.File('./Processed_Paired_CP.h5', 'r') as f:\n", " smiles_cp = f['canonical_smiles'][:]\n", " control_cp = f['control'][:]\n", " target_cp = f['target'][:]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['O=S(=O)(NCCNC/C=C/c1ccc(Br)cc1)c1cccc2cnccc12',\n", " 'c1nc(CC2CCNCC2)c[nH]1',\n", " 'C(=Cc1ccccc1)CN1CCN(CCOC(c2ccccc2)c2ccccc2)CC1', ...,\n", " 'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)C(=O)c2cc(-c3ccccc3)on2)OCc2cnnn2CCCC1=O',\n", " 'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)C(=O)c2ccc(N3CCN(C)CC3)cc2)OCc2cnnn2CCCC1=O',\n", " 'C[C@H](CO)N1C[C@H](C)[C@H](CN(C)S(=O)(=O)c2ccc3ccccc3c2)OCc2cnnn2CCCC1=O'],\n", " dtype='