We're Hiring!

How to edit MapAnnotation (key-value pair annotations)?

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby Kouichi_C_Nakamura » Sat Jun 09, 2018 11:37 am

I've written a MATLAB function to unlink Annotation object(s) from image(s).

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
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby jmoore » Mon Jun 11, 2018 6:49 am

Hi Kouichi,

this is great. If you'd like to, feel free to open a pull request against the GitHub repository. I would think this could live beside linkAnnotation.m.

All the best,
~Josh
User avatar
jmoore
Site Admin
 
Posts: 1591
Joined: Fri May 22, 2009 1:29 pm
Location: Germany

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby Kouichi_C_Nakamura » Mon Jun 11, 2018 11:37 am

Thanks, I'm thinking of turn this to more versatile omero_unlinkAnnotation(). It would be more useful.

I haven't tested the code below for all the possible parentType. That would a bit beyond my scope.

Code: Select all
function omero_unlinkAnnotation(session,parentType,parentID,varargin)
% omero_unlinkAnnotation allows you to unlink all or specified
% MapAnnotation (Key-Value Pairs in GUI) from parent in OMERO server.
%
% SYNTAX
% omero_unlinkAnnotation(session,parentType,parentID)
% omero_unlinkAnnotation(session,parentType,parentID,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('knakamura', 'omeroomero')
%
% parentType  'project' | 'dataset' | 'image' | 'screen' | 'plate' |
%             'plateacquisition' | 'roi'
%
% parentID    vector of non-negative integers
%             parentID 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 parent 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', parentID);
%
%             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

objects = getObjectTypes();

p = inputParser;
p.addRequired('session',@(x) isjava(x));
p.addRequired('parentType',@(x) ischar(x) && ismember(x, {objects.name}));
p.addRequired('parentID',@(x) isnumeric(x) && isvector(x));

p.addOptional('annt',[],@(x) isempty(x) || all(isa(x, 'omero.model.Annotation')));
p.parse(session,parentType,parentID,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


% https://docs.openmicroscopy.org/omero/5.4.6/developers/Model/StructuredAnnotations.html
% Project
% Dataset
% Pixels
% OriginalFile
% PlaneInfo
% Roi
% Channel
% Folder

% https://github.com/openmicroscopy/openmicroscopy/blob/develop/examples/Training/matlab/ReadData.m#L334


switch parentType
   
    case 'project'
        ParentType = 'Project';
    case 'dataset'
        ParentType = 'Dataset';
    case 'image'
        ParentType = 'Image';
    case 'screen'
        ParentType = 'Screen';
    case 'plate'
        ParentType = 'Plate';
    case 'plateacquisition'
        ParentType = 'PlateAcquisition';
    case 'roi'
        ParentType = 'Roi';
end

parents = session.getQueryService().findAllByQuery(...
    [sprintf('select obj from %s as obj left outer join fetch obj.annotationLinks as link join fetch link.child as annotation where obj.id =  ',ParentType), ...
    num2str(parentID)], []);

% 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(parents);
for k = 1:n
   
    prt = parents.get(k-1);
   
    al = prt.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())
                   
                    prt.unlinkAnnotation(al.get(i).getChild());
                   
                end
            end
       
        end

    else
       
        prt.clearAnnotationLinks % did work (clear all the AnnotationLinks at once)
       
    end
   
    prt = session.getUpdateService().saveAndReturnObject(prt);

end


end
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby Kouichi_C_Nakamura » Mon Jun 11, 2018 3:01 pm

Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby jmoore » Mon Jun 11, 2018 3:22 pm

Ah, that looks great. Thanks.
~Josh
User avatar
jmoore
Site Admin
 
Posts: 1591
Joined: Fri May 22, 2009 1:29 pm
Location: Germany

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby Kouichi_C_Nakamura » Mon Jun 25, 2018 12:42 pm

I wrote a function for preparing MapAnnotationI object from string array of cell array of strings in MATLAB.

Do you think this is worth adding to the repo as well, or is it too niche? My hope is that this will make manipulation of MapAnnotation much easier in MATLAB.

Code: Select all
str = ["", "hoge";"bar","hoo";"ajaper",""];
ma = omero_str2MapAnnotation(str);
link1 = linkAnnotation(session, ma, 'image', imageID);



Code: Select all
function ma = omero_str2MapAnnotation(str, varargin)
% omero_str2MapAnnotation returns MapAnnotation object of OMERO from
% string array or cell array of strings
%
% SYNTAX
% ma = omero_str2MapAnnotation(str)
% ma = omero_str2MapAnnotation(str,iseditable)
%
% REQUIREMENTS
%
%   OMERO.matlab toolbox
%   https://docs.openmicroscopy.org/latest/omero/developers/Matlab.html
%
%   Before using this function, you need to run an equivalent of the
%   following command.
%
%     client = loadOmero('demo.openmicroscopy.org', 4064)
%
% INPUT ARGUMENTS
% str         string array | cell array of strings
%             Number of columns must be 2.
%
% iseditable  false (default) | true | 0 | 1
%             (Optional) If true or 1, MapAnnotation (Key-Value Pairs) will
%             be editable via GUI (OMERO.web or OMERO.insight)
%
%
%
% OUTPUT ARGUMENTS
% ma          MapAnnotationI objects
%             To link ma to an image in OMERO, identify image ID from OMERO
%             GUI and execute the following command
%     
%               client = loadOmero('demo.openmicroscopy.org', 4064)
%               session = client.createSession(username, password)
%
%               link1 = linkAnnotation(session, ma, 'image', imageID);
%
%               clear
%               unloadOmero
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% kouichi.c.nakamura@gmail.com
% 09-Jun-2018 15:20:17
%
% See also
% omero_xlsIHC2MapAnnotation, linkAnnotation

p = inputParser;
p.addRequired('str',@(x) size(str,2) ==2 && iscellstr(x) || isstring(x) );
p.addOptional('iseditable',false,@(x) isscalar(x) && x == 1 || x == 0);

p.parse(str,varargin{:});

iseditable = p.Results.iseditable;


%% Job

if iscellstr(str) %#ok<ISCLSTR>
   
   str = string(str);
   
end


import java.util.ArrayList
eval('import omero.model.NamedValue')

li = ArrayList;

for r = 1:size(str,1)
   
    li.add(NamedValue(str{r,1},str{r,2}));

end

eval('import omero.model.MapAnnotationI')

ma = MapAnnotationI(int64(1),true); % 'false' results in Java exception occurred: omero.UnloadedEntityException:
ma.setMapValue(li);

if iseditable
    %NOTE this is required to make it editable from GUI
    eval('import omero.constants.metadata.NSCLIENTMAPANNOTATION')
    ma.setNs(rstring(NSCLIENTMAPANNOTATION.value));
end




end


Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby sbesson » Tue Jun 26, 2018 2:17 pm

Hi Kouichi,

thanks for starting the discussion and offering to contribute back into the upstream source code.

As you have experienced, creating map annotations from primary MATLAB objects like cell arrays is not immediate. From our perspective, having some form of helper methods that would simplify the generation of these fundamental objects makes complete sense.

In terms of location, it would certainly make sense to have this type of function alongside other functions of the toolbox i.e. under https://github.com/openmicroscopy/openm ... src/helper.

Happy to see this turned into a Pull Request against https://github.com/openmicroscopy/openmicroscopy or keep the conversation here if you want additional feedback.

Best,
Sebastien
User avatar
sbesson
Team Member
 
Posts: 421
Joined: Tue Feb 28, 2012 7:20 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby Kouichi_C_Nakamura » Tue Jun 26, 2018 2:53 pm

Thanks for the positive comments.

As to pull request, the one I made before is already being processed, but not finished.

In this circumstance, should I create another branch from the develop branch and then make a separate pull request? Or is it better to add this function to the one being processed?
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby sbesson » Tue Jun 26, 2018 3:12 pm

Hi Kouichi,

please create a separate Pull Request if possible. The functionality are certainly complementary from a workflow perspective but independent from each other from a code perspective. As we are reviewing your contributions, this allows us to integrate any of them as soon they are ready while coupling the two changes would tie their merging once everything has been validated.

That being said, we definitely aim towards integrating both of your contributions in the upcoming OMERO 5.4.7.

Best,
Sebastien
User avatar
sbesson
Team Member
 
Posts: 421
Joined: Tue Feb 28, 2012 7:20 pm

Re: How to edit MapAnnotation (key-value pair annotations)?

Postby Kouichi_C_Nakamura » Tue Jun 26, 2018 4:38 pm

Raised another pull request.

https://github.com/openmicroscopy/openm ... /pull/5792

strToMapAnnotation.m components\tools\OmeroM\src\helper
mapAnnotationToCellstr.m components\tools\OmeroM\src\helper
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

PreviousNext

Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 1 guest