Mercurial > repos > ufz > omero_dataset_to_plate
comparison omero_get_id.py @ 0:5ad32d18fe82 draft
planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit 636cbb62d59819caca5bc9eab0a8ec31be5bdd46
author | ufz |
---|---|
date | Mon, 16 Dec 2024 20:56:16 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:5ad32d18fe82 |
---|---|
1 import argparse | |
2 import csv | |
3 import json | |
4 import sys | |
5 | |
6 import ezomero as ez | |
7 | |
8 | |
9 def get_ids_ezo(user, pws, host, port, final_obj_type, parent_obj_type, parent_id=None, tsv_file="id_list.tsv"): | |
10 | |
11 # Function to write tabular file from the ezomero output | |
12 def write_ids_to_tsv(data): | |
13 with open(tsv_file, 'w', newline='') as f: | |
14 writer = csv.writer(f, delimiter='\t') | |
15 for item in data: | |
16 writer.writerow([item]) # Write each ID | |
17 | |
18 with ez.connect(user, pws, "", host, port, secure=True) as conn: | |
19 | |
20 if final_obj_type == "Project": | |
21 proj_ids = ez.get_project_ids(conn) | |
22 write_ids_to_tsv(proj_ids) | |
23 return proj_ids | |
24 | |
25 elif final_obj_type == "Dataset": | |
26 args = {'project': None} | |
27 if parent_obj_type == "Project": | |
28 args['project'] = parent_id | |
29 ds_ids = ez.get_dataset_ids(conn, **args) | |
30 write_ids_to_tsv(ds_ids) | |
31 return ds_ids | |
32 | |
33 elif final_obj_type == "Image": | |
34 args = { | |
35 'project': None, | |
36 'dataset': None, | |
37 'plate': None, | |
38 'well': None | |
39 } | |
40 if parent_obj_type == "Project": | |
41 args['project'] = parent_id | |
42 elif parent_obj_type == "Dataset": | |
43 args['dataset'] = parent_id | |
44 elif parent_obj_type == "Plate": | |
45 args['plate'] = parent_id | |
46 elif parent_obj_type == "Well": | |
47 args['well'] = parent_id | |
48 elif parent_obj_type != "All": | |
49 raise ValueError("Object set as parent_obj_type is not compatible") | |
50 | |
51 ds_ims = ez.get_image_ids(conn, **args) | |
52 write_ids_to_tsv(ds_ims) | |
53 return ds_ims | |
54 | |
55 elif final_obj_type == "Annotation": | |
56 map_annot_ids = ez.get_map_annotation_ids(conn, parent_obj_type, parent_id) | |
57 write_ids_to_tsv(map_annot_ids) | |
58 return map_annot_ids | |
59 | |
60 elif final_obj_type == "Tag": | |
61 tag_ids = ez.get_tag_ids(conn, parent_obj_type, parent_id) | |
62 write_ids_to_tsv(tag_ids) | |
63 return tag_ids | |
64 | |
65 elif final_obj_type == "Roi": | |
66 roi_ids = ez.get_roi_ids(conn, parent_id) | |
67 write_ids_to_tsv(roi_ids) | |
68 return roi_ids | |
69 | |
70 elif final_obj_type == "Table": | |
71 file_ann_ids = ez.get_file_annotation_ids(conn, parent_obj_type, parent_id) | |
72 write_ids_to_tsv(file_ann_ids) | |
73 return file_ann_ids | |
74 | |
75 else: | |
76 sys.exit(f"Unsupported object type: {filter}") | |
77 | |
78 | |
79 # Argument parsing | |
80 if __name__ == "__main__": | |
81 parser = argparse.ArgumentParser(description="Fetch OMERO object IDs as TSV from parent object.") | |
82 parser.add_argument("--credential-file", dest="credential_file", type=str, | |
83 required=True, help="Credential file (JSON file with username and password for OMERO)") | |
84 parser.add_argument('--host', required=True, | |
85 help="Host server address.") | |
86 parser.add_argument('--port', required=True, type=int, | |
87 help='OMERO port') | |
88 parser.add_argument('--final_obj_type', required=True, | |
89 help="Type of object to fetch ID: Project, Dataset, Image, Annotation, Tag, Roi, or Table.") | |
90 parser.add_argument('--parent_obj_type', required=True, | |
91 help="Type of object from which you fetch IDs: Project, Dataset, Plate, Well, Image (or 'All' if you want to get all objects).") | |
92 parser.add_argument('--parent_id', required=False, type=int, | |
93 help="ID of the OMERO object in `--parent_obj_type`, not required if you used `--parent_obj_type All`.") | |
94 parser.add_argument('--tsv_file', default='id_list.tsv', | |
95 help="Output TSV file path.") | |
96 args = parser.parse_args() | |
97 | |
98 if args.parent_id is None and args.parent_obj_type != "All": | |
99 raise ValueError("ID is only optional is you use `--parent_obj_type All`") | |
100 | |
101 if args.final_obj_type == "Roi" and args.parent_obj_type != "Image": | |
102 raise ValueError("Roi IDs can only be retrived from images, use `--parent_obj_type Image`") | |
103 | |
104 if args.parent_obj_type == "All" and args.final_obj_type not in ["Image", "Dataset", "Project"]: | |
105 raise ValueError("Only Images, Datasets and Projects is compatible with `--parent_obj_type All`") | |
106 | |
107 with open(args.credential_file, 'r') as f: | |
108 crds = json.load(f) | |
109 | |
110 # Call the main function to get the object and save it as a TSV | |
111 get_ids_ezo(user=crds['username'], pws=crds['password'], host=args.host, | |
112 port=args.port, | |
113 final_obj_type=args.final_obj_type, | |
114 parent_obj_type=args.parent_obj_type, | |
115 parent_id=args.parent_id, | |
116 tsv_file=args.tsv_file) |