diff idr_download_by_ids.py @ 10:4aed70472589 draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/idr_download commit bb22e94226d3bcf241a6fe27e426b541a36a0815"
author iuc
date Wed, 24 Nov 2021 21:01:02 +0000
parents 97f70f7ed077
children cbd605a24336
line wrap: on
line diff
--- a/idr_download_by_ids.py	Fri Feb 26 20:12:11 2021 +0000
+++ b/idr_download_by_ids.py	Wed Nov 24 21:01:02 2021 +0000
@@ -110,7 +110,7 @@
 
 
 def download_image_data(
-    image_ids,
+    image_ids_or_dataset_id, dataset=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,14 +129,6 @@
                 omero_username = 'public'
                 omero_password = 'public'
 
-    # 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'
@@ -160,6 +152,47 @@
                 TemporaryDirectory()
             )
 
+        if dataset:
+            dataset_warning_id = 'Dataset-ID: {0}'.format(image_ids_or_dataset_id[0])
+            try:
+                dataset_id = int(image_ids_or_dataset_id[0])
+            except ValueError:
+                image_ids = None
+            else:
+                try:
+                    dataset = conn.getObject("Dataset", dataset_id)
+                except Exception as e:
+                    # respect skip_failed on unexpected errors
+                    if skip_failed:
+                        warn(str(e), dataset_warning_id, warn_skip=True)
+                    else:
+                        raise
+                else:
+                    image_ids = [image.id for image in dataset.listChildren()]
+
+            if image_ids is None:
+                if skip_failed:
+                    warn(
+                        'Unable to find a dataset with this ID in the '
+                        'database.',
+                        dataset_warning_id,
+                        warn_skip=True
+                    )
+                else:
+                    raise ValueError(
+                        '{0}: Unable to find a dataset with this ID in the '
+                        'database. Aborting!'
+                        .format(dataset_warning_id)
+                    )
+
+        else:
+            # 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_or_dataset_id
+            ]
         for image_id in image_ids:
             image_warning_id = 'Image-ID: {0}'.format(image_id)
             try:
@@ -330,8 +363,9 @@
 if __name__ == "__main__":
     p = argparse.ArgumentParser()
     p.add_argument(
-        'image_ids', nargs='*', default=[],
-        help='one or more IDR image ids for which to retrieve data (default: '
+        'image_ids_or_dataset_id', nargs='*', default=[],
+        help='one or more IDR image ids or a single dataset id'
+             'for which to retrieve data (default: '
              'read ids from stdin).'
     )
     p.add_argument(
@@ -378,9 +412,14 @@
     p.add_argument(
         '-cf', '--config-file', dest='config_file', default=None
     )
+    p.add_argument(
+        '--dataset', action='store_true'
+    )
     args = p.parse_args()
-    if not args.image_ids:
-        args.image_ids = sys.stdin.read().split()
+    if not args.image_ids_or_dataset_id:
+        args.image_ids_or_dataset_id = sys.stdin.read().split()
+    if args.dataset and len(args.image_ids_or_dataset_id) > 1:
+        warn("Multiple dataset ids provided. Only the first one will be used.")
     if 'center' in args:
         args.coord, args.width, args.height = (
             args.center[:2], args.center[2], args.center[3]