Mercurial > repos > ufz > omero_filter
comparison omero_filter.py @ 0:8a3324018fc5 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:54:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a3324018fc5 |
---|---|
1 import argparse | |
2 import csv | |
3 import json | |
4 import sys | |
5 | |
6 import ezomero as ez | |
7 | |
8 | |
9 def filter_ids_ezo(user, pws, host, port, filter, id, value1, value2=None, tsv_file="filter_list.tsv"): | |
10 | |
11 # Transform the id input in a list of integer | |
12 id = id.split(',') | |
13 id = list(map(int, id)) | |
14 | |
15 # Function to write tabular file from the ezomero output | |
16 def write_ids_to_tsv(data): | |
17 with open(tsv_file, 'w', newline='') as f: | |
18 writer = csv.writer(f, delimiter='\t') | |
19 for item in data: | |
20 writer.writerow([item]) # Write each ID | |
21 | |
22 with ez.connect(user, pws, "", host, port, secure=True) as conn: | |
23 | |
24 if filter == "filename": | |
25 fn_ids = ez.filter_by_filename(conn, id, value1) | |
26 write_ids_to_tsv(fn_ids) | |
27 return fn_ids | |
28 | |
29 elif filter == "KP": | |
30 kp_ims = ez.filter_by_kv(conn, id, value1, value2) | |
31 write_ids_to_tsv(kp_ims) | |
32 return kp_ims | |
33 | |
34 elif filter == "tag": | |
35 tg_dict = ez.filter_by_tag_value(conn, id, value1) | |
36 write_ids_to_tsv(tg_dict) | |
37 return tg_dict | |
38 | |
39 else: | |
40 sys.exit(f"Unsupported object type: {filter}") | |
41 | |
42 | |
43 # Argument parsing | |
44 if __name__ == "__main__": | |
45 parser = argparse.ArgumentParser(description="Fetch and save data as TSV based on object type.") | |
46 parser.add_argument("--credential-file", dest="credential_file", type=str, required=True, | |
47 help="Credential file (JSON file with username and password for OMERO)") | |
48 parser.add_argument('--host', required=True, | |
49 help="Host server address.") | |
50 parser.add_argument('--port', required=True, type=int, | |
51 help='OMERO port') | |
52 parser.add_argument('--filter', required=True, | |
53 help="Filter type - Filename, Key-Value Pairs, Tag") | |
54 parser.add_argument('--id', required=True, | |
55 help="List of images IDs") | |
56 parser.add_argument('--value1', required=True, | |
57 help="First searching values - Filename, Key, Tag") | |
58 parser.add_argument('--value2', required=False, | |
59 help="Second searching values - Value (necessary just for Key-Value Pairs filter") | |
60 parser.add_argument('--tsv_file', default='filter_list.tsv', | |
61 help="Output TSV file path.") | |
62 args = parser.parse_args() | |
63 | |
64 if args.filter == "KP" and args.value2 is None: | |
65 raise ValueError("'--value 2' is necessary to retrieve KP") | |
66 | |
67 with open(args.credential_file, 'r') as f: | |
68 crds = json.load(f) | |
69 | |
70 # Call the main function to get the object and save it as a TSV | |
71 filter_ids_ezo(user=crds['username'], pws=crds['password'], host=args.host, | |
72 port=args.port, | |
73 filter=args.filter, | |
74 value1=args.value1, | |
75 value2=args.value2, | |
76 id=args.id, | |
77 tsv_file=args.tsv_file) |