def run(password, project_name, dataset_name, host, port): for user_number in range(1, 51): username = "******" % user_number print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) # make sure only one result is returned by query params.page(0, 1) query = "from Project where name='%s' \ AND details.owner.omeName=:username \ ORDER BY id DESC" % project_name service = conn.getQueryService() pr_list = service.findAllByQuery(query, params, conn.SERVICE_OPTS) if pr_list is None: print("No project with name %s found" % project_name) continue project_id = pr_list[0].getId().getValue() print(username, project_id) params = omero.sys.ParametersI() params.addString('username', username) # make sure only one result is returned by query params.page(0, 1) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username \ ORDER BY id DESC" % dataset_name service = conn.getQueryService() ds_list = service.findAllByQuery(query, params, conn.SERVICE_OPTS) if ds_list is None: print("No dataset with name %s found" % dataset_name) continue dataset_id = ds_list[0].getId().getValue() print(username, dataset_id) link = ProjectDatasetLinkI() link.setParent(ProjectI(project_id, False)) link.setChild(DatasetI(dataset_id, False)) conn.getUpdateService().saveObject(link) except Exception as exc: print("Error while linking to project: %s" % str(exc)) finally: conn.close()
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() dataset = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(dataset) == 0: print("No dataset with name %s found" % target) continue dataset_id = dataset[0].getId().getValue() print('dataset', dataset_id) dataset = conn.getObject("Dataset", dataset_id) kvp_sets = [ [["mitomycin-A", "0mM"], ["PBS", "10mM"], ["incubation (mins)", "20"], ["temperature", "37"], ["Organism", "H**o sapiens"]], [["mitomycin-A", "20mM"], ["PBS", "10mM"], ["incubation (mins)", "10"], ["temperature", "40"], ["Organism", "H**o sapiens"]], [["mitomycin-A", "10microM"], ["PBS", "10mM"], ["incubation (mins)", "5"], ["temperature", "37"], ["Organism", "H**o sapiens"]], [["mitomycin-A", "0mM"], ["PBS", "10mM"], ["incubation (mins)", "2"], ["temperature", "68"], ["Organism", "H**o sapiens"]], ] for count, image in enumerate(dataset.listChildren()): key_value_data = kvp_sets[count % 4] map_ann = omero.gateway.MapAnnotationWrapper(conn) # Use 'client' namespace to allow editing in Insight & web namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION map_ann.setNs(namespace) map_ann.setValue(key_value_data) map_ann.save() # NB: only link a client map annotation to a single object image.linkAnnotation(map_ann) print('linking to image', image.getName()) except Exception as exc: print("Error while setting key-value pairs: %s" % str(exc)) finally: conn.close()
def run(password, admin_name, target, tag, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() updateService = conn.getUpdateService() ds = conn.getObject("Dataset", attributes={'name': target}, opts={'owner': conn.getUserId()}) if ds is None: print("No dataset with name %s found" % target) continue params = omero.sys.ParametersI() params.addString('username', admin_name) query = "from TagAnnotation where textvalue='%s' \ AND details.owner.omeName=:username" % tag query_service = conn.getQueryService() tags = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(tags) == 0: print("No tag with name %s found" % tag) continue tag_id = tags[0].id.getValue() print(tag_id) links = [] for image in ds.listChildren(): name = image.getName() if name in images_to_tag: # Check first that the image is not tagged params = omero.sys.ParametersI() params.addLong('parent', image.id) params.addLong('child', tag_id) query = "select link from ImageAnnotationLink as link \ where link.parent.id=:parent \ AND link.child.id=:child" values = query_service.findAllByQuery( query, params, conn.SERVICE_OPTS) if len(values) == 0: link = ImageAnnotationLinkI() link.parent = ImageI(image.id, False) link.child = TagAnnotationI(tag_id, False) links.append(link) else: print("Tag %s already linked to %s" % (tag, name)) if len(links) > 0: updateService.saveArray(links) except Exception as exc: print("Error when tagging the images: %s" % str(exc)) finally: conn.close()
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target service = conn.getQueryService() dataset = service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(dataset) == 0: print("No dataset with name %s found" % target) continue dataset_obj = dataset[0] datasetId = dataset[0].getId().getValue() print('dataset', datasetId) params2 = omero.sys.ParametersI() params2.addId(dataset_obj.getId()) query = "select l.child.id from DatasetImageLink \ l where l.parent.id = :id" images = service.projection(query, params2, conn.SERVICE_OPTS) values = [] for k in range(0, len(images)): image_id = images[k][0].getValue() image = conn.getObject("Image", image_id) u = omero.model.LengthI(0.33, UnitsLength.MICROMETER) p = image.getPrimaryPixels()._obj p.setPhysicalSizeX(u) p.setPhysicalSizeY(u) values.append(p) if len(images) > 0: conn.getUpdateService().saveArray(values) except Exception as exc: print("Error during calibration: %s" % str(exc)) finally: conn.close()
def run(password, dataset_name, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Image where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() images = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(images) == 0: print("No images with name %s found" % target) continue image_id = images[0].getId().getValue() print('id', image_id) dataset = omero.model.DatasetI() dataset.setName(omero.rtypes.rstring(dataset_name)) dataset = conn.getUpdateService().saveAndReturnObject(dataset) dataset_id = dataset.getId().getValue() print(username, dataset_id) link = omero.model.DatasetImageLinkI() link.setParent(dataset) link.setChild(omero.model.ImageI(image_id, False)) conn.getUpdateService().saveObject(link) except Exception as exc: print("Error while linking images: %s" % str(exc)) finally: conn.close()
class OMEROConnectionManager: ''' Basic management of an OMERO Connection. Methods which make use of a connection will attempt to connect if connect was not already successfuly executed ''' def __init__(self, config_file=None): # Set the connection as not established self.conn = None self.SUUID = None self.HOST = None self.PORT = None self.USERNAME = None self.PASSWORD = None # If config_file not specified, first see if there's an active OMERO # CLI session. if config_file is None: store = SessionsStore() session_props = store.get_current() self.HOST, self.USERNAME, self.SUUID, self.PORT = session_props # config_file specified, or no active session. Continue with reading # connection params from the config file. if config_file is not None or self.SUUID is None: # Normalize file path. if config_file is None: config_file = '~/.omero/config' config_file = os.path.expanduser(config_file) # Check config file exists. if not (os.path.exists(config_file) and os.path.isfile(config_file)): sys.stderr.write('No active OMERO CLI session and ' 'configuration file {} does not ' 'exist\n'.format(config_file)) sys.exit(1) # Check permisisons on config file. if os.stat(config_file).st_mode & 0077: sys.stderr.write('Configuration file contains private ' 'credentials and must not be accessible by ' 'other users. Please run:\n\n' ' chmod 600 {}\n\n'.format(config_file)) sys.exit(1) # Read the credentials file. config = ConfigParser.RawConfigParser() config.read(config_file) self.HOST = config.get('OMEROCredentials', 'host') self.PORT = config.getint('OMEROCredentials', 'port') self.USERNAME = config.get('OMEROCredentials', 'username') self.PASSWORD = config.get('OMEROCredentials', 'password') def connect(self): ''' Create an OMERO Connection ''' # If connection already established just return it if self.conn is not None: return self.conn # Initialize the connection. At least HOST and PORT will be defined, # but USERNAME and PASSWORD may be None if we are connecting to an # existing session via its uuid. self.conn = BlitzGateway(username=self.USERNAME, passwd=self.PASSWORD, host=self.HOST, port=self.PORT) # Connect. If USERNAME and PASSWORD are None then SUUID must be # defined. connected = self.conn.connect(sUuid=self.SUUID) # Check that the connection was established if not connected: sys.stderr.write('Error: Connection not available, ' 'please check your user name and password.\n') sys.exit(1) return self.conn def disconnect(self): ''' Terminate the OMERO Connection ''' self.conn.seppuku(softclose=True) self.conn = None def hql_query(self, query, params=None): ''' Execute the given HQL query and return the results. Optionally accepts a parameters object. For conveniance, will unwrap the OMERO types ''' # Connect if not already connected if self.conn is None: self.connect() if params is None: params = ParametersI() # Set OMERO Group to -1 to query across all available data self.conn.SERVICE_OPTS.setOmeroGroup(-1) # Get the Query Service qs = self.conn.getQueryService() # Execute the query rows = qs.projection(query, params, self.conn.SERVICE_OPTS) # Unwrap the query results unwrapped_rows = [] for row in rows: unwrapped_row=[] for column in row: if column is None: unwrapped_row.append(None) else: unwrapped_row.append(column.val) unwrapped_rows.append(unwrapped_row) return unwrapped_rows def __del__(self): self.disconnect()
class Omg(object): """ OMERO gateway that wraps Blitz gateway and CLI, intended for scripting and interactive work. Attributes ---------- conn : Blitz gateway connection """ def __init__(self, conn=None, user=None, passwd=None, server=SERVER, port=PORT, skey=None): """ Requires active Blitz connection OR username plus password or sesskey """ if conn is None and (user is None or (passwd is None and skey is None)): raise ValueError("Bad parameters," + self.__init__.__doc__) if conn is not None: if conn.isConnected(): self.conn = conn else: raise ValueError("Cannot initialize with closed connection!") else: if passwd is not None: self.conn = BlitzGateway(user, passwd, host=server, port=port) self.conn.connect() else: self.conn = BlitzGateway(user, host=server, port=port) self.conn.connect(skey) if self.conn.isConnected(): self._server = self.conn.host self._port = self.conn.port self._user = self.conn.getUser().getName() self._key = self.conn.getSession().getUuid().getValue() print("Connected to {0} (port {1}) as {2}, session key={3}".format( self._server, self._port, self._user, self._key)) else: print("Failed to open connection :-(") def ls(self): """ Print groups, then projects/datasets/images for current group. """ print("Groups for {0}:-".format(self.conn.getUser().getName())) for gid, gname in self._ls_groups(): print(" {0} ({1})".format(gname, str(gid))) curr_grp = self.conn.getGroupFromContext() gid, gname = curr_grp.getId(), curr_grp.getName() print("\nData for current group, {0} ({1}):-".format(gname, gid)) for pid, pname in self._ls_projects(): print(" Project: {0} ({1})".format(pname, str(pid))) for did, dname in self._ls_datasets(pid): print(" Dataset: {0} ({1})".format(dname, str(did))) for iid, iname in self._ls_images(did): print(" Image: {0} ({1})".format(iname, str(iid))) # TODO, list orphaned Datasets and Images def _ls_groups(self): """list groups (id, name) this session is a member of""" groups = self.conn.getGroupsMemberOf() return [(group.getId(), group.getName()) for group in groups] def _ls_projects(self): """list projects (id, name) in the current session group""" projs = self.conn.listProjects(self.conn.getUserId()) return [(proj.getId(), proj.getName()) for proj in projs] def _ls_datasets(self, proj_id): """list datasets (id, name) within the project id given""" dsets = self.conn.getObject("Project", proj_id).listChildren() return [(dset.getId(), dset.getName()) for dset in dsets] def _ls_images(self, dset_id): """list images (id, name) within the dataset id given""" imgs = self.conn.getObject("Dataset", dset_id).listChildren() return [(img.getId(), img.getName()) for img in imgs] def chgrp(self, group_id): """ Change group for this session to the group_id given. """ self.conn.setGroupForSession(group_id) def get(self, im_id, get_att=True): """ Download the specified image as an OME-TIFF to current directory, with attachments also downloaded to folder: img_path + '_attachments' Return : path to downloaded image """ img = self.conn.getObject("Image", oid=im_id) img_name = self._unique_name(img.getName(), im_id) img_path = os.path.join(os.getcwd(), img_name) img_file = open(str(img_path + ".ome.tiff"), "wb") fsize, blockgen = img.exportOmeTiff(bufsize=65536) for block in blockgen: img_file.write(block) img_file.close() fa_type = omero.model.FileAnnotationI attachments = [ann for ann in img.listAnnotations() if ann.OMERO_TYPE == fa_type] if get_att and len(attachments) > 0: att_dir = img_path + "_attachments" os.mkdir(att_dir) def download_attachment(att, att_dir): """download OMERO file annotation to att_dir""" att_file = open(os.path.join(att_dir, att.getFileName()), "wb") for att_chunk in att.getFileInChunks(): att_file.write(att_chunk) att_file.close() for att in attachments: download_attachment(att, att_dir) return img_path def _unique_name(self, img_name, im_id): """Make unique name combining a file basename & OMERO Image id""" path_and_base, ext = os.path.splitext(img_name) base = os.path.basename(path_and_base) # name in OMERO can has path return "{0}_{1}".format(base, str(im_id)) def dget(self, dataset_id): """ Download an entire OMERO Dataset to the current directory. """ downloads = [] wdir = os.getcwd() dset_name = self.conn.getObject("Dataset", dataset_id).getName() dset_path = os.path.join(wdir, dset_name + "_D" + str(dataset_id)) os.mkdir(dset_path) os.chdir(dset_path) for img_id, img_name in self._ls_images(dataset_id): downloads.append(self.get(img_id)) os.chdir(wdir) return downloads def pget(self, project_id): """ Download an entire OMERO Project to the current directory. """ downloads = [] wdir = os.getcwd() proj_name = self.conn.getObject("Project", project_id).getName() proj_path = os.path.join(wdir, proj_name + "_P" + str(project_id)) os.mkdir(proj_path) os.chdir(proj_path) for dset_id, dset_name in self._ls_datasets(project_id): downloads.extend(self.dget(dset_id)) os.chdir(wdir) return downloads def put(self, filename, name=None, dataset=None): """ Import filename using OMERO CLI, optionally with a specified name to a specified dataset (dataset_id). Return : OMERO image Id """ cli = omero.cli.CLI() cli.loadplugins() import_args = ["import"] import_args.extend(["-s", str(self._server)]) import_args.extend(["-k", str(self._key)]) if dataset is not None: import_args.extend(["-d", str(dataset)]) if name is not None: import_args.extend(["-n", str(name)]) clio = "cli.out" clie = "cli.err" import_args.extend(["---errs=" + clie, "---file=" + clio, "--"]) import_args.append(filename) cli.invoke(import_args, strict=True) pix_id = int(open(clio, 'r').read().rstrip()) im_id = self.conn.getQueryService().get("Pixels", pix_id).image.id.val os.remove(clio) os.remove(clie) return im_id def describe(self, im_id, description): """ Append to image description. """ img = self.conn.getObject("Image", oid=im_id) old_description = img.getDescription() or "" img.setDescription(old_description + "\n" + description) img.save() def attach(self, im_id, attachments): """ Attach a list of files to an image. """ img = self.conn.getObject("Image", oid=im_id) for attachment in attachments.split(): fann = self.conn.createFileAnnfromLocalFile(attachment) img.linkAnnotation(fann) img.save() # TODO: ls_tags() and tag() methods? def mkp(self, project_name, description=None): """ Make new OMERO project in current group, returning the new project Id. """ # see: omero/lib/python/omeroweb/webclient/controller/container.py proj = omero.model.ProjectI() proj.name = omero.rtypes.rstring(str(project_name)) if description is not None and description != "": proj.description = omero.rtypes.rstring(str(description)) return self._save_and_return_id(proj) def mkd(self, dataset_name, project_id=None, description=None): """ Make new OMERO dataset, returning the new dataset Id. """ dset = omero.model.DatasetI() dset.name = omero.rtypes.rstring(str(dataset_name)) if description is not None and description != "": dset.description = omero.rtypes.rstring(str(description)) if project_id is not None: l_proj_dset = omero.model.ProjectDatasetLinkI() proj = self.conn.getObject("Project", project_id) l_proj_dset.setParent(proj._obj) l_proj_dset.setChild(dset) dset.addProjectDatasetLink(l_proj_dset) return self._save_and_return_id(dset) def _save_and_return_id(self, obj): """Save new omero object and return id assgined to it""" # see: OmeroWebGateway.saveAndReturnId # in: lib/python/omeroweb/webclient/webclient_gateway.py u_s = self.conn.getUpdateService() res = u_s.saveAndReturnObject(obj, self.conn.SERVICE_OPTS) res.unload() return res.id.val def im(self, im_id): """ Return an Im object for the image id specified. """ img = self.conn.getObject("Image", im_id) # build pixel np.ndarray nx, ny = img.getSizeX(), img.getSizeY() nz, nt, nc = img.getSizeZ(), img.getSizeT(), img.getSizeC() planes = [(z, c, t) for c in range(nc) for t in range(nt) for z in range(nz)] pix_gen = img.getPrimaryPixels().getPlanes(planes) pix = np.array([i for i in pix_gen]).reshape((nc, nt, nz, ny, nx)) # initialize Im using pix and extracted metadata meta = self._extract_meta(img, im_id) return Im(pix=pix, meta=meta) def _extract_meta(self, img, im_id): """Extract metadata attributes from OMERO Blitz gateway Image""" meta = {} meta['name'] = self._unique_name(img.getName(), im_id) meta['description'] = img.getDescription() def _extract_ch_info(ch): """extract core metadata for for channel, return as dict""" ch_info = {'label': ch.getLabel()} ch_info['ex_wave'] = ch.getExcitationWave() ch_info['em_wave'] = ch.getEmissionWave() ch_info['color'] = ch.getColor().getRGB() return ch_info meta['channels'] = [_extract_ch_info(ch) for ch in img.getChannels()] meta['pixel_size'] = {'x': img.getPixelSizeX(), 'y': img.getPixelSizeY(), 'z': img.getPixelSizeZ(), 'units': "um"} tag_type = omero.model.TagAnnotationI tags = [ann for ann in img.listAnnotations() if ann.OMERO_TYPE == tag_type] meta['tags'] = {tag.getValue() + " (" + str(tag.getId()) + ")": tag.getDescription() for tag in tags} fa_type = omero.model.FileAnnotationI attachments = [ann for ann in img.listAnnotations() if ann.OMERO_TYPE == fa_type] meta['attachments'] = [att.getFileName() + " (" + str(att.getId()) + ")" for att in attachments] user_id = self.conn.getUser().getName() + " (" + \ str(self.conn.getUser().getId()) + ") @" + self.conn.host meta_ext = {} meta_ext['user_id'] = user_id meta['meta_ext'] = meta_ext # TODO: ROIs, display settings? # objective: Image.loadOriginalMetadata()[1][find 'Lens ID Number'][1], return meta def imput(self, im, dataset_id=None): """ Create a new OMERO Image using an Im object, returning new image id. """ # see: omero/lib/python/omero/util/script_utils.py # see: omero/lib/python/omeroweb/webclient/webclient_gateway.py # see: https://gist.github.com/will-moore/4141708 if not isinstance(im, Im): raise TypeError("first imput argument must be of type Im") nc, nt, nz, ny, nx = im.shape ch_nums = range(nc) q_s = self.conn.getQueryService() p_s = self.conn.getPixelsService() c_s = self.conn.getContainerService() u_s = self.conn.getUpdateService() pu_s = self.conn.c.sf.createRawPixelsStore() q_ptype = "from PixelsType as p where p.value='{0}'".format( str(im.dtype)) pixelsType = q_s.findByQuery(q_ptype, None) im_id = p_s.createImage(nx, ny, nz, nt, ch_nums, pixelsType, im.name, im.description) img_i = c_s.getImages("Image", [im_id.getValue()], None)[0] img = self.conn.getObject("Image", im_id.getValue()) pix_id = img_i.getPrimaryPixels().getId().getValue() pu_s.setPixelsId(pix_id, True) for c in range(nc): for t in range(nt): for z in range(nz): plane = im.pix[c, t, z, :, :] script_utils.uploadPlaneByRow(pu_s, plane, z, c, t) l_dset_im = omero.model.DatasetImageLinkI() dset = self.conn.getObject("Dataset", dataset_id) l_dset_im.setParent(dset._obj) l_dset_im.setChild(img._obj) self._update_meta(im, im_id) u_s.saveObject(l_dset_im, self.conn.SERVICE_OPTS) return im_id.getValue() def _update_meta(self, im, im_id): """Set OMERO Image metadata using Im metadata"""
class OMEROConnectionManager(object): ''' Basic management of an OMERO Connection. Methods which make use of a connection will attempt to connect if connection was not already successfuly executed ''' def __init__(self, config_file=Path.home() / '.omero' / 'config'): self.config_file = config_file # Set the connection as not established self.conn = None def connect(self): ''' Create an OMERO Connection ''' # If connection already established just return it if self.conn is not None: return self.conn params = get_params_from_session() if params is None: params = get_params_from_config_file(self.config_file) # Initialize the connection. At least HOST and PORT will be defined, # but USERNAME and PASSWORD may be None if we are connecting to an # existing session via its uuid. self.conn = BlitzGateway(username=params.get('username'), passwd=params.get('password'), host=params['host'], port=params['port']) # Connect. If USERNAME and PASSWORD are None then SUUID must be # defined. connected = self.conn.connect(sUuid=params.get('suuid')) # Check that the connection was established if not connected: sys.stderr.write('Error: Connection not available, ' 'please check your user name and password.\n') sys.exit(1) return self.conn def disconnect(self): ''' Terminate the OMERO Connection ''' if self.conn: self.conn.seppuku(softclose=True) self.conn = None def hql_query(self, query, params=None): ''' Execute the given HQL query and return the results. Optionally accepts a parameters object. For conveniance, will unwrap the OMERO types ''' # Connect if not already connected if self.conn is None: self.connect() if params is None: params = ParametersI() # Set OMERO Group to -1 to query across all available data self.conn.SERVICE_OPTS.setOmeroGroup(-1) # Get the Query Service qs = self.conn.getQueryService() # Execute the query rows = qs.projection(query, params, self.conn.SERVICE_OPTS) # Unwrap the query results unwrapped_rows = [] for row in rows: unwrapped_row = [] for column in row: if column is None: unwrapped_row.append(None) else: unwrapped_row.append(column.val) unwrapped_rows.append(unwrapped_row) return unwrapped_rows def __del__(self): self.disconnect()
def run(): """ """ dataTypes = [rstring("Plate")] client = scripts.client( "Manage_Plate_Acquisitions.py", "Add or remove PlateAcquisition(s) in a given Plate", scripts.String("Data_Type", optional=False, grouping="1", description="The data type you want to work with.", values=dataTypes, default="Plate"), scripts.List("IDs", optional=False, grouping="2", description="List of Plate IDs").ofType(rlong(0)), scripts.String("Mode", optional=False, grouping="3", description="Select if you want to add or " "remove PlateAcquisitions", values=[rstring("Add"), rstring("Remove")], default="Add"), version="0.2", authors=["Niko Klaric"], institutions=["Glencoe Software Inc."], contact="*****@*****.**", ) try: scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) connection = BlitzGateway(client_obj=client) updateService = connection.getUpdateService() queryService = connection.getQueryService() processedMessages = [] for plateId in scriptParams["IDs"]: plateObj = connection.getObject("Plate", plateId) if plateObj is None: client.setOutput( "Message", rstring("ERROR: No Plate with ID %s" % plateId)) return if scriptParams["Mode"] == "Add": plateAcquisitionObj = PlateAcquisitionI() plateAcquisitionObj.setPlate(PlateI(plateObj.getId(), False)) wellGrid = plateObj.getWellGrid() for axis in wellGrid: for wellObj in axis: wellSampleList = wellObj.copyWellSamples() plateAcquisitionObj.addAllWellSampleSet(wellSampleList) plateAcquisitionObj = updateService.saveAndReturnObject( plateAcquisitionObj) plateAcquisitionId = plateAcquisitionObj.getId()._val processedMessages.append( "Linked new PlateAcquisition with ID %d" " to Plate with ID %d." % (plateAcquisitionId, plateId)) else: params = ParametersI() params.addId(plateId) queryString = """ FROM PlateAcquisition AS pa LEFT JOIN FETCH pa.wellSample LEFT OUTER JOIN FETCH pa.annotationLinks WHERE pa.plate.id = :id """ plateAcquisitionList = queryService.findAllByQuery( queryString, params, connection.SERVICE_OPTS) if plateAcquisitionList: updateList = [] for plate_acquisition in plateAcquisitionList: for well_sample in plate_acquisition.copyWellSample(): well_sample.setPlateAcquisition(None) updateList.append(well_sample) updateService.saveArray(updateList) plate_acquisition.clearWellSample() plate_acquisition.clearAnnotationLinks() plate_acquisition = updateService.saveAndReturnObject( plate_acquisition) updateService.deleteObject(plate_acquisition) processedMessages.append( "%d PlateAcquisition(s) removed from Plate with ID %d." % (len(plateAcquisitionList), plateId)) client.setOutput("Message", rstring("No errors. %s" % " ".join(processedMessages))) finally: client.closeSession()
parser = argparse.ArgumentParser( description="generate fake masks that overlap" ) parser.add_argument("--to-host", default="localhost") parser.add_argument("--to-user", default="root") parser.add_argument("--to-pass", default="omero") parser.add_argument("target_image", type=int, help="output image") ns = parser.parse_args() image_id_dst = ns.target_image local = BlitzGateway(ns.to_user, ns.to_pass, host=ns.to_host, secure=True) local.connect() query_service = local.getQueryService() update_service = local.getUpdateService() query = "FROM Image WHERE id = :id" params = ParametersI() params.addId(image_id_dst) count = 0 image = query_service.findByQuery(query, params) def make_circle(h, w): x = np.arange(0, w) y = np.arange(0, h) arr = np.zeros((y.size, x.size), dtype=bool)
def run(username, password, plate_id, host, port): conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() query_service = conn.getQueryService() # Create a name for the Original File tablename = "Channels_Min_Max_Intensity" # Go through all wells in Plate, adding row for each plate = conn.getObject("Plate", plate_id) wellIds = [] rowData = [] chCount = 0 for well in plate._listChildren(): well = omero.gateway.WellWrapper(conn, well) image = well.getImage() if image is None: continue wellIds.append(well.id) chCount = image.getSizeC() row = [] print("well, image", well.id, image.id) params = omero.sys.ParametersI() params.addId(image.getPixelsId()) query = """select pixels from Pixels as pixels left outer join fetch pixels.channels as channels join fetch channels.statsInfo where pixels.id=:id""" result = query_service.findAllByQuery(query, params) row = [] for pix in result: for ch in pix.iterateChannels(): si = ch.statsInfo row.extend([si.globalMin.val, si.globalMax.val]) rowData.append(row) print('wellIds', wellIds) print('rowData', rowData) # Now we know how many channels, we can make the table col1 = omero.grid.WellColumn('Well', '', []) columns = [col1] colNames = [] for chIdx in range(chCount): for name in ['Ch%sMin' % chIdx, 'Ch%sMax' % chIdx]: colNames.append(name) columns.append(omero.grid.LongColumn(name, '', [])) table = conn.c.sf.sharedResources().newTable(1, tablename) table.initialize(columns) # Add Data from above data1 = omero.grid.WellColumn('Well', '', wellIds) data = [data1] for colIdx in range(len(rowData[0])): colData = [r[colIdx] for r in rowData] print("colData", len(colData)) name = colNames[colIdx] data.append(omero.grid.LongColumn(name, '', colData)) print("Adding data: ", len(data)) table.addData(data) table.close() print("table closed...") orig_file = table.getOriginalFile() fileAnn = omero.model.FileAnnotationI() fileAnn.ns = rstring(NAMESPACE) fileAnn.setFile(omero.model.OriginalFileI(orig_file.id.val, False)) fileAnn = conn.getUpdateService().saveAndReturnObject(fileAnn) link = omero.model.PlateAnnotationLinkI() link.setParent(omero.model.PlateI(plate_id, False)) link.setChild(omero.model.FileAnnotationI(fileAnn.id.val, False)) print("save link...") conn.getUpdateService().saveAndReturnObject(link) except Exception as exc: print("Error while changing names: %s" % str(exc)) finally: conn.close()
HOST = config.get('OMERODetails', 'host') PORT = config.getint('OMERODetails', 'port') USERNAME = config.get('OMERODetails', 'username') PASSWORD = config.get('OMERODetails', 'password') conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT) connected = conn.connect() if not connected: import sys sys.stderr.write("Error: Connection not available, please check your user name and password.\n") sys.exit(1) session = conn.getSession() queryService = conn.getQueryService() params = Parameters() ldapSearch = LDAPSearch(ldap_details) # Check users query = "from Experimenter" experimenters = queryService.findAllByQuery(query, params) print('Experimenters') for experimenter in experimenters: output = '%s' %experimenter.omeName.getValue() if experimenter.email and experimenter.email.getValue().strip() != '': output = output + ', %s ' %experimenter.email.getValue() if len(ldapSearch.userSearch(experimenter.omeName.getValue())) == 1:
def run(): """ """ dataTypes = [rstring("Plate")] client = scripts.client( "Manage_Plate_Acquisitions.py", "Add or remove PlateAcquisition(s) in a given Plate", scripts.String("Data_Type", optional=False, grouping="1", description="The data type you want to work with.", values=dataTypes, default="Plate"), scripts.List("IDs", optional=False, grouping="2", description="List of Plate IDs").ofType(rlong(0)), scripts.String("Mode", optional=False, grouping="3", description="Select if you want to add or " "remove PlateAcquisitions", values=[rstring("Add"), rstring("Remove")], default="Add"), version="0.2", authors=["Niko Klaric"], institutions=["Glencoe Software Inc."], contact="*****@*****.**", ) try: scriptParams = {} for key in client.getInputKeys(): if client.getInput(key): scriptParams[key] = client.getInput(key, unwrap=True) connection = BlitzGateway(client_obj=client) updateService = connection.getUpdateService() queryService = connection.getQueryService() processedMessages = [] for plateId in scriptParams["IDs"]: plateObj = connection.getObject("Plate", plateId) if plateObj is None: client.setOutput( "Message", rstring("ERROR: No Plate with ID %s" % plateId)) return if scriptParams["Mode"] == "Add": plateAcquisitionObj = PlateAcquisitionI() plateAcquisitionObj.setPlate(PlateI(plateObj.getId(), False)) wellGrid = plateObj.getWellGrid() for axis in wellGrid: for wellObj in axis: wellSampleList = wellObj.copyWellSamples() plateAcquisitionObj.addAllWellSampleSet(wellSampleList) plateAcquisitionObj = updateService.saveAndReturnObject( plateAcquisitionObj) plateAcquisitionId = plateAcquisitionObj.getId()._val processedMessages.append( "Linked new PlateAcquisition with ID %d" " to Plate with ID %d." % (plateAcquisitionId, plateId)) else: params = ParametersI() params.addId(plateId) queryString = """ FROM PlateAcquisition AS pa LEFT JOIN FETCH pa.wellSample LEFT OUTER JOIN FETCH pa.annotationLinks WHERE pa.plate.id = :id """ plateAcquisitionList = queryService.findAllByQuery( queryString, params, connection.SERVICE_OPTS) if plateAcquisitionList: updateList = [] for plate_acquisition in plateAcquisitionList: for well_sample in plate_acquisition.copyWellSample(): well_sample.setPlateAcquisition(None) updateList.append(well_sample) updateService.saveArray(updateList) plate_acquisition.clearWellSample() plate_acquisition.clearAnnotationLinks() plate_acquisition = updateService.saveAndReturnObject( plate_acquisition) updateService.deleteObject(plate_acquisition) processedMessages.append( "%d PlateAcquisition(s) removed from Plate with ID %d." % (len(plateAcquisitionList), plateId)) client.setOutput( "Message", rstring("No errors. %s" % " ".join(processedMessages))) finally: client.closeSession()
# Suggests OMERO.cli commands to fix the idr0004 field-image mapping. # author: [email protected] from omero.gateway import BlitzGateway from omero.rtypes import wrap from omero.sys import ParametersI import os conn = BlitzGateway(os.environ.get('IDR_USER', 'root'), os.environ.get('IDR_PASSWORD', 'omero'), host=os.environ.get('IDR_HOST', 'localhost')) conn.connect() conn.setGroupForSession(3) # Public query_service = conn.getQueryService() # Find the plates of idr0004. query = """ SELECT child.id FROM ScreenPlateLink WHERE parent.name LIKE :name """ params = ParametersI() params.add('name', wrap('idr0004-%')) rows = query_service.projection(query, params) plate_ids = [row[0].val for row in rows]
c_names = [] colors = [] for ch in img.getChannels(): c_names.append(ch.getLabel()) colors.append(ch.getColor().getRGB()) # Save channel names and colors # ================================================================= print("Applying channel Names:", c_names, " Colors:", colors) for i, c in enumerate(new_img.getChannels()): lc = c.getLogicalChannel() lc.setName(c_names[i]) lc.save() r, g, b = colors[i] # need to reload channels to avoid optimistic lock on update c_obj = conn.getQueryService().get("Channel", c.getId()) c_obj.red = rint(r) c_obj.green = rint(g) c_obj.blue = rint(b) c_obj.alpha = rint(255) conn.getUpdateService().saveObject(c_obj) new_img.resetRDefs() # reset based on colors above # Apply pixel sizes from original image # ================================================================= new_pix = conn.getQueryService().get("Pixels", new_img.getPixelsId()) new_pix.setPhysicalSizeX(pixels.getPhysicalSizeX()) new_pix.setPhysicalSizeY(pixels.getPhysicalSizeY()) new_pix.setPhysicalSizeZ(pixels.getPhysicalSizeZ()) conn.getUpdateService().saveObject(new_pix)
parser.add_argument("--to-user", default="root") parser.add_argument("--to-pass", default="omero") parser.add_argument("source_image", type=int, help="input image") parser.add_argument("target_image", type=int, help="output image") ns = parser.parse_args() image_id_src = ns.source_image image_id_dst = ns.target_image idr = BlitzGateway(ns.from_user, ns.from_pass, host=ns.from_host, secure=True) local = BlitzGateway(ns.to_user, ns.to_pass, host=ns.to_host, secure=True) idr.connect() local.connect() query_service = idr.getQueryService() update_service = local.getUpdateService() query = "FROM Mask WHERE roi.image.id = :id" params = ParametersI() params.addId(image_id_src) count = 0 for mask_src in query_service.findAllByQuery(query, params): mask_dst = MaskI() mask_dst.x = mask_src.x mask_dst.y = mask_src.y mask_dst.width = mask_src.width mask_dst.height = mask_src.height
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print username conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() dataset = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(dataset) == 0: print "No dataset with name %s found" % target continue dataset_id = dataset[0].getId().getValue() print 'dataset', dataset_id dataset = conn.getObject("Dataset", dataset_id) kvp_set1 = [["mitomycin-A", "0mM"], ["PBS", "10mM"], ["incubation", "10min"], ["temperature", "37"], ["Organism", "H**o sapiens"]] kvp_set2 = [["mitomycin-A", "20mM"], ["PBS", "10mM"], ["incubation", "10min"], ["temperature", "37"], ["Organism", "H**o sapiens"]] kvp_set3 = [["mitomycin-A", "10microM"], ["PBS", "10mM"], ["incubation", "5min"], ["temperature", "37"], ["Organism", "H**o sapiens"]] kvp_set4 = [["mitomycin-A", "0mM"], ["PBS", "10mM"], ["incubation", "5min"], ["temperature", "68"], ["Organism", "H**o sapiens"]] images_kvp_order = [('A10.pattern1.tif', kvp_set1), ('A10.pattern2.tif', kvp_set2), ('A10.pattern5.tif', kvp_set2), ('A1.pattern1.tif', kvp_set4), ('A1.pattern2.tif', kvp_set1), ('A5.pattern1.tif', kvp_set3), ('A5.pattern2.tif', kvp_set2), ('A5.pattern3.tif', kvp_set2), ('A5.pattern4.tif', kvp_set2), ('A6.pattern1.tif', kvp_set3), ('A6.pattern2.tif', kvp_set2), ('A6.pattern3.tif', kvp_set2), ('B12.pattern1.tif', kvp_set1), ('B12.pattern2.tif', kvp_set1), ('B12.pattern3.tif', kvp_set1), ('B12.pattern4.tif', kvp_set3), ('B12.pattern5.tif', kvp_set3), ('C4.pattern1.tif', kvp_set2), ('C4.pattern2.tif', kvp_set2), ('C4.pattern3.tif', kvp_set2), ('C4.pattern4.tif', kvp_set2), ('C4.pattern5.tif', kvp_set2), ('C4.pattern6.tif', kvp_set2), ('C4.pattern7.tif', kvp_set3), ('C4.pattern8.tif', kvp_set3), ('C4.pattern9.tif', kvp_set1), ('C4.pattern.tif', kvp_set1), ('E4.pattern5.tif', kvp_set3), ('E4.pattern6.tif', kvp_set1), ('E4.pattern7.tif', kvp_set3), ('E4.pattern8.tif', kvp_set3), ('E4.pattern9.tif', kvp_set1)] images_kvp_order = dict(images_kvp_order) for image in dataset.listChildren(): if image.getName() in images_kvp_order: print images_kvp_order[image.getName()] key_value_data = images_kvp_order[image.getName()] map_ann = omero.gateway.MapAnnotationWrapper(conn) # Use 'client' namespace to allow editing in Insight & web namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION map_ann.setNs(namespace) map_ann.setValue(key_value_data) map_ann.save() # NB: only link a client map annotation to a single object image.linkAnnotation(map_ann) print 'linking to image', image.getName() except Exception as exc: print "Error while setting key-value pairs: %s" % str(exc) finally: conn.close()
colors = [] for ch in image.getChannels(): cNames.append(ch.getLabel()) colors.append(ch.getColor().getRGB()) # Save channel names and colors # ================================================================= print "Applying channel Names:", cNames, " Colors:", colors for i, c in enumerate(newImg.getChannels()): lc = c.getLogicalChannel() lc.setName(cNames[i]) lc.save() r, g, b = colors[i] # need to reload channels to avoid optimistic lock on update cObj = conn.getQueryService().get("Channel", c.id) cObj.red = rint(r) cObj.green = rint(g) cObj.blue = rint(b) cObj.alpha = rint(255) conn.getUpdateService().saveObject(cObj) newImg.resetRDefs() # reset based on colors above # Apply pixel sizes from original image # ================================================================= newPix = conn.getQueryService().get("Pixels", newImg.getPixelsId()) physicalSizeX = pixels.getPhysicalSizeX() if physicalSizeX is not None: newPix.setPhysicalSizeX(rdouble(physicalSizeX))
def run(password, target, host, port): for i in range(1, 51): username = "******" % i print(username) conn = BlitzGateway(username, password, host=host, port=port) try: conn.connect() params = omero.sys.ParametersI() params.addString('username', username) query = "from Dataset where name='%s' \ AND details.owner.omeName=:username" % target query_service = conn.getQueryService() datasets = query_service.findAllByQuery(query, params, conn.SERVICE_OPTS) if len(datasets) == 0: print("No datasets with name %s found" % target) continue dataset_id = datasets[0].getId().getValue() print('dataset', dataset_id) params2 = omero.sys.ParametersI() params2.addId(dataset_id) query = "select l.child.id from DatasetImageLink l \ where l.parent.id = :id" images = query_service.projection(query, params2, conn.SERVICE_OPTS) for k in range(0, len(images)): image_id = images[k][0].getValue() delta_t = 300 image = conn.getObject("Image", image_id) params = omero.sys.ParametersI() params.addLong('pid', image.getPixelsId()) query = "from PlaneInfo as Info where \ Info.theZ=0 and Info.theC=0 and pixels.id=:pid" info_list = query_service.findAllByQuery( query, params, conn.SERVICE_OPTS) print('info_list', len(info_list)) if len(info_list) == 0: print("Creating info...", image.getSizeT()) info_list = [] for t_index in range(image.getSizeT()): print(' t', t_index) info = PlaneInfoI() info.theT = rint(t_index) info.theZ = rint(0) info.theC = rint(0) info.pixels = PixelsI(image.getPixelsId(), False) dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) info_list.append(info) else: for info in info_list: unwrap_t = unwrap(info.theT) unwrap_z = unwrap(info.theZ) print('theT %s, theZ %s' % (unwrap_t, unwrap_z)) t_index = info.theT.getValue() dt = t_index * delta_t info.deltaT = TimeI(dt, UnitsTime.SECOND) print("Saving info_list", len(info_list)) conn.getUpdateService().saveArray(info_list) except Exception as exc: print("Error when setting the timestamps: %s" % str(exc)) finally: conn.close()
Used after import of SPW data to provide spatial settings on well samples. Tested by spw_test. """ import argparse import omero from omero.gateway import BlitzGateway from omero.model.enums import UnitsLength parser = argparse.ArgumentParser() parser.add_argument('host', help='OMERO host') parser.add_argument('port', help='OMERO port') parser.add_argument('key', help='OMERO session key') parser.add_argument('plateId', help='Plate ID to process', type=int) args = parser.parse_args() conn = BlitzGateway(host=args.host, port=args.port) conn.connect(args.key) update = conn.getUpdateService() plate = conn.getObject('Plate', args.plateId) r = UnitsLength.REFERENCEFRAME cols = 3 for well in plate.listChildren(): for i, ws in enumerate(well.listChildren()): x = i % cols y = i / cols ws = conn.getQueryService().get('WellSample', ws.id) ws.posY = omero.model.LengthI(y, r) ws.posX = omero.model.LengthI(x, r) update.saveObject(ws)