Plugin to use Bio-Formats and ImageJ2

Hello,
I want to use Bio-formats within an ImageJ2 plugin but I cannot get the plugin to recognise any formats other than tiff, jpeg etc. Any microscopy proprietary formats can’t be read. I’ve been struggling with what seems to be a basic problem for a while now so I would really appreciate anyone from the Bioformats or Image.net teams giving me a few pointers.

Thanks
David

This is the code I’m using

// create the ImageJ application context with all available services
        final ImageJ ij = new ImageJ();
        ij.launch(args);
        ij.ui().showUI();
        // ask the user for a file to open
        final File file = ij.ui().chooseFile(null, "open");

        if (file != null) {
            // load the dataset
            final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());
            // show the image
            ij.ui().show(dataset);
           
        }

This is the very long dependency list I’m using in my pom file:

<dependencies>
		<dependency>
			<groupId>net.imagej</groupId>
			<artifactId>imagej</artifactId>
		</dependency>
		<dependency>
			<groupId>loci</groupId>
    		<artifactId>loci-common</artifactId>
    		<version>5.0.0-beta1</version>
		</dependency>
		<dependency>
    		<groupId>loci</groupId>
    		<artifactId>loci_plugins</artifactId>
    		<version>5.0.0-beta1</version>
		</dependency>
		<dependency>
    		<groupId>ome</groupId>
    		<artifactId>formats-api</artifactId>
    		<version>6.1.1</version>
		</dependency>
		<dependency>
    		<groupId>loci</groupId>
    		<artifactId>loci-common</artifactId>
    		<version>5.0.0-beta1</version>
		</dependency>		
		<dependency>
    		<groupId>net.imagej</groupId>
    		<artifactId>imagej</artifactId>
    		<version>imagej-2.0.0-rc-69</version>
		</dependency>
		
		<dependency>
			<groupId>net.imglib2</groupId>
			<artifactId>imglib2</artifactId>
		</dependency>
		
		<dependency>
			<groupId>io.scif</groupId>
			<artifactId>scifio</artifactId>
		</dependency>
		<dependency>
			<groupId>io.scif</groupId>
			<artifactId>scifio-ome-xml</artifactId>
		</dependency>
		<dependency>
			<groupId>org.scijava</groupId>
			<artifactId>scijava-common</artifactId>
		</dependency>
		<dependency>
    		<groupId>ome</groupId>
    		<artifactId>bioformats_package</artifactId>
    		<version>6.5.1</version>
		</dependency>
		<dependency>
    		<groupId>ome</groupId>
    		<artifactId>formats-gpl</artifactId>
    		<version>6.1.1</version>
		</dependency>
		<dependency>
    		<groupId>org.openmicroscopy</groupId>
    		<artifactId>ome-common</artifactId>
    		<version>6.0.6</version>
		</dependency>
		<dependency>
    		<groupId>org.openmicroscopy</groupId>
    		<artifactId>ome-xml</artifactId>
    		<version>6.2.2</version>
		</dependency>
	</dependencies>

This is the error:

Exception in thread "main" java.io.IOException: io.scif.img.ImgIOException: io.scif.FormatException: C:\Users\coil\Desktop\Emoke\201209_L2_4_FalconFibers_IF_ChCID488_RbCENPC555_rCal1647_01_02_R3D_D3D_prj.dv: No supported format found.
	at io.scif.services.DefaultDatasetIOService.open(DefaultDatasetIOService.java:135)
	at io.scif.services.DefaultDatasetIOService.open(DefaultDatasetIOService.java:110)
	at coil.bio.ed.ac.uk.TestOpener.main(TestOpener.java:87)
Caused by: io.scif.img.ImgIOException: io.scif.FormatException: C:\Users\coil\Desktop\Emoke\201209_L2_4_FalconFibers_IF_ChCID488_RbCENPC555_rCal1647_01_02_R3D_D3D_prj.dv: No supported format found.
	at io.scif.img.ImgOpener.createReader(ImgOpener.java:425)
	at io.scif.img.ImgOpener.openImgs(ImgOpener.java:145)
	at io.scif.services.DefaultDatasetIOService.open(DefaultDatasetIOService.java:125)
	... 2 more
Caused by: io.scif.FormatException: C:\Users\coil\Desktop\Emoke\201209_L2_4_FalconFibers_IF_ChCID488_RbCENPC555_rCal1647_01_02_R3D_D3D_prj.dv: No supported format found.
	at io.scif.services.DefaultFormatService.getFormatList(DefaultFormatService.java:350)
	at io.scif.services.DefaultFormatService.getFormat(DefaultFormatService.java:316)
	at io.scif.services.DefaultInitializeService.initializeReader(DefaultInitializeService.java:88)
	at io.scif.img.ImgOpener.createReader(ImgOpener.java:419)
	... 4 more

The code snippet you posted wont actually be using Bio-Formats to read the images which is why the proprietary formats aren’t being recognised. There are some examples of how to use Bio-Formats from within a macro in bioformats/components/bio-formats-plugins/utils at develop · ome/bioformats · GitHub

Hello David,

Thank you for replying. I’ve looked at the code you recommended and I’m now very confused. The code you suggest appears to be ImageJ 1.x as it opens an ImagePlus but it does it using a massive amount of code. The same thing can be accomplished using the following

static{System.setProperty(“plugins.dir”, “C:/Workspace/IJ/plugins”); //This line for working in Eclipse

IJ.run(“Bio-Formats Importer”);
ImagePlus imp = WindowManager.getCurrentImage();

Does ImageJ2 use Bioformats as the various web pages I’ve been looking at suggest that SCIFIO does use it and that ImageJ2 should open proprietary formats. This post by ctrueden:

states that you should not import bioformats jar files to use it and should use Maven but as you can see by my pom.xml file I’ve put in an extensive list of bioformats related dependencies, none of which worked. I believe ctrueden is one of the developers of ImageJ2 so there seems to be a disconnect between what the imageJ2 developers expect and what the bioformats developers expect when it comes to plugin development.

Can you advise on what is the best course, import jar files or go down the maven route.

Thanks

David

Sorry David, those are ImageJ1 plugins. To use the Bio-Formats readers with SCIFIO it looks like you should use the plugin: GitHub - scifio/scifio-bf-compat: SCIFIO (@scifio/scifio) plug-in allowing the of use Bio-Formats readers.

I would definetly stick to using Maven if you can, the link above provides some extra dependencies for the scifio-bf-compat plugin

1 Like

Have you tried with these dependencies ?

<dependency>
			<groupId>ome</groupId>
			<artifactId>formats-bsd</artifactId>
			<exclusions>
				<exclusion>
					<!-- NB: Exclude problematic uber-JAR that bundles commons-io. -->
					<groupId>ch.systems.cisd</groupId>
					<artifactId>jhdf5</artifactId>
				</exclusion>
				<exclusion>
					<!-- NB: Exclude problematic xalan bundle -->
					<groupId>xalan</groupId>
					<artifactId>xalan</artifactId>
				</exclusion>
				<exclusion>
					<!-- NB: Exclude problematic xml-apis bundle -->
					<groupId>xml-apis</groupId>
					<artifactId>xml-apis</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>ome</groupId>
			<artifactId>formats-gpl</artifactId>
			<exclusions>
				<exclusion>
					<!-- NB: Exclude problematic uber-JAR that bundles commons-io. -->
					<groupId>ch.systems.cisd</groupId>
					<artifactId>jhdf5</artifactId>
				</exclusion>
				<exclusion>
					<!-- NB: Exclude problematic xalan bundle -->
					<groupId>xalan</groupId>
					<artifactId>xalan</artifactId>
				</exclusion>
				<exclusion>
					<!-- NB: Exclude problematic xml-apis bundle -->
					<groupId>xml-apis</groupId>
					<artifactId>xml-apis</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

Not sure the exclusions are necessary.

The required dependencies are explained in the readme of GitHub - scifio/scifio-bf-compat: SCIFIO (@scifio/scifio) plug-in allowing the of use Bio-Formats readers. , as explained by @dgault

Thanks for replying @NicoKiaru and @dgault with your help I’ve managed to get an ImageJ2 plugin to open proprietary formats. Just in case any others are struggling I’ll put my pom.xml dependency list and java code below

Java:

public static void main(final String... args) throws Exception {
        // create the ImageJ application context with all available services
        ImageJ ij = new net.imagej.ImageJ();
        ij.ui().showUI();
    	
    	
        // ask the user for a file to open
        final File file = ij.ui().chooseFile(null, "open");
        String fileName = file.toString(); 
        
        if (fileName != null) {
            
        	// load the dataset
        	Dataset dataset = ij.scifio().datasetIO().open(fileName, null);
 			
        	// show the image
            ij.ui().show(dataset);

            // invoke the plugin
            //	ij.command().run(ExampleFileOpen.class, true);
        }
    }

Pom:

<repositories>
		<repository>
			<id>scijava.public</id>
			<url>https://maven.scijava.org/content/groups/public</url>
		</repository>
		
		<repository>
    		<id>imagej.public</id>
    		<url>http://maven.imagej.net/content/groups/public</url>
  		</repository>
  
	</repositories>

	<dependencyManagement>
  		<dependencies>
    		<!-- NB: Inherit the SCIFIO version management. -->
    		<dependency>
      		<groupId>net.imagej</groupId>
      		<artifactId>pom-imagej</artifactId>
      		<version>5.12.0</version>
    		</dependency>
  		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>net.imagej</groupId>
			<artifactId>imagej</artifactId>
		</dependency>
		
		<dependency>
    		<groupId>ome</groupId>
    		<artifactId>bioformats_package</artifactId>
    		<version>6.5.1</version> 
		</dependency>
		
		<dependency>
    		<groupId>sc.fiji</groupId>
    		<artifactId>fiji-lib</artifactId>
		</dependency>

		<dependency>
    		<groupId>ome</groupId>
    		<artifactId>scifio</artifactId>
    		<version>5.0.0-rc2</version>
		</dependency>
	
		<dependency>
  			<groupId>io.scif</groupId>
  			<artifactId>scifio</artifactId>
		</dependency>
		
  		<dependency>
  			<groupId>io.scif</groupId>
  			<artifactId>scifio-bf-compat</artifactId>
		</dependency>

		<dependency>
  			<groupId>ome</groupId>
  			<artifactId>formats-bsd</artifactId>
		</dependency>

		<dependency>
  			<groupId>ome</groupId>
  			<artifactId>formats-gpl</artifactId>
		</dependency>
	</dependencies>
1 Like

This is redundant and can be omitted, since maven.imagej.net was migrated to maven.scijava.org a while ago, and the former now redirects to the latter.

Instead of pom-imagej, I recommend pom-scijava, which is the actual “bill of materials” (BOM) for ImageJ and Fiji.
Instead of putting this in <dependencyManagement>, I’d recommend you use pom-scijava directly as a parent POM in your project.

1 Like

Ok, thanks @imagejan I’ll change my pom to reflect your advice as its going to form the starting block for most of my scripts so needs to be correct.
Regards
David

1 Like

Hi @dkelly604, @imagejan, I wonder if I can ask for your thoughts.

I’ve been banging my head on the same thing but keep getting errors and can’t seem to find a combination that works.

I started with the project here: imagej/example-imagej-command: A simple Maven project implementing an ImageJ command (github.com), but once I added bioformats I ran into duplicated classes errors.

After reading the above I wanted to learn a little and start fresh. If I use the pom.xml sections below (I’m omitting all the middle part about developers and git and the such; let me know if there are other crucial parts to list here).

	<parent>
		<groupId>org.scijava</groupId>
		<artifactId>pom-scijava</artifactId>
		<version>30.0.0</version>
		<relativePath />
	</parent>

        <repositories>
		<repository>
			<id>scijava.public</id>
			<url>https://maven.scijava.org/content/groups/public</url>
		</repository>
		<repository>
            <id>imagej.public</id>
            <url>http://maven.imagej.net/content/groups/public</url>
  		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>net.imagej</groupId>
			<artifactId>imagej</artifactId>
		</dependency>
	</dependencies>

With just this my project will compile fine, and when I run I get IJ2 and the JFrame that I’m starting with. Great.

Now when I add the below I get Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (enforce-rules) on project at the end of the long output, with Rule 3: org.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message: No Duplicate Classes Allowed! at the start.

        <dependency>
              <groupId>ome</groupId>
              <artifactId>bioformats_package</artifactId>
              <version>6.5.1</version> 
		</dependency>

Would really appreciate your input. I don’t write java plugins often, and everytime I try maven and have mostly gone back to manually pulling the jars and pushing to my IJ folder to test. Would love to be able to write with maven, and debug all in the same place.


So, I tried to be clever and see what I might be missing. I added

        <repository>
            <id>ome.releases</id>
            <url>https://artifacts.openmicroscopy.org/artifactory/ome.releases/</url>
        </repository>

and used the most recent bioformats pom code from the maven website

        <!-- https://mvnrepository.com/artifact/ome/bioformats_package -->
        <dependency>
               <groupId>ome</groupId>
                <artifactId>bioformats_package</artifactId>
                <version>6.6.1</version>
                <type>pom</type>
        </dependency>

but alas, got the same duplicated classes errors :frowning:

Thanks in advance for your time.

Neil

Adding more repositories won’t help, the scijava.public repository is all you need, as it mirrors Maven Central and all the relevant repositories from the ImageJ and OME communities. Just remove imagej.public as well as this ome.releases, it only adds complexity to your POM.


Some of the downstream dependencies of bioformats drag in some jars that contain duplicate classes, i.e. that are already contained in other artifacts, with the same package names and class names. Usually this is the result of re-packaging some classes without shading them, i.e. putting them into different, “shaded” packages.

I don’t remember where this situation is documented, but if you look at other projects that depend on bioformats, you’ll find that the solution is to add some specific exclusions to the bio-formats dependency, see e.g. here:

Hope that helps.

If you link to your own repository, I can have a look at your pom.xml and try to suggest more improvements and simplifications.

Hi @imagejan, thanks for the speedy reply.

I managed to get around the error with your exclude suggestion. See:

I guessed at the group and artifact IDs, and would love any insights on how to best look these things up. I updated the BF version to the latest and that reduced the number of duplicates by a huge amount.

Interestingly, and this is a subtlety of my learning, but it fails if I comment out the <type>pom</type> line, which I understand defaults to a jar. If it safer to use pom in general as jar adds it to the classpath, and presumably causes more duplicates?

Thanks again for your time.
Neil

Running the below command be useful in identifying the duplicate classes:
mvn org.basepom.maven:duplicate-finder-maven-plugin:1.5.0:check

Hello Neil,
I’m very new to ImageJ2 so couldn’t answer your question but I was also getting the duplicate classes so thanks #imagejan and #dgault for the fix

Regards
David

1 Like