最近想将单细胞数据从Seurat转换到H5AD格式,但发现Seurat V5直接转换会报错。这里记录一下解决的方案。
library(Seurat)
## Loading required package: SeuratObject
## Loading required package: sp
## 'SeuratObject' was built under R 4.4.0 but the current version is
## 4.4.1; it is recomended that you reinstall 'SeuratObject' as the ABI
## for R may have changed
##
## Attaching package: 'SeuratObject'
## The following objects are masked from 'package:base':
##
## intersect, t
# remotes::install_github("mojaveazure/seurat-disk")
library(SeuratDisk)
## Registered S3 method overwritten by 'SeuratDisk':
## method from
## as.sparse.H5Group Seurat
# Load the PBMC dataset
pbmc.data <- Read10X(data.dir = "data/filtered_gene_bc_matrices/hg19/")
# Initialize the Seurat object with the raw (non-normalized data).
pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)
## Warning: Feature names cannot have underscores ('_'), replacing with dashes
## ('-')
pbmc
## An object of class Seurat
## 13714 features across 2700 samples within 1 assay
## Active assay: RNA (13714 features, 0 variable features)
## 1 layer present: counts
# standard seurat
pbmc <- NormalizeData(pbmc)
## Normalizing layer: counts
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
## Finding variable features for layer counts
pbmc <- ScaleData(pbmc)
## Centering and scaling data matrix
直接转换会报错
SaveH5Seurat(pbmc, filename = "data/pbmc.h5Seurat")
Convert("data/pbmc.h5Seurat", dest = "h5ad")
Error in assay.group$obj_copy_to(dst_loc = dfile, dst_name = “X”, src_name = x.data) : HDF5-API Errors: error #000: ../../src/hdf5-1.12.2/src/H5Ocopy.c in H5Ocopy(): line 240: unable to copy object class: HDF5 major: Object header minor: Unable to copy object
error #001: ../../src/hdf5-1.12.2/src/H5VLcallback.c in H5VL_object_copy(): line 5495: object copy failed class: HDF5 major: Virtual Object Layer minor: Unable to copy object error #002: ../../src/hdf5-1.12.2/src/H5VLcallback.c in H5VL__object_copy(): line 5456: object copy failed class: HDF5 major: Virtual Object Layer minor: Unable to copy object error #003: ../../src/hdf5-1.12.2/src/H5VLnative_object.c in H5VL__native_object_copy(): line 125: unable to copy object class: HDF5 major: Object header minor: Unable to copy object error #004: ../../src/hdf5-1.12.2/src/H5Ocopy.c in H5O__copy(): line 291: source object not found class: HDF5 major: Symbol table minor: Obj
Seurat V5 多了layer
结构,可能导致了转换的失败。将Seurat V5的Assay转换成V3的Assay就可以使用SeuratDisk
的函数进行转换了
# remove existed files
file.remove('data/pbmc.h5Seurat')
## Warning in file.remove("data/pbmc.h5Seurat"): cannot remove file
## 'data/pbmc.h5Seurat', reason 'No such file or directory'
## [1] FALSE
file.remove('data/pbmc.h5ad')
## Warning in file.remove("data/pbmc.h5ad"): cannot remove file 'data/pbmc.h5ad',
## reason 'No such file or directory'
## [1] FALSE
pbmc[["RNA3"]] <- as(object = pbmc[["RNA"]], Class = "Assay")
## Warning: Key 'rna_' taken, using 'rna3_' instead
DefaultAssay(pbmc) <- "RNA3"
pbmc[["RNA"]] <- NULL
pbmc <- RenameAssays(object = pbmc, RNA3 = 'RNA')
## Renaming default assay from RNA3 to RNA
## Warning: Key 'rna3_' taken, using 'rna_' instead
# by defauly, scale.data only contains highly variable genes, which will be the defalut expression data of h5ad
# to use all features, remove scale.data
pbmc[["RNA"]]$scale.data <- NULL
SaveH5Seurat(pbmc, filename = "data/pbmc.h5Seurat")
## Creating h5Seurat file for version 3.1.5.9900
## Adding counts for RNA
## Adding data for RNA
## Adding variable features for RNA
## Adding feature-level metadata for RNA
Convert("data/pbmc.h5Seurat", dest = "h5ad")
## Validating h5Seurat file
## Adding data from RNA as X
## Transfering meta.features to var
## Adding counts from RNA as raw
## Transfering meta.features to raw/var
## Transfering meta.data to obs
希望SeuratDisk这个小问题会在将来修复
完。
Ref:
https://github.com/satijalab/seurat/issues/8220
https://github.com/satijalab/seurat/discussions/7402
https://github.com/satijalab/seurat/issues/8054
https://satijalab.org/seurat/articles/seurat5_essential_commands