Skip to content

Instantly share code, notes, and snippets.

@chris-allan
Created May 23, 2014 10:50
Show Gist options
  • Save chris-allan/469b5ad3b3e3592c6692 to your computer and use it in GitHub Desktop.
Save chris-allan/469b5ad3b3e3592c6692 to your computer and use it in GitHub Desktop.
Create OMERO annotations on an Image from the command line
#!/usr/bin/env python
# encoding: utf-8
"""
Create OMERO annotations on an Image from the command line
Copyright (C) 2014 Glencoe Software, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sys
import omero
from omero.rtypes import rlong, rint, rbool, rstring, rdouble, rtime
from omero_ext import argparse
from uuid import uuid4 as uuid
from time import time
# Requires the following to be executed before this script:
#
# omero login
# export ICE_CONFIG=`omero sessions file`
#
client = omero.client()
session = client.getProperty("omero.sess")
session = client.joinSession(session)
p = argparse.ArgumentParser()
p.add_argument('image_id', type=long)
ns, other = p.parse_known_args()
query_service = session.getQueryService()
update_service = session.getUpdateService()
# Load the Image from the server with associated annotations so that
# we can created and link new annotations to it.
params = omero.sys.ParametersI()
params.addId(ns.image_id)
image = query_service.findByQuery(
'select image from Image as image '
'left join fetch image.annotationLinks as a_link '
'left join fetch a_link.child '
'where image.id = :id', params, {'omero.group': '-1'})
print 'Image:%d has %d annotations!' % (
ns.image_id, image.sizeOfAnnotationLinks()
)
# CommentAnnotation (String)
annotation = omero.model.CommentAnnotationI()
annotation.setTextValue(rstring('This is a comment!'))
image.linkAnnotation(annotation)
# BooleanAnnotation (Boolean)
annotation = omero.model.BooleanAnnotationI()
annotation.setBoolValue(rbool(True))
image.linkAnnotation(annotation)
# DoubleAnnotation (Double precision floating point)
annotation = omero.model.DoubleAnnotationI()
annotation.setDoubleValue(rdouble(3.14159265359))
image.linkAnnotation(annotation)
# LongAnnotation (64-bit integer)
annotation = omero.model.LongAnnotationI()
annotation.setLongValue(rlong(2014))
image.linkAnnotation(annotation)
# TermAnnotation (String)
annotation = omero.model.TermAnnotationI()
# uuid() provides a UUID of the form '369399bf-b670-4261-bd88-c765cc99868e'
annotation.setTermValue(rstring(str(uuid())))
image.linkAnnotation(annotation)
# TimestampAnnotation (UTC UNIX timestamp in milliseconds since epoch)
annotation = omero.model.TimestampAnnotationI()
# time() provides the UTC UNIX timestamp in seconds as floating point with
# microsecond precision.
annotation.setTimeValue(rtime(long(time() * 1000)))
image.linkAnnotation(annotation)
# XmlAnnotation (String)
annotation = omero.model.XmlAnnotationI()
annotation.setTextValue(rstring("""<?xml version="1.0" encoding="UTF-8"?>
<hello>
<world>Hello world!</world>
</hello>
"""))
image.linkAnnotation(annotation)
# XXX: No example provided for FileAnnotation or TagAnnotation
image = update_service.saveAndReturnObject(image)
print 'Image:%d now has %d annotations!' % (
ns.image_id, image.sizeOfAnnotationLinks()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment