Mercurial > repos > ufz > omero_metadata_import
changeset 0:352e9d4eaf70 draft
planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit b9d952c8874f75f738ca4121d7e5274a7946035a
author | ufz |
---|---|
date | Wed, 07 Aug 2024 12:54:19 +0000 |
parents | |
children | 588d6fa22fc4 |
files | README.md omero_metadata_import.xml omero_metadata_upload.py test-data/input1.tif test-data/input2.tif test-data/metadata.tsv test-data/omero_output.txt test-data/output_KV_import.txt test-data/output_table_import.txt test-data/output_target_import.txt |
diffstat | 10 files changed, 266 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Wed Aug 07 12:54:19 2024 +0000 @@ -0,0 +1,19 @@ +# OMERO import images + +## Set up user credentials on Galaxy to connect to other omero instance + +To enable users to set their credentials for this tool, +make sure the file `config/user_preferences_extra.yml` has the following section: + +``` + omero_account: + description: Your OMERO instance connection credentials + inputs: + - name: username + label: Username + type: text + required: False + - name: password + label: Password + type: password + required: False \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/omero_metadata_import.xml Wed Aug 07 12:54:19 2024 +0000 @@ -0,0 +1,147 @@ +<tool id="omero_metadata_import" name="OMERO Metadata Import" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" + profile="20.01" license="MIT"> + <description> with ezomero </description> + <macros> + <token name="@TOOL_VERSION@">5.18.0</token> + <token name="@VERSION_SUFFIX@">0</token> + </macros> + <xrefs> + <xref type="bio.tools">omero</xref> + </xrefs> + <requirements> + <requirement type="package" version="3.0.1">ezomero</requirement> + <requirement type="package" version="2.2.2">pandas</requirement> + <requirement type="package" version="21.0.2">openjdk</requirement> + </requirements> + <command detect_errors="exit_code"><![CDATA[ + python $__tool_directory__/omero_metadata_upload.py + --user $__user__.extra_preferences.get('omero|username', $test_username) + --pws $__user__.extra_preferences.get('omero|password', $test_password) + --host $omero_host + --port $omero_port + --obj_type $obj_type + --ann_type $ann_type + --ann_file $ann_file + --an_name $an_name + --log_file $log + #if $object_id_selection == "existing_object" + --did "$did" + #end if + ]]></command> + <inputs> + <param argument="omero_host" type="text" optional="false" label="OMERO host URL"/> + <param argument="omero_port" type="integer" optional="false" value="4064" label="OMERO port"/> + <param argument="obj_type" type="select" optional="true" label="Target Object Type"> + <option value="project">Project</option> + <option value="screen">Screen</option> + <option value="dataset">Dataset</option> + <option value="image">Image</option> + </param> + <conditional name="object_id"> + <param name="object_id_selection" type="select" label="Selection" help="Create a new OMERO object or target an existing one"> + <option value="new_object">Create new object</option> + <option value="existing_object">Target an existing object</option> + </param> + <when value="new_object"/> + <when value="existing_object"> + <param name="did" type="integer" value="" optional="false" label="Object ID"/> + </when> + </conditional> + <param argument="ann_type" type="select" optional="false" label="Annotation type" help="Select annotation format"> + <option value="table">Table</option> + <option value="KV">Key-Value Pairs</option> + </param> + <param argument="ann_file" type="data" format="tabular" optional="false" label="Annotation file" help="Select annotation file"/> + <param argument="an_name" type="text" optional="false" label="Annotation Name"/> + <param name="test_username" type="hidden" value=""/> + <param name="test_password" type="hidden" value=""/> + </inputs> + <outputs> + <data name="log" format="txt"/> + </outputs> + <tests> + <test> + <param name="omero_host" value="host.docker.internal"/> + <param name="omero_port" value="6064"/> + <param name="obj_type" value="project"/> + <param name="ann_type" value="table"/> + <param name="object_id_selection" value="new_object"/> + <param name="ann_file" value="metadata.tsv"/> + <param name="an_name" value="Table_Test"/> + <param name="test_username" value="root"/> + <param name="test_password" value="omero"/> + <output name="log" value="output_table_import.txt" ftype="txt"> + <assert_contents> + <has_text text="SUCCESS: Successfully uploaded metadata for project"/> + </assert_contents> + </output> + </test> + <test> + <param name="omero_host" value="host.docker.internal"/> + <param name="omero_port" value="6064"/> + <param name="obj_type" value="dataset"/> + <param name="ann_type" value="KV"/> + <param name="object_id_selection" value="new_object"/> + <param name="ann_file" value="metadata.tsv"/> + <param name="an_name" value="KV_Test"/> + <param name="test_username" value="root"/> + <param name="test_password" value="omero"/> + <output name="log" value="output_KV_import.txt" ftype="txt"> + <assert_contents> + <has_text text="SUCCESS: Successfully uploaded metadata for dataset"/> + </assert_contents> + </output> + </test> + <test> + <param name="omero_host" value="host.docker.internal"/> + <param name="omero_port" value="6064"/> + <param name="obj_type" value="dataset"/> + <param name="ann_type" value="KV"/> + <param name="object_id_selection" value="existing_object"/> + <param name="did" value="1"/> + <param name="ann_file" value="metadata.tsv"/> + <param name="an_name" value="Additional_KV_Values"/> + <param name="test_username" value="root"/> + <param name="test_password" value="omero"/> + <output name="log" value="output_target_import.txt" ftype="txt"> + <assert_contents> + <has_text text="SUCCESS: Successfully uploaded metadata for dataset"/> + </assert_contents> + </output> + </test> + </tests> + <help> +Description +----------- + +Tool to import metadata (Table and K/V pairs) into user defined OMERO.server. +Metadata can be attached to an existing OMERO object (Project, Dataset, Screen, or Image). +Optionally, a new OMERO object can be created before uploading the metadata. +If the object does not exist, a new one will be created. + +**Tabular metadata file example** + +- Table + + +------------+---------------+---------------+ + | image_id | attribute_1 | attribute_2 | + +============+===============+===============+ + | 235 | 0 | 1 | + +------------+---------------+---------------+ + | 564 | 1 | 1 | + +------------+---------------+---------------+ + | 120 | 0 | 1 | + +------------+---------------+---------------+ + +- Key-Value Pairs + + +--------+---------+----------+ + | Key1 | Key2 | Key3 | + +========+=========+==========+ + | Value1 | Value2 | Value3 | + +--------+---------+----------+ + </help> + <citations> + <citation type="doi">10.1038/nmeth.1896</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/omero_metadata_upload.py Wed Aug 07 12:54:19 2024 +0000 @@ -0,0 +1,92 @@ +import argparse +from datetime import datetime + +import ezomero as ez +import pandas as pd + + +def metadata_import_ezo(user, pws, host, port, obj_type, did=None, ann_type="table", ann_file=None, an_name=None, + log_file='metadata_import_log.txt'): + def upload_metadata(conn, obj_type, did, data_dict, df, ann_type, an_name): + try: + if ann_type == "KV": + id_map_ann = ez.post_map_annotation(conn, obj_type, object_id=int(did), kv_dict=data_dict, ns=an_name) + ma_dict = ez.get_map_annotation(conn, id_map_ann) + return ma_dict + elif ann_type == "table": + id_tb_ann = ez.post_table(conn, df, object_type=obj_type, object_id=int(did), title=an_name, + headers=True) + tb_dict = ez.get_table(conn, id_tb_ann) + return tb_dict + except Exception as e: + log_error(f"Error uploading metadata for {obj_type} with ID {did}: {str(e)}") + return None + + def log_error(message): + with open(log_file, 'w') as f: + f.write(f"ERROR: {message}\n") + + def log_success(message): + with open(log_file, 'w') as f: + f.write(f"SUCCESS: {message}\n") + + try: + df = pd.read_csv(ann_file, delimiter='\t') + except FileNotFoundError as e: + log_error(f"Annotation file not found: {str(e)}") + return + + if ann_type == "table": + data_dict = df.to_dict(orient='records') + elif ann_type == "KV": + data_dict = {col: df[col].iloc[0] for col in df.columns} + + try: + with ez.connect(user, pws, "", host, port, secure=True) as conn: + if obj_type == "project": + if did is None: + did = ez.post_project(conn, project_name=str(datetime.now())) + result = upload_metadata(conn, "Project", did, data_dict, df, ann_type, an_name) + elif obj_type == "screen": + if did is None: + did = ez.post_screen(conn, screen_name=str(datetime.now())) + result = upload_metadata(conn, "Screen", did, data_dict, df, ann_type, an_name) + elif obj_type == "dataset": + if did is None: + did = ez.post_dataset(conn, dataset_name=str(datetime.now())) + result = upload_metadata(conn, "Dataset", did, data_dict, df, ann_type, an_name) + elif obj_type == "image": + result = upload_metadata(conn, "Image", did, data_dict, df, ann_type, an_name) + else: + raise ValueError("Unsupported object type provided: {}".format(obj_type)) + + if result is not None: + log_success(f"Successfully uploaded metadata for {obj_type} with ID {did}. Result: {result}") + else: + log_error(f"Failed to upload metadata for {obj_type} with ID {did}.") + + conn.close() + + except Exception as e: + log_error(f"Connection error: {str(e)}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Import metadata into OMERO.') + parser.add_argument('--user', required=True, help='OMERO username') + parser.add_argument('--pws', required=True, help='OMERO password') + parser.add_argument('--host', required=True, help='OMERO host') + parser.add_argument('--port', required=True, type=int, help='OMERO port') + parser.add_argument('--obj_type', required=True, choices=['project', 'screen', 'dataset', 'image'], + help='Type of OMERO object') + parser.add_argument('--did', type=int, help='ID of the object (if it exists)') + parser.add_argument('--ann_type', required=True, choices=['table', 'KV'], help='Annotation type') + parser.add_argument('--ann_file', required=True, help='Path to the annotation file') + parser.add_argument('--an_name', required=True, help='Namespace or title for the annotation') + parser.add_argument('--log_file', default='metadata_import_log.txt', help='Path to the log file') + + args = parser.parse_args() + + metadata_import_ezo(user=args.user, pws=args.pws, host=args.host, port=args.port, + obj_type=args.obj_type, did=args.did, ann_type=args.ann_type, + ann_file=args.ann_file, an_name=args.an_name, log_file=args.log_file)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/metadata.tsv Wed Aug 07 12:54:19 2024 +0000 @@ -0,0 +1,2 @@ +Key1 Key2 +Value1 Value2 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/omero_output.txt Wed Aug 07 12:54:19 2024 +0000 @@ -0,0 +1,2 @@ +Image:1 +Image:2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/output_KV_import.txt Wed Aug 07 12:54:19 2024 +0000 @@ -0,0 +1,1 @@ +SUCCESS: Successfully uploaded metadata for dataset with ID 1. Result: {'Key1': 'Value1', 'Key2': 'Value2'}