R语言rhdf5读写hdf5并展示文件组织结构和索引数据

Aggie ·
更新时间:2024-09-21
· 69 次阅读

前言

h5只是一种简单的数据组织格式【层级数据存储格式(HierarchicalDataFormat:HDF)】,该格式被设计用以存储和组织大量数据。

在一些单细胞文献中,作者通常会将分析的数据上传到GEO数据库保存为.h5格式文件,而不是我们常见的工程文件(rds文件,表格数据等),所以为了解析利用这些数据需要对hdf5格式的组织结构有一定的了解。

(注:在Seurat包中有现成的函数Seurat::Read10X_h5()可以用来提取表达矩阵,但似乎此外无法从h5文件中提取更多的信息)。

GEO数据库

在R语言中对HDF5进行操作的软件包为rhdf5

安装 install.packages("BiocManager");BiocManager::install("rhdf5");library(rhdf5) 打开.h5文件 和 展示内容的组织结构 h5_file= H5Fopen("new.h5") ####如下所示,new.h5文件内创建了一个组(group1_mat) #组内又创建了df和matrix两个层级用以保存矩阵和数据框 > h5dump(h5_file,load=FALSE) $group1_mat $group1_mat$df group name otype dclass dim 1 / df H5I_DATASET COMPOUND 5 $group1_mat$matrix group name otype dclass dim 1 / matrix H5I_DATASET FLOAT 3 x 2 数据索引通过“$”符进行 > h5_file$group1_mat$df C_1 C_2 C_3 name 1 3 5 69 xx 2 2 8 60 yy 3 8 4 92 gg 4 1 6 16 ll 5 7 4 25 mm 关闭hdf5文件 H5Fclose(h5_file)#关闭当前打开的hdf5文件 h5closeAll()#关闭所有打开的hdf5文件 构建自己的hdf5文件 ###准备数据 mdat <- matrix(c(0,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE,dimnames = list(c("row1", "row2"),c("C.1", "C.2", "C.3"))) df <- data.frame(C_1 = c(3,2,8,1,7),C_2 = c(5,8,4,6,4),C_3 = round(runif(n = 5), 2) * 100,name = c("xx","yy","gg",'ll','mm')) mdat.spar <- Matrix::Matrix(mdat, sparse = TRUE) my_array <- array(seq(0.1,2.0,by=0.1),dim=c(5,2,2)) my_list <- list(my_array[,,1],my_array[,,2]) my_string <- "This is one hdf structure file" ###构建.h5文件 h5createFile("new.h5") # Saving matrix information. h5createGroup("new.h5","group1_mat") h5write(mdat, "new.h5", "group1_mat/matrix") h5write(df, "new.h5", "group1_mat/df") # Saving sparse_matrix information. mdat.spar <- as(mdat, "dgCMatrix") h5createGroup("new.h5","group2_sparseMTX") h5write(mdat.spar@x, "new.h5", "group2_sparseMTX/data") h5write(dim(mdat.spar), "new.h5", "group2_sparseMTX/shape") h5write(mdat.spar@i, "new.h5", "group2_sparseMTX/indices") # already zero-indexed. h5write(mdat.spar@p, "new.h5", "group2_sparseMTX/indptr") # Saving array and list data h5createGroup("new.h5","group3_aL") h5write(my_list, "new.h5", "group3_aL/list") h5write(my_array, "new.h5", "group3_aL/array") # Saving string data h5createGroup("new.h5","group4_string") h5write(my_string, "new.h5", "group4_string/string") h5closeAll()

参考官方说明 rhdf5 - HDF5 interface for R (bioconductor.org)

以上就是R语言rhdf5读写hdf5并展示文件组织结构和索引数据的详细内容,更多关于R语言rhdf5读写hdf5的资料请关注软件开发网其它相关文章!



数据 展示 文件组 索引

需要 登录 后方可回复, 如果你还没有账号请 注册新账号