{"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":"code","source":"%%capture\n!pip3 install scanpy[leiden]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-27T14:17:10.504578Z","iopub.execute_input":"2023-02-27T14:17:10.505814Z","iopub.status.idle":"2023-02-27T14:17:30.625439Z","shell.execute_reply.started":"2023-02-27T14:17:10.505675Z","shell.execute_reply":"2023-02-27T14:17:30.623401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport requests ","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:17:30.629292Z","iopub.execute_input":"2023-02-27T14:17:30.629927Z","iopub.status.idle":"2023-02-27T14:17:30.638492Z","shell.execute_reply.started":"2023-02-27T14:17:30.629860Z","shell.execute_reply":"2023-02-27T14:17:30.636656Z"},"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-02-27T14:17:30.641139Z","iopub.execute_input":"2023-02-27T14:17:30.641970Z","iopub.status.idle":"2023-02-27T14:17:30.656776Z","shell.execute_reply.started":"2023-02-27T14:17:30.641909Z","shell.execute_reply":"2023-02-27T14:17:30.654763Z"},"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\nfrom tqdm import tqdm\n\nsc.settings.verbosity = 3\nsc.set_figure_params(dpi=150)\nsns.set_style(\"ticks\")","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:17:30.660912Z","iopub.execute_input":"2023-02-27T14:17:30.661477Z","iopub.status.idle":"2023-02-27T14:17:34.134290Z","shell.execute_reply.started":"2023-02-27T14:17:30.661428Z","shell.execute_reply":"2023-02-27T14:17:34.132652Z"},"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-02-27T14:17:34.136268Z","iopub.execute_input":"2023-02-27T14:17:34.137168Z","iopub.status.idle":"2023-02-27T14:17:34.776893Z","shell.execute_reply.started":"2023-02-27T14:17:34.137107Z","shell.execute_reply":"2023-02-27T14:17:34.775669Z"},"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-02-27T14:17:34.778094Z","iopub.execute_input":"2023-02-27T14:17:34.779567Z","iopub.status.idle":"2023-02-27T14:17:34.788730Z","shell.execute_reply.started":"2023-02-27T14:17:34.779502Z","shell.execute_reply":"2023-02-27T14:17:34.787361Z"},"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()\n#adata = cite_raw","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:17:34.790611Z","iopub.execute_input":"2023-02-27T14:17:34.791485Z","iopub.status.idle":"2023-02-27T14:18:47.721907Z","shell.execute_reply.started":"2023-02-27T14:17:34.791416Z","shell.execute_reply":"2023-02-27T14:18:47.718984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cite_targets = sc.read_h5ad(FP_CITE_RAW_TRAIN_TARGETS)\ncite_targets_df=pd.DataFrame(columns=cite_targets.var_names, \n                             index=cite_targets.obs_names, \n                             data=cite_targets.X.todense())\ncite_raw.obs=cite_raw.obs.join(cite_targets_df)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:18:47.726636Z","iopub.execute_input":"2023-02-27T14:18:47.727312Z","iopub.status.idle":"2023-02-27T14:18:49.200968Z","shell.execute_reply.started":"2023-02-27T14:18:47.727262Z","shell.execute_reply":"2023-02-27T14:18:49.199261Z"},"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(cite_raw, n_top_genes=3000, batch_key=\"donor\")\nsc.experimental.pp.normalize_pearson_residuals_pca(cite_raw)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:18:49.202964Z","iopub.execute_input":"2023-02-27T14:18:49.203379Z","iopub.status.idle":"2023-02-27T14:23:33.048663Z","shell.execute_reply.started":"2023-02-27T14:18:49.203345Z","shell.execute_reply":"2023-02-27T14:23:33.046675Z"},"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-02-27T14:23:33.057264Z","iopub.execute_input":"2023-02-27T14:23:33.057805Z","iopub.status.idle":"2023-02-27T14:23:38.530574Z","shell.execute_reply.started":"2023-02-27T14:23:33.057759Z","shell.execute_reply":"2023-02-27T14:23:38.528670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see, there is a strong dispersion of MkP cells by PC2. Let us look at it more closely.","metadata":{}},{"cell_type":"code","source":"sc.pl.pca(cite_raw[cite_raw.obs['cell_type']=='MkP'], color=[ \"donor\", \"day\", \"n_genes_counts\"])","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:23:38.532806Z","iopub.execute_input":"2023-02-27T14:23:38.533491Z","iopub.status.idle":"2023-02-27T14:23:39.948387Z","shell.execute_reply.started":"2023-02-27T14:23:38.533451Z","shell.execute_reply":"2023-02-27T14:23:39.945467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"PC2 has correlation with the day and negative correlation with number of genes expressed. Perhaps, PC2 shows the differentiation of MkP cells.","metadata":{}},{"cell_type":"markdown","source":"Let us show an approximation line and get a new component, by projecting on this line","metadata":{}},{"cell_type":"code","source":"z = np.polyfit(cite_raw[cite_raw.obs['cell_type']=='MkP'].obsm['X_pca'][:,0],\n               cite_raw[cite_raw.obs['cell_type']=='MkP'].obsm['X_pca'][:,1], 1)\np = np.poly1d(z)\nsns.scatterplot(cite_raw.obsm['X_pca'][:,0],\n                cite_raw.obsm['X_pca'][:,1], \n                hue=cite_raw.obs['cell_type'], s=2)\nplt.plot([-30,200],p([-30,200]), color='black')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:23:39.950795Z","iopub.execute_input":"2023-02-27T14:23:39.951455Z","iopub.status.idle":"2023-02-27T14:23:43.885378Z","shell.execute_reply.started":"2023-02-27T14:23:39.951409Z","shell.execute_reply":"2023-02-27T14:23:43.883217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"at=np.arctan(z[0])\nsns.scatterplot(cite_raw.obsm['X_pca'][:,0]*np.cos(at)+cite_raw.obsm['X_pca'][:,1]*np.sin(at),\n            -cite_raw.obsm['X_pca'][:,0]*np.sin(at)+cite_raw.obsm['X_pca'][:,1]*np.cos(at),\n                s=2, hue=cite_raw.obs['cell_type'])\ncite_raw.obs['MkP_disp_approx_line']=cite_raw.obsm['X_pca'][:,0]*np.cos(at)+cite_raw.obsm['X_pca'][:,1]*np.sin(at)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:23:43.887900Z","iopub.execute_input":"2023-02-27T14:23:43.888441Z","iopub.status.idle":"2023-02-27T14:23:52.101144Z","shell.execute_reply.started":"2023-02-27T14:23:43.888402Z","shell.execute_reply":"2023-02-27T14:23:52.099070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cell cycle","metadata":{}},{"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":"2023-02-27T14:23:52.103862Z","iopub.execute_input":"2023-02-27T14:23:52.105954Z","iopub.status.idle":"2023-02-27T14:23:52.127953Z","shell.execute_reply.started":"2023-02-27T14:23:52.105799Z","shell.execute_reply":"2023-02-27T14:23:52.124497Z"},"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-02-27T14:23:52.131903Z","iopub.execute_input":"2023-02-27T14:23:52.132571Z","iopub.status.idle":"2023-02-27T14:23:52.178500Z","shell.execute_reply.started":"2023-02-27T14:23:52.132521Z","shell.execute_reply":"2023-02-27T14:23:52.176138Z"},"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":"2023-02-27T14:23:52.180496Z","iopub.execute_input":"2023-02-27T14:23:52.180968Z","iopub.status.idle":"2023-02-27T14:23:59.083224Z","shell.execute_reply.started":"2023-02-27T14:23:52.180932Z","shell.execute_reply":"2023-02-27T14:23:59.080258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyze MkP","metadata":{}},{"cell_type":"code","source":"cite_raw=cite_raw[cite_raw.obs['cell_type']=='MkP']","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:23:59.086095Z","iopub.execute_input":"2023-02-27T14:23:59.087614Z","iopub.status.idle":"2023-02-27T14:23:59.324907Z","shell.execute_reply.started":"2023-02-27T14:23:59.087542Z","shell.execute_reply":"2023-02-27T14:23:59.323067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,10))\nsns.scatterplot(data=cite_raw.obs, x=\"G1S_genes_Tirosh_mean\", y=\"G2M_genes_Tirosh_mean\", \n                hue='MkP_disp_approx_line', s=5, palette='viridis')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:23:59.327319Z","iopub.execute_input":"2023-02-27T14:23:59.329060Z","iopub.status.idle":"2023-02-27T14:24:01.842753Z","shell.execute_reply.started":"2023-02-27T14:23:59.328990Z","shell.execute_reply":"2023-02-27T14:24:01.840427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('G1S_genes_Tirosh_mean correlation with MkP_disp',\n      np.corrcoef(cite_raw.obs['MkP_disp_approx_line'],cite_raw.obs['G1S_genes_Tirosh_mean'])[0,1])\nprint('G2M_genes_Tirosh_mean correlation with MkP_disp',\n      np.corrcoef(cite_raw.obs['MkP_disp_approx_line'],cite_raw.obs['G2M_genes_Tirosh_mean'])[0,1])","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:24:01.845501Z","iopub.execute_input":"2023-02-27T14:24:01.846056Z","iopub.status.idle":"2023-02-27T14:24:01.857457Z","shell.execute_reply.started":"2023-02-27T14:24:01.846017Z","shell.execute_reply":"2023-02-27T14:24:01.855630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"High levels on MkP_disp component fall into G0","metadata":{}},{"cell_type":"markdown","source":"Look for correlations between the MkP_disp component and expressed genes","metadata":{}},{"cell_type":"code","source":"corrs=[]\nfor i in tqdm(range(len(cite_raw.var_names))):\n    corrs.append(np.corrcoef(cite_raw.obs['MkP_disp_approx_line'],cite_raw.X[:,i].todense().squeeze())[0,1])\ncite_raw.var[\"corrs_MkP_disp\"] = corrs","metadata":{"execution":{"iopub.status.busy":"2023-02-27T14:24:01.860717Z","iopub.execute_input":"2023-02-27T14:24:01.861573Z","iopub.status.idle":"2023-02-27T15:57:48.019713Z","shell.execute_reply.started":"2023-02-27T14:24:01.861523Z","shell.execute_reply":"2023-02-27T15:57:48.018354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(range(200),cite_raw.var[\"corrs_MkP_disp\"].sort_values(ascending=False)[:200])\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:57:48.021939Z","iopub.execute_input":"2023-02-27T15:57:48.022401Z","iopub.status.idle":"2023-02-27T15:57:48.445762Z","shell.execute_reply.started":"2023-02-27T15:57:48.022360Z","shell.execute_reply":"2023-02-27T15:57:48.444590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Look for correlations between the MkP_disp component and CD proteins","metadata":{}},{"cell_type":"code","source":"display(cite_raw.var[\"corrs_MkP_disp\"].sort_values(ascending=False)[:50])","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:57:48.447223Z","iopub.execute_input":"2023-02-27T15:57:48.447564Z","iopub.status.idle":"2023-02-27T15:57:48.463250Z","shell.execute_reply.started":"2023-02-27T15:57:48.447533Z","shell.execute_reply":"2023-02-27T15:57:48.462069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrs=[]\nfor i in tqdm(cite_targets.var_names):\n    corrs.append(np.corrcoef(cite_raw[~cite_raw.obs['CD45'].isna()].obsm['X_pca'][:,1],\n                             cite_raw[~cite_raw.obs['CD45'].isna()].obs[i])[0,1])\n    \ncite_targets.var[\"corr_MkP_disp_proteins\"]=corrs","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:57:48.464793Z","iopub.execute_input":"2023-02-27T15:57:48.465180Z","iopub.status.idle":"2023-02-27T15:57:51.048340Z","shell.execute_reply.started":"2023-02-27T15:57:48.465149Z","shell.execute_reply":"2023-02-27T15:57:51.047264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(cite_targets.var[\"corr_MkP_disp_proteins\"].sort_values(ascending=False)[:50])","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:57:51.049536Z","iopub.execute_input":"2023-02-27T15:57:51.049868Z","iopub.status.idle":"2023-02-27T15:57:51.059752Z","shell.execute_reply.started":"2023-02-27T15:57:51.049820Z","shell.execute_reply":"2023-02-27T15:57:51.058672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Genes enrichment analysis","metadata":{}},{"cell_type":"code","source":"!pip install gseapy\nimport gseapy as gp","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:57:51.061365Z","iopub.execute_input":"2023-02-27T15:57:51.062064Z","iopub.status.idle":"2023-02-27T15:58:04.150310Z","shell.execute_reply.started":"2023-02-27T15:57:51.062030Z","shell.execute_reply":"2023-02-27T15:58:04.149050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = gp.get_library_name()\nprint(names)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:58:04.153268Z","iopub.execute_input":"2023-02-27T15:58:04.153764Z","iopub.status.idle":"2023-02-27T15:58:04.437390Z","shell.execute_reply.started":"2023-02-27T15:58:04.153716Z","shell.execute_reply":"2023-02-27T15:58:04.436345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_genes=list(cite_raw[cite_raw.var[\"corrs_MkP_disp\"].sort_values(ascending=False)[:50]].var['gene_names'])","metadata":{"execution":{"iopub.status.busy":"2023-02-27T15:58:04.438646Z","iopub.execute_input":"2023-02-27T15:58:04.438978Z","iopub.status.idle":"2023-02-27T15:58:04.460982Z","shell.execute_reply.started":"2023-02-27T15:58:04.438950Z","shell.execute_reply":"2023-02-27T15:58:04.459991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names=['KEGG_2021_Human','GO_Biological_Process_2021'  ]","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:11:16.402484Z","iopub.execute_input":"2023-02-27T17:11:16.402988Z","iopub.status.idle":"2023-02-27T17:11:16.408653Z","shell.execute_reply.started":"2023-02-27T17:11:16.402951Z","shell.execute_reply":"2023-02-27T17:11:16.407268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enr = gp.enrichr(\n    gene_list=corr_genes, \n    gene_sets= names, # , 'GO_Biological_Process_2021', 'MSigDB_Oncogenic_Signatures',\n    organism='human',\n    outdir=None,\n)\ncol = 'Adjusted P-value' #P-value # Adjusted P-value - with Bonferoni type correction for checking multiple lists, while just \"P-value\" - is without\ndisplay( enr.results.sort_values(col).head(50) )","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:11:22.809900Z","iopub.execute_input":"2023-02-27T17:11:22.810345Z","iopub.status.idle":"2023-02-27T17:12:23.230023Z","shell.execute_reply.started":"2023-02-27T17:11:22.810310Z","shell.execute_reply":"2023-02-27T17:12:23.227736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It does not work, make it by hand","metadata":{}},{"cell_type":"code","source":"sets={}\nfor name in names:\n    r = requests.get('https://maayanlab.cloud/Enrichr/geneSetLibrary?mode=text&libraryName='+name)\n    for s in r.content.decode().split('\\n'):\n        a=s.split('\\t')\n        sets[a[0]]=a[1:]","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:12:32.719238Z","iopub.execute_input":"2023-02-27T17:12:32.720026Z","iopub.status.idle":"2023-02-27T17:12:41.986519Z","shell.execute_reply.started":"2023-02-27T17:12:32.719990Z","shell.execute_reply":"2023-02-27T17:12:41.985188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(sets))","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:12:41.988975Z","iopub.execute_input":"2023-02-27T17:12:41.989453Z","iopub.status.idle":"2023-02-27T17:12:41.996461Z","shell.execute_reply.started":"2023-02-27T17:12:41.989410Z","shell.execute_reply":"2023-02-27T17:12:41.995179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bm = gp.biomart.Biomart()\ndf = bm.query(\n                dataset='hsapiens_gene_ensembl',\n                attributes=[\"ensembl_gene_id\", \"external_gene_name\", \"entrezgene_id\"],\n                filename=\"hsapiens_gene_ensembl.background.genes.txt\"\n                )\nbgenes=pd.read_csv(\"hsapiens_gene_ensembl.background.genes.txt\", sep='\\t')","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:12:41.997997Z","iopub.execute_input":"2023-02-27T17:12:41.999045Z","iopub.status.idle":"2023-02-27T17:12:45.647786Z","shell.execute_reply.started":"2023-02-27T17:12:41.998986Z","shell.execute_reply":"2023-02-27T17:12:45.646441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bg=bgenes.external_gene_name.unique()\nlen(bg)","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:12:45.649714Z","iopub.execute_input":"2023-02-27T17:12:45.650169Z","iopub.status.idle":"2023-02-27T17:12:45.664976Z","shell.execute_reply.started":"2023-02-27T17:12:45.650138Z","shell.execute_reply":"2023-02-27T17:12:45.663815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enr = gp.enrichr(\n    gene_list=corr_genes, \n    gene_sets= sets, \n    organism='human',\n    background=bg,\n    outdir=None,\n)\ncol = 'Adjusted P-value'\ndisplay( enr.results.sort_values(col).head(50) )\n","metadata":{"execution":{"iopub.status.busy":"2023-02-27T17:12:45.666334Z","iopub.execute_input":"2023-02-27T17:12:45.666650Z","iopub.status.idle":"2023-02-27T17:12:56.601702Z","shell.execute_reply.started":"2023-02-27T17:12:45.666622Z","shell.execute_reply":"2023-02-27T17:12:56.600534Z"},"trusted":true},"execution_count":null,"outputs":[]}]}