Mercurial > repos > iuc > idr_download_by_ids
comparison idr_download_by_ids.py @ 11:cbd605a24336 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/idr_download commit b68715960d1107593db13dd9e1dbd8d4b905cc6f"
author | iuc |
---|---|
date | Sat, 15 Jan 2022 12:25:29 +0000 |
parents | 4aed70472589 |
children | f92941d1a85e |
comparison
equal
deleted
inserted
replaced
10:4aed70472589 | 11:cbd605a24336 |
---|---|
5 import tarfile | 5 import tarfile |
6 from contextlib import ExitStack | 6 from contextlib import ExitStack |
7 from tempfile import TemporaryDirectory | 7 from tempfile import TemporaryDirectory |
8 | 8 |
9 from libtiff import TIFF | 9 from libtiff import TIFF |
10 from omero.cli import cli_login | |
10 from omero.gateway import BlitzGateway # noqa | 11 from omero.gateway import BlitzGateway # noqa |
11 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa | 12 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa |
12 | 13 |
13 | 14 |
14 def warn(message, image_identifier, warn_skip=False): | 15 def warn(message, image_identifier, warn_skip=False): |
109 return selection | 110 return selection |
110 | 111 |
111 | 112 |
112 def download_image_data( | 113 def download_image_data( |
113 image_ids_or_dataset_id, dataset=False, | 114 image_ids_or_dataset_id, dataset=False, |
115 download_original=False, | |
114 channel=None, z_stack=0, frame=0, | 116 channel=None, z_stack=0, frame=0, |
115 coord=(0, 0), width=0, height=0, region_spec='rectangle', | 117 coord=(0, 0), width=0, height=0, region_spec='rectangle', |
116 skip_failed=False, download_tar=False, omero_host='idr.openmicroscopy.org', omero_secured=False, config_file=None | 118 skip_failed=False, download_tar=False, omero_host='idr.openmicroscopy.org', omero_secured=False, config_file=None |
117 ): | 119 ): |
118 | 120 |
127 | 129 |
128 if omero_username == "" or omero_password == "": | 130 if omero_username == "" or omero_password == "": |
129 omero_username = 'public' | 131 omero_username = 'public' |
130 omero_password = 'public' | 132 omero_password = 'public' |
131 | 133 |
132 if region_spec not in ['rectangle', 'center']: | 134 if not download_original and region_spec not in ['rectangle', 'center']: |
133 raise ValueError( | 135 raise ValueError( |
134 'Got unknown value "{0}" as region_spec argument' | 136 'Got unknown value "{0}" as region_spec argument' |
135 .format(region_spec) | 137 .format(region_spec) |
136 ) | 138 ) |
137 with ExitStack() as exit_stack: | 139 with ExitStack() as exit_stack: |
222 raise ValueError( | 224 raise ValueError( |
223 '{0}: Unable to find an image with this ID in the ' | 225 '{0}: Unable to find an image with this ID in the ' |
224 'database. Aborting!' | 226 'database. Aborting!' |
225 .format(image_warning_id) | 227 .format(image_warning_id) |
226 ) | 228 ) |
227 | 229 if not download_original: |
228 try: | 230 try: |
229 # try to extract image properties | 231 # try to extract image properties |
230 # if anything goes wrong here skip the image | 232 # if anything goes wrong here skip the image |
231 # or abort. | 233 # or abort. |
232 image_name = os.path.splitext(image.getName())[0] | 234 image_name = os.path.splitext(image.getName())[0] |
233 image_warning_id = '{0} (ID: {1})'.format( | 235 image_warning_id = '{0} (ID: {1})'.format( |
234 image_name, image_id | 236 image_name, image_id |
237 ) | |
238 | |
239 if region_spec == 'rectangle': | |
240 tile = get_clipping_region(image, *coord, width, height) | |
241 elif region_spec == 'center': | |
242 tile = get_clipping_region( | |
243 image, | |
244 *_center_to_ul(*coord, width, height) | |
245 ) | |
246 | |
247 ori_z, z_stack = z_stack, confine_plane(image, z_stack) | |
248 ori_frame, frame = frame, confine_frame(image, frame) | |
249 num_channels = image.getSizeC() | |
250 if channel is None: | |
251 channel_index = 0 | |
252 else: | |
253 channel_index = find_channel_index(image, channel) | |
254 except Exception as e: | |
255 # respect skip_failed on unexpected errors | |
256 if skip_failed: | |
257 warn(str(e), image_warning_id, warn_skip=True) | |
258 continue | |
259 else: | |
260 raise | |
261 | |
262 # region sanity checks and warnings | |
263 if tile[2] < width or tile[3] < height: | |
264 # The downloaded image region will have smaller dimensions | |
265 # than the specified width x height. | |
266 warn( | |
267 'Downloaded image dimensions ({0} x {1}) will be smaller ' | |
268 'than the specified width and height ({2} x {3}).' | |
269 .format(tile[2], tile[3], width, height), | |
270 image_warning_id | |
271 ) | |
272 | |
273 # z-stack sanity checks and warnings | |
274 if z_stack != ori_z: | |
275 warn( | |
276 'Specified image plane ({0}) is out of bounds. Using {1} ' | |
277 'instead.' | |
278 .format(ori_z, z_stack), | |
279 image_warning_id | |
280 ) | |
281 | |
282 # frame sanity checks and warnings | |
283 if frame != ori_frame: | |
284 warn( | |
285 'Specified image frame ({0}) is out of bounds. Using ' | |
286 'frame {1} instead.' | |
287 .format(ori_frame, frame), | |
288 image_warning_id | |
289 ) | |
290 | |
291 # channel index sanity checks and warnings | |
292 if channel is None: | |
293 if num_channels > 1: | |
294 warn( | |
295 'No specific channel selected for multi-channel ' | |
296 'image. Using first of {0} channels.' | |
297 .format(num_channels), | |
298 image_warning_id | |
299 ) | |
300 else: | |
301 if channel_index == -1 or channel_index >= num_channels: | |
302 if skip_failed: | |
303 warn( | |
304 str(channel) | |
305 + ' is not a known channel name for this image.', | |
306 image_warning_id, | |
307 warn_skip=True | |
308 ) | |
309 continue | |
310 else: | |
311 raise ValueError( | |
312 '"{0}" is not a known channel name for image {1}. ' | |
313 'Aborting!' | |
314 .format(channel, image_warning_id) | |
315 ) | |
316 | |
317 # download and save the region as TIFF | |
318 fname = '__'.join( | |
319 [image_name, str(image_id)] + [str(x) for x in tile] | |
235 ) | 320 ) |
236 | 321 try: |
237 if region_spec == 'rectangle': | 322 if fname[-5:] != '.tiff': |
238 tile = get_clipping_region(image, *coord, width, height) | 323 fname += '.tiff' |
239 elif region_spec == 'center': | 324 |
240 tile = get_clipping_region( | 325 fname = fname.replace(' ', '_') |
241 image, | 326 |
242 *_center_to_ul(*coord, width, height) | 327 im_array = get_image_array(image, tile, z_stack, channel_index, frame) |
243 ) | 328 |
244 | 329 if download_tar: |
245 ori_z, z_stack = z_stack, confine_plane(image, z_stack) | 330 fname = os.path.join(tempdir, fname) |
246 ori_frame, frame = frame, confine_frame(image, frame) | 331 try: |
247 num_channels = image.getSizeC() | 332 tiff = TIFF.open(fname, mode='w') |
248 if channel is None: | 333 tiff.write_image(im_array) |
249 channel_index = 0 | 334 finally: |
250 else: | 335 tiff.close() |
251 channel_index = find_channel_index(image, channel) | 336 # move image into tarball |
252 except Exception as e: | 337 if download_tar: |
253 # respect skip_failed on unexpected errors | 338 archive.add(fname, os.path.basename(fname)) |
254 if skip_failed: | 339 os.remove(fname) |
255 warn(str(e), image_warning_id, warn_skip=True) | 340 except Exception as e: |
256 continue | 341 if skip_failed: |
257 else: | 342 # respect skip_failed on unexpected errors |
258 raise | 343 warn(str(e), image_warning_id, warn_skip=True) |
259 | 344 continue |
260 # region sanity checks and warnings | 345 else: |
261 if tile[2] < width or tile[3] < height: | 346 raise |
262 # The downloaded image region will have smaller dimensions | |
263 # than the specified width x height. | |
264 warn( | |
265 'Downloaded image dimensions ({0} x {1}) will be smaller ' | |
266 'than the specified width and height ({2} x {3}).' | |
267 .format(tile[2], tile[3], width, height), | |
268 image_warning_id | |
269 ) | |
270 | |
271 # z-stack sanity checks and warnings | |
272 if z_stack != ori_z: | |
273 warn( | |
274 'Specified image plane ({0}) is out of bounds. Using {1} ' | |
275 'instead.' | |
276 .format(ori_z, z_stack), | |
277 image_warning_id | |
278 ) | |
279 | |
280 # frame sanity checks and warnings | |
281 if frame != ori_frame: | |
282 warn( | |
283 'Specified image frame ({0}) is out of bounds. Using ' | |
284 'frame {1} instead.' | |
285 .format(ori_frame, frame), | |
286 image_warning_id | |
287 ) | |
288 | |
289 # channel index sanity checks and warnings | |
290 if channel is None: | |
291 if num_channels > 1: | |
292 warn( | |
293 'No specific channel selected for multi-channel ' | |
294 'image. Using first of {0} channels.' | |
295 .format(num_channels), | |
296 image_warning_id | |
297 ) | |
298 else: | 347 else: |
299 if channel_index == -1 or channel_index >= num_channels: | 348 try: |
349 # try to extract image properties | |
350 # if anything goes wrong here skip the image | |
351 # or abort. | |
352 image_name = os.path.splitext(image.getName())[0] | |
353 image_warning_id = '{0} (ID: {1})'.format( | |
354 image_name, image_id | |
355 ) | |
356 original_image_name = image.getFileset().listFiles()[0].getName() | |
357 fname = image_name + "__" + str(image_id) + os.path.splitext(original_image_name)[1] | |
358 fname = fname.replace(' ', '_') | |
359 fname = fname.replace('/', '_') | |
360 download_directory = "./" | |
361 if download_tar: | |
362 download_directory = tempdir | |
363 with cli_login("-u", omero_username, "-s", omero_host, "-w", omero_password) as cli: | |
364 cli.invoke(["download", f"Image:{image_id}", download_directory]) | |
365 if cli.rv != 0: | |
366 raise Exception("Download failed.") | |
367 # This will download to download_directory/original_image_name | |
368 os.rename(os.path.join(download_directory, original_image_name), | |
369 os.path.join(download_directory, fname)) | |
370 # move image into tarball | |
371 if download_tar: | |
372 archive.add(os.path.join(download_directory, fname), | |
373 os.path.basename(fname)) | |
374 os.remove(os.path.join(download_directory, fname)) | |
375 except Exception as e: | |
376 # respect skip_failed on unexpected errors | |
300 if skip_failed: | 377 if skip_failed: |
301 warn( | 378 warn(str(e), image_warning_id, warn_skip=True) |
302 str(channel) | |
303 + ' is not a known channel name for this image.', | |
304 image_warning_id, | |
305 warn_skip=True | |
306 ) | |
307 continue | 379 continue |
308 else: | 380 else: |
309 raise ValueError( | 381 raise |
310 '"{0}" is not a known channel name for image {1}. ' | |
311 'Aborting!' | |
312 .format(channel, image_warning_id) | |
313 ) | |
314 | |
315 # download and save the region as TIFF | |
316 fname = '__'.join( | |
317 [image_name, str(image_id)] + [str(x) for x in tile] | |
318 ) | |
319 try: | |
320 if fname[-5:] != '.tiff': | |
321 fname += '.tiff' | |
322 | |
323 fname = fname.replace(' ', '_') | |
324 | |
325 im_array = get_image_array(image, tile, z_stack, channel_index, frame) | |
326 | |
327 if download_tar: | |
328 fname = os.path.join(tempdir, fname) | |
329 try: | |
330 tiff = TIFF.open(fname, mode='w') | |
331 tiff.write_image(im_array) | |
332 finally: | |
333 tiff.close() | |
334 # move image into tarball | |
335 if download_tar: | |
336 archive.add(fname, os.path.basename(fname)) | |
337 os.remove(fname) | |
338 except Exception as e: | |
339 if skip_failed: | |
340 # respect skip_failed on unexpected errors | |
341 warn(str(e), image_warning_id, warn_skip=True) | |
342 continue | |
343 else: | |
344 raise | |
345 | 382 |
346 | 383 |
347 def _center_to_ul(center_x, center_y, width, height): | 384 def _center_to_ul(center_x, center_y, width, height): |
348 if width > 0: | 385 if width > 0: |
349 ext_x = (width - 1) // 2 | 386 ext_x = (width - 1) // 2 |
367 help='one or more IDR image ids or a single dataset id' | 404 help='one or more IDR image ids or a single dataset id' |
368 'for which to retrieve data (default: ' | 405 'for which to retrieve data (default: ' |
369 'read ids from stdin).' | 406 'read ids from stdin).' |
370 ) | 407 ) |
371 p.add_argument( | 408 p.add_argument( |
409 '--download-original', dest='download_original', action='store_true', | |
410 help="download the original file uploaded to omero" | |
411 ) | |
412 p.add_argument( | |
372 '-c', '--channel', | 413 '-c', '--channel', |
373 help='name of the channel to retrieve data for ' | 414 help='name of the channel to retrieve data for ' |
374 '(note: the first channel of each image will be downloaded if ' | 415 '(note: the first channel of each image will be downloaded if ' |
375 'left unspecified)' | 416 'left unspecified)' |
376 ) | 417 ) |