RDS to raw data matrices

I attempted to convert the rds file (Seurat Object) to raw data matrices for uploading in Cellenics. However, I fail to do so. Using their script, a demultiplex folder was created but without a matrics file. Can anyone tell me where I am wrong?

Hi! We would need a bit more detail to figure out what’s wrong. There might be an error printed in your R console when generating the count matrices. Could you upload that?

Thanks!

Here is my script.
library(Seurat)
library(“DropletUtils”)
seurat ← readRDS(“CRB1.D35.2samples.combined.rds”)
Seurat::DefaultAssay(seurat) ← “RNA”
counts ← seurat[[“RNA”]]@data
str(seurat)
data.pfx ← gsub(“(.+)_[A-Z]±1$”, “\1”, colnames(seurat), perl=TRUE)
sample.names ← unique(data.pfx)

demultiplex_convert_to_10x ← function(obj, samples) {
if(class(seurat) != “Seurat”) {
message(“WARNING: this rds file does not contain a Seurat object! STOP RUNNING THIS SCRIPT”)
message(“Check the data type by running:”)
message(“class(seurat)”)
stop()
}
if(!dir.exists(file.path(getwd(), “demultiplexed”))) {
dir.create(file.path(getwd(), “demultiplexed”))
} else {
print(“WARNING! A demultiplexed directory already exists”)
return()
}
for (i in 1:length(samples)) {
print(paste0(“Converting sample “, samples[i]))
obj.sub ← obj[[samples[i]]]
DropletUtils::write10xCounts(path = paste0(getwd(),”/demultiplexed/”,samples[i]), x = obj.sub[[“RNA”]]@data, type = “sparse”, version=“3”)
}
}

demultiplex_convert_to_10x(obj = seurat, samples = sample.names)

Hi,

My first suspect would the regex not being correct for the sample multiplexing.

Could you attach the output of the str(seurat) command?

In addition, you could check if there’s a metadata variable that we could use to split the seurat object more easily. They are usually stored in the seurat@meta.data slot. If you paste the output of the str(seurat@meta.data) command we can check. It could be a variable named ‘samples’ or something like that.

"Happy Thanksgiving”

Hi German,

Thank you for your prompt reply. Please find the attached screenshot of output.

I am looking for necessary support in my need.

Thank you

Hi Rupendra,

Thanks for the details. I think there could be 2 main problems here. One is the regex, and the other is that you’re correctly extracting the count matrix from the seurat object, but then you’re using the seurat object instead of the count matrix to demultiplex. In your case, the samples are encoded in the barcode names, so you should use the count matrix. To fix this, could you please try to do 2 things:

  1. Replace your regex (data.pfx ← gsub(“(.+)_[A-Z]±1$”, “\1”, colnames(seurat), perl=TRUE)) with this one: data.pfx <- gsub("(.+)_[A-Z]+-1$", "\\1", colnames(seurat), perl=TRUE)
    Make sure that it has +- instead of ± and \\1 instead of \1.

  2. Replace your demultiplex_convert_to_10x function with this one:

demultiplex_convert_to_10x <- function(obj, samples) {
  if(!dir.exists(file.path(getwd(), "demultiplexed"))) {
    dir.create(file.path(getwd(), "demultiplexed"))
  } else {
    print("WARNING! A demultiplexed directory already exists")
    return()
  }
  for (i in 1:length(samples)) {
    print(paste0("Converting sample ", samples[i]))
    DropletUtils::write10xCounts(path = paste0(getwd(),"/demultiplexed/",samples[i]), x = obj[,grep(paste0("^",samples[i],".*"),colnames(obj))], type = "sparse", version="3")
   }
}

demultiplex_convert_to_10x(obj = counts, samples = sample.names)

And call this function by giving as input the counts object instead of the seurat object, like this: demultiplex_convert_to_10x(obj = counts, samples = sample.names)
Make sure to delete your demultiplexed folder (if already exists) before running this.

Let us know if this works.

Hi Sara,

The script works without a hitch. I am grateful for the time and effort you have put in.

Thank you.

2 Likes