satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.29k stars 915 forks source link

Trouble creating seurat obj from matrix #7739

Closed JunlinJLiu closed 4 months ago

JunlinJLiu commented 1 year ago

I don't know if this has been asked before but I could find anything yet...

I tried to read an hdf5 sc-seq file into Seurat but it was too large so I wanted to load the first 10000 data. The data is in the following format: h5ls("expression_matrix.hdf5")

group    name       otype  dclass             dim
0     /    data   H5I_GROUP                        
1 /data  counts H5I_DATASET INTEGER 1169320 x 31053
2 /data    gene H5I_DATASET  STRING           31053
3 /data samples H5I_DATASET  STRING         1169320
4 /data   shape H5I_DATASET INTEGER               2

According to the structure I used the following code to load the first 10000 data:

counts <- h5read("expression_matrix.hdf5", "/data/counts", start = c(1,1), count = c(10000, 31053)) 
genes <- h5read("expression_matrix.hdf5", "/data/gene") # first 10,000 genes 
samples <- h5read("expression_matrix.hdf5", "/data/samples")  [1:10000]  
counts <- t(counts) 
rownames(counts) <- genes 
colnames(counts) <- samples 
Mouse <- CreateSeuratObject(counts = counts, project = "mouse_sample", min.cells = 3, min.features = 200)

The last step gave me this error:

Counts matrix provided is not sparse. Creating V5 assay in Seurat Object.
Error in slot(object = object, name = "features")[[layer]] <- features :  more elements supplied than there are to replace

I had no clue what caused this error. Can anybody give me a hint?

samuel-marsh commented 1 year ago

Hi,

Not member of dev team but hopefully can be helpful. Have you tried reading in file using Seurat V5 beta and the BPCells package (https://satijalab.org/seurat/articles/seurat5_bpcells_interaction_vignette)? Do you still have issues using that pipeline?

Best, Sam

JunlinJLiu commented 1 year ago

Have you tried reading in file using Seurat V5 beta and the BPCells package (https://satijalab.org/seurat/articles/seurat5_bpcells_interaction_vignette)? Do you still have issues using that pipeline?

Thanks for the reply Sam! I think the problem with my data is that everything is nested under data so instead of finding counts, gene, and sample we have to look at data/counts etc. And I couldn't find anything in these methods that let me adjust that path. And that's basically why I'm manually loading the matrix and converting them into seurat.

clee700 commented 11 months ago

Did you find a solution? I have the same issue.

clee700 commented 11 months ago

I still do not know what caused the issue or what it meant, but creating it as an Assay Object then turning that into a Seurat object seems to have worked.

dat = CreateAssayObject(gmat.use) dat = CreateSeuratObject(dat)

erzakiev commented 8 months ago

I encounter a similar error, while also reading in an h5 data in CSR/CSC format... The CreateAssayObject workaround by clee700 worked...

Error in slot(object = object, name = "cells")[[layer]] <- cells : 
  more elements supplied than there are to replace
zhangfeiran commented 5 months ago

It turns out this is due to row/colnames of counts must be character but not array, thus

genes <- as.character(genes)
cells <- as.character(cells)

before assigning the row/colnames solved the problem.

mhkowalski commented 4 months ago

Hello,

Thanks everyone for your contributions. I'm closing this now as it appears to be resolved, but please open a new issue if you are continuing to have trouble. Thanks!

yojetsharma commented 3 days ago

I have been facing this. The suggested solutions didn't resolve this issue for me.