seurat-disk icon indicating copy to clipboard operation
seurat-disk copied to clipboard

Problem with LoadH5Seurat

Open ydai3 opened this issue 2 years ago • 17 comments

I want to convert an h5ad project to Seurat project, with the following command in R:

Convert("program_5.ovarian_doublet96.cell_filtering.h5ad", dest = "h5seurat",overwrite = T, verbose = T) ov<-LoadH5Seurat("program_5.ovarian_doublet96.cell_filtering.h5seurat")

The first step works well. But in the second step, an error was reported:

Validating h5Seurat file Initializing RNA with data Error in sparseMatrix(i = x[["indices"]][] + 1, p = x[["indptr"]][], x = x[["data"]][], : 'dims' must contain all (i,j) pairs

Can anyone help me figure out where is wrong? Thank you so much!

ydai3 avatar Feb 10 '23 18:02 ydai3

One of the members of my team had the same issue. I am looking at fixing this.

pvalle6 avatar Mar 09 '23 00:03 pvalle6

I am facing the same issue. Has this been solved?

Leticia314 avatar Apr 24 '23 11:04 Leticia314

I am also facing the same issue. Has this been solved?

dhanusha2504 avatar May 25 '23 16:05 dhanusha2504

I am facing the same issue. Can you please advise?

sergio-rutella avatar May 26 '23 08:05 sergio-rutella

Hi,

Thank you for reaching out. But currently I have not found a solution yet, so I worked on the h5ad object directly. Sincerely hope to get advice on it if you figure it out later. Thank you!

ydai3 avatar May 26 '23 17:05 ydai3

Has anyone solved this issue? I still see this problem every now and then.

coschoi avatar Jul 19 '23 17:07 coschoi

I think I know what is going wrong. The h5ad sparse count matrix must be compressed sparse Row matrix instead of Col matrix. Be aware of the difference between csc_matrix and csr_matrix.

Sakura1a2a3a avatar Sep 27 '23 23:09 Sakura1a2a3a

Waiting for a solution , have met the same issue .

karlie002 avatar Oct 20 '23 07:10 karlie002

Same here!

madeofrats avatar Nov 30 '23 13:11 madeofrats

same

Sycholi avatar Dec 11 '23 08:12 Sycholi

I have the same issue trying to load a h5seurat file converted from h5ad. In the h5ad format the sparse matrix in CSR format, as suggested by a previous comment (https://github.com/mojaveazure/seurat-disk/issues/141#issuecomment-1738215646).

aaronbwong avatar Jan 03 '24 11:01 aaronbwong

For anyone still trying, this is the solution that did the trick for me, however without using LoadH5Seurat...

#install.packages("reticulate")
library(reticulate)

# set pat to python (needs scanpy so in terminal install via pip: "python -m pip install scanpy")
path_to_python <- "/home/tbeder/miniconda3/bin/python3"
use_python(path_to_python)

# import scanpy
sc <- import("scanpy")
# read h5ad```
adata <- sc$read_h5ad("Galaxy20-[BoneMarrow(10X_and_Rhapsody)].h5ad")
# install zellkonverter
remotes::install_github("theislab/zellkonverter")

library(zellkonverter)
SCE <- AnnData2SCE(
  adata,
  X_name = "counts"
)
dim(SCE@assays@data$counts)

# get counts from single cell experiment
counts <- SCE@assays@data$counts

# create the seurat object
library(Seurat)
object <- CreateSeuratObject(counts = counts, assay = "RNA", min.cells = 10)

ThomasBeder avatar Jan 24 '24 20:01 ThomasBeder

For anyone who still figuring out reason and solutions, especially anyone who want to keep all meta data. I met an exactly same error and now I have solved it by using loom method. The reason behind this error was that either or both of "row_attrs/Gene" and "col_attrs/CellID" of your h5ad file were stored as other names like "var_names" or other things, which can not be directly read by Seuratdisk or loomR packages. Here I recommend open your h5ad file in the form of adata, because it can check whether it has been downloaded completely. Then, write this adata into a loom file and check ra.keys and ca.keys of this loom file. so we can change names to "Gene" and "CellID". Finally, we can access it in R by directly load it. Here I attached part of my python code to check names and how I renamed them.

with loompy.connect(loom_path) as ds:
    print("Row attributes available:", ds.ra.keys())
    print("Column attributes available:", ds.ca.keys())

def inspect_and_fix_loom(loom_file):
    with loompy.connect(loom_file, 'r+') as ds:
            ds.ra['Gene'] = ds.ra['var_names'] # here replace 'var_names' with your own gene name variable
            ds.ca['CellID'] = ds.ca['obs_names'] # here replace 'obs_names' with your own cell ID variable

inspect_and_fix_loom(loom_path)

CatCatLiang avatar May 10 '24 03:05 CatCatLiang

A bit dirty but simple way to solve this is to switch to the package zellkonverter. It will automatically create and use a python environment to read h5ad file, but eventually you will get a SingleCellExperiment object in R.

sce <- zellkonverter::readH5AD(h5ad_file, verbose = TRUE)
sce <- sce %>% scuttle::logNormCounts()
# convert it to Seurat object if you want
sce_seurat <- Seurat::as.Seurat(sce)

h4rvey-g avatar Jun 14 '24 14:06 h4rvey-g

I have also used the following code, which works beautifully.

seurat = sceasy::convertFormat("XXX.h5ad", from = "anndata", to = "seurat")

sergio-rutella avatar Jun 14 '24 14:06 sergio-rutella

I think I know what is going wrong. The h5ad sparse count matrix must be compressed sparse Row matrix instead of Col matrix. Be aware of the difference between csc_matrix and csr_matrix.

It's the correct answer

Zackaly avatar Jun 25 '24 09:06 Zackaly

Seems that the problem for me is indeed the csc_matrix. Fixed with adata.X = scipy.sparse.csr_matrix(adata.X)

lutrarutra avatar Jul 29 '24 12:07 lutrarutra