squidpy.gr.calculate_niche_utag
- squidpy.gr.calculate_niche_utag(data, *, resolutions, n_neighbors=15, use_layer=None, spatial_connectivities_key='spatial_connectivities', embedding_key_added='niche_embedding', use_rep=None, key_added='utag_niche', min_niche_size=None, cluster_mask=None, copy=False, table_key=None, flavor='igraph', n_iterations=-1, rng=None)[source]
Compute spatial niches from UTAG-style feature aggregation.
UTAG [Kim et al., 2022], adapted from https://github.com/ElementoLab/utag/blob/main/utag/segmentation.py
This method computes a spatially aggregated feature representation for each observation and clusters that representation with Leiden. The resulting niches group observations that occur in similar local molecular environments.
A spatial connectivity graph must already be available in
adata.obsp[spatial_connectivities_key]. The graph determines how features are propagated or aggregated across spatially neighboring observations; this function does not construct the graph itself.The method proceeds as follows:
Selects an input feature matrix from
adata.Xor fromadata.layers[use_layer].Performs a normalized (by number of cell-neighbors) aggregation of features over the spatial connectivity graph, producing a new feature matrix in which each observation reflects information from its local spatial neighborhood.
Reduces that matrix with PCA; the scores are the niche embedding.
Constructs a k-nearest-neighbor graph in that embedding space.
Applies Leiden clustering at each requested resolution.
If the input contains gene expression values, the embedding describes local expression programs rather than merely the categorical composition of neighboring cells.
This differs from
calculate_niche_neighborhood(), which uses counts or proportions of a categoricalgroupsannotation. UTAG-style aggregation can identify niches that have similar local expression patterns even when their neighborhoods contain different annotated cell types, or when cell-type labels are unavailable.- Parameters:
adata – Annotated data object.
resolutions (
float|list[float]) – Resolution parameter(s) for Leiden clustering. A single value produces one niche annotation. Supplying multiple values runs Leiden clustering independently at each resolution and adds one niche column per resolution toadata.obs.n_neighbors (
int) – Number of nearest neighbors used to construct the k-nearest-neighbor graph on the spatially aggregated embedding before Leiden clustering.Key in
adata.layerscontaining the feature matrix to aggregate. IfNone, usesadata.X.Typically, this should contain a normalized expression matrix or another observation-by-feature representation appropriate for local aggregation. The selected matrix determines what biological signal is used to define niches.
spatial_connectivities_key (
str) – Key inanndata.AnnData.obspcontaining the spatial connectivity matrix.cluster_mask (
Series|None) – Booleanpandas.Seriesindexed likeanndata.AnnData.obs.Falseobservations are labeled'not_a_niche'and take no part in the clustering, though they still reach their neighbors through the graph. Observations it omits are kept. To drop them from the graph as well, subset and rebuild it withspatial_neighbors().use_rep (
str|None) – Key inanndata.AnnData.obsmholding the representation to aggregate over each neighborhood, or'X'forX. Taken as already reduced, so the PCA of the aggregate is skipped. Rejected together withuse_layer.key_added (
str) – Stem of theanndata.AnnData.obscolumns the labels are written to, one per resolution, namedf"{key_added}_res={resolution}".embedding_key_added (
str) – Controls the name of the key to be added inanndata.AnnData.obsm, which will hold the niche embedding computed.min_niche_size (
int|None) – Minimum number of observations required for a niche. Niches with fewer observations are relabeled'not_a_niche'.copy (
bool) – IfTrue, return the result, otherwise save it to theadataobject.table_key (
str|None) – Key inspatialdata.SpatialData.tableswhere the table is stored. Required whenadatais aspatialdata.SpatialDataobject and ignored otherwise.flavor (
Literal['igraph','leidenalg']) – Leiden backend passed toscanpy.tl.leiden(). Defaults to'igraph'(the'leidenalg'backend is deprecated in scanpy).n_iterations (
int) – Number of Leiden iterations.-1iterates until convergence.rng (
int|integer|Sequence[int] |SeedSequence|Generator|BitGenerator|None) – Pseudorandom number generator state, following SPEC 7. When None, a newnumpy.random.Generatoris created using entropy from the operating system. Types other thannumpy.random.Generatorare passed tonumpy.random.default_rng()to instantiate a generator. Every resolution is clustered with an independent rng derived from it.
- Return type:
- Returns:
If
copy=True, returns a copy ofadatawith the aggregated features in.obsm[embedding_key_added], PCA-reduced unlessuse_repwas given, and niche assignments added to.obs. Otherwise, modifiesadatain place and returnsNone.