sparrow.tb.leiden#
- sparrow.tb.leiden(sdata, labels_layer, table_layer, output_layer, calculate_umap=True, calculate_neighbors=True, rank_genes=True, n_neighbors=35, n_pcs=17, resolution=0.8, key_added='leiden', index_names_var=None, index_positions_var=None, random_state=100, overwrite=False, **kwargs)#
Applies leiden clustering on the
table_layerof the SpatialData object with optional UMAP calculation and gene ranking.This function executes the leiden clustering algorithm (via
sc.tl.leiden) on spatial data encapsulated by a SpatialData object. It optionally computes a UMAP (Uniform Manifold Approximation and Projection) for dimensionality reduction and ranks genes based on their contributions to the clustering. The clustering results, along with optional UMAP and gene ranking, are added to thesdata.tables[output_layer]for downstream analysis.- Parameters:
sdata (
SpatialData) – The input SpatialData object.labels_layer (
str|list[str] |None) – The labels layer(s) ofsdataused to select the cells via the_REGION_KEYinsdata.tables[table_layer].obs. Note that ifoutput_layeris equal totable_layerandoverwriteisTrue, cells insdata.tables[table_layer]linked to otherlabels_layer(via the_REGION_KEY), will be removed fromsdata.tables[table_layer]. If a list of labels layers is provided, they will therefore be clustered together (e.g. multiple samples).table_layer (
str) – The table layer insdataon which to perform clustering on.output_layer (
str) – The output table layer insdatato which table layer with results of clustering will be written.calculate_umap (
bool(default:True)) – IfTrue, calculates a UMAP viascanpy.tl.umapfor visualization of computed clusters.calculate_neighbors (
bool(default:True)) – IfTrue, calculates neighbors viascanpy.pp.neighborsrequired for leiden clustering. Set to False if neighbors are already calculated forsdata.tables[table_layer].rank_genes (
bool(default:True)) – IfTrue, ranks genes based on their contributions to the clusters viascanpy.tl.rank_genes_groups. TODO: To be moved to a separate function.n_neighbors (
int(default:35)) – The number of neighbors to consider when calculating neighbors viascanpy.pp.neighbors. Ignored ifcalculate_umapisFalse.n_pcs (
int(default:17)) – The number of principal components to use when calculating neighbors viascanpy.pp.neighbors. Ignored ifcalculate_umapisFalse.resolution (
float(default:0.8)) – Cluster resolution passed toscanpy.tl.leiden.key_added (
str(default:'leiden')) – The key under which the clustering results are added to the SpatialData object (insdata.tables[table_layer].obs).index_names_var (
Iterable[str] |None(default:None)) – List of index names to subset insdata.tables[table_layer].var.index_positions_varwill be used ifNone.index_positions_var (
Iterable[int] |None(default:None)) – List of integer positions to subset insdata.tables[table_layer].var. Used ifindex_names_varisNone.random_state (
int(default:100)) – A random state for reproducibility of the clustering.overwrite (
bool(default:False)) – IfTrue, overwrites theoutput_layerif it already exists insdata.**kwargs – Additional keyword arguments passed to the leiden clustering algorithm (
sc.tl.leiden).
- Returns:
: The input
sdatawith the clustering results added.
Notes
The function updates the SpatialData object in-place, adding clustering labels, and optionally UMAP coordinates and gene rankings, facilitating downstream analyses and visualization.
Gene ranking based on cluster contributions is intended for identifying marker genes that characterize each cluster.
Warning
The function is intended for use with spatial omics data. Input data should be appropriately preprocessed (e.g. via
sparrow.tb.preprocess_transcriptomicsorsparrow.tb.preprocess_proteomics) to ensure meaningful clustering results.The
rank_genesfunctionality is marked for relocation to enhance modularity and clarity of the codebase.
See also
sparrow.tb.preprocess_transcriptomicspreprocess transcriptomics data.
sparrow.tb.preprocess_proteomicspreprocess proteomics data.