Skip to content

Commit 32ad453

Browse files
committed
Added logic to optionally insert collection mode and colour flags for the Atlas and GridSquare tables in ISPyB for the CLEM workflow
1 parent 08b9a0e commit 32ad453

5 files changed

Lines changed: 84 additions & 5 deletions

File tree

src/murfey/server/ispyb.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,23 @@ def do_update_atlas(
186186
atlas_image: str,
187187
pixel_size: float,
188188
slot: int | None,
189+
collection_mode: str | None = None,
190+
color_flags: dict[str, str | int] = {},
189191
):
190192
try:
191193
with ISPyBSession() as db:
192194
atlas = db.query(Atlas).filter(Atlas.atlasId == atlas_id).one()
193195
atlas.atlasImage = atlas_image or atlas.atlasImage
194196
atlas.pixelSize = pixel_size or atlas.pixelSize
195197
atlas.cassetteSlot = slot or atlas.cassetteSlot
198+
atlas.mode = collection_mode or atlas.mode
199+
# Optionally insert colour flags if present
200+
if color_flags:
201+
for (
202+
col_name,
203+
value,
204+
) in color_flags.items():
205+
setattr(atlas, col_name, value)
196206
db.add(atlas)
197207
db.commit()
198208
return {"success": True, "return_value": atlas.atlasId}
@@ -209,6 +219,7 @@ def do_insert_grid_square(
209219
atlas_id: int,
210220
grid_square_id: int,
211221
grid_square_parameters: GridSquareParameters,
222+
color_flags: dict[str, int] = {},
212223
):
213224
# most of this is for mypy
214225
if (
@@ -235,6 +246,10 @@ def do_insert_grid_square(
235246
stageLocationY=grid_square_parameters.y_stage_position,
236247
pixelSize=grid_square_parameters.pixel_size,
237248
)
249+
# Optionally insert colour flags
250+
if color_flags:
251+
for col_name, value in color_flags.items():
252+
setattr(record, col_name, value)
238253
try:
239254
with ISPyBSession() as db:
240255
db.add(record)
@@ -250,7 +265,10 @@ def do_insert_grid_square(
250265
return {"success": False, "return_value": None}
251266

252267
def do_update_grid_square(
253-
self, grid_square_id: int, grid_square_parameters: GridSquareParameters
268+
self,
269+
grid_square_id: int,
270+
grid_square_parameters: GridSquareParameters,
271+
color_flags: dict[str, int] = {},
254272
):
255273
try:
256274
with ISPyBSession() as db:
@@ -290,6 +308,12 @@ def do_update_grid_square(
290308
grid_square.stageLocationY = grid_square_parameters.y_stage_position
291309
if grid_square_parameters.pixel_size:
292310
grid_square.pixelSize = grid_square_parameters.pixel_size
311+
if grid_square_parameters.collection_mode:
312+
grid_square.mode = grid_square_parameters.collection_mode
313+
# Optionally insert colour flags
314+
if color_flags:
315+
for col_name, value in color_flags.items():
316+
setattr(grid_square, col_name, value)
293317
db.add(grid_square)
294318
db.commit()
295319
return {"success": True, "return_value": grid_square.gridSquareId}

src/murfey/util/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ class GridSquareParameters(BaseModel):
157157
pixel_size: Optional[float] = None
158158
angle: Optional[float] = None
159159

160+
# Collection mode
161+
collection_mode: Optional[str] = None
162+
160163

161164
class FoilHoleParameters(BaseModel):
162165
tag: str

src/murfey/workflows/clem/register_preprocessing_results.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import logging
1212
import re
1313
import traceback
14+
from collections.abc import Collection
1415
from importlib.metadata import entry_points
1516
from pathlib import Path
1617
from typing import Literal, Optional
@@ -186,6 +187,41 @@ def _register_clem_image_series(
186187
logger.info(f"CLEM preprocessing results registered for {result.series_name!r} ")
187188

188189

190+
color_columns = {
191+
"gray": "hasGrey",
192+
"red": "hasRed",
193+
"green": "hasGreen",
194+
"blue": "hasBlue",
195+
"cyan": "hasCyan",
196+
"magenta": "hasMagenta",
197+
"yellow": "hasYellow",
198+
}
199+
200+
201+
def _get_color_flags(
202+
colors: Collection[str] = [],
203+
):
204+
return {
205+
color_columns[color]: (1 if color in color_columns.keys() else 0)
206+
for color in colors
207+
}
208+
209+
210+
def _determine_collection_mode(
211+
colors: Collection[str] = [],
212+
):
213+
if not colors:
214+
logger.warning("No colours were present in returned result")
215+
return None
216+
if "gray" in colors:
217+
if len(colors) == 1:
218+
return "Bright Field"
219+
else:
220+
return "Bright Field and Fluorescent"
221+
else:
222+
return "Fluorescent"
223+
224+
189225
def _register_dcg_and_atlas(
190226
session_id: int,
191227
instrument_name: str,
@@ -245,6 +281,10 @@ def _register_dcg_and_atlas(
245281
"atlas": atlas_name,
246282
"atlas_pixel_size": atlas_pixel_size,
247283
"sample": dcg_entry.sample,
284+
"color_flags": _get_color_flags(result.output_files.keys()),
285+
"collection_mode": _determine_collection_mode(
286+
result.output_files.keys()
287+
),
248288
}
249289
if entry_point_result := entry_points(
250290
group="murfey.workflows", name="atlas_update"
@@ -269,6 +309,8 @@ def _register_dcg_and_atlas(
269309
"atlas": atlas_name,
270310
"atlas_pixel_size": atlas_pixel_size,
271311
"sample": None,
312+
"color_flags": _get_color_flags(result.output_files.keys()),
313+
"collection_mode": _determine_collection_mode(result.output_files.keys()),
272314
}
273315
if entry_point_result := entry_points(
274316
group="murfey.workflows", name="data_collection_group"
@@ -410,6 +452,7 @@ def _register_grid_square(
410452
y_stage_position=0.5 * (clem_img_series.y0 + clem_img_series.y1),
411453
pixel_size=clem_img_series.image_pixel_size,
412454
image=clem_img_series.thumbnail_search_string,
455+
collection_mode=_determine_collection_mode(result.output_files.keys()),
413456
)
414457
# Register or update the grid square entry as required
415458
if grid_square_result := murfey_db.exec(

src/murfey/workflows/register_atlas_update.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ def run(
1919
logger.info(f"Registering updated atlas: \n{message}")
2020

2121
_transport_object.do_update_atlas(
22-
message["atlas_id"],
23-
message["atlas"],
24-
message["atlas_pixel_size"],
25-
message["sample"],
22+
atlas_id=message["atlas_id"],
23+
atlas_image=message["atlas"],
24+
pixel_size=message["atlas_pixel_size"],
25+
slot=message["sample"],
26+
# Extract optional parameters
27+
collection_mode=message.get("collection_mode"),
28+
color_flags=message.get("color_flags", {}),
2629
)
2730
if dcg_hooks := entry_points(group="murfey.hooks", name="data_collection_group"):
2831
try:

src/murfey/workflows/register_data_collection_group.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ def run(message: dict, murfey_db: SQLModelSession) -> dict[str, bool]:
6767
pixelSize=message.get("atlas_pixel_size", 0),
6868
cassetteSlot=message.get("sample"),
6969
)
70+
# Optionally set the collection mode and color flags
71+
if collection_mode := message.get("collection_mode"):
72+
atlas_record.mode = collection_mode
73+
if color_flags := message.get("color_flags", {}):
74+
for col_name, value in color_flags.items():
75+
setattr(atlas_record, col_name, value)
7076
atlas_id = _transport_object.do_insert_atlas(atlas_record).get(
7177
"return_value", None
7278
)

0 commit comments

Comments
 (0)