We're Hiring!

How to edit existing Map Annotations (key-value pairs)??

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.

How to edit existing Map Annotations (key-value pairs)??

Postby Kouichi_C_Nakamura » Wed Dec 05, 2018 3:53 pm

How have asked something similar before (http://www.openmicroscopy.org/community ... f=6&t=8504), but this time question is a bit more specific.

I already have a MapAnnotation which is linked to an image or multiple images on OMERO. While keeping the ID of MapAnnotation unchanged, how can I modify the content of MapAnnotations?

Because the link is already there, I thought something like below might work, but it says:

Java exception occurred:
Ice.ObjectNotExistException



Code: Select all
client = loadOmero('xxxxxxxxxx', 4064);
session = client.createSession(username, password);

imageID = 74;
ma2 = getObjectAnnotations(session, 'map', 'image', imageID);

% modify the content of s
s = string(mapAnnotationToCellstr(ma2));
s_ = ["Experimenter","Mr Default Contrast";s];


keys = cellstr(s_(:,1));
values = cellstr(s_(:,2));

nv = java.util.ArrayList();
for i = 1 : numel(keys)
    nv.add(omero.model.NamedValue(keys{i}, values{i}));
end
ma2.setMapValue(nv);

context = java.util.HashMap;
parent = getObjects(session, 'image', imageID );
group = parent(1).getDetails().getGroup().getId().getValue();

mapAnnotation = session.getUpdateService().saveAndReturnObject(...
    mapAnnotation, context);


Can I get your help, please? Once I figure it out, I'll turn it into a MATLAB function:

Code: Select all
mapAnnotation = updateMapAnnotation(session, mapAnnotation, keyvalue, varargin)



mapAnnotationToCellstr.m

https://github.com/openmicroscopy/openm ... oCellstr.m
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit existing Map Annotations (key-value pairs)??

Postby manics » Wed Dec 05, 2018 4:34 pm

Here's an example of modifying a map annotation in Python I've quickly written, hopefully it's enough to get you started. If you need some Java or Matlab specific help just ask.
Code: Select all
: ma = conn.getObject('MapAnnotation',46151)._obj

: mv = ma.getMapValue()

: print(mv)
[object #0 (::omero::model::NamedValue)
{
    name = aaa
    value = 111
}, object #0 (::omero::model::NamedValue)
{
    name = bbb
    value = 222
}]

: del mv[1]

: print mv
[object #0 (::omero::model::NamedValue)
{
    name = aaa
    value = 111
}]

: mv.append(omero.model.NamedValue('new name', 'new value'))

: print mv
[object #0 (::omero::model::NamedValue)
{
    name = aaa
    value = 111
}, object #0 (::omero::model::NamedValue)
{
    name = new name
    value = new value
}]

: conn.getUpdateService().saveAndReturnObject(ma)
User avatar
manics
Team Member
 
Posts: 261
Joined: Mon Oct 08, 2012 11:01 am
Location: Dundee

Re: How to edit existing Map Annotations (key-value pairs)??

Postby Kouichi_C_Nakamura » Wed Dec 05, 2018 5:12 pm

Thanks. Looks very similar to mine. I'll look into it further.
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit existing Map Annotations (key-value pairs)??

Postby Kouichi_C_Nakamura » Wed Dec 05, 2018 10:13 pm

It may need more testing, but it worked well. Thank you. I'll make a Pull Request shortly.

Code: Select all
function ma = updateMapAnnotation(session, ma, keyvalue, varargin)
% updateMapAnnotation will update the content (key-value pairs) of
% MapAnnotation ma while maintaining its ID.
%
% SYNTAX
% ma = updateMapAnnotation(session, ma, keyvalue)
% ma = updateMapAnnotation(____,'Param',value)
%
% INPUT ARGUMENTS
% session     omero.api.ServiceFactoryPrxHelper object
%
% ma          MapAnnotationI object
%
% keyvalue    cell array of characters | string array
%             The number of columns must be 2. The first colum is for keys
%             and the second column is for values.
%
% OPTIONAL PARA<ETER/VALUE PAIRS
% 'namespace' char
%             Namespace for the MapAnnotation. Use the following value,
%             if you want to make this MapAnnotation editable via GUI.
%
%               char(omero.constants.metadata.NSCLIENTMAPANNOTATION)
%
% 'description'
%             char
%             Description for the MapAnnotation.
%
% 'group'     a positive integer
%             group ID
%
% 'iseditable'
%             false (default) | true | 0 | 1
%             If true or 1, MapAnnotation (Key-Value Pairs) will
%             be editable via GUI (OMERO.web or OMERO.insight). If true,
%             this will overwrite 'namespace'.
%
%
%
% OUTPUT ARGUMENTS
% ma          MapAnnotationI object
%
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% kouichi.c.nakamura@gmail.com
% 05-Dec-2018 18:23:13
%
% See also
% writeMapAnnotation, strToMapAnnotation
% https://www.openmicroscopy.org/community/viewtopic.php?f=6&t=8644

ip = inputParser;
ip.addRequired('session',@(x) isscalar(x));
ip.addRequired('ma',@(x) isa(x,'omero.model.MapAnnotationI'));
ip.addRequired('keyvalue',@(x) isempty(x) || size(x,2) == 2 && (iscellstr(x) || isstring (x)));
ip.addParameter('namespace', '', @ischar);
ip.addParameter('description', '', @ischar);
ip.addParameter('iseditable', false, @(x) isscalar(x) && x == 1 || x == 0);
ip.addParameter('group', [], @(x) isscalar(x) && isnumeric(x));
ip.parse(session, ma, keyvalue,varargin{:});


context = java.util.HashMap;
% Check if the Annotation exists on the server
try
    group = ma.getDetails().getGroup().getId().getValue();
    context.put('omero.group', java.lang.String(num2str(group)));
    if isempty(ip.Results.group)
         index = length(varargin);
         varargin{index + 1} = 'group';
         varargin{index + 2} = group;
    end
catch
end

% In case the MapAnnotation does not yet exist on the server:
if ~context.containsKey('omero.group') && ~isempty(ip.Results.group)
    context.put(...
        'omero.group', java.lang.String(num2str(ip.Results.group)));
end


if ~isempty(ip.Results.description)
    ma.setDescription(rstring(ip.Results.description));
end


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

%update the keys and values of the object

keys = cellstr(keyvalue(:,1));
values = cellstr(keyvalue(:,2));

nv = java.util.ArrayList();
for i = 1 : numel(keys)
    nv.add(omero.model.NamedValue(keys{i}, values{i}));
end
ma.setMapValue(nv);

ma = session.getUpdateService().saveAndReturnObject(...
    ma,context);


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

Re: How to edit existing Map Annotations (key-value pairs)??

Postby Kouichi_C_Nakamura » Thu Dec 06, 2018 11:32 am

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

Re: How to edit existing Map Annotations (key-value pairs)??

Postby jmoore » Mon Dec 10, 2018 9:34 am

Thanks, Kouichi!
User avatar
jmoore
Site Admin
 
Posts: 1591
Joined: Fri May 22, 2009 1:29 pm
Location: Germany


Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 0 guests