Skip to content

Commit

Permalink
Update replicate_zarr_structure tasks, so that they copy the old zatt…
Browse files Browse the repository at this point in the history
…rs file, including coordinateTransformations (ref #112)
  • Loading branch information
tcompa committed Jul 21, 2022
1 parent 00e011a commit 5c9a964
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 61 deletions.
46 changes: 18 additions & 28 deletions fractal/tasks/replicate_zarr_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Institute for Biomedical Research and Pelkmans Lab from the University of
Zurich.
"""

import json
from glob import glob

Expand Down Expand Up @@ -53,9 +52,6 @@ def replicate_zarr_structure(zarrurl, newzarrurl=None):
well_rows_columns = sorted(
[rc.split("/")[-2:] for rc in glob(zarrurl + "*/*")]
)
levels = sorted(
list(set([rc.split("/")[-1] for rc in glob(zarrurl + "*/*/*/*")]))
)

group_plate = zarr.group(newzarrurl)
plate = zarrurl.replace(".zarr/", "").split("/")[-1]
Expand Down Expand Up @@ -97,36 +93,30 @@ def replicate_zarr_structure(zarrurl, newzarrurl=None):

for row, column in well_rows_columns:

group_well = group_plate.create_group(f"{row}/{column}/")
# Find sites in COL/ROW/.zattrs
path_well_zattrs = zarrurl + f"{row}/{column}/.zattrs"
with open(path_well_zattrs) as well_zattrs_file:
well_zattrs = json.load(well_zattrs_file)
well_images = well_zattrs["well"]["images"]
list_FOVs = sorted([img["path"] for img in well_images])

# Create well group
group_well = group_plate.create_group(f"{row}/{column}/")
group_well.attrs["well"] = {
"images": [{"path": "0"}], # FOV (only 0, by now)
"images": well_images,
"version": "0.3",
}
group_field = group_well.create_group("0/") # noqa: F841

group_field.attrs["multiscales"] = [
{
"version": "0.3",
"axes": [
{"name": "c", "type": "channel"},
{
"name": "z",
"type": "space",
"unit": "micrometer",
},
{"name": "y", "type": "space"},
{"name": "x", "type": "space"},
],
"datasets": [{"path": level} for level in levels],
}
]
for FOV in list_FOVs:
# Create FOV group
group_FOV = group_well.create_group(f"{FOV}/") # noqa: F841

# Copy .zattrs file at the COL/ROW/SITE level
path_zattrs = zarrurl + f"{row}/{column}/0/.zattrs"
with open(path_zattrs) as zattrs_file:
zattrs = json.load(zattrs_file)
group_field.attrs["omero"] = zattrs["omero"]
# Copy .zattrs file at the COL/ROW/SITE level
path_FOV_zattrs = zarrurl + f"{row}/{column}/{FOV}/.zattrs"
with open(path_FOV_zattrs) as FOV_zattrs_file:
FOV_zattrs = json.load(FOV_zattrs_file)
for key in FOV_zattrs.keys():
group_FOV.attrs[key] = FOV_zattrs[key]


if __name__ == "__main__":
Expand Down
45 changes: 12 additions & 33 deletions fractal/tasks/replicate_zarr_structure_mip.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ def replicate_zarr_structure_mip(zarrurl):
[rc.split("/")[-2:] for rc in glob(zarrurl + "*/*")]
)

# Identify subfolders of the FOV folder
level_folders = sorted(
list(set([rc.split("/")[-1] for rc in glob(zarrurl + "*/*/*/*")]))
)
# Filter out subfolders with non-numeric names (e.g. "labels")
levels = [level for level in level_folders if level.isnumeric()]

group_plate = zarr.group(zarrurl_mip)
plate = zarrurl.replace(".zarr/", "").split("/")[-1]
group_plate.attrs["plate"] = {
Expand Down Expand Up @@ -91,43 +84,29 @@ def replicate_zarr_structure_mip(zarrurl):
for row, column in well_rows_columns:

# Find sites in COL/ROW/.zattrs
path_zattrs = zarrurl + f"{row}/{column}/.zattrs"
with open(path_zattrs) as zattrs_file:
zattrs = json.load(zattrs_file)
well_images = zattrs["well"]["images"]
path_well_zattrs = zarrurl + f"{row}/{column}/.zattrs"
with open(path_well_zattrs) as well_zattrs_file:
well_zattrs = json.load(well_zattrs_file)
well_images = well_zattrs["well"]["images"]
list_FOVs = sorted([img["path"] for img in well_images])

# Create well group
group_well = group_plate.create_group(f"{row}/{column}/")

group_well.attrs["well"] = {
"images": well_images,
"version": "0.3",
}

for FOV in list_FOVs:
group_field = group_well.create_group(f"{FOV}/") # noqa: F841
group_field.attrs["multiscales"] = [
{
"version": "0.3",
"axes": [
{"name": "c", "type": "channel"},
{
"name": "z",
"type": "space",
"unit": "micrometer",
},
{"name": "y", "type": "space"},
{"name": "x", "type": "space"},
],
"datasets": [{"path": level} for level in levels],
}
]
# Create FOV group
group_FOV = group_well.create_group(f"{FOV}/") # noqa: F841

# Copy .zattrs file at the COL/ROW/SITE level
path_zattrs = zarrurl + f"{row}/{column}/{FOV}/.zattrs"
with open(path_zattrs) as zattrs_file:
zattrs = json.load(zattrs_file)
group_field.attrs["omero"] = zattrs["omero"]
path_FOV_zattrs = zarrurl + f"{row}/{column}/{FOV}/.zattrs"
with open(path_FOV_zattrs) as FOV_zattrs_file:
FOV_zattrs = json.load(FOV_zattrs_file)
for key in FOV_zattrs.keys():
group_FOV.attrs[key] = FOV_zattrs[key]


if __name__ == "__main__":
Expand Down

0 comments on commit 5c9a964

Please sign in to comment.