Mercurial > repos > ufz > omero_metadata_import
comparison omero_roi_upload.py @ 1:588d6fa22fc4 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:55 +0000 |
parents | |
children | e41f70e69349 |
comparison
equal
deleted
inserted
replaced
0:352e9d4eaf70 | 1:588d6fa22fc4 |
---|---|
1 import argparse | |
2 import re | |
3 | |
4 import pandas as pd | |
5 from ezomero import connect, post_roi | |
6 from ezomero.rois import Ellipse, Label, Line, Point, Polygon, Polyline, Rectangle | |
7 | |
8 | |
9 def parse_color(color_str): | |
10 if not color_str: | |
11 return None | |
12 return tuple(map(int, re.findall(r'\d+', color_str))) | |
13 | |
14 | |
15 def parse_points(points_str): | |
16 if not points_str: | |
17 return None | |
18 # Remove leading and trailing brackets and split into individual points | |
19 points_str = points_str.strip("[]") | |
20 points = points_str.split("),(") | |
21 points = [point.strip("()") for point in points] # Remove any remaining parentheses | |
22 return [tuple(map(float, point.split(','))) for point in points] | |
23 | |
24 | |
25 def create_shape(row): | |
26 shape_type = row['shape'] | |
27 shape = None | |
28 | |
29 if shape_type == 'Ellipse': | |
30 shape = Ellipse( | |
31 x=row['x'], | |
32 y=row['y'], | |
33 x_rad=row['x_rad'], | |
34 y_rad=row['y_rad'], | |
35 z=row.get('z'), | |
36 c=row.get('c'), | |
37 t=row.get('t'), | |
38 fill_color=parse_color(row.get('fill_color')), | |
39 stroke_color=parse_color(row.get('stroke_color')), | |
40 stroke_width=row.get('stroke_width') | |
41 ) | |
42 elif shape_type == 'Label': | |
43 shape = Label( | |
44 x=row['x'], | |
45 y=row['y'], | |
46 label=row['label'], | |
47 fontSize=row['fontSize'], | |
48 z=row.get('z'), | |
49 c=row.get('c'), | |
50 t=row.get('t'), | |
51 fill_color=parse_color(row.get('fill_color')), | |
52 stroke_color=parse_color(row.get('stroke_color')), | |
53 stroke_width=row.get('stroke_width') | |
54 ) | |
55 elif shape_type == 'Line': | |
56 shape = Line( | |
57 x1=row['x1'], | |
58 y1=row['y1'], | |
59 x2=row['x2'], | |
60 y2=row['y2'], | |
61 markerStart=row.get('markerStart', None), | |
62 markerEnd=row.get('markerEnd', None), | |
63 label=row.get('label', None), | |
64 z=row.get('z'), | |
65 c=row.get('c'), | |
66 t=row.get('t'), | |
67 fill_color=parse_color(row.get('fill_color')), | |
68 stroke_color=parse_color(row.get('stroke_color')), | |
69 stroke_width=row.get('stroke_width') | |
70 ) | |
71 elif shape_type == 'Point': | |
72 shape = Point( | |
73 x=row['x'], | |
74 y=row['y'], | |
75 z=row.get('z'), | |
76 c=row.get('c'), | |
77 t=row.get('t'), | |
78 fill_color=parse_color(row.get('fill_color')), | |
79 stroke_color=parse_color(row.get('stroke_color')), | |
80 stroke_width=row.get('stroke_width') | |
81 ) | |
82 elif shape_type == 'Polygon': | |
83 shape = Polygon( | |
84 points=parse_points(row['points']), | |
85 z=row.get('z'), | |
86 c=row.get('c'), | |
87 t=row.get('t'), | |
88 fill_color=parse_color(row.get('fill_color')), | |
89 stroke_color=parse_color(row.get('stroke_color')), | |
90 stroke_width=row.get('stroke_width') | |
91 ) | |
92 elif shape_type == 'Polyline': | |
93 shape = Polyline( | |
94 points=parse_points(row['points']), | |
95 z=row.get('z'), | |
96 c=row.get('c'), | |
97 t=row.get('t'), | |
98 fill_color=parse_color(row.get('fill_color')), | |
99 stroke_color=parse_color(row.get('stroke_color')), | |
100 stroke_width=row.get('stroke_width') | |
101 ) | |
102 elif shape_type == 'Rectangle': | |
103 shape = Rectangle( | |
104 x=row['x'], | |
105 y=row['y'], | |
106 width=row['width'], | |
107 height=row['height'], | |
108 z=row.get('z'), | |
109 c=row.get('c'), | |
110 t=row.get('t'), | |
111 fill_color=parse_color(row.get('fill_color')), | |
112 stroke_color=parse_color(row.get('stroke_color')), | |
113 stroke_width=row.get('stroke_width') | |
114 ) | |
115 return shape | |
116 | |
117 | |
118 def main(input_file, conn, image_id, log_file): | |
119 # Open log file | |
120 with open(log_file, 'w') as log: | |
121 df = pd.read_csv(input_file, sep='\t') | |
122 for index, row in df.iterrows(): | |
123 msg = f"Processing row {index + 1}/{len(df)}: {row.to_dict()}" | |
124 print(msg) | |
125 log.write(msg + "\n") | |
126 shape = create_shape(row) | |
127 if shape: | |
128 roi_name = row['roi_name'] if 'roi_name' in row else None | |
129 roi_description = row['roi_description'] if 'roi_description' in row else None | |
130 roi_id = post_roi(conn, image_id, [shape], name=roi_name, description=roi_description) | |
131 msg = f"ROI ID: {roi_id} for row {index + 1}" | |
132 print(msg) | |
133 log.write(msg + "\n") | |
134 else: | |
135 msg = f"Skipping row {index + 1}: Unable to create shape" | |
136 print(msg) | |
137 log.write(msg + "\n") | |
138 | |
139 | |
140 if __name__ == "__main__": | |
141 parser = argparse.ArgumentParser( | |
142 description="Create shapes from a tabular file and optionally post them as an ROI to OMERO.") | |
143 parser.add_argument("--input_file", help="Path to the input tabular file.") | |
144 parser.add_argument("--image_id", type=int, required=True, help="ID of the image to which the ROI will be linked") | |
145 parser.add_argument("--host", type=str, required=True, help="OMERO server host") | |
146 parser.add_argument("--user", type=str, required=True, help="OMERO username") | |
147 parser.add_argument("--psw", type=str, required=True, help="OMERO password") | |
148 parser.add_argument("--port", type=int, default=4064, help="OMERO server port") | |
149 parser.add_argument("--log_file", type=str, default="process.txt", help="Log file path") | |
150 | |
151 args = parser.parse_args() | |
152 | |
153 conn = connect( | |
154 host=args.host, | |
155 user=args.user, | |
156 password=args.psw, | |
157 port=args.port, | |
158 group="", | |
159 secure=True | |
160 ) | |
161 | |
162 try: | |
163 main(args.input_file, conn, args.image_id, args.log_file) | |
164 finally: | |
165 conn.close() |