Code for reading h5ad files in R
1
0
Entering edit mode
13 months ago
JACKY ▴ 170

How can I read and view h5ad files in R? I tried the Seurat package but I see no function that can read such files. I found another package called SeuratDisk but it's confusing.

Say I have a file called GSE123456.h5ad, how do I read it?

r RNA-seq single-cell • 5.5k views
ADD COMMENT
0
Entering edit mode
13 months ago
fracarb8 ★ 1.7k

What do you find confusing about SeuratDisk? The documentation literately gives you the code:

# docu example
Convert("pbmc3k_final.h5ad", dest = "h5seurat", overwrite = TRUE)
pbmc3k <- LoadH5Seurat("pbmc3k_final.h5seurat")
ADD COMMENT
0
Entering edit mode

The reason I said its confusing is because I got errors. So for the code you provided here is the result :

> GSE120575 = SeuratDisk::LoadH5Seurat("singleCell/GSE120575.h5seurat")

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

Any idea what this error is ? thank you!

ADD REPLY
0
Entering edit mode

Where did you get the initial h5ad file from? Are you sure it is not corrupt?

ADD REPLY
1
Entering edit mode

It's not corrupt because when I tried reading it with python it worked:

import anndata
adata = anndata.read_h5ad("GSE123456.h5ad")

And I can access, for example, the number of genes and the number of observations :

num_observations = adata.obs.shape[0]
print(f"Number of observations: {num_observations}")

num_genes = adata.var.shape[0]
print(f"Number of genes: {num_genes}")

Number of observations: 60
Number of genes: 14654
ADD REPLY
0
Entering edit mode

are you using seurat v5? Do you get the error if you set options(Seurat.object.assay.version = "v3")?

ADD REPLY

Login before adding your answer.

Traffic: 2395 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6