|
| 1 | + |
| 2 | +"""Interface to the image management server |
| 3 | +This module contains the functionality to upload and download |
| 4 | +imaging data (raw and metadata) form the OMERO server (v5.4). |
| 5 | +It requires that the following software be installed within the Python |
| 6 | +environment you are loading this module: |
| 7 | + * OMERO.cli (https://docs.openmicroscopy.org/omero/5.4.0/users/cli/index.html) |
| 8 | + * OMERO Python language bindings (https://docs.openmicroscopy.org/omero/5.4.0/developers/Python.html) |
| 9 | +This code is based on the following documentation: |
| 10 | + https://docs.openmicroscopy.org/omero/5.4.0/developers/Python.html |
| 11 | +It contains the following functions: |
| 12 | + * omero_connect - connects to server |
| 13 | + * TODO... |
| 14 | + |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +def omero_connect(usr, pwd, host, port): |
| 19 | + """ |
| 20 | + Connects to the OMERO Server with the provided username and password. |
| 21 | +
|
| 22 | + Args: |
| 23 | + usr: The username to log into OMERO |
| 24 | + pwd: a password associated with the given username |
| 25 | + host: the OMERO hostname |
| 26 | + port: the port at which the OMERO server can be reached |
| 27 | +
|
| 28 | + Returns: |
| 29 | + Connected BlitzGateway to the OMERO Server with the provided credentials |
| 30 | +
|
| 31 | + """ |
| 32 | + from omero.gateway import BlitzGateway |
| 33 | + |
| 34 | + conn = BlitzGateway(usr, pwd, host=host, port=port) |
| 35 | + connected = conn.connect() |
| 36 | + conn.setSecure(True) |
| 37 | + |
| 38 | + if not connected: |
| 39 | + print("Error: Connection not available") |
| 40 | + |
| 41 | + return conn |
| 42 | + |
| 43 | +def print_data_ids(conn): |
| 44 | + """ |
| 45 | + Prints all IDs of the data objects(Projects, Datasets, Images) associated with the logged in user on the OMERO server |
| 46 | +
|
| 47 | + Args: |
| 48 | + conn: Established Connection to the OMERO Server via a BlitzGateway |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Nothing except a printed text output to console |
| 52 | +
|
| 53 | + """ |
| 54 | + |
| 55 | + for project in conn.getObjects("Project"): |
| 56 | + print('project: ' + str(project.getName()) + ' -- ' + str(project.getId())) |
| 57 | + |
| 58 | + for dataset in project.listChildren(): |
| 59 | + print('ds: ' + str(dataset.getName()) + ' -- ' + str(dataset.getId())) |
| 60 | + |
| 61 | + for image in dataset.listChildren(): |
| 62 | + print('img: ' + str(image.getName()) + ' -- ' + str(image.getId())) |
| 63 | + |
| 64 | +def get_omero_dataset_id(conn, openbis_project_id, openbis_sample_id): |
| 65 | + """ |
| 66 | + Prints all IDs of the data objects(Projects, Datasets, Images) associated with the logged in user on the OMERO server |
| 67 | +
|
| 68 | + Args: |
| 69 | + conn: Established Connection to the OMERO Server via a BlitzGateway |
| 70 | + openbis_project_id: Id specifying the project information stored on OpenBIS |
| 71 | + openbis_sample_id: Id specifying the sample information stored on OpenBIS |
| 72 | + Returns: |
| 73 | + omero_dataset_id: Id specifying the dataset information stored on OMERO |
| 74 | +
|
| 75 | + """ |
| 76 | + omero_dataset_id = -1 |
| 77 | + found_id = False |
| 78 | + |
| 79 | + my_exp_id = conn.getUser().getId() |
| 80 | + default_group_id = conn.getEventContext().groupId |
| 81 | + |
| 82 | + for project in conn.getObjects("Project"): |
| 83 | + |
| 84 | + if found_id: |
| 85 | + break |
| 86 | + |
| 87 | + if project.getName() == openbis_project_id: |
| 88 | + for dataset in project.listChildren(): |
| 89 | + |
| 90 | + if dataset.getName() == openbis_sample_id: |
| 91 | + omero_dataset_id = dataset.getId() |
| 92 | + |
| 93 | + found_id = True |
| 94 | + break |
| 95 | + |
| 96 | + return omero_dataset_id |
| 97 | + |
| 98 | +def register_image_file(file_path, project_id, sample_id, usr, pwd, host, port=4064): |
| 99 | + """ |
| 100 | + This function imports an image file to an omero server using the OMERO.cli (using Bio-formats) |
| 101 | + This function assumes the OMERO.cli is installed |
| 102 | + Example: |
| 103 | + register_image_file("data/test_img.nd2", "project_x", "sample_y", |
| 104 | + "joe_usr", "joe_pwd", "192.168.2.2") |
| 105 | + Args: |
| 106 | + file_path (string): the path to the fastq file to validate |
| 107 | + project_id (string): the corresponding project ID in openBIS server |
| 108 | + sample_id (string): the corresponding sample ID in openBIS server |
| 109 | + usr (string): username for the OMERO server |
| 110 | + pwd (string): password for the OMERO server |
| 111 | + host (string): OMERO server address |
| 112 | + port (int): OMERO server port |
| 113 | + Returns: |
| 114 | + list of strings: list of newly generated omero IDs for registered images |
| 115 | + (a file can contain many images) |
| 116 | + """ |
| 117 | + |
| 118 | + import subprocess |
| 119 | + |
| 120 | + image_ids = [] |
| 121 | + |
| 122 | + conn = omero_connect(usr, pwd, host, str(port)) |
| 123 | + ds_id = get_omero_dataset_id(conn, project_id, sample_id) |
| 124 | + |
| 125 | + if ds_id != -1: |
| 126 | + |
| 127 | + cmd = "omero-importer -s " + host + " -p " + str(port) + " -u " + usr + " -w " + pwd + " -d " + str(int(ds_id)) + " " + file_path |
| 128 | + print("----cmd: " + cmd) |
| 129 | + |
| 130 | + proc = subprocess.Popen(cmd, |
| 131 | + stdout=subprocess.PIPE, |
| 132 | + stderr=subprocess.PIPE, |
| 133 | + shell=True, |
| 134 | + universal_newlines=True) |
| 135 | + |
| 136 | + std_out, std_err = proc.communicate() |
| 137 | + |
| 138 | + print("std_out: " + std_out) |
| 139 | + print("std_err: " + std_err) |
| 140 | + print("return_code: " + str(proc.returncode)) |
| 141 | + |
| 142 | + if int(proc.returncode) == 0: |
| 143 | + |
| 144 | + print("-->" + std_out) |
| 145 | + |
| 146 | + fist_line = std_out.splitlines()[0] |
| 147 | + image_ids = fist_line[6:].split(',') |
| 148 | + |
| 149 | + print("id list: " + str(image_ids)) |
| 150 | + |
| 151 | + else: |
| 152 | + print("return code fail") |
| 153 | + |
| 154 | + else: |
| 155 | + print("invalid sample_id") |
| 156 | + |
| 157 | + return image_ids |
| 158 | + |
| 159 | +def register_image_file_with_dataset_id(file_path, dataset_id, usr, pwd, host, port=4064): |
| 160 | + """ |
| 161 | + This function imports an image file to an omero server using the OMERO.cli (using Bio-formats) |
| 162 | + This function assumes the OMERO.cli is installed |
| 163 | + Example: |
| 164 | + register_image_file("data/test_img.nd2", 10, |
| 165 | + "joe_usr", "joe_pwd", "192.168.2.2") |
| 166 | + Args: |
| 167 | + file_path (string): the path to the fastq file to validate |
| 168 | + dataset_id (string): the ID of the omero dataset |
| 169 | + usr (string): username for the OMERO server |
| 170 | + pwd (string): password for the OMERO server |
| 171 | + host (string): OMERO server address |
| 172 | + port (int): OMERO server port |
| 173 | + Returns: |
| 174 | + list of strings: list of newly generated omero IDs for registered images |
| 175 | + (a file can contain many images) |
| 176 | + """ |
| 177 | + |
| 178 | + import subprocess |
| 179 | + |
| 180 | + image_ids = [] |
| 181 | + |
| 182 | + ds_id = dataset_id |
| 183 | + |
| 184 | + if ds_id != -1: |
| 185 | + |
| 186 | + cmd = "omero-importer -s " + host + " -p " + str(port) + " -u " + usr + " -w " + pwd + " -d " + str(int(ds_id)) + " " + file_path |
| 187 | + |
| 188 | + proc = subprocess.Popen(cmd, |
| 189 | + stdout=subprocess.PIPE, |
| 190 | + stderr=subprocess.PIPE, |
| 191 | + shell=True, |
| 192 | + universal_newlines=True) |
| 193 | + |
| 194 | + std_out, std_err = proc.communicate() |
| 195 | + |
| 196 | + if int(proc.returncode) == 0: |
| 197 | + |
| 198 | + fist_line = std_out.splitlines()[0] |
| 199 | + image_ids = fist_line[6:].split(',') |
| 200 | + |
| 201 | + else: |
| 202 | + image_ids = -1 |
| 203 | + |
| 204 | + else: |
| 205 | + image_ids = -1 |
| 206 | + |
| 207 | + return image_ids |
| 208 | + |
| 209 | + |
| 210 | +######################################## |
| 211 | +#functions to register numpy arrays |
| 212 | + |
| 213 | +def generate_array_plane(new_img): |
| 214 | + """ |
| 215 | + TODO |
| 216 | + """ |
| 217 | + |
| 218 | + img_shape = new_img.shape |
| 219 | + size_z = img_shape[4] |
| 220 | + size_t = img_shape[0] |
| 221 | + size_c = img_shape[1] |
| 222 | + |
| 223 | + for z in range(size_z): # all Z sections |
| 224 | + for c in range(size_c): # all channels |
| 225 | + for t in range(size_t): # all time-points |
| 226 | + |
| 227 | + new_plane = new_img[t, c, :, :, z] |
| 228 | + yield new_plane |
| 229 | + |
| 230 | +def create_array(conn, img, img_name, img_desc, ds): |
| 231 | + """ |
| 232 | + TODO |
| 233 | + """ |
| 234 | + |
| 235 | + dims = img.shape |
| 236 | + z = dims[4] |
| 237 | + t = dims[0] |
| 238 | + c = dims[1] |
| 239 | + |
| 240 | + new_img = conn.createImageFromNumpySeq(generate_array_plane(img), |
| 241 | + img_name, |
| 242 | + z, c, t, |
| 243 | + description=img_desc, |
| 244 | + dataset=ds) |
| 245 | + |
| 246 | + return new_img.getId() |
| 247 | + |
| 248 | +def register_image_array(img, img_name, img_desc, project_id, sample_id, usr, pwd, host, port=4064): |
| 249 | + """ |
| 250 | + This function imports a 5D (time-points, channels, x, y, z) numpy array of an image |
| 251 | + to an omero server using the OMERO Python bindings |
| 252 | + Example: |
| 253 | + register_image_array(hypercube, "tomo_0", "this is a tomogram", |
| 254 | + "project_x", "sample_y", "joe_usr", "joe_pwd", "192.168.2.2") |
| 255 | + Args: |
| 256 | + file_path (string): the path to the fastq file to validate |
| 257 | + project_id (string): the corresponding project ID in openBIS server |
| 258 | + sample_id (string): the corresponding sample ID in openBIS server |
| 259 | + usr (string): username for the OMERO server |
| 260 | + pwd (string): password for the OMERO server |
| 261 | + host (string): OMERO server address |
| 262 | + port (int): OMERO server port |
| 263 | + Returns: |
| 264 | + int: newly generated omero ID for registered image array |
| 265 | + """ |
| 266 | + |
| 267 | + img_id = -1 |
| 268 | + save_flag = 0 |
| 269 | + |
| 270 | + conn = omero_connect(usr, pwd, host, str(port)) |
| 271 | + |
| 272 | + for project in conn.getObjects("Project"): |
| 273 | + if project.getName() == project_id: |
| 274 | + for dataset in project.listChildren(): |
| 275 | + if dataset.getName() == sample_id: |
| 276 | + |
| 277 | + img_id = create_array(conn, img, img_name, img_desc, dataset) |
| 278 | + |
| 279 | + save_flag = 1 |
| 280 | + break |
| 281 | + if save_flag == 1: |
| 282 | + break |
| 283 | + |
| 284 | + return int(img_id) |
| 285 | + |
| 286 | +def get_image_array(conn, image_id): |
| 287 | + """ |
| 288 | + This function retrieves an image from an OMERO server as a numpy array |
| 289 | + TODO |
| 290 | + """ |
| 291 | + |
| 292 | + import numpy as np |
| 293 | + |
| 294 | + image = conn.getObject("Image", image_id) |
| 295 | + |
| 296 | + #construct numpy array (t, c, x, y, z) |
| 297 | + |
| 298 | + size_x = image.getSizeX() |
| 299 | + size_y = image.getSizeY() |
| 300 | + size_z = image.getSizeZ() |
| 301 | + size_c = image.getSizeC() |
| 302 | + size_t = image.getSizeT() |
| 303 | + |
| 304 | + # X and Y fields have to be aligned this way since during generation of the image from the numpy array the 2darray is expected to be (Y,X) |
| 305 | + # See Documentation here https://downloads.openmicroscopy.org/omero/5.5.1/api/python/omero/omero.gateway.html#omero.gateway._BlitzGateway |
| 306 | + hypercube = np.zeros((size_t, size_c, size_y, size_x, size_z)) |
| 307 | + |
| 308 | + pixels = image.getPrimaryPixels() |
| 309 | + |
| 310 | + for t in range(size_t): |
| 311 | + for c in range(size_c): |
| 312 | + for z in range(size_z): |
| 313 | + plane = pixels.getPlane(z, c, t) # get a numpy array. |
| 314 | + hypercube[t, c, :, :, z] = plane |
| 315 | + |
| 316 | + return hypercube |
| 317 | + |
| 318 | +################################ |
| 319 | + |
| 320 | +def add_annotations_to_image(conn, image_id, key_value_data): |
| 321 | + """ |
| 322 | + TODO |
| 323 | + """ |
| 324 | + |
| 325 | + import omero |
| 326 | + |
| 327 | + map_ann = omero.gateway.MapAnnotationWrapper(conn) |
| 328 | + # Use 'client' namespace to allow editing in Insight & web |
| 329 | + namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION |
| 330 | + map_ann.setNs(namespace) |
| 331 | + map_ann.setValue(key_value_data) |
| 332 | + map_ann.save() |
| 333 | + |
| 334 | + image = conn.getObject("Image", image_id) |
| 335 | + # NB: only link a client map annotation to a single object |
| 336 | + image.linkAnnotation(map_ann) |
| 337 | + |
| 338 | + return 0 |
| 339 | + |
| 340 | + |
| 341 | +######################### |
| 342 | +##app |
| 343 | + |
| 344 | +from optparse import OptionParser |
| 345 | + |
| 346 | +###OMERO server info |
| 347 | +USERNAME = "usr" |
| 348 | +PASSWORD = "pwd" |
| 349 | +HOST = "host" |
| 350 | +PORT = 4064 |
| 351 | + |
| 352 | + |
| 353 | +def get_args(): |
| 354 | + parser = OptionParser() |
| 355 | + parser.add_option('-f', '--file', dest='file_path', default="None", help='file to register') |
| 356 | + parser.add_option('-d', '--dataset', dest='dataset_id', default="None", help='dataset id for registration') |
| 357 | + |
| 358 | + parser.add_option('-p', '--project', dest='project_id', default="None", help='project id for dataset id retrieval') |
| 359 | + parser.add_option('-s', '--sample', dest='sample_id', default="None", help='sample id for dataset id retrieval') |
| 360 | + |
| 361 | + (options, args) = parser.parse_args() |
| 362 | + return options |
| 363 | + |
| 364 | +if __name__ == '__main__': |
| 365 | + |
| 366 | + args = get_args() |
| 367 | + |
| 368 | + if args.file_path != "None": |
| 369 | + img_ids = register_image_file_with_dataset_id(args.file_path, int(args.dataset_id), USERNAME, PASSWORD, HOST) |
| 370 | + |
| 371 | + id_str = "" |
| 372 | + for id_i in img_ids: |
| 373 | + id_str = id_str + id_i + " " |
| 374 | + |
| 375 | + print id_str |
| 376 | + else: |
| 377 | + |
| 378 | + conn = omero_connect(USERNAME, PASSWORD, HOST, str(PORT)) |
| 379 | + ds_id = get_omero_dataset_id(conn, str(args.project_id), str(args.sample_id)) |
| 380 | + |
| 381 | + print ds_id |
0 commit comments