Skip to contents

To gain more insights into a tissues composition the clustering function can be applied. It is possible to cluster by expression values, deconvolution results or pathway and transcription factor activities.

library(spacedeconv)
library(SpatialExperiment)

data("single_cell_data_3")
data("spatial_data_3")
single_cell_data_3 <- spacedeconv::preprocess(single_cell_data_3)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#> ℹ testing parameter
#> ✔ parameter OK [93ms]
#> 
#> ℹ Removing 8 observations with umi count below threshold
#> ✔ Removed 8 observations with umi count below threshold [1.7s]
#> 
#> ℹ Removing 5862 variables with all zero expression
#> Warning in spacedeconv::preprocess(single_cell_data_3): There are 13
#> mitochondrial genes present. Consider removing them.
#> ✔ Removed 5862 variables with all zero expression [975ms]
#> 
#> ℹ Checking for ENSEMBL Identifiers
#> ! Warning: ENSEMBL identifiers detected in gene names
#> ℹ Checking for ENSEMBL Identifiersℹ Consider using Gene Names for first-generation deconvolution tools
#> ℹ Checking for ENSEMBL Identifiers✔ Finished Preprocessing [11ms]
spatial_data_3 <- spacedeconv::preprocess(spatial_data_3)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#> ℹ testing parameter
#> ✔ parameter OK [34ms]
#> 
#> ℹ Removing 137 observations with umi count below threshold
#> ✔ Removed 137 observations with umi count below threshold [132ms]
#> 
#> ℹ Removing 13049 variables with all zero expression
#> Warning in spacedeconv::preprocess(spatial_data_3): There are 13 mitochondrial
#> genes present. Consider removing them.
#> ✔ Removed 13049 variables with all zero expression [167ms]
#> 
#> ℹ Checking for ENSEMBL Identifiers
#> ! Warning: ENSEMBL identifiers detected in gene names
#> ℹ Checking for ENSEMBL Identifiersℹ Consider using Gene Names for first-generation deconvolution tools
#> ℹ Checking for ENSEMBL Identifiers✔ Finished Preprocessing [12ms]

spatial_data_3 <- spacedeconv::normalize(spatial_data_3, method = "cpm")
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#> ℹ testing parameter
#> ✔ parameter OK [21ms]
#> 
#> ℹ Normalizing using cpm
#> ✔ Finished normalization using cpm [1.6s]
#> 
#> ℹ Please note the normalization is stored in an additional assay
signature <- spacedeconv::build_model(
  single_cell_obj = single_cell_data_3,
  cell_type_col = "celltype_major",
  method = "spatialdwls", verbose = T
)
#> class of selected matrix:  dgCMatrix 
#> [1] "finished runPCA_factominer, method == factominer"
deconv <- spacedeconv::deconvolute(
  spatial_obj = spatial_data_3,
  single_cell_obj = single_cell_data_3,
  cell_type_col = "celltype_major",
  method = "spatialdwls",
  signature = signature,
  assay_sp = "cpm"
)
#> class of selected matrix:  dgCMatrix 
#> [1] "finished runPCA_factominer, method == factominer"

To cluster deconvolution results, set spmethod to the result prefix, for example "spatialdwls". You can further set the following parameters:

  • nclusters: Number of clusters you want, can be a range
  • spmethod: should be the deconvolution tool used, or progeny/dorothea when clustering decoupleR results
  • method: kmeans or hclust
  • dist_method: for hclust, which distance method to use (“correlation”, “euclidean”, “maximum”, “manhattan”, “canberra”, “binary”, “minkowski”)
  • hclust_method: for hclust, agglomeration method to us (“complete”, “ward.D”, “ward.D2”, “single”, “average”, “mcquitty”, “median”, “centroid”)

For expression-based clustering, set spmethod = "expression". This applies the Seurat clustering workflow to the counts assay. You can further set the following parameters:

  • clusres: Cluster resolution, check the Seurat Vignette for details.
  • pca_dim: Number of PCA dimensions to use
cluster <- spacedeconv::cluster(deconv, spmethod = "expression", clusres = 0.5)
#> ── spacedeconv ─────────────────────────────────────────────────────────────────
#> ℹ testing parameter
#> ✔ parameter OK [6ms]
#> 
#> ℹ Extracting data
#> ℹ Clustering: expression
#> ℹ Extracting dataℹ Cluster resolution: 0.5
#> ℹ Extracting data✔ Extracted data for clustering [25ms]
#> 
#> ℹ Extracting data
#> Warning: The `slot` argument of `GetAssayData()` is deprecated as of SeuratObject 5.0.0.
#> ℹ Please use the `layer` argument instead.
#> ℹ The deprecated feature was likely used in the Seurat package.
#>   Please report the issue at <https://github.com/satijalab/seurat/issues>.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: The `slot` argument of `SetAssayData()` is deprecated as of SeuratObject 5.0.0.
#> ℹ Please use the `layer` argument instead.
#> ℹ The deprecated feature was likely used in the Seurat package.
#>   Please report the issue at <https://github.com/satijalab/seurat/issues>.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Computing nearest neighbor graph
#> Computing SNN
#> ℹ Extracting data
#> ✔ Extracted data for clustering [26.3s]
plot_spatial(
  spe = cluster, # plot the clustering stored in this object
  result = "cluster_expression_res_0.5",
  title = "Clustering",
  density = F
)

With an available clustering you can extract the top features for each cluster. Here we extract the top features for each cluster based on expression, but we want the top features from the deconvolution results from this area. See the associated clusters in the plot above.

get_cluster_features(cluster, clusterid = "cluster_expression_res_0.5", spmethod = "spatialdwls")
#> $`0`
#> spatialdwls_Endothelial     spatialdwls_T.cells         spatialdwls_PVL 
#>               0.5972377               0.4385143               0.2566206 
#> 
#> $`1`
#> spatialdwls_Cancer.Epithelial      spatialdwls_Plasmablasts 
#>                     1.6133398                    -0.1128160 
#>           spatialdwls_B.cells 
#>                    -0.1579763 
#> 
#> $`2`
#>              spatialdwls_CAFs spatialdwls_Normal.Epithelial 
#>                     0.6753223                     0.6311670 
#>           spatialdwls_B.cells 
#>                     0.1644404 
#> 
#> $`3`
#> spatialdwls_Myeloid    spatialdwls_CAFs spatialdwls_T.cells 
#>           1.4896333           0.6364549           0.3913211 
#> 
#> $`4`
#> spatialdwls_Normal.Epithelial               spatialdwls_PVL 
#>                    1.78275640                    0.76915868 
#>      spatialdwls_Plasmablasts 
#>                   -0.06106109 
#> 
#> $`5`
#>           spatialdwls_Myeloid spatialdwls_Cancer.Epithelial 
#>                     0.8857356                     0.7512144 
#>       spatialdwls_Endothelial 
#>                     0.1940212