Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created May 22, 2012 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-moore/2769549 to your computer and use it in GitHub Desktop.
Save will-moore/2769549 to your computer and use it in GitHub Desktop.
Python example for adding various annotations to a Project.
#!/usr/bin/env python
"""
Script for adding annotations to Project.
"""
import omero.clients
from omero import client_wrapper
from omero.rtypes import *
import random
from datetime import datetime
user = 'root'
pw = 'omero'
host = 'localhost'
blitzcon = client_wrapper(user, pw, host=host, port=4064)
blitzcon.connect()
updateService = blitzcon.getUpdateService()
# get parent to annotate
pId = 52
parent = blitzcon.getObject("Project", pId)._obj
def saveAndLinkAnnotation(updateService, parent, annotation, ns=None, description=None):
""" Saves the Annotation and Links it to a Project, Dataset or Image """
if ns:
annotation.setNs(rstring(ns))
if description:
annotation.setDescription(rstring(description))
annotation = updateService.saveAndReturnObject(annotation)
if type(parent) == omero.model.DatasetI:
l = omero.model.DatasetAnnotationLinkI()
elif type(parent) == omero.model.ProjectI:
l = omero.model.ProjectAnnotationLinkI()
elif type(parent) == omero.model.ImageI:
l = omero.model.ImageAnnotationLinkI()
else:
return
parent = parent.__class__(parent.id.val, False)
l.setParent(parent)
l.setChild(annotation)
return updateService.saveAndReturnObject(l)
# Text Annotations
def addTag(updateService, parent, text, ns=None, description=None):
""" Adds a Tag. """
child = omero.model.TagAnnotationI()
child.setTextValue(rstring(text))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
def addComment(updateService, parent, text, ns=None, description=None):
""" Adds a Comment. """
child = omero.model.CommentAnnotationI()
child.setTextValue(rstring(text))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
def addXmlAnnotation(updateService, parent, xmlText, ns=None, description=None):
""" Adds XML Annotation. """
child = omero.model.XmlAnnotationI()
child.setTextValue(rstring(xmlText))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
# Basic Annotations
def addBooleanAnnotation(updateService, parent, boolean, ns=None, description=None):
""" Adds a Boolean Annotation. """
child = omero.model.BooleanAnnotationI()
child.setBoolValue(rbool(boolean))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
def addDoubleAnnotation(updateService, parent, double, ns=None, description=None):
""" Adds a Double Annotation. """
child = omero.model.DoubleAnnotationI()
child.setDoubleValue(rdouble(double))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
def addLongAnnotation(updateService, parent, longValue, ns=None, description=None):
""" Adds a Long Annotation. """
child = omero.model.LongAnnotationI()
child.setLongValue(rlong(longValue))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
def addTermAnnotation(updateService, parent, term, ns=None, description=None):
""" Adds a Term Annotation. """
child = omero.model.TermAnnotationI()
child.setTermValue(rstring(term))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
def addTimestampAnnotation(updateService, parent, timeValue, ns=None, description=None):
""" Adds a Timestamp Annotation. """
child = omero.model.TimestampAnnotationI()
child.setTimeValue(rtime(timeValue))
saveAndLinkAnnotation(updateService, parent, child, ns, description)
xml = "<testXml><testElement><annotation>Text</annotation></testElement></testXml>"
doubleVal = random.random()
timeVal = datetime.now().microsecond
addTag(updateService, parent, "Test-Tag", "test/omero/tag/ns", description=None)
addComment(updateService, parent, "Test-Comment", ns="test/omero/comment/ns", description=None)
addXmlAnnotation(updateService, parent, xml, ns="test/omero/xml/ns", description="Test xml annotation description")
addBooleanAnnotation(updateService, parent, True, ns="test/omero/boolean/ns", description="True if True, otherwise False")
addDoubleAnnotation(updateService, parent, doubleVal, ns="test/omero/double/ns", description="Random number!")
addLongAnnotation(updateService, parent, 123456, ns="test/omero/long/ns", description=None)
addTermAnnotation(updateService, parent, "Metaphase", ns="test/omero/term/ns", description="Metaphase is part of mitosis")
addTimestampAnnotation(updateService, parent, timeVal, ns="test/omero/timestamp/ns", description=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment