Identifies spatial niches by performing k-means clustering on PhiSpace cell type scores. Optionally performs PCA before clustering to reduce dimensionality and improve clustering performance. Can test multiple numbers of niches and return all results for comparison.

findNiches(
  spe,
  reducedDimName = "PhiSpace",
  n_niches,
  use_pca = TRUE,
  ncomp = NULL,
  center = TRUE,
  scale = FALSE,
  kmeans_iter = 500L,
  kmeans_nstart = 20L,
  kmeans_algorithm = "Lloyd",
  seed = 94863,
  store_pca = FALSE,
  pca_name = "PhiSpace_PCA"
)

Arguments

spe

SpatialExperiment or SingleCellExperiment object containing PhiSpace scores.

reducedDimName

Name of the reducedDim containing PhiSpace scores (default: "PhiSpace").

n_niches

Integer or vector of integers. Number(s) of niches (clusters) to identify. If a vector is provided, multiple clustering results will be generated.

use_pca

Logical. Whether to perform PCA before clustering (default: TRUE).

ncomp

Integer. Number of principal components to use if use_pca = TRUE. If NULL, defaults to half the number of cell types in PhiSpace scores.

center

Logical. Whether to center data for PCA (default: TRUE).

scale

Logical. Whether to scale data for PCA (default: FALSE).

kmeans_iter

Integer. Maximum number of iterations for k-means (default: 500).

kmeans_nstart

Integer. Number of random starts for k-means (default: 20).

kmeans_algorithm

Character. Algorithm for k-means clustering (default: "Lloyd").

seed

Integer. Random seed for reproducibility (default: 94863).

store_pca

Logical. Whether to store PCA results in the object (default: FALSE).

pca_name

Character. Name for stored PCA results if store_pca = TRUE (default: "PhiSpace_PCA").

Value

The input object with niche assignments added to colData. If a single n_niches is provided, results are stored as "spatial_niches". If multiple n_niches are provided, results are stored as "spatial_niches_k7", "spatial_niches_k8", etc. If store_pca = TRUE, PCA results are also stored in reducedDims.

Details

This function identifies spatial niches by clustering cells/spots based on their PhiSpace cell type composition scores. The workflow consists of:

  1. Extract PhiSpace scores from the specified reducedDim

  2. Optionally perform PCA to reduce dimensionality and focus on major patterns

  3. Apply k-means clustering to identify niches (for each k in n_niches)

  4. Store niche assignments in colData

PCA is recommended when you have many cell types (>20) as it can improve clustering by focusing on the main axes of variation in cell type composition.

When multiple values of n_niches are provided, the function will test each one and store all results, allowing you to compare different numbers of niches and choose the most appropriate one for your analysis.

Examples

if (FALSE) { # \dontrun{
# Basic usage - find 9 niches using PCA
spe <- findNiches(spe, n_niches = 9)

# Test multiple numbers of niches
spe <- findNiches(spe, n_niches = c(7, 8, 9, 10))

# Find niches without PCA
spe <- findNiches(spe, n_niches = 6, use_pca = FALSE)

# Custom PCA settings
spe <- findNiches(spe, n_niches = c(6, 8, 10), ncomp = 15, store_pca = TRUE)

# Access niche assignments
table(spe$spatial_niches)  # for single n_niches
table(spe$spatial_niches_k8)  # for multiple n_niches

# Visualize niches spatially
VizSpatial(spe, groupBy = "spatial_niches")
VizSpatial(spe, groupBy = "spatial_niches_k8")
} # }