Mercurial > repos > ufz > omero_roi_import
comparison omero_metadata_upload.py @ 0:9051b91545f6 draft
planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit 5b1b30d409355cee98485815c1dd4ac48649bcc1
author | ufz |
---|---|
date | Thu, 05 Sep 2024 11:55:44 +0000 |
parents | |
children | 4f4dc352d660 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9051b91545f6 |
---|---|
1 import argparse | |
2 from datetime import datetime | |
3 | |
4 import ezomero as ez | |
5 import pandas as pd | |
6 | |
7 | |
8 def metadata_import_ezo(user, pws, host, port, obj_type, did=None, ann_type="table", ann_file=None, an_name=None, | |
9 log_file='metadata_import_log.txt'): | |
10 def upload_metadata(conn, obj_type, did, data_dict, df, ann_type, an_name): | |
11 try: | |
12 if ann_type == "KV": | |
13 id_map_ann = ez.post_map_annotation(conn, obj_type, object_id=int(did), kv_dict=data_dict, ns=an_name) | |
14 ma_dict = ez.get_map_annotation(conn, id_map_ann) | |
15 return ma_dict | |
16 elif ann_type == "table": | |
17 id_tb_ann = ez.post_table(conn, df, object_type=obj_type, object_id=int(did), title=an_name, | |
18 headers=True) | |
19 tb_dict = ez.get_table(conn, id_tb_ann) | |
20 return tb_dict | |
21 except Exception as e: | |
22 log_error(f"Error uploading metadata for {obj_type} with ID {did}: {str(e)}") | |
23 return None | |
24 | |
25 def log_error(message): | |
26 with open(log_file, 'w') as f: | |
27 f.write(f"ERROR: {message}\n") | |
28 | |
29 def log_success(message): | |
30 with open(log_file, 'w') as f: | |
31 f.write(f"SUCCESS: {message}\n") | |
32 | |
33 try: | |
34 df = pd.read_csv(ann_file, delimiter='\t') | |
35 except FileNotFoundError as e: | |
36 log_error(f"Annotation file not found: {str(e)}") | |
37 return | |
38 | |
39 if ann_type == "table": | |
40 data_dict = df.to_dict(orient='records') | |
41 elif ann_type == "KV": | |
42 data_dict = {col: df[col].iloc[0] for col in df.columns} | |
43 | |
44 try: | |
45 with ez.connect(user, pws, "", host, port, secure=True) as conn: | |
46 if obj_type == "project": | |
47 if did is None: | |
48 did = ez.post_project(conn, project_name=str(datetime.now())) | |
49 result = upload_metadata(conn, "Project", did, data_dict, df, ann_type, an_name) | |
50 elif obj_type == "screen": | |
51 if did is None: | |
52 did = ez.post_screen(conn, screen_name=str(datetime.now())) | |
53 result = upload_metadata(conn, "Screen", did, data_dict, df, ann_type, an_name) | |
54 elif obj_type == "dataset": | |
55 if did is None: | |
56 did = ez.post_dataset(conn, dataset_name=str(datetime.now())) | |
57 result = upload_metadata(conn, "Dataset", did, data_dict, df, ann_type, an_name) | |
58 elif obj_type == "image": | |
59 result = upload_metadata(conn, "Image", did, data_dict, df, ann_type, an_name) | |
60 else: | |
61 raise ValueError("Unsupported object type provided: {}".format(obj_type)) | |
62 | |
63 if result is not None: | |
64 log_success(f"Successfully uploaded metadata for {obj_type} with ID {did}. Result: {result}") | |
65 else: | |
66 log_error(f"Failed to upload metadata for {obj_type} with ID {did}.") | |
67 | |
68 conn.close() | |
69 | |
70 except Exception as e: | |
71 log_error(f"Connection error: {str(e)}") | |
72 | |
73 | |
74 if __name__ == "__main__": | |
75 parser = argparse.ArgumentParser(description='Import metadata into OMERO.') | |
76 parser.add_argument('--user', required=True, help='OMERO username') | |
77 parser.add_argument('--pws', required=True, help='OMERO password') | |
78 parser.add_argument('--host', required=True, help='OMERO host') | |
79 parser.add_argument('--port', required=True, type=int, help='OMERO port') | |
80 parser.add_argument('--obj_type', required=True, choices=['project', 'screen', 'dataset', 'image'], | |
81 help='Type of OMERO object') | |
82 parser.add_argument('--did', type=int, help='ID of the object (if it exists)') | |
83 parser.add_argument('--ann_type', required=True, choices=['table', 'KV'], help='Annotation type') | |
84 parser.add_argument('--ann_file', required=True, help='Path to the annotation file') | |
85 parser.add_argument('--an_name', required=True, help='Namespace or title for the annotation') | |
86 parser.add_argument('--log_file', default='metadata_import_log.txt', help='Path to the log file') | |
87 | |
88 args = parser.parse_args() | |
89 | |
90 metadata_import_ezo(user=args.user, pws=args.pws, host=args.host, port=args.port, | |
91 obj_type=args.obj_type, did=args.did, ann_type=args.ann_type, | |
92 ann_file=args.ann_file, an_name=args.an_name, log_file=args.log_file) |