Skip to content

Instantly share code, notes, and snippets.

@dpwrussell
Last active May 17, 2016 17:09
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 dpwrussell/1d6edc06e8c8310f7906d413d20dcf52 to your computer and use it in GitHub Desktop.
Save dpwrussell/1d6edc06e8c8310f7906d413d20dcf52 to your computer and use it in GitHub Desktop.
Example of querying tags from images
import omero
from omero.gateway import BlitzGateway
from omero.sys import ParametersI
import sys
USERNAME = ''
PASSWORD = ''
HOST = 'localhost'
PORT = 4064
def connect():
""" Create an OMERO Connection """
# Initialize the connection
conn = BlitzGateway(USERNAME,
PASSWORD,
host=HOST,
port=PORT)
# Connect
connected = conn.connect()
# 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 conn
def queryImageTags(image_id):
""" Query for tag annotations attached to an image. This will also
return details (owner, group, etc) about the annotation.
"""
conn = connect()
qs = conn.getQueryService()
conn.SERVICE_OPTS.setOmeroGroup(-1)
params = ParametersI()
params.add('iid', omero.rtypes.wrap(image_id))
q = """
SELECT anno
FROM Image image
JOIN image.annotationLinks links
JOIN links.child anno
WHERE image.id = :iid
AND anno.class = TagAnnotation
"""
for row in qs.projection(q, params, conn.SERVICE_OPTS):
print 'Tag: %s (%s)' % (row[0].val.getTextValue().val,
row[0].val.getDescription().val)
def queryImagesTags():
""" Query for all tag annotations attached to images. This will also
return details (owner, group, etc) about the annotation.
"""
conn = connect()
qs = conn.getQueryService()
conn.SERVICE_OPTS.setOmeroGroup(-1)
params = ParametersI()
q = """
SELECT image, anno
FROM Image image
JOIN image.annotationLinks links
JOIN links.child anno
WHERE anno.class = TagAnnotation
"""
for row in qs.projection(q, params, conn.SERVICE_OPTS):
image = row[0].val
anno = row[1].val
print 'Image: %s Tag: %s (%s)' % (image.getName().val,
anno.getTextValue().val,
anno.getDescription().val)
def queryImageTagsTerse(image_id):
""" Query for tag annotation details only, attached to an image.
"""
conn = connect()
qs = conn.getQueryService()
conn.SERVICE_OPTS.setOmeroGroup(-1)
params = ParametersI()
params.add('iid', omero.rtypes.wrap(image_id))
q = """
SELECT anno.textValue, anno.description
FROM Image image
JOIN image.annotationLinks links
JOIN links.child anno
WHERE image.id = :iid
AND anno.class = TagAnnotation
"""
for row in qs.projection(q, params, conn.SERVICE_OPTS):
print 'Tag: %s (%s)' % (row[0].val,
row[1].val)
if __name__ == "__main__":
# Must be a long
queryImageTags(103L)
queryImagesTags()
# Must be a long
queryImageTagsTerse(103L)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment