77# ' Write an element to an H5AD file
88# '
99# ' @param value The value to write
10- # ' @param file Path to a H5AD file or an open H5AD handle
10+ # ' @param file An open H5AD handle
1111# ' @param name Name of the element within the H5AD file
1212# ' @param compression The compression to use when writing the element. Can be
1313# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -31,11 +31,11 @@ write_h5ad_element <- function(
3131) {
3232 compression <- match.arg(compression )
3333
34- # cli::cli_alert_info("Writing {.path {name}} with {.pkg rhdf5}")
35-
3634 # Sparse matrices
3735 write_fun <-
38- if (inherits(value , " sparseMatrix" )) {
36+ if (is.null(value )) {
37+ write_h5ad_null
38+ } else if (inherits(value , " sparseMatrix" )) {
3939 # Sparse matrices
4040 write_h5ad_sparse_array
4141 } else if (is.factor(value )) {
@@ -148,12 +148,42 @@ write_h5ad_encoding <- function(file, name, encoding, version) {
148148 )
149149}
150150
151+ # ' Write H5AD null
152+ # '
153+ # ' Write a null dataset to an H5AD file
154+ # '
155+ # ' @param value Value to write, not used
156+ # ' @param file An open H5AD handle
157+ # ' @param name Name of the element within the H5AD file
158+ # ' @param compression Not used as there is no value
159+ # ' @param version Encoding version of the element to write
160+ # '
161+ # ' @noRd
162+ write_h5ad_null <- function (value , file , name , compression , version = " 0.1.0" ) {
163+ if (isFALSE(getOption(" anndataR.write_null" , " TRUE" ))) {
164+ return (invisible (NULL ))
165+ }
166+
167+ h5s <- rhdf5 :: H5Screate(" H5S_NULL" )
168+ on.exit(rhdf5 :: H5Sclose(h5s ), add = TRUE )
169+
170+ h5d <- rhdf5 :: H5Dcreate(
171+ file ,
172+ name = name ,
173+ dtype_id = " H5T_IEEE_F32LE" ,
174+ h5space = h5s
175+ )
176+ on.exit(rhdf5 :: H5Dclose(h5d ), add = TRUE )
177+
178+ write_h5ad_encoding(file , name , " null" , version )
179+ }
180+
151181# ' Write H5AD dense array
152182# '
153183# ' Write a dense array to an H5AD file
154184# '
155185# ' @param value Value to write
156- # ' @param file Path to a H5AD file or an open H5AD handle
186+ # ' @param file An open H5AD handle
157187# ' @param name Name of the element within the H5AD file
158188# ' @param compression The compression to use when writing the element. Can be
159189# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -222,7 +252,7 @@ write_h5ad_dense_array <- function(
222252# ' @noRd
223253# '
224254# ' @param value Value to write
225- # ' @param file Path to a H5AD file or an open H5AD handle
255+ # ' @param file An open H5AD handle
226256# ' @param name Name of the element within the H5AD file
227257# ' @param compression The compression to use when writing the element. Can be
228258# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -295,7 +325,7 @@ write_h5ad_sparse_array <- function(
295325# ' @noRd
296326# '
297327# ' @param value Value to write
298- # ' @param file Path to a H5AD file or an open H5AD handle
328+ # ' @param file An open H5AD handle
299329# ' @param name Name of the element within the H5AD file
300330# ' @param compression The compression to use when writing the element. Can be
301331# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -339,7 +369,7 @@ write_h5ad_nullable_boolean <- function(
339369# ' @noRd
340370# '
341371# ' @param value Value to write
342- # ' @param file Path to a H5AD file or an open H5AD handle
372+ # ' @param file An open H5AD handle
343373# ' @param name Name of the element within the H5AD file
344374# ' @param compression The compression to use when writing the element. Can be
345375# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -382,7 +412,7 @@ write_h5ad_nullable_integer <- function(
382412# ' @noRd
383413# '
384414# ' @param value Value to write
385- # ' @param file Path to a H5AD file or an open H5AD handle
415+ # ' @param file An open H5AD handle
386416# ' @param name Name of the element within the H5AD file
387417# ' @param compression The compression to use when writing the element. Can be
388418# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -394,6 +424,14 @@ write_h5ad_string_array <- function(
394424 compression ,
395425 version = " 0.2.0"
396426) {
427+ if (! is.vector(value )) {
428+ if (is.matrix(value )) {
429+ value <- t(value )
430+ } else if (is.array(value )) {
431+ value <- aperm(value )
432+ }
433+ }
434+
397435 hdf5_write_dataset(
398436 file = file ,
399437 name = name ,
@@ -411,7 +449,7 @@ write_h5ad_string_array <- function(
411449# ' @noRd
412450# '
413451# ' @param value Value to write
414- # ' @param file Path to a H5AD file or an open H5AD handle
452+ # ' @param file An open H5AD handle
415453# ' @param name Name of the element within the H5AD file
416454# ' @param compression The compression to use when writing the element. Can be
417455# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -467,7 +505,7 @@ write_h5ad_categorical <- function(
467505# ' @noRd
468506# '
469507# ' @param value Value to write
470- # ' @param file Path to a H5AD file or an open H5AD handle
508+ # ' @param file An open H5AD handle
471509# ' @param name Name of the element within the H5AD file
472510# ' @param compression The compression to use when writing the element. Can be
473511# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -496,7 +534,7 @@ write_h5ad_string_scalar <- function(
496534# ' @noRd
497535# '
498536# ' @param value Value to write
499- # ' @param file Path to a H5AD file or an open H5AD handle
537+ # ' @param file An open H5AD handle
500538# ' @param name Name of the element within the H5AD file
501539# ' @param compression The compression to use when writing the element. Can be
502540# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -535,7 +573,7 @@ write_h5ad_numeric_scalar <- function(
535573# ' @noRd
536574# '
537575# ' @param value Value to write
538- # ' @param file Path to a H5AD file or an open H5AD handle
576+ # ' @param file An open H5AD handle
539577# ' @param name Name of the element within the H5AD file
540578# ' @param compression The compression to use when writing the element. Can be
541579# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
@@ -569,7 +607,7 @@ write_h5ad_mapping <- function(
569607# ' @noRd
570608# '
571609# ' @param value Value to write
572- # ' @param file Path to a H5AD file or an open H5AD handle
610+ # ' @param file An open H5AD handle
573611# ' @param name Name of the element within the H5AD file
574612# ' @param compression The compression to use when writing the element. Can be
575613# ' one of `"none"`, `"gzip"` or `"lzf"`. Defaults to `"none"`.
0 commit comments