{"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":"# Differential expression between annotated cell types and between leiden clusters in CITE-seq input data (train and test)","metadata":{}},{"cell_type":"code","source":"%%capture\n!pip3 install scanpy[leiden]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-29T19:13:02.252908Z","iopub.execute_input":"2023-01-29T19:13:02.253324Z","iopub.status.idle":"2023-01-29T19:13:14.971401Z","shell.execute_reply.started":"2023-01-29T19:13:02.253289Z","shell.execute_reply":"2023-01-29T19:13:14.969898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:47:20.178800Z","iopub.execute_input":"2023-01-29T18:47:20.179237Z","iopub.status.idle":"2023-01-29T18:47:20.183554Z","shell.execute_reply.started":"2023-01-29T18:47:20.179200Z","shell.execute_reply":"2023-01-29T18:47:20.182747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR_RAW = \"/kaggle/input/msci-raw-counts-anndata\"\n# Cite\nFP_CITE_RAW_TRAIN_INPUTS = os.path.join(DATA_DIR_RAW,\"train_cite_inputs_raw.ann.h5\")\nFP_CITE_RAW_TRAIN_TARGETS = os.path.join(DATA_DIR_RAW,\"train_cite_targets_raw.ann.h5\")\nFP_CITE_RAW_TEST_INPUTS = os.path.join(DATA_DIR_RAW,\"test_cite_inputs_raw.ann.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:47:22.682833Z","iopub.execute_input":"2023-01-29T18:47:22.683224Z","iopub.status.idle":"2023-01-29T18:47:22.689517Z","shell.execute_reply.started":"2023-01-29T18:47:22.683191Z","shell.execute_reply":"2023-01-29T18:47:22.688071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\n\nimport anndata as ad\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport scanpy as sc\n\nsc.settings.verbosity = 3\nsc.set_figure_params(dpi=150)\nsns.set_style(\"ticks\")","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:47:25.654608Z","iopub.execute_input":"2023-01-29T18:47:25.655038Z","iopub.status.idle":"2023-01-29T18:47:28.151606Z","shell.execute_reply.started":"2023-01-29T18:47:25.655000Z","shell.execute_reply":"2023-01-29T18:47:28.150685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata_fp = \"/kaggle/input/open-problems-multimodal/metadata.csv\"\nadd_metadata_fp = \"/kaggle/input/open-problems-multimodal/metadata_cite_day_2_donor_27678.csv\"\nmetadata = pd.read_csv(metadata_fp)\nadd_metadata = pd.read_csv(add_metadata_fp)\nmetadata = pd.concat([metadata, add_metadata])\nmetadata.set_index(\"cell_id\", inplace=True)\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:47:29.187644Z","iopub.execute_input":"2023-01-29T18:47:29.188315Z","iopub.status.idle":"2023-01-29T18:47:29.675618Z","shell.execute_reply.started":"2023-01-29T18:47:29.188251Z","shell.execute_reply":"2023-01-29T18:47:29.674331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def annotate(adata: ad.AnnData, metadata: pd.DataFrame, f_donors: [str]):\n    adata.obs[\"cell_type\"] = metadata[\"cell_type\"][adata.obs_names]\n    adata.obs[\"day\"] = metadata[\"day\"][adata.obs_names].astype(str)\n    adata.obs[\"donor\"] = metadata[\"donor\"][adata.obs_names].astype(str)\n    adata.obs[\"gender\"] = \"m\"\n    adata.obs.loc[adata.obs[\"donor\"].isin(f_donors), \"gender\"] = \"f\"\n    adata.obs[\"n_genes_counts\"] = (adata.X > 0).sum(axis=1).A1\n    adata.obs[\"read_depth\"] = adata.X.sum(axis=1).A1","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:47:32.648081Z","iopub.execute_input":"2023-01-29T18:47:32.649054Z","iopub.status.idle":"2023-01-29T18:47:32.657376Z","shell.execute_reply.started":"2023-01-29T18:47:32.649010Z","shell.execute_reply":"2023-01-29T18:47:32.656251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Input raw data","metadata":{}},{"cell_type":"code","source":"cite_raw_train = sc.read_h5ad(FP_CITE_RAW_TRAIN_INPUTS)\ncite_raw_test = sc.read_h5ad(FP_CITE_RAW_TEST_INPUTS)\ncite_raw = ad.concat([cite_raw_train, cite_raw_test], merge=\"same\")\ndel cite_raw_train\ndel cite_raw_test\ngc.collect()\nannotate(cite_raw, metadata, f_donors = [\"13176\", ])\ncite_raw.layers[\"counts\"] = cite_raw.X.copy()\nadata = cite_raw","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:47:37.384970Z","iopub.execute_input":"2023-01-29T18:47:37.385443Z","iopub.status.idle":"2023-01-29T18:48:36.669954Z","shell.execute_reply.started":"2023-01-29T18:47:37.385405Z","shell.execute_reply":"2023-01-29T18:48:36.668593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get HVG and normalise variance with analytical pearson residuals, count PCA","metadata":{}},{"cell_type":"code","source":"sc.experimental.pp.highly_variable_genes(adata, n_top_genes=3000, batch_key=\"donor\")\nsc.experimental.pp.normalize_pearson_residuals_pca(adata)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:49:13.617415Z","iopub.execute_input":"2023-01-29T18:49:13.618175Z","iopub.status.idle":"2023-01-29T18:52:00.756830Z","shell.execute_reply.started":"2023-01-29T18:49:13.618138Z","shell.execute_reply":"2023-01-29T18:52:00.755353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot PCA on analytical pearson residuals","metadata":{}},{"cell_type":"code","source":"sc.pl.pca(cite_raw, color=[\"cell_type\", \"donor\", \"day\", \"n_genes_counts\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-25T09:46:23.421209Z","iopub.execute_input":"2023-01-25T09:46:23.421953Z","iopub.status.idle":"2023-01-25T09:46:28.059362Z","shell.execute_reply.started":"2023-01-25T09:46:23.421898Z","shell.execute_reply":"2023-01-25T09:46:28.047850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot UMAP","metadata":{}},{"cell_type":"code","source":"sc.pp.neighbors(cite_raw, n_pcs=15, n_neighbors=20)\nsc.tl.umap(cite_raw, min_dist=0.2)\nsc.pl.umap(cite_raw, color=[\"cell_type\", \"donor\", \"day\"], frameon=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:24:59.25572Z","iopub.execute_input":"2022-12-10T15:24:59.256783Z","iopub.status.idle":"2022-12-10T15:28:19.897209Z","shell.execute_reply.started":"2022-12-10T15:24:59.256711Z","shell.execute_reply":"2022-12-10T15:28:19.894949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cite_raw.obs[\"EryP\"] = (cite_raw.obs.cell_type == \"EryP\").astype(\"str\")\ncite_raw.obs[\"MkP\"] = (cite_raw.obs.cell_type == \"MkP\").astype(\"str\")","metadata":{"execution":{"iopub.status.busy":"2023-01-25T09:46:35.227665Z","iopub.execute_input":"2023-01-25T09:46:35.228154Z","iopub.status.idle":"2023-01-25T09:46:35.351981Z","shell.execute_reply.started":"2023-01-25T09:46:35.228113Z","shell.execute_reply":"2023-01-25T09:46:35.350738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"proteins=[\"CD62P\", \"CD71\", \"CD82\", \"KLRG1\", \"CD112\", \"CD42b\", \"CD88\", \"CD41\", \"CD62L\", \"CD58\"]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:52:37.877698Z","iopub.execute_input":"2023-01-29T18:52:37.878234Z","iopub.status.idle":"2023-01-29T18:52:37.887915Z","shell.execute_reply.started":"2023-01-29T18:52:37.878186Z","shell.execute_reply":"2023-01-29T18:52:37.886652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lst=[\"CD62P\"]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:53:01.390041Z","iopub.execute_input":"2023-01-29T18:53:01.390484Z","iopub.status.idle":"2023-01-29T18:53:01.395709Z","shell.execute_reply.started":"2023-01-29T18:53:01.390446Z","shell.execute_reply":"2023-01-29T18:53:01.394398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"G1S_genes_Tirosh = ['ENSG00000100297', 'ENSG00000132646', 'ENSG00000176890', 'ENSG00000168496', 'ENSG00000073111', 'ENSG00000104738', 'ENSG00000167325', 'ENSG00000076248', 'ENSG00000131153', 'ENSG00000076003', 'ENSG00000144354', 'ENSG00000143476', 'ENSG00000198056', 'ENSG00000276043', 'ENSG00000151725', 'ENSG00000119969', 'ENSG00000049541', 'ENSG00000117748', 'ENSG00000132780', 'ENSG00000111247', 'ENSG00000112312', 'ENSG00000092470', 'ENSG00000163950', 'ENSG00000175305', 'ENSG00000012963', 'ENSG00000077514', 'ENSG00000095002', 'ENSG00000156802', 'ENSG00000051180', 'ENSG00000171848', 'ENSG00000093009', 'ENSG00000094804', 'ENSG00000174371', 'ENSG00000075131', 'ENSG00000136982', 'ENSG00000197299', 'ENSG00000118412', 'ENSG00000162607', 'ENSG00000092853', 'ENSG00000101868', 'ENSG00000159259', 'ENSG00000136492', 'ENSG00000129173']\nG2M_genes_Tirosh = ['ENSG00000164104', 'ENSG00000170312', 'ENSG00000137804', 'ENSG00000175063', 'ENSG00000089685', 'ENSG00000088325', 'ENSG00000131747', 'ENSG00000080986', 'ENSG00000123975', 'ENSG00000143228', 'ENSG00000173207', 'ENSG00000148773', 'ENSG00000120802', 'ENSG00000117724', 'ENSG00000013810', 'ENSG00000129195', 'ENSG00000113810', 'ENSG00000157456', 'ENSG00000169607', 'ENSG00000136108', 'ENSG00000178999', 'ENSG00000169679', 'ENSG00000138160', 'ENSG00000143401', 'ENSG00000188229', 'ENSG00000075218', 'ENSG00000138182', 'ENSG00000123485', 'ENSG00000111665', 'ENSG00000189159', 'ENSG00000117399', 'ENSG00000112742', 'ENSG00000158402', 'ENSG00000142945', 'ENSG00000100401', 'ENSG00000010292', 'ENSG00000126787', 'ENSG00000184661', 'ENSG00000134690', 'ENSG00000114346', 'ENSG00000137807', 'ENSG00000072571', 'ENSG00000087586', 'ENSG00000134222', 'ENSG00000011426', 'ENSG00000143815', 'ENSG00000175216', 'ENSG00000138778', 'ENSG00000102974', 'ENSG00000117650', 'ENSG00000092140', 'ENSG00000139354', 'ENSG00000094916', 'ENSG00000115163']","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:31:41.633398Z","iopub.execute_input":"2022-12-10T15:31:41.634294Z","iopub.status.idle":"2022-12-10T15:31:41.647522Z","shell.execute_reply.started":"2022-12-10T15:31:41.634226Z","shell.execute_reply":"2022-12-10T15:31:41.64598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cite_raw.var[\"ensembl_ids\"] = [ t.split('_')[0] for t in cite_raw.var_names]\ncite_raw.var[\"gene_names\"] = [ t.split('_')[1] for t in cite_raw.var_names]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:52:43.650513Z","iopub.execute_input":"2023-01-29T18:52:43.650963Z","iopub.status.idle":"2023-01-29T18:52:43.683527Z","shell.execute_reply.started":"2023-01-29T18:52:43.650926Z","shell.execute_reply":"2023-01-29T18:52:43.682190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cite_raw.obs[\"markers_Mkp\"]= cite_raw[:,cite_raw.var[\"gene_names\"].isin(lst)].X.sum(axis=1).A1 ","metadata":{"execution":{"iopub.status.busy":"2023-01-25T09:47:47.301471Z","iopub.execute_input":"2023-01-25T09:47:47.301960Z","iopub.status.idle":"2023-01-25T09:47:47.337113Z","shell.execute_reply.started":"2023-01-25T09:47:47.301916Z","shell.execute_reply":"2023-01-25T09:47:47.335853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for elem in proteins:\n    v=cite_raw[:,cite_raw.var[\"gene_names\"].isin([elem])].X.sum(axis=1).A1\n    v=np.clip(v, np.percentile(v, 5), np.percentile(v, 95))\n    cite_raw.obs[elem]= v","metadata":{"execution":{"iopub.status.busy":"2023-01-29T19:17:11.424792Z","iopub.execute_input":"2023-01-29T19:17:11.425218Z","iopub.status.idle":"2023-01-29T19:17:15.186494Z","shell.execute_reply.started":"2023-01-29T19:17:11.425182Z","shell.execute_reply":"2023-01-29T19:17:15.185185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for elem in proteins:\n    v=cite_raw[:,cite_raw.var[\"gene_names\"].isin([elem])].X.sum(axis=1).A1\n    v=[1 if elem >= np.median(v) else 0 for elem in v]\n    cite_raw.obs[elem]= v","metadata":{"execution":{"iopub.status.busy":"2023-01-29T19:29:50.645690Z","iopub.execute_input":"2023-01-29T19:29:50.646172Z","iopub.status.idle":"2023-01-29T19:34:48.830113Z","shell.execute_reply.started":"2023-01-29T19:29:50.646135Z","shell.execute_reply":"2023-01-29T19:34:48.828100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.pca(cite_raw, color=[elem for elem in proteins])","metadata":{"execution":{"iopub.status.busy":"2023-01-29T19:17:18.601599Z","iopub.execute_input":"2023-01-29T19:17:18.602037Z","iopub.status.idle":"2023-01-29T19:17:26.520879Z","shell.execute_reply.started":"2023-01-29T19:17:18.602002Z","shell.execute_reply":"2023-01-29T19:17:26.519406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.umap(cite_raw, color=[\"markers_Mkp\"], frameon=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-21T10:39:36.549960Z","iopub.execute_input":"2023-01-21T10:39:36.550358Z","iopub.status.idle":"2023-01-21T10:39:36.937611Z","shell.execute_reply.started":"2023-01-21T10:39:36.550324Z","shell.execute_reply":"2023-01-21T10:39:36.936149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cite_raw.obs[\"G1S_genes_Tirosh_mean\"] = cite_raw[:,cite_raw.var[\"ensembl_ids\"].isin(G1S_genes_Tirosh)].X.sum(axis=1).A1 / len(G1S_genes_Tirosh)\ncite_raw.obs[\"G2M_genes_Tirosh_mean\"] = cite_raw[:,cite_raw.var[\"ensembl_ids\"].isin(G2M_genes_Tirosh)].X.sum(axis=1).A1 / len(G2M_genes_Tirosh)\ncite_raw.obs[\"G1S+2M_genes_Tirosh_mean\"] = cite_raw[:,cite_raw.var[\"ensembl_ids\"].isin(G1S_genes_Tirosh + G2M_genes_Tirosh)].X.sum(axis=1).A1 / len(G1S_genes_Tirosh + G2M_genes_Tirosh)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:35:18.531048Z","iopub.execute_input":"2022-12-10T15:35:18.531532Z","iopub.status.idle":"2022-12-10T15:35:22.857139Z","shell.execute_reply.started":"2022-12-10T15:35:18.531494Z","shell.execute_reply":"2022-12-10T15:35:22.855847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.umap(cite_raw, color=[\"cell_type\", \"EryP\", \"MkP\", \"G1S+2M_genes_Tirosh_mean\", \"n_genes_counts\", \"read_depth\", \"day\", \"donor\"], frameon=False, ncols=3)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:35:25.320347Z","iopub.execute_input":"2022-12-10T15:35:25.320788Z","iopub.status.idle":"2022-12-10T15:35:33.2377Z","shell.execute_reply.started":"2022-12-10T15:35:25.320753Z","shell.execute_reply":"2022-12-10T15:35:33.236373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.umap(cite_raw, color=[\"cell_type\", \"G1S+2M_genes_Tirosh_mean\", \"n_genes_counts\", \"read_depth\", \"day\", \"donor\"], frameon=False, ncols=3)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:36:54.491498Z","iopub.execute_input":"2022-12-10T15:36:54.492001Z","iopub.status.idle":"2022-12-10T15:37:00.473337Z","shell.execute_reply.started":"2022-12-10T15:36:54.491965Z","shell.execute_reply":"2022-12-10T15:37:00.472308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalise data for differential expression","metadata":{}},{"cell_type":"code","source":"# Для отрисовки и дифференциальной экспрессии\nsc.pp.normalize_total(adata, target_sum=1e4)\nsc.pp.log1p(adata)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:37:00.475224Z","iopub.execute_input":"2022-12-10T15:37:00.475813Z","iopub.status.idle":"2022-12-10T15:37:16.893892Z","shell.execute_reply.started":"2022-12-10T15:37:00.475776Z","shell.execute_reply":"2022-12-10T15:37:16.892581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Differential expression between annotated cell types","metadata":{}},{"cell_type":"markdown","source":"## Wilcoxon","metadata":{}},{"cell_type":"code","source":"sc.tl.rank_genes_groups(adata, groupby=\"cell_type\", method=\"wilcoxon\", key_added=\"rank_genes_groups_cell_type\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:37:16.895707Z","iopub.execute_input":"2022-12-10T15:37:16.896242Z","iopub.status.idle":"2022-12-10T15:46:10.773232Z","shell.execute_reply.started":"2022-12-10T15:37:16.896185Z","shell.execute_reply":"2022-12-10T15:46:10.771767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.rank_genes_groups(adata, key=\"rank_genes_groups_cell_type\", gene_symbols=\"gene_names\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:24:32.819669Z","iopub.execute_input":"2022-12-10T16:24:32.82013Z","iopub.status.idle":"2022-12-10T16:24:34.795028Z","shell.execute_reply.started":"2022-12-10T16:24:32.820094Z","shell.execute_reply":"2022-12-10T16:24:34.793897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.rank_genes_groups_heatmap(adata, key=\"rank_genes_groups_cell_type\", gene_symbols=\"gene_names\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T17:09:38.271909Z","iopub.execute_input":"2022-12-10T17:09:38.272339Z","iopub.status.idle":"2022-12-10T17:09:44.941709Z","shell.execute_reply.started":"2022-12-10T17:09:38.272301Z","shell.execute_reply":"2022-12-10T17:09:44.94059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.rank_genes_groups_matrixplot(\n    adata,\n    n_genes=5, key=\"rank_genes_groups_cell_type\",\n    values_to_plot=\"logfoldchanges\",\n    cmap='bwr',\n    vmin=-4,\n    vmax=4,\n    min_logfoldchange=2,\n    colorbar_title='log fold change',\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T17:16:15.657648Z","iopub.execute_input":"2022-12-10T17:16:15.658134Z","iopub.status.idle":"2022-12-10T17:16:24.601862Z","shell.execute_reply.started":"2022-12-10T17:16:15.658099Z","shell.execute_reply":"2022-12-10T17:16:24.60061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cell_type in adata.obs.cell_type.unique():\n    de_df = sc.get.rank_genes_groups_df(adata, group=cell_type, key=\"rank_genes_groups_cell_type\")\n    de_df.to_csv(f\"wilcoxon_{cell_type}_vs_rest.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T17:22:48.386616Z","iopub.execute_input":"2022-12-10T17:22:48.387093Z","iopub.status.idle":"2022-12-10T17:22:52.4699Z","shell.execute_reply.started":"2022-12-10T17:22:48.387043Z","shell.execute_reply":"2022-12-10T17:22:52.469002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## logreg","metadata":{}},{"cell_type":"code","source":"sc.tl.rank_genes_groups(adata, groupby=\"cell_type\", method=\"logreg\", key_added=\"rank_genes_groups_logreg_cell_type\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:27:36.696571Z","iopub.execute_input":"2022-12-10T16:27:36.697131Z","iopub.status.idle":"2022-12-10T16:44:39.792449Z","shell.execute_reply.started":"2022-12-10T16:27:36.697081Z","shell.execute_reply":"2022-12-10T16:44:39.790755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.rank_genes_groups(adata, key=\"rank_genes_groups_logreg_cell_type\", gene_symbols=\"gene_names\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:44:39.797097Z","iopub.execute_input":"2022-12-10T16:44:39.797676Z","iopub.status.idle":"2022-12-10T16:44:41.961852Z","shell.execute_reply.started":"2022-12-10T16:44:39.79762Z","shell.execute_reply":"2022-12-10T16:44:41.960432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Leiden clustering","metadata":{}},{"cell_type":"code","source":"for resolution in [0.2, 0.5, 1, 2]:\n    sc.tl.leiden(adata, resolution=resolution, key_added=f\"leiden_{resolution}\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:46:11.230829Z","iopub.execute_input":"2022-12-10T15:46:11.231171Z","iopub.status.idle":"2022-12-10T15:54:18.361457Z","shell.execute_reply.started":"2022-12-10T15:46:11.23114Z","shell.execute_reply":"2022-12-10T15:54:18.360174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cluster_keys = [\"cell_type\", \"G1S+2M_genes_Tirosh_mean\", \"read_depth\", \"day\", \"donor\"] + [f\"leiden_{resolution}\" for resolution in [0.2, 0.5]]\nsc.pl.umap(\n    adata,\n    color=cluster_keys,\n    frameon=False,\n#     legend_loc=\"on data\",\n    legend_fontoutline=2,\n    ncols=3\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:54:18.36306Z","iopub.execute_input":"2022-12-10T15:54:18.36348Z","iopub.status.idle":"2022-12-10T15:54:26.159121Z","shell.execute_reply.started":"2022-12-10T15:54:18.363442Z","shell.execute_reply":"2022-12-10T15:54:26.15791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.tl.dendrogram(adata, groupby=\"leiden_0.2\", n_pcs=15, use_rep=\"X_pca\")\nsc.pl.dendrogram(adata, groupby=\"leiden_0.2\", orientation=\"left\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:57:59.821399Z","iopub.execute_input":"2022-12-10T16:57:59.821995Z","iopub.status.idle":"2022-12-10T16:58:00.120028Z","shell.execute_reply.started":"2022-12-10T16:57:59.82195Z","shell.execute_reply":"2022-12-10T16:58:00.118833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.tl.dendrogram(adata, groupby=\"cell_type\", n_pcs=15, use_rep=\"X_pca\")\nsc.pl.dendrogram(adata, groupby=\"cell_type\", orientation=\"left\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T17:03:17.471407Z","iopub.execute_input":"2022-12-10T17:03:17.471954Z","iopub.status.idle":"2022-12-10T17:03:17.750465Z","shell.execute_reply.started":"2022-12-10T17:03:17.47191Z","shell.execute_reply":"2022-12-10T17:03:17.749139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cluster_keys = [\"cell_type\", ] + [f\"leiden_{resolution}\" for resolution in [0.2,]]\nsc.pl.umap(\n    adata,\n    color=cluster_keys,\n    frameon=False,\n    legend_loc=\"on data\",\n    legend_fontoutline=2,\n    ncols=3\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:49:58.986625Z","iopub.execute_input":"2022-12-10T16:49:58.987692Z","iopub.status.idle":"2022-12-10T16:50:01.165465Z","shell.execute_reply.started":"2022-12-10T16:49:58.987642Z","shell.execute_reply":"2022-12-10T16:50:01.16446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Differential expression between leiden clusters","metadata":{}},{"cell_type":"code","source":"sc.tl.rank_genes_groups(adata, groupby=\"leiden_0.2\", method=\"wilcoxon\", key_added=\"rank_genes_groups_leiden_0.2\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T15:54:31.55072Z","iopub.execute_input":"2022-12-10T15:54:31.551163Z","iopub.status.idle":"2022-12-10T16:04:00.479087Z","shell.execute_reply.started":"2022-12-10T15:54:31.551125Z","shell.execute_reply":"2022-12-10T16:04:00.477646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.rank_genes_groups(adata, key=\"rank_genes_groups_leiden_0.2\", gene_symbols=\"gene_names\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:44:41.963422Z","iopub.execute_input":"2022-12-10T16:44:41.963835Z","iopub.status.idle":"2022-12-10T16:44:44.982038Z","shell.execute_reply.started":"2022-12-10T16:44:41.963798Z","shell.execute_reply":"2022-12-10T16:44:44.980759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cluster in adata.obs[\"leiden_0.2\"].unique():\n    de_df = sc.get.rank_genes_groups_df(adata, group=cluster, key=\"rank_genes_groups_leiden_0.2\")\n    de_df.to_csv(f\"wilcoxon_leiden_0.2_{cluster}_vs_rest.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T17:25:30.303901Z","iopub.execute_input":"2022-12-10T17:25:30.304378Z","iopub.status.idle":"2022-12-10T17:25:36.116923Z","shell.execute_reply.started":"2022-12-10T17:25:30.304336Z","shell.execute_reply":"2022-12-10T17:25:36.115943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.tl.rank_genes_groups(adata, groupby=\"leiden_0.2\", method=\"wilcoxon\", key_added=\"rank_genes_groups_leiden_0.2_5_6vs1\", groups=[\"5\", \"6\"], reference=\"1\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:09:56.971817Z","iopub.execute_input":"2022-12-10T16:09:56.972332Z","iopub.status.idle":"2022-12-10T16:13:36.797991Z","shell.execute_reply.started":"2022-12-10T16:09:56.972294Z","shell.execute_reply":"2022-12-10T16:13:36.795531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.pl.rank_genes_groups(adata, key=\"rank_genes_groups_leiden_0.2_5_6vs1\", gene_symbols=\"gene_names\")","metadata":{"execution":{"iopub.status.busy":"2022-12-10T16:44:44.985093Z","iopub.execute_input":"2022-12-10T16:44:44.985898Z","iopub.status.idle":"2022-12-10T16:44:45.666105Z","shell.execute_reply.started":"2022-12-10T16:44:44.985845Z","shell.execute_reply":"2022-12-10T16:44:45.665162Z"},"trusted":true},"execution_count":null,"outputs":[]}]}