MERFISH

preamble

library(sf)
library(dplyr)
library(ggplot2)
library(patchwork)
library(SpatialData)
library(SpatialData.data)
library(SpatialData.plot)
(sd <- MouseBrainMERFISH())
class: SpatialData
- images(1):
  - rasterized (1,522,575)
- labels(0):
- points(1):
  - single_molecule (3714642)
- shapes(2):
  - anatomical (6,polygon)
  - cells (2389,circle)
- tables(1):
  - table (268,2389) [cells]
coordinate systems(1):
- global(4): rasterized anatomical cells single_molecule

analysis

feature_key(p <- point(sd))
[1] "cell_type"
data(p) |>
    distinct(cell_type) |>
    pull(cell_type)
[1] "VISp_wm"      "VISp_V"       "VISp"         "VISp_VI"      "VISp_I"      
[6] "VISp_II/III"  "VISp_IV"      "outside_VISp"
filter(p, cell_type == "VISp_V")
class: SpatialDataPoint
count: 589648 
data(3): cell_type __null_dask_index__ geometry
plotSpatialData() + 
    plotImage(sd) + 
    plotPoint(sd, n=222, key="VISp_V", col="cyan")

plotSpatialData() +
    plotImage(sd) + 
    plotShape(sd, linewidth=1, fill=NA, col="white") +   # regions
    plotPoint(sd, size=0.2, n=4e3, colour="cell_type") + # transcripts
    guides(col=guide_legend(override.aes=list(size=2))) +
    scale_color_manual(values=rainbow(8))

# downsample transcripts for speed
sp <- sd
p <- point(sp)
p <- p[sample(length(p), 2e4)]
point(sp) <- p
# query polygonal region + 10% expansion
s <- shape(sd, "anatomical")
ps <- lapply(seq_along(s), \(.) {
    roi <- st_buffer(st_as_sf(data(s[., ])), 0.1)
    sp <- crop(sp, roi)
    plotSpatialData() +
        plotImage(sp) + ggtitle(paste0("ROI", .)) +
        plotShape(sp, fill=NA, col="gold", size=0.8) +
        plotPoint(sp, i=1, n=2e3, col="turquoise", size=0.2) 
})
wrap_plots(ps, nrow=2) & theme_void() & theme(plot.title=element_text(hjust=0.5))

appendix

sessionInfo()
R version 4.6.0 (2026-04-24)
Platform: aarch64-apple-darwin23
Running under: macOS Sequoia 15.6.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.6/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] SpatialData.plot_0.99.6 SpatialData.data_0.99.6 SpatialData_0.99.35    
[4] patchwork_1.3.2         ggplot2_4.0.3           dplyr_1.2.1            
[7] sf_1.1-1               

loaded via a namespace (and not attached):
  [1] DBI_1.3.0                   bitops_1.0-9               
  [3] RBGL_1.89.0                 httr2_1.2.2                
  [5] anndataR_1.3.0              rlang_1.2.0                
  [7] magrittr_2.0.5              Rarr_2.1.7                 
  [9] otel_0.2.0                  matrixStats_1.5.0          
 [11] e1071_1.7-17                compiler_4.6.0             
 [13] RSQLite_2.4.6               dir.expiry_1.21.0          
 [15] paws.storage_0.9.0          png_0.1-9                  
 [17] fftwtools_0.9-11            vctrs_0.7.3                
 [19] pkgconfig_2.0.3             wk_0.9.5                   
 [21] crayon_1.5.3                fastmap_1.2.0              
 [23] dbplyr_2.5.2                XVector_0.53.0             
 [25] paws.common_0.8.9           rmarkdown_2.31             
 [27] graph_1.91.0                purrr_1.2.2                
 [29] bit_4.6.0                   xfun_0.57                  
 [31] cachem_1.1.0                grumpy_0.1.0               
 [33] jsonlite_2.0.0              blob_1.3.0                 
 [35] DelayedArray_0.39.1         uuid_1.2-2                 
 [37] tweenr_2.0.3                jpeg_0.1-11                
 [39] tiff_0.1-12                 parallel_4.6.0             
 [41] R6_2.6.1                    RColorBrewer_1.1-3         
 [43] reticulate_1.46.0           GenomicRanges_1.65.0       
 [45] assertthat_0.2.1            Rcpp_1.1.1-1.1             
 [47] Seqinfo_1.3.0               SummarizedExperiment_1.43.0
 [49] knitr_1.51                  R.utils_2.13.0             
 [51] IRanges_2.47.0              Matrix_1.7-5               
 [53] tidyselect_1.2.1            duckspatial_1.0.0          
 [55] rstudioapi_0.18.0           dichromat_2.0-0.1          
 [57] abind_1.4-8                 EBImage_4.55.0             
 [59] curl_7.1.0                  lattice_0.22-9             
 [61] tibble_3.3.1                Biobase_2.73.1             
 [63] withr_3.0.2                 S7_0.2.2                   
 [65] evaluate_1.0.5              units_1.0-1                
 [67] proxy_0.4-29                polyclip_1.10-7            
 [69] BiocFileCache_3.3.0         pillar_1.11.1              
 [71] filelock_1.0.3              MatrixGenerics_1.25.0      
 [73] KernSmooth_2.23-26          stats4_4.6.0               
 [75] generics_0.1.4              RCurl_1.98-1.18            
 [77] nanoarrow_0.8.0             S4Vectors_0.51.1           
 [79] scales_1.4.0                class_7.3-23               
 [81] glue_1.8.1                  tools_4.6.0                
 [83] ggnewscale_0.5.2            locfit_1.5-9.12            
 [85] grid_4.6.0                  SingleCellExperiment_1.35.0
 [87] duckdb_1.5.2                basilisk_1.25.0            
 [89] ggforce_0.5.0               cli_3.6.6                  
 [91] rappdirs_0.3.4              S4Arrays_1.13.0            
 [93] arrow_24.0.0                geoarrow_0.4.2             
 [95] gtable_0.3.6                R.methodsS3_1.8.2          
 [97] digest_0.6.39               BiocGenerics_0.59.0        
 [99] classInt_0.4-11             SparseArray_1.13.2         
[101] ZarrArray_1.1.0             htmlwidgets_1.6.4          
[103] farver_2.1.2                memoise_2.0.1              
[105] htmltools_0.5.9             R.oo_1.27.1                
[107] lifecycle_1.0.5             bit64_4.8.0                
[109] MASS_7.3-65