Skip to content

Commit

Permalink
In replicate_zarr tasks, also replicate the ROI table (after 3D->2D c…
Browse files Browse the repository at this point in the history
…onversion, if needed) (ref #113)
  • Loading branch information
tcompa committed Jul 21, 2022
1 parent 09c3717 commit 7cb95a6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
36 changes: 27 additions & 9 deletions fractal/tasks/replicate_zarr_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import json
from glob import glob

import anndata as ad
import zarr
from anndata.experimental import write_elem


def replicate_zarr_structure(zarrurl, newzarrurl=None):
Expand Down Expand Up @@ -107,16 +109,32 @@ def replicate_zarr_structure(zarrurl, newzarrurl=None):
"version": "0.3",
}

for FOV in list_FOVs:
# Create FOV group
group_FOV = group_well.create_group(f"{FOV}/") # noqa: F841
# Check that only the 0-th FOV exists
FOV = 0
if len(list_FOVs) > 1:
raise Exception(
"ERROR: we are in a single-merged-FOV scheme, "
f"but there are {len(list_FOVs)} FOVs."
)

# Create FOV group
group_FOV = group_well.create_group(f"{FOV}/")

# Copy .zattrs file at the COL/ROW/FOV 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]

# Read FOV ROI table
FOV_ROI_table = ad.read_zarr(
zarrurl + f"{row}/{column}/0/tables/FOV_ROI_table"
)

# 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]
# Create table group and write table
group_tables = group_FOV.create_group("tables/")
write_elem(group_tables, "FOV_ROI_table", FOV_ROI_table)


if __name__ == "__main__":
Expand Down
41 changes: 32 additions & 9 deletions fractal/tasks/replicate_zarr_structure_mip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import json
from glob import glob

import anndata as ad
import zarr
from anndata.experimental import write_elem

from fractal.tasks.lib_regions_of_interest import convert_FOV_ROIs_3D_to_2D


def replicate_zarr_structure_mip(zarrurl):
Expand Down Expand Up @@ -97,16 +101,35 @@ def replicate_zarr_structure_mip(zarrurl):
"version": "0.3",
}

for FOV in list_FOVs:
# Create FOV group
group_FOV = group_well.create_group(f"{FOV}/") # noqa: F841
# Check that only the 0-th FOV exists
FOV = 0
if len(list_FOVs) > 1:
raise Exception(
"ERROR: we are in a single-merged-FOV scheme, "
f"but there are {len(list_FOVs)} FOVs."
)

# Create FOV group
group_FOV = group_well.create_group(f"{FOV}/")

# Copy .zattrs file at the COL/ROW/FOV 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]

# Read FOV ROI table
FOV_ROI_table = ad.read_zarr(
zarrurl + f"{row}/{column}/0/tables/FOV_ROI_table"
)

# Convert 3D FOVs to 2D
new_FOV_ROI_table = convert_FOV_ROIs_3D_to_2D(FOV_ROI_table)

# 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]
# Create table group and write new table
group_tables = group_FOV.create_group("tables/")
write_elem(group_tables, "FOV_ROI_table", new_FOV_ROI_table)


if __name__ == "__main__":
Expand Down

0 comments on commit 7cb95a6

Please sign in to comment.