1
2 import omero
3 import xml.sax
4 from omero.gateway import XmlAnnotationWrapper
5
7 """
8 Returns a map of SPIM data according to the specification at http://www.ome-xml.org/wiki/SPIM/InitialSupport
9 where extra Objectives are stored in the Instrument, multiple Images are linked to the same 'spim-set' annotation, one
10 Image for each SPIM angle.
11 Extra Objective attributes, SPIM angles and Stage Positions stored in XML annotations with 3 different namespaces.
12 """
13
14 instrument = image.getInstrument()
15
16 obs = []
17 for o in instrument.getObjectives():
18 ob = []
19 ob.append( ('Model', o.model ) )
20 ob.append( ('Manufacturer', o.manufacturer ) )
21 ob.append( ('Serial Number', o.serialNumber ) )
22 ob.append( ('Nominal Magnification', o.nominalMagnification ) )
23 obs.append(ob)
24
25 images = []
26 objExtras = []
27 spimAngles = []
28 stagePositions = {}
29
30 def getLinkedImages(annId):
31 query = "select i from Image as i join i.annotationLinks as a_link join a_link.child as a where a.id='%s'" % annId
32 imgs = conn.getQueryService().findAllByQuery(query, None)
33 return [omero.gateway.ImageWrapper(conn, i) for i in imgs]
34
35
36 for ann in image.listAnnotations():
37 if isinstance(ann, XmlAnnotationWrapper):
38 xmlText = ann.textValue
39 if ann.ns == "ome-xml.org:additions:post2010-06:objective":
40 elementNames = ['ObjectiveAdditions']
41 elif ann.ns == "ome-xml.org:additions:post2010-06:spim:set":
42 elementNames = ['SpimImage']
43
44 images = getLinkedImages(ann.id)
45 else:
46 continue
47
48 handler = AnnXmlHandler(elementNames)
49 xml.sax.parseString(xmlText, handler)
50
51 if ann.ns == "ome-xml.org:additions:post2010-06:objective":
52 objExtras.extend(handler.attributes)
53 elif ann.ns == "ome-xml.org:additions:post2010-06:spim:set":
54 spimAngles.extend(handler.attributes)
55
56
57 for i in images:
58 spos = []
59 for ann in i.listAnnotations():
60 if isinstance(ann, XmlAnnotationWrapper) and ann.ns == "ome-xml.org:additions:post2010-06:spim:positions":
61 xmlText = ann.textValue
62 handler = AnnXmlHandler(['StagePosition'])
63 xml.sax.parseString(xmlText, handler)
64 spos.extend(handler.attributes)
65
66 stagePositions[i.id] = spos
67
68
69
70
71
72
73
74
75
76
77 if len(objExtras) == 0 and len(stagePositions) == 0 and len(spimAngles) == 0:
78 return None
79
80 return {'images':images, 'obs': obs, 'objExtras':objExtras, 'stagePositions':stagePositions, 'spimAngles': spimAngles}
81
82
83
85 """ Parse XML to get Objective attributes """
87 self.inElement = False
88 self.elementNames = elementNames
89 self.attributes = []
90
92 if name in self.elementNames:
93 kv = {}
94 for k, v in attributes.items():
95 kv[str(k)] = str(v)
96 self.attributes.append(kv)
97 self.inElement = True
98 self.buffer = ""
99
101 if self.inElement:
102 self.buffer += data
103
104
106 self.inElement = False
107