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

FeaturePlot for average expression level of a set of genes #528

Closed
ysbioinfo opened this issue Jun 6, 2018 · 7 comments
Closed

FeaturePlot for average expression level of a set of genes #528

ysbioinfo opened this issue Jun 6, 2018 · 7 comments

Comments

@ysbioinfo
Copy link

HI
Thank you for developing such a powerful and user-friendly software. I am analyzing some drop-seq data by Seurat. In your vignette, you show how to visualize a feature (usually the expression level of a gene) on the tSNE plot. But as you know, some cell types cannot be well defined by only one marker gene; using a set of genes may be a better choice. So I wonder if I could plot the average expression level of a gene set as a feature on the tSNE plot. Is this possible using FeaturePlot? If not, is there some tricks to do it with the Seurat object? Any advice will be appreciated.
Thank you very much!

Yang

@leonfodoulian
Copy link
Contributor

Hi Yang,

I am not sure if Seurat already has an implemented function for this, but here is my workaround for it:

# Select genes of interest (using sample() here for demonstration purposes)
gene.set <- sample(x = rownames(x = object@data), size = 100, replace = FALSE)

# Get mean expression of genes of interest per cell
mean.exp <- colMeans(x = object@data[gene.set, ], na.rm = TRUE)

# Add mean expression values in 'object@meta.data$gene.set.score'
if (all(names(x = mean.exp) == rownames(x = object@meta.data))) {
  cat("Cell names order match in 'mean.exp' and 'object@meta.data':\n", 
      "adding gene set mean expression values in 'object@meta.data$gene.set.score'")
  object@meta.data$gene.set.score <- mean.exp
}

# Plot mean expression using Seurat::FeaturePlot()
FeaturePlot(object = object, features.plot = "gene.set.score")

Steps are commented, but if you need further clarification as to what each code line is doing, please let me know.

Best,
Leon

@leonfodoulian
Copy link
Contributor

Also, I don't know if averaging scaled values might be a better choice here compared to simply averaging raw or normalised expression values. You can simply exchange object@data[gene.set, ] with object@scale.data[gene.set, ] when computing colMeans().

Best,
Leon

@ysbioinfo
Copy link
Author

Thanks for your quick reply! Your advice perfectly solve my problem!

@YiweiNiu
Copy link

Hi,

I would like to provide another solution using AverageExpression

# Select genes of interest (using sample() here for demonstration purposes)
gene.set <- sample(x = rownames(x = object@data), size = 100, replace = FALSE)

# stash original cell identity
obj <- StashIdent(obj, save.name = 'cluster')

# add decoy cell identity
Idents(obj) <- '1'

# compute average gene expression
gene.set.exp <- geneAverageExpression(obj, features = gene.set)

# add it to meta.data
obj@meta.data$gene.set.exp <- gene.set.exp[,1]

# plot
FeaturePlot(object = obj, features.plot = "gene.set.exp")

# restore original cell identity
Idents(obj) <- 'cluster'

Through changing the cell identity, average gene expression can also be easily computed across other groups (e.g. control/case, tumor/normal).

@In-tron
Copy link

In-tron commented Jan 8, 2021

Hi Yang,

I am not sure if Seurat already has an implemented function for this, but here is my workaround for it:

# Select genes of interest (using sample() here for demonstration purposes)
gene.set <- sample(x = rownames(x = object@data), size = 100, replace = FALSE)

# Get mean expression of genes of interest per cell
mean.exp <- colMeans(x = object@data[gene.set, ], na.rm = TRUE)

# Add mean expression values in 'object@meta.data$gene.set.score'
if (all(names(x = mean.exp) == rownames(x = object@meta.data))) {
  cat("Cell names order match in 'mean.exp' and 'object@meta.data':\n", 
      "adding gene set mean expression values in 'object@meta.data$gene.set.score'")
  object@meta.data$gene.set.score <- mean.exp
}

# Plot mean expression using Seurat::FeaturePlot()
FeaturePlot(object = object, features.plot = "gene.set.score")

Steps are commented, but if you need further clarification as to what each code line is doing, please let me know.

Best,
Leon

Thanks leon for the solution. Now I am encountering the error: "Error in colMeans(x = CD4T.integrated@meta.data[gene.set, ], na.rm = TRUE) : 'x' must be numeric". "CD4T.integrated" is the seurat object used. Do you have any suggestion to solve this problem? Thanks!

@MelinaPB
Copy link

MelinaPB commented May 24, 2024

Hi @In-tron

I had the same problem and I solved it changing the code a little bit:

# Get mean expression of genes of interest per cell
# I used:
mean.exp <- colMeans(x = seu@assays$RNA$data[gene.set, ], na.rm = TRUE)
#Insted of:
mean.exp <- colMeans(x = seu@data[gene.set, ], na.rm = TRUE)
 
# Plot mean expression using Seurat::FeaturePlot()
#I used: 
FeaturePlot(object = seu, features = "gene.set.score")
#Instead of: 
FeaturePlot(object = seu, features.plot = "gene.set.score")

Good luck,

Thanks leon for the solution. Now I am encountering the error: "Error in colMeans(x = CD4T.integrated@meta.data[gene.set, ], na.rm = TRUE) : 'x' must be numeric". "CD4T.integrated" is the seurat object used. Do you have any suggestion to solve this problem? Thanks!

@In-tron
Copy link

In-tron commented May 25, 2024

Hi @In-tron

I had the same problem and I solved it changing the code a little bit:

# Get mean expression of genes of interest per cell
# I used:
mean.exp <- colMeans(x = seu@assays$RNA$data[gene.set, ], na.rm = TRUE)
#Insted of:
mean.exp <- colMeans(x = seu@data[gene.set, ], na.rm = TRUE)
 
# Plot mean expression using Seurat::FeaturePlot()
#I used: 
FeaturePlot(object = seu, features = "gene.set.score")
#Instead of: 
FeaturePlot(object = seu, features.plot = "gene.set.score")

Good luck,

Thanks leon for the solution. Now I am encountering the error: "Error in colMeans(x = CD4T.integrated@meta.data[gene.set, ], na.rm = TRUE) : 'x' must be numeric". "CD4T.integrated" is the seurat object used. Do you have any suggestion to solve this problem? Thanks!

Thank you!

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