Repository 'idr_download_by_ids'
hg clone https://toolshed.g2.bx.psu.edu/repos/iuc/idr_download_by_ids

Changeset 0:57aa9597cd31 (2020-02-08)
Next changeset 1:9340cbc7796c (2020-03-24)
Commit message:
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/idr_download commit 253efabdfea3a1fecc4c0f2c54c0e97a7d7960ab"
added:
idr_download_by_ids.py
idr_download_by_ids.xml
test-data/ids.txt
test-data/test1.tiff
b
diff -r 000000000000 -r 57aa9597cd31 idr_download_by_ids.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/idr_download_by_ids.py Sat Feb 08 13:24:39 2020 -0500
[
b'@@ -0,0 +1,271 @@\n+import argparse\n+import os\n+import sys\n+\n+from matplotlib import pyplot as plt\n+from omero.gateway import BlitzGateway  # noqa\n+from omero.constants.namespaces import NSBULKANNOTATIONS  # noqa\n+\n+\n+def warn(message, image_identifier):\n+    print(\n+        \'ImageSpecWarning for {0}: {1}\'\n+        .format(image_identifier, message),\n+        file=sys.stderr\n+    )\n+\n+\n+def find_channel_index(image, channel_name):\n+    channel_name = channel_name.lower()\n+    for n, channel in enumerate(image.getChannels()):\n+        if channel_name == channel.getLabel().lower():\n+            return n\n+    # Check map annotation for information (this is necessary for some images)\n+    for ann in image.listAnnotations(NSBULKANNOTATIONS):\n+        pairs = ann.getValue()\n+        for p in pairs:\n+            if p[0] == "Channels":\n+                channels = p[1].replace(" ", "").split(";")\n+                for n, c in enumerate(channels):\n+                    for value in c.split(\':\'):\n+                        if channel_name == value.lower():\n+                            return n\n+    return -1\n+\n+\n+def get_clipping_region(image, x, y, w, h):\n+    # If the (x, y) coordinate falls outside the image boundaries, we\n+    # cannot just shift it because that would render the meaning of\n+    # w and h undefined (should width and height be decreased or the whole\n+    # region be shifted to keep them fixed?).\n+    # It may be better to abort in this situation.\n+    if x < 0 or y < 0:\n+        raise ValueError(\n+            \'Too small upper left coordinate ({0}, {1}) for clipping region.\'\n+            .format(x, y)\n+        )\n+    size_x = image.getSizeX()\n+    size_y = image.getSizeY()\n+    if x >= size_x or y >= size_y:\n+        raise ValueError(\n+            \'Upper left coordinate ({0}, {1}) of clipping region lies \'\n+            \'outside of image.\'\n+            .format(x, y)\n+        )\n+    # adjust width and height to the image dimensions\n+    if w <= 0 or x + w > size_x:\n+        w = size_x - x\n+    if h <= 0 or y + h > size_y:\n+        h = size_y - y\n+    return [x, y, w, h]\n+\n+\n+def confine_plane(image, z):\n+    if z < 0:\n+        z = 0\n+    else:\n+        max_z = image.getSizeZ() - 1\n+        if z > max_z:\n+            z = max_z\n+    return z\n+\n+\n+def confine_frame(image, t):\n+    if t < 0:\n+        t = 0\n+    else:\n+        max_t = image.getSizeT() - 1\n+        if t > max_t:\n+            t = max_t\n+    return t\n+\n+\n+def download_plane_as_tiff(image, tile, z, c, t, fname):\n+    pixels = image.getPrimaryPixels()\n+    selection = pixels.getTile(theZ=z, theT=t, theC=c, tile=tile)\n+\n+    if fname[-5:] != \'.tiff\':\n+        fname += \'.tiff\'\n+    plt.imsave(fname, selection)\n+\n+\n+def download_image_data(\n+    image_ids,\n+    channel=None, z_stack=0, frame=0,\n+    coord=(0, 0), width=0, height=0, region_spec=\'rectangle\',\n+    skip_failed=False\n+):\n+\n+    # connect to idr\n+    conn = BlitzGateway(\'public\', \'public\',\n+                        host=\'idr.openmicroscopy.org\',\n+                        secure=True)\n+    conn.connect()\n+\n+    try:\n+        prefix = \'image-\'\n+        for image_id in image_ids:\n+            if image_id[:len(prefix)] == prefix:\n+                image_id = image_id[len(prefix):]\n+            image_id = int(image_id)\n+            image = conn.getObject("Image", image_id)\n+\n+            if image is None:\n+                image_warning_id = \'Image-ID: {0}\'.format(image_id)\n+                if skip_failed:\n+                    warn(\n+                        \'Unable to find an image with this ID in the \'\n+                        \'database. Skipping download!\',\n+                        image_warning_id\n+                    )\n+                    continue\n+                raise ValueError(\n+                    \'{0}: Unable to find an image with this ID in the \'\n+                    \'database. Aborting!\'\n+                    .format(image_warning_id)\n+                )\n+\n+            image_name = os.path.splitext(image.getN'..b'\'\n+                    \'frame {1} instead.\'\n+                    .format(ori_frame, frame),\n+                    image_warning_id\n+                )\n+            # Get the channel index. If the index is not valid, skip the image\n+            if channel is None:\n+                channel_index = 0\n+                num_channels = image.getSizeC()\n+                if num_channels > 1:\n+                    warn(\n+                        \'No specific channel selected for multi-channel \'\n+                        \'image. Using first of {0} channels.\'\n+                        .format(num_channels),\n+                        image_warning_id\n+                    )\n+            else:\n+                channel_index = find_channel_index(image, channel)\n+                if channel_index == -1:\n+                    raise ValueError(\n+                        \'"{0}" is not a known channel name for image {1}\'\n+                        .format(channel, image.getName())\n+                    )\n+\n+            # download and save the region as TIFF\n+            fname = \'_\'.join(\n+                [image_name, str(image_id)] + [str(x) for x in tile]\n+            )\n+            download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname)\n+    finally:\n+        # Close the connection\n+        conn.close()\n+\n+\n+def _center_to_ul(center_x, center_y, width, height):\n+    if width > 0:\n+        ext_x = (width - 1) // 2\n+        ul_x = max([center_x - ext_x, 0])\n+        width = center_x + ext_x + 1 - ul_x\n+    else:\n+        ul_x = 0\n+    if height > 0:\n+        ext_y = (height - 1) // 2\n+        ul_y = max([center_y - ext_y, 0])\n+        height = center_y + ext_y + 1 - ul_y\n+    else:\n+        ul_y = 0\n+    return ul_x, ul_y, width, height\n+\n+\n+if __name__ == "__main__":\n+    p = argparse.ArgumentParser()\n+    p.add_argument(\n+        \'image_ids\', nargs=\'*\', default=[],\n+        help=\'one or more IDR image ids for which to retrieve data (default: \'\n+             \'read ids from stdin).\'\n+    )\n+    p.add_argument(\n+        \'-c\', \'--channel\',\n+        help=\'name of the channel to retrieve data for \'\n+             \'(note: the first channel of each image will be downloaded if \'\n+             \'left unspecified)\'\n+    )\n+    region = p.add_mutually_exclusive_group()\n+    region.add_argument(\n+        \'--rectangle\', nargs=4, type=int, default=argparse.SUPPRESS,\n+        help=\'specify a clipping region for the image as x y width height, \'\n+             \'where x and y give the upper left coordinate of the rectangle \'\n+             \'to clip to. Set width and height to 0 to extend the rectangle \'\n+             \'to the actual size of the image.\'\n+    )\n+    region.add_argument(\n+        \'--center\', nargs=4, type=int, default=argparse.SUPPRESS,\n+        help=\'specify a clipping region for the image as x y width height, \'\n+             \'where x and y define the center of a width x height rectangle. \'\n+             \'Set either width or height to 0 to extend the region to the \'\n+             \'actual size of the image along the x- or y-axis.\\n\'\n+             \'Note: Even values for width and height will be rounded down to \'\n+             \'the nearest odd number.\'\n+    )\n+    p.add_argument(\n+        \'-f\', \'--frame\', type=int, default=0\n+    )\n+    p.add_argument(\n+        \'-z\', \'--z-stack\', type=int, default=0\n+    )\n+    p.add_argument(\n+        \'--skip-failed\', action=\'store_true\'\n+    )\n+    args = p.parse_args()\n+    if not args.image_ids:\n+        args.image_ids = sys.stdin.read().split()\n+    if \'center\' in args:\n+        args.coord, args.width, args.height = (\n+            args.center[:2], args.center[2], args.center[3]\n+        )\n+        args.region_spec = \'center\'\n+        del args.center\n+    elif \'rectangle\' in args:\n+        args.coord, args.width, args.height = (\n+            args.rectangle[:2], args.rectangle[2], args.rectangle[3]\n+        )\n+        args.region_spec = \'rectangle\'\n+        del args.rectangle\n+    download_image_data(**vars(args))\n'
b
diff -r 000000000000 -r 57aa9597cd31 idr_download_by_ids.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/idr_download_by_ids.xml Sat Feb 08 13:24:39 2020 -0500
[
b'@@ -0,0 +1,257 @@\n+<?xml version="1.0"?>\n+<tool id="idr_download_by_ids" name="IDR Download" version="0.9" profile="18.09">\n+    <description>- download images from the Image Data Resource using image IDs</description>\n+    <macros>\n+        <xml name="region_spec" token_pos="upper-left corner">\n+            <param name="x_coord" type="integer" value="0" min="0"\n+            label="x-coordinate of region @POS@" />\n+            <param name="y_coord" type="integer" value="0" min="0"\n+            label="y-coordinate of region @POS@" />\n+            <param name="width" type="integer" value="0" min="0"\n+            label="Region width"\n+            help="Set to zero to extend the rectangle maximally along the x-axis of the image" />\n+            <param name="height" type="integer" value="0" min="0"\n+            label="Region height"\n+            help="Set to zero to extend the rectangle maximally along the y-axis of the image" />\n+        </xml>\n+    </macros>\n+    <requirements>\n+        <requirement type="package" version="5.6.0">python-omero</requirement>\n+        <requirement type="package" version="3.1.3">matplotlib</requirement>\n+    </requirements>\n+    <command detect_errors="exit_code"><![CDATA[\n+        mkdir downloads && cd downloads &&\n+        #if str($image_ids.source) == \'link\':\n+            python -c \'print("${image_ids.id_spec}".replace(",", "|").split("?show=")[-1].replace("|", "\\n"))\'\n+            ## https://idr.openmicroscopy.org/webclient/?show=image-3426274|image-3426275|image-3426276|image-3426277\n+        #else:\n+            cat \'${image_ids.id_spec}\'\n+        #end if\n+        | python \'$__tool_directory__/idr_download_by_ids.py\' \n+        #set $channel = str($channel).strip()\n+        #if $channel:\n+            -c \'$channel\'\n+        #end if\n+        -f $frame\n+        -z $z_section\n+        #if str($clip_image.select):\n+            ${clip_image.select} ${clip_image.x_coord} ${clip_image.y_coord} ${clip_image.width} ${clip_image.height}\n+        #end if\n+        $skip_failed\n+\n+        2> >(tee -a $out_log >&2)\n+    ]]></command>\n+<inputs>\n+        <conditional name="image_ids">\n+            <param name="source" type="select" label="How would you like to specify the IDs of images to download?">\n+                <option value="link">As text (comma-separated list of IDs or a valid IDR link)</option>\n+                <option value="dataset">As a dataset (one image ID per line)</option>\n+            </param>\n+            <when value="link">\n+                <param name="id_spec" type="text"\n+                label="Image IDs to download"\n+                help="You can enter a single image-id, or a comma (or \'|\')-separated list of IDs. Alternatively, you can paste here a link to an image selection obtained through the IDR webclient.">\n+                    <sanitizer>\n+                        <valid><add value="|" /></valid>\n+                    </sanitizer>\n+                </param>\n+            </when>\n+            <when value="dataset">\n+                <param name="id_spec" type="data" format="txt"\n+                label="Select a dataset with image IDs (one per line)" />\n+            </when>\n+        </conditional>\n+        <param name="channel" type="text"\n+        label="Name of the channel to download"\n+        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." />\n+        <param name="z_section" type="integer" value="0" min="0"\n+        label="z-plane of images to download" />\n+        <param name="frame" type="integer" value="0" min="0"\n+        label="Image frame to download" />\n+        <conditional name="clip_image">\n+            <param name="select" type="select"\n+            label="Limit the download to a selected region of the image?">\n+                <option value="">No, download the entire image plane</option>\n+                <option value="--rectangle">Specify a region using its upper-left '..b' </output_collection>\n+            <output name="out_log">\n+                <assert_contents>\n+                    <has_text text="(ID: 9036710): Specified image plane (4) is out of bounds"/>\n+                    <has_n_lines n="1"/>\n+                </assert_contents>\n+            </output>\n+        </test>\n+        <test expect_failure="true">\n+            <!-- Test behavior with non-existing image-ID 9036708999 -->\n+            <param name="source" value="link" />\n+            <param name="id_spec" value="https://idr.openmicroscopy.org/webclient/?show=image-9036708999|image-9036710|image-9036711" />\n+            <param name="channel" value="Spo20(51-91)" />\n+            <param name="z_section" value="4" />\n+            <conditional name="clip_image">\n+                <param name="select" value="" />\n+            </conditional>\n+            <param name="frame" value="20" />\n+        </test>\n+        <test>\n+            <!-- Repeat test with non-existing image-ID 9036708999,\n+            but use skip-failed option -->\n+            <param name="source" value="link" />\n+            <param name="id_spec" value="https://idr.openmicroscopy.org/webclient/?show=image-9036708999|image-9036710|image-9036711" />\n+            <param name="channel" value="Spo20(51-91)" />\n+            <param name="z_section" value="4" />\n+            <conditional name="clip_image">\n+                <param name="select" value="" />\n+            </conditional>\n+            <param name="frame" value="20" />\n+            <param name="skip_failed" value="true" />\n+            <output_collection name="output_file" type="list" count="2">\n+                <element name="171101_LeadingEdgeDeletionPSMMovies01_15_R3D_9036711_0_0_1024_1024">\n+                    <assert_contents>\n+                        <has_size value="4194510" />\n+                    </assert_contents>\n+                </element>\n+            </output_collection>\n+            <output name="out_log">\n+                <assert_contents>\n+                    <has_text text="9036708999: Unable to find an image with this ID in the database. Skipping download!" />\n+                    <has_text text="(ID: 9036710): Specified image plane (4) is out of bounds" />\n+                    <has_n_lines n="2"/>\n+                </assert_contents>\n+            </output>\n+        </test>\n+    </tests>\n+    <help><![CDATA[\n+Download image data from the IDR_ (Image Data Resource) - a public repository\n+of reference image datasets from published scientific studies.\n+\n+.. _IDR: https://idr.openmicroscopy.org/about/\n+\n+-----\n+\n+.. class:: infomark\n+\n+**Input**\n+\n+A set of image IDs as can be obtained from the IDR webclient_ like this:\n+\n+1. select the images you want to download\n+2. click on the \'Link\' button on the top right of the page and copy the provided\n+   URL\n+3. paste the URL into the input field of this tool, for example: ::\n+\n+    https://idr.openmicroscopy.org/webclient/?show=image-9036708|image-9036710|image-9036711\n+\n+Alternatively, you can simply provide a list of known image IDs in the text\n+input field (comma or \'|\'-separated ), or as an input file (each ID on a\n+separate line).\n+\n+Most images in the IDR have more than two dimensions. Thus, there are\n+parameters available which allow you to select a particular recording channel,\n+z-plane or time frame to download.\n+You can also select a region which will be used to crop the images, rather\n+than downloading the entire file.\n+\n+.. _webclient: https://idr.openmicroscopy.org/\n+\n+\n+-----\n+\n+.. class:: infomark\n+\n+**Output**\n+\n+Downloaded images will be saved in TIFF format. If you selected multiple image\n+IDs to download, these will be saved as elements of a collection.\n+\n+The accompanying error log dataset may hold valuable information about requests\n+that could not be fulfilled exactly as specified, or that were found to be\n+ambiguous.\n+    ]]></help>\n+    <citations>\n+        <citation type="doi">10.1038/nmeth.4326</citation>\n+    </citations>\n+</tool>\n'
b
diff -r 000000000000 -r 57aa9597cd31 test-data/ids.txt
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/ids.txt Sat Feb 08 13:24:39 2020 -0500
b
@@ -0,0 +1,1 @@
+1884807
b
diff -r 000000000000 -r 57aa9597cd31 test-data/test1.tiff
b
Binary file test-data/test1.tiff has changed