15
15
import os
16
16
from glob import glob
17
17
18
+ import pandas as pd
18
19
import zarr
20
+ from anndata .experimental import write_elem
19
21
20
22
from fractal .tasks .lib_parse_filename_metadata import parse_metadata
23
+ from fractal .tasks .lib_regions_of_interest import prepare_ROIs_table
21
24
from fractal .tasks .metadata_parsing import parse_yokogawa_metadata
22
25
23
26
@@ -28,6 +31,7 @@ def create_zarr_structure(
28
31
path_dict_channels = None ,
29
32
num_levels = None ,
30
33
coarsening_xy = None ,
34
+ metadata_table = "mlf_mrf" ,
31
35
):
32
36
33
37
"""
@@ -64,6 +68,21 @@ def create_zarr_structure(
64
68
"ERROR in create_zarr_structure_multifov: in_paths is None"
65
69
)
66
70
71
+ # Preliminary checks on metadata_table
72
+ if metadata_table != "mlf_mrf" and not isinstance (
73
+ metadata_table , pd .core .frame .DataFrame
74
+ ):
75
+ raise Exception (
76
+ "ERROR: metadata_table must be a known string or a "
77
+ "pandas DataFrame}"
78
+ )
79
+ if metadata_table != "mlf_mrf" :
80
+ raise NotImplementedError (
81
+ "We currently only support "
82
+ 'metadata_table="mlf_mrf", '
83
+ f"and not { metadata_table } "
84
+ )
85
+
67
86
# Identify all plates and all channels, across all input folders
68
87
plates = []
69
88
channels = None
@@ -116,6 +135,9 @@ def create_zarr_structure(
116
135
else :
117
136
plates .append (plate )
118
137
138
+ # Update dict_plate_paths
139
+ dict_plate_paths [plate ] = in_path
140
+
119
141
# Check that channels are the same as in previous plates
120
142
if channels is None :
121
143
channels = tmp_channels [:]
@@ -125,9 +147,6 @@ def create_zarr_structure(
125
147
f"ERROR\n { info } \n ERROR: expected channels " "{channels}"
126
148
)
127
149
128
- # Update dict_plate_paths
129
- dict_plate_paths [plate ] = in_path
130
-
131
150
# Check that all channels are in the allowed_channels
132
151
if not set (channels ).issubset (set (dict_channels .keys ())):
133
152
msg = "ERROR in create_zarr_structure\n "
@@ -143,36 +162,42 @@ def create_zarr_structure(
143
162
print (f"actual_channels: { actual_channels } " )
144
163
145
164
zarrurls = {"plate" : [], "well" : []}
146
- # zarrurls_in_paths = {}
147
-
148
- # PARSE METADATA
149
- # FIXME: hard-coded paths
150
- root = (
151
- "/data/active/fractal/3D/PelkmansLab/"
152
- "CardiacMultiplexing/Cycle1_testSubset/"
153
- )
154
- mrf_path = root + "MeasurementDetail.mrf"
155
- mlf_path = root + "MeasurementData.mlf"
156
-
157
- site_metadata , total_files = parse_yokogawa_metadata (
158
- mrf_path = mrf_path , mlf_path = mlf_path
159
- )
160
-
161
- # PIXEL SIZES
162
- pixel_size_z = site_metadata ["pixel_size_z" ][0 ]
163
- pixel_size_y = site_metadata ["pixel_size_y" ][0 ]
164
- pixel_size_x = site_metadata ["pixel_size_x" ][0 ]
165
165
166
+ # Sanitize out_path
166
167
if not out_path .endswith ("/" ):
167
168
out_path += "/"
169
+
170
+ # Loop over plates
168
171
for plate in plates :
169
172
173
+ # Retrieve path corresponding to this plate
174
+ in_path = dict_plate_paths [plate ]
175
+
170
176
# Define plate zarr
171
177
zarrurl = f"{ out_path } { plate } .zarr"
172
178
print (f"Creating { zarrurl } " )
173
179
group_plate = zarr .group (zarrurl )
174
180
zarrurls ["plate" ].append (zarrurl )
175
- # zarrurls_in_paths[zarrurl] = dict_plate_paths[plate]
181
+
182
+ # Obtain FOV-metadata dataframe
183
+ if metadata_table == "mlf_mrf" :
184
+ mrf_path = f"{ in_path } MeasurementDetail.mrf"
185
+ mlf_path = f"{ in_path } MeasurementData.mlf"
186
+ site_metadata , total_files = parse_yokogawa_metadata (
187
+ mrf_path = mrf_path , mlf_path = mlf_path
188
+ )
189
+ # FIXME: hardcoded
190
+ image_size = {"x" : 2560 , "y" : 2160 }
191
+
192
+ # Extract pixel sizes
193
+ pixel_size_z = site_metadata ["pixel_size_z" ][0 ]
194
+ pixel_size_y = site_metadata ["pixel_size_y" ][0 ]
195
+ pixel_size_x = site_metadata ["pixel_size_x" ][0 ]
196
+
197
+ # Extract bit_depth #FIXME
198
+ # bit_depth = site_metadata["bit_depth"][0]
199
+ # if bit_depth == 8:
200
+ # dtype
176
201
177
202
# Identify all wells
178
203
plate_prefix = dict_plate_prefixes [plate ]
@@ -291,7 +316,7 @@ def create_zarr_structure(
291
316
}
292
317
for ind_level in range (num_levels )
293
318
],
294
- # Global rescaling to physiacl units
319
+ # Global rescaling to physical units
295
320
"coordinateTransformations" : [
296
321
{
297
322
"type" : "scale" ,
@@ -328,6 +353,13 @@ def create_zarr_structure(
328
353
],
329
354
}
330
355
356
+ # Prepare and write anndata table of FOV ROIs
357
+ FOV_ROIs_table = prepare_ROIs_table (
358
+ site_metadata .loc [f"{ row + column } " ], image_size = image_size
359
+ )
360
+ group_tables = group_field .create_group ("tables/" ) # noqa: F841
361
+ write_elem (group_tables , "FOV_ROI_table" , FOV_ROIs_table )
362
+
331
363
return zarrurls , actual_channels
332
364
333
365
@@ -375,4 +407,5 @@ def create_zarr_structure(
375
407
num_levels = args .num_levels ,
376
408
coarsening_xy = args .coarsening_xy ,
377
409
path_dict_channels = args .path_dict_channels ,
410
+ # metadata_table=args.metadata_table, #FIXME
378
411
)
0 commit comments