How to access OME Metadata from plugin

Hello
I’m trying to read the OME Metadata from a Nikon .nd2 image. Specifically I want to access the channel name, the metadata is in the format below

I’ve tried the code below but I get a null value

ImageProcessorReader r = new ImageProcessorReader();
r.setId(filepath);
String field = “Channel”;
Object test = r.getMetadataValue(field);

I’d be grateful for any advice on how to access this data, as I’ve had a look on the forum and the only related post was for Matlab and ImageJ macros which is where I got the idea for the above code.

Regards
David

Hi David, theres a few ways to accomplish this depending on the type of macro you need to use. If you are using the r.getMetadataValue(field) value instead then you will want to use the same String that appears in the Show Info window with the metadata key value pairs. Probably the easiest way to access all of the metadata values from a macro would likely be using the below jython example:

reader = ImageProcessorReader()
omeMeta = MetadataTools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(file)
omeMeta.getChannelName(seriesIndex, channelIndex)

Hello @dgault

Thanks for responding. I’m writing this plugin in Eclipse using Java which is having trouble with the MetadataTools in your code. I can find an import loci.formats.MetadataTools; but it gives an error (unable to resolve). I’m using the latest version of loci and all the other loci imports work so I’m wondering if there is a problem with this particular import which is stopping me from trying your code. I’m totally unfamiliar with jython but it looks very similar to the normal java. Do you think this will work if MetadataTools can be fixed.

Regards

David

The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.

Yeah, the same code should work fine with Java, the Bio-Formats API is also Java so it actually makes things easier. The MetadataTools would indeed be loci.formats.MetadataTools, how exactly are you including the dependencies, are you using Maven or Gradle for example or downloading the jars directly?

You can find some Java examples at https://github.com/ome/bio-formats-examples/tree/master/src/main/java, for your particular case this one might be most relevant: https://github.com/ome/bio-formats-examples/blob/bcd0fdd42a9788f733c5157bfcda53234ce87a3a/src/main/java/ReadPhysicalSize.java

The full documentation is at https://docs.openmicroscopy.org/bio-formats/6.5.1/developers/java-library.html

I’m downloading the jars directly and putting them into the plugins directory of my ImageJ install. I guess loci.jar must be working on some level as the other loci imports are OK its just MetadataTools. My code is below.

Thanks

David

import java.awt.Frame;
import java.awt.Window;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import ij.IJ;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.plugin.PlugIn;
import ij.text.TextWindow;
import loci.formats.FormatException;
import loci.formats.MetadataTools;
import loci.formats.ReaderWrapper;
import loci.formats.meta.MetadataStore;
import loci.plugins.util.ImageProcessorReader;

public class Nikon_Z_projection_ implements PlugIn{
String imageFilename;
String directoryPath;

static{
System.setProperty("plugins.dir", "C:/Workspace/IJ/plugins");
}

public void run(String arg) {

processDirectory();

}

public void processDirectory() {
String fts;
String dirstr = IJ.getDirectory("Choose the directory");
File f = new File(dirstr);
ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
int arraysize = names.size();
//Cycle through the files in the directory
for (int a=0; a<arraysize;a++){
fts = names.get(a); //Get specified item from arraylist
Boolean foundit = fts.contains(".nd2");
String filepath = dirstr + fts;
//Construct string to open file using Bio-Formats
if(foundit == true){
String OpenCommand =(dirstr + fts);
IJ.run("Bio-Formats Importer", "open=" + OpenCommand + " color_mode=Default open_files display_ome-xml rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");

getDetails(fts,filepath);

if(a==0){
openDialog();
}
}
}
}

public void openDialog(){
String title="Example";
int width=512,height=512;

GenericDialog gd = new GenericDialog("Z Projector");
gd.addStringField("Title: ", imageFilename);
gd.addNumericField("Width: ", width, 0);
gd.addNumericField("Height: ", height, 0);
gd.showDialog();
if (gd.wasCanceled()) return;
title = gd.getNextString();
width = (int)gd.getNextNumber();
height = (int)gd.getNextNumber();
IJ.newImage(title, "8-bit", width, height, 1);

}

public void getDetails(String fts, String filepath) {
String textWindowName = "OME Metadata - " + fts;
Window frame = null;
frame = WindowManager.getWindow(textWindowName);

ReaderWrapper reader = new ImageProcessorReader();
MetadataStore omeMeta = MetadataTools.createOMEXMLMetadata();
reader.setMetadataStore(omeMeta);
reader.setId(filepath);
int seriesIndex;
int channelIndex;
omeMeta.getChannelName(seriesIndex, channelIndex);

}
}

The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.

All of the Bio-Formats jars will be available in a default Fiji install in the jars/bioformats folder, the MetadataTools in particular will be in the formats-api.jar. You can download the individual components from https://downloads.openmicroscopy.org/bio-formats/6.5.1/artifacts/?C=M;O=D if necessary, formats-api, formats-bsd and formats-gpl are likely the 3 you will need.

If you wanted to use Maven for your dependencies there is an example pom in https://github.com/ome/bio-formats-examples. You can see https://docs.openmicroscopy.org/bio-formats/6.5.1/developers/java-library.html for further details on other methods of including the jars in your Java project.

Thanks, that worked, I put formats-api.jar into my projects libraries in Eclipse. The code I used to extract the channel names is below in case it helps anyone else:

public ArrayList getDetails() throws FormatException, IOException {
int seriesIndex = 0;

	ImageProcessorReader reader = new ImageProcessorReader();
	IMetadata omeMeta = MetadataTools.createOMEXMLMetadata();
	reader.setMetadataStore(omeMeta);
	reader.setId(filepath);
	int numChannels = omeMeta.getChannelCount(seriesIndex);
	ArrayList<String> ChannelName = new ArrayList<String>();

	for (int z = 0; z<numChannels;z++){
		String nextName = omeMeta.getChannelName(seriesIndex, z);
		ChannelName.add(nextName);
	}
	reader.close();
	
	return (ChannelName);
}

I would like to use Maven for dependencies but that would requite using ImageJ2. Unfortunately I can’t find any web pages giving advice/help on using bioformats to open images as yet within ImageJ2, which currently prevents me using ImageJ2.
Thanks for the help, much appreciated.
David