Hi,
With the scaled (regressed) seurat object, how can I add K-means clustering or hierarchical clustering info to it?
# Create and setup Seurat objects for each dataset
WM115 <- CreateSeuratObject(raw.data = WM115.data, project = "WM115")
WM115 <- FilterCells(WM115, subset.names = c("nGene", "percent.mito"),
low.thresholds = c(500, -Inf), high.thresholds = c(Inf, 0.15))
WM115 <- NormalizeData(WM115)
WM115 <- FindVariableGenes(WM115 do.plot = F, display.progress = F)
## always scale z score for PCA
WM115<- ScaleData(object =WM115, vars.to.regress = c("nUMI", "percent.mito"))
# graph based clustering
WM115 <- FindClusters(WM115,
resolution = 0.6, dims.use = 1:10)
## how to add k-means and others?
....
Thanks!
Tommy
essentially I want to cluster single cells using k-means and color the points in the tsne plot. now the clustering is based on a graph-based (KNN) method, and do differentially expression based on the k-means clusters
Hi Tommy,
If you have already computed these clustering independently, and would like to add these data to the Seurat object, you can simply add the clustering results in any column in [email protected]. You can then set the clustering results as identity of your cells by using the Seurat::SetAllIdent() function. For an example on how to use this function, you can check issue #325. This should allow your cells to be coloured accordingly when plotting the tSNE embeddings using Seurat::TSNEPlot(). An alternative is to pass the column name storing these data in [email protected] as input to the group.by argument of Seurat::TSNEPlot().
# Add k-means clustering results to '[email protected]$k.means.clusters'
[email protected]$k.means.clusters <- k.means.result
# Plot tSNE, and colour cells by k-means identity
TSNEPlot(object = object, group.by = "k.means.clusters")
Best,
Leon
Thank you! this helped!
I am trying to do something similar
# Add our clonal groupings
[email protected]$membership <- membership
# Set cell identity to sample identity
katoiii_s <- SetIdent(object = katoiii_s, value = "membership")
# tSNE plot
katoiii_s <- TSNEPlot(object = katoiii_s, group.by = "membership")
And I get the error:
Error in `[[.Seurat`(object, reduction) :
Cannot find 'tsne' in this Seurat object
Hi
can we import customised clustering info from 10x (csv file with each barcode assigned to a cell type = cluster) into seurat, if yes, how can this be done?
thanks
ib
@crazyhottommy
You can also do like:
object$kmeans_10 = kmeans(x = object@reductions[["pca"]]@cell.embeddings, centers = 10, iter.max = 100, nstart = 100)$cluster
DimPlot(object, reduction = "tsne", label = T, group.by = "kmeans_10", split.by = "orig.ident") + NoLegend()
Idents(object) = "kmeans_10"
FindAllMarkers(object, assay = "RNA", only.pos = TRUE)
Most helpful comment
Hi Tommy,
If you have already computed these clustering independently, and would like to add these data to the Seurat object, you can simply add the clustering results in any column in
[email protected]. You can then set the clustering results as identity of your cells by using theSeurat::SetAllIdent()function. For an example on how to use this function, you can check issue #325. This should allow your cells to be coloured accordingly when plotting the tSNE embeddings usingSeurat::TSNEPlot(). An alternative is to pass the column name storing these data in[email protected]as input to thegroup.byargument ofSeurat::TSNEPlot().Best,
Leon