### Pre-processing data

SC-MEB requires minimal data pre-processing, but we provide a helper function to automate it.

spatialPreprocess() log-normalizes the count matrix and performs PCA on the top n.HVGs highly variable genes, keeping the top n.PCs principal components. Additionally, the spatial sequencing platform is added as metadata in the SingleCellExperiment for downstream analyses. If you do not wish to rerun PCA, running spatialPreprocess() with the flag skip.PCA=TRUE will only add the metadata SC-MEB requires.

```
set.seed(114)
library(scuttle)
library(scran)
library(scater)
library(BiocSingular)
CRC <- spatialPreprocess(CRC, platform="Visium")
```

Here, we set the basic paramters for our function `SC.MEB`

```
platform = "Visium"
beta_grid = seq(0,4,0.2)
K_set= 2:10
parallel=TRUE
num_core = 3
PX = TRUE
maxIter_ICM = 10
maxIter = 50
```

### Clustering

#### Selecting the number of clusters using BIC

`selectKPlot(fit, K_set = K_set, criterion = "BIC")`

#### Selecting the number of clusters using Modified BIC

Here we briefly explain how to choose the parameter c in the modified BIC. In general, For the ST or Visium dataset, it often ranges from 0.4 to 1 while for the MERFISH dataset with large number of cells, it often becomes larger, for example 10,20. Most importantly, SC-MEB is fast, scaling well in terms of sample size, which allow the user to tune the c based on their prior knowledge about the tissues or cells.

`selectKPlot(fit, K_set = K_set, criterion = "MBIC")`

#### Visualizing spatial clusters

We can plot the cluster assignments over the spatial locations of the spots with ClusterPlot().

```
out = selectK(fit, K_set = K_set, criterion = "BIC")
pos = matrix(cbind(colData(CRC)[,c(4)],20000-colData(CRC)[,c(3)]), 2988, 2)
ClusterPlot(out, pos, size = 3, shape = 16)
```

or

```
out = selectK(fit, K_set = K_set, criterion = "MBIC")
pos = matrix(cbind(colData(CRC)[,c(4)],20000-colData(CRC)[,c(3)]), 2988, 2)
ClusterPlot(out, pos, size = 3, shape = 16)
```