Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get objects by map annotations list #392

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4244,7 +4244,64 @@ def add_param(param, value):
if len(ids) == 0:
return []
return self.getObjects(obj_type, ids)

def getObjectsByMapAnnotationsList(self, obj_type=None, key_vals=[], ns=None, opts={}):
"""
Retrieve objects linked to Map Annotations, filter by list of key and value dicts.

:param obj_type: 'Dataset', 'Image' etc.
:param key_vals: Filters by a list of dicts that contain 'key' and 'value'
:return: Generator yielding Objects
:rtype: :class:`BlitzObjectWrapper` generator
"""
wrapper = KNOWN_WRAPPERS.get(obj_type.lower(), None)
if not wrapper:
raise AttributeError("Don't know how to handle '%s'" % obj_type)

params = omero.sys.ParametersI()
# Total length of key/val pairs to match
# stores in a set to avoid duplicates
key_vals_count = len(key_vals)
keys = set()
vals = set()
for kv in key_vals:
keys.add(kv['key'])
vals.add(kv['value'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are adding both the keys and vals to different sets here, you lose the key-value pairing?


# Formatting for query list string
keys_str = str(keys).replace('{', '(').replace('}', ')')
vals_str = str(vals).replace('{', '(').replace('}', ')')

# Opts to Params
# Parse opts dict to build params
limit = opts.get('limit', 500)
offset = opts.get('offset', 0)
if offset is not None and limit is not None:
params.page(offset, limit)

# Query
query = """
select distinct obj.id from %sAnnotationLink oal
join oal.parent obj
join oal.child ann
join ann.mapValue mv
where mv.name in %s
and mv.value in %s
""" % (wrapper().OMERO_CLASS, keys_str, vals_str)
# filter by namespace
if ns is not None:
query += "and ann.ns = '" + ns + "'"
query += """
group by obj.id
having count(*) >= %s
""" % key_vals_count

# Return objects
result = self.getQueryService().projection(query, params, self.SERVICE_OPTS)
ids = [row[0].val for row in result]
if len(ids) == 0:
return []
return self.getObjects(obj_type, ids)

################
# Enumerations #
Expand Down
Loading