cluster
cluster ¶
cluster(
data,
mode="expression",
layer="raw",
subsetMarkers=None,
method="kmeans",
k=10,
nPcs=None,
leidenResolution=1,
leidenNearestNeighbors=30,
dbscanEps=0.5,
dbscanMinSamples=5,
log=True,
randomState=0,
subCluster=None,
subClusterGroup=None,
collapseLabels=False,
label=None,
verbose=True,
outputDir=None,
maxWorkers=None,
streamData=False,
sdataTable=None,
)
Cluster cells by expression, or by their neighbourhood composition.
Three methods are available — kmeans, leiden, and dbscan — over either of
two inputs:
mode="expression"clusters the marker matrix chosen bylayer. This is ordinary phenotyping-by-clustering.mode="spatial"clusters a neighbourhood matrix inuns, wherelayernames aunskey rather than alayerskey. Feed it the output ofneighCount,neighExp,neighLDA, orneighNMFand you get Recurrent Cellular Neighbourhoods. This replaces scimap's separatespatial_cluster.
Sub-clustering is built in: set subCluster to an existing label column and
subClusterGroup to the labels you want to split, and only those cells are
re-clustered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(AnnData | SpatialData | str, required)
|
The cell table. An |
required |
mode
|
str
|
|
'expression'
|
layer
|
str
|
In expression mode: |
'raw'
|
subsetMarkers
|
list
|
Cluster on these markers only. Expression mode. |
None
|
method
|
str
|
|
'kmeans'
|
k
|
int
|
Number of clusters, for |
10
|
nPcs
|
int
|
Principal components to reduce to before clustering, for |
None
|
leidenResolution
|
float
|
Leiden resolution. Higher gives more, smaller clusters. |
1
|
leidenNearestNeighbors
|
int
|
Neighbours used to build the Leiden graph. |
30
|
dbscanEps
|
float
|
DBSCAN neighbourhood radius, in the units of the clustered matrix. |
0.5
|
dbscanMinSamples
|
int
|
Minimum points for a DBSCAN core point. |
5
|
log
|
bool
|
Apply |
True
|
randomState
|
int
|
Seed, for reproducible clusters. |
0
|
subCluster
|
str
|
Column in |
None
|
subClusterGroup
|
list
|
Which labels of |
None
|
collapseLabels
|
bool
|
Give cells outside |
False
|
label
|
str
|
Column in |
None
|
maxWorkers
|
int
|
Cap on parallel workers. Defaults to |
None
|
verbose
|
bool
|
Print progress messages. |
True
|
outputDir
|
str
|
Directory to write the updated object to. With |
None
|
streamData
|
bool
|
Run out of core against an |
False
|
sdataTable
|
str
|
Which |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
adata |
AnnData | SpatialData | None
|
The updated object with |
Example
# Cluster on expression.
adata = sp.tl.cluster(adata, method="leiden", leidenResolution=1.0, label="leiden")
adata = sp.tl.umap(adata)
sp.pl.clusterPlots(adata, groupBy="leiden")
# Recurrent Cellular Neighbourhoods: cluster the neighbourhood matrix.
adata = sp.tl.neighCount(adata, phenotype="phenotype", radius=30)
adata = sp.tl.cluster(
adata, mode="spatial", layer="neighCount", method="kmeans", k=6,
label="neighbourhood",
)
sp.pl.spatialScatterPlot(adata, colorBy="neighbourhood", s=3)
# Split one existing cluster further, keeping the others intact.
adata = sp.tl.cluster(
adata,
method="kmeans",
k=4,
subCluster="leiden",
subClusterGroup=["3"],
collapseLabels=True,
label="leiden_refined",
)