{"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":"# Gene expression visualization using UMAP\n\n>UMAP (Uniform Manifold Approximation and Projection) is a novel manifold learning technique for dimension reduction. UMAP is constructed from a theoretical framework based in Riemannian geometry and algebraic topology. The result is a practical scalable algorithm that is applicable to real world data. The UMAP algorithm is competitive with t-SNE for visualization quality, and arguably preserves more of the global structure with superior run time performance. Furthermore, UMAP has no computational restrictions on embedding dimension, making it viable as a general purpose dimension reduction technique for machine learning\n(McInnes, Leland; Healy, John; Melville, James (2018-12-07). \"Uniform manifold approximation and projection for dimension reduction\". arXiv:1802.03426.)\n\n### Please Upvote if you Find this Useful :)","metadata":{}},{"cell_type":"code","source":"!pip install umap-learn","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-19T02:17:23.121301Z","iopub.execute_input":"2023-09-19T02:17:23.121757Z","iopub.status.idle":"2023-09-19T02:17:37.685639Z","shell.execute_reply.started":"2023-09-19T02:17:23.121723Z","shell.execute_reply":"2023-09-19T02:17:37.683977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport sklearn\nimport umap","metadata":{"execution":{"iopub.status.busy":"2023-09-19T02:17:58.519194Z","iopub.execute_input":"2023-09-19T02:17:58.519579Z","iopub.status.idle":"2023-09-19T02:17:58.526062Z","shell.execute_reply.started":"2023-09-19T02:17:58.519550Z","shell.execute_reply":"2023-09-19T02:17:58.524613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_parquet(\"/kaggle/input/open-problems-single-cell-perturbations/de_train.parquet\")\ndf_gene_exp = df.drop(columns=[\"cell_type\",\"sm_name\", \"sm_lincs_id\", \"SMILES\", \"control\"])\ndf_info = df[[\"cell_type\",\"sm_name\", \"sm_lincs_id\", \"SMILES\", \"control\"]]","metadata":{"execution":{"iopub.status.busy":"2023-09-19T02:18:03.123731Z","iopub.execute_input":"2023-09-19T02:18:03.124133Z","iopub.status.idle":"2023-09-19T02:18:03.211508Z","shell.execute_reply.started":"2023-09-19T02:18:03.124095Z","shell.execute_reply":"2023-09-19T02:18:03.210205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# do UMAP clustering\nmapper = umap.UMAP(random_state=42,\n                   n_neighbors=5,\n                   min_dist=0.4,\n                   metric=\"correlation\")\nembedding = mapper.fit_transform(df_gene_exp)\n\nembedding_x = embedding[:, 0]\nembedding_y = embedding[:, 1]\n\n\n# plot UMAP\ncolors=[\"r\", \"b\", \"g\", \"y\", \"m\", \"c\", \"k\", \"w\"]\ndic_c = {}\n\nfor (cell,c) in zip(df_info[\"cell_type\"].unique(), colors):\n    dic_c[cell]=c\n    cell_i = df_info[df_info[\"cell_type\"]==cell].index.to_list()\n    plt.scatter(embedding_x[cell_i], embedding_y[cell_i], label=cell, s=10, color=c)\n    \ncont = df_info[df_info[\"control\"]==True]\nfor cell in cont[\"cell_type\"].unique():\n    i = cont.query(\"cell_type==@cell\").index.to_list()\n    plt.scatter(embedding_x[i], embedding_y[i], label=f\"positive_ctrl_{cell}\", marker=\"^\",color=dic_c[cell], s=40, edgecolors=\"black\")\n\nplt.grid()\nplt.legend(loc='upper left', bbox_to_anchor=(1, 1))\n#plt.title()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-19T02:39:10.785238Z","iopub.execute_input":"2023-09-19T02:39:10.785653Z","iopub.status.idle":"2023-09-19T02:39:26.694396Z","shell.execute_reply.started":"2023-09-19T02:39:10.785616Z","shell.execute_reply":"2023-09-19T02:39:26.693139Z"},"trusted":true},"execution_count":null,"outputs":[]}]}