Skip to content

Commit

Permalink
Address #14
Browse files Browse the repository at this point in the history
- Makes it possible to handle empty tables (e.g. 0 features)
- Adds notion of nullable-integer and nullable-boolean
  • Loading branch information
gtca committed Nov 28, 2023
1 parent 9a66a48 commit bdac38f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion R/ReadH5MU.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ ReadH5MU <- function(file) {
# Metadata, features metadata, and variable features
for (modality in names(modalities)) {
# Append modality metadata
srt@meta.data <- cbind.data.frame(srt@meta.data, mod_obs[[modality]])
srt@meta.data <- cbind.data.frame(srt@meta.data, mod_obs[[modality]][obs_names,,drop=FALSE])

# Add modality feature metadata
meta_features_names <- rownames(srt[[modality]]@meta.features)
Expand Down
27 changes: 23 additions & 4 deletions R/ReadUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ read_table_encv1 <- function(dataset, set_index = TRUE) {
columns <- columns[columns != "__categories"]

col_list <- lapply(columns, function(name) {
values <- dataset[[name]]$read()
if (dataset[[name]]$dims == 0) {
values <- list()
} else {
values <- dataset[[name]]$read()
}
values_attr <- tryCatch({
h5attributes(dataset[[name]])
}, error = function(e) {
Expand All @@ -59,7 +63,13 @@ read_table_encv1 <- function(dataset, set_index = TRUE) {
}
values
})
table <- data.frame(Reduce(cbind.data.frame, col_list))
empty_columns <- vapply(col_list, function(e) { length(e) == 0 }, c(TRUE))
if (all(empty_columns)) {
# no rows
table <- data.frame(matrix(ncol = length(columns), nrow = 0))
} else {
table <- data.frame(Reduce(cbind.data.frame, col_list), drop=FALSE)
}
colnames(table) <- columns
table
}
Expand All @@ -77,13 +87,22 @@ read_column <- function(column, etype, eversion) {

values <- factor(as.integer(codes), labels = categories[1:length(codes_notna)])
} else {
warning(paste0("Cannot recognise encoving-version ", eversion))
warning(paste0("Cannot recognise encoding-version ", eversion))
}
} else if (etype == "nullable-integer" || etype == "nullable-boolean") {
# https://anndata.readthedocs.io/en/latest/fileformat-prose.html#nullable-integers-and-booleans
if (eversion == "0.1.0") {
values <- column[["values"]]$read()
mask <- column[["mask"]]$read()
values[mask] <- NA
} else {
warning(paste0("Cannot recognise encoding-version ", eversion))
}
} else {
values <- column$read()
}
# } else {
# stop(paste0("Cannot recognise encoving-type ", etype))
# stop(paste0("Cannot recognise encoding-type ", etype))
# }
values
}
Expand Down

0 comments on commit bdac38f

Please sign in to comment.