{"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,"trusted":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":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-27T08:53:39.947962Z","iopub.execute_input":"2023-10-27T08:53:39.948439Z","iopub.status.idle":"2023-10-27T08:53:41.797273Z","shell.execute_reply.started":"2023-10-27T08:53:39.948403Z","shell.execute_reply":"2023-10-27T08:53:41.795753Z"},"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-27T08:44:21.900541Z","iopub.execute_input":"2023-10-27T08:44:21.901013Z","iopub.status.idle":"2023-10-27T08:44:22.397899Z","shell.execute_reply.started":"2023-10-27T08:44:21.900973Z","shell.execute_reply":"2023-10-27T08:44:22.396421Z"},"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-27T08:57:47.921916Z","iopub.execute_input":"2023-10-27T08:57:47.922451Z","iopub.status.idle":"2023-10-27T09:00:48.862706Z","shell.execute_reply.started":"2023-10-27T08:57:47.922407Z","shell.execute_reply":"2023-10-27T09:00:48.861519Z"},"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\")\ninstall.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":{"iopub.status.busy":"2023-10-27T08:50:30.538045Z","iopub.execute_input":"2023-10-27T08:50:30.538516Z","iopub.status.idle":"2023-10-27T08:50:30.546384Z","shell.execute_reply.started":"2023-10-27T08:50:30.538471Z","shell.execute_reply":"2023-10-27T08:50:30.545145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! `which Rscript` r_install.r","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-10-27T08:57:28.739915Z","iopub.execute_input":"2023-10-27T08:57:28.740332Z","iopub.status.idle":"2023-10-27T08:57:29.894468Z","shell.execute_reply.started":"2023-10-27T08:57:28.740299Z","shell.execute_reply":"2023-10-27T08:57:29.892855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Run Limma code - v2 - ground truth pseudobulk","metadata":{}},{"cell_type":"code","source":"import os \ndef remove_folder_contents(folder):\n    for the_file in os.listdir(folder):\n        file_path = os.path.join(folder, the_file)\n        try:\n            if os.path.isfile(file_path):\n                os.unlink(file_path)\n            elif os.path.isdir(file_path):\n                remove_folder_contents(file_path)\n                os.rmdir(file_path)\n        except Exception as e:\n            print(e)\n\nfolder_path = '/kaggle/working'\nremove_folder_contents(folder_path)\nos.rmdir(folder_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.663541Z","iopub.execute_input":"2023-10-27T08:50:31.664712Z","iopub.status.idle":"2023-10-27T08:50:31.747589Z","shell.execute_reply.started":"2023-10-27T08:50:31.664648Z","shell.execute_reply":"2023-10-27T08:50:31.744895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from csv import reader\ncell_rename_all = []\nwith open('/kaggle/input/lincs-data/data_2_r/cell_names.dat') as out_f:\n    for el in reader(out_f):\n        cell_rename_all.append(el[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:52:58.280688Z","iopub.execute_input":"2023-10-27T08:52:58.281242Z","iopub.status.idle":"2023-10-27T08:52:58.299037Z","shell.execute_reply.started":"2023-10-27T08:52:58.2812Z","shell.execute_reply":"2023-10-27T08:52:58.297697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cell_rename_all)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:53:00.815504Z","iopub.execute_input":"2023-10-27T08:53:00.816706Z","iopub.status.idle":"2023-10-27T08:53:00.821989Z","shell.execute_reply.started":"2023-10-27T08:53:00.816654Z","shell.execute_reply":"2023-10-27T08:53:00.821062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## mkdirs\nfor rename_cell_type in cell_rename_all:\n    cell_type_working_dir = 'cell_type_{}'.format(rename_cell_type)\n    os.makedirs(cell_type_working_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:53:02.637297Z","iopub.execute_input":"2023-10-27T08:53:02.638218Z","iopub.status.idle":"2023-10-27T08:53:02.645862Z","shell.execute_reply.started":"2023-10-27T08:53:02.63817Z","shell.execute_reply":"2023-10-27T08:53:02.644591Z"},"trusted":true},"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/lincs-data/data_2_r/cell_type!' /kaggle/input/r-code-run/r_code.sh > ./r_code.sh","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:53:04.828131Z","iopub.execute_input":"2023-10-27T08:53:04.828541Z","iopub.status.idle":"2023-10-27T08:53:05.982772Z","shell.execute_reply.started":"2023-10-27T08:53:04.828507Z","shell.execute_reply":"2023-10-27T08:53:05.981493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" !chmod u+x  /kaggle/working/neurips-2023-scripts/limma_fit.r","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:56:57.186062Z","iopub.execute_input":"2023-10-27T08:56:57.186544Z","iopub.status.idle":"2023-10-27T08:56:58.291513Z","shell.execute_reply.started":"2023-10-27T08:56:57.186508Z","shell.execute_reply":"2023-10-27T08:56:58.289796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! bash r_code.sh","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:57:00.473895Z","iopub.execute_input":"2023-10-27T08:57:00.474764Z","iopub.status.idle":"2023-10-27T08:57:04.775335Z","shell.execute_reply.started":"2023-10-27T08:57:00.474723Z","shell.execute_reply":"2023-10-27T08:57:04.774255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir v2\n!mv cell_type* v2","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.758556Z","iopub.status.idle":"2023-10-27T08:50:31.758927Z","shell.execute_reply.started":"2023-10-27T08:50:31.758744Z","shell.execute_reply":"2023-10-27T08:50:31.758762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport subprocess\nfrom IPython.display import FileLink, display\n\ndef download_file(path, download_file_name):\n    os.chdir('/kaggle/working/')\n    zip_name = f\"/kaggle/working/{download_file_name}.zip\"\n    command = f\"zip {zip_name} {path} -r\"\n    result = subprocess.run(command, shell=True, capture_output=True, text=True)\n    if result.returncode != 0:\n        print(\"Unable to run zip command!\")\n        print(result.stderr)\n        return\n    display(FileLink(f'{download_file_name}.zip'))\ndownload_file('/kaggle/working/v2', 'v2')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.759836Z","iopub.status.idle":"2023-10-27T08:50:31.760189Z","shell.execute_reply.started":"2023-10-27T08:50:31.760007Z","shell.execute_reply":"2023-10-27T08:50:31.760024Z"},"trusted":true},"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_NPC.TAK/voom.pdf\", width=500, height=500)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.761943Z","iopub.status.idle":"2023-10-27T08:50:31.762854Z","shell.execute_reply.started":"2023-10-27T08:50:31.76262Z","shell.execute_reply":"2023-10-27T08:50:31.762643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_Myeloid/voom.pdf\", width=500, height=500)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.763997Z","iopub.status.idle":"2023-10-27T08:50:31.764859Z","shell.execute_reply.started":"2023-10-27T08:50:31.764626Z","shell.execute_reply":"2023-10-27T08:50:31.764646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_NK/voom.pdf\", width=500, height=500)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.765921Z","iopub.status.idle":"2023-10-27T08:50:31.766313Z","shell.execute_reply.started":"2023-10-27T08:50:31.766114Z","shell.execute_reply":"2023-10-27T08:50:31.766133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_T4/voom.pdf\", width=500, height=500)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.770974Z","iopub.status.idle":"2023-10-27T08:50:31.771507Z","shell.execute_reply.started":"2023-10-27T08:50:31.771204Z","shell.execute_reply":"2023-10-27T08:50:31.771225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_T8/voom.pdf\", width=500, height=500)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.773151Z","iopub.status.idle":"2023-10-27T08:50:31.773607Z","shell.execute_reply.started":"2023-10-27T08:50:31.773388Z","shell.execute_reply":"2023-10-27T08:50:31.773412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"./v2/cell_type_Treg/voom.pdf\", width=500, height=500)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T08:50:31.774994Z","iopub.status.idle":"2023-10-27T08:50:31.775434Z","shell.execute_reply.started":"2023-10-27T08:50:31.7752Z","shell.execute_reply":"2023-10-27T08:50:31.77522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"---","metadata":{}},{"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":[]}]}