Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created April 1, 2021 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-moore/12d31c026bfe5adaea4b2341494069a0 to your computer and use it in GitHub Desktop.
Save will-moore/12d31c026bfe5adaea4b2341494069a0 to your computer and use it in GitHub Desktop.
Examples for how to query OMERO for Images based on pixel type of Map Annotations
# https://forum.image.sc/t/harmonization-of-image-metadata-for-different-file-formats-omero-mde/50827/8
import omero
from omero.gateway import BlitzGateway
conn = BlitzGateway('username', 'password', port=4064, host='omero.server.org')
conn.connect()
# current group
group_id = conn.getEventContext().groupId
print('group_id', group_id)
# Use -1 for cross-group query
conn.SERVICE_OPTS.setOmeroGroup(group_id)
# Find Images by Pixel Type
# https://docs.openmicroscopy.org/omero-blitz/5.5.8/slice2html/omero/model/Pixels.html#pixelsType
params = omero.sys.ParametersI()
params.addString('value', 'uint8')
# pagination
offset = 0
limit = 100
params.page(offset, limit)
query = """
select img from Image img
join fetch img.pixels as pixels
where pixels.pixelsType.value = :value
"""
images = conn.getQueryService().findAllByQuery(query, params, conn.SERVICE_OPTS)
for img in images:
print(img.id.val, img.name.val)
# Find Images by Key-Value pairs (map annotations)
params = omero.sys.ParametersI()
params.addString('key', 'Primary Antibody')
params.addString('value', 'ACA')
offset = 0
limit = 100
params.page(offset, limit)
# Here we use 'projection' query to load specific values rather than whole objects
# This can be more performant, but here I'm using it based on Map-Annotation
# query examples in omero-mapr
query = """
select image.id, image.name from
ImageAnnotationLink ial
join ial.child a
join a.mapValue mv
join ial.parent image
where mv.name = :key and mv.value = :value"""
result = conn.getQueryService().projection(query, params, conn.SERVICE_OPTS)
for row in result:
print("Img ID: %s Name: %s" % (row[0].val, row[1].val))
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment