Setup
pizzarr
does not depend on the raster
package, but we use it for this demo to render an RGB array to an image using R. We also install the Rarr
package to be able to use Blosc as the decompression codec, since the image we want to load is Blosc-compressed.
install.packages("raster")
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("Rarr")
Demo
library(pizzarr)
# The path to the root of the OME-NGFF Zarr store.
root <- "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr"
# Open the OME-NGFF as an HttpStore.
store <- HttpStore$new(root)
g <- zarr_open_group(store)
# Using the OME metadata, get the path to the first resolution of the image pyramid.
attrs <- g$get_attrs()$to_list()
resolution_paths <- attrs$multiscales[[1]]$datasets[[1]]$path
first_resolution <- resolution_paths[[1]]
# Load the 4-dimensional array of pixels (as a ZarrArray instance).
zarr_arr <- g$get_item(first_resolution)
print(zarr_arr$get_shape())
# [1] 2 236 275 271
z_index <- 118
# Load a single Z-slice of pixels (as a NestedArray instance).
nested_arr <- zarr_arr$get_item(list(slice(1, 2), slice(z_index, z_index), slice(NA, NA), slice(NA, NA)))
print(nested_arr$shape)
# [1] 2 1 275 271
# Extract the NestedArray contents as a base R array.
arr <- nested_arr$data
# Convert to a pseudo-RGB array to make raster package happy.
rg_arr <- aperm(arr, c(2, 4, 3, 1))[1,,,]
rgb_arr <- array(dim=c(271, 275, 3))
rgb_arr[,,1] <- rg_arr[,,1]
rgb_arr[,,2] <- rg_arr[,,2]
rgb_arr[,,3] <- 0
# Convert the base R array to a raster object.
# Re-scale the channel values for visualization.
arr_as_raster <- raster::as.raster(rgb_arr / max(rgb_arr))
# Plot the raster object.
raster::plot(arr_as_raster)