## ----eval=FALSE--------------------------------------------------------------- # if (!requireNamespace("BiocManager", quietly=TRUE)) # install.packages("BiocManager") # # BiocManager::install("tidySpatialExperiment") ## ----eval=FALSE--------------------------------------------------------------- # if (!requireNamespace("devtools", quietly=TRUE)) # install.packages("devtools") # # devtools::install_github("william-hutchison/tidySpatialExperiment") ## ----message=FALSE, results=FALSE--------------------------------------------- # Load example SpatialExperiment object library(tidySpatialExperiment) example(read10xVisium) ## ----------------------------------------------------------------------------- spe ## ----------------------------------------------------------------------------- spe |> colData() |> head() spe |> spatialCoords() |> head() spe |> imgData() ## ----------------------------------------------------------------------------- spe |> filter(array_col < 5) ## ----------------------------------------------------------------------------- spe |> mutate(in_region = c(in_tissue & array_row < 10)) ## ----------------------------------------------------------------------------- # Nest the SpatialExperiment object by sample_id spe_nested <- spe |> nest(data = -sample_id) # View the nested SpatialExperiment object spe_nested # Unnest the nested SpatialExperiment objects spe_nested |> unnest(data) ## ----------------------------------------------------------------------------- spe |> filter(sample_id == "section1" & in_tissue) |> # Add a column with the sum of feature counts per cell mutate(count_sum = purrr::map_int(.cell, ~ spe[, .x] |> counts() |> sum() )) |> # Plot with tidySpatialExperiment and ggplot2 ggplot(ggplot2::aes(x = reorder(.cell, count_sum), y = count_sum)) + ggplot2::geom_point() + ggplot2::coord_flip() ## ----eval=FALSE, message=FALSE, warning=FALSE--------------------------------- # spe |> # filter(sample_id == "section1") |> # plot_ly( # x = ~ array_col, # y = ~ array_row, # color = ~ in_tissue, # type = "scatter" # ) ## ----eval=FALSE--------------------------------------------------------------- # spe_regions <- # spe |> # filter(sample_id == "section1") |> # mutate(region = tidygate::gate_chr(array_col, array_row)) ## ----echo=FALSE--------------------------------------------------------------- # Manually set gate information to match demonstration spe_regions <- spe |> filter(sample_id == "section1") |> mutate(region = ifelse( array_row < 48 & array_row > 20 & array_col < 80 & array_col > 60, 1, 0)) ## ----------------------------------------------------------------------------- spe_regions_aggregated <- spe_regions |> aggregate_cells(region) ## ----------------------------------------------------------------------------- # Join feature data in wide format, preserving the SpatialExperiment object spe |> join_features(features = c("ENSMUSG00000025915", "ENSMUSG00000042501"), shape = "wide") |> head() # Join feature data in long format, discarding the SpatialExperiment object spe |> join_features(features = c("ENSMUSG00000025915", "ENSMUSG00000042501"), shape = "long") |> head() ## ----------------------------------------------------------------------------- spe |> aggregate_cells(in_tissue, assays = "counts") ## ----------------------------------------------------------------------------- spe |> filter(sample_id == "section1") |> mutate(in_ellipse = ellipse(array_col, array_row, c(20, 40), c(20, 20))) |> ggplot(aes(x = array_col, y = array_row, colour = in_ellipse)) + geom_point() ## ----------------------------------------------------------------------------- spe |> select(-.cell) |> head() ## ----error=TRUE--------------------------------------------------------------- # sample_id is not removed, despite the user's request spe |> select(-sample_id) # This change maintains separation of sample_ids and is permitted spe |> mutate(sample_id = stringr::str_c(sample_id, "_modified")) |> head() # This change does not maintain separation of sample_ids and produces an error spe |> mutate(sample_id = "new_sample") ## ----error=TRUE--------------------------------------------------------------- # Attempting to remove pxl_col_in_fullres produces an error spe |> select(-pxl_col_in_fullres) # Attempting to modify pxl_col_in_fullres produces an error spe |> mutate(pxl_col_in_fullres) ## ----------------------------------------------------------------------------- sessionInfo()