[Bioc-devel] How to trigger h5read.<classname> with h5read function in rhdf5

Nathaniel Hayden nhayden at fhcrc.org
Thu Aug 7 02:49:23 CEST 2014


When reading from an hdf5 file I would like to automatically call a 
function I define when datasets of an arbitrary type (see: 'class') are 
read from an hdf5 file. Since it looks like the existing infrastructure 
(courtesy of the 'callGeneric' parameter in h5read) in rhdf5 was made 
for this, I would like to avoid duplicating work. But I can't find an 
example of the h5read.<classname> functionality indicated in the 
callGeneric description in the h5read man page.

A simple example is if the type is integer, I want as.integer to be 
automatically called on the read-in object before it gets passed back. 
But I intend to extend this to other Bioconductor classes of arbitrary 
complexity.

Based on the documentation, it seems like either using attr(foo "class") 
<- "integer" (in conjunction with h5write(<...>, write.attributes=TRUE) 
or adding a 'class' attribute through the h5writeAttribute interface 
should be enough to trigger the h5read.integer function upon calling 
h5read. Neither seems to work. Note that I can pass read.attributes=TRUE 
and the attributes get assigned the object (for example, the object 
comes back with a "class" attribute), but that's not exactly what I'm after.

In looking at the R/h5read.R source code, it looks like the block where 
the h5read.<classname> call gets set up (around line 59) queries the 
"class" attribute of the read-in obj before the h5 object's attributes 
are actually read, so the 'cl' variable never seems to get set.

Here's an example where I would expect h5read.<classname> to be invoked, 
but it doesn't:

library(rhdf5)
h5read.integer <- function(obj) { as.integer(obj) } ## h5read.<classname>
debug(h5read.integer)
exists(paste("h5read","integer",sep="."),mode="function")

h5fl <- tempfile(fileext=".h5")
h5createFile(h5fl)
ints <- 42L:33L
attr(ints, "class") <- "integer"
h5write(ints, h5fl, "foo", write.attributes=TRUE)
H5close()

## h5writeAttribute route
##fid <- H5Fopen(h5fl)
##did <- H5Dopen(fid, "foo")
##h5writeAttribute("integer", did, name="class")
##H5close()

##res <- h5read(h5fl, "foo", read.attributes=FALSE)
res <- h5read(h5fl, "foo", read.attributes=TRUE)

Running the external h5dump utility confirms that a "class" attribute is 
attached to the foo DATASET, which seems to match what the h5read man 
page prescribes. If I edit the source code to set the 'cl' variable to 
"integer" my h5read.integer function gets invoked, as expected.

Any help would be much appreciated. Thank you.



More information about the Bioc-devel mailing list