Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created June 10, 2020 09:44
Show Gist options
  • Save will-moore/bee8a8ad37a75ccb8b1461b66cfb886c to your computer and use it in GitHub Desktop.
Save will-moore/bee8a8ad37a75ccb8b1461b66cfb886c to your computer and use it in GitHub Desktop.
OMERO query for Images via Comments (or Tags) on their ROIs
# See https://forum.image.sc/t/searching-by-roi-comment-field-in-omero-web/38808
# Usage:
# $ python roi_comments_search.py my_search_term
import argparse
import sys
import omero
import omero.clients
from omero.sys import ParametersI
from omero.rtypes import rstring
from omero.cli import cli_login
from omero.gateway import BlitzGateway
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('query', help='Search Text')
args = parser.parse_args(argv)
query_text = args.query
with cli_login() as cli:
conn = BlitzGateway(client_obj=cli._client)
qs = conn.getQueryService()
query = """select annLink from RoiAnnotationLink as annLink
join fetch annLink.parent as roi
join fetch roi.image
join fetch annLink.child as ann where ann.textValue like :text"""
params = ParametersI()
with_wildcards = f'%{ query_text }%'
params.addString('text', rstring(with_wildcards))
results = qs.findAllByQuery(query, params, conn.SERVICE_OPTS)
print('FOUND', len(results))
for r in results:
image = r.parent.image
print(r.parent.id.val, image.id.val, image.name.val)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment