{ "cells": [ { "cell_type": "markdown", "id": "d974a397", "metadata": {}, "source": [ "## Animation" ] }, { "cell_type": "code", "execution_count": 1, "id": "f3b56b9e", "metadata": {}, "outputs": [], "source": [ "import unist\n", "import scanpy as sc" ] }, { "cell_type": "markdown", "id": "f4dd348e", "metadata": {}, "source": [ "The datasets used for this tutorial is the reconstruction of human metastatic lymph node and can be downloaded at:\n", "\n", "https://drive.google.com/file/d/1FtQ-yUSH418SYTPFRgR-ONolvwmYXgJh/view?usp=sharing" ] }, { "cell_type": "code", "execution_count": 2, "id": "f7c6b851", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 831406 × 0\n", " obs: 'pred_label'\n", " obsm: 'spatial'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## read in data\n", "adata = sc.read(\"/Users/shuilan/Library/CloudStorage/OneDrive-InsideMDAnderson/3D spatial transcriptomics/data/3D/OpenST_predicted.h5ad\")\n", "adata" ] }, { "cell_type": "code", "execution_count": 3, "id": "2eb4359c", "metadata": {}, "outputs": [], "source": [ "from unist.downstream.vis import three_d_plot, construct_pc, rotate_model" ] }, { "cell_type": "code", "execution_count": 4, "id": "b7a34e02", "metadata": {}, "outputs": [], "source": [ "anno_colors = {\n", " 'T_cell': '#E64B35',\n", " 'Cytotoxic_IFN_signaling': '#D73027',\n", " 'Dendritic_cells': '#F0E442',\n", " 'Macrophages': '#A6CEE3',\n", " 'M1_macrophages': '#1F78B4',\n", " 'Mast_cells': '#FF7F00',\n", " 'Germinal_Center_Plasma_IgM_B_cell': '#FDB462',\n", " 'Plasma_IgA': '#B3DE69',\n", " 'Plasma_IgG': '#33A02C',\n", " 'Fibroblasts': '#CAB2D6',\n", " 'CAF': '#6A3D9A',\n", " 'CAM': '#8E0152',\n", " 'High_Endothelial_Venules': '#4575B4',\n", " 'Cortex_CCL21': '#91CF60',\n", " 'Tumor': '#999999',\n", " 'Tumor_Keratin_Pearl': '#252525'\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "id": "1e397668", "metadata": {}, "outputs": [], "source": [ "adata = adata[adata.obs[\"pred_label\"] != \"unknown\"].copy()" ] }, { "cell_type": "code", "execution_count": 6, "id": "9b04e8fd", "metadata": {}, "outputs": [], "source": [ "pc_model, plot_cmap = construct_pc(\n", " adata=adata.copy(),\n", " spatial_key=\"spatial\",\n", " groupby=\"pred_label\",\n", " key_added=\"pred_label\",\n", " colormap=anno_colors\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "id": "25aa68e6", "metadata": {}, "outputs": [], "source": [ "rotate_model(model=pc_model, angle=(90, 0, 0), inplace=True)\n", "rotate_model(model=pc_model, angle=(0, 90, 0), inplace=True)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4a63f918", "metadata": {}, "outputs": [], "source": [ "import os\n", "from pathlib import Path\n", "os.chdir(f\"/Users/shuilan/Library/CloudStorage/OneDrive-InsideMDAnderson/3D spatial transcriptomics/data\")\n", "out_image_path = f\"animations\"\n", "Path(out_image_path).mkdir(parents=True, exist_ok=True)" ] }, { "cell_type": "code", "execution_count": 10, "id": "8c8fab88", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: imageio[ffmpeg] in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (2.37.2)\n", "Requirement already satisfied: numpy in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (from imageio[ffmpeg]) (1.26.4)\n", "Requirement already satisfied: pillow>=8.3.2 in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (from imageio[ffmpeg]) (12.1.1)\n", "Collecting imageio-ffmpeg (from imageio[ffmpeg])\n", " Downloading imageio_ffmpeg-0.6.0-py3-none-macosx_10_9_intel.macosx_10_9_x86_64.whl.metadata (1.5 kB)\n", "Requirement already satisfied: psutil in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (from imageio[ffmpeg]) (7.2.2)\n", "Downloading imageio_ffmpeg-0.6.0-py3-none-macosx_10_9_intel.macosx_10_9_x86_64.whl (24.9 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.9/24.9 MB\u001b[0m \u001b[31m26.7 MB/s\u001b[0m \u001b[33m0:00:00\u001b[0m eta \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hInstalling collected packages: imageio-ffmpeg\n", "Successfully installed imageio-ffmpeg-0.6.0\n" ] } ], "source": [ "! pip install imageio[ffmpeg]" ] }, { "cell_type": "code", "execution_count": 10, "id": "5adfcc36", "metadata": {}, "outputs": [], "source": [ "plot_text = 'PC model for metastasis lymph node'\n", "\n", "three_d_plot(\n", " pc_model,\n", " key=\"pred_label\",\n", " model_size=4,\n", " filename=os.path.join(out_image_path, f\"whole.mp4\"),\n", " window_size=(1500, 1500),\n", " view_up=(0, 0, 1),\n", " cpo=\"yz\",\n", " show_legend=False,\n", " off_screen=False,\n", " model_style=\"points\",\n", " opacity=1,\n", " jupyter=\"static\",\n", " background=\"white\",\n", " framerate=10,\n", " pad_ratio=0.3,\n", " text=plot_text,\n", " text_kwargs={\n", " \"font_size\": 40,\n", " \"font_family\": \"arial\",\n", " \"color\": \"black\",\n", " \"position\": \"upper_left\"}\n", ")" ] }, { "cell_type": "markdown", "id": "bbd9e1bd", "metadata": {}, "source": [ "## Animation Demo\n", "\n", "" ] } ], "metadata": { "kernelspec": { "display_name": "unist", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.19" } }, "nbformat": 4, "nbformat_minor": 5 }