-
Notifications
You must be signed in to change notification settings - Fork 0
/
original_metadata_server.py
111 lines (93 loc) · 3.62 KB
/
original_metadata_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This a basic OMERO script that runs server-side and
turns original metadata into map annotations
"""
# Import
import omero
import omero.scripts as scripts
from omero.gateway import BlitzGateway
from omero.rtypes import robject, rstring
# load
def original_metadata_to_map_ann(conn, dataset_id, attributes=None):
"""
Load the images in the specified dataset
:param conn: The BlitzGateway
:param dataset_id: The dataset's id
:return: The Images or None
"""
dataset = conn.getObject("Dataset", dataset_id)
images = []
if dataset is None:
return None
for image in dataset.listChildren():
images.append(image)
if len(images) == 0:
return None
for image in images:
print("---- Processing image", image.id)
image = conn.getObject("Image", image.id)
map_ann_data = []
# Pick these items of metadata -> map annotation
if attributes is None:
attributes = ('Information|Document|Comment', 'Information|Document|Description')
# Load the 'Original Metadata' for the image
# ==========================================
#if not image.listAnnotations():
om = image.loadOriginalMetadata()
if om is not None:
key_values = om[1] + om[2]
for key_value in key_values:
if len(key_value) > 1 and key_value[0] in attributes:
map_ann_data.append([key_value[0], str(key_value[1])])
# Create a 'map' annotation (list of key: value pairs)
# ====================================================
map_ann = omero.gateway.MapAnnotationWrapper(conn)
map_ann.setNs('original.metadata.from.script')
map_ann.setValue(map_ann_data)
map_ann.save()
image.linkAnnotation(map_ann)
return images
# main
if __name__ == "__main__":
# Start declaration
# Define the script name and description, and a single 'required' parameter
client = scripts.client(
'Original_Metadata_Server_Side.py',
"""
This script does connect to OMERO.
""",
scripts.Long("datasetId", optional=False,grouping='1'),
scripts.String("Tags", grouping="2.1",
description="Optional parameter, contains the names of the key-value pairs, returns all map annotation if left empty, values should be separated with commas"),
authors=["OME Team", "OME Team"],
institutions=["University of Dundee"],
contact="ome-users@lists.openmicroscopy.org.uk",
)
# Start script
try:
# process the list of arguments
script_params = {}
for key in client.getInputKeys():
if client.getInput(key):
script_params[key] = client.getInput(key, unwrap=True)
dataset_id = script_params["datasetId"]
attributes = script_params["Tags"]
attributes = attributes.split(", ")
attributes = tuple(i for i in attributes)
# wrap client to use the Blitz Gateway if len(key_value) > 1 and key_value[0] in attributes:
conn = BlitzGateway(client_obj=client)
# load the images
images = original_metadata_to_map_ann(conn, dataset_id, attributes)
# return output to the user
if images is None:
message = "No images found"
else:
message = "Returned %s images" % len(images)
# return first image:
client.setOutput("Image", robject(images[0]._obj))
client.setOutput("Message", rstring(message))
# end output
finally:
client.closeSession()