findNiches.Rd
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"
)
SpatialExperiment or SingleCellExperiment object containing PhiSpace scores.
Name of the reducedDim containing PhiSpace scores (default: "PhiSpace").
Integer or vector of integers. Number(s) of niches (clusters) to identify. If a vector is provided, multiple clustering results will be generated.
Logical. Whether to perform PCA before clustering (default: TRUE).
Integer. Number of principal components to use if use_pca = TRUE. If NULL, defaults to half the number of cell types in PhiSpace scores.
Logical. Whether to center data for PCA (default: TRUE).
Logical. Whether to scale data for PCA (default: FALSE).
Integer. Maximum number of iterations for k-means (default: 500).
Integer. Number of random starts for k-means (default: 20).
Character. Algorithm for k-means clustering (default: "Lloyd").
Integer. Random seed for reproducibility (default: 94863).
Logical. Whether to store PCA results in the object (default: FALSE).
Character. Name for stored PCA results if store_pca = TRUE (default: "PhiSpace_PCA").
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.
This function identifies spatial niches by clustering cells/spots based on their PhiSpace cell type composition scores. The workflow consists of:
Extract PhiSpace scores from the specified reducedDim
Optionally perform PCA to reduce dimensionality and focus on major patterns
Apply k-means clustering to identify niches (for each k in n_niches)
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.
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")
} # }