diff --git a/tools/omero/omero_get.xml b/tools/omero/omero_get.xml deleted file mode 100644 index b21a0cca..00000000 --- a/tools/omero/omero_get.xml +++ /dev/null @@ -1,118 +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..fc9cde96 --- /dev/null +++ b/tools/omero/omero_get_id.py @@ -0,0 +1,113 @@ +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 + 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("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("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 args.final_obj_type not 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..823f07fe --- /dev/null +++ b/tools/omero/omero_get_id.xml @@ -0,0 +1,199 @@ + + 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 diff --git a/tools/omero/omero_get.py b/tools/omero/omero_get_value.py similarity index 55% rename from tools/omero/omero_get.py rename to tools/omero/omero_get_value.py index c8cc484d..597c830a 100644 --- a/tools/omero/omero_get.py +++ b/tools/omero/omero_get_value.py @@ -5,9 +5,9 @@ import ezomero as ez -def get_object_ezo(user, pws, host, port, obj_type, id=None, tsv_file="id_list.tsv"): +def get_object_ezo(user, pws, host, port, obj_type, ids, tsv_file): # Function to write tabular file from the ezomero output - def write_ids_to_tsv(data, header): + def write_values_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 @@ -15,7 +15,7 @@ def write_ids_to_tsv(data, header): if is_empty: writer.writerow([header]) # Write the header for item in data: - writer.writerow([item]) # Write each ID + writer.writerow([item]) # Write each value # Function to write tabular file from a dictionary ezomero output def write_dict_to_tsv(data, headers): @@ -28,31 +28,34 @@ def write_dict_to_tsv(data, headers): for key, value in data.items(): writer.writerow([key, value]) # Write each key-value pair + # Function to write tabular file from list of list ezomero output + def write_table_to_tsv(data): + with open(tsv_file, 'w') as f: + for row in data: + f.write('\t'.join([str(val) for val in row]) + '\n') + 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": - ma_dict = ez.get_map_annotation(conn, int(id)) + if obj_type == "Annotation": + ma_dict = {} + for maid in 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"]) + elif obj_type == "Tag": + tags = [] + for tag_id in ids: + tags.append(ez.get_tag(conn, tag_id)) + # Sort the tags for consistency: + tags.sort + write_values_to_tsv(tags, "Tags") + return tags + elif obj_type == "Table": + if len(ids) > 1: + raise ValueError("Only one table can be exported at a time") + table = ez.get_table(conn, ids[0]) + write_table_to_tsv(table) return table else: raise ValueError(f"Unsupported object type: {obj_type}") @@ -72,13 +75,27 @@ def write_dict_to_tsv(data, headers): 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 specific OMERO object.") + help="Type of object to fetch: Annotation, Table or Tag.") + group = parser.add_mutually_exclusive_group() + group.add_argument('--ids', nargs='+', type=int, + help="IDs of the OMERO objects.") + group.add_argument('--ids_path', + help="File with IDs of the OMERO objects (one per line).") parser.add_argument('--tsv_file', default='id_list.tsv', required=True, help="Output TSV file path.") args = parser.parse_args() + if args.ids_path: + args.ids = [] + with open(args.ids_path, 'r') as f: + for line in f: + try: + args.ids.append(int(line)) + except ValueError: + print(f"{line.strip()} is not a valid ID.") + if len(args.ids) == 0: + raise ValueError("Cound not find a single ID in the file.") + with open(args.credential_file, 'r') as f: crds = json.load(f) @@ -86,5 +103,5 @@ def write_dict_to_tsv(data, headers): get_object_ezo(user=crds['username'], pws=crds['password'], host=args.host, port=args.port, obj_type=args.obj_type, - id=args.id, + ids=args.ids, tsv_file=args.tsv_file) diff --git a/tools/omero/omero_get_value.xml b/tools/omero/omero_get_value.xml new file mode 100644 index 00000000..991ff95c --- /dev/null +++ b/tools/omero/omero_get_value.xml @@ -0,0 +1,99 @@ + + with ezomero + + 5.18.0 + 0 + + + omero + + + ezomero + + openjdk + + + + + + + + ^[a-zA-Z0-9._-]*$ + '..' not in value + + + + + + + + + + + + + + + [0-9]+(,[0-9]+)* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Description +----------- + +Tool to fetch Annotation, Tag and Tables from IDs. + +The IDs can be obtained with the tool OMERO get IDs with ezomero + + + + 10.1038/nmeth.1896 + + \ No newline at end of file