Mercurial > repos > iuc > idr_download_by_ids
comparison idr_download_by_ids.py @ 6:5c743356df83 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/idr_download commit bf331f8e60e7bc1daf1cd71d2068062d925e7ffa"
author | iuc |
---|---|
date | Mon, 06 Jul 2020 19:33:59 -0400 |
parents | e08b1dc0480c |
children | f067504aa92a |
comparison
equal
deleted
inserted
replaced
5:e08b1dc0480c | 6:5c743356df83 |
---|---|
1 import argparse | 1 import argparse |
2 import os | 2 import os |
3 import sys | 3 import sys |
4 import tarfile | 4 import tarfile |
5 import time | 5 from contextlib import ExitStack |
6 from tempfile import TemporaryFile | 6 from tempfile import TemporaryDirectory |
7 | 7 |
8 from libtiff import TIFF | 8 from libtiff import TIFF |
9 from PIL import Image | |
10 from omero.gateway import BlitzGateway # noqa | 9 from omero.gateway import BlitzGateway # noqa |
11 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa | 10 from omero.constants.namespaces import NSBULKANNOTATIONS # noqa |
12 | 11 |
13 | 12 |
14 def warn(message, image_identifier, warn_skip=False): | 13 def warn(message, image_identifier, warn_skip=False): |
127 raise ValueError( | 126 raise ValueError( |
128 'Got unknown value "{0}" as region_spec argument' | 127 'Got unknown value "{0}" as region_spec argument' |
129 .format(region_spec) | 128 .format(region_spec) |
130 ) | 129 ) |
131 | 130 |
132 # connect to idr | 131 with ExitStack() as exit_stack: |
133 conn = BlitzGateway('public', 'public', | 132 # connect to idr |
134 host='idr.openmicroscopy.org', | 133 conn = exit_stack.enter_context( |
135 secure=True) | 134 BlitzGateway( |
136 conn.connect() | 135 'public', 'public', |
137 | 136 host='idr.openmicroscopy.org', |
138 if download_tar: | 137 secure=True |
139 # create an archive file to write images to | 138 ) |
140 archive = tarfile.open('images.tar', mode='w') | 139 ) |
141 | 140 # exit_stack.callback(conn.connect().close) |
142 try: | 141 if download_tar: |
142 # create an archive file to write images to | |
143 archive = exit_stack.enter_context( | |
144 tarfile.open('images.tar', mode='w') | |
145 ) | |
146 tempdir = exit_stack.enter_context( | |
147 TemporaryDirectory() | |
148 ) | |
149 | |
143 for image_id in image_ids: | 150 for image_id in image_ids: |
144 image_warning_id = 'Image-ID: {0}'.format(image_id) | 151 image_warning_id = 'Image-ID: {0}'.format(image_id) |
145 try: | 152 try: |
146 image_id = int(image_id) | 153 image_id = int(image_id) |
147 except ValueError: | 154 except ValueError: |
269 | 276 |
270 fname = fname.replace(' ', '_') | 277 fname = fname.replace(' ', '_') |
271 | 278 |
272 im_array = get_image_array(image, tile, z_stack, channel_index, frame) | 279 im_array = get_image_array(image, tile, z_stack, channel_index, frame) |
273 | 280 |
274 # pack images into tarball | |
275 if download_tar: | 281 if download_tar: |
276 tar_img = Image.fromarray(im_array) | 282 fname = os.path.join(tempdir, fname) |
277 # Use TemporaryFile() for intermediate storage of images. | 283 try: |
278 # TO DO: could this be improved by using | 284 tiff = TIFF.open(fname, mode='w') |
279 # SpooledTemporaryFile with a suitable max_size? | 285 tiff.write_image(im_array) |
280 with TemporaryFile() as buf: | 286 finally: |
281 tar_img.save(buf, format='TIFF') | 287 tiff.close() |
282 tarinfo = tarfile.TarInfo(name=fname) | 288 # move image into tarball |
283 buf.seek(0, 2) | 289 if download_tar: |
284 tarinfo.size = buf.tell() | 290 archive.add(fname, os.path.basename(fname)) |
285 tarinfo.mtime = time.time() | 291 os.remove(fname) |
286 buf.seek(0) | |
287 archive.addfile(tarinfo=tarinfo, fileobj=buf) | |
288 else: # save image as individual file | |
289 try: | |
290 tiff = TIFF.open(fname, mode='w') | |
291 tiff.write_image(im_array) | |
292 finally: | |
293 tiff.close() | |
294 except Exception as e: | 292 except Exception as e: |
295 if skip_failed: | 293 if skip_failed: |
296 # respect skip_failed on unexpected errors | 294 # respect skip_failed on unexpected errors |
297 warn(str(e), image_warning_id, warn_skip=True) | 295 warn(str(e), image_warning_id, warn_skip=True) |
298 continue | 296 continue |
299 else: | 297 else: |
300 raise | 298 raise |
301 | |
302 finally: | |
303 # close the archive file,if it is created | |
304 if download_tar: | |
305 archive.close() | |
306 # Close the connection | |
307 conn.close() | |
308 | 299 |
309 | 300 |
310 def _center_to_ul(center_x, center_y, width, height): | 301 def _center_to_ul(center_x, center_y, width, height): |
311 if width > 0: | 302 if width > 0: |
312 ext_x = (width - 1) // 2 | 303 ext_x = (width - 1) // 2 |