comparison omero_roi_upload.py @ 2:e41f70e69349 draft

planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/omero commit 266752b0162fbdb32f132a6702cb661ae36f48f0
author ufz
date Thu, 12 Sep 2024 16:30:50 +0000
parents 588d6fa22fc4
children
comparison
equal deleted inserted replaced
1:588d6fa22fc4 2:e41f70e69349
1 import argparse 1 import argparse
2 import json
2 import re 3 import re
3 4
5 import numpy as np
4 import pandas as pd 6 import pandas as pd
5 from ezomero import connect, post_roi 7 from ezomero import connect, post_roi
6 from ezomero.rois import Ellipse, Label, Line, Point, Polygon, Polyline, Rectangle 8 from ezomero.rois import Ellipse, Label, Line, Point, Polygon, Polyline, Rectangle
7 9
8 10
33 x_rad=row['x_rad'], 35 x_rad=row['x_rad'],
34 y_rad=row['y_rad'], 36 y_rad=row['y_rad'],
35 z=row.get('z'), 37 z=row.get('z'),
36 c=row.get('c'), 38 c=row.get('c'),
37 t=row.get('t'), 39 t=row.get('t'),
40 label=row.get('label'),
38 fill_color=parse_color(row.get('fill_color')), 41 fill_color=parse_color(row.get('fill_color')),
39 stroke_color=parse_color(row.get('stroke_color')), 42 stroke_color=parse_color(row.get('stroke_color')),
40 stroke_width=row.get('stroke_width') 43 stroke_width=row.get('stroke_width')
41 ) 44 )
42 elif shape_type == 'Label': 45 elif shape_type == 'Label':
58 y1=row['y1'], 61 y1=row['y1'],
59 x2=row['x2'], 62 x2=row['x2'],
60 y2=row['y2'], 63 y2=row['y2'],
61 markerStart=row.get('markerStart', None), 64 markerStart=row.get('markerStart', None),
62 markerEnd=row.get('markerEnd', None), 65 markerEnd=row.get('markerEnd', None),
63 label=row.get('label', None), 66 label=row.get('label'),
64 z=row.get('z'), 67 z=row.get('z'),
65 c=row.get('c'), 68 c=row.get('c'),
66 t=row.get('t'), 69 t=row.get('t'),
67 fill_color=parse_color(row.get('fill_color')), 70 fill_color=parse_color(row.get('fill_color')),
68 stroke_color=parse_color(row.get('stroke_color')), 71 stroke_color=parse_color(row.get('stroke_color')),
73 x=row['x'], 76 x=row['x'],
74 y=row['y'], 77 y=row['y'],
75 z=row.get('z'), 78 z=row.get('z'),
76 c=row.get('c'), 79 c=row.get('c'),
77 t=row.get('t'), 80 t=row.get('t'),
81 label=row.get('label'),
78 fill_color=parse_color(row.get('fill_color')), 82 fill_color=parse_color(row.get('fill_color')),
79 stroke_color=parse_color(row.get('stroke_color')), 83 stroke_color=parse_color(row.get('stroke_color')),
80 stroke_width=row.get('stroke_width') 84 stroke_width=row.get('stroke_width')
81 ) 85 )
82 elif shape_type == 'Polygon': 86 elif shape_type == 'Polygon':
83 shape = Polygon( 87 shape = Polygon(
84 points=parse_points(row['points']), 88 points=parse_points(row['points']),
85 z=row.get('z'), 89 z=row.get('z'),
86 c=row.get('c'), 90 c=row.get('c'),
87 t=row.get('t'), 91 t=row.get('t'),
92 label=row.get('label'),
88 fill_color=parse_color(row.get('fill_color')), 93 fill_color=parse_color(row.get('fill_color')),
89 stroke_color=parse_color(row.get('stroke_color')), 94 stroke_color=parse_color(row.get('stroke_color')),
90 stroke_width=row.get('stroke_width') 95 stroke_width=row.get('stroke_width')
91 ) 96 )
92 elif shape_type == 'Polyline': 97 elif shape_type == 'Polyline':
93 shape = Polyline( 98 shape = Polyline(
94 points=parse_points(row['points']), 99 points=parse_points(row['points']),
95 z=row.get('z'), 100 z=row.get('z'),
96 c=row.get('c'), 101 c=row.get('c'),
97 t=row.get('t'), 102 t=row.get('t'),
103 label=row.get('label'),
98 fill_color=parse_color(row.get('fill_color')), 104 fill_color=parse_color(row.get('fill_color')),
99 stroke_color=parse_color(row.get('stroke_color')), 105 stroke_color=parse_color(row.get('stroke_color')),
100 stroke_width=row.get('stroke_width') 106 stroke_width=row.get('stroke_width')
101 ) 107 )
102 elif shape_type == 'Rectangle': 108 elif shape_type == 'Rectangle':
106 width=row['width'], 112 width=row['width'],
107 height=row['height'], 113 height=row['height'],
108 z=row.get('z'), 114 z=row.get('z'),
109 c=row.get('c'), 115 c=row.get('c'),
110 t=row.get('t'), 116 t=row.get('t'),
117 label=row.get('label'),
111 fill_color=parse_color(row.get('fill_color')), 118 fill_color=parse_color(row.get('fill_color')),
112 stroke_color=parse_color(row.get('stroke_color')), 119 stroke_color=parse_color(row.get('stroke_color')),
113 stroke_width=row.get('stroke_width') 120 stroke_width=row.get('stroke_width')
114 ) 121 )
115 return shape 122 return shape
117 124
118 def main(input_file, conn, image_id, log_file): 125 def main(input_file, conn, image_id, log_file):
119 # Open log file 126 # Open log file
120 with open(log_file, 'w') as log: 127 with open(log_file, 'w') as log:
121 df = pd.read_csv(input_file, sep='\t') 128 df = pd.read_csv(input_file, sep='\t')
129 # Replace nan to none
130 df = df.replace({np.nan: None})
122 for index, row in df.iterrows(): 131 for index, row in df.iterrows():
123 msg = f"Processing row {index + 1}/{len(df)}: {row.to_dict()}" 132 msg = f"Processing row {index + 1}/{len(df)}: {row.to_dict()}"
124 print(msg) 133 print(msg)
125 log.write(msg + "\n") 134 log.write(msg + "\n")
126 shape = create_shape(row) 135 shape = create_shape(row)
141 parser = argparse.ArgumentParser( 150 parser = argparse.ArgumentParser(
142 description="Create shapes from a tabular file and optionally post them as an ROI to OMERO.") 151 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.") 152 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") 153 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") 154 parser.add_argument("--host", type=str, required=True, help="OMERO server host")
146 parser.add_argument("--user", type=str, required=True, help="OMERO username") 155 parser.add_argument("--credential-file", dest="credential_file", type=str, required=True, help="Credential file (JSON file with username and password for OMERO)")
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") 156 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") 157 parser.add_argument("--log_file", type=str, default="process.txt", help="Log file path")
150 158
151 args = parser.parse_args() 159 args = parser.parse_args()
152 160
161 with open(args.credential_file, 'r') as f:
162 crds = json.load(f)
163
153 conn = connect( 164 conn = connect(
154 host=args.host, 165 host=args.host,
155 user=args.user, 166 user=crds['username'],
156 password=args.psw, 167 password=crds['password'],
157 port=args.port, 168 port=args.port,
158 group="", 169 group="",
159 secure=True 170 secure=True
160 ) 171 )
161 172