forked from CFGrote/ometa
-
Notifications
You must be signed in to change notification settings - Fork 3
/
04-Remove_KeyVal.py
169 lines (136 loc) · 5.74 KB
/
04-Remove_KeyVal.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
MIF/Key_Value_remove.py"
Remove all key-value pairs from:
* selected image(s)
* selected dataset(s) and the images contained in them
-----------------------------------------------------------------------------
Copyright (C) 2018
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
------------------------------------------------------------------------------
@author Christian Evenhuis
<a href="mailto:[email protected]">[email protected]</a>
@version 5.3.3
@since 5.3.3
"""
from omero.gateway import BlitzGateway
import omero
from omero.cmd import Delete2
from omero.rtypes import rlong, rstring, robject
import omero.scripts as scripts
import re
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def RemoveMapAnnotations(conn, dtype, Id ):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
obj = conn.getObject(dtype,int(Id))
name = obj.getName()
anns = list( obj.listAnnotations())
mapann_ids = [ann.id for ann in anns
if isinstance(ann, omero.gateway.MapAnnotationWrapper) ]
print(mapann_ids )
try:
delete = Delete2(targetObjects={'MapAnnotation': mapann_ids})
handle = conn.c.sf.submit(delete)
conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True,
failontimeout=False, closehandle=False)
return 0
except Exception as ex:
print("Failed to delete links: {} ".format(ex.message) )
return 1
return
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def getObjects(conn, scriptParams):
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"""
File the list of objects
@param conn: Blitz Gateway connection wrapper
@param scriptParams: A map of the input parameters
"""
# we know scriptParams will have "Data_Type" and "IDs" since these
# parameters are not optional
dataType = scriptParams["Data_Type"]
ids = scriptParams["IDs"]
# dataType is 'Dataset' or 'Image' so we can use it directly in
# getObjects()
objs = list(conn.getObjects(dataType, ids)) # generator of images or datasets
if len(objs) == 0:
print("No {} found for specified IDs".format(dataType) )
return
objs_ret = []
if dataType == 'Dataset':
for ds in objs:
print("Processing Images from Dataset: {}".format(ds.getName()) )
objs_ret.append( ds )
imgs = list(ds.listChildren())
objs_ret.extend(imgs)
else:
print("Processing Images identified by ID")
objs_ret= objs
return objs_ret
if __name__ == "__main__":
"""
The main entry point of the script, as called by the client via the
scripting service, passing the required parameters.
"""
dataTypes = [rstring('Dataset'),rstring('Image')] # only works on datasets
# Here we define the script name and description.
# Good practice to put url here to give users more guidance on how to run
# your script.
client = scripts.client(
'Remove_Key_Value.py',
("Remove key-value pairs from"
" Image IDs or by the Dataset IDs.\nSee"
" http://www.openmicroscopy.org/site/support/omero5.2/developers/"
"scripts/user-guide.html for the tutorial that uses this script."),
scripts.String(
"Data_Type", optional=False, grouping="1",
description="The data you want to work with.", values=dataTypes,
default="Dataset"),
scripts.List(
"IDs", optional=False, grouping="2",
description="List of Dataset IDs or Image IDs").ofType(rlong(0)),
scripts.String(
"New_Description", grouping="3",
description="The new description to set for each Image in the"
" Dataset"),
version="5.3.3",
authors=["Christian Evenhuis", "MIF"],
institutions=["University of Technology Sydney"],
contact="[email protected]"
)
try:
scriptParams = {}
for key in client.getInputKeys():
if client.getInput(key):
# unwrap rtypes to String, Integer etc
scriptParams[key] = client.getInput(key, unwrap=True)
print(scriptParams) # handy to have inputs in the std-out log
# wrap client to use the Blitz Gateway
conn = BlitzGateway(client_obj=client)
## do the editing...
objs = getObjects(conn, scriptParams)
nfailed = 0
for obj in objs:
print("Processing : {}".format(obj.getName() ))
ret = RemoveMapAnnotations( conn, obj.OMERO_CLASS, obj.getId() )
nfailed = nfailed + ret
## now handle the result, displaying message and returning image if
## appropriate
nobjs = len(objs)
message = "Key value data deleted from {} of {} files".format( nobjs-nfailed, nobjs)
client.setOutput("Message", rstring(message))
except:
pass
finally:
client.closeSession()