Skip to content

Commit

Permalink
Read OME-Zarr HCS from S3 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tischi committed Nov 22, 2023
1 parent e1485b0 commit b760696
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>37.0.0</version>
<version>36.0.0</version>
</parent>

<groupId>org.embl.mobie</groupId>
Expand Down
66 changes: 31 additions & 35 deletions src/main/java/org/embl/mobie/lib/hcs/Plate.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@
import org.embl.mobie.io.ImageDataFormat;
import org.embl.mobie.io.SpimDataOpener;
import org.embl.mobie.io.toml.TPosition;
import org.embl.mobie.io.util.IOHelper;
import org.embl.mobie.lib.MoBIEHelper;
import org.embl.mobie.lib.color.ColorHelper;

import ch.epfl.biop.bdv.img.bioformats.entity.SeriesIndex;
import org.embl.mobie.lib.hcs.omezarr.Image;
import org.embl.mobie.lib.hcs.omezarr.OMEZarrHCSHelper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -86,45 +88,19 @@ public Plate( String hcsDirectory ) throws IOException
if ( hcsDirectory.endsWith( ".zarr" ) )
{
hcsPattern = HCSPattern.OMEZarr;
imageDataFormat = ImageDataFormat.OmeZarr;

final int minDepth = 3;
final int maxDepth = 3;
final Path rootPath = Paths.get(hcsDirectory);
final int rootPathDepth = rootPath.getNameCount();
imageSitePaths = Files.walk( rootPath, maxDepth )
.filter( e -> e.toFile().isDirectory() )
.filter( e -> e.getNameCount() - rootPathDepth >= minDepth )
.map( e -> e.toString() )
.collect( Collectors.toList() );
}
else
{
imageSitePaths = Files.walk( Paths.get( hcsDirectory ), 3 )
.map( p -> p.toString() )
.collect( Collectors.toList() );
hcsPattern = determineHCSPattern( hcsDirectory, imageSitePaths );
imageSitePaths = imageSitePaths.stream()
.filter( path -> hcsPattern.setMatcher( path ) ) // skip files like .DS_Store a.s.o.
.collect( Collectors.toList() );
}

if ( hcsPattern == HCSPattern.Operetta )
{
//final File xml = new File( hcsDirectory, "Index.idx.xml" );
final File xml = new File( hcsDirectory, "Index.xml" );
operettaMetadata = new OperettaMetadata( xml );
imageSitePaths = imageSitePaths.stream()
.filter( path -> operettaMetadata.contains( path ) ) // skip files like .DS_Store a.s.o.
.collect( Collectors.toList() );
if ( IOHelper.getType( hcsDirectory ).equals( IOHelper.ResourceType.S3 ) )
imageDataFormat = ImageDataFormat.OmeZarrS3;
else
imageDataFormat = ImageDataFormat.OmeZarr;

}
else if ( hcsPattern == HCSPattern.OMEZarr )
{
imageSitePaths = OMEZarrHCSHelper.sitePathsFromMetadata( hcsDirectory );

// determine the number of channels
try
{
final String firstImagePath = imageSitePaths.get( 0 );
AbstractSpimData< ? > spimData = new SpimDataOpener().open( firstImagePath, ImageDataFormat.OmeZarr );
AbstractSpimData< ? > spimData = new SpimDataOpener().open( firstImagePath, imageDataFormat );
final int numChannels = spimData.getSequenceDescription().getViewSetupsOrdered().size();
final List< String > channels = IntStream.range( 0, numChannels )
.mapToObj( i -> ( ( Integer ) i ).toString() )
Expand All @@ -136,6 +112,26 @@ else if ( hcsPattern == HCSPattern.OMEZarr )
throw new RuntimeException( e );
}
}
else if ( hcsPattern == HCSPattern.Operetta )
{
//final File xml = new File( hcsDirectory, "Index.idx.xml" );
final File xml = new File( hcsDirectory, "Index.xml" );
operettaMetadata = new OperettaMetadata( xml );
imageSitePaths = imageSitePaths.stream()
.filter( path -> operettaMetadata.contains( path ) ) // skip files like .DS_Store a.s.o.
.collect( Collectors.toList() );

}
else
{
imageSitePaths = Files.walk( Paths.get( hcsDirectory ), 3 )
.map( p -> p.toString() )
.collect( Collectors.toList() );
hcsPattern = determineHCSPattern( hcsDirectory, imageSitePaths );
imageSitePaths = imageSitePaths.stream()
.filter( path -> hcsPattern.setMatcher( path ) ) // skip files like .DS_Store a.s.o.
.collect( Collectors.toList() );
}

buildPlateMap( imageSitePaths );
}
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/embl/mobie/lib/hcs/omezarr/OMEZarrHCSHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.embl.mobie.lib.hcs.omezarr;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.embl.mobie.io.util.IOHelper;
import org.embl.mobie.lib.serialize.JsonHelper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class OMEZarrHCSHelper
{
public static final String ZATTRS = "/.zattrs";

public static List< String > sitePathsFromFolderStructure( String hcsDirectory ) throws IOException
{
List< String > imageSitePaths;
final int minDepth = 3;
final int maxDepth = 3;
final Path rootPath = Paths.get( hcsDirectory );
final int rootPathDepth = rootPath.getNameCount();
imageSitePaths = Files.walk( rootPath, maxDepth )
.filter( e -> e.toFile().isDirectory() )
.filter( e -> e.getNameCount() - rootPathDepth >= minDepth )
.map( e -> e.toString() )
.collect( Collectors.toList() );
return imageSitePaths;
}

public static List< String > sitePathsFromMetadata( String hcsDirectory ) throws IOException
{
List< String > imageSitePaths = new ArrayList<>();
Gson gson = JsonHelper.buildGson(false);

String plateUri = hcsDirectory;
final String plateJson = IOHelper.read( plateUri + ZATTRS );
//System.out.println( plateJson );
HCSMetadata hcsMetadata = gson.fromJson(plateJson, new TypeToken< HCSMetadata >() {}.getType());

int i = 0;
for ( Well well : hcsMetadata.plate.wells )
{
System.out.println( "Parsing well: " + well.path );
String wellPath = well.path;
String wellUri = IOHelper.combinePath( plateUri, wellPath);
final String wellJson = IOHelper.read( wellUri + ZATTRS );
WellMetadata wellMetadata = gson.fromJson( wellJson, new TypeToken< WellMetadata >() {}.getType() );

for ( Image image : wellMetadata.well.images )
{
String imageUri = IOHelper.combinePath( wellUri, image.path );
imageSitePaths.add( imageUri );
}

i++;
if ( i > 1 ) break;
}

return imageSitePaths;
}
}
46 changes: 46 additions & 0 deletions src/test/java/hcs/HCSOMEZarrS3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-
* #%L
* Fiji viewer for MoBIE projects
* %%
* Copyright (C) 2018 - 2023 EMBL
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package hcs;

import mpicbg.spim.data.SpimDataException;
import net.imagej.ImageJ;
import org.embl.mobie.MoBIE;
import org.embl.mobie.MoBIESettings;

import java.io.IOException;

class HCSOMEZarrS3
{
public static void main( String[] args ) throws SpimDataException, IOException
{
new ImageJ().ui().showUI();

new MoBIE( "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.1/plates/5966.zarr", new MoBIESettings(), 0.1, 0.0 );
}
}

0 comments on commit b760696

Please sign in to comment.