Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created July 2, 2012 20:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-moore/3035653 to your computer and use it in GitHub Desktop.
Save will-moore/3035653 to your computer and use it in GitHub Desktop.
Search for OMERO Images with key-value pairs in Original Metadata
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Sript to look for images with key-value pairs in Original Metadata
# See https://www.openmicroscopy.org/community/viewtopic.php?f=4&t=1226
import omero
import omero.scripts as scripts
from omero.gateway import BlitzGateway
from omero.rtypes import *
def check_image(conn, img, params) :
"""
Check image metadata for matches
@param conn: The BlitzGateway connection
@param img: The ImageWrapper object
@param params: The script parameters
"""
key = params["Key"] # Key the user is looking for
value = params["Value"] # Value (may end with * wildcard)
om = img.loadOriginalMetadata()
if om is not None:
#global_metadata : om[1]
#series_metadata : om[2]
for keyValue in (om[1] + om[2]):
if len(keyValue) > 1:
if keyValue[0] == key:
if value[-1] == "*":
if keyValue[1].startswith(value):
return True
else:
if keyValue[1] == value:
return True
def search_images(conn, params):
"""
Parse each image for metadata
@param conn: The BlitzGateway connection
@param params: The script parameters
"""
images = []
all_imgs = conn.getObjects("Image")
for img in all_imgs:
print "Checking Image: %s ID: %s" % (img.getName(), img.getId())
if check_image(conn, img, params):
images.append(img)
return images
def runAsScript():
"""
The main entry point of the script, as called by the client via the
scripting service, passing the required parameters.
"""
client = scripts.client('Search_Orig_Metadata.py', """\
Look for images that have key-value matches in their Original Metadata
""",
scripts.String("Key", optional=False, grouping="1.1",
description="The name of the Metadata value."),
scripts.String("Value", optional=False, grouping="1.2",
description="Value you're looking for. End with * for wild-card"),
)
conn = BlitzGateway(client_obj=client)
# Process the list of args above.
params = {}
for key in client.getInputKeys():
if client.getInput(key):
params[key] = client.getInput(key, unwrap=True)
print params
# Call the main script - returns the number of images and total bytes
results = search_images(conn, params)
msg = "Found %s images" % len(results)
print msg
client.setOutput("Message", rstring(msg))
if len(results) > 0:
client.setOutput("Image", robject(results[0]._obj))
if __name__ == "__main__":
"""
Python entry point
"""
runAsScript()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment