我只是在学习使用 rhdf5 包。看起来,对于没有暗名的矩阵的创建和索引,操作真的很简单
library(rhdf5)
my.mat <- matrix(rnorm(400,2,1), nrow=100, ncol=4)
fl <- tempfile()
h5createFile(fl)
h5write(my.mat, fl, "mat")
h5read(fl, "mat", list(2:3, 3:4))
## [,1] [,2]
## [1,] 0.3199968 1.947390
## [2,] 1.3338179 2.623461
h5read(fl, "mat", list(2:3, NULL))
## [,1] [,2] [,3] [,4]
## [1,] 1.247648 -0.380762 0.3199968 1.947390
## [2,] 3.157954 1.334057 1.3338179 2.623461
该包似乎支持某些功能,例如,用于编写 data.frame 对象,但我最终“滚动我自己的”函数来创建和子集/选择带有暗名的矩阵。下面是 write 函数,将 HDF5 属性添加到数据集
h5matrix_write <-
function(obj, file, name, ...)
{
if (!is.matrix(obj) || is.null(dimnames(obj)) ||
any(sapply(dimnames(obj), is.null)))
stop("'obj' must be a matrix with row and column names")
fid <- if (file.exists(file))
H5Fopen(file)
else
H5Fcreate(file)
h5createDataset(fid, name, dim=dim(obj))
did <- H5Dopen(fid, name)
h5createAttribute(fid, "rownames", nrow(obj), storage.mode="character",
size=max(nchar(rownames(obj))))
h5createAttribute(fid, "colnames", ncol(obj), storage.mode="character",
size=max(nchar(colnames(obj))))
h5writeDataset(obj, fid, name)
h5writeAttribute(rownames(obj), did, "rownames")
h5writeAttribute(colnames(obj), did, "colnames")
H5Dclose(did)
H5Fclose(fid)
file
}
为了读取子集,我检查索引是否为字符向量。如果是这样,我确定矩阵中的索引并使用它来提取相关值
h5matrix_select <-
function(file, name, i, j, ...)
{
## FIXME: doesn't handle logical subsetting
fid <- H5Fopen(fl)
did <- H5Dopen(fid, "mat")
rownames <- H5Aread(H5Aopen(did, "rownames"))
if (missing(i))
i <- seq_along(rownames)
else if (is.character(i)) {
i <- match(i, rownames)
if (any(is.na(i)))
stop(sum(is.na(i)), " unknown row names")
}
rownames <- rownames[i]
colnames <- H5Aread(H5Aopen(did, "colnames"))
if (missing(j))
j <- seq_along(colnames)
else if (is.character(j)) {
j <- match(j, colnames)
if (any(is.na(j)))
stop(sum(is.na(j)), " unknown colnames")
}
colnames <- colnames[j]
value <- h5read(file, name, list(i, j))
dimnames(value) <- list(rownames, colnames)
value
}
实际操作:
dimnames(my.mat) <- list(paste0("rid", seq_len(nrow(my.mat))),
paste0("cid", seq_len(ncol(my.mat))))
fl <- h5matrix_write(my.mat, tempfile(), "mat")
h5matrix_select(fl, "mat", 4:5, 2:3)
## cid2 cid3
## rid4 0.4716097 2.3490782
## rid5 2.0896238 0.5141749
h5matrix_select(fl, "mat", 4:5)
## cid1 cid2 cid3 cid4
## rid4 2.0947833 0.4716097 2.3490782 3.139687
## rid5 0.8258651 2.0896238 0.5141749 2.509301
set.seed(123)
h5matrix_select(fl, "mat", sample(rownames(my.mat), 3), 2:3)
## cid2 cid3
## rid29 0.6694079 3.795752
## rid79 2.1635644 2.892343
## rid41 3.7779177 1.685139
(h5read(fl, "mat", read.attributes=TRUE) 读取所有内容;我认为@jimmyb 的更简单方法(将行名存储为单独的变量)也适用于 rhdf5。
在 Bioconductor mailing list 上询问有关 Bioconductor 包的问题是合适的,包作者可能更可能看到它。