comparison omero_get_value.py @ 3:2cf36947bcb6 draft default tip

planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit 6f47dff3775455a8c45d39a3863a2306cd942443
author ufz
date Tue, 08 Apr 2025 15:12:35 +0000
parents c23735b45a4a
children
comparison
equal deleted inserted replaced
2:c7b2650718fe 3:2cf36947bcb6
1 import argparse 1 import argparse
2 import csv 2 import csv
3 import json 3 import json
4 import os
4 import sys 5 import sys
5 6
6 import ezomero as ez 7 import ezomero as ez
8 import pandas as pd
7 9
8 10
9 def get_object_ezo(user, pws, host, port, obj_type, ids, tsv_file): 11 def get_object_ezo(user, pws, host, port, obj_type, ids, out_dir):
10 # Function to write tabular file from the ezomero output 12 # Function to write tabular file from the ezomero output
11 def write_values_to_tsv(data, header): 13 def write_values_to_tsv(data, header):
12 with open(tsv_file, 'w', newline='') as f: 14 with open("output.tsv", 'w', newline='') as f:
13 writer = csv.writer(f, delimiter='\t') 15 writer = csv.writer(f, delimiter='\t')
14 writer.writerow([header]) # Write the header 16 writer.writerow([header]) # Write the header
15 for item in data: 17 for item in data:
16 writer.writerow([item]) # Write each value 18 writer.writerow([item]) # Write each value
17 19
18 # Function to write tabular file from a dictionary ezomero output 20 # Function to write tabular file from a dictionary ezomero output
19 def write_dict_to_tsv(data, headers): 21 def write_dict_to_tsv(data, headers):
20 with open(tsv_file, 'w', newline='') as f: 22 with open("output.tsv", 'w', newline='') as f:
21 writer = csv.writer(f, delimiter='\t') 23 writer = csv.writer(f, delimiter='\t')
22 writer.writerow(headers) # Write the headers 24 writer.writerow(headers) # Write the headers
23 for key, value in data.items(): 25 for key, value in data.items():
24 writer.writerow([key, value]) # Write each key-value pair 26 writer.writerow([key, value]) # Write each key-value pair
25 27
26 # Function to write tabular file from list of list ezomero output 28 # Function to write tabular file from list of list ezomero output
27 def write_table_to_tsv(data): 29 def write_table_to_tsv(data, id):
28 with open(tsv_file, 'w') as f: 30 with open(f"./output/ID_{id}_table.tsv", 'w') as f:
29 for row in data: 31 for row in data:
30 f.write('\t'.join([str(val) for val in row]) + '\n') 32 f.write('\t'.join([str(val) for val in row]) + '\n')
31 33
32 with ez.connect(user, pws, "", host, port, secure=True) as conn: 34 with ez.connect(user, pws, "", host, port, secure=True) as conn:
33 if obj_type == "Annotation": 35 if obj_type == "Annotation":
34 ma_dict = {} 36 ma_dict = {}
35 for maid in ids: 37 for maid in ids:
36 current_ma_dict = ez.get_map_annotation(conn, maid) 38 current_ma_dict = ez.get_map_annotation(conn, maid)
37 ma_dict = {**ma_dict, **current_ma_dict} 39 ma_dict = {**ma_dict, **current_ma_dict}
40 print(ma_dict)
38 write_dict_to_tsv(ma_dict, ["Annotation ID", "Annotation Value"]) 41 write_dict_to_tsv(ma_dict, ["Annotation ID", "Annotation Value"])
39 return ma_dict
40 elif obj_type == "Tag": 42 elif obj_type == "Tag":
41 tags = [] 43 tags = []
42 for tag_id in ids: 44 for tag_id in ids:
43 tags.append(ez.get_tag(conn, tag_id)) 45 tags.append(ez.get_tag(conn, tag_id))
44 # Sort the tags for consistency: 46 # Sort the tags for consistency:
45 tags.sort 47 tags.sort
46 write_values_to_tsv(tags, "Tags") 48 write_values_to_tsv(tags, "Tags")
47 return tags
48 elif obj_type == "Table": 49 elif obj_type == "Table":
49 if len(ids) > 1: 50 for id in ids:
50 raise ValueError("Only one table can be exported at a time") 51 table = ez.get_table(conn, id)
51 table = ez.get_table(conn, ids[0]) 52 print(table)
52 write_table_to_tsv(table) 53 write_table_to_tsv(table, id)
53 return table 54 elif obj_type == ("Attachment"):
54 55 for id in ids:
56 attch_path = ez.get_file_annotation(conn, id, folder_path='./output/')
57 base_name = os.path.basename(attch_path)
58 df = pd.read_csv(attch_path, sep='\t')
59 df.to_csv(f"./output/ID_{id}_{base_name}", sep='\t', index=False)
60 os.remove(attch_path)
55 else: 61 else:
56 sys.exit(f"Unsupported object type: {filter}") 62 sys.exit(f"Unsupported object type: {filter}")
57 63
58 64
59 # Argument parsing 65 # Argument parsing
70 group = parser.add_mutually_exclusive_group() 76 group = parser.add_mutually_exclusive_group()
71 group.add_argument('--ids', nargs='+', type=int, 77 group.add_argument('--ids', nargs='+', type=int,
72 help="IDs of the OMERO objects.") 78 help="IDs of the OMERO objects.")
73 group.add_argument('--ids_path', 79 group.add_argument('--ids_path',
74 help="File with IDs of the OMERO objects (one per line).") 80 help="File with IDs of the OMERO objects (one per line).")
75 parser.add_argument('--tsv_file', default='id_list.tsv', required=True, 81 parser.add_argument('--out_dir', required=True,
76 help="Output TSV file path.") 82 help="Output path.")
77 args = parser.parse_args() 83 args = parser.parse_args()
78 84
79 if args.ids_path: 85 if args.ids_path:
80 args.ids = [] 86 args.ids = []
81 with open(args.ids_path, 'r') as f: 87 with open(args.ids_path, 'r') as f:
93 # Call the main function to get the object and save it as a TSV 99 # 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, 100 get_object_ezo(user=crds['username'], pws=crds['password'], host=args.host,
95 port=args.port, 101 port=args.port,
96 obj_type=args.obj_type, 102 obj_type=args.obj_type,
97 ids=args.ids, 103 ids=args.ids,
98 tsv_file=args.tsv_file) 104 out_dir=args.out_dir)