comparison idr_download_by_ids.py @ 4:11036f6197d6 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/idr_download commit 3c3f1023af1edb4c63f59f4311cc078d9e88923f"
author iuc
date Fri, 15 May 2020 14:54:02 -0400
parents 381f248febba
children e08b1dc0480c
comparison
equal deleted inserted replaced
3:381f248febba 4:11036f6197d6
1 import argparse 1 import argparse
2 import os 2 import os
3 import sys 3 import sys
4 import tarfile
4 5
5 from libtiff import TIFF 6 from libtiff import TIFF
7 from PIL import Image
8 from tempfile import TemporaryFile
6 from omero.gateway import BlitzGateway # noqa 9 from omero.gateway import BlitzGateway # noqa
7 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa 10 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa
8 11
9 12
10 def warn(message, image_identifier, warn_skip=False): 13 def warn(message, image_identifier, warn_skip=False):
90 if t > max_t: 93 if t > max_t:
91 t = max_t 94 t = max_t
92 return t 95 return t
93 96
94 97
95 def download_plane_as_tiff(image, tile, z, c, t, fname): 98 def get_image_array(image, tile, z, c, t):
96 pixels = image.getPrimaryPixels() 99 pixels = image.getPrimaryPixels()
97 try: 100 try:
98 selection = pixels.getTile(theZ=z, theT=t, theC=c, tile=tile) 101 selection = pixels.getTile(theZ=z, theT=t, theC=c, tile=tile)
99 except Exception: 102 except Exception:
100 warning = '{0} (ID: {1})'.format(image.getName(), 103 warning = '{0} (ID: {1})'.format(image.getName(),
101 image.getId()) 104 image.getId())
102 warn('Could not download the requested region', warning) 105 warn('Could not download the requested region', warning)
103 return 106 return
104 107
105 if fname[-5:] != '.tiff': 108 return selection
106 fname += '.tiff'
107 try:
108 fname = fname.replace(' ', '_')
109 tiff = TIFF.open(fname, mode='w')
110 tiff.write_image(selection)
111 finally:
112 tiff.close()
113 109
114 110
115 def download_image_data( 111 def download_image_data(
116 image_ids, 112 image_ids,
117 channel=None, z_stack=0, frame=0, 113 channel=None, z_stack=0, frame=0,
118 coord=(0, 0), width=0, height=0, region_spec='rectangle', 114 coord=(0, 0), width=0, height=0, region_spec='rectangle',
119 skip_failed=False 115 skip_failed=False, download_tar=False
120 ): 116 ):
121 # basic argument sanity checks and adjustments 117 # basic argument sanity checks and adjustments
122 prefix = 'image-' 118 prefix = 'image-'
123 # normalize image ids by stripping off prefix if it exists 119 # normalize image ids by stripping off prefix if it exists
124 image_ids = [ 120 image_ids = [
135 # connect to idr 131 # connect to idr
136 conn = BlitzGateway('public', 'public', 132 conn = BlitzGateway('public', 'public',
137 host='idr.openmicroscopy.org', 133 host='idr.openmicroscopy.org',
138 secure=True) 134 secure=True)
139 conn.connect() 135 conn.connect()
136
137 if download_tar:
138 # create an archive file to write images to
139 archive = tarfile.open('images.tar', mode='w')
140 140
141 try: 141 try:
142 for image_id in image_ids: 142 for image_id in image_ids:
143 image_warning_id = 'Image-ID: {0}'.format(image_id) 143 image_warning_id = 'Image-ID: {0}'.format(image_id)
144 try: 144 try:
261 # download and save the region as TIFF 261 # download and save the region as TIFF
262 fname = '__'.join( 262 fname = '__'.join(
263 [image_name, str(image_id)] + [str(x) for x in tile] 263 [image_name, str(image_id)] + [str(x) for x in tile]
264 ) 264 )
265 try: 265 try:
266 download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname) 266 if fname[-5:] != '.tiff':
267 fname += '.tiff'
268
269 fname = fname.replace(' ', '_')
270
271 im_array = get_image_array(image, tile, z_stack, channel_index, frame)
272
273 # pack images into tarball
274 if download_tar:
275 tar_img = Image.fromarray(im_array)
276 # Use TemporaryFile() for intermediate storage of images.
277 # TO DO: could this be improved by using
278 # SpooledTemporaryFile with a suitable max_size?
279 with TemporaryFile() as buf:
280 tar_img.save(buf, format='TIFF')
281 tarinfo = tarfile.TarInfo(name=fname)
282 buf.seek(0, 2)
283 tarinfo.size = buf.tell()
284 buf.seek(0)
285 archive.addfile(tarinfo=tarinfo, fileobj=buf)
286 else: # save image as individual file
287 try:
288 tiff = TIFF.open(fname, mode='w')
289 tiff.write_image(im_array)
290 finally:
291 tiff.close()
267 except Exception as e: 292 except Exception as e:
268 if skip_failed: 293 if skip_failed:
269 # respect skip_failed on unexpected errors 294 # respect skip_failed on unexpected errors
270 warn(str(e), image_warning_id, warn_skip=True) 295 warn(str(e), image_warning_id, warn_skip=True)
271 continue 296 continue
272 else: 297 else:
273 raise 298 raise
299
274 finally: 300 finally:
301 # close the archive file,if it is created
302 if download_tar:
303 archive.close()
275 # Close the connection 304 # Close the connection
276 conn.close() 305 conn.close()
277 306
278 307
279 def _center_to_ul(center_x, center_y, width, height): 308 def _center_to_ul(center_x, center_y, width, height):
329 '-z', '--z-stack', type=int, default=0 358 '-z', '--z-stack', type=int, default=0
330 ) 359 )
331 p.add_argument( 360 p.add_argument(
332 '--skip-failed', action='store_true' 361 '--skip-failed', action='store_true'
333 ) 362 )
363 p.add_argument(
364 '--download-tar', action='store_true'
365 )
334 args = p.parse_args() 366 args = p.parse_args()
335 if not args.image_ids: 367 if not args.image_ids:
336 args.image_ids = sys.stdin.read().split() 368 args.image_ids = sys.stdin.read().split()
337 if 'center' in args: 369 if 'center' in args:
338 args.coord, args.width, args.height = ( 370 args.coord, args.width, args.height = (