### Install scplotter from GitHub Source: https://github.com/pwwang/scplotter/blob/master/README.md Install the scplotter package from GitHub using either the remotes or devtools package. ```r remotes::install_github("pwwang/scplotter") ``` ```r devtools::install_github("pwwang/scplotter") ``` -------------------------------- ### Load scplotter and document Source: https://github.com/pwwang/scplotter/blob/master/notebooks/Working_with_anndata_h5ad.ipynb Load the scplotter library and document its functions. This is a common setup step for R packages. ```r devtools::document() devtools::load_all() ``` -------------------------------- ### Load Seurat Package Source: https://github.com/pwwang/scplotter/blob/master/pkgdown/assets/Seurat.XeniumInSitu.html Loads the Seurat package. Ensure Seurat is installed before running. ```R suppressPackageStartupMessages({ library(Seurat) }) ``` -------------------------------- ### Install scplotter using Conda Source: https://github.com/pwwang/scplotter/blob/master/README.md Install the scplotter package using Conda. ```bash $ conda install pwwang::r-scplotter ``` -------------------------------- ### Setup Giotto Environment and Load scplotter Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.SlideSeq.ipynb Ensures the Giotto environment is accessible and loads the scplotter package. It's crucial to set the RETICULATE_PYTHON environment variable to point to your Python interpreter if Giotto cannot find it automatically. ```R library(Giotto) # Ensure Giotto can access a python env genv_exists <- suppressMessages(checkGiottoEnvironment()) print(genv_exists) python_path <- file.path(Sys.getenv("HOME"), "miniconda3", "envs", "giotto_env", "bin", "python") Sys.setenv(RETICULATE_PYTHON = python_path) invisible(capture.output(suppressMessages(set_giotto_python_path(python_path = python_path)))) # library(scplotter) devtools::load_all() ``` -------------------------------- ### Load Seurat and scplotter Packages Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_VisiumHD.ipynb Loads necessary R packages for Seurat and scplotter. Ensure scplotter is installed or loaded via devtools. ```r suppressPackageStartupMessages({ library(Seurat) }) # Load the scplotter package # library(scplotter) devtools::load_all() # devtools::load_all("../../../plotthis") ``` -------------------------------- ### Load Package and Plot Spatial Features in Seurat Source: https://github.com/pwwang/scplotter/blob/master/pkgdown/assets/Seurat.XeniumInSitu.html Loads the 'scplotter' package and attempts to plot spatial features. This function is used for visualizing gene expression or other features on spatial data. Note: This example may produce an error if the Seurat object is not correctly formatted. ```R devtools::load_all() SpatialFeaturePlot(xenium.obj, features = c("Gad1", "Sst", "Pvalb", "Gfap")) ``` -------------------------------- ### Download h5ad file Source: https://github.com/pwwang/scplotter/blob/master/notebooks/Working_with_anndata_h5ad.ipynb Download a sample .h5ad file for demonstration purposes. Ensure the data directory exists. ```r h5ad_file <- "./data/pancreas_velocity.h5ad" ``` -------------------------------- ### Prepare Visium HD Data with Giotto Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.VisiumHD.ipynb Initializes and processes Visium HD data, including overlap calculation and conversion to a gene matrix. Requires the Giotto package. ```python visiumHD = calculateOverlap(visiumHD, spatial_info = 'hex400', feat_info = 'rna') # convert overlap results to bin by gene matrix visiumHD = overlapToMatrix(visiumHD, poly_info = 'hex400', feat_info = 'rna', name = 'raw') # this action will automatically create an active spatial unit, ie. hexbin 400 activeSpatUnit(visiumHD) # filter on gene expression matrix visiumHD <- filterGiotto(visiumHD, expression_threshold = 1, feat_det_in_min_cells = 5, min_det_feats_per_cell = 25) # normalize and scale gene expression data visiumHD <- normalizeGiotto(visiumHD, scalefactor = 1000, verbose = T) # add cell and gene statistics visiumHD <- addStatistics(visiumHD) ``` -------------------------------- ### Prepare Nanostring CosMx Data with Giotto Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Nanostring_CosMx.ipynb This section outlines the initial steps for preparing Nanostring CosMx data within the Giotto environment. It covers data loading and subsetting based on various criteria like expression data, spatial locations, and metadata. ```python options(repr.plot.width = 11, repr.plot.height = 10) SpatFeaturePlot( cosmx, image = "black", feature = "total_expr", points_size = 0.4, points_palette = "BluYl", points_palette_reverse = TRUE ) ``` -------------------------------- ### Load 10x Visium HD Data and Create Giotto Object Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.VisiumHD.ipynb Loads 10x Visium HD data, including expression matrices and tissue positions, and processes them into a Giotto object. This involves creating spatial points and tessellating the spatial extent. ```R library(Giotto) ## Set instructions results_folder <- "data/Human_Colorectal_Cancer_workshop.results" instructions <- createGiottoInstructions( save_dir = results_folder, save_plot = FALSE, show_plot = TRUE, return_plot = TRUE, python_path = python_path ) ## Provide the path to the visium folder data_path <- "data/Human_Colorectal_Cancer_workshop/square_002um" expression_path <- file.path(data_path, 'raw_feature_bc_matrix') expr_results <- get10Xmatrix(path_to_data = expression_path, gene_column_index = 1) tissue_positions_path <- file.path(data_path, 'spatial/tissue_positions.parquet') tissue_positions <- data.table::as.data.table(arrow::read_parquet(tissue_positions_path)) matrix_tile_dt <- data.table::as.data.table(Matrix::summary(expr_results)) genes <- expr_results@Dimnames[[1]] samples <- expr_results@Dimnames[[2]] matrix_tile_dt[, gene := genes[i]] matrix_tile_dt[, pixel := samples[j]] expr_pos_data <- data.table::merge.data.table(matrix_tile_dt, tissue_positions, by.x = 'pixel', by.y = 'barcode') expr_pos_data <- expr_pos_data[,.(pixel, pxl_row_in_fullres, pxl_col_in_fullres, gene, x)] colnames(expr_pos_data) = c('pixel', 'x', 'y', 'gene', 'count') giotto_points = createGiottoPoints(x = expr_pos_data[,.(x, y, gene, pixel, count)]) hexbin400 <- tessellate(extent = ext(giotto_points), shape = 'hexagon', shape_size = 400, name = 'hex400') # gpoints provides spatial gene expression information # gpolygons provides spatial unit information (here = hexagon tiles) visiumHD = createGiottoObjectSubcellular(gpoints = list('rna' = giotto_points), gpolygons = list('hex400' = hexbin400), instructions = instructions) # create spatial centroids for each spatial unit (hexagon) visiumHD = addSpatialCentroidLocations(gobject = visiumHD, poly_info = 'hex400') visiumHD ``` -------------------------------- ### Create Giotto CosMx Object Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Nanostring_CosMx.ipynb Initializes a Giotto object from Nanostring CosMx data. This involves specifying the directory, version, and which fields of view (FOVs) to load. It also sets up Giotto instructions for saving and displaying plots. ```r library(Giotto) ## Set instructions results_folder <- "data/Nanostring_CosMx_Subcellular_Lung_Cancer.results" instrs <- createGiottoInstructions( save_dir = results_folder, save_plot = FALSE, show_plot = TRUE, return_plot = TRUE, python_path = python_path ) data_path <- "data/Nanostring_CosMx_Subcellular_Lung_Cancer/Lung12/Lung12-Flat_files_and_images/" ## create giotto cosmx object cosmx <- suppressWarnings(createGiottoCosMxObject( cosmx_dir = data_path, version = "legacy", # set to this for legacy NSCLC dataset FOVs = seq_len(28), # defaults to all FOVs load_expression = FALSE, # defaults to FALSE (see feature aggregation step below) instructions = instrs )) force(cosmx) ``` -------------------------------- ### Loading and Processing Xenium Images Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Xenium.ipynb Create a Giotto Large Image List from TIFF paths and append it to the Giotto object. Adjust image brightness using `@max_window` if needed. ```R img_paths <- c( sprintf("data/Xenium_Giotto_workshop/morphology_focus/morphology_focus_%04d.tif", 0:3), "data/Xenium_Giotto_workshop/he_mini.tif" ) img_list <- createGiottoLargeImageList( img_paths, # naming is based on the channel metadata above names = c("DAPI", "18S", "ATP1A1/CD45/E-Cadherin", "alphaSMA/Vimentin", "HE"), use_rast_ext = TRUE, verbose = FALSE ) # make some images brighter img_list[[1]]@max_window <- 5000 img_list[[2]]@max_window <- 5000 img_list[[3]]@max_window <- 5000 # append images to gobject g <- setGiotto(g, img_list) ``` -------------------------------- ### Create GiottoVisiumObject Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Visium.ipynb Creates a Giotto object directly from a Visium directory, specifying expression data, image name, and gene column index. Initializes Giotto instructions for saving and displaying plots. ```R library(Giotto) ## Set instructions results_folder <- "data/Adult_Mouse_Brain.results" instructions <- createGiottoInstructions( save_dir = results_folder, save_plot = FALSE, show_plot = TRUE, return_plot = TRUE, python_path = python_path ) ## Provide the path to the visium folder data_path <- "data/Adult_Mouse_Brain" ## Create object directly from the visium folder visium_brain <- createGiottoVisiumObject( visium_dir = data_path, expr_data = "raw", png_name = "tissue_lowres_image.png", gene_column_index = 2, instructions = instructions ) visium_brain$in_tissue <- ifelse(visium_brain$in_tissue == 1, "Yes", "No") # visium_brain@cell_metadata ``` -------------------------------- ### Creating and Processing Pseudo-Visium Data Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Xenium.ipynb This snippet demonstrates the creation of a pseudo-Visium object from Giotto data, setting the spatial unit, calculating overlaps, filtering cells and features, normalizing data, adding statistics, and creating a spatial network. This is a comprehensive preprocessing pipeline for spatial data. ```r pvis <- makePseudoVisium( extent = ext(g, prefer = c("polygon", "points"), all_data = TRUE), # all_data = TRUE is the default micron_size = 1 ) g <- setGiotto(g, pvis) g <- addSpatialCentroidLocations(g, poly_info = "pseudo_visium") activeSpatUnit(g) <- "pseudo_visium" g <- calculateOverlap(g, spatial_info = "pseudo_visium", feat_info = "rna" ) g <- overlapToMatrix(g) g <- filterGiotto(g, expression_threshold = 1, feat_det_in_min_cells = 1, min_det_feats_per_cell = 100 ) g <- normalizeGiotto(g) g <- addStatistics(g) g <- createSpatialNetwork(g, method = "Delaunay") ``` -------------------------------- ### Load VisiumHD Data and Create Seurat Object Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_VisiumHD.ipynb Reads 10x VisiumHD counts from an H5 file and creates a Seurat object with spatial assay. Spatial coordinates are added from a separate directory. ```r # https://www.10xgenomics.com/datasets/visium-hd-cytassist-gene-expression-libraries-of-mouse-intestine counts <- Read10X_h5("data/Visium_HD_Mouse_Small_Intestine/binned_outputs/square_008um/filtered_feature_bc_matrix.h5") # Create a Seurat object object <- CreateSeuratObject(counts = counts, assay = "Spatial", project = "Visium_HD_Mouse_Small_Intestine") # Add spatial coordinates object[["slice1"]] <- Read10X_Image("data/Visium_HD_Mouse_Small_Intestine/binned_outputs/square_008um/spatial/") object <- NormalizeData(object) object ``` -------------------------------- ### Load Giotto Mini Dataset Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.vizgen.ipynb Loads the 'vizgen' dataset using GiottoData::loadGiottoMini. This function reads the Giotto object and its associated information. ```R g <- GiottoData::loadGiottoMini(dataset = "vizgen") g ``` -------------------------------- ### Initialize MathJax Source: https://github.com/pwwang/scplotter/blob/master/pkgdown/assets/Seurat.XeniumInSitu.html Configures and initializes MathJax for rendering mathematical equations. It sets up equation numbering, inline and display math delimiters, and processing options. ```javascript init_mathjax = function() { if (window.MathJax) { // MathJax loaded MathJax.Hub.Config({ TeX: { equationNumbers: { autoNumber: "AMS", useLabelIds: true } }, tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\\"]"] ], processEscapes: true, processEnvironments: true }, displayAlign: 'center', CommonHTML: { linebreaks: { automatic: true } } }); MathJax.Hub.Queue(["Typeset", MathJax.Hub]); } } init_mathjax(); ``` -------------------------------- ### Load seqFISH Mini Dataset Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.seqFISH.ipynb Loads the seqFISH mini dataset using Giotto's data loading function. This prepares the data for subsequent analysis steps. ```R seqfish_mini <- GiottoData::loadGiottoMini("seqfish") seqfish_mini ``` -------------------------------- ### Capture and Display Session Information Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_VisiumHD.ipynb Captures the current R session information, formats it as a string, and prints it to the console, excluding BLAS/LAPACK paths for brevity. ```r x <- sessionInfo() x <- capture.output(print(x)) # hide the BLAS/LAPACK paths x <- x[!startsWith(x, "BLAS/LAPACK:")] cat(paste(x, collapse = "\n")) ``` -------------------------------- ### Create Spatial Network Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Visium.ipynb Create a spatial network for the Giotto object using k-Nearest Neighbors (kNN). ```r visium_brain <- createSpatialNetwork(gobject = visium_brain, method = "kNN", k = 6, maximum_distance_knn = 400, name = "spatial_network") ``` -------------------------------- ### Load 10X Data and Create Giotto Object Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.SlideSeq.ipynb Loads expression matrix and spatial locations from 10X Genomics data and creates a Giotto object. Ensure the file paths are correct and that the spatial locations are filtered to match the barcodes in the expression matrix. ```R library(Giotto) ## Set instructions results_folder <- "data/Giotto_SlideSeq.results" instr <- createGiottoInstructions( save_dir = results_folder, save_plot = FALSE, show_plot = TRUE, return_plot = TRUE, python_path = python_path ) ## Provide the path to the visium folder data_path <- "data/Giotto_SlideSeq" expression_matrix <- get10Xmatrix(file.path(data_path, "2020-12-19_Puck_201112_26.matched.digital_expression")) spatial_locs <- data.table::fread(file.path(data_path, "2020-12-19_Puck_201112_26.BeadLocationsForR.csv.tar")) spatial_locs <- spatial_locs[spatial_locs$barcodes %in% colnames(expression_matrix),] giotto_object <- createGiottoObject( expression = expression_matrix, spatial_locs = spatial_locs, instructions = instructions ) force(giotto_object) ``` -------------------------------- ### Load and Preprocess Akoya CODEX Data with Seurat Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.Akoya_CODEX.ipynb Loads Akoya CODEX data, normalizes, scales, runs PCA, UMAP, and finds clusters. Ensure the 'scplotter' package is loaded. ```r options(future.globals.maxSize = 512 * 1024^3) # 512 GB suppressPackageStartupMessages({ library(Seurat) }) # Load the scplotter package # library(scplotter) devtools::load_all() # devtools::load_all("../../../plotthis") codex.obj <- LoadAkoya(filename = "data/Akoya_CODEX/LN7910_20_008_11022020_reg001_compensated.csv", type = "processor", fov = "HBM754.WKLP.262") codex.obj <- NormalizeData(object = codex.obj, normalization.method = "CLR", margin = 2) codex.obj <- ScaleData(codex.obj) VariableFeatures(codex.obj) <- rownames(codex.obj) # since the panel is small, treat all features as variable. codex.obj <- RunPCA(object = codex.obj, npcs = 20, verbose = FALSE) codex.obj <- RunUMAP(object = codex.obj, dims = 1:20, verbose = FALSE) codex.obj <- FindNeighbors(object = codex.obj, dims = 1:20, verbose = FALSE) codex.obj <- FindClusters(object = codex.obj, verbose = FALSE, resolution = 0.4, n.start = 1) codex.obj ``` -------------------------------- ### Load Nanostring Data with Seurat Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.Nanostring_CosMx.ipynb Loads Nanostring CosMx data from a specified directory using Seurat's LoadNanostring function. Ensure the scplotter package is loaded. ```R options(future.globals.maxSize = 512 * 1024^3) # 512 GB suppressPackageStartupMessages({ library(Seurat) }) # Load the scplotter package # library(scplotter) devtools::load_all() # devtools::load_all("../../../plotthis") # see also the vignette for scplotter to work with CosMx data prepared by Giotto data.dir <- "data/CosMx_Quarter_Brain/Quarter_Brain" obj <- LoadNanostring(data.dir = data.dir, fov = 'fov') obj ``` -------------------------------- ### Load 10x Visium Spatial Data Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_Visium.ipynb Loads 10x Visium spatial data from a specified directory. Ensure the path to the data is correct. ```r # https://s3-eu-west-1.amazonaws.com/pfigshare-u-files/49797231/Visium_FFPE_Mouse_Kidney.tar.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIYCQYOYV5JSSROOA/20250602/eu-west-1/s3/aws4_request&X-Amz-Date=20250602T214211Z&X-Amz-Expires=10&X-Amz-SignedHeaders=host&X-Amz-Signature=c71d8aca8e60a009323d7dfefa7df0dc08e9769df54ab2f3c6800a17ba045afd kidney <- Seurat::Load10X_Spatial("data/Visium_FFPE_Mouse_Kidney/") ``` -------------------------------- ### Basic Spatial Visualization Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.SlideSeq.ipynb Generates basic spatial plots of the Giotto object. `SpatDimPlot` can be used to visualize cells, with options to customize point size, color, and shape. ```R options(repr.plot.width = 12, repr.plot.height = 6) # devtools::load_all() p1 <- SpatDimPlot(giotto_object, points_size = 2, points_color_by = "lightblue") p2 <- SpatDimPlot(giotto_object, points_size = 2, points_color_by = "lightblue", points_shape = 21, points_border_color = "grey40") p1 + p2 ``` -------------------------------- ### Run Integrated UMAP Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Spatial_CITE-Seq.ipynb Generates an integrated UMAP visualization based on the WNN analysis, combining information from different modalities. ```r my_giotto_object <- runIntegratedUMAP(my_giotto_object) # modality1 = "rna", # modality2 = "protein") ``` -------------------------------- ### Session Information Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Visium.ipynb Capture and print session information, excluding BLAS/LAPACK paths. ```r x <- sessionInfo() x <- capture.output(print(x)) # hide the BLAS/LAPACK paths x <- x[!startsWith(x, "BLAS/LAPACK:")] cat(paste(x, collapse = "\n")) ``` -------------------------------- ### Create Giotto Object and Image Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Spatial_CITE-Seq.ipynb This snippet details the process of creating a Giotto object by loading spatial coordinates, RNA, and protein expression matrices. It also includes creating and adding a spatial image to the Giotto object. ```R library(Giotto) ## Set instructions results_folder <- "data/Giotto_Spatial_CITE-Seq.results" instructions <- createGiottoInstructions( save_dir = results_folder, save_plot = FALSE, show_plot = TRUE, return_plot = TRUE, python_path = python_path ) ## Provide the path to the visium folder data_path <- "data/Giotto_Spatial_CITE-Seq" x <- data.table::fread(file.path(data_path, "GSE213264_RAW/GSM6578065_humanskin_RNA.tsv.gz")) spatial_coords <- data.frame(cell_ID = x$X) spatial_coords <- cbind(spatial_coords, stringr::str_split_fixed(spatial_coords$cell_ID, pattern = "x", n = 2)) colnames(spatial_coords)[2:3] = c("sdimx", "sdimy") spatial_coords$sdimx <- as.integer(spatial_coords$sdimx) spatial_coords$sdimy <- as.integer(spatial_coords$sdimy) spatial_coords$sdimy <- spatial_coords$sdimy*(-1) rna_matrix <- data.table::fread(file.path(data_path, "GSE213264_RAW/GSM6578065_humanskin_RNA.tsv.gz")) rna_matrix <- rna_matrix[rna_matrix$X %in% spatial_coords$cell_ID,] rna_matrix <- rna_matrix[match(spatial_coords$cell_ID, rna_matrix$X),] rna_matrix <- t(rna_matrix[,-1]) colnames(rna_matrix) <- spatial_coords$cell_ID protein_matrix <- data.table::fread(file.path(data_path, "GSE213264_RAW/GSM6578074_humanskin_protein.tsv.gz")) protein_matrix <- protein_matrix[protein_matrix$X %in% spatial_coords$cell_ID,] protein_matrix <- protein_matrix[match(spatial_coords$cell_ID, protein_matrix$X),] protein_matrix <- t(protein_matrix[,-1]) colnames(protein_matrix) <- spatial_coords$cell_ID my_giotto_object <- createGiottoObject(expression = list(rna = list(raw = rna_matrix), protein = list(raw = protein_matrix)), expression_feat = list("rna", "protein"), spatial_locs = spatial_coords, instructions = instructions) my_giotto_image <- createGiottoImage(gobject = my_giotto_object, do_manual_adj = TRUE, scale_factor = 0.5, mg_object = file.path(data_path, "skin.jpg"), negative_y = TRUE) my_giotto_object <- addGiottoImage(gobject = my_giotto_object, images = list(my_giotto_image), spat_loc_name = "raw") force(my_giotto_object) ``` -------------------------------- ### Run Seurat Analysis Pipeline Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.Xenium.ipynb Performs standard Seurat analysis steps including SCTransform normalization, PCA, UMAP, and clustering on the Xenium object. ```R xenium.obj <- SCTransform(xenium.obj, assay = "Xenium") xenium.obj <- RunPCA(xenium.obj, npcs = 30, features = rownames(xenium.obj)) xenium.obj <- RunUMAP(xenium.obj, dims = 1:30) xenium.obj <- FindNeighbors(xenium.obj, reduction = "pca", dims = 1:30) xenium.obj <- FindClusters(xenium.obj, resolution = 0.3) ``` -------------------------------- ### Run Integrated Weighted Nearest Neighbor (WNN) Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Spatial_CITE-Seq.ipynb Performs WNN analysis to integrate information from multiple modalities (e.g., RNA and protein). Requires specifying the number of neighbors (k). ```r my_giotto_object <- runWNN(my_giotto_object, # modality_1 = "rna", # modality_2 = "protein", # pca_name_modality_1 = "rna.pca", # pca_name_modality_2 = "protein.pca", k = 20) ``` -------------------------------- ### Create Giotto Object from CODEX Data Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.CODEX.ipynb Initializes a Giotto object using expression, spatial location, and metadata from CODEX data. Ensure the paths to your data files are correctly specified. ```r library(Giotto) ## Set instructions results_folder <- "data/codex_spleen.results" instructions <- createGiottoInstructions( save_dir = results_folder, save_plot = FALSE, show_plot = TRUE, return_plot = TRUE, python_path = python_path ) data_path <- "data/codex_spleen/" # 2. create giotto object from provided paths #### expr_path <- paste0(data_path, "codex_BALBc_3_expression.txt.gz") loc_path <- paste0(data_path, "codex_BALBc_3_coord.txt") meta_path <- paste0(data_path, "codex_BALBc_3_annotation.txt") # read in data information # expression info codex_expression <- readExprMatrix(expr_path, transpose = FALSE) # cell coordinate info codex_locations <- data.table::fread(loc_path) # metadata codex_metadata <- data.table::fread(meta_path) ## stitch x.y tile coordinates to global coordinates xtilespan <- 1344 ytilespan <- 1008 # TODO: expand the documentation and input format of stitchTileCoordinates. Probably not enough information for new users. stitch_file <- stitchTileCoordinates(location_file = codex_metadata, Xtilespan = xtilespan, Ytilespan = ytilespan) codex_locations <- stitch_file[,.(Xcoord, Ycoord)] # create Giotto object codex_test <- createGiottoObject(expression = codex_expression, spatial_locs = codex_locations, instructions = instructions) codex_metadata$cell_ID <- as.character(codex_metadata$cellID) codex_test <- addCellMetadata(codex_test, new_metadata = codex_metadata, by_column = TRUE, column_cell_ID = "cell_ID") # subset Giotto object cell_metadata <- pDataDT(codex_test) cell_IDs_to_keep <- cell_metadata[Imaging_phenotype_cell_type != "dirt" & Imaging_phenotype_cell_type != "noid" & Imaging_phenotype_cell_type != "capsule",]$cell_ID codex_test <- subsetGiotto(codex_test, cell_ids = cell_IDs_to_keep) ## filter codex_test <- filterGiotto(gobject = codex_test, expression_threshold = 1, feat_det_in_min_cells = 10, min_det_feats_per_cell = 2, expression_values = "raw", verbose = TRUE) codex_test <- normalizeGiotto(gobject = codex_test, scalefactor = 6000, verbose = TRUE, log_norm = FALSE, library_size_norm = FALSE, scale_feats = FALSE, scale_cells = TRUE) ## add gene & cell statistics codex_test <- addStatistics(gobject = codex_test, expression_values = "normalized") ## adjust expression matrix for technical or known variables codex_test <- adjustGiottoMatrix(gobject = codex_test, expression_values = "normalized", batch_columns = "sample_Xtile_Ytile", covariate_columns = NULL, return_gobject = TRUE, update_slot = "custom") ``` -------------------------------- ### Simple Spatial Visualization Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.Xenium.ipynb Performs a simple spatial visualization using SpatDimPlot, displaying specified layers and features. Adjust 'nmols' for density representation. ```r options(repr.plot.width = 7, repr.plot.height = 6) SpatDimPlot( g, image = "black", # put shapes at last layers = c("image", "points", "shapes"), features = c("ABCC11", "ACE2", "ACKR1", "ACTA2", "ACTG2", "ADAM28"), shapes_border_color = "cyan", shapes_border_size = 0.1, shapes_fill_by = "black", points_size = 0.1, nmols = 10000 ) ``` -------------------------------- ### Basic Spatial Plotting with Giotto Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.CODEX.ipynb Generates a basic spatial plot of the Giotto object. Use for initial visualization of cell locations. ```python options(repr.plot.width = 7, repr.plot.height = 7) SpatDimPlot(codex_test, color_by = "lightblue", size = 0.25) ``` -------------------------------- ### Spatial Plotting by Sample Group Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.CODEX.ipynb Creates a spatial plot grouped by sample tiles. Useful for visualizing distinct sample regions. ```python options(repr.plot.width = 14, repr.plot.height = 7) SpatDimPlot( codex_test, group_by = "sample_Xtile_Ytile", size = 0.2 ) ``` -------------------------------- ### Load Annotated Visium Mouse Cortex Data Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_Visium.ipynb Loads annotated Visium mouse cortex data from an RData file. The loaded object 'visium.brain' contains Seurat object information. ```r # https://figshare.com/articles/dataset/10X_visium_data_for_spatial-informed_cell-cell_communication/23621151?file=41446839 load("data/visium_mouse_cortex_annotated.RData") visium.brain ``` -------------------------------- ### Visualize Clusters on Spatial Units Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.VisiumHD.ipynb Displays spatial units colored by Leiden cluster, with points overlaid. Useful for understanding the spatial distribution of cell clusters. Requires Giotto. ```python options(repr.plot.width = 7, repr.plot.height = 6) visiumHD@cell_metadata$hex400$rna$leiden_clus <- as.factor(visiumHD@cell_metadata$hex400$rna$leiden_clus) SpatFeaturePlot( visiumHD, image = "black", shapes = TRUE, spat_unit = 'hex400', shapes_fill_by = "leiden_clus", shapes_feat_type = 'hex400', points_size = 0.1 ) ``` -------------------------------- ### Run scVelo pipeline with Python Source: https://github.com/pwwang/scplotter/blob/master/notebooks/Working_with_anndata_h5ad.ipynb Execute a Python script using reticulate to run the scVelo pipeline for velocity analysis on anndata objects. This includes filtering, normalization, moment calculation, and velocity embedding. ```python import scanpy as sc import scvelo as scv adata = sc.read_h5ad('./data/pancreas.h5ad') scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=2000, log=True) scv.pp.moments(adata, n_pcs=30, n_neighbors=30) scv.tl.velocity(adata, mode='stochastic') scv.tl.velocity_graph(adata) scv.tl.velocity_embedding(adata, basis='pca') adata.write('./data/pancreas_velocity.h5ad') ``` ```r python_script <- " import scanpy as sc import scvelo as scv adata = sc.read_h5ad('./data/pancreas.h5ad') scv.pp.filter_and_normalize(adata, min_shared_counts=20, n_top_genes=2000, log=True) scv.pp.moments(adata, n_pcs=30, n_neighbors=30) scv.tl.velocity(adata, mode='stochastic') scv.tl.velocity_graph(adata) scv.tl.velocity_embedding(adata, basis='pca') adata.write('./data/pancreas_velocity.h5ad') " if (!file.exists("./data/pancreas_velocity.h5ad")) { reticulate::py_run_string(python_script) } ``` -------------------------------- ### Visualize Spatial Features Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.VisiumHD.ipynb Generates spatial plots to visualize features like the number of detected features per cell. Requires Giotto and plotting configurations. ```python options(repr.plot.width = 7, repr.plot.height = 6) SpatFeaturePlot( visiumHD, features = "nr_feats", points_size = 6, points_shape = 21 ) ``` -------------------------------- ### Spatial Feature Plot with Cropping Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.Xenium.ipynb Demonstrates two methods for cropping spatial plots: using the Crop function to create a new FOV object and using the 'ext' argument directly in SpatFeaturePlot. ```r options(repr.plot.width = 8, repr.plot.height = 7) crop <- Crop(xenium.obj[["fov"]], x = c(600, 2100), y = c(900, 4700)) xenium.obj[["crop"]] <- crop p1 <- SpatFeaturePlot(xenium.obj, fov = "crop", features = "Slc17a7", image = "black", size = 0.2, upper_quantile = .95) # use ext argument to crop p2 <- SpatFeaturePlot(xenium.obj, ext = c(600, 2100, 900, 4700), features = "Slc17a7", image = "black", size = 0.2, upper_quantile = .95) p1 + p2 ``` -------------------------------- ### JupyterLab Input Focus and Hover Styling Source: https://github.com/pwwang/scplotter/blob/master/pkgdown/assets/Seurat.XeniumInSitu.html Defines focus and hover states for input and select elements in JupyterLab's FormGroup. Includes outline and box-shadow for focus, and background for hover. ```css /* stylelint-disable selector-max-type */ .jp-FormGroup-content fieldset input:focus, .jp-FormGroup-content fieldset select:focus { -moz-outline-radius: unset; outline: var(--jp-border-width) solid var(--md-blue-500); outline-offset: -1px; box-shadow: inset 0 0 4px var(--md-blue-300); } .jp-FormGroup-content fieldset input:hover:not(:focus), .jp-FormGroup-content fieldset select:hover:not(:focus) { background-color: var(--jp-border-color2); } /* stylelint-enable selector-max-type */ ``` -------------------------------- ### Create Spatial Network Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Spatial_CITE-Seq.ipynb Constructs a spatial network based on k-Nearest Neighbors (kNN) or maximum distance. This is crucial for spatial analysis and feature binning. ```r my_giotto_object <- createSpatialNetwork(gobject = my_giotto_object, method = "kNN", k = 6, maximum_distance_knn = 5, name = "spatial_network") ``` -------------------------------- ### Render Mermaid Diagrams Source: https://github.com/pwwang/scplotter/blob/master/pkgdown/assets/Seurat.XeniumInSitu.html Initializes Mermaid for rendering diagrams. It handles diagram parsing, SVG generation, error handling, and cleaning of SVG output. This is executed when the DOM is ready and Mermaid diagrams are present. ```javascript document.addEventListener("DOMContentLoaded", async () => { const diagrams = document.querySelectorAll(".jp-Mermaid > pre.mermaid"); // do not load mermaidjs if not needed if (!diagrams.length) { return; } const mermaid = (await import("https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs")).default; const parser = new DOMParser(); mermaid.initialize({ maxTextSize: 100000, maxEdges: 100000, startOnLoad: false, fontFamily: window .getComputedStyle(document.body) .getPropertyValue("--jp-ui-font-family"), theme: document.querySelector("body[data-jp-theme-light='true']") ? "default" : "dark", }); let _nextMermaidId = 0; function makeMermaidImage(svg) { const img = document.createElement("img"); const doc = parser.parseFromString(svg, "image/svg+xml"); const svgEl = doc.querySelector("svg"); const { maxWidth } = svgEl?.style || {}; const firstTitle = doc.querySelector("title"); const firstDesc = doc.querySelector("desc"); img.setAttribute("src", `data:image/svg+xml,${encodeURIComponent(svg)}`); if (maxWidth) { img.width = parseInt(maxWidth); } if (firstTitle) { img.setAttribute("alt", firstTitle.textContent); } if (firstDesc) { const caption = document.createElement("figcaption"); caption.className = "sr-only"; caption.textContent = firstDesc.textContent; return [img, caption]; } return [img]; } async function makeMermaidError(text) { let errorMessage = ""; try { await mermaid.parse(text); } catch (err) { errorMessage = `${err}`; } const result = document.createElement("details"); result.className = 'jp-RenderedMermaid-Details'; const summary = document.createElement("summary"); summary.className = 'jp-RenderedMermaid-Summary'; const pre = document.createElement("pre"); const code = document.createElement("code"); code.innerText = text; pre.appendChild(code); summary.appendChild(pre); result.appendChild(summary); const warning = document.createElement("pre"); warning.innerText = errorMessage; result.appendChild(warning); return [result]; } async function renderOneMarmaid(src) { const id = `jp-mermaid-${_nextMermaidId++}`; const parent = src.parentNode; let raw = src.textContent.trim(); const el = document.createElement("div"); el.style.visibility = "hidden"; document.body.appendChild(el); let results = null; let output = null; try { let { svg } = await mermaid.render(id, raw, el); svg = cleanMermaidSvg(svg); results = makeMermaidImage(svg); output = document.createElement("figure"); results.map(output.appendChild, output); } catch (err) { parent.classList.add("jp-mod-warning"); results = await makeMermaidError(raw); output = results[0]; } finally { el.remove(); } parent.classList.add("jp-RenderedMermaid"); parent.appendChild(output); } /** * Post-process to ensure mermaid diagrams contain only valid SVG and XHTML. */ function cleanMermaidSvg(svg) { return svg.replace(RE_VOID_ELEMENT, replaceVoidElement); } /** * A regular expression for all void elements, which may include attributes and * a slash. * * @see https://developer.mozilla.org/en-US/docs/Glossary/Void_element * * Of these, only `
` is generated by Mermaid in place of `\n`, * but _any_ "malformed" tag will break the SVG rendering entirely. */ const RE_VOID_ELEMENT = /<\s*(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)\s*([^>]*?)\s*>/gi; /** * Ensure a void element is closed with a slash, preserving any attributes. */ function replaceVoidElement(match, tagName, attributes) { return `<${tagName}${attributes ? ` ${attributes}` : ""}/>`; } for (const src of diagrams) { renderOneMarmaid(src); } }); ``` -------------------------------- ### Plot Spatial Dimensions with Different Themes Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_Visium.ipynb Generates two spatial dimension plots for the 'visium.brain' object, one with a default theme and another with a black background image. Combines the plots. ```r options(repr.plot.width = 14, repr.plot.height = 5) p1 <- SpatDimPlot(visium.brain, theme = "theme_this") p2 <- SpatDimPlot(visium.brain, theme = "theme_this", image = "black") p1 + p2 ``` -------------------------------- ### Import and Append Xenium Data Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Xenium.ipynb Imports Xenium data using `importXenium` and appends expression and cell metadata to an existing Giotto object. This step is crucial for combining raw Xenium data with Giotto's analytical framework. ```r x <- importXenium(data_path) x$filetype$expression <- "mtx" # change to mtx instead of .h5 which is not in the mini dataset ex <- x$load_expression() featType(ex) featType(ex[[2]]) <- c("NegControlProbe") featType(ex[[3]]) <- c("NegControlCodeword") featType(ex[[4]]) <- c("UnassignedCodeword") force(ex) g2 <- g # append the expression info g2 <- setGiotto(g2, ex) # load cell metadata cx <- x$load_cellmeta() g2 <- setGiotto(g2, cx) force(g2) ``` -------------------------------- ### Preprocessing and Dimensionality Reduction for CosMx Data Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Nanostring_CosMx.ipynb Perform essential preprocessing steps including calculating highly variable features, running PCA, t-SNE, UMAP, nearest network creation, and Leiden clustering. Ensure sufficient global variable size for `future.apply` if errors occur. ```r cosmx <- calculateHVF(cosmx, method = "var_p_resid", var_number = 500, # leaving as NULL results in too few genes to use save_plot = TRUE, save_param = list( save_name = "10_hvf" ) ) # If you get an Error related to future.apply, please modify the maximum size # of global variables by running: options(future.globals.maxSize = 1e10) # print HVFs gene_metadata <- fDataDT(cosmx) gene_metadata[hvf == "yes", feat_ID] cosmx <- runPCA(cosmx, scale_unit = FALSE, center = FALSE, expression_values = "pearson" ) cosmx <- runtSNE(cosmx, dimensions_to_use = 1:10 ) cosmx <- runUMAP(cosmx, dimensions_to_use = 1:10 ) cosmx <- createNearestNetwork(cosmx, dimensions_to_use = 1:10, k = 10 ) cosmx <- doLeidenCluster(cosmx, resolution = 0.07, n_iterations = 100 ) ``` -------------------------------- ### Visualize Spatial Clusters Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Seurat.10x_VisiumHD.ipynb Simulates clustering and visualizes the spatial distribution of these clusters using SpatDimPlot. Set seed for reproducibility. ```r # simulate the clustering set.seed(8525) object$seurat_clusters <- paste0("c", sample(1:5, ncol(object), replace = TRUE)) Idents(object) <- "seurat_clusters" options(repr.plot.width = 6, repr.plot.height = 5) SpatDimPlot(object) ``` -------------------------------- ### Perform Dimensionality Reduction and Clustering Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.VisiumHD.ipynb Calculates highly variable features, runs PCA and UMAP, creates a nearest neighbor network, and performs Leiden clustering. Requires Giotto. ```python visiumHD <- calculateHVF(visiumHD, zscore_threshold = 1) visiumHD <- runPCA(visiumHD, expression_values = 'normalized', feats_to_use = 'hvf') visiumHD <- runUMAP(visiumHD, dimensions_to_use = 1:14, n_threads = 10) # sNN network (default) visiumHD <- createNearestNetwork(visiumHD, dimensions_to_use = 1:14, k = 5) ## leiden clustering #### visiumHD <- doLeidenClusterIgraph(visiumHD, resolution = 0.5, n_iterations = 1000, spat_unit = 'hex400') ``` -------------------------------- ### Create RNA Nearest Network Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Spatial_CITE-Seq.ipynb Builds a k-Nearest Neighbors (kNN) network for the RNA modality using PCA for dimension reduction. Specify the number of dimensions and k neighbors. ```r my_giotto_object <- createNearestNetwork(gobject = my_giotto_object, spat_unit = "cell", feat_type = "rna", type = "kNN", dim_reduction_name = "rna.pca", name = "rna_kNN.pca", dimensions_to_use = 1:10, k = 20) ``` -------------------------------- ### Dimensionality Reduction and Clustering Source: https://github.com/pwwang/scplotter/blob/master/notebooks/spatial/Giotto.Visium.ipynb Perform PCA, UMAP, tSNE, create nearest networks, and run Leiden/Louvain clustering. Converts cluster results to factors. ```r visium_brain <- runPCA(gobject = visium_brain) my_features <- head(getFeatureMetadata(visium_brain, output = "data.table")$feat_ID, 1000) visium_brain <- runPCA(gobject = visium_brain, feats_to_use = my_features, name = "custom_pca") visium_brain <- runUMAP(visium_brain, dimensions_to_use = 1:10) visium_brain <- runtSNE(gobject = visium_brain, dimensions_to_use = 1:10) visium_brain <- createNearestNetwork(gobject = visium_brain, dimensions_to_use = 1:10, k = 15) visium_brain <- createNearestNetwork(gobject = visium_brain, dimensions_to_use = 1:10, k = 15, type = "kNN") visium_brain <- doLeidenCluster(gobject = visium_brain, resolution = 0.4, n_iterations = 1000) visium_brain <- doLouvainCluster(visium_brain) visium_brain@cell_metadata$cell$rna$leiden_clus <- as.factor(visium_brain@cell_metadata$cell$rna$leiden_clus) visium_brain@cell_metadata$cell$rna$louvain_clus <- as.factor(visium_brain@cell_metadata$cell$rna$louvain_clus) ```