SCTK allows users to do clustering on their data with various methods. The clustering methods that SCTK adopts are mainly graph based algorithms, such as Louvain and Leiden, which partitions a Shared Nearest-Neighbor (SNN) graph. The graph can be constructed by Scran [1] or Seurat [2], and the graph clustering is done by igraph or Seurat, respectively. Meanwhile, we also support traditional method such as K-Means [3].
SCTK allows different types data matrix as input. When using Scran’s SNN graph based clustering algorithms, users can specify either an expression matrix or a low-dimension representation as the input, while for Seurat’s methods and K-Means, users can only use the reduced dimensions. For any of the options, we recommend using a low-dimension representation as the input.
To view detailed instructions on how to use these methods, please select ‘Interactive Analysis’ for using clustering methods in Shiny application or ‘Console Analysis’ for using these methods in R console from the tabs below:
Entry of The Panel
From anywhere of the UI, the panel for clustering can be accessed from the top navigation panel at the circled tab shown below.
The UI consists of the parameter setting panel on the left and the visualization panel on the right.
Run Clustering
User will choose an algorithm to run the clustering at the very first step. The slide-down option list is constructed in a grouped style. Each group lists all the algorithm that a dependency (shown with grey text) supports. By selecting an algorithm that belongs to different groups, the parameter settings will change.
Method specific parameter settings are shown and explained below:
For the choices in the algorithm list, please refer to
igraph::cluster_louvain()
,
igraph::cluster_leiden()
,
igraph::cluster_walktrap()
,
igraph::cluster_infomap()
,
igraph::cluster_fast_greedy()
,
igraph::cluster_label_prop()
, and
igraph::cluster_leading_eigen()
.
assay
or altExp
,
respectively, or a reduced dimension, technically
reducedDim
, is allowed.K
is an
integer scalar specifying the number of nearest neighbors to consider
during graph construction. Considering more neighbors results in larger
groups. (See parameter k
of
scran::buildSNNGraph()
)resolution_parameter
of
igraph::cluster_leiden()
)steps
of
igraph::cluster_walktrap()
)type
of scran::buildSNNGraph()
)assay
or altExp
is chosen, the algorithm will
perform a PCA on the matrix and obtain the top PCs for downstream use.
This number could be determined with the help of an Elbow plot in the “Dimension Reduction” section.
For better visualization, we suggest generating 2D embedding with the same number of
components from the selected reduced dimensions.When the selected algorithm belongs to “K-Means” group, the parameter settings will look like the figure above.
reducedDim
and should be
obtained in advance from Dimensionality Reduction
tab.When the selected algorithm belongs to “Seurat” group, the parameter settings will look like the figure above.
reducedDim
and should be
obtained in advance from “Feature Selection & Dimension
Reduction” tab.Visualization
The visualization is implemented with a scatter plot of a chosen low-dimension embedding, colored with a chosen cluster assignment (the newly generated result by default). SCTK by default uses the matrix used for clustering calculation for the visualization. However, usually, users use a PCA for clustering while needing a UMAP/t-SNE embedding for visualization. To change the embedding used, please click on the blue settings (cog) button on the left-top corner.
colData(sce)
) are accessible if “Select
from All Present Annotation” is chosen.The clustering methods mentioned in the Introduction can be easily applied to any preprocessed SCE object. Meanwhile, there are other types of clustering methods supported by SCTK, but usually dependent to a curated workflow, such as the Celda Curated Workflow. Here we present the usage of the three functions only.
Basic Parameters
For each of the three functions, the common things are:
inSCE
.colData
column name to save the cluster labels -
clusterName
algorithm
As for another essential parameter, the data matrix used for running the algorithm is a must. Here, users should notice that:
runScranSNN()
, either a feature expression matrix
or a dimension reduction is acceptable. Users need to use:
useAssay
for a full-sized expression data
(assay
)useAltExp
and altExpAssay
for a subsetted
expression data (altExp
)useReducedDim
for a dimension reduction.
(reducedDim
)useAltExp
and altExpRedDim
for a reduced
dimension stored in the altExp
object.runKMeans()
, only a dimension reduction is
acceptable. Users need to use useReducedDim
to pass the
argument.runSeuratFindClusters()
, it is included within the
Seurat Curated Workflow, yet usable as an independent function. However,
it will be complicated to use this way. It is recommended to invoke the
Seurat clustering functionality in the UI since it is automated there.
We will still present the complicated workflow later.When using useReducedDim
as data input, users should be
aware of the number of top components being passed to the underlying
method. This is usually controlled by argument nComp
. To
determine this parameter, users can refer to the explanation in Dimensionality Reduction
Documentation. Meanwhile, for better visualization, we also
recommend users to generate 2D Embedding
using the same number of top components from the same dimension
reduction result.
Other parameters are method specific, please refer to the function manual pages for the detail. The typical command call for for each method is shown below:
# Scran method
sce <- runScranSNN(sce, useReducedDim = "PCA", clusterName = "scranSNN")
# K-means method
sce <- runKMeans(sce, nCenters = 9, useReducedDim = "PCA", clusterName = "KMeans")
# Seurat method
sce <- runSeuratFindClusters(sce, useAssay = "seuratScaledData")
Example
To demonstrate simple and clear examples, here we use the “PBMC-3k” dataset from “10X” which can be easily imported with SCTK functions. The preprocessing only includes necessary steps before getting cluster labels (i.e. QC and filtering are excluded).
library(singleCellTK)
sce <- importExampleData("pbmc3k")
sce <- runNormalization(sce, outAssayName = "logcounts", normalizationMethod = "logNormCounts")
# Default HVG method is "vst" from Seurat
sce <- runFeatureSelection(sce, useAssay = "counts")
sce <- setTopHVG(sce)
sce <- runDimReduce(sce, useAssay = "logcounts", useFeatureSubset = "HVG_vst2000", scale = TRUE, reducedDimName = "PCA")
# Optional visualization
sce <- runDimReduce(sce, method = "scaterUMAP", useReducedDim = "PCA", reducedDimName = "UMAP", nComponents = 10)
plotDimRed(sce, "UMAP")
k
, the number of nearest neighbors used to construct
the graph. Smaller value indicates higher resolution and larger number
of clusters.nComp
, the number of components to use when
useAssay
or useAltExp
is specified. WON’T work
with useReducedDim
.weightType
and algorithm
, users should
choose from a given list of options, which can be found with
?runScranSNN
. For the introduction of those options, please
refer to scran::buildSNNGraph()
and igraph.objective_function
and
resolution_parameter
for Leiden [6], steps
for Walktrap [7]. Please also refer to function reference for
detail.
# Directly use an assay
sce <- runScranSNN(sce, useAssay = "logcounts", clusterName = "scranSNN_logcounts")
plotSCEDimReduceColData(sce, colorBy = "scranSNN_logcounts", reducedDimName = "UMAP")
# Directly use a dimension reduction
sce <- runScranSNN(inSCE = sce, useReducedDim = "PCA", nComp = 10, clusterName = "scranSNN_PCA")
plotSCEDimReduceColData(inSCE = sce, colorBy = "scranSNN_PCA", reducedDimName = "UMAP")
nCenters
, the number of final clusters. This is
required.nIter
, the maximum number of iterations allowed.nStart
, the number of random sets to choose. Since
K-Means is an algorithm with reasonable randomness, the function allows
attempting multiple initial configurations and reports on the best
one.seed
, The seed for the random number generator.algorithm
, users should choose from a given list of
options, which can be found with ?runKMeans
. For the
introduction of those options, please refer to
?stats::kmeans
.
sce <- runKMeans(inSCE = sce, useReducedDim = "PCA", nComp = 10, nCenters = 10, clusterName = "kmeans")
plotSCEDimReduceColData(inSCE = sce, colorBy = "kmeans", reducedDimName = "UMAP")
Seurat clustering method is recommended to be used in the Seurat Curated Workflow in console analysis. When users preprocessed the dataset with other approaches, the clustering with Seurat function can be complicated, since some sorts of Seurat specific metadata is always needed.
# Prepare what seurat function needs
pca <- reducedDim(sce, "PCA")[,1:10]
rownames(pca) <- gsub("_", "-", rownames(pca))
stdev <- as.numeric(attr(pca, "percentVar"))
new_pca <- Seurat::CreateDimReducObject(embeddings = pca, assay = "RNA", stdev = stdev, key = "PC_")
# Then we can take the Seurat Curated Workflow independent dimension
# reduction as an input.
sce <- runSeuratFindClusters(sce, externalReduction = new_pca)
plotSCEDimReduceColData(inSCE = sce, colorBy = "Seurat_louvain_Resolution0.8", reducedDimName = "UMAP")