changeset 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
files idr_download_by_ids.py idr_download_by_ids.xml test-data/ids_tar.txt
diffstat 3 files changed, 86 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/idr_download_by_ids.py	Wed Apr 22 11:43:18 2020 -0400
+++ b/idr_download_by_ids.py	Fri May 15 14:54:02 2020 -0400
@@ -1,8 +1,11 @@
 import argparse
 import os
 import sys
+import tarfile
 
 from libtiff import TIFF
+from PIL import Image
+from tempfile import TemporaryFile
 from omero.gateway import BlitzGateway  # noqa
 from omero.constants.namespaces import NSBULKANNOTATIONS  # noqa
 
@@ -92,7 +95,7 @@
     return t
 
 
-def download_plane_as_tiff(image, tile, z, c, t, fname):
+def get_image_array(image, tile, z, c, t):
     pixels = image.getPrimaryPixels()
     try:
         selection = pixels.getTile(theZ=z, theT=t, theC=c, tile=tile)
@@ -102,21 +105,14 @@
         warn('Could not download the requested region', warning)
         return
 
-    if fname[-5:] != '.tiff':
-        fname += '.tiff'
-    try:
-        fname = fname.replace(' ', '_')
-        tiff = TIFF.open(fname, mode='w')
-        tiff.write_image(selection)
-    finally:
-        tiff.close()
+    return selection
 
 
 def download_image_data(
     image_ids,
     channel=None, z_stack=0, frame=0,
     coord=(0, 0), width=0, height=0, region_spec='rectangle',
-    skip_failed=False
+    skip_failed=False, download_tar=False
 ):
     # basic argument sanity checks and adjustments
     prefix = 'image-'
@@ -138,6 +134,10 @@
                         secure=True)
     conn.connect()
 
+    if download_tar:
+        # create an archive file to write images to
+        archive = tarfile.open('images.tar', mode='w')
+
     try:
         for image_id in image_ids:
             image_warning_id = 'Image-ID: {0}'.format(image_id)
@@ -263,7 +263,32 @@
                 [image_name, str(image_id)] + [str(x) for x in tile]
             )
             try:
-                download_plane_as_tiff(image, tile, z_stack, channel_index, frame, fname)
+                if fname[-5:] != '.tiff':
+                    fname += '.tiff'
+
+                fname = fname.replace(' ', '_')
+
+                im_array = get_image_array(image, tile, z_stack, channel_index, frame)
+
+                # pack images into tarball
+                if download_tar:
+                    tar_img = Image.fromarray(im_array)
+                    # Use TemporaryFile() for intermediate storage of images.
+                    # TO DO: could this be improved by using
+                    # SpooledTemporaryFile with a suitable max_size?
+                    with TemporaryFile() as buf:
+                        tar_img.save(buf, format='TIFF')
+                        tarinfo = tarfile.TarInfo(name=fname)
+                        buf.seek(0, 2)
+                        tarinfo.size = buf.tell()
+                        buf.seek(0)
+                        archive.addfile(tarinfo=tarinfo, fileobj=buf)
+                else:  # save image as individual file
+                    try:
+                        tiff = TIFF.open(fname, mode='w')
+                        tiff.write_image(im_array)
+                    finally:
+                        tiff.close()
             except Exception as e:
                 if skip_failed:
                     # respect skip_failed on unexpected errors
@@ -271,7 +296,11 @@
                     continue
                 else:
                     raise
+
     finally:
+        # close the archive file,if it is created
+        if download_tar:
+            archive.close()
         # Close the connection
         conn.close()
 
@@ -331,6 +360,9 @@
     p.add_argument(
         '--skip-failed', action='store_true'
     )
+    p.add_argument(
+        '--download-tar', action='store_true'
+    )
     args = p.parse_args()
     if not args.image_ids:
         args.image_ids = sys.stdin.read().split()
--- a/idr_download_by_ids.xml	Wed Apr 22 11:43:18 2020 -0400
+++ b/idr_download_by_ids.xml	Fri May 15 14:54:02 2020 -0400
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<tool id="idr_download_by_ids" name="IDR Download" version="0.30" profile="18.09">
+<tool id="idr_download_by_ids" name="IDR Download" version="0.40" profile="18.09">
     <description>- download images from the Image Data Resource using image IDs</description>
     <macros>
         <xml name="region_spec" token_pos="upper-left corner">
@@ -38,6 +38,7 @@
             ${clip_image.select} ${clip_image.x_coord} ${clip_image.y_coord} ${clip_image.width} ${clip_image.height}
         #end if
         $skip_failed
+        $download_tar
 
         2> >(tee -a $out_log >&2)
     ]]></command>
@@ -86,12 +87,20 @@
         <param name="skip_failed" type="boolean" checked="false" truevalue="--skip-failed" falsevalue=""
         label="Skip failed retrievals?"
         help="By default the tool will fail with an error on the first non-retrievable image ID. Here, you can choose to skip non-retrievable image IDs and continue downloading the available ones instead. The error log will contain warnings about failed IDs in this case." />
+        <param name="download_tar" type="boolean" checked="true" truevalue="--download-tar" falsevalue=""
+        label="Download images in a tarball?"
+        help="By default, the tool will download a tarball containing individual images into your Galaxy history. This option is strongly recommended, especially for a large number of files. With this option set to No, you will download a collection of images, only suggested for a small number of files." >
+        </param>
     </inputs>
     <outputs>
         <data name="out_log" format="txt" label="${tool.name} error log" />
         <collection name="output_file" type="list">
             <discover_datasets pattern="__name_and_ext__" directory="downloads" />
+            <filter>not download_tar</filter>
         </collection>
+        <data name="output_tar" format="tar" from_work_dir="./downloads/images.tar" label ="Images tarball">
+            <filter>download_tar</filter>
+        </data> 
     </outputs>
     <tests>
         <test>
@@ -106,6 +115,7 @@
                 <param name="height" value="5" />
             </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" file="test1.tiff"/>
             </output_collection>
@@ -122,6 +132,7 @@
                 <param name="height" value="5" />
             </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" file="test1.tiff"/>
             </output_collection>
@@ -138,6 +149,7 @@
                 <param name="height" value="5" />
             </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" file="test1.tiff"/>
             </output_collection>
@@ -151,6 +163,7 @@
                 <param name="select" value="" />
             </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">
                     <assert_contents>
@@ -175,6 +188,7 @@
                 <param name="select" value="" />
             </conditional>
             <param name="frame" value="20" />
+            <param name="download_tar" value="false" />
         </test>
         <test>
             <!-- Repeat test with non-existing image-ID 9036708999,
@@ -188,6 +202,7 @@
             </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">
                 <element name="171101_LeadingEdgeDeletionPSMMovies01_15_R3D__9036711__0__0__1024__1024">
                     <assert_contents>
@@ -203,6 +218,31 @@
                 </assert_contents>
             </output>
         </test>
+        <test>
+            <!-- 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>
+            <param name="frame" value="0" />
+            <output name="output_tar">
+                    <assert_contents>
+                        <has_size value="1382400" />
+                        <has_archive_member path=".*/*__1828167__0__0__671__511.tiff" >
+                            <has_size value="685884" />
+                        </has_archive_member>
+                        <has_archive_member path=".*/*__1828658__0__0__671__511.tiff" >
+                            <has_size value="685884" />
+                        </has_archive_member>
+                    </assert_contents>
+            </output>
+        </test>
     </tests>
     <help><![CDATA[
 Download image data from the IDR_ (Image Data Resource) - a public repository
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/ids_tar.txt	Fri May 15 14:54:02 2020 -0400
@@ -0,0 +1,2 @@
+1828167
+1828658