From af692744d62bd8d84ddf84a8a29666dc18da1b7e Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:18:01 -0400 Subject: [PATCH 1/8] Add original R script. The API as it presently exists is a group of functions that the user brings into her workspace with `source()`. --- R/omx.R | 449 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 449 insertions(+) create mode 100644 R/omx.R diff --git a/R/omx.R b/R/omx.R new file mode 100644 index 0000000..ef24b11 --- /dev/null +++ b/R/omx.R @@ -0,0 +1,449 @@ +#===== +#omx.r +#===== + +#Read and Write Open Matrix Files +#Ben Stabler, stabler@pbworld.com, 08/21/13 +#Brian Gregor, gregor@or-analytics.com, 12/18/13 +#Requires the rhdf5 v2.5.1+ package from bioconductor +#Transposes matrix when writing it to file to be in row major order like C/Python +################################################################################ + +#Load the rhdf5 library +library( rhdf5 ) + +#Function to create an OMX file that is ready for writing data +#------------------------------------------------------------- +#This function creates an OMX file, establishes the shape attribute (number of rows and columns) and version attribute, and creates the data and lookup groups. +#Arguments: +#OMXFileName = full path name of the OMX file to create +#Numrows = number of rows that all matrices in the file will have +#Numcols = number of columns that all matrices in the file will have +#Level = compression level +#Return: TRUE + +createFileOMX <- function( Filename, Numrows, Numcols, Level=1 ) { + if(file.exists(Filename)) { file.remove(Filename) } + Shape <- c( Numrows, Numcols ) + H5File <- H5Fcreate( Filename ) + h5writeAttribute( 0.2, H5File, "OMX_VERSION" ) + h5writeAttribute( Shape, H5File, "SHAPE" ) + h5createGroup(Filename,"data") + h5createGroup(Filename,"lookup") + H5Fclose( H5File ) + TRUE +} + +#Utility function to read the SHAPE and VERSION attributes +#--------------------------------------------------------- +#This function reads the SHAPE and VERSION attributes of an OMX file. This is called by several other functions +#Arguments: +#OMXFileName = full path name of the OMX file being read +#Return: List containing the SHAPE and VERSION attributes. +#SHAPE component is a vector of number of rows and columns +#VERSION component is the version number +getRootAttrOMX <- function( OMXFileName ) { + H5File <- H5Fopen( OMXFileName ) + H5Attr <- H5Aopen( H5File, "SHAPE" ) + RootAttr <- list() + RootAttr$SHAPE <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + H5Attr <- H5Aopen( H5File, "OMX_VERSION" ) + RootAttr$VERSION <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + H5Fclose( H5File ) + RootAttr +} + +#Function to write OMX matrix data +#--------------------------------- +#This function writes OMX matrix data. A full matrix can be written or just portions of an existing matrix. It allows overwriting existing matrix values, but only if the "Replace" argument is set to TRUE. If only portions of the matrix are to be written to, the full matrix must already exist. +#Arguments: +#OMXFileName = full path name of the OMX file to store the matrix in +#Matrix = matrix object to be stored +#MatrixSaveName = name under which the matrix will be saved in the OMX file +#RowIndex = vector of positional indexes that rows of matrix object are written to. NULL value means that all rows are written to. +#ColIndex = vector of positional indexes that columns of matrix object are written to. NULL value means that all columns are written to. +#NaValue = value that will be used to replace NA values in the matrix (NA is not a value that can be stored in OMX) +#Replace = TRUE or FALSE value to determine whether an existing matrix of the same name should be replaced by new matrix +#Description = String that describes the matrix +#Function returns TRUE if completed successfully +#Return: TRUE + +writeMatrixOMX <- function( OMXFileName, Matrix, MatrixSaveName, RowIndex=NULL, ColIndex=NULL, NaValue=-1 , + Replace=FALSE, Description="" ) { + #Get names of matrices in the file vc + Contents <- h5ls( OMXFileName ) + MatrixNames <- Contents$name[ Contents$group == "/data" ] + MatrixExists <- MatrixSaveName %in% MatrixNames + # Get the matrix dimensions specified in the file + RootAttr <- getRootAttrOMX( OMXFileName ) + Shape <- RootAttr$SHAPE + #Check whether there is matrix of that name already in the file + if( MatrixExists & Replace == FALSE ){ + stop( paste("A matrix named '", MatrixSaveName, "' already exists. Value of 'Replace' argument must be TRUE in order to overwrite.", sep="") ) + } + #Allow indexed writing (if RowIndex and ColIndex are not NULL) only if the matrix already exists + if( !( is.null( RowIndex ) & is.null( ColIndex ) ) ){ + if( !MatrixExists ){ + stop( "Indexed writing to a matrix only allowed if a full matrix of that name already exists." ) + } + } + #If neither dimension will be written to indexes, write the full matrix and add the NA attribute + if( is.null( RowIndex ) & is.null( ColIndex ) ){ + #Check conformance of matrix dimensions with OMX file + if( !all( dim( Matrix ) == Shape ) ){ + stop( paste( "Matrix dimensions not consistent with", OMXFileName, ":", Shape[1], "Rows,", Shape[2], "Cols" ) ) + } + #Transpose matrix and convert NA to designated storage value + Matrix <- t( Matrix ) + Matrix[ is.na( Matrix ) ] <- NaValue + #Write matrix to file + ItemName <- paste( "data", MatrixSaveName, sep="/" ) + h5write( Matrix, OMXFileName, ItemName ) + #Add the NA storage value and matrix descriptions as attributes to the matrix + H5File <- H5Fopen( OMXFileName ) + H5Group <- H5Gopen( H5File, "data" ) + H5Data <- H5Dopen( H5Group, MatrixSaveName ) + h5writeAttribute( NaValue, H5Data, "NA" ) + h5writeAttribute( Description, H5Data, "Description" ) + #Close everything up before exiting + H5Dclose( H5Data ) + H5Gclose( H5Group ) + H5Fclose( H5File ) + #Otherwise write only to the indexed positions + } else { + if( is.null( RowIndex ) ) RowIndex <- 1:Shape[1] + if( is.null( ColIndex ) ) ColIndex <- 1:Shape[2] + #Check that indexes are within matrix dimension ranges + if( any( RowIndex <= 0 ) | ( max( RowIndex ) > Shape[1] ) ){ + stop( "One or more values of 'RowIndex' are outside the index range of the matrix." ) + } + if( any( ColIndex <= 0 ) | ( max( ColIndex ) > Shape[2] ) ){ + stop( "One or more values of 'ColIndex' are outside the index range of the matrix." ) + } + #Check that there are no duplicated indices + if( any( duplicated( RowIndex ) ) ){ + stop( "Duplicated index values in 'RowIndex'. Not permitted." ) + } + if( any( duplicated( ColIndex ) ) ){ + stop( "Duplicated index values in 'ColIndex'. Not permitted." ) + } + #Combine the row and column indexes into a list + #Indices are reversed since matrix is stored in transposed form + Indices <- list( RowIndex, ColIndex ) + #Transpose matrix and convert NA to designated storage value + Matrix <- t( Matrix ) + Matrix[ is.na( Matrix ) ] <- NaValue + # Write the matrix to the indexed positions + ItemName <- paste( "data", MatrixSaveName, sep="/" ) + h5write( Matrix, OMXFileName, ItemName, index=Indices ) + } + TRUE +} + + +#Function to write a lookup vector to an OMX file +#------------------------------------------------ +#This function writes a lookup vector to the file. It allows the user to specify if the lookup vector applies only to rows or columns (in case the matrix is not square and/or the rows and columns don't have the same meanings. +#Arguments: +#OMXFileName = full path name of the OMX file to store the lookup vector in +#LookupVector = lookup vector object to be stored +#LookupSaveName = name under which the lookup vector will be saved in the OMX file +#LookupDim = matrix dimension that the lookup vector is associated with +#Values can be "row", "col", or NULL. A lookup dimension attribute is optional. +#Description = string that describes the matrix +#Return: TRUE +writeLookupOMX <- function( OMXFileName, LookupVector, LookupSaveName, LookupDim=NULL, Replace=FALSE, Description="" ) { + #Check whether there is lookup of that name already in the file + Contents = h5ls( OMXFileName ) + LookupNames <- Contents$name[ Contents$group == "/lookup" ] + if( ( LookupSaveName %in% LookupNames ) & ( Replace == FALSE ) ){ + stop( paste("A lookup named '", LookupSaveName, "' already exists. 'Replace' must be TRUE in order to overwrite.", sep="") ) + } + #Error check lookup dimension arguments + if( !is.null( LookupDim ) ){ + if( !( LookupDim %in% c( "row", "col" ) ) ) { + stop( "LookupDim argument must be 'row', 'col', or NULL." ) + } + } + Len <- length( LookupVector ) + RootAttr <- getRootAttrOMX( OMXFileName ) + Shape <- RootAttr$SHAPE + if( is.null( LookupDim ) ) { + if( Shape[1] != Shape[2] ) { + stop( "Matrix is not square. You must specify the 'LookupDim'" ) + } + if( Len != Shape[1] ) { + stop( paste( OMXFileName, " has ", Shape[1], " rows and columns. LookupVector has ", Len, " positions.", sep="" ) ) + } + } + if( !is.null( LookupDim ) ){ + if( LookupDim == "row" ){ + if( Len != Shape[1] ){ + stop( paste( "Length of 'LookupVector' does not match row dimension of", OMXFileName ) ) + } + } + if( LookupDim == "col" ){ + if( Len != Shape[2] ){ + stop( paste( "Length of 'LookupVector' does not match column dimension of", OMXFileName ) ) + } + } + } + #Write lookup vector to file + ItemName <- paste( "lookup", LookupSaveName, sep="/" ) + h5write( LookupVector, OMXFileName, ItemName ) + #Write attributes + H5File <- H5Fopen( OMXFileName ) + H5Group <- H5Gopen( H5File, "lookup" ) + H5Data <- H5Dopen( H5Group, LookupSaveName ) + h5writeAttribute( Description, H5Data, "Description" ) + if( !is.null( LookupDim ) ) { + h5writeAttribute( LookupDim, H5Data, "DIM" ) + } + #Close everything up before exiting + H5Dclose( H5Data ) + H5Gclose( H5Group ) + H5Fclose( H5File ) + TRUE +} + +#Function to list the contents of an OMX file +#-------------------------------------------- +#This function lists the contents of an omx file. These include: +#OMX version +#Matrix shape +#Names, descriptions, datatypes, and NA values of all of the matrices in an OMX file. +#Names and descriptions of indices and whether each index applies to rows, columns or both +#Arguments: +#OMXFileName = full path name of the OMX file +#Return: A list with 5 components: +#Version - the OMX version number +#Rows - number of rows in the matrix +#Columns - number of columns in the matrix +#Matrices - a dataframe identifying the matrices and all their attributes +#Lookups - a dataframe identifying the lookups and all their attributes +listOMX <- function( OMXFileName ) { + #Get the version and shape information + RootAttr <- getRootAttrOMX( OMXFileName ) + Version <- RootAttr$VERSION + Shape <- RootAttr$SHAPE + #Use the h5ls function to read the contents of the file + Contents <- h5ls( OMXFileName ) + MatrixContents <- Contents[ Contents$group == "/data", ] + LookupContents <- Contents[ Contents$group == "/lookup", ] + #Read the matrix attribute information + Names <- MatrixContents$name + Types <- MatrixContents$dclass + H5File <- H5Fopen( OMXFileName ) + H5Group <- H5Gopen( H5File, "data" ) + MatAttr <- list() + for( i in 1:length(Names) ) { + Attr <- list() + H5Data <- H5Dopen( H5Group, Names[i] ) + if(H5Aexists(H5Data, "NA")) { + H5Attr <- H5Aopen( H5Data, "NA" ) + Attr$navalue <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + } + if(H5Aexists(H5Data, "Description")) { + H5Attr <- H5Aopen( H5Data, "Description" ) + Attr$description <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + } + MatAttr[[Names[i]]] <- Attr + H5Dclose( H5Data ) + rm( Attr ) + } + H5Gclose( H5Group ) + H5Fclose( H5File ) + MatAttr <- do.call( rbind, lapply( MatAttr, function(x) data.frame(x) ) ) + rm( Names, Types ) + #Read the lookup attribute information + H5File <- H5Fopen( OMXFileName ) + H5Group <- H5Gopen( H5File, "lookup" ) + Names <- LookupContents$name + Types <- LookupContents$dclass + LookupAttr <- list() + for( i in 1:length(Names) ) { + Attr <- list() + H5Data <- H5Dopen( H5Group, Names[i] ) + if( H5Aexists( H5Data, "DIM" ) ) { + H5Attr <- H5Aopen( H5Data, "DIM" ) + Attr$lookupdim <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + } else { + Attr$lookupdim <- "" + } + if( H5Aexists( H5Data, "Description" ) ) { + H5Attr <- H5Aopen( H5Data, "Description" ) + Attr$description <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + } else { + Attr$description <- "" + } + LookupAttr[[Names[i]]] <- Attr + H5Dclose( H5Data ) + rm( Attr ) + } + H5Gclose( H5Group ) + H5Fclose( H5File ) + LookupAttr <- do.call( rbind, lapply( LookupAttr, function(x) data.frame(x) ) ) + rm( Names, Types ) + #Combine the results into a list + if(length(MatAttr)>0) { + MatInfo <- cbind( MatrixContents[,c("name","dclass","dim")], MatAttr ) + } else { + MatInfo <- MatrixContents[,c("name","dclass","dim")] + } + LookupInfo <- cbind( LookupContents[,c("name","dclass","dim")], LookupAttr ) + list( OMXVersion=Version, Rows=Shape[1], Columns=Shape[2], Matrices=MatInfo, Lookups=LookupInfo ) +} + +#Function to read an OMX matrix +#------------------------------ +#This function reads an entire matrix in an OMX file or portions of a matrix using indexing. +#Arguments: +#OMXFileName = Path name of the OMX file where the matrix resides. +#MatrixName = Name of the matrix in the OMX file +#RowIndex = Vector containing numerical indices of the rows to be read. +#ColIndex = Vector containing numerical indices of the columns to be read +#Return: a matrix +readMatrixOMX <- function( OMXFileName, MatrixName, RowIndex=NULL, ColIndex=NULL ) { + #Get the matrix dimensions specified in the file + RootAttr <- getRootAttrOMX( OMXFileName ) + Shape <- RootAttr$SHAPE + #Identify the item to be read + ItemName <- paste( "data", MatrixName, sep="/" ) + #Check that RowIndex is proper + if( !is.null( RowIndex ) ) { + if( any( RowIndex <= 0 ) | ( max( RowIndex ) > Shape[1] ) ){ + stop( "One or more values of 'RowIndex' are outside the index range of the matrix." ) + } + if( any( duplicated( RowIndex ) ) ){ + stop( "Duplicated index values in 'RowIndex'. Not permitted." ) + } + } + #Check that ColIndex is proper + if( !is.null( ColIndex ) ) { + if( any( ColIndex <= 0 ) | ( max( ColIndex ) > Shape[2] ) ){ + stop( "One or more values of 'ColIndex' are outside the index range of the matrix." ) + } + if( any( duplicated( ColIndex ) ) ){ + stop( "Duplicated index values in 'ColIndex'. Not permitted." ) + } + } + #Combine the row and column indexes into a list + #Indexes are reversed since matrix is stored in transposed form + Indices <- list( ColIndex, RowIndex ) + #Read the indexed positions of the matrix + Result <- t( h5read( OMXFileName, ItemName, index=list(ColIndex,RowIndex) ) ) + #Replace the NA values with NA + NAValue = as.vector( attr( h5read( OMXFileName, ItemName, read.attribute=T ), "NA" ) ) + if(!is.null(NAValue)) { + Result[ Result == NAValue ] <- NA + } + #Return the result + Result +} + +#Function to read an OMX lookup +#------------------------------ +#This function reads a lookup and its attributes. +#Arguments: +#OMXFileName = Path name of the OMX file where the lookup resides. +#LookupName = Name of the lookup in the OMX file +#Return: a list having 2 components +#Lookup = The lookup vector +#LookupDim = The name of the matrix dimension the lookup corresponds to +readLookupOMX <- function( OMXFileName, LookupName ) { + #Identify the item to be read + ItemName <- paste( "lookup", LookupName, sep="/" ) + #Read the lookup + Lookup <- h5read( OMXFileName, ItemName ) + #Read the name of the dimension the lookup corresponds + H5File <- H5Fopen( OMXFileName ) + H5Group <- H5Gopen( H5File, "lookup" ) + H5Data <- H5Dopen( H5Group, LookupName ) + if( H5Aexists( H5Data, "DIM" ) ) { + H5Attr <- H5Aopen( H5Data, "DIM" ) + Dim <- H5Aread( H5Attr ) + H5Aclose( H5Attr ) + } else { + Dim <- "" + } + H5Dclose( H5Data ) + H5Gclose( H5Group ) + H5Fclose( H5File ) + #Return the lookup and the corresponding dimension + list( Lookup=Lookup, LookupDim=Dim ) +} + +#Function to return portion of OMX matrix based using selection statements +#------------------------------------------------------------------------- +#This function reads a portion of an OMX matrix using selection statements to define the portion +#Multiple selection selection statements can be used for each dimension +#Each selection statement is a logical expression represented in a double-quoted string +#The left operand is the name of a lookup vector +#The operator can be any logical operator including %in% +#The right operand is the value or values to check against. This can be the name of a vector defined in the calling environment +#If the right operand contains literal string values, those values must be single-quoted +#Multiple selection conditions may be used as argument by including in a vector +#Multiple selection conditions are treated as intersections +#Arguments: +#OMXFileName = Path name of the OMX file where the lookup resides. +#MatrixName = Name of the matrix in the OMX file +#RowSelection = Row selection statement or vector of row selection statements (see above) +#ColSelection = Column selection statement or vector of column selection statements (see above +#RowLabels = Name of lookup to use for labeling rows +#ColLabels = Name of lookup to use for labeling columns +#Return: The selected matrix + +readSelectedOMX <- function( OMXFileName, MatrixName, RowSelection=NULL, ColSelection=NULL, RowLabels=NULL, ColLabels=NULL ) { + #Get the matrix dimensions specified in the file + RootAttr <- getRootAttrOMX( OMXFileName ) + Shape <- RootAttr$SHAPE + #Define function to parse a selection statement and return corresponding data indices + findIndex <- function( SelectionStmt ) { + StmtParse <- unlist( strsplit( SelectionStmt, " " ) ) + IsBlank <- sapply( StmtParse, nchar ) == 0 + StmtParse <- StmtParse[ !IsBlank ] + LookupName <- StmtParse[1] + Lookup <- readLookupOMX( OMXFileName, LookupName ) + assign( LookupName, Lookup[[1]] ) + which( eval( parse( text=SelectionStmt ) ) ) + } + #Make index for row selection + if( !is.null( RowSelection ) ) { + RowIndex <- 1:Shape[1] + for( Stmt in RowSelection ) { + Index <- findIndex( Stmt ) + RowIndex <- intersect( RowIndex, Index ) + rm( Index ) + } + } else { + RowIndex <- NULL + } + #Make index for column selection + if( !is.null( ColSelection ) ) { + ColIndex <- 1:Shape[2] + for( Stmt in ColSelection ) { + Index <- findIndex( Stmt ) + ColIndex <- intersect( ColIndex, Index ) + rm( Index ) + } + } else { + ColIndex <- NULL + } + #Extract the matrix meeting the selection criteria + Result <- readMatrixOMX( OMXFileName, MatrixName, RowIndex=RowIndex, ColIndex=ColIndex ) + #Label the rows and columns + if( !is.null( RowLabels ) ) { + rownames( Result ) <- readLookupOMX( OMXFileName, RowLabels )[[1]][ RowIndex ] + } + if( !is.null( ColLabels ) ) { + colnames( Result ) <- readLookupOMX( OMXFileName, ColLabels )[[1]][ ColIndex ] + } + #Return the matrix + Result +} From 2f9708409b9649d766a7d7269396a0291b9b1274 Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:19:02 -0400 Subject: [PATCH 2/8] Ignore R construction files. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..807ea25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData From 5d005437e90edfd6133dcd41b3e4210f0e5c4289 Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:34:34 -0400 Subject: [PATCH 3/8] Add Apache license. This is the original license from the OMX repo. --- DESCRIPTION | 2 +- LICENSE.TXT | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 LICENSE.TXT diff --git a/DESCRIPTION b/DESCRIPTION index 443a39f..88d67a3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,5 +6,5 @@ Date: 2015-07-15 Author: Ben Stabler, Brian Gregor Maintainer: Greg Macfarlane Description: Look at OMX website. -License: GPL +License: Apache LazyData: TRUE diff --git a/LICENSE.TXT b/LICENSE.TXT new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE.TXT @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. From 41f9ec71877bfe2615ba14cdbc7752cf71e3fe8f Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:35:31 -0400 Subject: [PATCH 4/8] Update Description Add a version number and descriptive text. --- DESCRIPTION | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 88d67a3..5dec25c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,10 +1,12 @@ Package: omxr Type: Package Title: Read and Write Open Matrix Files -Version: 0.1 +Version: 0.1.0 Date: 2015-07-15 Author: Ben Stabler, Brian Gregor Maintainer: Greg Macfarlane -Description: Look at OMX website. +Description: The Open Matrix eXchange (OMX) file type is an open specification +for sharing data from transportation models. The specification is built on HDF5; +APIs to read and write are available for Cube, Emme, Python, and R. License: Apache LazyData: TRUE From 4f26841355089784f3c87773e44d74a97c6299bf Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:35:39 -0400 Subject: [PATCH 5/8] Add readme. --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3e06ad --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +OMX: Open Matrix exchange format API for R +=== + +This is a reimplementation of the R API maintained by osPlanning and stored in a +[GitHub repository](https://github.com/osPlanning/omx/). + +To install these functions in your workspace, + + source('R/omx.R') + +Note that these functions depend on the rhdf5 v2.5.1+ package from bioconductor. + + + +License +----------------- +All code written in the OMX project, including all API implementations, +is under the Apache License, version 2.0. + +See LICENSE.TXT for the full Apache 2.0 license text. From 95dec59018af08550d9d0acb082ea96746152fa9 Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:37:47 -0400 Subject: [PATCH 6/8] Format description. --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5dec25c..023dee4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ Date: 2015-07-15 Author: Ben Stabler, Brian Gregor Maintainer: Greg Macfarlane Description: The Open Matrix eXchange (OMX) file type is an open specification -for sharing data from transportation models. The specification is built on HDF5; -APIs to read and write are available for Cube, Emme, Python, and R. + for sharing data from transportation models. The specification is built on HDF5; + APIs to read and write are available for Cube, Emme, Python, and R. License: Apache LazyData: TRUE From 9eb9c1f35b68b519ccff3142c954c0ad0d025c36 Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:39:06 -0400 Subject: [PATCH 7/8] Remove library call. Calling the library in this script simply prevents the package from building. The package will call it when necessary. --- R/omx.R | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/R/omx.R b/R/omx.R index ef24b11..5aba993 100644 --- a/R/omx.R +++ b/R/omx.R @@ -9,8 +9,6 @@ #Transposes matrix when writing it to file to be in row major order like C/Python ################################################################################ -#Load the rhdf5 library -library( rhdf5 ) #Function to create an OMX file that is ready for writing data #------------------------------------------------------------- @@ -39,7 +37,7 @@ createFileOMX <- function( Filename, Numrows, Numcols, Level=1 ) { #This function reads the SHAPE and VERSION attributes of an OMX file. This is called by several other functions #Arguments: #OMXFileName = full path name of the OMX file being read -#Return: List containing the SHAPE and VERSION attributes. +#Return: List containing the SHAPE and VERSION attributes. #SHAPE component is a vector of number of rows and columns #VERSION component is the version number getRootAttrOMX <- function( OMXFileName ) { @@ -70,7 +68,7 @@ getRootAttrOMX <- function( OMXFileName ) { #Function returns TRUE if completed successfully #Return: TRUE -writeMatrixOMX <- function( OMXFileName, Matrix, MatrixSaveName, RowIndex=NULL, ColIndex=NULL, NaValue=-1 , +writeMatrixOMX <- function( OMXFileName, Matrix, MatrixSaveName, RowIndex=NULL, ColIndex=NULL, NaValue=-1 , Replace=FALSE, Description="" ) { #Get names of matrices in the file vc Contents <- h5ls( OMXFileName ) @@ -222,7 +220,7 @@ writeLookupOMX <- function( OMXFileName, LookupVector, LookupSaveName, LookupDim #Rows - number of rows in the matrix #Columns - number of columns in the matrix #Matrices - a dataframe identifying the matrices and all their attributes -#Lookups - a dataframe identifying the lookups and all their attributes +#Lookups - a dataframe identifying the lookups and all their attributes listOMX <- function( OMXFileName ) { #Get the version and shape information RootAttr <- getRootAttrOMX( OMXFileName ) @@ -258,7 +256,7 @@ listOMX <- function( OMXFileName ) { H5Gclose( H5Group ) H5Fclose( H5File ) MatAttr <- do.call( rbind, lapply( MatAttr, function(x) data.frame(x) ) ) - rm( Names, Types ) + rm( Names, Types ) #Read the lookup attribute information H5File <- H5Fopen( OMXFileName ) H5Group <- H5Gopen( H5File, "lookup" ) @@ -288,7 +286,7 @@ listOMX <- function( OMXFileName ) { } H5Gclose( H5Group ) H5Fclose( H5File ) - LookupAttr <- do.call( rbind, lapply( LookupAttr, function(x) data.frame(x) ) ) + LookupAttr <- do.call( rbind, lapply( LookupAttr, function(x) data.frame(x) ) ) rm( Names, Types ) #Combine the results into a list if(length(MatAttr)>0) { @@ -301,14 +299,14 @@ listOMX <- function( OMXFileName ) { } #Function to read an OMX matrix -#------------------------------ +#------------------------------ #This function reads an entire matrix in an OMX file or portions of a matrix using indexing. #Arguments: #OMXFileName = Path name of the OMX file where the matrix resides. #MatrixName = Name of the matrix in the OMX file #RowIndex = Vector containing numerical indices of the rows to be read. #ColIndex = Vector containing numerical indices of the columns to be read -#Return: a matrix +#Return: a matrix readMatrixOMX <- function( OMXFileName, MatrixName, RowIndex=NULL, ColIndex=NULL ) { #Get the matrix dimensions specified in the file RootAttr <- getRootAttrOMX( OMXFileName ) @@ -342,13 +340,13 @@ readMatrixOMX <- function( OMXFileName, MatrixName, RowIndex=NULL, ColIndex=NULL NAValue = as.vector( attr( h5read( OMXFileName, ItemName, read.attribute=T ), "NA" ) ) if(!is.null(NAValue)) { Result[ Result == NAValue ] <- NA - } + } #Return the result Result } #Function to read an OMX lookup -#------------------------------ +#------------------------------ #This function reads a lookup and its attributes. #Arguments: #OMXFileName = Path name of the OMX file where the lookup resides. @@ -440,10 +438,10 @@ readSelectedOMX <- function( OMXFileName, MatrixName, RowSelection=NULL, ColSele #Label the rows and columns if( !is.null( RowLabels ) ) { rownames( Result ) <- readLookupOMX( OMXFileName, RowLabels )[[1]][ RowIndex ] - } + } if( !is.null( ColLabels ) ) { colnames( Result ) <- readLookupOMX( OMXFileName, ColLabels )[[1]][ ColIndex ] } #Return the matrix - Result -} + Result +} From 7e10e061b803a98bb0c01bdefe0c8d3babfecde7 Mon Sep 17 00:00:00 2001 From: Greg Macfarlane Date: Wed, 15 Jul 2015 21:40:01 -0400 Subject: [PATCH 8/8] Add email address for maintainer. --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 023dee4..0e1d445 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -4,7 +4,7 @@ Title: Read and Write Open Matrix Files Version: 0.1.0 Date: 2015-07-15 Author: Ben Stabler, Brian Gregor -Maintainer: Greg Macfarlane +Maintainer: Greg Macfarlane Description: The Open Matrix eXchange (OMX) file type is an open specification for sharing data from transportation models. The specification is built on HDF5; APIs to read and write are available for Cube, Emme, Python, and R.