Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use bioformats2raw to export HCS data #118

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_long_description() -> str:
author="The Open Microscopy Team",
author_email="",
python_requires=">=3",
install_requires=["omero-py>=5.6.0", "ome-zarr>=0.3.1.dev0"],
install_requires=["omero-py>=5.6.0", "ome-zarr>=0.3.0"],
long_description=long_description,
keywords=["OMERO.CLI", "plugin"],
url="https://github.com/ome/omero-cli-zarr/",
Expand Down
86 changes: 53 additions & 33 deletions src/omero_zarr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any, Callable, List

from omero.cli import CLI, BaseControl, Parser, ProxyStringType
from omero.gateway import BlitzGateway, BlitzObjectWrapper
from omero.gateway import BlitzGateway, BlitzObjectWrapper, ImageWrapper
from omero.model import ImageI, PlateI
from zarr.hierarchy import open_group
from zarr.storage import FSStore
Expand Down Expand Up @@ -307,7 +307,11 @@ def export(self, args: argparse.Namespace) -> None:
image_to_zarr(image, args)
elif isinstance(args.object, PlateI):
plate = self._lookup(self.gateway, "Plate", args.object.id)
plate_to_zarr(plate, args)
if args.bf or args.bfpath:
self._bf_export(plate, args)
plate_to_zarr(plate, args, write_binary=False)
else:
plate_to_zarr(plate, args, write_binary=True)

def _lookup(
self, gateway: BlitzGateway, otype: str, oid: int
Expand All @@ -319,25 +323,35 @@ def _lookup(
self.ctx.die(110, f"No such {otype}: {oid}")
return obj

def _bf_export(self, image: BlitzObjectWrapper, args: argparse.Namespace) -> None:
def _bf_export(self, obj: BlitzObjectWrapper, args: argparse.Namespace) -> None:
def _get_first_image(obj: BlitzObjectWrapper) -> ImageWrapper:
if obj.OMERO_CLASS == "Plate":
for well in obj.listChildren():
for ws in well.listChildren():
return ws.getImage()
else:
return obj

if args.bfpath:
abs_path = Path(args.bfpath)
elif image.getInplaceImport():
p = image.getImportedImageFilePaths()["client_paths"][0]
abs_path = Path("/") / Path(p)
else:
if self.client is None:
raise Exception("This cannot happen") # mypy is confused
prx, desc = self.client.getManagedRepository(description=True)
p = image.getImportedImageFilePaths()["server_paths"][0]
abs_path = Path(desc._path._val) / Path(desc._name._val) / Path(p)
temp_target = (Path(args.output) or Path.cwd()) / f"{image.id}.tmp"
image_target = (Path(args.output) or Path.cwd()) / f"{image.id}.zarr"
first_image = _get_first_image(obj)
if first_image.getInplaceImport():
p = first_image.getImportedImageFilePaths()["client_paths"][0]
abs_path = Path("/") / Path(p)
else:
if self.client is None:
raise Exception("This cannot happen") # mypy is confused
prx, desc = self.client.getManagedRepository(description=True)
p = first_image.getImportedImageFilePaths()["server_paths"][0]
abs_path = Path(desc._path._val) / Path(desc._name._val) / Path(p)
temp_target = (Path(args.output) or Path.cwd()) / f"{obj.id}.tmp"
zarr_target = (Path(args.output) or Path.cwd()) / f"{obj.id}.zarr"

if temp_target.exists():
self.ctx.die(111, f"{temp_target.resolve()} already exists")
if image_target.exists():
self.ctx.die(111, f"{image_target.resolve()} already exists")
if zarr_target.exists():
self.ctx.die(111, f"{zarr_target.resolve()} already exists")

cmd: List[str] = [
"bioformats2raw",
Expand All @@ -353,9 +367,10 @@ def _bf_export(self, image: BlitzObjectWrapper, args: argparse.Namespace) -> Non
cmd.append(f"--resolutions={args.resolutions}")
if args.max_workers:
cmd.append(f"--max_workers={args.max_workers}")
cmd.append(f"--series={image.series}")
cmd.append("--no-root-group")
cmd.append("--no-ome-meta-export")
if obj.OMERO_CLASS == "Image":
cmd.append(f"--series={obj.series}")
cmd.append("--no-root-group")
cmd.append("--no-ome-meta-export")

self.ctx.dbg(" ".join(cmd))
process = subprocess.Popen(
Expand All @@ -367,21 +382,26 @@ def _bf_export(self, image: BlitzObjectWrapper, args: argparse.Namespace) -> Non
if stderr:
self.ctx.err(stderr.decode("utf-8"))
if process.returncode == 0:
image_source = temp_target / "0"
image_source.rename(image_target)
temp_target.rmdir()
self.ctx.out(f"Image exported to {image_target.resolve()}")

# Add OMERO metadata
store = FSStore(
str(image_target.resolve()),
auto_mkdir=False,
normalize_keys=False,
mode="w",
)
root = open_group(store)
add_omero_metadata(root, image)
add_toplevel_metadata(root)
if obj.OMERO_CLASS == "Image":
image_source = temp_target / "0"
image_source.rename(zarr_target)
temp_target.rmdir()
else:
temp_target.rename(zarr_target)
self.ctx.out(f"{obj.OMERO_CLASS} exported to {zarr_target.resolve()}")

if obj.OMERO_CLASS == "Image":
# Add OMERO metadata
store = FSStore(
str(zarr_target.resolve()),
auto_mkdir=False,
normalize_keys=False,
mode="w",
)

root = open_group(store)
add_omero_metadata(root, obj)
add_toplevel_metadata(root)


try:
Expand Down
37 changes: 22 additions & 15 deletions src/omero_zarr/raw_pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ def marshal_acquisition(acquisition: omero.gateway._PlateAcquisitionWrapper) ->
return acq


def plate_to_zarr(plate: omero.gateway._PlateWrapper, args: argparse.Namespace) -> None:
def plate_to_zarr(
plate: omero.gateway._PlateWrapper,
args: argparse.Namespace,
write_binary: Optional[bool] = True,
) -> None:
"""
Exports a plate to a zarr file using the hierarchy discussed here ('Option 3'):
https://github.com/ome/omero-ms-zarr/issues/73#issuecomment-706770955
Expand Down Expand Up @@ -262,23 +266,26 @@ def plate_to_zarr(plate: omero.gateway._PlateWrapper, args: argparse.Namespace)
row_group = root.require_group(row)
col_group = row_group.require_group(col)
field_group = col_group.require_group(field_name)
add_image(img, field_group, cache_dir=cache_dir)
if write_binary:
add_image(img, field_group, cache_dir=cache_dir)
add_omero_metadata(field_group, img)
# Update Well metadata after each image
write_well_metadata(col_group, fields)
if write_binary:
# Update Well metadata after each image
write_well_metadata(col_group, fields)
max_fields = max(max_fields, field + 1)
print_status(int(t0), int(time.time()), count, total)

# Update plate_metadata after each Well
write_plate_metadata(
root,
row_names,
col_names,
wells=list(well_paths),
field_count=max_fields,
acquisitions=plate_acq,
name=plate.name,
)
if write_binary:
# Update plate_metadata after each Well
write_plate_metadata(
root,
row_names,
col_names,
wells=list(well_paths),
field_count=max_fields,
acquisitions=plate_acq,
name=plate.name,
)

add_toplevel_metadata(root)
print("Finished.")
Expand All @@ -287,7 +294,7 @@ def plate_to_zarr(plate: omero.gateway._PlateWrapper, args: argparse.Namespace)
def add_omero_metadata(zarr_root: Group, image: omero.gateway.ImageWrapper) -> None:

image_data = {
"id": 1,
"id": image.getId(),
"channels": [channelMarshal(c) for c in image.getChannels()],
"rdefs": {
"model": (image.isGreyscaleRenderingModel() and "greyscale" or "color"),
Expand Down