def localization_files_from_image_url(image_url, out_dir): """ Parameters ---------- image_url : str url from OMERO web client, gotten typically by clicking the link symbol with an image selected, i.e. `Link to this image`. out_dir : str path / name of tempfile.TemporaryDirectory Returns ------- localization_files : list paths to localization files saved to disk """ from urllib.parse import urlparse, parse_qs from omero.util.populate_roi import DownloadingOriginalFileProvider url = urlparse(image_url) # for `Link to this image` image_id = int(parse_qs(url.query)['show'][0].split('-')[-1]) localization_files = [] with cli_login(*LOGIN_ARGS) as cli: conn = BlitzGateway(client_obj=cli._client) # Would be nice to specify pyme.localizations namespace, but probably # reliable to check file extentsion since one can manually attach # localizations with no namespace specified # localization_links = list(conn.getAnnotationLinks('Image', # parent_ids=[image_id], # ns="pyme.localizations")) localization_links = list( conn.getAnnotationLinks('Image', parent_ids=[image_id])) raw_file_store = conn.createRawFileStore() for link in localization_links: try: # select for Blitzwrapped omero types with getFile attr og_file = link.getChild().getFile()._obj except AttributeError: # not all BlitzWrapped omero types have getFile continue filename = og_file.getName().getValue() if os.path.splitext(filename)[-1] not in ['.hdf', '.h5r']: continue path = os.path.join(out_dir, filename) localization_files.append(path) raw_file_store.setFileId(og_file.id.val) with open(path, 'wb') as f: f.write(raw_file_store.read(0, og_file.size.val)) return localization_files
owner = tag.getDetails().owner.id.val #print tag.textValue, owner tags.append([tag.textValue, str(tag.id), str(owner)]) #sort the tags in descending order to allow to see duplicates tags.sort(key=lambda tag: tag[0].lower()) print "sorted tags", tags prev_tag = "" prev_id = 0 for t in tags: tag_id = str(t[1]) if t[0] == prev_tag: # move all tagged objects to previous tags and delete for link in conn.getAnnotationLinks('Image', ann_ids=[tag_id]): link._obj.child = omero.model.TagAnnotationI(prev_id, False) link.save() conn.deleteObjects('TagAnnotation', [tag_id]) prev_tag = t[0] prev_id = tag_id # Return some value(s). # Here, we return anything useful the script has produced. # NB: The Insight and web clients will display the "Message" output. msg = "Script ran OK" client.setOutput("Message", rstring(msg)) client.closeSession()
owner = tag.getDetails().owner.id.val #print tag.textValue, owner tags.append([tag.textValue, str(tag.id), str(owner)]) #sort the tags in descending order to allow to see duplicates tags.sort(key=lambda tag: tag[0].lower()) print "sorted tags", tags prev_tag = "" prev_id = 0 for t in tags: tag_id = str(t[1]) if t[0] == prev_tag: # move all tagged objects to previous tags and delete for link in conn.getAnnotationLinks('Image', ann_ids=[tag_id]): link._obj.child = omero.model.TagAnnotationI(prev_id, False) link.save() for link in conn.getAnnotationLinks('Dataset', ann_ids=[tag_id]): link._obj.child = omero.model.TagAnnotationI(prev_id, False) link.save() for link in conn.getAnnotationLinks('Project', ann_ids=[tag_id]): link._obj.child = omero.model.TagAnnotationI(prev_id, False) link.save() conn.deleteObjects('TagAnnotation', [tag_id]) prev_tag = t[0] prev_id = tag_id # Return some value(s). # Here, we return anything useful the script has produced.
ann_ids = [] for ann in image.listAnnotations(): print(ann.getId(), ann.OMERO_TYPE) ann_ids.append(ann.id) print(" added by ", ann.link.getDetails().getOwner().getOmeName()) if ann.OMERO_TYPE == omero.model.TagAnnotationI: print("Tag value:", ann.getTextValue()) # Get Links between Objects and Annotations # Find links to Images, unlink Images from these annotations # and link them to a new Tag Annotation tag_ann = omero.gateway.TagAnnotationWrapper(conn) tag_ann.setValue("Replacement Tag") tag_ann.save() ann_ids = ann_ids[:1] # Just use first annotation for link in conn.getAnnotationLinks('Image', ann_ids=ann_ids): print("Image ID:", link.getParent().id) print("Annotation ID:", link.getChild().id) # Update the child of the underlying omero.model.ImageAnnotationLinkI link._obj.child = omero.model.TagAnnotationI(tag_ann.id, False) link.save() # Find Annotations linked to Object(s), filter by namespace (optional) for link in conn.getAnnotationLinks('Image', parent_ids=[imageId], ns="test.namespace"): print("Annotation ID:", link.getChild().id) # Close connection # ================ # When you are done, close the session to free up server resources.