comparison omero_get_children_ids.py @ 0:b0876c73076b draft

planemo upload for repository https://github.com/lldelisle/tools-lldelisle/tree/master/tools/omero_get_children_ids commit 8ac44b0341c70ce330fc0f24712b6f9b59b14731
author lldelisle
date Fri, 22 Dec 2023 12:55:40 +0000
parents
children 82f2efb46200
comparison
equal deleted inserted replaced
-1:000000000000 0:b0876c73076b
1 import argparse
2 import json
3
4 from omero.gateway import BlitzGateway
5
6
7 def get_omero_credentials(config_file):
8 if config_file is None: # IDR connection
9 omero_username = "public"
10 omero_password = "public"
11 else: # other omero instance
12 with open(config_file) as f:
13 cfg = json.load(f)
14 omero_username = cfg["username"]
15 omero_password = cfg["password"]
16
17 if omero_username == "" or omero_password == "":
18 omero_username = "public"
19 omero_password = "public"
20 return (omero_username, omero_password)
21
22
23 def recursive_get_children_id(parent_object, final_object_type):
24 output = []
25 if parent_object.OMERO_CLASS == 'WellSample':
26 return [parent_object.getImage().id]
27 for children in parent_object.listChildren():
28 if children.OMERO_CLASS == final_object_type.title():
29 output.append(children.id)
30 else:
31 # We need to go one step further
32 output += recursive_get_children_id(children, final_object_type)
33 return output
34
35
36 def get_children_ids(parent_object_type,
37 omero_id,
38 final_object_type,
39 omero_username,
40 omero_password,
41 omero_host="idr.openmicroscopy.org",
42 omero_secured=False):
43 # Connect to omero:
44 with BlitzGateway(
45 omero_username, omero_password, host=omero_host, secure=omero_secured
46 ) as conn:
47 # Retrieve omero object
48 parent_object = conn.getObject(parent_object_type.title(), omero_id)
49 return recursive_get_children_id(parent_object, final_object_type)
50
51
52 if __name__ == "__main__":
53 p = argparse.ArgumentParser()
54 p.add_argument("-oh", "--omero-host", type=str,
55 default="idr.openmicroscopy.org")
56 p.add_argument("--omero-secured", action="store_true", default=True)
57 p.add_argument("-cf", "--config-file", dest="config_file",
58 default=None)
59 p.add_argument("--parent-object-type", dest="parent_object_type",
60 type=str, default=None, required=True)
61 p.add_argument("--omero-id", dest="omero_id",
62 type=int, default=None, required=True)
63 p.add_argument("--final-object-type", dest="final_object_type",
64 type=str, default=None, required=True)
65 p.add_argument("--output", type=str, default=None, required=True)
66 args = p.parse_args()
67 children_ids = get_children_ids(
68 args.parent_object_type,
69 args.omero_id,
70 args.final_object_type,
71 *get_omero_credentials(args.config_file),
72 omero_host=args.omero_host,
73 omero_secured=args.omero_secured,
74 )
75 with open(args.output, 'w') as fo:
76 fo.write('\n'.join([str(id) for id in children_ids]))
77 fo.write('\n')