def get_image_ids(self): """Get the Ids of imported images. Note that this will not find images if they have not been imported. Also, while image_ids are returned, this method also sets ``self.image_ids``. Returns ------- image_ids : list of ints Ids of images imported from the specified client path, which itself is derived from ``self.file_path`` and ``self.filename``. """ if self.imported is not True: logging.error(f'File {self.file_path} has not been imported') return None else: q = self.conn.getQueryService() params = Parameters() path_query = str(self.file_path).strip('/') params.map = {"cpath": rstring(path_query)} results = q.projection( "SELECT i.id FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " WHERE u.clientPath=:cpath", params, self.conn.SERVICE_OPTS) self.image_ids = [r[0].val for r in results] return self.image_ids
def image_has_imported_filename(conn, im_id, imported_filename): """Ask whether an image is associated with a particular image file. THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED Sometimes we know the filename of an image that has been imported into OMERO but not necessarily the image ID. This is frequently the case when we want to annotate a recently imported image. This funciton will help to filter a list of image IDs to only those associated with a particular filename in ImportedImageFiles. Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. im_id : int ID of OMERO image. imported_filename : str The full filename (with extension) of the file whose OMERO image we are looking for. NOT the path of the image. Returns ------- answer : boolean Answer to the question of whether the given image has an associated ImportedImageFile of the given name. Notes ----- This function should be used as a filter on an image list that has been already narrowed down as much as possible. Note that many different images in OMERO may share the same filename (e.g., image.tif). Examples -------- >>> im_ids = get_image_ids(conn, dataset=303) >>> im_ids = [im_id for im_id in im_ids ... if image_has_imported_filename(conn, im_id, "feb_2020.tif")] """ q = conn.getQueryService() params = Parameters() params.map = {"imid": rlong(im_id)} results = q.projection( "SELECT o.name FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " JOIN u.originalFile o" " WHERE i.id=:imid", params, conn.SERVICE_OPTS) imp_filenames = [r[0].val for r in results] if imported_filename in imp_filenames: return True else: return False
def getAnnotationsForObjects(obj_type, oids): # Get the images that match hql = "select distinct link.child.id from %sAnnotationLink link " \ "where link.parent.id in (:oids)" % obj_type params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in oids]) qs = conn.getQueryService() return [result[0].val for result in qs.projection(hql,params, service_opts)]
def getObjectsWithAllAnnotations(obj_type, annids): # Get the images that match hql = "select link.parent.id from %sAnnotationLink link " \ "where link.child.id in (:oids) " \ "group by link.parent.id " \ "having count (distinct link.child) = %s" % (obj_type, len(annids)) params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in set(annids)]) qs = conn.getQueryService() return [x[0].getValue() for x in qs.projection(hql,params,service_opts)]
def user_creation_time(conn, user_id): """Return creation date of Experimenter.""" q = conn.getQueryService() params = Parameters() params.map = {"userid": rlong(user_id)} results = q.projection( "SELECT e.event.time" " FROM EventLog e" " WHERE e.action='INSERT' and" " e.entityType='ome.model.meta.Experimenter' and" " e.entityId=:userid", params, conn.SERVICE_OPTS) time = datetime.fromtimestamp(results[0][0].val / 1000) return time
def getAnnotationsForObjects(obj_type, oids): # Get the images that match hql = "select distinct link.child.id from %sAnnotationLink link " \ "where link.parent.id in (:oids)" % obj_type params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in oids]) qs = conn.getQueryService() return [ result[0].val for result in qs.projection(hql, params, service_opts) ]
def filter_by_filename(conn, im_ids, imported_filename): """Filter list of image ids by originalFile name Sometimes we know the filename of an image that has been imported into OMERO but not necessarily the image ID. This is frequently the case when we want to annotate a recently imported image. This funciton will help to filter a list of image IDs to only those associated with a particular filename. Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. im_ids : list of int List of OMERO image IDs. imported_filename : str The full filename (with extension) of the file whose OMERO image we are looking for. NOT the path of the image. Returns ------- filtered_im_ids : list of int Filtered list of images with originalFile name matching ``imported_filename``. Notes ----- This function should be used as a filter on an image list that has been already narrowed down as much as possible. Note that many different images in OMERO may share the same filename (e.g., image.tif). Examples -------- >>> im_ids = get_image_ids(conn, dataset=303) >>> im_ids = filter_by_filename(conn, im_ids, "feb_2020.tif")] """ q = conn.getQueryService() params = Parameters() params.map = {"oname": rstring(imported_filename)} results = q.projection( "SELECT i.id FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " JOIN u.originalFile o" " WHERE o.name=:oname", params, conn.SERVICE_OPTS) im_id_matches = [r[0].val for r in results] return list(set(im_ids) & set(im_id_matches))
def getObjectsWithAllAnnotations(obj_type, annids): # Get the images that match hql = "select link.parent.id from %sAnnotationLink link " \ "where link.child.id in (:oids) " \ "group by link.parent.id " \ "having count (distinct link.child) = %s" % (obj_type, len(annids)) params = Parameters() params.map = {} params.map["oids"] = rlist([rlong(o) for o in set(annids)]) qs = conn.getQueryService() return [ x[0].getValue() for x in qs.projection(hql, params, service_opts) ]
def all_originalfile_sizes(conn): """Return the total size of all OriginalFiles.""" q = conn.getQueryService() params = Parameters() results = q.projection("SELECT sum(f.size)" " FROM OriginalFile f", params, conn.SERVICE_OPTS) return results[0].val
def originalfile_size_date(conn, originalfile_id): """Return size of OriginalFile in bytes, with date stamp. Note: OriginalFile doesn't necessarily have an associated size. Needs to be part of an image fileset """ q = conn.getQueryService() params = Parameters() params.map = {"fid": rlong(originalfile_id)} results = q.projection( "SELECT f.size, ce.time" " FROM OriginalFile f" " JOIN f.details.creationEvent ce" " WHERE f.id=:fid", params, conn.SERVICE_OPTS) return [(x[0].val, datetime.fromtimestamp(x[1].val / 1000)) for x in results]
def all_originalfiles(conn): """Return a list of all OriginalFile ids associated with Images.""" q = conn.getQueryService() params = Parameters() results = q.projection( "SELECT f.id" " FROM Image i" " JOIN i.fileset fs" " JOIN fs.usedFiles fe" " JOIN fe.originalFile f", params, conn.SERVICE_OPTS) return [x[0].val for x in results]
def get_plate_ids(self): """Get the Ids of imported plates. Note that this will not find plates if they have not been imported. Also, while plate_ids are returned, this method also sets ``self.plate_ids``. Returns ------- plate_ids : list of ints Ids of plates imported from the specified client path, which itself is derived from ``self.file_path`` and ``self.filename``. """ if self.imported is not True: logging.error(f'File {self.file_path} has not been imported') return None else: print("time to get some IDs") q = self.conn.getQueryService() print(q) params = Parameters() path_query = str(self.file_path).strip('/') print(f"path query: f{path_query}") params.map = {"cpath": rstring(path_query)} print(params) results = q.projection( "SELECT DISTINCT p.id FROM Plate p" " JOIN p.plateAcquisitions pa" " JOIN pa.wellSample ws" " JOIN ws.image i" " JOIN i.fileset fs" " JOIN fs.usedFiles u" " WHERE u.clientPath=:cpath", params, self.conn.SERVICE_OPTS) print(results) self.plate_ids = [r[0].val for r in results] return self.plate_ids
def sessions_per_day(conn, date): """Return sessions and users per day. Return a list of tuples with users who started sessions on a certain date and the number of sessions per user. Note: date should be a string formatted YYYY-MM-DD """ q = conn.getQueryService() params = Parameters() query = "SELECT s.owner.omeName, count(s)" + \ " FROM Session s" + \ " WHERE s.started > cast('" + date + " 00:00', timestamp)" + \ " AND s.started < cast('" + date + " 23:59', timestamp)" + \ " GROUP by s.owner.omeName" results = q.projection(query, params, conn.SERVICE_OPTS) return [(x[0].val, x[1].val) for x in results]
def index(request, conn=None, **kwargs): request.session.modified = True # TODO Hardcode menu as search until I figure out what to do with menu menu = 'search' template = "tagsearch/tagnav.html" # tree support init = {'initially_open': None, 'initially_select': []} first_sel = None initially_open_owner = None # E.g. backwards compatible support for # path=project=51|dataset=502|image=607 (select the image) path = request.REQUEST.get('path', '') i = path.split("|")[-1] if i.split("=")[0] in ('project', 'dataset', 'image', 'screen', 'plate', 'tag'): init['initially_select'].append(str(i).replace("=", '-')) # Now we support show=image-607|image-123 (multi-objects selected) show = request.REQUEST.get('show', '') for i in show.split("|"): if i.split("-")[0] in ('project', 'dataset', 'image', 'screen', 'plate', 'tag', 'acquisition', 'run', 'well'): # alternatives for 'acquisition' i = i.replace('run', 'acquisition') init['initially_select'].append(str(i)) if len(init['initially_select']) > 0: # tree hierarchy open to first selected object init['initially_open'] = [init['initially_select'][0]] first_obj, first_id = init['initially_open'][0].split("-", 1) # if we're showing a tag, make sure we're on the tags page... if first_obj == "tag" and menu != "usertags": return HttpResponseRedirect( reverse(viewname="load_template", args=['usertags']) + "?show=" + init['initially_select'][0]) try: # set context to 'cross-group' conn.SERVICE_OPTS.setOmeroGroup('-1') if first_obj == "tag": first_sel = conn.getObject("TagAnnotation", long(first_id)) else: first_sel = conn.getObject(first_obj, long(first_id)) initially_open_owner = first_sel.details.owner.id.val # Wells aren't in the tree, so we need parent... if first_obj == "well": parentNode = \ first_sel.getWellSample().getPlateAcquisition() ptype = "acquisition" # No Acquisition for this well, use Plate instead if parentNode is None: parentNode = first_sel.getParent() ptype = "plate" first_sel = parentNode init['initially_open'] = [ "%s-%s" % (ptype, parentNode.getId()) ] init['initially_select'] = init['initially_open'][:] except: # invalid id pass if first_obj not in ("project", "screen"): # need to see if first item has parents if first_sel is not None: for p in first_sel.getAncestry(): # parents of tags must be tags (no OMERO_CLASS) if first_obj == "tag": init['initially_open'].insert(0, "tag-%s" % p.getId()) else: init['initially_open'].insert( 0, "%s-%s" % (p.OMERO_CLASS.lower(), p.getId())) initially_open_owner = p.details.owner.id.val if init['initially_open'][0].split("-")[0] == 'image': init['initially_open'].insert(0, "orphaned-0") # need to be sure that tree will be correct omero.group if first_sel is not None: switch_active_group(request, first_sel.details.group.id.val) # search support global_search_form = GlobalSearchForm(data=request.REQUEST.copy()) if menu == "search": if global_search_form.is_valid(): init['query'] = global_search_form.cleaned_data['search_query'] # get url without request string - used to refresh page after switch # user/group etc url = reverse(viewname="tagsearch") # validate experimenter is in the active group active_group = request.session.get('active_group') or \ conn.getEventContext().groupId # prepare members of group... s = conn.groupSummary(active_group) leaders = s["leaders"] members = s["colleagues"] userIds = [u.id for u in leaders] userIds.extend([u.id for u in members]) users = [] if len(leaders) > 0: users.append(("Owners", leaders)) if len(members) > 0: users.append(("Members", members)) users = tuple(users) # check any change in experimenter... user_id = request.REQUEST.get('experimenter') if initially_open_owner is not None: # if we're not already showing 'All Members'... if (request.session.get('user_id', None) != -1): user_id = initially_open_owner try: user_id = long(user_id) except: user_id = None # Check is user_id is in a current group if (user_id not in (set(map(lambda x: x.id, leaders)) | set(map(lambda x: x.id, members))) and user_id != -1): # All users in group is allowed user_id = None if user_id is None: # ... or check that current user is valid in active group user_id = request.session.get('user_id', None) if user_id is None or int(user_id) not in userIds: if user_id != -1: # All users in group is allowed user_id = conn.getEventContext().userId request.session['user_id'] = user_id if conn.isAdmin(): # Admin can see all groups myGroups = [ g for g in conn.getObjects("ExperimenterGroup") if g.getName() not in ("user", "guest") ] else: myGroups = list(conn.getGroupsMemberOf()) myGroups.sort(key=lambda x: x.getName().lower()) new_container_form = ContainerForm() # Create and set the form params = Parameters() qs = conn.getQueryService() service_opts = conn.SERVICE_OPTS.copy() service_opts.setOmeroGroup(active_group) def get_tags(obj): # Get tags # It is not sufficient to simply get the objects as there may be tags # which are not applied which don't really make sense to display # tags = list(self.conn.getObjects("TagAnnotation")) hql = """ SELECT DISTINCT link.child.id, link.child.textValue FROM %sAnnotationLink link WHERE link.child.class IS TagAnnotation ORDER BY link.child.textValue """ % obj return [(result[0].val, result[1].val) for result in qs.projection(hql, params, service_opts)] # List of tuples (id, value) tags = set(get_tags('Image')) tags.update(get_tags('Dataset')) tags.update(get_tags('Project')) # Convert back to an ordered list and sort tags = list(tags) tags.sort(key=lambda x: x[1].lower()) form = TagSearchForm(tags, conn) context = { 'init': init, 'myGroups': myGroups, 'new_container_form': new_container_form, 'global_search_form': global_search_form } context['groups'] = myGroups context['active_group'] = conn.getObject("ExperimenterGroup", long(active_group)) for g in context['groups']: g.groupSummary() # load leaders / members context['active_user'] = conn.getObject("Experimenter", long(user_id)) context['isLeader'] = conn.isLeader() context['current_url'] = url context['template'] = template context['tagnav_form'] = form return context
def get_image_ids(conn, dataset=None, well=None): """return a list of image ids based on project and dataset Parameters ---------- conn : ``omero.gateway.BlitzGateway`` object OMERO connection. dataset : int, optional ID of Dataset from which to return image IDs. well : int, optional ID of Well from which to return image IDs. Returns ------- im_ids : list of ints List of image IDs contained in the given Dataset, Well, or orphans. Notes ----- User and group information comes from the `conn` object. Be sure to use ``ezomero.set_group`` to specify group prior to passing the `conn` object to this function. If no Dataset or Well is specified, orphaned images are returned. Examples -------- Return orphaned images: >>> orphans = get_image_ids(conn) Return IDs of all images from Dataset with ID 448: >>> ds_ims = get_image_ids(conn, dataset=448) """ if (dataset is not None) & (well is not None): raise Exception('Dataset and Well can not both be specified') q = conn.getQueryService() params = Parameters() if dataset is not None: if not isinstance(dataset, int): raise TypeError('dataset must be integer') params.map = {"dataset": rlong(dataset)} results = q.projection( "SELECT i.id FROM Dataset d" " JOIN d.imageLinks dil" " JOIN dil.child i" " WHERE d.id=:dataset", params, conn.SERVICE_OPTS) elif well is not None: if not isinstance(well, int): raise TypeError('well must be integer') params.map = {"well": rlong(well)} results = q.projection( "SELECT i.id FROM Well w" " JOIN w.wellSamples ws" " JOIN ws.image i" " WHERE w.id=:well", params, conn.SERVICE_OPTS) elif (well is None) & (dataset is None): results = q.projection( "SELECT i.id FROM Image i" " WHERE NOT EXISTS (" " SELECT dil FROM DatasetImageLink dil" " WHERE dil.child=i.id" " )" " AND NOT EXISTS (" " SELECT ws from WellSample ws" " WHERE ws.image=i.id" " )", params, conn.SERVICE_OPTS) else: results = [] return [r[0].val for r in results]