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