We're Hiring!

Retrieving Images associated with an annotation

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

Retrieving Images associated with an annotation

Postby bweinstein » Wed Jul 17, 2013 10:44 pm

Hello all,

I have a couple of questions for you. I am using Python to write scripts to utilize my Omero server.

1. Is it possible to retrieve all LongAnnotations with a particular namespace (i.e. 'Passage_Num') that have a certain value? For example, lets say that I am looking for all 'Passage_Num' annotations with a value of 2.

2. Is it then possible to find and retrieve the images that these annotations link to?

Any help you could provide would be much appreciated. I have been stuck on this all day.

Thanks so much,

Bryan
bweinstein
 
Posts: 3
Joined: Wed Jul 17, 2013 10:34 pm

Re: Retrieving Images associated with an annotation

Postby atarkowska » Thu Jul 18, 2013 9:08 am

Hi Brian

Please find example python script below. I hope it will be helpful.

Code: Select all
import os
import omero
from omero.rtypes import rstring, rlong
from omero.gateway import BlitzGateway

# testing setup
USERNAME=
PASSWORD=
HOST=
PORT=
IMAGE_ID=

# Create a connection
# =================================================================
conn = BlitzGateway('USERNAME', 'PASSWORD', host='HOST', port=PORT)
conn.connect()

# Get Image
img = conn.getObject("Image", IMAGE_ID)


# Create Long Annotation
# =================================================================
namespace = "Passage_Num"
longval = 10

ann = omero.gateway.LongAnnotationWrapper()
ann.setLongValue(rlong(longval))
ann.setNs(namespace)

# Link Annotation to the given image
img.linkAnnotation(ann)


# Retrival
# =================================================================

# Retrive all annotations linked to the object (Image)
for ann in img.listAnnotations():
    if isinstance(ann, omero.gateway.LongAnnotationWrapper):
        print ann.getNs(), ann.getLongValue()


# Retrive all annotations by filter
nsToInclude = [namespace]
nsToExclude = []
metadataService = conn.getMetadataService()
annotations = metadataService.loadSpecifiedAnnotations('omero.model.LongAnnotation', nsToInclude, nsToExclude, None)
for ann in annotations:
    print ann.getId(), ann.getLongValue(), ann.getNs()
   
    # Find out parent objects of the annotation (one object at a time)
    for img in conn.getObjectsByAnnotations('Image',[ann.getId()]):
        print img.getId(), img.getName()

conn._closeSession()


Ola
atarkowska
 
Posts: 327
Joined: Mon May 18, 2009 12:44 pm

Re: Retrieving Images associated with an annotation

Postby bweinstein » Fri Jul 19, 2013 3:23 pm

Thanks so much! I was missing the "getObjectsByAnnotations" command. I am now able to accomplish what I want.

I still have one question, however; is it possible to get all annotations with a particular value? I understand how to get all long annotations with a given namespace (i.e. "Passage_Num" in the example we are discussing). But can I directly query and retrieve all annotations with a value of, let's say, 3, without looping over all of the annotations and retrieving their values?

And again, thanks so much, you saved me a lot of time!
bweinstein
 
Posts: 3
Joined: Wed Jul 17, 2013 10:34 pm

Re: Retrieving Images associated with an annotation

Postby wmoore » Fri Jul 19, 2013 11:01 pm

The conn.getObjects() method can be passed a dictionary of attributes:

# if you want to restrict the type of annotation, E.g. Tags only:
Code: Select all
>>> tags = conn.getObjects("TagAnnotation", attributes={'textValue':'Anaphase'})
>>> for tag in tags:
...     print tag
...
<TagAnnotationWrapper id=229>


If you want all annotations, use "Annotation"
Code: Select all
>>> anns = conn.getObjects("Annotation", attributes={'longValue':long(5)})
>>> for a in anns:
...     print a
...
<LongAnnotationWrapper id=242>
<LongAnnotationWrapper id=244>
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: Retrieving Images associated with an annotation

Postby bweinstein » Thu Jul 25, 2013 2:33 pm

Aha! This is what I needed. Thanks so much!
bweinstein
 
Posts: 3
Joined: Wed Jul 17, 2013 10:34 pm


Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 0 guests