Retrieving Images associated with an annotation
Posted:
Wed Jul 17, 2013 10:44 pm
by bweinstein
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
Re: Retrieving Images associated with an annotation
Posted:
Thu Jul 18, 2013 9:08 am
by atarkowska
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
Re: Retrieving Images associated with an annotation
Posted:
Fri Jul 19, 2013 3:23 pm
by bweinstein
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!
Re: Retrieving Images associated with an annotation
Posted:
Fri Jul 19, 2013 11:01 pm
by wmoore
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>
Re: Retrieving Images associated with an annotation
Posted:
Thu Jul 25, 2013 2:33 pm
by bweinstein
Aha! This is what I needed. Thanks so much!