comparison idr_download_by_ids.py @ 3:381f248febba draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/idr_download commit 0ae8913ec1c14caedc9836d4889650ea836a1d70"
author iuc
date Wed, 22 Apr 2020 11:43:18 -0400
parents 17b1cd0f4812
children 11036f6197d6
comparison
equal deleted inserted replaced
2:17b1cd0f4812 3:381f248febba
5 from libtiff import TIFF 5 from libtiff import TIFF
6 from omero.gateway import BlitzGateway # noqa 6 from omero.gateway import BlitzGateway # noqa
7 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa 7 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa
8 8
9 9
10 def warn(message, image_identifier): 10 def warn(message, image_identifier, warn_skip=False):
11 message = message.rstrip()
12 if warn_skip:
13 if message[-1] in ['.', '!', '?']:
14 skip_msg = ' Skipping download!'
15 else:
16 skip_msg = '. Skipping download!'
17 else:
18 skip_msg = ''
11 print( 19 print(
12 'ImageSpecWarning for {0}: {1}' 20 'ImageSpecWarning for {0}: {1}{2}'
13 .format(image_identifier, message), 21 .format(
22 image_identifier,
23 message,
24 skip_msg
25 ),
14 file=sys.stderr 26 file=sys.stderr
15 ) 27 )
16 28
17 29
18 def find_channel_index(image, channel_name): 30 def find_channel_index(image, channel_name):
91 return 103 return
92 104
93 if fname[-5:] != '.tiff': 105 if fname[-5:] != '.tiff':
94 fname += '.tiff' 106 fname += '.tiff'
95 try: 107 try:
108 fname = fname.replace(' ', '_')
96 tiff = TIFF.open(fname, mode='w') 109 tiff = TIFF.open(fname, mode='w')
97 tiff.write_image(selection) 110 tiff.write_image(selection)
98 finally: 111 finally:
99 tiff.close() 112 tiff.close()
100 113
103 image_ids, 116 image_ids,
104 channel=None, z_stack=0, frame=0, 117 channel=None, z_stack=0, frame=0,
105 coord=(0, 0), width=0, height=0, region_spec='rectangle', 118 coord=(0, 0), width=0, height=0, region_spec='rectangle',
106 skip_failed=False 119 skip_failed=False
107 ): 120 ):
121 # basic argument sanity checks and adjustments
122 prefix = 'image-'
123 # normalize image ids by stripping off prefix if it exists
124 image_ids = [
125 iid[len(prefix):] if iid[:len(prefix)] == prefix else iid
126 for iid in image_ids
127 ]
128
129 if region_spec not in ['rectangle', 'center']:
130 raise ValueError(
131 'Got unknown value "{0}" as region_spec argument'
132 .format(region_spec)
133 )
108 134
109 # connect to idr 135 # connect to idr
110 conn = BlitzGateway('public', 'public', 136 conn = BlitzGateway('public', 'public',
111 host='idr.openmicroscopy.org', 137 host='idr.openmicroscopy.org',
112 secure=True) 138 secure=True)
113 conn.connect() 139 conn.connect()
114 140
115 try: 141 try:
116 prefix = 'image-'
117 for image_id in image_ids: 142 for image_id in image_ids:
118 if image_id[:len(prefix)] == prefix: 143 image_warning_id = 'Image-ID: {0}'.format(image_id)
119 image_id = image_id[len(prefix):] 144 try:
120 image_id = int(image_id) 145 image_id = int(image_id)
121 image = conn.getObject("Image", image_id) 146 except ValueError:
147 image = None
148 else:
149 try:
150 image = conn.getObject("Image", image_id)
151 except Exception as e:
152 # respect skip_failed on unexpected errors
153 if skip_failed:
154 warn(str(e), image_warning_id, warn_skip=True)
155 continue
156 else:
157 raise
122 158
123 if image is None: 159 if image is None:
124 image_warning_id = 'Image-ID: {0}'.format(image_id)
125 if skip_failed: 160 if skip_failed:
126 warn( 161 warn(
127 'Unable to find an image with this ID in the ' 162 'Unable to find an image with this ID in the '
128 'database. Skipping download!', 163 'database.',
129 image_warning_id 164 image_warning_id,
165 warn_skip=True
130 ) 166 )
131 continue 167 continue
132 raise ValueError( 168 raise ValueError(
133 '{0}: Unable to find an image with this ID in the ' 169 '{0}: Unable to find an image with this ID in the '
134 'database. Aborting!' 170 'database. Aborting!'
135 .format(image_warning_id) 171 .format(image_warning_id)
136 ) 172 )
137 173
138 image_name = os.path.splitext(image.getName())[0] 174 try:
139 image_warning_id = '{0} (ID: {1})'.format( 175 # try to extract image properties
140 image_name, image_id 176 # if anything goes wrong here skip the image
141 ) 177 # or abort.
142 178 image_name = os.path.splitext(image.getName())[0]
143 if region_spec == 'rectangle': 179 image_warning_id = '{0} (ID: {1})'.format(
144 tile = get_clipping_region(image, *coord, width, height) 180 image_name, image_id
145 elif region_spec == 'center': 181 )
146 tile = get_clipping_region( 182
147 image, 183 if region_spec == 'rectangle':
148 *_center_to_ul(*coord, width, height) 184 tile = get_clipping_region(image, *coord, width, height)
149 ) 185 elif region_spec == 'center':
150 else: 186 tile = get_clipping_region(
151 raise ValueError( 187 image,
152 'Got unknown value "{0}" as region_spec argument' 188 *_center_to_ul(*coord, width, height)
153 .format(region_spec) 189 )
154 ) 190
191 ori_z, z_stack = z_stack, confine_plane(image, z_stack)
192 ori_frame, frame = frame, confine_frame(image, frame)
193 num_channels = image.getSizeC()
194 if channel is None:
195 channel_index = 0
196 else:
197 channel_index = find_channel_index(image, channel)
198 except Exception as e:
199 # respect skip_failed on unexpected errors
200 if skip_failed:
201 warn(str(e), image_warning_id, warn_skip=True)
202 continue
203 else:
204 raise
205
206 # region sanity checks and warnings
155 if tile[2] < width or tile[3] < height: 207 if tile[2] < width or tile[3] < height:
156 # The downloaded image region will have smaller dimensions 208 # The downloaded image region will have smaller dimensions
157 # than the specified width x height. 209 # than the specified width x height.
158 warn( 210 warn(
159 'Downloaded image dimensions ({0} x {1}) will be smaller ' 211 'Downloaded image dimensions ({0} x {1}) will be smaller '
160 'than the specified width and height ({2} x {3}).' 212 'than the specified width and height ({2} x {3}).'
161 .format(tile[2], tile[3], width, height), 213 .format(tile[2], tile[3], width, height),
162 image_warning_id 214 image_warning_id
163 ) 215 )
164 216
165 ori_z, z_stack = z_stack, confine_plane(image, z_stack) 217 # z-stack sanity checks and warnings
166 if z_stack != ori_z: 218 if z_stack != ori_z:
167 warn( 219 warn(
168 'Specified image plane ({0}) is out of bounds. Using {1} ' 220 'Specified image plane ({0}) is out of bounds. Using {1} '
169 'instead.' 221 'instead.'
170 .format(ori_z, z_stack), 222 .format(ori_z, z_stack),
171 image_warning_id 223 image_warning_id
172 ) 224 )
173 225
174 ori_frame, frame = frame, confine_frame(image, frame) 226 # frame sanity checks and warnings
175 if frame != ori_frame: 227 if frame != ori_frame:
176 warn( 228 warn(
177 'Specified image frame ({0}) is out of bounds. Using ' 229 'Specified image frame ({0}) is out of bounds. Using '
178 'frame {1} instead.' 230 'frame {1} instead.'
179 .format(ori_frame, frame), 231 .format(ori_frame, frame),
180 image_warning_id 232 image_warning_id
181 ) 233 )
182 # Get the channel index. If the index is not valid, skip the image 234
235 # channel index sanity checks and warnings
183 if channel is None: 236 if channel is None:
184 channel_index = 0
185 num_channels = image.getSizeC()
186 if num_channels > 1: 237 if num_channels > 1:
187 warn( 238 warn(
188 'No specific channel selected for multi-channel ' 239 'No specific channel selected for multi-channel '
189 'image. Using first of {0} channels.' 240 'image. Using first of {0} channels.'
190 .format(num_channels), 241 .format(num_channels),
191 image_warning_id 242 image_warning_id
192 ) 243 )
193 else: 244 else:
194 channel_index = find_channel_index(image, channel) 245 if channel_index == -1 or channel_index >= num_channels:
195 if channel_index == -1 or channel_index >= image.getSizeC(): 246 if skip_failed:
196 raise ValueError( 247 warn(
197 '"{0}" is not a known channel name for image {1}' 248 str(channel)
198 .format(channel, image.getName()) 249 + ' is not a known channel name for this image.',
199 ) 250 image_warning_id,
251 warn_skip=True
252 )
253 continue
254 else:
255 raise ValueError(
256 '"{0}" is not a known channel name for image {1}. '
257 'Aborting!'
258 .format(channel, image_warning_id)
259 )
200 260
201 # download and save the region as TIFF 261 # download and save the region as TIFF
202 fname = '__'.join( 262 fname = '__'.join(
203 [image_name, str(image_id)] + [str(x) for x in tile] 263 [image_name, str(image_id)] + [str(x) for x in tile]
204 ) 264 )
205 download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname) 265 try:
266 download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname)
267 except Exception as e:
268 if skip_failed:
269 # respect skip_failed on unexpected errors
270 warn(str(e), image_warning_id, warn_skip=True)
271 continue
272 else:
273 raise
206 finally: 274 finally:
207 # Close the connection 275 # Close the connection
208 conn.close() 276 conn.close()
209 277
210 278