How to tag and retrieve tags from data

How to tag and retrieve tags from data#

Another key concept of organizing data on OMERO (besides key-value pairs) is given by tags. Can be used to group data in a more flexible way than the hierarchy of projects, datasets and images. For a more in-depth explanation of some of the underlying concepts, see these excellent training materials regarding OMERO in general.

import ezomero
host = 'omero-int.biotec.tu-dresden.de'
user = ''  # replace this with your username
secure = True
port = 4064
group = 'default'

conn = ezomero.connect(host=host, user=user, secure=secure, port=port, group=group)

Tagging data#

The easiest and most straightforward way to tag your data is arguably to simply do this through the web interface of your OMERO servers. Omero extensions such as the excellent autotag extension make setting meaningful tags on your data a breeze. The question then becomes - how do you retrieve tags that have already been added to your data?

Reading tags from OMERO#

Let’s use ezomero again to retrieve tags that have been added to an image. This will return a list of tags (indicated by their respective ids) that have been added to the image. This is an important concept to grasp: Just like an image, a tag is an object in the OMERO database, and it has a unique identifier (id). This id is what is returned when you retrieve tags from an image.

tag_ids = ezomero.get_tag_ids(conn, object_type='Image', object_id=330, ns=None)
print(tag_ids)
[1088]

If we want to see the “value” of the tag, we can use the get_tag function to introspect the ids we just retrieved:

ezomero.get_tag(conn, tag_id=tag_ids[0])
'Fluorescence'

Adding a tag to data#

Adding a tag to an image is a little less straightforward. For once, it makes more sense to create and organize the tags through the web interface. The following tutorial introduces a way to set tags and tag groups from a yaml file, but this is a bit more complicated. For simplicity, let’s assume we already created some tags and we know their ids from the web interface:

new tag list new tag

To add this newly created tag to our image, we need to first retrieve both the tag and the image objects from the remote server:

tag_object = conn.getObject('Annotation', 8354)
image_object = conn.getObject('Image', 330)

In the next step, we link the tag to the image:

image_object.linkAnnotation(tag_object)

Let’s check whether this worked:

tag_ids = ezomero.get_tag_ids(conn, object_type='Image', object_id=330, ns=None)

for tag_id in tag_ids:
    print(ezomero.get_tag(conn, tag_id=tag_id), ' ', f'(Tag id: {tag_id})')
Fluorescence   (Tag id: 1088)
Raw data   (Tag id: 8354)