RBioFormats icon indicating copy to clipboard operation
RBioFormats copied to clipboard

Saving OIB: metadata is lost

Open hmbotelho opened this issue 3 years ago • 2 comments

When saving this OIB file with RBioFormats the pixel size metadata is lost.

Here's what I did:

library(RBioFormats)
img = read.image("Image0010_01.oib", normalize = FALSE)[,,2]
write.image(img, "Image0010_01.ome.tiff", force = TRUE, pixelType = "uint16")

Doing the equivalent using the bfconvert command line tool preserves the pixel size metadata:

bfconvert Image0010_01.oib Image0010_01.ome.tiff

More info here: https://github.com/hmbotelho/oib_to_tif

hmbotelho avatar May 16 '22 11:05 hmbotelho

Thanks a lot for reporting this! Actually, when converting an OIB file to OME.TIFF pixel size metadata seems to be preserved both by bfconvert and RBioFormats.

library(RBioFormats)
#> Loading required package: rJava
#> BioFormats library version 6.9.1

fname_oib <- "Image0010_01.oib"
oib <- read.image(fname_oib, normalize = FALSE, subset = list(C = 2))

globalMetadata(oib, pattern = "WidthConvertValue|HeightConvertValue|WidthUnit")
#> $`[Reference Image Parameter] WidthUnit`
#> [1] "um"
#> 
#> $`[Reference Image Parameter] HeightConvertValue`
#> [1] "6.214"
#> 
#> $`[Reference Image Parameter] WidthConvertValue`
#> [1] "6.214"

fname_tif <- sub(".oib", ".ome.tif", fname_oib)
write.image(oib, fname_tif, force = TRUE)
tif <- read.image(fname_tif, normalize = FALSE)

globalMetadata(tif, pattern = "WidthConvertValue|HeightConvertValue|WidthUnit")
#> $`[Reference Image Parameter] WidthUnit`
#> [1] "um"
#> 
#> $`[Reference Image Parameter] HeightConvertValue`
#> [1] "6.214"
#> 
#> $`[Reference Image Parameter] WidthConvertValue`
#> [1] "6.214"

Created on 2022-05-17 by the reprex package (v2.0.1)

Nevertheless, there seem to be some problem in handling of resolution-related metadata entries in the case of plain TIFF files, as illustrated by the following example. I will look into this, so stay tuned.

library(RBioFormats)
#> Loading required package: rJava
#> BioFormats library version 6.9.1

fname_oib <- "Image0010_01.oib"
fname_tif <- sub(".oib", ".tif", fname_oib)

oib <- read.image(fname_oib, normalize = FALSE, subset = list(C = 2))
write.image(oib, fname_tif, force = TRUE)
tif <- read.image(fname_tif, normalize = FALSE)

globalMetadata(tif, pattern = "Resolution")
#> $YResolution
#> [1] 0
#> 
#> $ResolutionUnit
#> [1] "Centimeter"
#> 
#> $XResolution
#> [1] 0

system(sprintf("bfconvert -channel 1 -overwrite %s %s", fname_oib, fname_tif))
tif <- read.image(fname_tif, normalize = FALSE)

globalMetadata(tif, pattern = "Resolution")
#> $YResolution
#> [1] 1609.269
#> 
#> $ResolutionUnit
#> [1] "Centimeter"
#> 
#> $XResolution
#> [1] 1609.269

Created on 2022-05-17 by the reprex package (v2.0.1)

aoles avatar May 16 '22 23:05 aoles

Thanks for checking this out and comparing TIF and OME.TIFF

Indeed the resolution metadata is stored by RBioFormats, but in a way which is not accessible to Fiji (which my downstream workflow relies on).

I think I found the source of the issue. It sems to be one OME-XML metadata field, which bftools sets as: <Pixels BigEndian="false" DimensionOrder="XYCZT" ID="Pixels:0" Interleaved="false" PhysicalSizeX="6.214" PhysicalSizeXUnit="µm" PhysicalSizeY="6.214" PhysicalSizeYUnit="µm" PhysicalSizeZ="1.0" PhysicalSizeZUnit="µm" SignificantBits="12" SizeC="2" SizeT="1" SizeX="512" SizeY="512" SizeZ="1" TimeIncrement="1.0" TimeIncrementUnit="s" Type="uint16">

and RBioFormats sets as <Pixels BigEndian="true" DimensionOrder="XYCZT" ID="Pixels:0" Interleaved="false" SignificantBits="16" SizeC="1" SizeT="1" SizeX="512" SizeY="512" SizeZ="1" Type="uint16">

Is there a way to edit this metadata field in RBioFormats? I checked the Metadata setting functions but did not know how to use them.

Maybe this approach can also help in dealing with plain TIFF files?

hmbotelho avatar May 17 '22 10:05 hmbotelho