diff 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
line wrap: on
line diff
--- a/idr_download_by_ids.py	Tue Apr 14 10:27:25 2020 -0400
+++ b/idr_download_by_ids.py	Wed Apr 22 11:43:18 2020 -0400
@@ -7,10 +7,22 @@
 from omero.constants.namespaces import NSBULKANNOTATIONS  # noqa
 
 
-def warn(message, image_identifier):
+def warn(message, image_identifier, warn_skip=False):
+    message = message.rstrip()
+    if warn_skip:
+        if message[-1] in ['.', '!', '?']:
+            skip_msg = ' Skipping download!'
+        else:
+            skip_msg = '. Skipping download!'
+    else:
+        skip_msg = ''
     print(
-        'ImageSpecWarning for {0}: {1}'
-        .format(image_identifier, message),
+        'ImageSpecWarning for {0}: {1}{2}'
+        .format(
+            image_identifier,
+            message,
+            skip_msg
+        ),
         file=sys.stderr
     )
 
@@ -93,6 +105,7 @@
     if fname[-5:] != '.tiff':
         fname += '.tiff'
     try:
+        fname = fname.replace(' ', '_')
         tiff = TIFF.open(fname, mode='w')
         tiff.write_image(selection)
     finally:
@@ -105,6 +118,19 @@
     coord=(0, 0), width=0, height=0, region_spec='rectangle',
     skip_failed=False
 ):
+    # basic argument sanity checks and adjustments
+    prefix = 'image-'
+    # normalize image ids by stripping off prefix if it exists
+    image_ids = [
+        iid[len(prefix):] if iid[:len(prefix)] == prefix else iid
+        for iid in image_ids
+    ]
+
+    if region_spec not in ['rectangle', 'center']:
+        raise ValueError(
+            'Got unknown value "{0}" as region_spec argument'
+            .format(region_spec)
+        )
 
     # connect to idr
     conn = BlitzGateway('public', 'public',
@@ -113,20 +139,30 @@
     conn.connect()
 
     try:
-        prefix = 'image-'
         for image_id in image_ids:
-            if image_id[:len(prefix)] == prefix:
-                image_id = image_id[len(prefix):]
-            image_id = int(image_id)
-            image = conn.getObject("Image", image_id)
+            image_warning_id = 'Image-ID: {0}'.format(image_id)
+            try:
+                image_id = int(image_id)
+            except ValueError:
+                image = None
+            else:
+                try:
+                    image = conn.getObject("Image", image_id)
+                except Exception as e:
+                    # respect skip_failed on unexpected errors
+                    if skip_failed:
+                        warn(str(e), image_warning_id, warn_skip=True)
+                        continue
+                    else:
+                        raise
 
             if image is None:
-                image_warning_id = 'Image-ID: {0}'.format(image_id)
                 if skip_failed:
                     warn(
                         'Unable to find an image with this ID in the '
-                        'database. Skipping download!',
-                        image_warning_id
+                        'database.',
+                        image_warning_id,
+                        warn_skip=True
                     )
                     continue
                 raise ValueError(
@@ -135,23 +171,39 @@
                     .format(image_warning_id)
                 )
 
-            image_name = os.path.splitext(image.getName())[0]
-            image_warning_id = '{0} (ID: {1})'.format(
-                image_name, image_id
-            )
+            try:
+                # try to extract image properties
+                # if anything goes wrong here skip the image
+                # or abort.
+                image_name = os.path.splitext(image.getName())[0]
+                image_warning_id = '{0} (ID: {1})'.format(
+                    image_name, image_id
+                )
+
+                if region_spec == 'rectangle':
+                    tile = get_clipping_region(image, *coord, width, height)
+                elif region_spec == 'center':
+                    tile = get_clipping_region(
+                        image,
+                        *_center_to_ul(*coord, width, height)
+                    )
 
-            if region_spec == 'rectangle':
-                tile = get_clipping_region(image, *coord, width, height)
-            elif region_spec == 'center':
-                tile = get_clipping_region(
-                    image,
-                    *_center_to_ul(*coord, width, height)
-                )
-            else:
-                raise ValueError(
-                    'Got unknown value "{0}" as region_spec argument'
-                    .format(region_spec)
-                )
+                ori_z, z_stack = z_stack, confine_plane(image, z_stack)
+                ori_frame, frame = frame, confine_frame(image, frame)
+                num_channels = image.getSizeC()
+                if channel is None:
+                    channel_index = 0
+                else:
+                    channel_index = find_channel_index(image, channel)
+            except Exception as e:
+                # respect skip_failed on unexpected errors
+                if skip_failed:
+                    warn(str(e), image_warning_id, warn_skip=True)
+                    continue
+                else:
+                    raise
+
+            # region sanity checks and warnings
             if tile[2] < width or tile[3] < height:
                 # The downloaded image region will have smaller dimensions
                 # than the specified width x height.
@@ -162,7 +214,7 @@
                     image_warning_id
                 )
 
-            ori_z, z_stack = z_stack, confine_plane(image, z_stack)
+            # z-stack sanity checks and warnings
             if z_stack != ori_z:
                 warn(
                     'Specified image plane ({0}) is out of bounds. Using {1} '
@@ -171,7 +223,7 @@
                     image_warning_id
                 )
 
-            ori_frame, frame = frame, confine_frame(image, frame)
+            # frame sanity checks and warnings
             if frame != ori_frame:
                 warn(
                     'Specified image frame ({0}) is out of bounds. Using '
@@ -179,10 +231,9 @@
                     .format(ori_frame, frame),
                     image_warning_id
                 )
-            # Get the channel index. If the index is not valid, skip the image
+
+            # channel index sanity checks and warnings
             if channel is None:
-                channel_index = 0
-                num_channels = image.getSizeC()
                 if num_channels > 1:
                     warn(
                         'No specific channel selected for multi-channel '
@@ -191,18 +242,35 @@
                         image_warning_id
                     )
             else:
-                channel_index = find_channel_index(image, channel)
-                if channel_index == -1 or channel_index >= image.getSizeC():
-                    raise ValueError(
-                        '"{0}" is not a known channel name for image {1}'
-                        .format(channel, image.getName())
-                    )
+                if channel_index == -1 or channel_index >= num_channels:
+                    if skip_failed:
+                        warn(
+                            str(channel)
+                            + ' is not a known channel name for this image.',
+                            image_warning_id,
+                            warn_skip=True
+                        )
+                        continue
+                    else:
+                        raise ValueError(
+                            '"{0}" is not a known channel name for image {1}. '
+                            'Aborting!'
+                            .format(channel, image_warning_id)
+                        )
 
             # download and save the region as TIFF
             fname = '__'.join(
                 [image_name, str(image_id)] + [str(x) for x in tile]
             )
-            download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname)
+            try:
+                download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname)
+            except Exception as e:
+                if skip_failed:
+                    # respect skip_failed on unexpected errors
+                    warn(str(e), image_warning_id, warn_skip=True)
+                    continue
+                else:
+                    raise
     finally:
         # Close the connection
         conn.close()