def create_containers(self, cli, project, dataset): """ Creates containers with names provided if they don't exist already. Returns Dataset ID. """ sessionId = cli._event_context.sessionUuid conn = BlitzGateway(host='localhost') conn.connect(sUuid = sessionId) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = wrap(conn.getUser().getId()) d = None dsId = None if project is not None: # We need to find or create a project # This is not nice but we really shouldn't be dealing with large numbers of objects here plist = list(conn.getObjects("Project", attributes={'name': project}, params=params)) if len(plist) == 0: # Create project and dataset then link p = self.create_project(conn, project) d = self.create_dataset(conn, dataset) dsId = d.id.val self.link_dataset(conn, p.id.val, dsId) else: # Pick the first, it's as good as any p = plist[0] print "Using existing Project:", project # Since Project already exists check children for dataset for c in p.listChildren(): if c.getName() == dataset: d = c dsId = d.getId() # No existing child dataset so create one and link if d is None: d = self.create_dataset(conn, dataset) dsId = d.id.val self.link_dataset(conn, p.getId(), dsId) else: print "Using existing Dataset:", dataset else: # There may be more than one dataset with the same name # This is not nice but we really shouldn't be dealing with large numbers of objects here dlist = list(conn.getObjects("Dataset", attributes={'name': dataset}, params=params)) if len(dlist) != 0: # We want one without a parent, the first will do for c in dlist: if len(c.listParents()) == 0: d = c dsId = d.getId() if d is None: dsId = self.create_dataset(conn, dataset).id.val else: print "Using existing Dataset:", dataset return dsId
def main(): try: conn = BlitzGateway(username, password, host=server, port=4064) conn.connect() #Returns True when connected updateService = conn.getUpdateService() from omero.rtypes import rdouble, rint, rstring images = conn.getObjects("Image", opts={ 'owner': owner_id, 'dataset': datasetID }) for image in images: imageId = image.getId() roi_service = conn.getRoiService() result = roi_service.findByImage(imageId, None) for roi in result.rois: print("ROI: ID:", roi.getId().getValue()) for s in roi.copyShapes(): if type(s) == omero.model.EllipseI: ctr_x = s.getX().getValue() ctr_y = s.getY().getValue() x, y, w, h = rect_roi_frm_ctr(image, ctr_x, ctr_y, width, height) MAX_GFP = max_proj(image, max_ch, x, y, w, h) BFZ9 = load_stack_plane(image, slc_ch, slc, x, y, w, h) hstk = np.stack((MAX_GFP, BFZ9), axis=0) tmp_str = 'output_' + str(imageId) + '_' + str( roi.getId().getValue()) + '.tif' output_hyperstack(hstk, tmp_str) finally: if conn: conn.close()
def run(name, password, dataset_name, anntype, ns, host, port): conn = BlitzGateway(name, password, host=host, port=port) try: conn.connect() datasets = conn.getObjects("Dataset", attributes={"name": dataset_name}) for dataset in datasets: print dataset.getId() ann_ids = [] given_type = None if anntype == "map": given_type = omero.model.MapAnnotationI if anntype == "file": given_type = omero.model.FileAnnotationI if ns == "none": ns = None for image in dataset.listChildren(): for a in image.listAnnotations(ns): if a.OMERO_TYPE == given_type: print a.getId(), a.OMERO_TYPE, a.ns ann_ids.append(a.id) # Delete the annotations link to the dataset for a in dataset.listAnnotations(ns): if a.OMERO_TYPE == given_type: print a.getId(), a.OMERO_TYPE, a.ns ann_ids.append(a.id) if len(ann_ids) > 0: print "Deleting %s annotations..." % len(ann_ids) conn.deleteObjects('Annotation', ann_ids, wait=True) except Exception as exc: print "Error while deleting annotations: %s" % str(exc) finally: conn.close()
def run(name, password, dataset_name, dataset_id, host, port): conn = BlitzGateway(name, password, host=host, port=port) try: conn.connect() roi_service = conn.getRoiService() datasets = [] if dataset_id >= 0: datasets.append(conn.getObject("Dataset", dataset_id)) else: datasets = conn.getObjects("Dataset", attributes={"name": dataset_name}) for dataset in datasets: print(dataset.getId()) for image in dataset.listChildren(): result = roi_service.findByImage(image.getId(), None, conn.SERVICE_OPTS) if result is not None: roi_ids = [roi.id.val for roi in result.rois] if len(roi_ids) > 0: print("Deleting %s ROIs..." % len(roi_ids)) conn.deleteObjects("Roi", roi_ids, wait=True) except Exception as exc: print("Error while deleting Rois: %s" % str(exc)) finally: conn.close()
def processImages(client, scriptParams): message = '' # for params with default values, we can get the value directly dataType = scriptParams['Data_Type'] ids = scriptParams['IDs'] # Get the datasets conn = BlitzGateway(client_obj = client) objects, logMessage = script_utils.getObjects(conn, scriptParams) message += logMessage if not objects: return message datasets = conn.getObjects(dataType, ids) good, chNames, msg = checkChannels(datasets) message += msg if not good: raise omero.ServerError( 'Channel check failed, ' + 'all images must have the same channels: %s' % message) return message
def create_screen(self, cli, screen): """ Creates screen with name provided if it doesn't exist already. Returns Screen ID. """ sessionId = cli._event_context.sessionUuid conn = BlitzGateway(host='localhost') conn.connect(sUuid = sessionId) params = omero.sys.Parameters() params.theFilter = omero.sys.Filter() params.theFilter.ownerId = wrap(conn.getUser().getId()) slist = list(conn.getObjects("Screen", attributes={'name': screen}, params=params)) if len(slist) == 0: print "Creating Screen:", screen s = ScreenI() s.name = wrap(screen.encode('ascii','ignore')) scrId = conn.getUpdateService().saveAndReturnObject(s).id.val else: scrId = slist[0].getId() print "Using Screen:", screen return scrId
Helper method to display info about OMERO objects. Not all objects will have a "name" or owner field. """ # print >> sys.stderr, """%s%s:%s Name:"%s" (owner=%s)""" % (\ # " " * indent, # obj.OMERO_CLASS,\ # obj.getId(),\ # obj.getName(),\ # obj.getOwnerOmeName()) # Retrieve Screening data: # ================================================================= conn.SERVICE_OPTS.setOmeroGroup('-1') ### Set the group to search within. -1 for all groups. print "ScreenID,Screen Name,PlateID,Plate Name,Image,ImageID" for screen in conn.getObjects("Screen"): print_obj(screen) for plate in screen.listChildren(): print_obj(plate, 5) plateId = plate.getId() for well in plate.listChildren(): index = well.countWellSample() for index in xrange(0, index): print ",".join(map(str, [\ screen.getId(),\ screen.getName(),\ plateId, '"' + \ plate.getName(),\ well.getImage(index).getName() + '"',\ well.getImage(index).getId()]))
scripts.List("IDs", grouping="2", optional=False).ofType(rlong(0)), scripts.String("First_key", grouping="3", default=""), scripts.String("First_value", grouping="4", default=""), scripts.String("Second_key", grouping="5", default=""), scripts.String("Second_value", grouping="6", default=""), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) print script_params namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION # get the 'IDs' parameter (which we have restricted to 'Image' IDs) ids = unwrap(client.getInput("IDs")) images = conn.getObjects("Dataset", ids) first_k = script_params["First_key"] first_v = script_params["First_value"] second_k = script_params["Second_key"] second_v = script_params["Second_value"] for i in images: print i.name key_value_data = [] to_delete = [] for ann in i.listAnnotations(ns=namespace): kv = ann.getValue() key_value_data.extend(kv) to_delete.append(ann.id)
# Cross-group query to find file conn.SERVICE_OPTS.setOmeroGroup(-1) # Create tab-separated file for Zegami collection format = 'PDF' zegami_tsv = "zegami.tsv" y = open(zegami_tsv, 'w') z = csv.writer(y, delimiter="\t") z.writerow([ "Gene", "Collection", "Compartment", "probe", "image", "figure_name", "figure_id" ]) # Get OMERO.Figure ID from files with "Zegami3" in the filename num = 1 for f in conn.getObjects("FileAnnotation", attributes={"ns": "omero.web.figure.json"}): figure_name = f.getFile().getName().upper() id = f.getId() if "ZEGAMI3" in figure_name and "TEMPLATE" not in figure_name: ann = conn.getObject("FileAnnotation", id) if ann is None: raise Exception('ups! No file annotation with id %d' % id) print('generating image for figure %d' % id) # Build .pdf of the Figure from the json file json = "".join(ann.getFileInChunks()) script_params = { 'Figure_JSON': json, 'Webclient_URI': 'https://omero1.bioch.ox.ac.uk/webclient/',
return channel_name # create the omero url string for cell profiler default_omero_url = "omero:iid=" # open the output file with open(out_file_name, 'w') as out_file: # declare a list to hold all channel names master_channel_list = [] # declare a list to hold the omero file urls url_list = [] # for each image in the dataset for image in conn.getObjects('Image', opts={'dataset': dataset_id}): print_obj(image) # get the channel channel = get_channel(image) # add the channel to the master channel list master_channel_list.append(channel) # save the image channel and url master_channel_list.append(channel) # get the image id image_id = image.getId() # create and save the image url to the url list
# we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) #search for tags in all the groups conn.SERVICE_OPTS.setOmeroGroup('-1') script_params = client.getInputs(unwrap=True) print script_params #get first the user ID as we want to search only for the tags that do not belong to the user id which is the admin myOwnerId = conn.getUserId() print 'myOwnerId', myOwnerId #create a list of tags and append only the tags, description,tag id and tag owner id to it tags = [] for tag in conn.getObjects("TagAnnotation"): owner = tag.getDetails().owner.id.val print tag.textValue, owner if owner == myOwnerId: continue desc = tag.description if tag.description else "" tags.append([tag.textValue, desc, str(tag.id), str(owner)]) #sort the tags in descending order to allow to see duplicates tags.sort(key=lambda tag: tag[0].islower()) #create the csv file in which to copy the tag list with open("tags_to_check-" + str(date.today()) + ".csv", "w") as f: f.write("tag name, description, tag ID, owner ID\n") for t in tags:
conn = BlitzGateway('root', 'omero', host='localhost') conn.connect() conn.SERVICE_OPTS.setOmeroGroup(-1) # ---- CONFIGURATION ---- TAG_COUNT = 1 # Number of tags each user should use (link) TAG_TARGETS = ['Project', 'Dataset', 'Image', "Screen", "Plate"] ROI_COUNT = 3 allUsers = [] for exp in conn.getObjects("Experimenter"): n = exp.getName() if n not in ["root", "guest"]: print n allUsers.append(exp) def addRect(roi, x=10, y=10, w=100, h=50, theZ=0, theT=0, label=None): """ create and save a rectangle shape, add it to roi """ rect = omero.model.RectI() rect.x = rdouble(x) rect.y = rdouble(y) rect.width = rdouble(w) rect.height = rdouble(h) if theZ is not None: rect.theZ = rint(theZ)
# ## Method # ### Connect to OMERO # In[5]: conn = BlitzGateway(usernm, passwd, host='camdu.warwick.ac.uk', port=4064) conn.connect() #Returns true when connected # ### Get OMERO IDs # List the details of the datasets we are interested in from OMERO. We need the IDs to call the images we want to analyse. The output allows us to identify the relevant datasets. # In[6]: print("\nList Datasets: \n", "=" * 50) datasets = conn.getObjects("Dataset", opts={'owner': 3}) #get all datasets from owner 3 (SR) keys = [] #create empty lists values = [] for obj in datasets: print("""%s%s:%s Name:"%s" (owner=%s)""" % (" " * 2, obj.OMERO_CLASS, obj.getId(), obj.getName(), obj.getOwnerOmeName())) keys.append(obj.getName()) #gather dataset names to use in dictionary values.append(obj.getId()) #gather dataset IDs to use in dictionary # Put names and IDs in dictionary to make it easy to call required datasets. # In[7]: Datasets = dict(zip(keys[:-1], values[:-1]))
sessionUuid = client.getInput("sessionUuid", unwrap=True) ids = unwrap(client.getInput("IDs")) # The managed_dir is where the local images are stored. managed_dir = client.sf.getConfigService().getConfigValue("omero.managed.dir") # Connect to remote omero c = omero.client(host=host, port=port, args=["--Ice.Config=/dev/null", "--omero.debug=1"]) c.joinSession(sessionUuid) try: # Just to test connection, print the projects. remote_conn = BlitzGateway(client_obj=c) for p in remote_conn.getObjects("Project"): print p.id, p.name print "Connected to ", REMOTE_HOST, ", now to transfer image" cli = omero.cli.CLI() cli.loadplugins() cli.set_client(c) del os.environ["ICE_CONFIG"] # Find image files uploaded_image_ids = [] for image_id in ids: image = local_conn.getObject("Image", image_id) print image.getName() temp_file = NamedTemporaryFile().name
# Use 'client' namespace to allow editing in Insight & web #namespace = NSCLIENTMAPANNOTATION # get the 'IDs' parameter (will match the id of the selected object to which the csv file has been attached) ids = unwrap(client.getInput("IDs")) file_id = script_params["File_Annotation"] file_ann = conn.getObject("FileAnnotation", file_id) csv_text = "".join(list(file_ann.getFileInChunks())) print csv_text lines = csv_text.split("\n") print lines #connect to the object objects = conn.getObjects(data_type, ids) print "objects", objects #create the list of the objects (Datasets, images or both) object_list = [] #loop for looking at data in project/datasets and images if data_type == 'Dataset': for ds in objects: if add_children: object_list.extend(list(ds.listChildren())) if add_self: object_list.append(ds) else: object_list = objects
# Create Tags # ================================================================= object_array = list() for i in xrange(3): tag = omero.model.TagAnnotationI() tag.setTextValue(rstring(tag_name)) tag.setDescription(rstring("%s %i" % (tag_name, i))) object_array.append(tag) conn.getUpdateService().saveArray(object_array) # Find the datasets by name. # ================================================================= datasets = conn.getObjects("Dataset", attributes={'name': dataset_name}) print "\nList Datasets:" for d in datasets: print "ID:", d.getId(), "Name:", d.getName() # Find the tag by textValue # ================================================================= Tags = conn.getObjects("TagAnnotation", attributes={'textValue': tag_name}) print "\nList Tags:" for t in Tags: print "ID:", t.getId(), "Text:", t.getTextValue(), "Desc:", t.getDescription() # Close connection: # =================================================================
obj.getOwnerOmeName(), obj.getDate(), obj.getDescription()) # open the json output file name with open(out_file_name, 'w') as out_file: # declare a list to hold all dataset data dataset_list = [] # get the user id and group id my_exp_id = conn.getUser().getId() default_group_id = conn.getEventContext().groupId # for each project filtered by this user for project in conn.getObjects("Project", opts={ 'owner': my_exp_id, 'group': default_group_id, 'order_by': 'lower(obj.name)' }): # assert that the project is owned by the user assert project.getDetails().getOwner().id == my_exp_id # declare a dict to store the dataset info dataset_dict = {} # for each dataset in the project for dataset in project.listChildren(): # store select dataset data in a dict dataset_id = "%s" % dataset.getId() dataset_dict['dataset_id'] = dataset_id dataset_dict['dataset_name'] = dataset.getName()
scripts.List("IDs", grouping="2", optional=False).ofType(rlong(0)), scripts.String("First_key", grouping="3", default=""), scripts.String("First_value", grouping="4", default=""), scripts.String("Second_key", grouping="5", default=""), scripts.String("Second_value", grouping="6", default=""), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) print script_params namespace = omero.constants.metadata.NSCLIENTMAPANNOTATION # get the 'IDs' parameter (which we have restricted to 'Dataset' IDs) ids = unwrap(client.getInput("IDs")) datasets = conn.getObjects("Dataset", ids) first_k = script_params["First_key"] first_v = script_params["First_value"] second_k = script_params["Second_key"] second_v = script_params["Second_value"] for ds in datasets: print ds.name key_value_data = [] to_delete = [] for ann in ds.listAnnotations(ns=namespace): kv = ann.getValue() key_value_data.extend(kv) to_delete.append(ann.id)
USER_NAMES = ["user-10", "user-11", "user-12"] # USER_NAMES = ["user-7", "user-8", "user-9", "user-10", "user-11", "user-12"] conn = BlitzGateway('root', 'omero', host='localhost') conn.connect() conn.SERVICE_OPTS.setOmeroGroup(-1) # ---- CONFIGURATION ---- TAG_COUNT = 1 # Number of tags each user should use (link) TAG_TARGETS = ['Project', 'Dataset', 'Image', "Screen", "Plate"] ROI_COUNT = 3 allUsers = [] for exp in conn.getObjects("Experimenter"): n = exp.getName() if n not in ["root", "guest"]: print n allUsers.append(exp) def addRect(roi, x=10, y=10, w=100, h=50, theZ=0, theT=0, label=None): """ create and save a rectangle shape, add it to roi """ rect = omero.model.RectI() rect.x = rdouble(x) rect.y = rdouble(y) rect.width = rdouble(w) rect.height = rdouble(h) if theZ is not None: rect.theZ = rint(theZ)
class OMEROConnection(object): def __init__(self, args, configs): self.args = args self.configs = configs self.connect_with = self.configs['CONNECT_WITH'] self.host = self.configs['OMERO_{}_HOST'.format(self.connect_with)] self.user = self.configs['OMERO_{}_USER'.format(self.connect_with)] self.password = self.configs['OMERO_{}_PASSWORD'.format( self.connect_with)] self.port = self.configs['OMERO_{}_PORT'.format(self.connect_with)] def __enter__(self): from omero.gateway import BlitzGateway # @UnresolvedImport self.conn = BlitzGateway(host=self.host, username=self.user, passwd=self.password, port=self.port) self.connected = self.conn.connect() # failed to connect if not self.connected: print_date( "Unable to connect to host '{}' on port {} using user '{}'". format(self.host, self.port, self.user)) print_date("Check that server is up") sys.exit(1) # keepalive self.conn.c.enableKeepAlive(5) self.omero_user = self.conn.getUser() self.userId = self.omero_user.getId() self.updateService = self.conn.getUpdateService() self.roiService = self.conn.getRoiService() return self def __exit__(self, exc_type, exc_value, traceback): # @UnusedVariable self.conn._closeSession() if self.args.verbose: print_date('Connection closed.') self.connected = False def list(self): """List images or ROIs :return int count: the number of images/ROIs """ if self.args.images: print_date("Listing images...") images = self.images() if self.args.summary: pass else: print_date("Structuring output...") image_view = ImageView(images) print(image_view) return len(images) elif self.args.rois: print_date("Listing ROIs...") rois = self.rois() if self.args.summary: pass else: print_date("Structuring output...") roi_view = ROIView(rois) print(roi_view) return len(rois) @property def projects(self): if self.args.project is not None: projects = self.conn.searchObjects(["Project"], self.args.project) return projects else: return self.conn.listProjects(self.userId) def datasets(self, project=None): """List the datasets associated with the current user :param project: a project :param bool in_project: are these datasets contained within a project? """ datasets = list() if self.args.dataset is not None: datasets += self.conn.searchObjects(["Dataset"], self.args.dataset) else: if project is not None: projects = self.conn.searchObjects(["Project"], project) for p in projects: datasets += p.listChildren() else: params = omero.sys.ParametersI() # @UndefinedVariable params.exp(self.userId) datasets += self.conn.getObjects("Dataset", params=params) return datasets def images(self, project=None, dataset=None): """The list of images associated with the current user If the image ID is specified only the required image is returned. (The project and dataset are ignored.) Otherwise: - If a project object is provided all images in all datasets in the project are returned. (The dataset is ignored.) - If a project object and dataset object are provided then only those images in the project and dataset are return. - If no project object is provided but a dataset object is provided then only those images in the dataset are returned. :param str project: OMERO project name :param str dataset: OMERO dataset name :return list images: a list of OMERO ``Image`` objects """ # assertions images = list() print_date("Retrieving images...") if self.args.image_id is not None: try: assert isinstance(self.args.image_id, int) or isinstance( self.args.image_id, long) except AssertionError: print_date("Invalid type for image ID: {}".format( type(self.args.image_id))) sys.exit(1) image = self.getImage(self.args.image_id) if image is not None: images.append(image) elif self.args.image_name is not None: images = self.conn.searchObjects(["Image"], self.args.image_name) else: if project is not None: # project specified print_date( "Searching for images in project '{}'".format(project)) # get all projects matching projects = self.conn.searchObjects(["Project"], project) # get all datasets in projects matching datasets_in_projects = dict() for p in projects: for d in p.listChildren(): datasets_in_projects[d.getName()] = d print_date("Found {} datasets in project '{}'".format( len(datasets_in_projects), project)) # dataset specified if dataset is not None: print_date( "Searching for images in dataset '{}'".format(dataset)) if dataset in datasets_in_projects.keys(): images += datasets_in_projects[dataset].listChildren() else: # dataset not specified print_date( "Searching for images in all {} datasets".format( len(datasets_in_projects))) for dataset in datasets_in_projects.keys(): images += datasets_in_projects[dataset].listChildren() else: # project not specified # dataset specified if dataset is not None: print_date( "Searching for images in dataset '{}'".format(dataset)) datasets = self.conn.searchObjects(["Dataset"], dataset) for dataset in datasets: images += dataset.listChildren() else: datasets = self.datasets() print_date( "Searching for images in all {} datasets".format( len(datasets))) for dataset in datasets: images += dataset.listChildren() print_date("Found {} image(s).".format(len(images))) return images def getImage(self, image_id): """Get the image with the image ID specified on the command line :param int image_id: command line arguments :return image: an image :rtype image: ``OMEROImage`` """ return self.conn.getObject("Image", image_id) def rois(self, project=None, dataset=None): """Get an iterator over the ROIs associated with the specified image ID :param int image_id: image ID """ rois = list() print_date("Retrieving ROIs...") if self.args.image_id is not None: return self.getROIs(self.args.image_id) else: for image in self.images(project, dataset): if image.getROICount() > 0: rois.append((image, self.getROIs(image.getId()))) roi_count = sum(map(lambda r: len(r[1]), rois)) print_date("Found {:,} ROIs in {:,} images.".format( roi_count, len(rois))) return rois def getROIs(self, image_id): result = self.roiService.findByImage(image_id, None) return result.rois def attachRois(self, omero_rois): """Attach the rois from the iterable""" non_rois = filter(lambda r: not isinstance(r, OMEROROI), omero_rois) try: assert len(non_rois) == 0 except AssertionError: print_date("Found {:,} non-ROI objects".format(len(non_rois))) return 1 for roi in omero_rois: self.saveRoi(roi) return os.EX_OK # save def saveRoi(self, roi): """Save the given ROI :param roi: an ROI object :type roi: `omero.model.Roi` """ import Ice try: self.updateService.saveObject(roi) except Ice.MemoryLimitException as e: # @UndefinedVariable print_date(str(e)) sys.exit(1) # delete def deleteRoi(self, roi_id): """ Delete the given ROI :param roi: an ROI object :type roi: `omero.model.Roi` """ from omero.callbacks import CmdCallbackI # @UnresolvedImport handle = self.conn.deleteObjects("Roi", [roi_id], deleteAnns=True, deleteChildren=True) callback = CmdCallbackI(self.conn.c, handle) while not callback.block(500): if self.args.verbose: print_date(".", newline=False, incl_date=False) time.sleep(2) callback.close(True)
for image in dataset.listChildren(): print_obj(image, 4) # Retrieve the datasets owned by the user currently logged in: # ================================================================= # Here we create an omero.sys.ParametersI instance which we # can use to filter the results that are returned. If we did # not pass the params argument to getObjects, then all Datasets # in the current group would be returned. print "\nList Datasets:" print "=" * 50 params = omero.sys.ParametersI() params.exp(conn.getUser().getId()) # only show current user's Datasets datasets = conn.getObjects("Dataset", params=params) for dataset in datasets: print_obj(dataset) # Retrieve the images contained in a dataset: # ================================================================= print "\nDataset:%s" % datasetId print "=" * 50 dataset = conn.getObject("Dataset", datasetId) print "\nImages in Dataset:", dataset.getName() for image in dataset.listChildren(): print_obj(image) # Retrieve an image by Image ID: # ================================================================= image = conn.getObject("Image", imageId)
object_array.append(dataset) conn.getUpdateService().saveArray(object_array) # Create Tags # ================================================================= object_array = list() for i in xrange(3): tag = omero.model.TagAnnotationI() tag.setTextValue(rstring(tag_name)) tag.setDescription(rstring("%s %i" % (tag_name, i))) object_array.append(tag) conn.getUpdateService().saveArray(object_array) # Find the datasets by name. # ================================================================= datasets = conn.getObjects("Dataset", attributes={'name': dataset_name}) print "\nList Datasets:" for d in datasets: print "ID:", d.getId(), "Name:", d.getName() # Find the tag by textValue # ================================================================= Tags = conn.getObjects("TagAnnotation", attributes={'textValue': tag_name}) print "\nList Tags:" for t in Tags: print "ID:", t.getId(), "Text:", t.getTextValue( ), "Desc:", t.getDescription() # Close connection: # ================================================================= # When you're done, close the session to free up server resources.
class TableConnection(object): """ A basic client-side wrapper for OMERO.tables which handles opening and closing tables. """ def __init__(self, user = None, passwd = None, host = 'localhost', client = None, tableName = None, tableId = None): """ Create a new table handler, either by specifying user and passwd or by providing a client object (for scripts) @param user Username @param passwd Password @param host The server hostname @param client Client object with an active session @param tableName The name of the table file @param tableId The OriginalFile ID of the table file """ if not client: client = omero.client(host) sess = client.createSession(user, passwd) client.enableKeepAlive(60) else: sess = client.getSession() self.conn = BlitzGateway(client_obj = client) self.res = sess.sharedResources() if (not self.res.areTablesEnabled()): raise TableConnectionError('OMERO.tables not enabled') repos = self.res.repositories() self.rid = repos.descriptions[0].id.val self.tableName = tableName self.tableId = tableId self.table = None def __enter__(self): print 'Entering Connection' return self def __exit__(self, type, value, traceback): print 'Exiting Connection' self.close() def close(self): print 'Closing Connection' try: self.closeTable() finally: self.conn._closeSession() def openTable(self, tableId = None, tableName = None): """ Opens an existing table by ID or name. If there are multiple tables with the same name this throws an error (should really use an annotation to keep track of this). If tableId is supplied it will be used in preference to tableName @param tableName The name of the table file @param tableId The OriginalFile ID of the table file @return handle to the table """ if not tableId and not tableName: tableId = self.tableId tableName = self.tableName if not tableId: if not tableName: tableName = self.tableName attrs = {'name': tableName} ofiles = list( self.conn.getObjects("OriginalFile", attributes = attrs)) if len(ofiles) > 1: raise TableConnectionError( 'Multiple tables with name:%s found' % tableName) if not ofiles: raise TableConnectionError( 'No table found with name:%s' % tableName) ofile = ofiles[0] else: attrs = {'id': long(tableId)} if tableName: attrs['name'] = tableName ofile = self.conn.getObject("OriginalFile", attributes = attrs) if not ofile: raise TableConnectionError('No table found with name:%s id:%s' % (tableName, tableId)) if self.tableId == ofile.getId(): print 'Using existing connection to table name:%s id:%d' % \ (tableName, tableId) else: self.closeTable() self.table = self.res.openTable(ofile._obj) self.tableId = ofile.getId() print 'Opened table name:%s id:%d' % (tableName, self.tableId) try: print '\t%d rows %d columns' % \ (self.table.getNumberOfRows(), len(self.table.getHeaders())) except omero.ApiUsageException: pass self.tableId = tableId return self.table def deleteAllTables(self): """ Delete all tables with self.tableName Will fail if there are any annotation links """ ofiles = self.conn.getObjects("OriginalFile", \ attributes = {'name': self.tableName}) ids = [f.getId() for f in ofiles] print 'Deleting ids:%s' % ids self.conn.deleteObjects('OriginalFile', ids) def dumpTable(self, table): """ Print out the table """ headers = table.getHeaders() print ', '.join([t.name for t in headers]) nrows = table.getNumberOfRows() #data = table.readCoordinates(xrange(table.getNumberOfRows)) for r in xrange(nrows): data = table.read(range(len(headers)), r, r + 1) print ', '.join(['%.2f' % c.values[0] for c in data.columns]) def closeTable(self): """ Close the table if open, and set table and tableId to None """ try: if self.table: self.table.close() finally: self.table = None self.tableId = None def newTable(self, schema): """ Create a new uninitialised table @param schema the table description @return A handle to the table """ self.closeTable() self.table = self.res.newTable(self.rid, self.tableName) ofile = self.table.getOriginalFile() self.tableId = ofile.getId().getValue() try: self.table.initialize(schema) print "Initialised '%s' (%d)" % (self.tableName, self.tableId) except Exception as e: print "Failed to create table: %s" % e try: self.table.delete except Exception as ed: print "Failed to delete table: %s" % ed self.table = None self.tableId = None raise e return self.table def chunkedRead(self, colNumbers, start, stop, chunk): """ Split a call to table.read(), into multiple chunks to limit the number of rows returned in one go. @param colNumbers A list of columns indices to be read @param start The first row to be read @param stop The last + 1 row to be read @param chunk The maximum number of rows to read in each call @return a data object, note lastModified will be set to the timestamp the first chunked call """ p = start q = min(start + chunk, stop) data = self.table.read(colNumbers, p, q) p, q = q, min(q + chunk, stop) while p < stop: data2 = self.table.read(colNumbers, p, q) data.rowNumbers.extend(data2.rowNumbers) for (c, c2) in izip(data.columns, data2.columns): c.values.extend(c2.values) p, q = q, min(q + chunk, stop) return data
def run_script(): # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - data_types = [rstring('Dataset')] client = scripts.client( 'Export_kvpairs_to_csv', """ This script reads the key values pairs of the images "children" of the dataset and creates a csv file which is then attached to the dataset """, scripts.String( "Data_Type", optional=False, grouping="1", description="Choose source of images", values=data_types, default="Dataset"), scripts.List( "IDs", optional=False, grouping="2", description="dataset ID.").ofType(rlong(0)), authors=["Christian Evenhuis"], institutions=["MIF UTS"], contact="*****@*****.**" ) try: # process the list of args above. script_params = {} for key in client.getInputKeys(): if client.getInput(key): script_params[key] = client.getInput(key, unwrap=True) # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) print("connection made") dataType = script_params["Data_Type"] print(dataType) ids = script_params["IDs"] datasets = list(conn.getObjects(dataType, ids)) # generator of images or datasets print(ids) print("datasets:") print( datasets ) for ds in datasets: # name of the file csv_name = "{}_metadata_out.csv".format(ds.getName()) print(csv_name) # remove the csv if it exists for ann in ds.listAnnotations(): if( isinstance(ann, omero.gateway.FileAnnotationWrapper) ): if( ann.getFileName() == csv_name ): # if the name matches delete it try: delete = Delete2(targetObjects={'FileAnnotation': [int(ann.getId())]}) handle = conn.c.sf.submit(delete) conn.c.waitOnCmd(handle, loops=10, ms=500, failonerror=True, failontimeout=False, closehandle=False) print("Deleted existing csv") except Exception, ex: print("Failed to delete existing csv: {}".format(ex.message)) else: print("No exisiting file") # filename key multiple vals # assemble the metadata into an OrderedDict of ( OrderedDict of Sets ) file_names = [ img.getName() for img in list(ds.listChildren()) ] kv_dict = OrderedDict() for img in ds.listChildren(): fn = img.getName() kv_dict[fn] = GetExistingMapAnnotions(img) # attach the data mess = attach_csv_file( conn, ds, kv_dict ) print(mess) mess="done" client.setOutput("Message", rstring(mess))
print_obj(dataset, 2) for image in dataset.listChildren(): print_obj(image, 4) # Retrieve the datasets owned by the user currently logged in # =========================================================== # Here we create an omero.sys.ParametersI instance which we # can use to filter the results that are returned. If we did # not pass the params argument to getObjects, then all Datasets # in the current group would be returned. print "\nList Datasets:" print "=" * 50 params = omero.sys.ParametersI() params.exp(conn.getUser().getId()) # only show current user's Datasets datasets = conn.getObjects("Dataset", params=params) for dataset in datasets: print_obj(dataset) # Retrieve the images contained in a dataset # ========================================== print "\nDataset:%s" % datasetId print "=" * 50 dataset = conn.getObject("Dataset", datasetId) print "\nImages in Dataset:", dataset.getName() for image in dataset.listChildren(): print_obj(image) # Retrieve an image by ID
# we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) print script_params # get the 'IDs' parameter of the csv file imported as annotation file_id = script_params["File_Annotation"] file_ann = conn.getObject("FileAnnotation", file_id) csv_text = "".join(list(file_ann.getFileInChunks())) print csv_text lines = csv_text.split("\n") print lines #check tags in all groups for g in conn.getObjects("ExperimenterGroup"): conn.SERVICE_OPTS.setOmeroGroup(g.id) if g.id < 3 or "public" in g.name: #this is important because i am not going to add tags to system, guest group and also not to any public group continue data = [] col_names = lines[0] name_index = 0 desc_index = 1 for l in lines[1:]: cols = l.split(",") print cols if len(cols) < 1:
class QGateWay(QObject): status = Signal(str) connected = Signal(BlitzGateway) disconnected = Signal() error = Signal(object) # singletons _conn: Optional[BlitzGateway] = None _host: Optional[str] = None _port: Optional[str] = None _user: Optional[str] = None def __init__(self, parent=None): super().__init__(parent) self.store = SessionsStore() self.destroyed.connect(self.close) atexit.register(self.close) self.worker: Optional[WorkerBase] = None self._next_worker: Optional[WorkerBase] = None @property def conn(self): return QGateWay._conn @conn.setter def conn(self, val): QGateWay._conn = val @property def host(self): return QGateWay._host @host.setter def host(self, val): QGateWay._host = val @property def port(self): return QGateWay._port @port.setter def port(self, val): QGateWay._port = val @property def user(self): return QGateWay._user @user.setter def user(self, val): QGateWay._user = val def isConnected(self): return self.conn and self.conn.isConnected() def close(self, hard=False): if self.isConnected(): self.conn.close(hard=hard) try: self.disconnected.emit() except RuntimeError: # if called during atexit the C/C++ object may be deleted pass def connect(self): if not self.conn: raise ValueError("No gateway to connect") if not self.conn.isConnected(): if not self.conn.c: self.conn._resetOmeroClient() if self.conn.connect(): self.connected.emit(self.conn) def get_current(self) -> Tuple[str, str, str, str]: return self.store.get_current() def _start_next_worker(self): if self._next_worker is not None: self.worker = self._next_worker self._next_worker = None self.worker.start() else: self.worker = None def _submit(self, func: Callable, *args, _wait=True, **kwargs) -> WorkerBase: new_worker = create_worker(func, *args, _start_thread=False, **kwargs) new_worker.finished.connect(self._start_next_worker) if self.worker and self.worker.is_running: self._next_worker = new_worker if not _wait: self.worker.quit() else: self.worker = new_worker self.worker.start() return new_worker def try_restore_session(self): return self._submit(self._try_restore_session) def _try_restore_session(self) -> Optional[SessionStats]: host, username, uuid, port = self.get_current() host = self.host or host username = self.user or username port = self.port or port if uuid and self.store.exists(host, username, uuid): try: self.status.emit("connecting...") session = self.store.attach(host, username, uuid) return self._on_new_session(session) except Exception as e: self.status.emit("Error") self.error.emit(e) return None def create_session(self, host: str, port: str, username: str, password: str): return self._submit(self._create_session, host, port, username, password) def _create_session(self, host: str, port: str, username: str, password: str): self.status.emit("connecting...") try: props = { "omero.host": host, "omero.user": username, } if port: props['omero.port'] = port session = self.store.create(username, password, props) return self._on_new_session(session) except Exception as e: self.status.emit("Error") self.error.emit(e) def _on_new_session(self, session: SessionStats): client = session[0] if not client: return self.conn = BlitzGateway(client_obj=client) self.host = client.getProperty("omero.host") self.port = client.getProperty("omero.port") self.user = client.getProperty("omero.user") self.connected.emit(self.conn) self.status.emit("") return self.conn def getObjects(self, name: str, **kwargs) -> Generator[BlitzObjectWrapper, None, None]: if not self.isConnected(): raise RuntimeError("No connection!") yield from self.conn.getObjects(name, **kwargs)
# List all Projects owned by the user currently logged in # ======================================================= # By default this returns Projects from all owners across # all groups. We can filter by group and owner using the # optional opts dict (new in 5.3.0) # We also order by name and use 'limit' and 'offset', # to load the first 5 Projects print("\nList Projects:") print("=" * 50) my_exp_id = conn.getUser().getId() default_group_id = conn.getEventContext().groupId for project in conn.getObjects("Project", opts={ 'owner': my_exp_id, 'group': default_group_id, 'order_by': 'lower(obj.name)', 'limit': 5, 'offset': 0 }): print_obj(project) assert project.getDetails().getOwner().id == my_exp_id # We can get Datasets with listChildren, since we have the Project already. # Or conn.getObjects("Dataset", opts={'project', id}) if we have Project ID for dataset in project.listChildren(): print_obj(dataset, 2) for image in dataset.listChildren(): print_obj(image, 4) # Find objects by ID. NB: getObjects() returns a generator, not a list datasets = conn.getObjects("Dataset", [datasetId, datasetId + 1])
for l in lines: kv = l.split(",", 1) print kv if len(kv) == 2: data.append(kv) elif len(kv) == 1: data.append([kv[0], ""]) # data = [l.split(",") for l in lines] # only link a client map annotation to a single object for dataset in conn.getObjects("Dataset", ids): map_ann = omero.gateway.MapAnnotationWrapper(conn) map_ann.setNs(namespace) map_ann.setValue(data) map_ann.save() dataset.linkAnnotation(map_ann) # Return some value(s). # Here, we return anything useful the script has produced. # NB: The Insight and web clients will display the "Message" output. msg = "Script ran OK" client.setOutput("Message", rstring(msg))
print lines data = [] col_names = lines[0] name_index = 0 desc_index = 1 for l in lines[1:]: cols = l.split(",") print cols if len(cols) < 1: continue text = cols[name_index] tags = list( conn.getObjects("TagAnnotation", attributes={"textValue": text})) if len(tags) > 0: print "Tag '%s' already exists" % text continue tag_ann = omero.gateway.TagAnnotationWrapper(conn) tag_ann.setValue(text) if len(cols) > 1: tag_ann.setDescription(cols[desc_index]) tag_ann.save() # Return some value(s). # Here, we return anything useful the script has produced. # NB: The Insight and web clients will display the "Message" output. msg = "Script ran OK"
# first parameter scripts.String( "Data_Type", optional=False, values=data_types, default="Image"), # second parameter scripts.List("IDs", optional=False).ofType(rlong(0)), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) print script_params # get the 'IDs' parameter of the Images ids = unwrap(client.getInput("IDs")) images = conn.getObjects("Image", ids) for i in images: image_name = i.name print "image name is", image_name tag_ann = omero.gateway.TagAnnotationWrapper(conn) tag_ann.setValue(image_name) tag_ann.save() i.linkAnnotation(tag_ann) #this is fundamental for actually copying the tag! # Return some value(s). # Here, we return anything useful the script has produced.
"select_users_move_to_public_group.py", ("Customised script for selecting users and adding them to the public domain group" ), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) print script_params # Use 'client' namespace to allow editing in Insight & web namespace = NSCLIENTMAPANNOTATION # list users with the 'IDs' parameter #exp_id = script_params["Experimenter"] experimenters = conn.getObjects("Experimenter") exp_ids = [] for e in experimenters: print e.id, e.firstName, e.lastName if e.id > 1: exp_ids.append(e.id) print "list ids", exp_ids # move users to a single object, in this case public domain public_group_id = 5 adminService = conn.getAdminService() for eid in exp_ids:
description="Name of the new OMERO.figure", default="Split View Figure"), authors=["Will Moore", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: # process the list of args above. params = client.getInputs(unwrap=True) print(params) # wrap client to use the Blitz Gateway conn = BlitzGateway(client_obj=client) # Call the main script - returns the new OMERO.figure ann ID images = list(conn.getObjects('Image', params["IDs"])) print(f'Found {len(images)} images') print(images) if len(images) == 0: message = "No images found" else: figure_id = create_omero_figure(conn, images, params) message = "Created figure: %s" % figure_id client.setOutput("Message", rstring(message)) except Exception: raise finally: client.closeSession()
def print_obj(obj, indent=0): """ Helper method to display info about OMERO objects. Not all objects will have a "name" or owner field. """ print >> sys.stderr, """%s%s:%s Name:"%s" (owner=%s)""" % (\ " " * indent, obj.OMERO_CLASS,\ obj.getId(),\ obj.getName(),\ obj.getOwnerOmeName()) conn.SERVICE_OPTS.setOmeroGroup('-1') # Retrieve a list of all projects owned by user: # ================================================================= print "\nList Projects:" print "=" * 50 for project in conn.getObjects("Project"): print_obj(project) for dataset in project.listChildren(): print_obj(dataset, 2) datasetId = dataset.getId() # Close connection: # ================================================================= # When you're done, close the session to free up server resources. conn._closeSession()
# this script only takes tags client = scripts.client( "merge_tags.py", ("Customised script for merging tags with the same names. Valid for images only" ), ) # we can now create our Blitz Gateway by wrapping the client object conn = BlitzGateway(client_obj=client) #search for tags in all the groups conn.SERVICE_OPTS.setOmeroGroup('-1') script_params = client.getInputs(unwrap=True) print script_params #in the listed tags check the duplicates and retain only the firts found and relink the images to this retained tag for g in conn.getObjects("ExperimenterGroup"): conn.SERVICE_OPTS.setOmeroGroup(g.id) print "----groups", g.id, g.name #create a list of tags and append only the tags, tag id and tag owner id to it tags = [] for tag in conn.getObjects("TagAnnotation"): owner = tag.getDetails().owner.id.val #print tag.textValue, owner tags.append([tag.textValue, str(tag.id), str(owner)]) #sort the tags in descending order to allow to see duplicates tags.sort(key=lambda tag: tag[0].lower()) print "sorted tags", tags
def run_script(): data_types = [ rstring('Image'), rstring('Dataset'), rstring('Project'), rstring('Screen'), rstring('Plate'), rstring('Well') ] client = scripts.client( 'Strip.py', """ Remove annotations from OMERO objects. Warning: This script really deletes the annotations, it does not just unlink them! """, scripts.String("Data_Type", optional=False, grouping="1", description="The target object type", values=data_types, default="Image"), scripts.List("IDs", optional=False, grouping="2", description="List of target objects \ ids").ofType(rlong(0)), scripts.Bool("Traverse", grouping="3", description="Also delete the annotations of all \ descendants", default=False), scripts.Bool("ROIs", grouping="4.1", description="Delete all ROIs", default=True), scripts.Bool("Tags", grouping="4.2", description="Delete all Tags", default=True), scripts.Bool("File Attachments", grouping="4.3", description="Delete all File Attachments", default=True), scripts.Bool("Key-Value Pairs", grouping="4.4", description="Delete all Key-Value Pairs", default=True), scripts.Bool("Comments", grouping="4.5", description="Delete all Comments", default=True), version="0.0.1", authors=["Dominik Lindner", "OME Team"], institutions=["University of Dundee"], contact="*****@*****.**", ) try: conn = BlitzGateway(client_obj=client) script_params = client.getInputs(unwrap=True) objects = conn.getObjects(script_params['Data_Type'], ids=script_params['IDs']) message = "Results:\n" for obj in objects: message += perform_action( conn, obj, del_r=script_params['ROIs'], del_f=script_params['File Attachments'], del_t=script_params['Tags'], del_m=script_params['Key-Value Pairs'], del_c=script_params['Comments'], traverse=script_params['Traverse']) + "\n" print message client.setOutput( "Message", rstring("Deleted %i ROIs and %i other annotations" % (rois_deleted, annos_deleted))) finally: client.closeSession()