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

[WIP] High Content Screening OME-NGFF example #8

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions examples/save_load_hcs_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import napari
import os
import shutil
import squidpy as sq
from ngff_tables_prototype.writer import write_spatial_anndata
from ngff_tables_prototype.reader import load_to_napari_viewer
import numpy as np
import pandas as pd
from pathlib import Path
import anndata as ad

# Instructions
# Download files from here:
# https://drive.google.com/drive/folders/1TWMcKSQuq_ZEkTPPCLOO4sE6oMlhSGu_?usp=sharing
# Place it in the ome-ngff-tables-prototype folder and run the script from
# the example folder
# Unzip the OME-Zarr file
output_fpath = Path("../HCS_Example_Data/UZH_2x2_HCS.ome.zarr")
feature_path = Path("../HCS_Example_Data/FeatureData")

def write_hcs_table() -> None:
# Loop over the 4 sites
# Check that data was downloaded
assert len(os.listdir(feature_path)) == 4, 'Feature data should contain \
for files. Check instructions above'
assert os.path.isdir(output_fpath), 'OME Zarr folder does not exist. Was it unzipped?'

# TODO: Check if feature data has already been written to the OME-Zarr file

# Loop through all 4 fields of view,
# load the feature data & save to OME-Zarr file
for site in range(4):
df = pd.read_csv(feature_path / f'HCS_FeatureData_Site{site}.csv')
df_x = df.iloc[:, :-2]
features_ad = ad.AnnData(df_x.loc[:, df_x.columns != 'Label'])
features_ad.obs = pd.concat([df['Label'], df.iloc[:, -2:]], axis = 1)
write_spatial_anndata(
file_path=output_fpath / 'B' / '03' / str(site),
tables_adata=features_ad,
tables_region="labels/label_image",
tables_instance_key="Label",
)

return


if __name__ == "__main__":
write_hcs_table()
print(output_fpath)
viewer = load_to_napari_viewer(
file_path=output_fpath / 'B' / '03' / '0',
groups=["labels/label_image", "tables/regions_table"],
)
napari.run()
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ install_requires =
napari-ome-zarr
ome-zarr
pandas
matplotlib
squidpy


[options.packages.find]
Expand Down
5 changes: 4 additions & 1 deletion src/ngff_tables_prototype/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def write_spatial_anndata(
# label group
label_image: Optional[np.ndarray] = None,
label_name: str = "label_image",
label_axes: list = ['y', 'x'],
# table group
tables_adata: Optional[AnnData] = None,
tables_region: Optional[Union[str, List[str]]] = None,
Expand Down Expand Up @@ -158,6 +159,8 @@ def write_spatial_anndata(
The name of the label image. See ome-zarr-py for details.
label_image : Union[str, List[str]]
The label image (i.e. segmentation mask). See ome-zarr-py for details.
label_axes: list
The labels for the label image axes. See ome-zarr-py for details. Defaults to ['y', 'x'] for 2D images
tables_adata:
The :class:`anndata.AnnData` table with gene expression and annotations.
tables_region
Expand Down Expand Up @@ -241,7 +244,7 @@ def write_spatial_anndata(
if label_image is not None:
# i.e. segmentation raster masks
# the function write labels will create the group labels, so we pass the root
write_labels(label_image, group=root, name=label_name)
write_labels(label_image, group=root, name=label_name, axes=label_axes)

if tables_adata is not None:
# e.g. expression table
Expand Down