changeset 0:9051b91545f6 draft

planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit 5b1b30d409355cee98485815c1dd4ac48649bcc1
author ufz
date Thu, 05 Sep 2024 11:55:44 +0000
parents
children e957793051a8
files README.md omero_metadata_upload.py omero_roi_import.xml omero_roi_upload.py test-data/input1.tif test-data/input2.tif test-data/input_roi.tsv test-data/metadata.tsv test-data/omero_output.txt test-data/output_KV_import.txt test-data/output_table_import.txt test-data/output_table_roi.txt test-data/output_target_import.txt
diffstat 13 files changed, 415 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Thu Sep 05 11:55:44 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_upload.py	Thu Sep 05 11:55:44 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/omero_roi_import.xml	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,109 @@
+<tool id="omero_roi_import" name="OMERO ROI 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@">1</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>
+        <!-- openjdk is needed: https://github.com/conda-forge/omero-py-feedstock/pull/16 -->
+        <requirement type="package" version="21.0.2">openjdk</requirement>
+    </requirements>
+    <command detect_errors="exit_code"><![CDATA[
+        python '$__tool_directory__/omero_roi_upload.py'  
+        --input_file '$input'  
+        --image_id $id  
+        --user '$__user__.extra_preferences.get("omero_account|username", $test_username)'  
+        --psw '$__user__.extra_preferences.get("omero_account|password", $test_password)'  
+        --host '$omero_host'  
+        --port $omero_port  
+        --log_file '$log'  
+    ]]></command>
+    <inputs>
+        <param argument="input" type="data" format="tabular" optional="false" label="Tab File with ROIs" help="Select ROIs Tabular file"/>
+        <param argument="id" type="integer" optional="false" label="Image ID where annotate the ROIs"/>
+        <param name="omero_host" type="text" label="OMERO host URL">
+            <validator type="regex" message="Enter a valid host location, for example, your.omero.server">^[a-zA-Z0-9._-]*$</validator>
+            <validator type="expression" message="No two dots (..) allowed">'..' not in value</validator>
+        </param>
+        <param argument="omero_port" type="integer" optional="false" value="4064" label="OMERO port"/>
+        <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="id" value="1"/>
+            <param name="input" value="input_roi.tsv"/>
+            <param name="test_username" value="root"/>
+            <param name="test_password" value="omero"/>
+            <output name="log" value="output_table_roi.txt" ftype="txt">
+                <assert_contents>
+                    <has_text text="ROI ID: 7 for row 7"/>
+                </assert_contents>
+            </output>
+        </test>
+    </tests>
+    <help>
+
+Description
+-----------
+
+Tool to upload Regions of Interest (ROIs) to an OMERO server based on shape data provided
+in a tabular format (TSV file). The tool reads the shape information from the TSV file, creates the
+corresponding ROIs in OMERO, and links them to a specified image.
+
+**Column Headers**:
+
+        - shape, x, y, x_rad, y_rad, width, height, label, fontSize, x1, y1, x2, y2, points, fill_color, stroke_color, stroke_width, z, c, t, roi_name, roi_description
+
+- *Shape Type*:
+
+    The **shape** column indicates the type of shape being defined, such as Ellipse, Label, Line, Point, Polygon, Polyline, or Rectangle.
+
+- *Position and Dimensions*:
+
+    The columns **x**, **y**, **x_rad**, **y_rad**, **width**, and **height** specify the position and dimensions of the shapes, where applicable. For example, Ellipse uses x, y, x_rad, and y_rad, while Rectangle uses x, y, width, and height.
+
+- *Text Labels*:
+
+    The **label** and **fontSize** columns are used for the Label shape, specifying the text content and font size.
+
+- *Line Coordinates*:
+
+    The columns **x1**, **y1**, **x2**, and **y2** are used for defining the start and end points of a Line.
+
+- *Point Coordinates*:
+
+    The **points** column is used for defining multiple points in shapes like Polygon and Polyline. The points are listed as coordinate pairs.
+
+- *Colors*:
+
+    The **fill_color** and **stroke_color** columns define the fill and stroke colors of the shapes in RGBA format.
+
+- *Stroke Width*:
+
+    The **stroke_width** column specifies the width of the stroke or border around the shapes.
+
+- *Z, C, T Coordinates*:
+
+    The **z**, **c**, and **t** columns indicate the Z-plane, channel, and time point to which the shape is associated in the image stack.
+
+- *ROI Identification*:
+
+    The **roi_name** and **roi_description** columns provide a name and description for each ROI, allowing for easy identification and documentation within OMERO.
+
+    </help>
+    <citations>
+        <citation type="doi">10.1038/nmeth.1896</citation>
+    </citations>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/omero_roi_upload.py	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,165 @@
+import argparse
+import re
+
+import pandas as pd
+from ezomero import connect, post_roi
+from ezomero.rois import Ellipse, Label, Line, Point, Polygon, Polyline, Rectangle
+
+
+def parse_color(color_str):
+    if not color_str:
+        return None
+    return tuple(map(int, re.findall(r'\d+', color_str)))
+
+
+def parse_points(points_str):
+    if not points_str:
+        return None
+    # Remove leading and trailing brackets and split into individual points
+    points_str = points_str.strip("[]")
+    points = points_str.split("),(")
+    points = [point.strip("()") for point in points]  # Remove any remaining parentheses
+    return [tuple(map(float, point.split(','))) for point in points]
+
+
+def create_shape(row):
+    shape_type = row['shape']
+    shape = None
+
+    if shape_type == 'Ellipse':
+        shape = Ellipse(
+            x=row['x'],
+            y=row['y'],
+            x_rad=row['x_rad'],
+            y_rad=row['y_rad'],
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    elif shape_type == 'Label':
+        shape = Label(
+            x=row['x'],
+            y=row['y'],
+            label=row['label'],
+            fontSize=row['fontSize'],
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    elif shape_type == 'Line':
+        shape = Line(
+            x1=row['x1'],
+            y1=row['y1'],
+            x2=row['x2'],
+            y2=row['y2'],
+            markerStart=row.get('markerStart', None),
+            markerEnd=row.get('markerEnd', None),
+            label=row.get('label', None),
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    elif shape_type == 'Point':
+        shape = Point(
+            x=row['x'],
+            y=row['y'],
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    elif shape_type == 'Polygon':
+        shape = Polygon(
+            points=parse_points(row['points']),
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    elif shape_type == 'Polyline':
+        shape = Polyline(
+            points=parse_points(row['points']),
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    elif shape_type == 'Rectangle':
+        shape = Rectangle(
+            x=row['x'],
+            y=row['y'],
+            width=row['width'],
+            height=row['height'],
+            z=row.get('z'),
+            c=row.get('c'),
+            t=row.get('t'),
+            fill_color=parse_color(row.get('fill_color')),
+            stroke_color=parse_color(row.get('stroke_color')),
+            stroke_width=row.get('stroke_width')
+        )
+    return shape
+
+
+def main(input_file, conn, image_id, log_file):
+    # Open log file
+    with open(log_file, 'w') as log:
+        df = pd.read_csv(input_file, sep='\t')
+        for index, row in df.iterrows():
+            msg = f"Processing row {index + 1}/{len(df)}: {row.to_dict()}"
+            print(msg)
+            log.write(msg + "\n")
+            shape = create_shape(row)
+            if shape:
+                roi_name = row['roi_name'] if 'roi_name' in row else None
+                roi_description = row['roi_description'] if 'roi_description' in row else None
+                roi_id = post_roi(conn, image_id, [shape], name=roi_name, description=roi_description)
+                msg = f"ROI ID: {roi_id} for row {index + 1}"
+                print(msg)
+                log.write(msg + "\n")
+            else:
+                msg = f"Skipping row {index + 1}: Unable to create shape"
+                print(msg)
+                log.write(msg + "\n")
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(
+        description="Create shapes from a tabular file and optionally post them as an ROI to OMERO.")
+    parser.add_argument("--input_file", help="Path to the input tabular file.")
+    parser.add_argument("--image_id", type=int, required=True, help="ID of the image to which the ROI will be linked")
+    parser.add_argument("--host", type=str, required=True, help="OMERO server host")
+    parser.add_argument("--user", type=str, required=True, help="OMERO username")
+    parser.add_argument("--psw", type=str, required=True, help="OMERO password")
+    parser.add_argument("--port", type=int, default=4064, help="OMERO server port")
+    parser.add_argument("--log_file", type=str, default="process.txt", help="Log file path")
+
+    args = parser.parse_args()
+
+    conn = connect(
+        host=args.host,
+        user=args.user,
+        password=args.psw,
+        port=args.port,
+        group="",
+        secure=True
+    )
+
+    try:
+        main(args.input_file, conn, args.image_id, args.log_file)
+    finally:
+        conn.close()
Binary file test-data/input1.tif has changed
Binary file test-data/input2.tif has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/input_roi.tsv	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,8 @@
+shape	x	y	x_rad	y_rad	label	fontSize	x1	y1	x2	y2	points	width	height	fill_color	stroke_color	stroke_width	z	c	t	roi_name	roi_description
+Ellipse	50.0	50.0	20.0	10.0										(255,0,0,128)	(0,0,0,255)	2	0	0	0	Example ROI	This is an example ROI
+Label	100.0	100.0			Test Label	12.0								(255,255,255,0)	(0,0,255,255)	1	0	0	0	Example ROI	This is an example ROI
+Line							200.0	200.0	250.0	250.0				(0,255,0,128)	(0,0,0,255)	2	0	1	0	Example ROI	This is an example ROI
+Point	150.0	150.0												(0,0,255,128)	(255,0,0,255)	3	0	2	0	Example ROI	This is an example ROI
+Polygon											(300,300),(350,350),(300,400)			(255,255,0,128)	(0,0,0,255)	2	1	0	0	Example ROI	This is an example ROI
+Polyline											(400,400),(450,450),(400,500)			(0,255,255,128)	(0,0,0,255)	3	0	0	0	Example ROI	This is an example ROI
+Rectangle	500.0	500.0										100.0	50.0	(255,0,255,128)	(0,0,0,255)	2	0	0	0	Example ROI	This is an example ROI
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/metadata.tsv	Thu Sep 05 11:55:44 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	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,2 @@
+Image:3
+Image:4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output_KV_import.txt	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,1 @@
+SUCCESS: Successfully uploaded metadata for dataset with ID 2. Result: {'Key1': 'Value1', 'Key2': 'Value2'}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output_table_import.txt	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,2 @@
+SUCCESS: Successfully uploaded metadata for project with ID 2. Result:      Key1    Key2
+0  Value1  Value2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output_table_roi.txt	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,14 @@
+Processing row 1/7: {'shape': 'Ellipse', 'x': 50.0, 'y': 50.0, 'x_rad': 20.0, 'y_rad': 10.0, 'label': nan, 'fontSize': nan, 'x1': nan, 'y1': nan, 'x2': nan, 'y2': nan, 'points': nan, 'width': nan, 'height': nan, 'fill_color': '(255,0,0,128)', 'stroke_color': '(0,0,0,255)', 'stroke_width': 2, 'z': 0, 'c': 0, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 1 for row 1
+Processing row 2/7: {'shape': 'Label', 'x': 100.0, 'y': 100.0, 'x_rad': nan, 'y_rad': nan, 'label': 'Test Label', 'fontSize': 12.0, 'x1': nan, 'y1': nan, 'x2': nan, 'y2': nan, 'points': nan, 'width': nan, 'height': nan, 'fill_color': '(255,255,255,0)', 'stroke_color': '(0,0,255,255)', 'stroke_width': 1, 'z': 0, 'c': 0, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 2 for row 2
+Processing row 3/7: {'shape': 'Line', 'x': nan, 'y': nan, 'x_rad': nan, 'y_rad': nan, 'label': nan, 'fontSize': nan, 'x1': 200.0, 'y1': 200.0, 'x2': 250.0, 'y2': 250.0, 'points': nan, 'width': nan, 'height': nan, 'fill_color': '(0,255,0,128)', 'stroke_color': '(0,0,0,255)', 'stroke_width': 2, 'z': 0, 'c': 1, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 3 for row 3
+Processing row 4/7: {'shape': 'Point', 'x': 150.0, 'y': 150.0, 'x_rad': nan, 'y_rad': nan, 'label': nan, 'fontSize': nan, 'x1': nan, 'y1': nan, 'x2': nan, 'y2': nan, 'points': nan, 'width': nan, 'height': nan, 'fill_color': '(0,0,255,128)', 'stroke_color': '(255,0,0,255)', 'stroke_width': 3, 'z': 0, 'c': 2, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 4 for row 4
+Processing row 5/7: {'shape': 'Polygon', 'x': nan, 'y': nan, 'x_rad': nan, 'y_rad': nan, 'label': nan, 'fontSize': nan, 'x1': nan, 'y1': nan, 'x2': nan, 'y2': nan, 'points': '(300,300),(350,350),(300,400)', 'width': nan, 'height': nan, 'fill_color': '(255,255,0,128)', 'stroke_color': '(0,0,0,255)', 'stroke_width': 2, 'z': 1, 'c': 0, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 5 for row 5
+Processing row 6/7: {'shape': 'Polyline', 'x': nan, 'y': nan, 'x_rad': nan, 'y_rad': nan, 'label': nan, 'fontSize': nan, 'x1': nan, 'y1': nan, 'x2': nan, 'y2': nan, 'points': '(400,400),(450,450),(400,500)', 'width': nan, 'height': nan, 'fill_color': '(0,255,255,128)', 'stroke_color': '(0,0,0,255)', 'stroke_width': 3, 'z': 0, 'c': 0, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 6 for row 6
+Processing row 7/7: {'shape': 'Rectangle', 'x': 500.0, 'y': 500.0, 'x_rad': nan, 'y_rad': nan, 'label': nan, 'fontSize': nan, 'x1': nan, 'y1': nan, 'x2': nan, 'y2': nan, 'points': nan, 'width': 100.0, 'height': 50.0, 'fill_color': '(255,0,255,128)', 'stroke_color': '(0,0,0,255)', 'stroke_width': 2, 'z': 0, 'c': 0, 't': 0, 'roi_name': 'Example ROI', 'roi_description': 'This is an example ROI'}
+ROI ID: 7 for row 7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output_target_import.txt	Thu Sep 05 11:55:44 2024 +0000
@@ -0,0 +1,1 @@
+SUCCESS: Successfully uploaded metadata for dataset with ID 1. Result: {'Key1': 'Value1', 'Key2': 'Value2'}