{"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":"## This notebook just running R (under a python kaggle kernal) and analysis the results in python\n\n### Files were prepared in another notebook: https://www.kaggle.com/code/awater1223/op2-02-pseudobulk-v2/notebook","metadata":{}},{"cell_type":"code","source":"!pip install scanpy","metadata":{"_kg_hide-output":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## download the R scripts\n!git clone https://github.com/openproblems-bio/neurips-2023-scripts","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:36:29.817976Z","iopub.execute_input":"2023-10-09T03:36:29.818380Z","iopub.status.idle":"2023-10-09T03:36:30.941967Z","shell.execute_reply.started":"2023-10-09T03:36:29.818349Z","shell.execute_reply":"2023-10-09T03:36:30.940793Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nimport pandas as pd\n\nfrom IPython.display import IFrame","metadata":{"execution":{"iopub.status.busy":"2023-10-09T06:07:51.326612Z","iopub.execute_input":"2023-10-09T06:07:51.326963Z","iopub.status.idle":"2023-10-09T06:07:51.332766Z","shell.execute_reply.started":"2023-10-09T06:07:51.326936Z","shell.execute_reply":"2023-10-09T06:07:51.331244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Setup environment for R","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:26:02.324881Z","iopub.execute_input":"2023-10-09T03:26:02.325739Z","iopub.status.idle":"2023-10-09T03:26:02.331648Z","shell.execute_reply.started":"2023-10-09T03:26:02.325695Z","shell.execute_reply":"2023-10-09T03:26:02.330747Z"}}},{"cell_type":"code","source":"r_dependencies = '''\n- r-base=4.3.1=h29c4799_3\n- r-base64enc=0.1_3=r43h57805ef_1006\n- r-cli=3.6.1=r43ha503ecb_1\n- r-cpp11=0.4.6=r43hc72bb7e_0\n- r-crayon=1.5.2=r43hc72bb7e_2\n- r-digest=0.6.33=r43ha503ecb_0\n- r-ellipsis=0.3.2=r43h57805ef_2\n- r-evaluate=0.21=r43hc72bb7e_1\n- r-fansi=1.0.4=r43h57805ef_1\n- r-fastmap=1.1.1=r43ha503ecb_1\n- r-glue=1.6.2=r43h57805ef_2\n- r-htmltools=0.5.6=r43ha503ecb_0\n- r-irdisplay=1.1=r43hd8ed1ab_2\n- r-irkernel=1.3.2=r43h785f33e_1\n- r-jsonlite=1.8.7=r43h57805ef_0\n- r-lifecycle=1.0.3=r43hc72bb7e_2\n- r-pbdzmq=0.3_9=r43ha81a24b_1\n- r-pillar=1.9.0=r43hc72bb7e_1\n- r-ragg=1.2.5=r43h85cdef0_2\n- r-repr=1.1.6=r43h785f33e_1\n- r-rlang=1.1.1=r43ha503ecb_1\n- r-systemfonts=1.0.4=r43haf97adc_2\n- r-textshaping=0.3.6=r43h24cd192_6\n- r-utf8=1.2.3=r43h57805ef_1\n- r-uuid=1.1_1=r43h57805ef_0\n- r-vctrs=0.6.3=r43ha503ecb_0\n'''\n\n\npackages = ' '.join(\n    pd.Series(\n        r_dependencies.replace('- ', '').split('\\n')[1:-1]\n    ).apply(lambda x: x.split('=')[0]).to_list()\n) + ' r-optparse r-reticulate r-rcpp gcc_impl_linux-64=13'\n\nos.system('conda install -c conda-forge -y {}'.format(packages))","metadata":{"_kg_hide-output":true,"scrolled":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2023-10-09T06:02:00.619755Z","iopub.execute_input":"2023-10-09T06:02:00.620129Z","iopub.status.idle":"2023-10-09T06:02:33.697207Z","shell.execute_reply.started":"2023-10-09T06:02:00.620099Z","shell.execute_reply":"2023-10-09T06:02:33.696088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r_install = '''\ninstall.packages(\"remotes\", repos = \"http://cran.us.r-project.org\", quiet=TRUE)\ninstall.packages(\"BiocManager\", repos = \"http://cran.us.r-project.org\", quiet=TRUE)\nBiocManager::install(\"edgeR\")\nremotes::install_cran(c(\"anndata\"), repos = \"https://cran.rstudio.com\")\n# install.packages(\"optparse\", repos = \"http://cran.us.r-project.org\", quiet=TRUE)   # package ‘optparse’ is not available for this version of R\nremotes::install_version(\"reticulate\", version = \"1.22\", repos = \"http://cran.us.r-project.org\")\n'''\n\nwith open('r_install.r', 'w') as f:\n    print (r_install, file=f)","metadata":{"_kg_hide-input":false,"scrolled":true,"_kg_hide-output":false},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! `which Rscript` r_install.r","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Limma code - v2 - ground truth pseudobulk","metadata":{}},{"cell_type":"code","source":"## mkdirs\n\ncell_rename_dict = {\n    'NK cells' : 'NK',\n    'T cells CD4+' : 'T4',\n    'T cells CD8+' : 'T8',\n    'T regulatory cells' : 'Treg',\n    'B cells' : 'B',\n    'Myeloid cells' : 'Myeloid',\n}\n\nfor rename_cell_type in cell_rename_dict.values():\n    cell_type_working_dir = 'cell_type_{}'.format(rename_cell_type)\n    os.makedirs(cell_type_working_dir, exist_ok=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## replaced the input path for kaggle\n! sed 's!--input_h5ad cell_type!--input_h5ad /kaggle/input/op2-02-pseudobulk-v2/cell_type!' /kaggle/input/op2-02-pseudobulk-v2/r_code.sh > ./r_code.sh","metadata":{"execution":{"iopub.status.busy":"2023-10-09T03:36:03.446362Z","iopub.execute_input":"2023-10-09T03:36:03.446733Z","iopub.status.idle":"2023-10-09T03:36:04.581561Z","shell.execute_reply.started":"2023-10-09T03:36:03.446705Z","shell.execute_reply":"2023-10-09T03:36:04.580337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! bash r_code.sh","metadata":{"_kg_hide-output":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir v2\n!mv cell_type* v2","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## View the voom plot","metadata":{}},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_B/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_Myeloid/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_NK/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_T4/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_T8/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_Treg/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"## Run Limma code - v3 - the pseudobulk","metadata":{"execution":{"iopub.status.busy":"2023-10-19T02:26:17.217521Z","iopub.execute_input":"2023-10-19T02:26:17.217893Z","iopub.status.idle":"2023-10-19T02:26:17.223727Z","shell.execute_reply.started":"2023-10-19T02:26:17.217864Z","shell.execute_reply":"2023-10-19T02:26:17.222390Z"}}},{"cell_type":"code","source":"## mkdirs\n\ncell_rename_dict = {\n    'NK cells' : 'NK',\n    'T cells CD4+' : 'T4',\n    'T cells CD8+' : 'T8',\n    'T regulatory cells' : 'Treg',\n    'B cells' : 'B',\n    'Myeloid cells' : 'Myeloid',\n}\n\nfor rename_cell_type in cell_rename_dict.values():\n    cell_type_working_dir = 'cell_type_{}'.format(rename_cell_type)\n    os.makedirs(cell_type_working_dir, exist_ok=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## replaced the input path for kaggle\n! sed 's!--input_h5ad cell_type!--input_h5ad /kaggle/input/op2-02-pseudobulk-v3/cell_type!' /kaggle/input/op2-02-pseudobulk-v3/r_code.sh > ./r_code.sh","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! bash r_code.sh","metadata":{"_kg_hide-output":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir v3\n!mv cell_type* v3","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"IFrame(\"./v3/cell_type_B/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v3/cell_type_Myeloid/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v3/cell_type_NK/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v3/cell_type_T4/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v3/cell_type_T8/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v3/cell_type_Treg/voom.pdf\", width=500, height=500)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"!rm -rf /kaggle/working/neurips-2023-scripts","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}