From a18ab9d2630768efec43cee744c5b6d8d85076be Mon Sep 17 00:00:00 2001 From: Lucille Delisle Date: Fri, 11 Oct 2024 12:04:51 +0200 Subject: [PATCH] convert omero_get to omero_get_id --- tools/omero/omero_get.py | 97 ------------------- tools/omero/omero_get.xml | 136 --------------------------- tools/omero/omero_get_id.py | 125 +++++++++++++++++++++++++ tools/omero/omero_get_id.xml | 174 +++++++++++++++++++++++++++++++++++ 4 files changed, 299 insertions(+), 233 deletions(-) delete mode 100644 tools/omero/omero_get.py delete mode 100644 tools/omero/omero_get.xml create mode 100644 tools/omero/omero_get_id.py create mode 100644 tools/omero/omero_get_id.xml diff --git a/tools/omero/omero_get.py b/tools/omero/omero_get.py deleted file mode 100644 index 5e0dd85d..00000000 --- a/tools/omero/omero_get.py +++ /dev/null @@ -1,97 +0,0 @@ -import argparse -import csv -import json - -import ezomero as ez - - -def get_object_ezo(user, pws, host, port, obj_type, id=None, tsv_file="id_list.tsv"): - # Function to write tabular file from the ezomero output - def write_ids_to_tsv(data, header): - with open(tsv_file, 'a+', newline='') as f: - f.seek(0) - is_empty = f.tell() == 0 # Check if file is empty - writer = csv.writer(f, delimiter='\t') - if is_empty: - writer.writerow([header]) # Write the header - for item in data: - writer.writerow([item]) # Write each ID - - # Function to write tabular file from a dictionary ezomero output - def write_dict_to_tsv(data, headers): - with open(tsv_file, 'a+', newline='') as f: - f.seek(0) - is_empty = f.tell() == 0 # Check if file is empty - writer = csv.writer(f, delimiter='\t') - if is_empty: - writer.writerow(headers) # Write the headers - for key, value in data.items(): - writer.writerow([key, value]) # Write each key-value pair - - try: - with ez.connect(user, pws, "", host, port, secure=True) as conn: - if obj_type == "dataset": - ds_ids = ez.get_dataset_ids(conn, project=int(id)) - write_ids_to_tsv(ds_ids, "Dataset IDs") - return ds_ids - elif obj_type == "image": - ds_ims = ez.get_image_ids(conn, dataset=int(id)) - write_ids_to_tsv(ds_ims, "Image IDs") - return ds_ims - elif obj_type == "annotation": - map_annot_ids = ez.get_map_annotation_ids(conn, "Image", int(id)) - ma_dict = {} - for maid in map_annot_ids: - current_ma_dict = ez.get_map_annotation(conn, maid) - ma_dict = {**ma_dict, **current_ma_dict} - write_dict_to_tsv(ma_dict, ["Annotation ID", "Annotation Value"]) - return ma_dict - elif obj_type == "project": - proj_ids = ez.get_project_ids(conn) - write_ids_to_tsv(proj_ids, "Project IDs") - return proj_ids - elif obj_type == "roi": - roi_ids = ez.get_roi_ids(conn, int(id)) - write_ids_to_tsv(roi_ids, "ROI IDs") - return roi_ids - elif obj_type == "table": - table = ez.get_table(conn, int(id)) - write_dict_to_tsv(table, ["Table ID", "Table Value"]) - return table - else: - raise ValueError(f"Unsupported object type: {obj_type}") - - except Exception as e: - print(f"Connection error: {str(e)}") - return None - - -# Argument parsing -if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Fetch and save data as TSV based on object type.") - parser.add_argument("--credential-file", dest="credential_file", type=str, - required=True, help="Credential file (JSON file with username and password for OMERO)") - parser.add_argument('--host', required=True, - help="Host server address.") - parser.add_argument('--port', required=True, type=int, - help='OMERO port') - parser.add_argument('--obj_type', required=True, - help="Type of object to fetch: dataset, image, annotation, project, roi, or table.") - parser.add_argument('--id', required=False, - help="ID of the OMERO object attached to this object (project id for dataset, dataset id for image, image id for annotation, roi, or table). This is ignored for project.") - parser.add_argument('--tsv_file', default='id_list.tsv', required=True, - help="Output TSV file path.") - args = parser.parse_args() - - if args.id is None and args.obj_type != "project": - raise ValueError(f"ID is required for {args.obj_type}") - - with open(args.credential_file, 'r') as f: - crds = json.load(f) - - # Call the main function to get the object and save it as a TSV - get_object_ezo(user=crds['username'], pws=crds['password'], host=args.host, - port=args.port, - obj_type=args.obj_type, - id=args.id, - tsv_file=args.tsv_file) diff --git a/tools/omero/omero_get.xml b/tools/omero/omero_get.xml deleted file mode 100644 index 8d86f192..00000000 --- a/tools/omero/omero_get.xml +++ /dev/null @@ -1,136 +0,0 @@ - - with ezomero - - 5.18.0 - 0 - - - omero - - - ezomero - - openjdk - - - - - - - - ^[a-zA-Z0-9._-]*$ - '..' not in value - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Description ------------ - -Tool to fetch project, dataset, images and ROIs IDs user defined OMERO.server. -Additionally, this tool fetch attachments and tables linked to an Image - -Options: -Project -> Project IDs present in the OMERO.server -Dataset -> Dataset IDs present in a specific Project -Image -> Image IDs present in a specific Dataset -Annotation -> Annotation File (Key-Value Pairs) linked to an Image -ROI -> ROI IDs linked to an Image -Table -> Table linked to an Image - - - - 10.1038/nmeth.1896 - - \ No newline at end of file diff --git a/tools/omero/omero_get_id.py b/tools/omero/omero_get_id.py new file mode 100644 index 00000000..af30883f --- /dev/null +++ b/tools/omero/omero_get_id.py @@ -0,0 +1,125 @@ +import argparse +import csv +import json + +import ezomero as ez + + +def get_ids_ezo(user, pws, host, port, final_obj_type, parent_obj_type, parent_id=None, tsv_file="id_list.tsv"): + # Function to write tabular file from the ezomero output + def write_ids_to_tsv(data, header): + with open(tsv_file, 'a+', newline='') as f: + f.seek(0) + is_empty = f.tell() == 0 # Check if file is empty + writer = csv.writer(f, delimiter='\t') + if is_empty: + writer.writerow([header]) # Write the header + for item in data: + writer.writerow([item]) # Write each ID + + # Function to write tabular file from a dictionary ezomero output + def write_dict_to_tsv(data, headers): + with open(tsv_file, 'a+', newline='') as f: + f.seek(0) + is_empty = f.tell() == 0 # Check if file is empty + writer = csv.writer(f, delimiter='\t') + if is_empty: + writer.writerow(headers) # Write the headers + for key, value in data.items(): + writer.writerow([key, value]) # Write each key-value pair + + try: + with ez.connect(user, pws, "", host, port, secure=True) as conn: + if final_obj_type == "Project": + proj_ids = ez.get_project_ids(conn) + write_ids_to_tsv(proj_ids, "Project IDs") + return proj_ids + elif final_obj_type == "Dataset": + args = {'project': None} + if parent_obj_type == "Project": + args['project'] = parent_id + ds_ids = ez.get_dataset_ids(conn, **args) + write_ids_to_tsv(ds_ids, "Dataset IDs") + return ds_ids + elif final_obj_type == "Image": + args = { + 'project': None, + 'dataset': None, + 'plate': None, + 'well': None + } + if parent_obj_type == "Project": + args['project'] = parent_id + elif parent_obj_type == "Dataset": + args['dataset'] = parent_id + elif parent_obj_type == "Plate": + args['plate'] = parent_id + elif parent_obj_type == "Well": + args['well'] = parent_id + elif parent_obj_type != "All": + raise ValueError(f"Object set as parent_obj_type is not compatible") + ds_ims = ez.get_image_ids(conn, **args) + write_ids_to_tsv(ds_ims, "Image IDs") + return ds_ims + elif final_obj_type == "Annotation": + map_annot_ids = ez.get_map_annotation_ids(conn, parent_obj_type, parent_id) + write_ids_to_tsv(map_annot_ids, "Annotation IDs") + return map_annot_ids + elif final_obj_type == "Tag": + tag_ids = ez.get_tag_ids(conn, parent_obj_type, parent_id) + write_ids_to_tsv(tag_ids, "Tag IDs") + return tag_ids + elif final_obj_type == "Roi": + roi_ids = ez.get_roi_ids(conn, parent_id) + write_ids_to_tsv(roi_ids, "ROI IDs") + return roi_ids + elif final_obj_type == "Table": + file_ann_ids = ez.get_file_annotation_ids(conn, parent_obj_type, parent_id) + write_ids_to_tsv(file_ann_ids, "Table IDs") + return file_ann_ids + else: + raise ValueError(f"Unsupported object type: {final_obj_type}") + + except Exception as e: + print(f"Connection error: {str(e)}") + return None + + +# Argument parsing +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Fetch OMERO object IDs as TSV from parent object.") + parser.add_argument("--credential-file", dest="credential_file", type=str, + required=True, help="Credential file (JSON file with username and password for OMERO)") + parser.add_argument('--host', required=True, + help="Host server address.") + parser.add_argument('--port', required=True, type=int, + help='OMERO port') + parser.add_argument('--final_obj_type', required=True, + help="Type of object to fetch ID: Project, Dataset, Image, Annotation, Tag, Roi, or Table.") + parser.add_argument('--parent_obj_type', required=True, + help="Type of object from which you fetch IDs: Project, Dataset, Plate, Well, Image (or 'All' if you want to get all objects).") + parser.add_argument('--parent_id', required=False, type=int, + help="ID of the OMERO object in `--parent_obj_type`, not required if you used `--parent_obj_type All`.") + parser.add_argument('--tsv_file', default='id_list.tsv', required=True, + help="Output TSV file path.") + args = parser.parse_args() + + if args.parent_id is None and args.parent_obj_type != "All": + raise ValueError(f"ID is only optional is you use `--parent_obj_type All`") + + if args.final_obj_type == "Roi" and args.parent_obj_type != "Image": + raise ValueError("Roi IDs can only be retrived from images, use `--parent_obj_type Image`") + + if args.parent_obj_type == "All" and not args.final_obj_type in ["Image", "Dataset", "Project"]: + raise ValueError("Only Images, Datasets and Projects is compatible with `--parent_obj_type All`") + + with open(args.credential_file, 'r') as f: + crds = json.load(f) + + # Call the main function to get the object and save it as a TSV + get_ids_ezo(user=crds['username'], pws=crds['password'], host=args.host, + port=args.port, + final_obj_type=args.final_obj_type, + parent_obj_type=args.parent_obj_type, + parent_id=args.parent_id, + tsv_file=args.tsv_file) diff --git a/tools/omero/omero_get_id.xml b/tools/omero/omero_get_id.xml new file mode 100644 index 00000000..85cd783b --- /dev/null +++ b/tools/omero/omero_get_id.xml @@ -0,0 +1,174 @@ + + with ezomero + + 5.18.0 + 0 + + + omero + + + ezomero + + openjdk + + + + + + + + ^[a-zA-Z0-9._-]*$ + '..' not in value + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Description +----------- + +Tool to fetch project, dataset, images, annotations, tags, table and ROIs IDs user defined OMERO.server. + +Options: +Project -> Project IDs present in the OMERO.server +Dataset -> Dataset IDs present in a specific Project or in the server +Image -> Image IDs present in a specific Dataset or Project or Well or Plate or in the server +Annotation (Key-Value Pairs) -> Annotation IDs linked to an Image or Dataset or Project or Well or Plate +Tag -> Tag IDs linked to an Image or Dataset or Project or Well or Plate +ROI -> ROI IDs linked to an Image +Table -> Table linked to an Image or Dataset or Project or Well or Plate + + + + 10.1038/nmeth.1896 + + \ No newline at end of file