Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add customized clustering info to seurat object #530

Closed
crazyhottommy opened this issue Jun 6, 2018 · 6 comments
Closed

add customized clustering info to seurat object #530

crazyhottommy opened this issue Jun 6, 2018 · 6 comments

Comments

@crazyhottommy
Copy link

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

@crazyhottommy
Copy link
Author

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

@leonfodoulian
Copy link
Contributor

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 object@meta.data. 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 object@meta.data as input to the group.by argument of Seurat::TSNEPlot().

# Add k-means clustering results to 'object@meta.data$k.means.clusters'
object@meta.data$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

@crazyhottommy
Copy link
Author

Thank you! this helped!

@thomasveith
Copy link

I am trying to do something similar

# Add our clonal groupings
katoiii_s@meta.data$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

@ibseq
Copy link

ibseq commented May 12, 2020

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

@bounlu
Copy link

bounlu commented May 28, 2020

@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)

andrewwbutler added a commit that referenced this issue Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants