comparison omero_get_value.py @ 0:c23735b45a4a draft

planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit 19d84fd5a372f1428e3e5670144881a56e8af8b2
author ufz
date Tue, 22 Oct 2024 11:53:30 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c23735b45a4a
1 import argparse
2 import csv
3 import json
4 import sys
5
6 import ezomero as ez
7
8
9 def get_object_ezo(user, pws, host, port, obj_type, ids, tsv_file):
10 # Function to write tabular file from the ezomero output
11 def write_values_to_tsv(data, header):
12 with open(tsv_file, 'w', newline='') as f:
13 writer = csv.writer(f, delimiter='\t')
14 writer.writerow([header]) # Write the header
15 for item in data:
16 writer.writerow([item]) # Write each value
17
18 # Function to write tabular file from a dictionary ezomero output
19 def write_dict_to_tsv(data, headers):
20 with open(tsv_file, 'w', newline='') as f:
21 writer = csv.writer(f, delimiter='\t')
22 writer.writerow(headers) # Write the headers
23 for key, value in data.items():
24 writer.writerow([key, value]) # Write each key-value pair
25
26 # Function to write tabular file from list of list ezomero output
27 def write_table_to_tsv(data):
28 with open(tsv_file, 'w') as f:
29 for row in data:
30 f.write('\t'.join([str(val) for val in row]) + '\n')
31
32 with ez.connect(user, pws, "", host, port, secure=True) as conn:
33 if obj_type == "Annotation":
34 ma_dict = {}
35 for maid in ids:
36 current_ma_dict = ez.get_map_annotation(conn, maid)
37 ma_dict = {**ma_dict, **current_ma_dict}
38 write_dict_to_tsv(ma_dict, ["Annotation ID", "Annotation Value"])
39 return ma_dict
40 elif obj_type == "Tag":
41 tags = []
42 for tag_id in ids:
43 tags.append(ez.get_tag(conn, tag_id))
44 # Sort the tags for consistency:
45 tags.sort
46 write_values_to_tsv(tags, "Tags")
47 return tags
48 elif obj_type == "Table":
49 if len(ids) > 1:
50 raise ValueError("Only one table can be exported at a time")
51 table = ez.get_table(conn, ids[0])
52 write_table_to_tsv(table)
53 return table
54
55 else:
56 sys.exit(f"Unsupported object type: {filter}")
57
58
59 # Argument parsing
60 if __name__ == "__main__":
61 parser = argparse.ArgumentParser(description="Fetch and save data as TSV based on object type.")
62 parser.add_argument("--credential-file", dest="credential_file", type=str,
63 required=True, help="Credential file (JSON file with username and password for OMERO)")
64 parser.add_argument('--host', required=True,
65 help="Host server address.")
66 parser.add_argument('--port', required=True, type=int,
67 help='OMERO port')
68 parser.add_argument('--obj_type', required=True,
69 help="Type of object to fetch: Annotation, Table or Tag.")
70 group = parser.add_mutually_exclusive_group()
71 group.add_argument('--ids', nargs='+', type=int,
72 help="IDs of the OMERO objects.")
73 group.add_argument('--ids_path',
74 help="File with IDs of the OMERO objects (one per line).")
75 parser.add_argument('--tsv_file', default='id_list.tsv', required=True,
76 help="Output TSV file path.")
77 args = parser.parse_args()
78
79 if args.ids_path:
80 args.ids = []
81 with open(args.ids_path, 'r') as f:
82 for line in f:
83 try:
84 args.ids.append(int(line))
85 except ValueError:
86 print(f"{line.strip()} is not a valid ID.")
87 if len(args.ids) == 0:
88 raise ValueError("Cound not find a single ID in the file.")
89
90 with open(args.credential_file, 'r') as f:
91 crds = json.load(f)
92
93 # Call the main function to get the object and save it as a TSV
94 get_object_ezo(user=crds['username'], pws=crds['password'], host=args.host,
95 port=args.port,
96 obj_type=args.obj_type,
97 ids=args.ids,
98 tsv_file=args.tsv_file)