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

Error when computing averaged expression values from seurat object: counts slot not existing #11

Closed
LiuCanidk opened this issue Apr 25, 2024 · 2 comments

Comments

@LiuCanidk
Copy link

Hi,
I encountered an error when computing averaged expression values from the seurat object.

sce=readRDS('sce.final.rm.rds')
library(Seurat)
library(METAFlux)
setwd('14.METAFlux')
df.sample=calculate_avg_exp(sce, 'orig.ident', n_bootstrap = 100, seed = 2024)

Error

Error in FUN(X[[i]], ...) : no slot of name "counts" for this object of class "Assay5"
3. FUN(X[[i]], ...)
2. lapply(1:n_bootstrap, get_ave_exp, myseurat, samples, myident) atC:\Users\Administrator\AppData\Local\Temp\RtmpWUlcOq\rstudio-scratch-233013cd6afa.R#6

  1. calculate_avg_exp(sce, "orig.ident", 1000, 2024)

I guess it's a problem with seuratV5 but found no specific information on how to solve this. My seurat object indeed has the counts layer.

sce
An object of class Seurat
51795 features across 35412 samples within 1 assay
Active assay: RNA (51795 features, 2000 variable features)
3 layers present: data, counts, scale.data
4 dimensional reductions calculated: pca, umap, harmony, UMAP_harmony

Any help would be greatly appreciated!

@yechanYS
Copy link

I've encountered similar issue.
Also, I think METAFlux package is not compatible with Seurat v5, yet.

Therefore, in Issue #5, I generated new codes for calculate_avg_exp() function by editing its source codes.
Also, I slightly changed these codes, from Issue #5, in order to get counts matrix from seurat v5 object.

For Seurat v5, I think it will be working if you execute below codes and use 'edit_calculate_avg_exp()' function instead of 'calculate_avg_exp()'.
All parameters are same with vignette.

generate_boots <- function(celltype, n) {
  dt <- data.frame(cluster = celltype, id = 1:length(celltype))
  index <- do.call(cbind, sapply(1:n, function(x) {
    splits <- dt %>%
      group_by(cluster) %>%
      sample_n(dplyr::n(), replace = TRUE) %>%
      ungroup() %>%
      dplyr::select("id")
  }))
  return(index)
}

get_ave_exp <- function(i, myseurat, samples,myident) {
  [email protected][samples[,i],]
  sample <-GetAssayData(myseurat, assay = "RNA")[,samples[,i]]
  name <- colnames(sample)
  for (j in 1:length(name)) {
    name[j] <- paste0(name[j], "_", j)
  }
  colnames(sample) <- name
  rownames(meta.data) <- name
  SeuratObject<-suppressWarnings(
    CreateSeuratObject(count=sample, meta.data = meta.data))
  SeuratObject<-NormalizeData(SeuratObject,verbose = TRUE)
  ave<-GetAssayData(AverageExpression(SeuratObject,group.by = myident,return.seurat = T), assay = "RNA") %>% as.data.frame()
  return(ave)
}

edit_calculate_avg_exp <- function(myseurat,myident,n_bootstrap,seed) {
  set.seed(seed)
  samples=generate_boots([email protected][,myident],n_bootstrap)
  exp <- lapply(1:n_bootstrap,get_ave_exp,myseurat,samples,myident)
  exp <- do.call(cbind, exp)
  return(exp)
}

mean_exp = edit_calculate_avg_exp(myseurat = sc, myident = 'celltype', n_bootstrap = 50, seed = 1) 

@LiuCanidk
Copy link
Author

Great! Thanks for your kind sharing code! @yechanYS
It worked after adjusting the source code. And I notice seuratV5 recommend using AggrerateExpression to perform pseudobulk analysis, maybe it is relevant. Anyway, again thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants