Advertisement
Guest User

Untitled

a guest
May 17th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import omero
  2. from omero.gateway import BlitzGateway
  3. from omero.sys import ParametersI
  4. import sys
  5.  
  6. USERNAME = ''
  7. PASSWORD = ''
  8. HOST = 'localhost'
  9. PORT = 4064
  10.  
  11.  
  12. def connect():
  13. """ Create an OMERO Connection """
  14.  
  15. # Initialize the connection
  16. conn = BlitzGateway(USERNAME,
  17. PASSWORD,
  18. host=HOST,
  19. port=PORT)
  20.  
  21. # Connect
  22. connected = conn.connect()
  23.  
  24. # Check that the connection was established
  25. if not connected:
  26. sys.stderr.write("Error: Connection not available, "
  27. "please check your user name and password.\n")
  28. sys.exit(1)
  29. return conn
  30.  
  31.  
  32. def queryImageTags(image_id):
  33. """ Query for tag annotations attached to an image. This will also
  34. return details (owner, group, etc) about the annotation.
  35. """
  36. conn = connect()
  37.  
  38. qs = conn.getQueryService()
  39.  
  40. conn.SERVICE_OPTS.setOmeroGroup(-1)
  41.  
  42. params = ParametersI()
  43. params.add('iid', omero.rtypes.wrap(image_id))
  44.  
  45. q = """
  46. SELECT anno
  47. FROM Image image
  48. JOIN image.annotationLinks links
  49. JOIN links.child anno
  50. WHERE image.id = :iid
  51. AND anno.class = TagAnnotation
  52. """
  53.  
  54. for row in qs.projection(q, params, conn.SERVICE_OPTS):
  55. print 'Tag: %s (%s)' % (row[0].val.getTextValue().val,
  56. row[0].val.getDescription().val)
  57.  
  58.  
  59. def queryImagesTags():
  60. """ Query for all tag annotations attached to images. This will also
  61. return details (owner, group, etc) about the annotation.
  62. """
  63. conn = connect()
  64.  
  65. qs = conn.getQueryService()
  66.  
  67. conn.SERVICE_OPTS.setOmeroGroup(-1)
  68.  
  69. params = ParametersI()
  70.  
  71. q = """
  72. SELECT image, anno
  73. FROM Image image
  74. JOIN image.annotationLinks links
  75. JOIN links.child anno
  76. WHERE anno.class = TagAnnotation
  77. """
  78.  
  79. for row in qs.projection(q, params, conn.SERVICE_OPTS):
  80. image = row[0].val
  81. anno = row[1].val
  82. print 'Image: %s Tag: %s (%s)' % (image.getName().val,
  83. anno.getTextValue().val,
  84. anno.getDescription().val)
  85.  
  86.  
  87. def queryImageTagsTerse(image_id):
  88. """ Query for tag annotation details only, attached to an image.
  89. """
  90. conn = connect()
  91.  
  92. qs = conn.getQueryService()
  93.  
  94. conn.SERVICE_OPTS.setOmeroGroup(-1)
  95.  
  96. params = ParametersI()
  97. params.add('iid', omero.rtypes.wrap(image_id))
  98.  
  99. q = """
  100. SELECT anno.textValue, anno.description
  101. FROM Image image
  102. JOIN image.annotationLinks links
  103. JOIN links.child anno
  104. WHERE image.id = :iid
  105. AND anno.class = TagAnnotation
  106. """
  107.  
  108. for row in qs.projection(q, params, conn.SERVICE_OPTS):
  109. print 'Tag: %s (%s)' % (row[0].val,
  110. row[1].val)
  111.  
  112.  
  113. if __name__ == "__main__":
  114.  
  115. # Must be a long
  116. queryImageTags(103L)
  117.  
  118. queryImagesTags()
  119.  
  120. # Must be a long
  121. queryImageTagsTerse(103L)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement