def get_image_ids(conn, plate_id, field_id=0): """Get image IDs for images in Plate""" conn.SERVICE_OPTS.setOmeroGroup('-1') query_service = conn.getQueryService() params = ParametersI() params.addId(plate_id) params.add('wsidx', rint(field_id)) query = "select img.id "\ "from Well well "\ "join well.wellSamples ws "\ "join ws.image img "\ "where well.plate.id = :id "\ "and index(ws) = :wsidx" p = query_service.projection(query, params, conn.SERVICE_OPTS) img_ids = [i[0].val for i in p] return img_ids
def get_child_counts(conn, link_class, parent_ids): """ Count child links for the specified parent_ids. @param conn: BlitzGateway @param link_class: Type of link, e.g. 'ProjectDatasetLink' @param parent_ids: List of Parent IDs @return A dict of parent_id: child_count """ ctx = deepcopy(conn.SERVICE_OPTS) ctx.setOmeroGroup(-1) params = ParametersI() params.add('ids', wrap([rlong(id) for id in parent_ids])) query = ("select chl.parent.id, count(chl.id) from %s chl" " where chl.parent.id in (:ids) group by chl.parent.id" % link_class) result = conn.getQueryService().projection(query, params, ctx) counts = {} for d in result: counts[d[0].val] = unwrap(d[1]) return counts
def get_wellsample_indices(conn, plate_id=None, plateacquisition_id=None): """ Return min and max WellSample index for a Plate OR PlateAcquisition @param conn: BlitzGateway @param plate_id: Plate ID @param plateacquisition_id: PlateAcquisition ID @return A dict of parent_id: child_count """ ctx = deepcopy(conn.SERVICE_OPTS) ctx.setOmeroGroup(-1) params = ParametersI() query = "select minIndex(ws), maxIndex(ws) from Well well " \ "join well.wellSamples ws" if plate_id is not None: query += " where well.plate.id=:plate_id " params.add('plate_id', rlong(plate_id)) elif plateacquisition_id is not None: query += " where ws.plateAcquisition.id=:plateacquisition_id" params.add('plateacquisition_id', rlong(plateacquisition_id)) result = conn.getQueryService().projection(query, params, ctx) result = [r for r in unwrap(result)[0] if r is not None] return result
def attributes_by_attributes(conn, name="Gene Symbol", value="ASH2L", ns="openmicroscopy.org/mapr/gene", ns2="openmicroscopy.org/mapr/phenotype", name2=None, sId=None ): """ Return a list of neighbours attributes for given case insensitive attribute value. (Uses the python blitz gateway) """ from omero.rtypes import rstring, rlist, unwrap from omero.sys import ParametersI params = ParametersI() params.addString("value", value.lower()) q = ( "select distinct new map( mv.value as value) " "from Annotation as a " "join a.mapValue as mv " "where lower(mv.value) = :value {where_claus}" ) where_claus = [] if name: params.addString("name", name) where_claus.append("and mv.name = :name") q = q.format(**{'where_claus':" ".join(where_claus)}) values = [v[0]['value'] for v in unwrap( conn.getQueryService().projection(q, params))] params = ParametersI() valuelist = [rstring(unicode(v)) for v in values] params.add('values', rlist(valuelist)) params.addString("ns", ns) params.addString("ns2", ns2) q = ( "select distinct new map(" "mv.name as name, " "mv.value as value, " "mv2.name as name2, " "mv2.value as value2) " "from Image as i " "join i.annotationLinks as ial " "join i.annotationLinks as ial2 " "join ial.child as a " "join a.mapValue as mv " "join ial2.child as a2 " "join a2.mapValue as mv2 " "where a.ns = :ns and a2.ns = :ns2 " "and mv.value in (:values) {where_claus} " ) where_claus = [] if name: params.addString("name", name) where_claus.append("and mv.name = :name") if name2: params.addString("name2", name2) where_claus.append("and mv2.name = :name2") q = q.format(**{'where_claus':" ".join(where_claus)}) if sId != None: q = q + ("and i in (select image from WellSample " "where well.plate in " "(select child from ScreenPlateLink where parent.id = {sId}))") screenidList = [] screenidList.append(str(sId)) q = q.format(**{'sId':" ".join(screenidList)}) res = {} for r in unwrap(conn.getQueryService().projection(q, params)): r = r[0] try: res[(r['name'], r['value'])].append((r['name2'], r['value2'])) except KeyError: res[(r['name'], r['value'])] = [(r['name2'], r['value2'])] return res
def testQueryTaggedUnique(self): # get group we're working on... ctx = self.client.sf.getAdminService().getEventContext() groupId = ctx.groupId print 'groupId', groupId # Admin sets permissions to read-ann admin = self.root.sf.getAdminService() rootUpdate = self.root.sf.getUpdateService() gr = admin.getGroup(groupId) p = PermissionsI() p.setUserRead(True) p.setUserWrite(True) p.setGroupRead(True) p.setGroupAnnotate(True) p.setGroupWrite(False) p.setWorldRead(False) p.setWorldAnnotate(False) p.setWorldWrite(False) gr.details.permissions = p admin.updateGroup(gr) # Update context for user ctx = self.client.sf.getAdminService().getEventContext() update = self.client.sf.getUpdateService() queryService = self.client.sf.getQueryService() tagCount = 5 # User creates tag linked to images tag = TagAnnotationI() tag.textValue = wrap("test_iQuerySpeed") links = [] for i in range(tagCount): iid = createImageWithPixels(self.client, self.uuid()) link = ImageAnnotationLinkI() link.parent = ImageI(iid, False) link.child = tag links.append(link) links = update.saveAndReturnArray(links) tag = links[0].child # check permissions p = tag.getDetails().getPermissions() assert p.isGroupRead() assert p.isGroupAnnotate() # Root also links user's tag to images rootLinks = [] for l in links: link = ImageAnnotationLinkI() link.parent = ImageI(l.parent.id, False) link.child = TagAnnotationI(l.child.id, False) rootLinks.append(link) rootUpdate.saveAndReturnArray(rootLinks, {'omero.group': str(groupId)}) q = """select distinct new map(obj.id as id, obj.name as name, obj.details.owner.id as ownerId, obj as image_details_permissions, obj.fileset.id as filesetId, lower(obj.name) as n , pix.sizeX as sizeX, pix.sizeY as sizeY, pix.sizeZ as sizeZ ) from Image obj left outer join obj.pixels pix join obj.annotationLinks alink where %s order by lower(obj.name), obj.id """ params = ParametersI() params.add('tid', tag.id) # We can get all the tagged images like this. # We use an additional select statement to give 'unique' results uniqueClause = """alink.id = (select max(alink.id) from ImageAnnotationLink alink where alink.child.id=:tid and alink.parent.id=obj.id)""" query = q % uniqueClause result1 = queryService.projection(query, params, {'omero.group': str(groupId)}) assert len(result1) == tagCount # Without the select statement, we get the same image returned # multiple times if there is no 'distinct' clause = "alink.child.id=:tid" query = q % clause result2 = queryService.projection(query, params, {'omero.group': str(groupId)}) assert len(result2) == tagCount for idx in range(len(result1)-1): # Omit final since == isn't defined for Ice objects. assert result1[idx] == result2[idx]
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] assert plate_ids # Loop through each field of those plates. query = """ SELECT id, image.name, image.fileset.id, well.row, well.column, well.plate.name FROM WellSample WHERE well.plate.id IN (:ids) """