changeset 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 4b794652dcdc
files idr_download_by_ids.py idr_download_by_ids.xml
diffstat 2 files changed, 293 insertions(+), 195 deletions(-) [+]
line wrap: on
line diff
--- a/idr_download_by_ids.py	Wed Nov 24 21:01:02 2021 +0000
+++ b/idr_download_by_ids.py	Sat Jan 15 12:25:29 2022 +0000
@@ -7,6 +7,7 @@
 from tempfile import TemporaryDirectory
 
 from libtiff import TIFF
+from omero.cli import cli_login
 from omero.gateway import BlitzGateway  # noqa
 from omero.constants.namespaces import NSBULKANNOTATIONS  # noqa
 
@@ -111,6 +112,7 @@
 
 def download_image_data(
     image_ids_or_dataset_id, dataset=False,
+    download_original=False,
     channel=None, z_stack=0, frame=0,
     coord=(0, 0), width=0, height=0, region_spec='rectangle',
     skip_failed=False, download_tar=False, omero_host='idr.openmicroscopy.org', omero_secured=False, config_file=None
@@ -129,7 +131,7 @@
                 omero_username = 'public'
                 omero_password = 'public'
 
-    if region_spec not in ['rectangle', 'center']:
+    if not download_original and region_spec not in ['rectangle', 'center']:
         raise ValueError(
             'Got unknown value "{0}" as region_spec argument'
             .format(region_spec)
@@ -224,124 +226,159 @@
                     'database. Aborting!'
                     .format(image_warning_id)
                 )
+            if not download_original:
+                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
+                    )
 
-            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)
+                        )
+
+                    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
 
-                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)
+                # 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.
+                    warn(
+                        'Downloaded image dimensions ({0} x {1}) will be smaller '
+                        'than the specified width and height ({2} x {3}).'
+                        .format(tile[2], tile[3], width, height),
+                        image_warning_id
+                    )
+
+                # z-stack sanity checks and warnings
+                if z_stack != ori_z:
+                    warn(
+                        'Specified image plane ({0}) is out of bounds. Using {1} '
+                        'instead.'
+                        .format(ori_z, z_stack),
+                        image_warning_id
+                    )
+
+                # frame sanity checks and warnings
+                if frame != ori_frame:
+                    warn(
+                        'Specified image frame ({0}) is out of bounds. Using '
+                        'frame {1} instead.'
+                        .format(ori_frame, frame),
+                        image_warning_id
                     )
 
-                ori_z, z_stack = z_stack, confine_plane(image, z_stack)
-                ori_frame, frame = frame, confine_frame(image, frame)
-                num_channels = image.getSizeC()
+                # channel index sanity checks and warnings
                 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
+                    if num_channels > 1:
+                        warn(
+                            'No specific channel selected for multi-channel '
+                            'image. Using first of {0} channels.'
+                            .format(num_channels),
+                            image_warning_id
+                        )
                 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.
-                warn(
-                    'Downloaded image dimensions ({0} x {1}) will be smaller '
-                    'than the specified width and height ({2} x {3}).'
-                    .format(tile[2], tile[3], width, height),
-                    image_warning_id
-                )
+                    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)
+                            )
 
-            # z-stack sanity checks and warnings
-            if z_stack != ori_z:
-                warn(
-                    'Specified image plane ({0}) is out of bounds. Using {1} '
-                    'instead.'
-                    .format(ori_z, z_stack),
-                    image_warning_id
+                # download and save the region as TIFF
+                fname = '__'.join(
+                    [image_name, str(image_id)] + [str(x) for x in tile]
                 )
+                try:
+                    if fname[-5:] != '.tiff':
+                        fname += '.tiff'
 
-            # frame sanity checks and warnings
-            if frame != ori_frame:
-                warn(
-                    'Specified image frame ({0}) is out of bounds. Using '
-                    'frame {1} instead.'
-                    .format(ori_frame, frame),
-                    image_warning_id
-                )
+                    fname = fname.replace(' ', '_')
+
+                    im_array = get_image_array(image, tile, z_stack, channel_index, frame)
 
-            # channel index sanity checks and warnings
-            if channel is None:
-                if num_channels > 1:
-                    warn(
-                        'No specific channel selected for multi-channel '
-                        'image. Using first of {0} channels.'
-                        .format(num_channels),
-                        image_warning_id
-                    )
-            else:
-                if channel_index == -1 or channel_index >= num_channels:
+                    if download_tar:
+                        fname = os.path.join(tempdir, fname)
+                    try:
+                        tiff = TIFF.open(fname, mode='w')
+                        tiff.write_image(im_array)
+                    finally:
+                        tiff.close()
+                    # move image into tarball
+                    if download_tar:
+                        archive.add(fname, os.path.basename(fname))
+                        os.remove(fname)
+                except Exception as e:
                     if skip_failed:
-                        warn(
-                            str(channel)
-                            + ' is not a known channel name for this image.',
-                            image_warning_id,
-                            warn_skip=True
-                        )
+                        # respect skip_failed on unexpected errors
+                        warn(str(e), 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]
-            )
-            try:
-                if fname[-5:] != '.tiff':
-                    fname += '.tiff'
-
-                fname = fname.replace(' ', '_')
-
-                im_array = get_image_array(image, tile, z_stack, channel_index, frame)
-
-                if download_tar:
-                    fname = os.path.join(tempdir, fname)
+                        raise
+            else:
                 try:
-                    tiff = TIFF.open(fname, mode='w')
-                    tiff.write_image(im_array)
-                finally:
-                    tiff.close()
-                # move image into tarball
-                if download_tar:
-                    archive.add(fname, os.path.basename(fname))
-                    os.remove(fname)
-            except Exception as e:
-                if skip_failed:
+                    # 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
+                    )
+                    original_image_name = image.getFileset().listFiles()[0].getName()
+                    fname = image_name + "__" + str(image_id) + os.path.splitext(original_image_name)[1]
+                    fname = fname.replace(' ', '_')
+                    fname = fname.replace('/', '_')
+                    download_directory = "./"
+                    if download_tar:
+                        download_directory = tempdir
+                    with cli_login("-u", omero_username, "-s", omero_host, "-w", omero_password) as cli:
+                        cli.invoke(["download", f"Image:{image_id}", download_directory])
+                        if cli.rv != 0:
+                            raise Exception("Download failed.")
+                    # This will download to download_directory/original_image_name
+                    os.rename(os.path.join(download_directory, original_image_name),
+                              os.path.join(download_directory, fname))
+                    # move image into tarball
+                    if download_tar:
+                        archive.add(os.path.join(download_directory, fname),
+                                    os.path.basename(fname))
+                        os.remove(os.path.join(download_directory, fname))
+                except Exception as e:
                     # respect skip_failed on unexpected errors
-                    warn(str(e), image_warning_id, warn_skip=True)
-                    continue
-                else:
-                    raise
+                    if skip_failed:
+                        warn(str(e), image_warning_id, warn_skip=True)
+                        continue
+                    else:
+                        raise
 
 
 def _center_to_ul(center_x, center_y, width, height):
@@ -369,6 +406,10 @@
              'read ids from stdin).'
     )
     p.add_argument(
+        '--download-original', dest='download_original', action='store_true',
+        help="download the original file uploaded to omero"
+    )
+    p.add_argument(
         '-c', '--channel',
         help='name of the channel to retrieve data for '
              '(note: the first channel of each image will be downloaded if '
--- a/idr_download_by_ids.xml	Wed Nov 24 21:01:02 2021 +0000
+++ b/idr_download_by_ids.xml	Sat Jan 15 12:25:29 2022 +0000
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<tool id="idr_download_by_ids" name="IDR/OMERO Download" version="0.43" profile="18.09">
+<tool id="idr_download_by_ids" name="IDR/OMERO Download" version="0.44" profile="18.09">
     <description>- download images from any OMERO instance using image IDs</description>
     <macros>
         <xml name="region_spec" token_pos="upper-left corner">
@@ -46,14 +46,18 @@
         #if str($image_ids.source) == 'omeroDatasetID':
             --dataset
         #end if
-        #set $channel = str($channel).strip()
-        #if $channel:
-            -c '$channel'
-        #end if
-        -f $frame
-        -z $z_section
-        #if str($clip_image.select):
-            ${clip_image.select} ${clip_image.x_coord} ${clip_image.y_coord} ${clip_image.width} ${clip_image.height}
+        #if $image_region.original == "original":
+            --download-original
+        #else:
+            #set $channel = str($image_region.channel).strip()
+            #if $channel:
+                -c '$channel'
+            #end if
+            -f $image_region.frame
+            -z $image_region.z_section
+            #if str($image_region.clip_image.select):
+                ${image_region.clip_image.select} ${image_region.clip_image.x_coord} ${image_region.clip_image.y_coord} ${image_region.clip_image.width} ${image_region.clip_image.height}
+            #end if
         #end if
         $skip_failed
         $download_tar
@@ -124,26 +128,36 @@
                 <param name="id_dataset_omero" type="integer" min = "0" value = "9059" label="Dataset ID"/>
             </when>
         </conditional>
-        <param name="channel" type="text"
-        label="Name of the channel to download"
-        help="For all image IDs only the specified channel will be downloaded. If left empty, the first channel (whatever this is) will be downloaded by default." />
-        <param name="z_section" type="integer" value="0" min="0"
-        label="z-plane of images to download" />
-        <param name="frame" type="integer" value="0" min="0"
-        label="Image frame to download" />
-        <conditional name="clip_image">
-            <param name="select" type="select"
-            label="Limit the download to a selected region of the image?">
-                <option value="">No, download the entire image plane</option>
-                <option value="--rectangle">Specify a region using its upper-left corner</option>
-                <option value="--center">Specify a width x height region around a central point</option>
+
+        <conditional name="image_region">
+            <param name="original" type="select" label="Which images do you want to download?">
+                <option value="TIFF" selected="true">Exported TIFF (single channel, single stack)</option>
+                <option value="original">Original file (file uploaded to omero, only available for private instances)</option>
             </param>
-            <when value="" />
-            <when value="--rectangle">
-                <expand macro="region_spec" />
-            </when>
-            <when value="--center">
-                <expand macro="region_spec" pos="center" />
+            <when value="original"/>
+            <when value="TIFF">
+                <param name="channel" type="text"
+                    label="Name of the channel to download"
+                    help="For all image IDs only the specified channel will be downloaded. If left empty, the first channel (whatever this is) will be downloaded by default." />
+                <param name="z_section" type="integer" value="0" min="0"
+                    label="z-plane of images to download" />
+                <param name="frame" type="integer" value="0" min="0"
+                    label="Image frame to download" />
+                <conditional name="clip_image">
+                    <param name="select" type="select"
+                        label="Limit the download to a selected region of the image?">
+                            <option value="">No, download the entire image plane</option>
+                            <option value="--rectangle">Specify a region using its upper-left corner</option>
+                            <option value="--center">Specify a width x height region around a central point</option>
+                    </param>
+                    <when value="" />
+                    <when value="--rectangle">
+                        <expand macro="region_spec" />
+                    </when>
+                    <when value="--center">
+                        <expand macro="region_spec" pos="center" />
+                    </when>
+                </conditional>
             </when>
         </conditional>
         <param name="skip_failed" type="boolean" checked="false" truevalue="--skip-failed" falsevalue=""
@@ -171,15 +185,18 @@
             </conditional>
             <param name="source" value="dataset" />
             <param name="id_spec" value="ids.txt" />
-            <param name="channel" value="PCNT" />
-            <conditional name="clip_image">
-                <param name="select" value="--rectangle" />
-                <param name="x_coord" value="3" />
-                <param name="y_coord" value="3" />
-                <param name="width" value="5" />
-                <param name="height" value="5" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/>
+                <param name="channel" value="PCNT" />
+                <conditional name="clip_image">
+                    <param name="select" value="--rectangle" />
+                    <param name="x_coord" value="3" />
+                    <param name="y_coord" value="3" />
+                    <param name="width" value="5" />
+                    <param name="height" value="5" />
+                </conditional>
+                <param name="frame" value="0" />
             </conditional>
-            <param name="frame" value="0" />
             <param name="download_tar" value="false" />
             <output_collection name="output_file" type="list">
                 <element name="Centrin_PCNT_Cep215_20110506_Fri-1545_0_SIR_PRJ__1884807__3__3__5__5" ftype="tiff" file="test0.tiff"/>
@@ -191,15 +208,18 @@
             </conditional>
             <param name="source" value="link" />
             <param name="id_spec" value="1884807" />
-            <param name="channel" value="PCNT" />
-            <conditional name="clip_image">
-                <param name="select" value="--rectangle" />
-                <param name="x_coord" value="3" />
-                <param name="y_coord" value="3" />
-                <param name="width" value="5" />
-                <param name="height" value="5" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="PCNT" />
+                <conditional name="clip_image">
+                    <param name="select" value="--rectangle" />
+                    <param name="x_coord" value="3" />
+                    <param name="y_coord" value="3" />
+                    <param name="width" value="5" />
+                    <param name="height" value="5" />
+                </conditional>
+                <param name="frame" value="2" />
             </conditional>
-            <param name="frame" value="2" />
             <param name="download_tar" value="false" />
             <output_collection name="output_file" type="list">
                 <element name="Centrin_PCNT_Cep215_20110506_Fri-1545_0_SIR_PRJ__1884807__3__3__5__5" ftype="tiff" file="test1.tiff"/>
@@ -211,15 +231,18 @@
             </conditional>
             <param name="source" value="link" />
             <param name="id_spec" value="1884807" />
-            <param name="channel" value="PCNT" />
-            <conditional name="clip_image">
-                <param name="select" value="--center" />
-                <param name="x_coord" value="5" />
-                <param name="y_coord" value="5" />
-                <param name="width" value="5" />
-                <param name="height" value="5" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="PCNT" />
+                <conditional name="clip_image">
+                    <param name="select" value="--center" />
+                    <param name="x_coord" value="5" />
+                    <param name="y_coord" value="5" />
+                    <param name="width" value="5" />
+                    <param name="height" value="5" />
+                </conditional>
+                <param name="frame" value="2" />
             </conditional>
-            <param name="frame" value="2" />
             <param name="download_tar" value="false" />
             <output_collection name="output_file" type="list">
                 <element name="Centrin_PCNT_Cep215_20110506_Fri-1545_0_SIR_PRJ__1884807__3__3__5__5" ftype="tiff" file="test2.tiff"/>
@@ -231,12 +254,15 @@
             </conditional>
             <param name="source" value="link" />
             <param name="id_spec" value="https://idr.openmicroscopy.org/webclient/?show=image-9036708|image-9036710|image-9036711" />
-            <param name="channel" value="Spo20(51-91)" />
-            <param name="z_section" value="4" />
-            <conditional name="clip_image">
-                <param name="select" value="" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="Spo20(51-91)" />
+                <param name="z_section" value="4" />
+                <conditional name="clip_image">
+                    <param name="select" value="" />
+                </conditional>
+                <param name="frame" value="20" />
             </conditional>
-            <param name="frame" value="20" />
             <param name="download_tar" value="false" />
             <output_collection name="output_file" type="list" count="3">
                 <element name="171101_LeadingEdgeDeletionPSMMovies01_15_R3D__9036711__0__0__1024__1024">
@@ -259,12 +285,15 @@
             <!-- Test behavior with non-existing image-ID 9036708999 -->
             <param name="source" value="link" />
             <param name="id_spec" value="https://idr.openmicroscopy.org/webclient/?show=image-9036708999|image-9036710|image-9036711" />
-            <param name="channel" value="Spo20(51-91)" />
-            <param name="z_section" value="4" />
-            <conditional name="clip_image">
-                <param name="select" value="" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="Spo20(51-91)" />
+                <param name="z_section" value="4" />
+                <conditional name="clip_image">
+                    <param name="select" value="" />
+                </conditional>
+                <param name="frame" value="20" />
             </conditional>
-            <param name="frame" value="20" />
             <param name="download_tar" value="false" />
         </test>
         <test>
@@ -275,12 +304,15 @@
             </conditional>
             <param name="source" value="link" />
             <param name="id_spec" value="https://idr.openmicroscopy.org/webclient/?show=image-9036708999|image-9036710|image-9036711" />
-            <param name="channel" value="Spo20(51-91)" />
-            <param name="z_section" value="4" />
-            <conditional name="clip_image">
-                <param name="select" value="" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="Spo20(51-91)" />
+                <param name="z_section" value="4" />
+                <conditional name="clip_image">
+                    <param name="select" value="" />
+                </conditional>
+                <param name="frame" value="20" />
             </conditional>
-            <param name="frame" value="20" />
             <param name="skip_failed" value="true" />
             <param name="download_tar" value="false" />
             <output_collection name="output_file" type="list" count="2">
@@ -305,15 +337,18 @@
             <!-- Test for download images in a tarball -->
             <param name="source" value="dataset" />
             <param name="id_spec" value="ids_tar.txt" />
-            <param name="channel" value="Hoechst" />
-            <conditional name="clip_image">
-                <param name="select" value="--rectangle" />
-                <param name="x_coord" value="0" />
-                <param name="y_coord" value="0" />
-                <param name="width" value="671" />
-                <param name="height" value="511" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="Hoechst" />
+                <conditional name="clip_image">
+                    <param name="select" value="--rectangle" />
+                    <param name="x_coord" value="0" />
+                    <param name="y_coord" value="0" />
+                    <param name="width" value="671" />
+                    <param name="height" value="511" />
+                </conditional>
+                <param name="frame" value="0" />
             </conditional>
-            <param name="frame" value="0" />
             <output name="output_tar">
                     <assert_contents>
                         <has_size value="1382400" />
@@ -335,15 +370,18 @@
             </conditional>
             <param name="source" value="dataset" />
             <param name="id_spec" value="ids.txt" />
-            <param name="channel" value="PCNT" />
-            <conditional name="clip_image">
-                <param name="select" value="--rectangle" />
-                <param name="x_coord" value="3" />
-                <param name="y_coord" value="3" />
-                <param name="width" value="5" />
-                <param name="height" value="5" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="PCNT" />
+                <conditional name="clip_image">
+                    <param name="select" value="--rectangle" />
+                    <param name="x_coord" value="3" />
+                    <param name="y_coord" value="3" />
+                    <param name="width" value="5" />
+                    <param name="height" value="5" />
+                </conditional>
+                <param name="frame" value="0" />
             </conditional>
-            <param name="frame" value="0" />
             <param name="download_tar" value="false" />
             <output_collection name="output_file" type="list">
                 <element name="Centrin_PCNT_Cep215_20110506_Fri-1545_0_SIR_PRJ__1884807__3__3__5__5" file="test0.tiff"/>
@@ -357,15 +395,18 @@
             </conditional>
             <param name="source" value="dataset" />
             <param name="id_spec" value="ids.txt" />
-            <param name="channel" value="PCNT" />
-            <conditional name="clip_image">
-                <param name="select" value="--rectangle" />
-                <param name="x_coord" value="3" />
-                <param name="y_coord" value="3" />
-                <param name="width" value="5" />
-                <param name="height" value="5" />
+            <conditional name="image_region">
+                <param name="original" value="TIFF"/> 
+                <param name="channel" value="PCNT" />
+                <conditional name="clip_image">
+                    <param name="select" value="--rectangle" />
+                    <param name="x_coord" value="3" />
+                    <param name="y_coord" value="3" />
+                    <param name="width" value="5" />
+                    <param name="height" value="5" />
+                </conditional>
+                <param name="frame" value="0" />
             </conditional>
-            <param name="frame" value="0" />
             <param name="download_tar" value="false" />
             <assert_stderr>
                 <has_text text="OMERO connection credentials are empty. Set your credentials via: User -> Preferences -> Manage Information" />
@@ -402,6 +443,22 @@
                 </element>
             </output_collection>
         </test>
+        <test>
+            <conditional name="omero_instance_type">
+                 <param name="omero_instance" value="idr" />
+            </conditional>
+            <!-- Test for download all original files from a dataset -->
+            <!-- Impossible in idr: -->
+            <param name="source" value="omeroDatasetID" />
+            <param name="id_dataset_omero" value="9059" />
+            <param name="skip_failed" value="true" />
+            <param name="download_tar" value="false" />
+            <conditional name="image_region">
+                <param name="original" value="original"/> 
+            </conditional>
+            <output_collection name="output_file" type="list" count="0">
+            </output_collection>
+        </test>
     </tests>
     <help><![CDATA[
 Download image data from the IDR_ (Image Data Resource) - a public repository