Page 2 of 4

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

PostPosted: Wed Jun 06, 2018 9:06 am
by Kouichi_C_Nakamura
Thank you for the detailed help!

img2.clearAnnotationLinks did work.

Code: Select all
img_id2 = 18683;
ma2 = getObjectAnnotations(session, 'map', 'image', img_id2);

images2 = 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(img_id2)], []);
   
img2 = images2.get(0)
   
img2.clearAnnotationLinks

img2 = session.getUpdateService().saveAndReturnObject(img2);

ma3 = getObjectAnnotations(session, 'map', 'image', img_id2);

% ma3 is empty



However, img2.unlinkAnnotation(ma2) did not work. It's shame because it can be more useful if I can unlink annotations specifically. I'm wondering why this does not work. Again, API documentation doesn't really help, so I need to ask you.

Code: Select all
img_id2 = 18683;
ma2 = getObjectAnnotations(session, 'map', 'image', img_id2);

images2 = 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(img_id2)], []);
   
img2 = images2.get(0)
   
img2.unlinkAnnotation(ma2)

img2 = session.getUpdateService().saveAndReturnObject(img2);

ma3 = getObjectAnnotations(session, 'map', 'image', img_id2);

% ma3 is not empty


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

PostPosted: Wed Jun 06, 2018 9:29 am
by Kouichi_C_Nakamura
Now that I know how to unlink MapAnnotation, I should be able to add (link) new MapAnnotation.

Code: Select all
import java.util.ArrayList
import ome.model.internal.NamedValue

li = ArrayList;
li.add(NamedValue('Experiment ID',experimentID));

% this way I can add as many NamedValue objects as I like

import ome.model.annotations.MapAnnotation

ma3 = MapAnnotation;
ma3.setMapValue(li);

link1 = linkAnnotation(session, ma3, 'image', img_id2); % this line issued an error

context = java.util.HashMap;
ma3 = session.getUpdateService().saveAndReturnObject(ma3, context);



However, the line below issued an error and I'm stuck again. Any ideas, please?

Maybe the way I instantiated MapAnnotation is wrong?

Code: Select all
>> link1 = linkAnnotation(session, ma3, 'image', img_id2);
No method 'setChild' with matching signature found for class 'omero.model.ImageAnnotationLinkI'.

Error in linkAnnotation (line 61)
link.setChild(annotation);

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

PostPosted: Wed Jun 06, 2018 9:35 am
by jmoore
Hi Kouichi,

the issue with

Code: Select all
img2.unlinkAnnotation(ma2)


is that the comparison is based on object identity. You currently have 2 versions of ma2: the one that you loaded via getObjectAnnotations and the one that's coming back from findAllByQuery. You will need to loop over the annotations and find the one that matches by id:

Code: Select all
$ javac -cp 'lib/client/*' kouichi.java
$ java -cp 'lib/client/*:.' kouichi
before: 1
after: 0
$ cat kouichi.java

import omero.model.*;

public class kouichi {

    public static void main(String[] args) {

        // Setup data
        Annotation ma2 = new MapAnnotationI(1L, false);
        Annotation copy = new MapAnnotationI(1L, false);
        Image img = new ImageI();
        img.linkAnnotation(copy);

        System.out.println("before: " + img.sizeOfAnnotationLinks());
        for (ImageAnnotationLink link : img.copyAnnotationLinks()) {
            if (link.getChild().getId().equals(ma2.getId())) {
                img.unlinkAnnotation(link.getChild());
            }
        }
        System.out.println("after: " + img.sizeOfAnnotationLinks());

    }

}


~Josh

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

PostPosted: Fri Jun 08, 2018 2:05 pm
by Kouichi_C_Nakamura
Hi Josh,

Thanks a lot for the example code. I managed to get unlinkAnnotation method working!

Now I also managed to save new MapAnnotation to an image.

Code: Select all
import java.util.ArrayList
import omero.model.NamedValue
import omero.model.MapAnnotationI

li = ArrayList;
li.add(NamedValue('Experiment ID',experimentID));
% ... add many other NamedValue objects to li

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

link1 = linkAnnotation(session, ma3, 'image', img_id2);


images2 = 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(img_id2)], []);

size(images2)
img2 = images2.get(0)

context = java.util.HashMap;
img2 = session.getUpdateService().saveAndReturnObject(img2, context);



Question 1

Key-Value Pairs added by this way was named with 'Added by: xxxxxx', and even though I logged in with the same account, I cannot edit it from OMERO.web side. Is this what's expected? Can I configure who can edit particular annotations?

Question 2

For above, I needed to use omero.model.MapAnnotationI class, but this was not defined in https://downloads.openmicroscopy.org/om ... lice2html/

Instead, I only found omero.model.MapAnnotation class (without large I) in the documentation. Why is this difference?

Question 3

Later, I realized that, in the above code, use of linkAnnotation suffices to link the MapAnnotation and session.getUpdateService().saveAndReturnObject(img2, context) was not needed. This contradicts my previous experience. Why the difference?

Sorry for asking many questions, but I really can't find answers in the documentation.

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

PostPosted: Fri Jun 08, 2018 2:32 pm
by jmoore
Hi Kouichi,

Kouichi_C_Nakamura wrote:
Question 1

Key-Value Pairs added by this way was named with 'Added by: xxxxxx', and even though I logged in with the same account, I cannot edit it from OMERO.web side. Is this what's expected? Can I configure who can edit particular annotations?


The GUIs only permit editing annotations that have been created with const string NSCLIENTMAPANNOTATION = "openmicroscopy.org/omero/client/mapAnnotation";.


Question 2

For above, I needed to use omero.model.MapAnnotationI class, but this was not defined in https://downloads.openmicroscopy.org/om ... lice2html/

Instead, I only found omero.model.MapAnnotation class (without large I) in the documentation. Why is this difference?


Please see http://docs.openmicroscopy.org/omero/5.4.6/developers/GettingStarted/AdvancedClientDevelopment.html?highlight=concrete%20class#objectfactory-and-casting :

Quote: "...you may have noticed how there are two classes for each type: Image and ImageI. Classes defined in slice are by default data objects, more like C++ā€™s structs than anything else. As soon as a class defines a method, however, it becomes an abstract entity and requires application writers to provide a concrete implementation (hence the ā€œIā€). All OMERO classes define methods, but OMERO takes care of providing the implementations for you via code generation. For each slice-defined and Ice-generated class omero.model.Something, there is an OMERO-generated class omero.model.SomethingI which can be instantiated."


Question 3

Later, I realized that, in the above code, use of linkAnnotation suffices to link the MapAnnotation and session.getUpdateService().saveAndReturnObject(img2, context) was not needed. This contradicts my previous experience. Why the difference?


If you mean the Matlab `linkAnnotation` function, then that will be because of the internal implementation, i.e. it's doing more than the simple data object method `image.linkAnnotation`. (You can probably guess that this function does more since it takes a `session` object: that would only be necessary if it's going to make remote calls).


Sorry for asking many questions, but I really can't find answers in the documentation.


No worries. We know they need working on, and we're happy to help.
~Josh

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

PostPosted: Fri Jun 08, 2018 3:01 pm
by Kouichi_C_Nakamura
Thanks a lot. Very helpful.

Question 2 is solved.

For Question 3, I think I might have confused linkAnnotation MATLAB function and Java method.

As to Question 1, can you tell me more about how to use the const value?

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

PostPosted: Fri Jun 08, 2018 3:05 pm
by jmoore
Kouichi_C_Nakamura wrote:Thanks a lot. Very helpful.

Question 2 is solved.


Great.

For Question 3, I think I might have confused linkAnnotation MATLAB function and Java method.


Understood.

As to Question 1, can you tell me more about how to use the const value?


You need to import the Java class omero.constants.metadata.NSCLIENTMAPANNOTATION and then use NSCLIENTMAPANNOTATION.value as the String value.

~Josh

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

PostPosted: Fri Jun 08, 2018 3:18 pm
by Kouichi_C_Nakamura
You need to import the Java class omero.constants.metadata.NSCLIENTMAPANNOTATION and then use NSCLIENTMAPANNOTATION.value as the String value.


Thanks. I obtained the string value, but how do I use it? How do I associate that with a particular MapAnnotation? I cannot find a method for that.

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

PostPosted: Fri Jun 08, 2018 3:43 pm
by jmoore

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

PostPosted: Fri Jun 08, 2018 4:22 pm
by Kouichi_C_Nakamura
Perfect. Thanks a lot. Now Key-Value Pairs are editable from GUI!

This really was almost impossible without your help.

Code: Select all
import omero.model.MapAnnotationI

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

%NOTE this is required to make it editable from GUI
import omero.constants.metadata.NSCLIENTMAPANNOTATION
ma3.setNs(rstring(NSCLIENTMAPANNOTATION.value));

link1 = linkAnnotation(session, ma3, 'image', img_id2);