Page 1 of 2

gateway.getFacility() error in MATLAB

PostPosted: Thu Nov 01, 2018 12:08 pm
by Kouichi_C_Nakamura
Hello, I'm trying to make use of OMERO Java gateway from within MATLAB. (Discussion http://www.openmicroscopy.org/community/viewtopic.php?f=6&t=8593&sid=b68cbf8d65a77f4baaeaec9041c58dbf)

My loadOmeroGateway.m allows me to obtain omero.gateway.Gateway object.
https://gist.github.com/kouichi-c-nakamura/c8d335bfe45ee15e3bd86771a8578596

Now, for a trial, I want to retrieve an image in an OMERO server, according to this guide:https://docs.openmicroscopy.org/omero/5.4.9/developers/Java.html#read-data

What I was trying to do in MATLAB was below:
Code: Select all
ctx = omero.gateway.SecurityContext(user.getGroupId());
imageId = 46629;
browse = gateway.getFacility(omero.gateway.facility.BrowseFacility.class);
image = browse.getImage(ctx, imageId);


However, gateway.getFacility issues an error:
Code: Select all
No method 'getFacility' with matching signature found for class
'omero.gateway.Gateway'.


As far as I can see `omero.gateway.facility.BrowseFacility.class` returns a char type 'omero.gateway.facility.BrowseFacility' in MATLAB (.class is Java class literal). I guess this must be in a different type.

According to API documentation...
Code: Select all
public <T extends Facility> T getFacility(Class<T> type)


I guess the problem here might be related to the use of Generics <T>, but I don't really get what is supposed to be for type here. What is "Class<T> type"? Can someone give me any advice, please?

https://docs.oracle.com/javase/specs/jl ... jls-15.8.2

The type of C.class, where C is the name of a class, interface, or array type (ยง4.3), is Class<C>.


According to this, `omero.gateway.facility.BrowseFacility.class` must be in the type Class<BrowseFacility>, while in reality it's converted to char string by MATLAB. Is my understanding right?

I've just read the thread below in StackOverflow, and started to think that there may not be an easy solution for this one.

https://stackoverflow.com/questions/4463625/using-java-generic-classes-in-matlab

Re: gateway.getFacility() error in MATLAB

PostPosted: Fri Nov 02, 2018 10:46 am
by Dominik
Hi Kouichi.

I've not tried it myself yet, but what happens if you pass the JNI signature of the BrowseFacility class to the getFacility method, ie.

Code: Select all
browse = gateway.getFacility('Lomero/gateway/facility/BrowseFacility;');


Regards,
Dominik

Re: gateway.getFacility() error in MATLAB

PostPosted: Fri Nov 02, 2018 11:29 am
by Dominik
Unfortunately the JNI signature doesn't work... but I found a way to do it now:

You can get the Java class with 'java.lang.Class.forName' method.

For some reason the simple method
Code: Select all
java.lang.Class.forName('omero.gateway.facility.BrowseFacility')

doesn't work. You'd need to set initialize to false and specify a classloader (can just use the one from the gateway), like that:
Code: Select all
clazz = java.lang.Class.forName('omero.gateway.facility.BrowseFacility', false, gateway.getClass().getClassLoader())


So a full example using your 'loadOmeroGateway' example would look like this:

Code: Select all
imageId = 96953;

simpleLogger = omero.log.SimpleLogger();
gateway = omero.gateway.Gateway(simpleLogger);

user = gateway.connect(cred);

ctx = omero.gateway.SecurityContext(user.getGroupId());

clazz = java.lang.Class.forName('omero.gateway.facility.BrowseFacility', false, gateway.getClass().getClassLoader())
browse = gateway.getFacility(clazz);
image = browse.getImage(ctx, imageId);
disp(image.getName());

gateway.disconnect();


Regards,
Dominik

Re: gateway.getFacility() error in MATLAB

PostPosted: Fri Nov 02, 2018 3:34 pm
by Kouichi_C_Nakamura
Thanks a lot, Dominik. It worked for me as well.

Now I think we've opened up the possibility of making all the OMERO-related MATLAB functions support Gateway object.

Code: Select all
clazz = java.lang.Class.forName('omero.gateway.facility.ROIFacility', ...
    false, gateway.getClass().getClassLoader())

roifac = gateway.getFacility(clazz);
roiresults = roifac.loadROIs(ctx, image.getId())
r = roiresults.iterator().next();
rois = r.getROIs()

j = rois.iterator();
roi = j.next();


This roi is an `omero.gateway.model.ROIData` object.

I want to get the text label for the ROI. If I can retrieve an `ome.model.roi.Label` object `label`, `label.getTextValue()` should be it.

How can I achieve that? Thanks again.

Re: gateway.getFacility() error in MATLAB

PostPosted: Mon Nov 05, 2018 1:15 pm
by Dominik
Unfortunately it's not that easy. You'd need to get the shapes from the ROIData object, cast the ShapeData objects into their real class, e.g. an EllipseData, and most of these shape classes have a field called 'text' which is basically the "label" for the ROI. Theoretically you could have multiple shapes per ROI, but if you create ROIs with an OMERO client they usually create one ROI per shape.

Regards,
Dominik

Re: gateway.getFacility() error in MATLAB

PostPosted: Fri Nov 09, 2018 4:07 pm
by Kouichi_C_Nakamura
Thanks a lot. Although `getShapeCount()` returns 1, `getShapes()` returns empty. So I cannot go further. Any suggestions, please?

Code: Select all
>> roi.getShapeCount
ans =
     1
>> roi.getShapes(0,0)
ans =
     []
>> class(ans)
ans =
    'double'

Re: gateway.getFacility() error in MATLAB

PostPosted: Fri Nov 09, 2018 4:34 pm
by Dominik
And you're sure the shape is on plane z=0 / t=0 ?

Regards,
Dominik

Re: gateway.getFacility() error in MATLAB

PostPosted: Fri Nov 09, 2018 4:51 pm
by Kouichi_C_Nakamura
Thanks! I realized that I can find which Z and T planes an ROI is on in iviewer GUI (I need to subtract 1 to use those numbers as input arguments).

Code: Select all
>> roi.getShapeCount
ans =
     1
>> shapes = roi.getShapes(7,0)
shapes =
[omero.gateway.model.TextData (id=258370)]


Now, the `shapes` in java.util.ArrayList.

How do I cast it to `omero.gateway.model.TextData`? This https://stackoverflow.com/questions/36868329/how-to-cast-arraylist-to-a-class says you can't.

Because now we've got the id, can I retrieve the TextData using the ID?

I had a look at omero-matlab functions for ROIs, but there isn't getXXXX functions in there.

Re: gateway.getFacility() error in MATLAB

PostPosted: Tue Nov 13, 2018 1:46 pm
by Dominik
You should be able to just do
Code: Select all
shape = shape.get(0)

to get the first shape, which in your case should give you the TextData object.

Kind Regards,
Dominik

Re: gateway.getFacility() error in MATLAB

PostPosted: Wed Jan 23, 2019 4:18 pm
by Kouichi_C_Nakamura
Since this operation may be a routine in Gateway usage, I have turned the above solution into a MATLAB function.

Code: Select all
function fac = getGatewayFacility(gateway,classname)
% getGatewayFacility is a MATLAB function to be used with OMERO Java
% Gateway.
%
% in Java, the following code returns the XxxxxFacility object fac.
%   XxxxxFacility fac = gateway.getFacility(XxxxxFacility.class)
%
% However, running the same code in MATLAB requires special handling and
% this functions does that for you. For more details, see
% https://docs.openmicroscopy.org/latest/omero/developers/Java.html
% http://www.openmicroscopy.org/community/viewtopic.php?f=6&t=8615
%
%
% SYNTAX
% fac = getGatewayFacility(classname)
%
% INPUT ARGUMENTS
% classname   'AdminFacility' | 'BrowseFacility' | 'DataManagerFacility' |
%             'Facility' | 'MetadataFacility' | 'RawDataFacility' |
%             'ROIFacility' | 'SearchFacility' | 'TablesFacility' |
%             'TablesFacilityHelper' | 'TransferFacility' |
%             'TransferFacilityHelper'
%
%             omero.gateway.facility Java class name in char row vector
%             eg. 'BrowseFacility' for omero.gateway.facility.BrowseFacility
%
%             https://downloads.openmicroscopy.org/latest/omero/api//omero/gateway/facility/package-summary.html
%
% OUTPUT ARGUMENTS
% fac         omero.gateway.facility.XxxxxxFacility Java object
%
%
% EXAMPLE
% browse = getGatewayFacility(gateway,'BrowseFacility')
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% kouichi.c.nakamura@gmail.com
% 23-Jan-2019 16:07:10
%
% See also
% https://docs.openmicroscopy.org/latest/omero/developers/Java.html#read-data
% http://www.openmicroscopy.org/community/viewtopic.php?f=6&t=8615

%TODO by convetion, it's better to be getFacility, but this name confilcts
% with gateway object's getFacility method.

%TODO it is possible to check classname

clazz = java.lang.Class.forName(['omero.gateway.facility.',classname ], ...
    false, gateway.getClass().getClassLoader());

fac = gateway.getFacility(clazz);


https://gist.github.com/kouichi-c-nakam ... 4fa185bb94