|
| 1 | +#' Read OMX attributes |
| 2 | +#' |
| 3 | +#' This function reads the SHAPE and VERSION attributes of an OMX file. This is |
| 4 | +#' called by several other functions. |
| 5 | +#' |
| 6 | +#' @param file full path name of the OMX file being read |
| 7 | +#' @return A list containing \code{SHAPE} and \code{VERSION} attributes. |
| 8 | +#' |
| 9 | +#' @export |
| 10 | +#' @import rhdf5 |
| 11 | +get_omx_attr <- function( file ) { |
| 12 | + H5File <- rhdf5::H5Fopen( file ) |
| 13 | + H5Attr <- rhdf5::H5Aopen( H5File, "SHAPE" ) |
| 14 | + RootAttr <- list() |
| 15 | + RootAttr$SHAPE <- rhdf5::H5Aread( H5Attr ) |
| 16 | + rhdf5::H5Aclose( H5Attr ) |
| 17 | + H5Attr <- rhdf5::H5Aopen( H5File, "OMX_VERSION" ) |
| 18 | + RootAttr$VERSION <- rhdf5::H5Aread( H5Attr ) |
| 19 | + rhdf5::H5Aclose( H5Attr ) |
| 20 | + rhdf5::H5Fclose( H5File ) |
| 21 | + |
| 22 | + RootAttr |
| 23 | +} |
| 24 | + |
| 25 | +#' Function to write matrix attribute |
| 26 | +#' |
| 27 | +#' @param file Full path name of the OMX file to store the matrix in. If this is |
| 28 | +#' a new matrix file, see \link{create_omx}. |
| 29 | +#' @param name Name of the matrix in the OMX object. |
| 30 | +#' @param attr_name Name of the attribute. |
| 31 | +#' @param value Attribute value |
| 32 | +#' |
| 33 | +#' @import rhdf5 |
| 34 | +#' |
| 35 | +#' @export |
| 36 | +write_matrix_attr <- function( file, name, attr_name, value) { |
| 37 | + H5File <- rhdf5::H5Fopen(file) |
| 38 | + H5Group <- rhdf5::rH5Gopen( H5File, "data" ) |
| 39 | + H5Data <- rhdf5::H5Dopen( H5Group, name ) |
| 40 | + rhdf5::h5writeAttribute(value, H5Data, attr_name) |
| 41 | + |
| 42 | + #Close everything up before exiting |
| 43 | + rhdf5::H5Dclose( H5Data ) |
| 44 | + rhdf5::H5Gclose( H5Group ) |
| 45 | + rhdf5::H5Fclose( H5File ) |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +#' List the contents of an OMX file |
| 50 | +#' |
| 51 | +#' @param file The path to the OMX file. |
| 52 | +#' |
| 53 | +#' @return A list with 5 elements: |
| 54 | +#' \describe{ |
| 55 | +#' \item{Version}{the OMX version number} |
| 56 | +#' \item{Rows}{The number of rows in the matrix.} |
| 57 | +#' \item{Columns}{The number of columns in the matrix.} |
| 58 | +#' \item{Matrices}{A \code{data.frame} identifying the matrices and their |
| 59 | +#' attributes} |
| 60 | +#' \item{Lookups}{A \code{data.frame} identifying the lookups and their |
| 61 | +#' attributes.} |
| 62 | +#' } |
| 63 | +#' |
| 64 | +#' @export |
| 65 | +#' @import rhdf5 |
| 66 | +list_omx <- function( file ) { |
| 67 | + #Get the version and shape information |
| 68 | + RootAttr <- get_omx_attr( file ) |
| 69 | + Version <- RootAttr$VERSION |
| 70 | + Shape <- RootAttr$SHAPE |
| 71 | + #Use the h5ls function to read the contents of the file |
| 72 | + Contents <- rhdf5::h5ls( file ) |
| 73 | + MatrixContents <- Contents[ Contents$group == "/data", ] |
| 74 | + LookupContents <- Contents[ Contents$group == "/lookup", ] |
| 75 | + #Read the matrix attribute information |
| 76 | + Names <- MatrixContents$name |
| 77 | + Types <- MatrixContents$dclass |
| 78 | + H5File <- rhdf5::H5Fopen( file ) |
| 79 | + H5Group <- rhdf5::H5Gopen( H5File, "data" ) |
| 80 | + MatAttr <- list() |
| 81 | + |
| 82 | + for( i in 1:length(Names) ) { |
| 83 | + Attr <- list(type="matrix") |
| 84 | + H5Data <- H5Dopen( H5Group, Names[i] ) |
| 85 | + if(H5Aexists(H5Data, "NA")) { |
| 86 | + H5Attr <- rhdf5::H5Aopen( H5Data, "NA" ) |
| 87 | + Attr$navalue <- rhdf5::H5Aread( H5Attr ) |
| 88 | + rhdf5::H5Aclose( H5Attr ) |
| 89 | + } |
| 90 | + if(H5Aexists(H5Data, "Description")) { |
| 91 | + H5Attr <- rhdf5::H5Aopen( H5Data, "Description" ) |
| 92 | + Attr$description <- rhdf5::H5Aread( H5Attr ) |
| 93 | + rhdf5::H5Aclose( H5Attr ) |
| 94 | + } |
| 95 | + MatAttr[[Names[i]]] <- Attr |
| 96 | + rhdf5::H5Dclose( H5Data ) |
| 97 | + rm( Attr ) |
| 98 | + } |
| 99 | + |
| 100 | + rhdf5::H5Gclose( H5Group ) |
| 101 | + rhdf5::H5Fclose( H5File ) |
| 102 | + |
| 103 | + MatAttr <- do.call( rbind, lapply( MatAttr, function(x) data.frame(x) ) ) |
| 104 | + rm( Names, Types ) |
| 105 | + |
| 106 | + #Read the lookup attribute information |
| 107 | + H5File <- rhdf5::H5Fopen( file ) |
| 108 | + H5Group <- rhdf5::H5Gopen( H5File, "lookup" ) |
| 109 | + Names <- LookupContents$name |
| 110 | + Types <- LookupContents$dclass |
| 111 | + LookupAttr <- list() |
| 112 | + |
| 113 | + if(length(Names)>0){ |
| 114 | + for( i in 1:length(Names) ) { |
| 115 | + Attr <- list() |
| 116 | + H5Data <- rhdf5::H5Dopen( H5Group, Names[i] ) |
| 117 | + |
| 118 | + if( rhdf5::H5Aexists( H5Data, "DIM" ) ) { |
| 119 | + H5Attr <- rhdf5::H5Aopen( H5Data, "DIM" ) |
| 120 | + Attr$lookupdim <- rhdf5::H5Aread( H5Attr ) |
| 121 | + rhdf5::H5Aclose( H5Attr ) |
| 122 | + } else { |
| 123 | + Attr$lookupdim <- "" |
| 124 | + } |
| 125 | + |
| 126 | + if( rhdf5::H5Aexists( H5Data, "Description" ) ) { |
| 127 | + H5Attr <- rhdf5::H5Aopen( H5Data, "Description" ) |
| 128 | + Attr$description <- rhdf5::H5Aread( H5Attr ) |
| 129 | + rhdf5::H5Aclose( H5Attr ) |
| 130 | + } else { |
| 131 | + Attr$description <- "" |
| 132 | + } |
| 133 | + |
| 134 | + LookupAttr[[Names[i]]] <- Attr |
| 135 | + rhdf5::H5Dclose( H5Data ) |
| 136 | + rm( Attr ) |
| 137 | + } |
| 138 | + |
| 139 | + rhdf5::H5Gclose( H5Group ) |
| 140 | + rhdf5::H5Fclose( H5File ) |
| 141 | + |
| 142 | + LookupAttr <- do.call( rbind, lapply( LookupAttr, function(x) data.frame(x) ) ) |
| 143 | + rm( Names, Types ) |
| 144 | + } |
| 145 | + |
| 146 | + #Combine the results into a list |
| 147 | + if(length(MatAttr)>0) { |
| 148 | + MatInfo <- cbind( MatrixContents[,c("name","dclass","dim")], MatAttr ) |
| 149 | + } else { |
| 150 | + MatInfo <- MatrixContents[,c("name","dclass","dim")] |
| 151 | + } |
| 152 | + |
| 153 | + if(length(LookupAttr)>0) { |
| 154 | + LookupInfo <- cbind( LookupContents[,c("name","dclass","dim")], LookupAttr ) |
| 155 | + } else { |
| 156 | + LookupInfo <- LookupContents[,c("name","dclass","dim")] |
| 157 | + } |
| 158 | + |
| 159 | + list( |
| 160 | + OMXVersion=Version, |
| 161 | + Rows=Shape[1], Columns=Shape[2], |
| 162 | + Matrices=MatInfo, Lookups=LookupInfo |
| 163 | + ) |
| 164 | +} |
0 commit comments