Import ome.xml file into ome.tiff and replace metadata

We use an (homebrewed) automated fluorescence microscope to acquire multichannel images. The device uses its own xml file to process the measurement. I have transformed it into ome xml format and stored.q-ome.xml (3,6 KB)
The associated images are stored as png images in a folder.

Now I would like to transform the data into an OME file by using the bfmatlab package.
According to the tutorial/documentation I have build an ome tiff with a minimal set of metadata.

% initialise the plane with 8 channels
plane = zeros(sy, sx, 1,8,1, 'uint8');

% fill the plane with the data from stored images.
...

% make minimal metadata
metadata = createMinimalOMEXMLMetadata(plane);
...

% save ome tiff
bfsave(plane, 'testimage.ome.tiff', 'metadata', metadata);

Until here it works well.

The question is: how to use/import my ome.xml and replace or extend the minimal metadate with it?

Thanks in advance
Micha

Hi @Miguelle

thanks for sharing a minimal example of your workflow. I think you should be able to use the MetadataConverter class. This will allow you to transfer the metadata from your OME-XML file into the metadata object that should be used for writing the OME-TIFF.

As a proof of concept, the following MATLAB code will read the OME-XML file and update the minimal OME metadata object initialized with the dimensions of the image with the rich metadata.

% make minimal metadata
metadata = createMinimalOMEXMLMetadata(plane);

% Open the source OME-XML and convert the metadata
r = bfGetReader('q-ome.xml');
source_metadata = r.getMetadataStore();
ome.xml.meta.MetadataConverter.convertMetadata(source_metadata, metadata)

% save ome tiff
...

Note you might need to fix a few validations issues in order to be able to read the XML file first.

Hi Sebastien,
thanks for your answer, I will try it next week.

Meanwhile I found another way. I passed the whole xml as string into createOMEXMLMetadata (but it is not really an elegant way):

metadata = OMEXMLService.createOMEXMLMetadata(xmlString);

But now another problem occured. After saving the ome.tiff the metadata are lost once again.

In orde to proof this effect I loaded an image with rich metadata and stored it once again. Seems to be a problem with the bfsave function?

fluoview-multi.tif (375,9 KB)

% loding the image with full (user specified) metadata:
% instrument, detector, extended channel info
data = bfopen('fluoview-multi.tif');

%check metadata
omeMeta = data{1, 4};
xmst=omeMeta.dumpXML 

% build a new image
% make new plane (dummy data)
sx=str2num(omeMeta.getPixelsSizeX(0));
sy=str2num(omeMeta.getPixelsSizeY(0));
sz=str2num(omeMeta.getPixelsSizeZ(0));
sc=str2num(omeMeta.getPixelsSizeC(0));
st=str2num(omeMeta.getPixelsSizeT(0));
pxtype=char(omeMeta.getPixelsType(0));
    
plane = zeros(sx, sy, sz ,sc ,st , pxtype);

% save the new image
bfsave(plane, 'new-multi.tif', 'metadata', omeMeta);

%get the image and check metadata again
reader = bfGetReader('new-multi.tif');
omeMeta = reader.getMetadataStore();
omeMeta.dumpXML

% now all user specific settings in metadata has been disappeared

Have a nice weekend

Hi,

For the latter question I think the issue comes from this line

bfsave guesses the format to write into based on the filename extension. The .tif suffix indicates it should write a plain multipage TIFF without the rich instrument metadata. To write OME-TIFF files, you will need to use a .ome.tif or ome.tiff extension.

Let us know how the metadata conversion works when you have a chance to give it a try. A pleasant week-end to you too.

Hi Sebastien,
unbelievable, but so simple. I have renamed the output file to *.ome.tif and it works. Thanks a lot.
I’ve read many of the documentation and api docs today, but I could not find (or I have not seen it) this hint/explanation. Maybe it would be very good to write it in big red letters.
However, the problem is solved.
Best regards