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_xml.py
392 lines (354 loc) · 13.8 KB
/
generate_xml.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
from ome_types import to_xml, OME
from ome_types.model import Project, ProjectRef
from ome_types.model import Dataset, DatasetRef
from ome_types.model import Image, ImageRef, Pixels
from ome_types.model import TagAnnotation, MapAnnotation, ROI
from ome_types.model import AnnotationRef, ROIRef, Map
from ome_types.model import Point, Line, Rectangle, Ellipse, Polygon
from ome_types.model.map import M
from omero.model import TagAnnotationI, MapAnnotationI
from omero.model import PointI, LineI, RectangleI, EllipseI, PolygonI
import ezomero
import argparse
def create_proj_and_ref(**kwargs):
proj = Project(**kwargs)
proj_ref = ProjectRef(id=proj.id)
return proj, proj_ref
def create_dataset_and_ref(**kwargs):
ds = Dataset(**kwargs)
ds_ref = DatasetRef(id=ds.id)
return ds, ds_ref
def create_pixels(obj):
# we're assuming a single Pixels object per image
pix_obj = obj.getPrimaryPixels()
pixels = Pixels(
id=obj.getId(),
dimension_order=pix_obj.getDimensionOrder().getValue(),
size_c=pix_obj.getSizeC(),
size_t=pix_obj.getSizeT(),
size_x=pix_obj.getSizeX(),
size_y=pix_obj.getSizeY(),
size_z=pix_obj.getSizeZ(),
type=pix_obj.getPixelsType().getValue(),
metadata_only=True)
return pixels
def create_image_and_ref(**kwargs):
img = Image(**kwargs)
img_ref = ImageRef(id=img.id)
return img, img_ref
def create_tag_and_ref(**kwargs):
tag = TagAnnotation(**kwargs)
tagref = AnnotationRef(id=tag.id)
return tag, tagref
def create_kv_and_ref(**kwargs):
kv = MapAnnotation(**kwargs)
kvref = AnnotationRef(id=kv.id)
return kv, kvref
def create_roi_and_ref(**kwargs):
roi = ROI(**kwargs)
roiref = ROIRef(id=roi.id)
return roi, roiref
def create_point(shape):
args = {'id': shape.getId().val, 'x': shape.getX().val,
'y': shape.getY().val}
args['text'] = ''
args['the_c'] = 0
args['the_z'] = 0
args['the_t'] = 0
if shape.getTextValue() is not None:
args['text'] = shape.getTextValue().val
if shape.getTheC() is not None:
args['the_c'] = max(shape.getTheC().val, 0)
if shape.getTheZ() is not None:
args['the_z'] = shape.getTheZ().val
if shape.getTheT() is not None:
args['the_t'] = shape.getTheT().val
if shape.getFillColor() is not None:
args['fill_color'] = shape.getFillColor().val
if shape.getLocked() is not None:
args['locked'] = shape.getLocked().val
if shape.getStrokeColor() is not None:
args['stroke_color'] = shape.getStrokeColor().val
pt = Point(**args)
return pt
def create_line(shape):
args = {'id': shape.getId().val, 'x1': shape.getX1().val,
'y1': shape.getY1().val, 'x2': shape.getX2().val,
'y2': shape.getY2().val}
args['text'] = ''
args['the_c'] = 0
args['the_z'] = 0
args['the_t'] = 0
if shape.getTextValue() is not None:
args['text'] = shape.getTextValue().val
if shape.getTheC() is not None:
args['the_c'] = max(shape.getTheC().val, 0)
if shape.getTheZ() is not None:
args['the_z'] = shape.getTheZ().val
if shape.getTheT() is not None:
args['the_t'] = shape.getTheT().val
if shape.getFillColor() is not None:
args['fill_color'] = shape.getFillColor().val
if shape.getLocked() is not None:
args['locked'] = shape.getLocked().val
if shape.getStrokeColor() is not None:
args['stroke_color'] = shape.getStrokeColor().val
ln = Line(**args)
return ln
def create_rectangle(shape):
args = {'id': shape.getId().val, 'x': shape.getX().val,
'y': shape.getY().val, 'height': shape.getHeight().val,
'width': shape.getWidth().val}
args['text'] = ''
args['the_c'] = 0
args['the_z'] = 0
args['the_t'] = 0
if shape.getTextValue() is not None:
args['text'] = shape.getTextValue().val
if shape.getTheC() is not None:
args['the_c'] = max(shape.getTheC().val, 0)
if shape.getTheZ() is not None:
args['the_z'] = shape.getTheZ().val
if shape.getTheT() is not None:
args['the_t'] = shape.getTheT().val
if shape.getFillColor() is not None:
args['fill_color'] = shape.getFillColor().val
if shape.getLocked() is not None:
args['locked'] = shape.getLocked().val
if shape.getStrokeColor() is not None:
args['stroke_color'] = shape.getStrokeColor().val
rec = Rectangle(**args)
return rec
def create_ellipse(shape):
args = {'id': shape.getId().val, 'x': shape.getX().val,
'y': shape.getY().val, 'radius_x': shape.getRadiusX().val,
'radius_y': shape.getRadiusY().val}
args['text'] = ''
args['the_c'] = 0
args['the_z'] = 0
args['the_t'] = 0
if shape.getTextValue() is not None:
args['text'] = shape.getTextValue().val
if shape.getTheC() is not None:
args['the_c'] = max(shape.getTheC().val, 0)
if shape.getTheZ() is not None:
args['the_z'] = shape.getTheZ().val
if shape.getTheT() is not None:
args['the_t'] = shape.getTheT().val
if shape.getFillColor() is not None:
args['fill_color'] = shape.getFillColor().val
if shape.getLocked() is not None:
args['locked'] = shape.getLocked().val
if shape.getStrokeColor() is not None:
args['stroke_color'] = shape.getStrokeColor().val
ell = Ellipse(**args)
return ell
def create_polygon(shape):
args = {'id': shape.getId().val, 'points': shape.getPoints().val}
args['text'] = ''
args['the_c'] = 0
args['the_z'] = 0
args['the_t'] = 0
if shape.getTextValue() is not None:
args['text'] = shape.getTextValue().val
if shape.getTheC() is not None:
args['the_c'] = max(shape.getTheC().val, 0)
if shape.getTheZ() is not None:
args['the_z'] = shape.getTheZ().val
if shape.getTheT() is not None:
args['the_t'] = shape.getTheT().val
if shape.getFillColor() is not None:
args['fill_color'] = shape.getFillColor().val
if shape.getLocked() is not None:
args['locked'] = shape.getLocked().val
if shape.getStrokeColor() is not None:
args['stroke_color'] = shape.getStrokeColor().val
pol = Polygon(**args)
return pol
def create_shapes(roi):
shapes = []
for s in roi.iterateShapes():
if isinstance(s, PointI):
p = create_point(s)
shapes.append(p)
if isinstance(s, LineI):
line = create_line(s)
shapes.append(line)
if isinstance(s, RectangleI):
r = create_rectangle(s)
shapes.append(r)
if isinstance(s, EllipseI):
e = create_ellipse(s)
shapes.append(e)
if isinstance(s, PolygonI):
pol = create_polygon(s)
shapes.append(pol)
else:
continue
return shapes
def populate_roi(obj, roi_obj, ome, conn):
id = obj.getId().getValue()
name = obj.getName()
if name is not None:
name = name.getValue()
desc = obj.getDescription()
if desc is not None:
desc = desc.getValue()
shapes = create_shapes(obj)
roi, roi_ref = create_roi_and_ref(id=id, name=name, description=desc,
union=shapes)
for ann in roi_obj.listAnnotations():
if ann.OMERO_TYPE == TagAnnotationI:
tag, ref = create_tag_and_ref(id=ann.getId(),
value=ann.getTextValue())
if tag not in ome.structured_annotations:
ome.structured_annotations.append(tag)
roi.annotation_ref.append(ref)
if ann.OMERO_TYPE == MapAnnotationI:
mmap = [M(k=_key, value=str(_value))
for _key, _value in
ann.getMapValueAsMap().items()]
for m in mmap:
if m.value == '':
m.value = ' '
kv, ref = create_kv_and_ref(id=ann.getId(),
namespace=ann.getNs(),
value=Map(
m=mmap))
if kv not in ome.structured_annotations:
ome.structured_annotations.append(kv)
roi.annotation_ref.append(ref)
if roi not in ome.rois:
ome.rois.append(roi)
return roi_ref
def populate_image(obj, ome, conn):
id = obj.getId()
name = obj.getName()
desc = obj.getDescription()
pix = create_pixels(obj)
img, img_ref = create_image_and_ref(id=id, name=name,
description=desc, pixels=pix)
for ann in obj.listAnnotations():
if ann.OMERO_TYPE == TagAnnotationI:
tag, ref = create_tag_and_ref(id=ann.getId(),
value=ann.getTextValue())
if tag not in ome.structured_annotations:
ome.structured_annotations.append(tag)
img.annotation_ref.append(ref)
if ann.OMERO_TYPE == MapAnnotationI:
mmap = [M(k=_key, value=str(_value))
for _key, _value in
ann.getMapValueAsMap().items()]
for m in mmap:
if m.value == '':
m.value = ' '
kv, ref = create_kv_and_ref(id=ann.getId(),
namespace=ann.getNs(),
value=Map(
m=mmap))
if kv not in ome.structured_annotations:
ome.structured_annotations.append(kv)
img.annotation_ref.append(ref)
roi_service = conn.getRoiService()
rois = roi_service.findByImage(id, None).rois
for roi in rois:
roi_obj = conn.getObject('Roi', roi.getId().getValue())
roi_ref = populate_roi(roi, roi_obj, ome, conn)
img.roi_ref.append(roi_ref)
if img not in ome.images:
ome.images.append(img)
return img_ref
def populate_dataset(obj, ome, conn):
id = obj.getId()
name = obj.getName()
desc = obj.getDescription()
ds, ds_ref = create_dataset_and_ref(id=id, name=name,
description=desc)
for ann in obj.listAnnotations():
if ann.OMERO_TYPE == TagAnnotationI:
tag, ref = create_tag_and_ref(id=ann.getId(),
value=ann.getTextValue())
if tag not in ome.structured_annotations:
ome.structured_annotations.append(tag)
ds.annotation_ref.append(ref)
if ann.OMERO_TYPE == MapAnnotationI:
mmap = [M(k=_key, value=str(_value))
for _key, _value in
ann.getMapValueAsMap().items()]
for m in mmap:
if m.value == '':
m.value = ' '
kv, ref = create_kv_and_ref(id=ann.getId(),
namespace=ann.getNs(),
value=Map(
m=mmap))
if kv not in ome.structured_annotations:
ome.structured_annotations.append(kv)
ds.annotation_ref.append(ref)
for img in obj.listChildren():
img_obj = conn.getObject('Image', img.getId())
img_ref = populate_image(img_obj, ome, conn)
ds.image_ref.append(img_ref)
if ds not in ome.datasets:
ome.datasets.append(ds)
return ds_ref
def populate_project(obj, ome, conn):
id = obj.getId()
name = obj.getName()
desc = obj.getDescription()
test_proj, _ = create_proj_and_ref(id=id, name=name, description=desc)
for ann in obj.listAnnotations():
if ann.OMERO_TYPE == TagAnnotationI:
tag, ref = create_tag_and_ref(id=ann.getId(),
value=ann.getTextValue())
if tag not in ome.structured_annotations:
ome.structured_annotations.append(tag)
test_proj.annotation_ref.append(ref)
if ann.OMERO_TYPE == MapAnnotationI:
mmap = [M(k=_key, value=str(_value))
for _key, _value in
ann.getMapValueAsMap().items()]
for m in mmap:
if m.value == '':
m.value = ' '
kv, ref = create_kv_and_ref(id=ann.getId(),
namespace=ann.getNs(),
value=Map(
m=mmap))
if kv not in ome.structured_annotations:
ome.structured_annotations.append(kv)
test_proj.annotation_ref.append(ref)
for ds in obj.listChildren():
ds_obj = conn.getObject('Dataset', ds.getId())
ds_ref = populate_dataset(ds_obj, ome, conn)
test_proj.dataset_ref.append(ds_ref)
ome.projects.append(test_proj)
def populate_xml(datatype, id, filepath, conn):
ome = OME()
obj = conn.getObject(datatype, id)
if datatype == 'Project':
populate_project(obj, ome, conn)
if datatype == 'Dataset':
populate_dataset(obj, ome, conn)
if datatype == 'Image':
populate_image(obj, ome, conn)
with open(filepath, 'w') as fp:
print(to_xml(ome), file=fp)
fp.close()
return
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('datatype',
type=str,
help='Type of data (project, dataset, image)'
' to be moved')
parser.add_argument('id',
type=int,
help='ID of the data to be moved')
parser.add_argument('filepath',
type=str,
help='filepath to save xml')
args = parser.parse_args()
conn = ezomero.connect()
populate_xml(args.datatype, args.id, args.filepath, conn)
conn.close()