{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SPACEc: TissUUmaps for interactive visualization\n", "Interactive visualization via TissUUmaps might be informative during multiple steps of the analysis. Apart from the general function provided with the TissUUmaps Python package, we provide specific functions that automatically phrase the input during multiple steps of the analysis." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instructions\n", "\n", "To use the TissUUmaps viewer you need:\n", "- A pickle file that contains the segmentation output and images \n", "- An AnnData object containing the currently used single cell data\n", "\n", "The *tm_prepare_input* function reads the named content for one region. For that, the user has to provide a region column and a region name. The pickle file has to match the specified region. \n", "The function creates a folder that contains all necessary input files that are needed to launch the TissUUmaps session. Additionally, the function can launch the TissUUmaps session. If the session is launched from the function a tmap file is created in the input directory that allows to open the session again (both from jupyter and the standalone viewer app).\n", "Alternatively, the function can be used to prepare the directory and the viewer can be launched separately to modify the display options in jupyter as well as host ports etc.\n", "\n", "If the Jupyter viewer is too small (might be a problem on small monitors), the user can use the link (displayed if function is executed) to display TissUUmaps in the browser. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# import spacec first\n", "import spacec as sp\n", "\n", "# silencing warnings\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "#import standard packages\n", "import os\n", "import scanpy as sc\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Specify the path to the data\n", "root_path = \"/home/user/path/SPACEc/\" # inset your own path\n", "\n", "data_path = root_path + 'example_data/raw/' # where the data is stored\n", "\n", "# where you want to store the output\n", "output_dir = root_path + 'example_data/output/'\n", "\n", "os.makedirs(output_dir, exist_ok=True)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AnnData object with n_obs × n_vars = 46789 × 59\n", " obs: 'DAPI', 'x', 'y', 'area', 'region_num', 'unique_region', 'condition', 'leiden_1', 'leiden_1_subcluster', 'cell_type_coarse', 'cell_type_coarse_subcluster', 'cell_type_coarse_f', 'cell_type_coarse_f_subcluster', 'cell_type_coarse_f_f', 'cell_type', 'CN_k20_n40', 'CN_k20_n30', 'CN_k20_n20', 'CN_k20_n25', 'CN_k20_n6', 'CN_k20_n6_annot'\n", " uns: 'CN_k20_n6_colors', 'Centroid_k20_n20', 'Centroid_k20_n25', 'Centroid_k20_n30', 'Centroid_k20_n40', 'Centroid_k20_n6', 'cell_type_coarse_f_colors', 'cell_type_colors', 'dendrogram_cell_type_coarse_f_subcluster', 'leiden', 'leiden_1_colors', 'neighbors', 'ppa_result_100', 'ppa_result_150', 'ppa_result_200', 'ppa_result_250', 'ppa_result_50', 'triDist', 'triDist_keyname', 'umap', 'unique_region_colors'\n", " obsm: 'X_pca', 'X_umap'\n", " layers: 'scaled'\n", " obsp: 'connectivities', 'distances'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# read in the annotated anndata\n", "adata = sc.read(output_dir + 'adata_nn_demo_annotated_cn.h5ad')\n", "adata" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DAPIxyarearegion_numunique_regionconditionleiden_1leiden_1_subclustercell_type_coarse...cell_type_coarse_fcell_type_coarse_f_subclustercell_type_coarse_f_fcell_typeCN_k20_n40CN_k20_n30CN_k20_n20CN_k20_n25CN_k20_n6CN_k20_n6_annot
0105.9931974.9863951472.238095147.01reg002tonsillitis2323,1Neutrophil...NeutrophilNeutrophilNeutrophilNeutrophil55555Marginal Zone B-DC-Enriched
1123.6776865.3595041322.851240242.01reg002tonsillitis1313M2 Macrophage...M2 MacrophageM2 MacrophageM2 MacrophageM2 Macrophage11111Marginal Zone
2107.2031255.7109381506.226562256.01reg002tonsillitis2323,1Neutrophil...NeutrophilNeutrophilNeutrophilNeutrophil55555Marginal Zone B-DC-Enriched
349.6609598.544521641.938356292.01reg002tonsillitis2121recluster...B cellB cellB cellB cell44444Germinal Center
4148.7025329.0063291303.702532158.01reg002tonsillitis1212,1recluster...CD4+ T cellCD4+ T cellCD4+ T cellCD4+ T cell11111Marginal Zone
\n", "

5 rows × 21 columns

\n", "
" ], "text/plain": [ " DAPI x y area region_num unique_region \\\n", "0 105.993197 4.986395 1472.238095 147.0 1 reg002 \n", "1 123.677686 5.359504 1322.851240 242.0 1 reg002 \n", "2 107.203125 5.710938 1506.226562 256.0 1 reg002 \n", "3 49.660959 8.544521 641.938356 292.0 1 reg002 \n", "4 148.702532 9.006329 1303.702532 158.0 1 reg002 \n", "\n", " condition leiden_1 leiden_1_subcluster cell_type_coarse ... \\\n", "0 tonsillitis 23 23,1 Neutrophil ... \n", "1 tonsillitis 13 13 M2 Macrophage ... \n", "2 tonsillitis 23 23,1 Neutrophil ... \n", "3 tonsillitis 21 21 recluster ... \n", "4 tonsillitis 12 12,1 recluster ... \n", "\n", " cell_type_coarse_f cell_type_coarse_f_subcluster cell_type_coarse_f_f \\\n", "0 Neutrophil Neutrophil Neutrophil \n", "1 M2 Macrophage M2 Macrophage M2 Macrophage \n", "2 Neutrophil Neutrophil Neutrophil \n", "3 B cell B cell B cell \n", "4 CD4+ T cell CD4+ T cell CD4+ T cell \n", "\n", " cell_type CN_k20_n40 CN_k20_n30 CN_k20_n20 CN_k20_n25 CN_k20_n6 \\\n", "0 Neutrophil 5 5 5 5 5 \n", "1 M2 Macrophage 1 1 1 1 1 \n", "2 Neutrophil 5 5 5 5 5 \n", "3 B cell 4 4 4 4 4 \n", "4 CD4+ T cell 1 1 1 1 1 \n", "\n", " CN_k20_n6_annot \n", "0 Marginal Zone B-DC-Enriched \n", "1 Marginal Zone \n", "2 Marginal Zone B-DC-Enriched \n", "3 Germinal Center \n", "4 Marginal Zone \n", "\n", "[5 rows x 21 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "adata.obs.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integrated use" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function allows the user to reshape the data for TissUUmaps and plot cells from a selected region on top of the original image." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Preparing TissUUmaps input...\n", "Opening TissUUmaps viewer...\n", "Creating project file /home/tim/Dokumente/SPACEc_Apr_2024/cache/reg002/reg002_project.tmap\n", "Loading url: http://localhost:5100/reg002_project.tmap?path=home/tim/Dokumente/SPACEc_Apr_2024/cache/reg002\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#create cache direction to store tissuumaps cache\n", "os.makedirs(output_dir + \"cache\", exist_ok=True)\n", "\n", "image_list, csv_paths = sp.tl.tm_viewer(\n", " adata,\n", " images_pickle_path= output_dir + 'seg_output_tonsil2.pickle',\n", " directory = output_dir + \"cache\", # Or inset your own path where you want to cache your images for TM visualization (you can delete this once you are done with TM)\n", " region_column = \"unique_region\",\n", " region = \"reg002\",\n", " xSelector = \"y\",\n", " ySelector = \"x\",\n", " color_by = \"cell_type\",\n", " keep_list = None,\n", " open_viewer=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive Catplot via the TissUUmaps viewer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function starts a simplified version that only shows the cell centroid without the original image. It can be used for fast and interactive visualization. Different from the function above, this function allows visualizing all regions at once. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sp.tl.tm_viewer_catplot(\n", " adata, # anndata object\n", " directory=None, # directory to save the generated csv files\n", " region_column=\"unique_region\", # column with the region information (user can select region in tm viewer)\n", " x=\"x\", # x coordinates\n", " y=\"y\", # y coordinates\n", " color_by=\"cell_type\", # cathegorical column to color by\n", " open_viewer=True, # open the tm viewer \n", " add_UMAP=True, # add UMAP to the tm viewer for exploring the feature space along with the spatial data\n", " keep_list=None) # List of columns to keep from `adata.obs`. If None, defaults to [region_column, x, y, color_by]" ] } ], "metadata": { "kernelspec": { "display_name": "spacec", "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.0" } }, "nbformat": 4, "nbformat_minor": 4 }