PRECAST: simulation

Wei Liu


This vignette introduces the PRECAST workflow for the analysis of integrating multiple spatial transcriptomics dataset. The workflow consists of three steps

We demonstrate the use of PRECAST to three simulated Visium data that are here, which can be downloaded to the current working path by the following command:

githubURL <- ""

Then load to R


The package can be loaded with the command:


Load the simulated data

First, we view the the three simulated spatial transcriptomics data with Visium platform.

data_simu ## a list including three Seurat object with default assay: RNA

Check the content in data_simu.


Create a PRECASTObject object

We show how to create a PRECASTObject object step by step. First, we create a Seurat list object using the count matrix and meta data of each data batch. Although data_simu is a prepared Seurat list object, we re-create a same objcet seuList to show the details.

## Get the gene-by-spot read count matrices
countList <- lapply(data_simu, function(x) x[["RNA"]]@counts)

## Get the meta data of each spot for each data batch
metadataList <- lapply(data_simu, function(x)

## ensure the row.names of metadata in metaList are the same as that of colnames count matrix in countList
M <- length(countList)
for(r in 1:M){
  row.names(metadataList[[r]]) <- colnames(countList[[r]])

## Create the Seurat list  object

seuList <- list()
for(r in 1:M){
  seuList[[r]] <- CreateSeuratObject(counts = countList[[r]],[[r]], project = "PRECASTsimu")

Prepare the PRECASTObject with preprocessing step.

Next, we use CreatePRECASTObject() to create a PRECASTObject based on the Seurat list object seuList. This function will do three things:

If the argument customGenelist is not NULL, then this function only does (3) based on customGenelist gene list.

In this simulated dataset, we don’t require to select genes, thus, we set customGenelist=row.names(seuList[[1]]), representing the user-defined gene list. User can retain the raw seurat list object by setting rawData.preserve = TRUE.

Fit PRECAST using simulated data

Add the model setting

Add adjacency matrix list and parameter setting of PRECAST. More model setting parameters can be found in model_set().


For function PRECAST, users can specify the number of clusters \(K\) or set K to be an integer vector by using modified BIC(MBIC) to determine \(K\). For convenience, we give a single K here.

Select a best model and use ARI to check the performance of clustering

Integrate the two samples by the function IntegrateSpaData.


First, user can choose a beautiful color schema using chooseColors().

cols_cluster <- chooseColors(palettes_name = 'Nature 10', n_colors = 7, plot_colors = TRUE)

Show the spatial scatter plot for clusters

p12 <- SpaPlot(seuInt, batch=NULL, cols=cols_cluster, point_size=2, combine=TRUE)
# users can plot each sample by setting combine=FALSE

Users can re-plot the above figures for specific need by returning a ggplot list object. For example, we only plot the spatial heatmap of first two data batches.

pList <- SpaPlot(seuInt, batch=NULL, cols=cols_cluster, point_size=2, combine=FALSE, title_name=NULL)
drawFigs(pList[1:2], layout.dim = c(1,2), common.legend = TRUE, legend.position = 'right', align='hv')

Show the spatial UMAP/tNSE RGB plot

seuInt <- AddUMAP(seuInt) 
SpaPlot(seuInt, batch=NULL,item='RGB_UMAP',point_size=1, combine=TRUE, text_size=15)

## Plot tSNE RGB plot
#seuInt <- AddTSNE(seuInt) 
#SpaPlot(seuInt, batch=NULL,item='RGB_TSNE',point_size=2, combine=T, text_size=15)

Show the tSNE plot based on the extracted features from PRECAST to check the performance of integration.

seuInt <- AddTSNE(seuInt, n_comp = 2) 

p1 <- dimPlot(seuInt, item='cluster', font_family='serif', cols=cols_cluster) # Times New Roman
p2 <- dimPlot(seuInt, item='batch', point_size = 1,  font_family='serif')
drawFigs(list(p1, p2), common.legend=FALSE, align='hv') 
# It is noted that only sample batch 1 has cluster 4, and only sample batch 2 has cluster 7. 

Show the UMAP plot based on the extracted features from PRECAST.

dimPlot(seuInt, reduction = 'UMAP3', item='cluster', cols=cols_cluster, font_family='serif')

Users can also use the visualization functions in Seurat package:

p1 <- DimPlot(seuInt[,1: 4226], reduction = 'position', cols=cols_cluster, pt.size =1) # plot the first data batch: first 4226 spots.
p2 <- DimPlot(seuInt, reduction = 'tSNE',cols=cols_cluster, pt.size=1)
drawFigs(list(p1, p2), layout.dim = c(1,2), common.legend = TRUE)

Combined differential expression analysis

dat_deg <- FindAllMarkers(seuInt)
n <- 2
dat_deg %>%
  group_by(cluster) %>%
  top_n(n = n, wt = avg_log2FC) -> top10


Session information