{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**One method of removing batch effects was pyComBat.**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:26:29.722698Z","iopub.execute_input":"2023-09-23T22:26:29.723713Z","iopub.status.idle":"2023-09-23T22:26:31.783270Z","shell.execute_reply.started":"2023-09-23T22:26:29.723679Z","shell.execute_reply":"2023-09-23T22:26:31.782162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = pd.read_csv('/kaggle/input/multiome-expression-celltype-donorid/multiome_expression_celltype_donorid.csv',index_col=0)\nD","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:28:23.800646Z","iopub.execute_input":"2023-09-23T22:28:23.801335Z","iopub.status.idle":"2023-09-23T22:32:01.783250Z","shell.execute_reply.started":"2023-09-23T22:28:23.801293Z","shell.execute_reply":"2023-09-23T22:32:01.782025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Before adjustment","metadata":{}},{"cell_type":"code","source":"pca = PCA(n_components=2)\npca.fit(D.iloc[:,2:])\nfeature = pca.transform(D.iloc[:,2:])\ndat_corrected_PCA = pd.DataFrame(feature, columns=[\"PC{}\".format(x + 1) for x in range(2)])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:32:08.019286Z","iopub.execute_input":"2023-09-23T22:32:08.019744Z","iopub.status.idle":"2023-09-23T22:32:36.643865Z","shell.execute_reply.started":"2023-09-23T22:32:08.019710Z","shell.execute_reply":"2023-09-23T22:32:36.641607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch = D['donor_id']\nle = LabelEncoder()\nBatch = le.fit_transform(batch)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:32:39.915954Z","iopub.execute_input":"2023-09-23T22:32:39.916383Z","iopub.status.idle":"2023-09-23T22:32:39.939807Z","shell.execute_reply.started":"2023-09-23T22:32:39.916349Z","shell.execute_reply":"2023-09-23T22:32:39.938746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.scatter(feature[:, 0], feature[:, 1], alpha=0.3, c=list(Batch),cmap=\"jet\")\nplt.grid()\nplt.xlabel(\"PC1\")\nplt.ylabel(\"PC2\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:44:18.709286Z","iopub.execute_input":"2023-09-23T22:44:18.710094Z","iopub.status.idle":"2023-09-23T22:44:19.774675Z","shell.execute_reply.started":"2023-09-23T22:44:18.710057Z","shell.execute_reply":"2023-09-23T22:44:19.773486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**There doesn't seem to be much impact between batches.**","metadata":{}},{"cell_type":"markdown","source":"# After adjustment","metadata":{}},{"cell_type":"code","source":"!pip install combat\n!pip install combat --upgrade\nfrom combat.pycombat import pycombat","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:32:57.257242Z","iopub.execute_input":"2023-09-23T22:32:57.257653Z","iopub.status.idle":"2023-09-23T22:33:30.105074Z","shell.execute_reply.started":"2023-09-23T22:32:57.257622Z","shell.execute_reply":"2023-09-23T22:33:30.103769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dat_combat = pycombat(D.iloc[:,2:].T,batch)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:33:32.897813Z","iopub.execute_input":"2023-09-23T22:33:32.898639Z","iopub.status.idle":"2023-09-23T22:34:35.789612Z","shell.execute_reply.started":"2023-09-23T22:33:32.898600Z","shell.execute_reply":"2023-09-23T22:34:35.788594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=2)\npca.fit(dat_combat.T)\nFeature = pca.transform(dat_combat.T)\nDat_corrected_PCA = pd.DataFrame(Feature, columns=[\"PC{}\".format(x + 1) for x in range(2)])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:34:59.683338Z","iopub.execute_input":"2023-09-23T22:34:59.683848Z","iopub.status.idle":"2023-09-23T22:35:26.936665Z","shell.execute_reply.started":"2023-09-23T22:34:59.683805Z","shell.execute_reply":"2023-09-23T22:35:26.935088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.scatter(Feature[:, 0], Feature[:, 1], alpha=0.3, c=list(Batch),cmap=\"jet\")\nplt.grid()\nplt.xlabel(\"PC1\")\nplt.ylabel(\"PC2\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-23T22:44:08.955622Z","iopub.execute_input":"2023-09-23T22:44:08.956969Z","iopub.status.idle":"2023-09-23T22:44:09.888485Z","shell.execute_reply.started":"2023-09-23T22:44:08.956913Z","shell.execute_reply":"2023-09-23T22:44:09.887577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conversely, has the variability increased?**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}