I only tested this for MapAnnotationI and TagAnnotationI, but so far working well.
Feel free to use/edit this code.
- Code: Select all
function omero_unlinkAnnotationFromImage(session,imageID,varargin)
% omero_unlinkAnnotationFromImage allows you to unlink all or specified
% MapAnnotation (Key-Value Pairs in GUI) from images in OMERO server.
%
% SYNTAX
% omero_unlinkAnnotationFromImage(session,imageID)
% omero_unlinkAnnotationFromImage(session,imageID,annt)
%
%
% BEFORE USE
%
% client = loadOmero('demo.openmicroscopy.org', 4064)
% username = 'xxxxxxx'
% password = 'xxxxxxx'
% session = client.createSession(username, password)
%
%
% AFTER USE
%
% clear
% unloadOmero
%
% REQUIREMENTS
%
% OMERO.matlab toolbox
% https://docs.openmicroscopy.org/omero/5.4.6/developers/Matlab.html
%
%
% INPUT ARGUMENTS
% session omero.api.ServiceFactoryPrxHelper object
%
% client = loadOmero('demo.openmicroscopy.org', 4064)
% session = client.createSession('xxxxxx', 'xxxxxx')
%
%
% imageID vector of non-negative integers
% imageID can be found via OMERO GUI
%
% annt omero.model.Annotation object | [] (default)
%
% (Optional) omero.model.Annotation object (supports
% omero.model.MapAnnotationI, omero.model.TagAnnotationI,
% omero.model.CommentAnnotationI, and omero.model.FileAnnotationI)
% to be unlinked from images If empty or not specified, all
% linked MapAnnotation objects will be unlinked.
%
% For example, to obtain MapAnnotationI object(s) from an image:
%
% annt = getObjectAnnotations(session, 'map', 'image', imageID);
%
% annt can be empty, scalar or a vector
%
%
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% kouichi.c.nakamura@gmail.com
% 09-Jun-2018 03:27:05
%
% See also
% IHCprotocol2summary, omero_IHCsummary2MapAnnotation
p = inputParser;
p.addRequired('session',@(x) isjava(x));
p.addRequired('imageID',@(x) isnumeric(x) && isvector(x));
p.addOptional('annt',[],@(x) isempty(x) || all(isa(x, 'omero.model.Annotation')));
p.parse(session,imageID,varargin{:});
annt = p.Results.annt;
x = annt;
assert(isa(x,'omero.model.MapAnnotationI') || isa(x,'omero.model.TagAnnotationI') ...
|| isa(x,'omero.model.CommentAnnotationI') || isa(x,'FileAnnotationI'))
clear x
images = session.getQueryService().findAllByQuery(...
['select img from Image as img left outer join fetch img.annotationLinks as link join fetch link.child as annotation where img.id = ', ...
num2str(imageID)], []);
n = size(images);
for k = 1:n
img = images.get(k-1);
al = img.copyAnnotationLinks();
if ~isempty(annt)
for j = 1:numel(annt)
for i = (1:size(al))-1
if al.get(i).getChild().getId().equals(annt(j).getId())
img.unlinkAnnotation(al.get(i).getChild());
end
end
end
else
img.clearAnnotationLinks % did work (clear all the AnnotationLinks at once)
end
img = session.getUpdateService().saveAndReturnObject(img);
end
end