R語言rhdf5讀寫hdf5并展示文件組織結(jié)構(gòu)和索引數(shù)據(jù)
前言
h5只是一種簡單的數(shù)據(jù)組織格式【層級數(shù)據(jù)存儲格式(HierarchicalDataFormat:HDF)】,該格式被設(shè)計用以存儲和組織大量數(shù)據(jù)。

在一些單細(xì)胞文獻(xiàn)中,作者通常會將分析的數(shù)據(jù)上傳到GEO數(shù)據(jù)庫保存為.h5格式文件,而不是我們常見的工程文件(rds文件,表格數(shù)據(jù)等),所以為了解析利用這些數(shù)據(jù)需要對hdf5格式的組織結(jié)構(gòu)有一定的了解。
(注:在Seurat包中有現(xiàn)成的函數(shù)Seurat::Read10X_h5()可以用來提取表達(dá)矩陣,但似乎此外無法從h5文件中提取更多的信息)。
GEO數(shù)據(jù)庫

在R語言中對HDF5進(jìn)行操作的軟件包為rhdf5。
安裝
install.packages("BiocManager");BiocManager::install("rhdf5");library(rhdf5)
打開.h5文件 和 展示內(nèi)容的組織結(jié)構(gòu)
h5_file= H5Fopen("new.h5")
####如下所示,new.h5文件內(nèi)創(chuàng)建了一個組(group1_mat)
#組內(nèi)又創(chuàng)建了df和matrix兩個層級用以保存矩陣和數(shù)據(jù)框
> 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數(shù)據(jù)索引通過“$”符進(jìn)行
> 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
關(guān)閉hdf5文件
H5Fclose(h5_file)#關(guān)閉當(dāng)前打開的hdf5文件 h5closeAll()#關(guān)閉所有打開的hdf5文件
構(gòu)建自己的hdf5文件
###準(zhǔn)備數(shù)據(jù)
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"
###構(gòu)建.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并展示文件組織結(jié)構(gòu)和索引數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于R語言rhdf5讀寫hdf5的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言關(guān)于泊松回歸知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于R語言關(guān)于泊松回歸知識點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05
聊聊R語言中Legend 函數(shù)的參數(shù)用法
這篇文章主要介紹了聊聊R語言中Legend 函數(shù)的參數(shù)用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
R語言實(shí)現(xiàn)操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了R語言實(shí)現(xiàn)操作MySQL數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
R語言關(guān)于生存分析知識點(diǎn)總結(jié)
在本篇文章里,小編給大家整理的是一篇關(guān)于R語言生存分析的相關(guān)知識點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)下吧。2021-05-05
R語言-實(shí)現(xiàn)按日期分組求皮爾森相關(guān)系數(shù)矩陣
這篇文章主要介紹了R語言-實(shí)現(xiàn)按日期分組求皮爾森相關(guān)系數(shù)矩陣,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語言基于Keras的MLP神經(jīng)網(wǎng)絡(luò)及環(huán)境搭建
這篇文章主要介紹了R語言基于Keras的MLP神經(jīng)網(wǎng)絡(luò),我并沒有使用python去對比結(jié)果,但NSS的文章中有做對比,數(shù)據(jù)顯示R與Python相比在各方面的差別都不大,具體內(nèi)容介紹跟隨小編一起看看吧2022-01-01

