This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_omero_objects.py
211 lines (189 loc) · 7.66 KB
/
generate_omero_objects.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import ezomero
import argparse
from omero.model import DatasetI
from omero.gateway import DatasetWrapper
from ome_types import from_xml
from ome_types.model import TagAnnotation, MapAnnotation
from ome_types.model import Line, Point, Rectangle, Ellipse, Polygon, Polyline
from omero.gateway import TagAnnotationWrapper, MapAnnotationWrapper
from ezomero import rois
def create_projects(pjs, conn):
pj_map = {}
for pj in pjs:
pj_id = ezomero.post_project(conn, pj.name, pj.description)
pj_map[pj.id] = pj_id
return pj_map
def create_datasets(dss, conn):
"""
Currently doing it the non-ezomero way because ezomero always
puts "orphan" Datasets in the user's default group
"""
ds_map = {}
for ds in dss:
dataset = DatasetWrapper(conn, DatasetI())
dataset.setName(ds.name)
if ds.description is not None:
dataset.setDescription(ds.description)
dataset.save()
ds_id = dataset.getId()
ds_map[ds.id] = ds_id
return ds_map
def create_annotations(ans, conn):
ann_map = {}
for an in ans:
if isinstance(an, TagAnnotation):
tag_ann = TagAnnotationWrapper(conn)
tag_ann.setValue(an.value)
tag_ann.setDescription(an.description)
tag_ann.save()
ann_map[an.id] = tag_ann.getId()
elif isinstance(an, MapAnnotation):
map_ann = MapAnnotationWrapper(conn)
namespace = an.namespace
map_ann.setNs(namespace)
key_value_data = []
for v in an.value.m:
key_value_data.append([v.k, v.value])
map_ann.setValue(key_value_data)
map_ann.save()
ann_map[an.id] = map_ann.getId()
return ann_map
def create_shapes(roi):
shapes = []
for shape in roi.union:
if isinstance(shape, Point):
sh = rois.Point(shape.x, shape.y, z=shape.the_z, c=shape.the_c,
t=shape.the_t, label=shape.text)
elif isinstance(shape, Line):
sh = rois.Line(shape.x1, shape.y1, shape.x2, shape.y2,
z=shape.the_z, c=shape.the_c, t=shape.the_t,
label=shape.text)
elif isinstance(shape, Rectangle):
sh = rois.Rectangle(shape.x, shape.y, shape.width, shape.height,
z=shape.the_z, c=shape.the_c, t=shape.the_t,
label=shape.text)
elif isinstance(shape, Ellipse):
sh = rois.Ellipse(shape.x, shape.y, shape.radius_x, shape.radius_y,
z=shape.the_z, c=shape.the_c, t=shape.the_t,
label=shape.text)
elif isinstance(shape, Polygon) or isinstance(shape, Polyline):
points = []
for pt in shape.points.split(" "):
# points sometimes come with a comma at the end...
pt = pt.rstrip(",")
points.append(tuple(float(x) for x in pt.split(",")))
sh = rois.Polygon(points, z=shape.the_z, c=shape.the_c,
t=shape.the_t, label=shape.text)
else:
continue
shapes.append(sh)
return shapes
def _int_to_rgba(omero_val):
""" Helper function returning the color as an Integer in RGBA encoding """
if omero_val < 0:
omero_val = omero_val + (2**32)
r = omero_val >> 24
g = omero_val - (r << 24) >> 16
b = omero_val - (r << 24) - (g << 16) >> 8
a = omero_val - (r << 24) - (g << 16) - (b << 8)
a = a / 256.0
return (r, g, b, a)
def create_rois(rois, imgs, img_map, conn):
for img in imgs:
for roiref in img.roi_ref:
roi = next(filter(lambda x: x.id == roiref.id, rois))
shapes = create_shapes(roi)
img_id_dest = img_map[img.id]
# using colors for the first shape
fill_color = _int_to_rgba(int(roi.union[0].fill_color))
stroke_color = _int_to_rgba(int(roi.union[0].stroke_color))
ezomero.post_roi(conn, img_id_dest, shapes, name=roi.name,
description=roi.description,
fill_color=fill_color, stroke_color=stroke_color)
return
def link_datasets(ome, proj_map, ds_map, conn):
for proj in ome.projects:
proj_id = proj_map[proj.id]
ds_ids = []
for ds in proj.dataset_ref:
ds_id = ds_map[ds.id]
ds_ids.append(ds_id)
ezomero.link_datasets_to_project(conn, ds_ids, proj_id)
return
def link_images(ome, ds_map, img_map, conn):
for ds in ome.datasets:
ds_id = ds_map[ds.id]
img_ids = []
for img in ds.image_ref:
img_id = img_map[img.id]
img_ids.append(img_id)
ezomero.link_images_to_dataset(conn, img_ids, ds_id)
return
def link_annotations(ome, proj_map, ds_map, img_map, ann_map, conn):
for proj in ome.projects:
proj_id = proj_map[proj.id]
proj_obj = conn.getObject("Project", proj_id)
anns = ome.structured_annotations
for annref in proj.annotation_ref:
ann = next(filter(lambda x: x.id == annref.id, anns))
ann_id = ann_map[ann.id]
if isinstance(ann, TagAnnotation):
ann_obj = conn.getObject("TagAnnotation", ann_id)
elif isinstance(ann, MapAnnotation):
ann_obj = conn.getObject("MapAnnotation", ann_id)
else:
continue
proj_obj.linkAnnotation(ann_obj)
for ds in ome.datasets:
ds_id = ds_map[ds.id]
ds_obj = conn.getObject("Dataset", ds_id)
anns = ome.structured_annotations
for annref in ds.annotation_ref:
ann = next(filter(lambda x: x.id == annref.id, anns))
ann_id = ann_map[ann.id]
if isinstance(ann, TagAnnotation):
ann_obj = conn.getObject("TagAnnotation", ann_id)
elif isinstance(ann, MapAnnotation):
ann_obj = conn.getObject("MapAnnotation", ann_id)
else:
continue
ds_obj.linkAnnotation(ann_obj)
for img in ome.images:
img_id = img_map[img.id]
img_obj = conn.getObject("Image", img_id)
anns = ome.structured_annotations
for annref in img.annotation_ref:
ann = next(filter(lambda x: x.id == annref.id, anns))
ann_id = ann_map[ann.id]
if isinstance(ann, TagAnnotation):
ann_obj = conn.getObject("TagAnnotation", ann_id)
elif isinstance(ann, MapAnnotation):
ann_obj = conn.getObject("MapAnnotation", ann_id)
else:
continue
img_obj.linkAnnotation(ann_obj)
return
def populate_omero(fp, img_map, conn):
ome = from_xml(fp)
proj_map = create_projects(ome.projects, conn)
print(proj_map)
ds_map = create_datasets(ome.datasets, conn)
print(ds_map)
ann_map = create_annotations(ome.structured_annotations, conn)
print(ann_map)
create_rois(ome.rois, ome.images, img_map, conn)
link_datasets(ome, proj_map, ds_map, conn)
link_images(ome, ds_map, img_map, conn)
link_annotations(ome, proj_map, ds_map, img_map, ann_map, conn)
conn.close()
return
if __name__ == "__main__":
conn = ezomero.connect('root', 'omero', host='localhost',
port=6064, group='system', secure=True)
parser = argparse.ArgumentParser()
parser.add_argument('filepath',
type=str,
help='filepath to load xml')
args = parser.parse_args()
image_map = {"Image:51": 1405, "Image:52": 1406, "Image:27423": 1404}
populate_omero(args.filepath, image_map, conn)