We're Hiring!

How to get the Java Gateway work in MATLAB?

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 get the Java Gateway work in MATLAB?

Postby Kouichi_C_Nakamura » Tue Sep 25, 2018 3:10 pm

I understand that one of the major weakness of OMERO-MATLAB is the fact that it does not support the Java Gateway (https://docs.openmicroscopy.org/omero/5 ... wayconnect).

Accept Gateway objects in all functions #5
https://github.com/ome/omero-matlab/issues/5

Following the example code shown in the above URL, I tried to use Java gateway from MATLAB.

Code: Select all
client = loadOmero(host, port) % this adds OMERO Java jar files to Java class paths
d = javaclasspath

d =
    {'xxxxxxx\OMERO-matlab\libs\omero_client.jar'}
    {'xxxxxxx\OMERO-matlab\libs\guava.jar'       }
    {'xxxxxxx\OMERO-matlab\libs'                 }



Code: Select all
cred = omero.gateway.LoginCredentials(userName, password, host, port);

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

user = gateway.connect(cred);


The above attempt ends up in the following execution error.

Code: Select all
'connect' requires one of the following:
  Control System Toolbox
  Instrument Control Toolbox
  OPC Toolbox
  Robotics System Toolbox
  Vehicle Network Toolbox


This implies that the method 'connect' of 'gateway' is not visible to MATLAB. Indeed, if I try to get the list of Java methods of the 'gateway' object, it issues a Java error, complaining that `loci/formats/meta/MetadataStore` is not found.

Code: Select all
methods(gateway)

Warning: A Java exception occurred getting the method description for the omero.gateway.Gateway class:
Java exception occurred:
java.lang.NoClassDefFoundError: loci/formats/meta/MetadataStore
   at java.lang.ClassLoader.defineClass1(Native Method)
   at java.lang.ClassLoader.defineClass(Unknown Source)
   at java.security.SecureClassLoader.defineClass(Unknown Source)
   at java.net.URLClassLoader.defineClass(Unknown Source)
   at java.net.URLClassLoader.access$100(Unknown Source)
   at java.net.URLClassLoader$1.run(Unknown Source)
   at java.net.URLClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(Unknown Source)
   at com.mathworks.jmi.CustomURLClassLoader.findClass(ClassLoaderManager.java:760)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.Class.getDeclaredMethods0(Native Method)
   at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
   at java.lang.Class.privateGetPublicMethods(Unknown Source)
   at java.lang.Class.getMethods(Unknown Source)
   at com.mathworks.jmi.OpaqueJavaInterface.getMethodDescriptions(OpaqueJavaInterface.java:278)
Caused by: java.lang.ClassNotFoundException: loci.formats.meta.MetadataStore
   at java.net.URLClassLoader.findClass(Unknown Source)
   at com.mathworks.jmi.CustomURLClassLoader.findClass(ClassLoaderManager.java:760)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   ... 17 more



Solution

Install Bioformats-MATLAB toolbox and run
Code: Select all
`bfCheckJavaPath();`

to add java class paths for Bioformats, which include `loci.meta`.

Then
Code: Select all
user = gateway.connect(cred);
worked properly.

Code: Select all
user =
omero.gateway.model.ExperimenterData (id=2)


Now we can think of the implementation of Java gateway, for example for `getPanelInfo.m`

If you guys can suggest a proper Java code for the operation needed for `getPanelInfo.m` using `gateway`, I might be able to translate that into MATLAB, though I can't promise.

Once we figure out one example, the rest of MATLAB functions may work in a similar way, I hope.
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to get the Java Gateway work in MATLAB?

Postby jburel » Wed Sep 26, 2018 8:07 pm

Thanks for pushing the toolbox forward
and finding a way to use the Java Gateway.
Could you explain what you have in mind for getPanelInfo.m?
Did you mean getPlaneInfo.m?


Jean-Marie
User avatar
jburel
Team Member
 
Posts: 348
Joined: Thu May 21, 2009 6:38 pm
Location: dundee

Re: How to get the Java Gateway work in MATLAB?

Postby Kouichi_C_Nakamura » Wed Sep 26, 2018 9:52 pm

Sorry, it was a typo of mine, and I meant `getPlaneInfo`, which you mentioned in the example in GitHub issue tracker. It doesn't really matter which function to pick up, but we need to start from somewhere.

Actually, the examples in the documentation page below might suffice for many purposes.

https://docs.openmicroscopy.org/omero/5 ... wayconnect

Soon, I will have to manipulate many images in OMERO from the command line for my work, so I might try gateway approach then.


BTW, I realized that using `client = loadOmero(host, port)` is not necessary for gateway.

We can think of something similar for gateway. For example, let's say we create`loadOmeroGateway.m` for this (any better name for this, please?). The syntax can be....

A
Code: Select all
[cred,gateway] = loadOmeroGateway(host, port, username, password)
user = gateway.connect(cred);

or

B
Code: Select all
[user,cred,gateway] = loadOmeroGateway(host, port, username, password)


We probably always want to use `connect` method right after the construction of gateway object, so the syntax B may be better?

Within `loadOmeroGateway`, the class paths for OMERO-MATLAB is added by the code snippet from `loadOmero.m`.


The function below worked in my environment.

Code: Select all
function [gateway,user,cred] = loadOmeroGateway(userName, password, host, port)
% loadOmeroGateway opens a connection to an OMERO server using Java gateway
% (https://docs.openmicroscopy.org/latest/omero/developers/Java.html#gatewayconnect).
%
% SYNTAX
% [gateway,user,cred] = loadOmeroGateway(userName, password, host, port)
%
%
% INPUT ARGUMENTS
% userName    char row vector
%             OMERO user name.
%
% password    char row vector
%             OMERO login password.
%
% host        char row vector
%             host OMERO server. Not including 'http:\\' etc
%
% port        scalar integer
%             Port number as an integer
%
%
% OUTPUT ARGUMENTS
% gateway     omero.gateway.Gateway Java object
%
%             # IMPORTANT #
%             In order to disconnet from OMERO.server, execute the below:
%         
%               gateway.disconnect();
%
% user        omero.gateway.model.ExperimenterData Java object
%
% cred        omero.gateway.LoginCredentials Java object
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% kouichi.c.nakamura@gmail.com
% 26-Sep-2018 23:53:43
%
% See also
% loadOmero
% https://javadoc.scijava.org/OMERO/omero/gateway/package-summary.html
%


% Check if "omero.client" is already on the classpath, if not
% then add the omero_client.jar to the javaclasspath.
if exist('omero.client','class') == 0
   
    disp('');
    disp('--------------------------');
    disp('OMERO.matlab Toolbox ');
    disp(omeroVersion);
    disp('--------------------------');
    disp('');
   
    % Add the JARs required by OMERO.matlab to the Java dynamic classpath
    % This will allow the import omero.* statement to pass successfully.
    omeroJars = getOmeroJars();
    cellfun(@javaaddpath, omeroJars);
    import omero.*;
   
    % Also add the OmeroM directory and its subdirectories to the path
    % so that functions and demos are available even if the user changes
    % directories. See the unloadOmero function for how to remove these
    % values.
    addpath(genpath(findOmero)); % OmeroM and subdirectories
   
    % If it does exist, then check that there aren't more than one
    % version active.
else
   
    w = which('omeroVersion','-ALL');
    sz = size(w);
    sz = sz(1);
    if sz > 1
        warning('OMERO:loadOmero','More than one OMERO version found!');
        disp(char(w));
    end
   
end

try
    bfCheckJavaPath; % add Bioformats MATLAB plugin java class paths
catch mexc1
    if strcmp(mexc1.identifier,'MATLAB:UndefinedFunction')
        error(['bfCheckJavaPath is not found. ',...
            'Check if Bioformats MATLAB toolbox is installed ',...
            'from https://www.openmicroscopy.org/bio-formats/downloads/ ',...
            'and the folder is added to the MATLAB search path.'])
    else
       throw(mexc1)
    end
end

p = inputParser;
p.addRequired('userName',@(x) ischar(x) && isrow(x))
p.addRequired('password',@(x) ischar(x) && isrow(x))
p.addRequired('host',@(x) ischar(x) && isrow(x))
p.addRequired('port',@(x) isnumeric(x) && isscalar(x))
p.parse(userName, password, host, port);

cred = omero.gateway.LoginCredentials(userName, password, host, port);

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

user = gateway.connect(cred);

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

Re: How to get the Java Gateway work in MATLAB?

Postby jburel » Thu Sep 27, 2018 9:22 am

Hi Kouichi

There is no method getPlaneInfo in the Java gateway. We should probably add such method.
The "Java" way to retrieve the information is actually already in https://github.com/openmicroscopy/openm ... laneInfo.m

Option B suggested below is probably better, port should be optional

I have not been able to run it.
I did not need to add Bio-Formats but i have another problem
"gateway.connect" leads to connect.m
I am using 2017a

Block diagram interconnections of dynamic systems

It might be that we have to introduce a new method in the Gateway to avoid such issue.
connectToServer
The Java example page is a good starting point

Jmarie
User avatar
jburel
Team Member
 
Posts: 348
Joined: Thu May 21, 2009 6:38 pm
Location: dundee

Re: How to get the Java Gateway work in MATLAB?

Postby Kouichi_C_Nakamura » Thu Sep 27, 2018 10:24 am

Thanks for the feedback.

What would be the default value of `port`?

"gateway.connect" leads to connect.m


I found this strange because, if `gateway` is `omero.gateway.Gateway` Java object, `gateway.connect` usually means `connect` is a method (or property/field) of that object. It sounds as though MATLAB interpreted `gateway` as something else (other class?).

Mine is R2018b, and it may be related to the toolboxes installed. From the previous error I got, the following toolbox might conflict with the name `connect`. Changing the method name may be the solution.

Control System Toolbox
Instrument Control Toolbox
OPC Toolbox
Robotics System Toolbox
Vehicle Network Toolbox
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to get the Java Gateway work in MATLAB?

Postby jburel » Thu Sep 27, 2018 12:38 pm

The default port will be set in the LoginCredentials
Code: Select all
public LoginCredentials(String username, String password, String host) {
        this(username, password, host, omero.constants.GLACIER2PORT.value);
    }

so if no port specified, new LoginCredentials(username, password, host) should be used
One optional parameter to add could also be "Encrypted" i.e. parameter used to encrypt all data transfer

I found the behaviour of the connect method strange too.

Cheers

jmarie
User avatar
jburel
Team Member
 
Posts: 348
Joined: Thu May 21, 2009 6:38 pm
Location: dundee

Re: How to get the Java Gateway work in MATLAB?

Postby Kouichi_C_Nakamura » Fri Sep 28, 2018 1:27 pm

Changed accordingly. In my environment, it works without port being specified.

[gateway,user,cred] = loadOmeroGateway(userName, password, host, varargin)
https://gist.github.com/kouichi-c-nakam ... 8b26a99509

I don't know how to reproduce the `connect` issue yet.
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to get the Java Gateway work in MATLAB?

Postby jburel » Mon Oct 01, 2018 1:59 pm

Hi Kouichi

I have tested the new function and it works without specifying the port
I still have an issue locally with the connect method so I had to comment the line out for now
As mentioned in my previous post, I don't need to have BF toolbox on the class path. So that probably something that the new method should not enforce.

Cheers
jmarie
User avatar
jburel
Team Member
 
Posts: 348
Joined: Thu May 21, 2009 6:38 pm
Location: dundee

Re: How to get the Java Gateway work in MATLAB?

Postby Kouichi_C_Nakamura » Mon Oct 01, 2018 2:35 pm

Thanks, jmarie.

I checked it again, but in my environment, commenting out `bfCheckJavaPath` results in the error below. Again this is related to the `connect` method.

Code: Select all
'connect' requires one of the following:
  Control System Toolbox
  Instrument Control Toolbox
  OPC Toolbox
  Robotics System Toolbox
  Vehicle Network Toolbox
Error in loadOmeroGateway (line 117)
user = gateway.connect(cred);
117 user = gateway.connect(cred);


But the `gateway` object is in the right class.

Code: Select all
K>> class(gateway)
ans =
    'omero.gateway.Gateway'


But bioformats is not available.
Code: Select all
K>> methods(gateway)
Warning: A Java exception occurred getting the method description for the omero.gateway.Gateway class:
Java exception occurred:
java.lang.NoClassDefFoundError: loci/formats/meta/MetadataStore
...




I don't quite understand this, but your issue with `connect` and my necessity of `bfCheckJavaPath` appear to be related somehow.

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

Re: How to get the Java Gateway work in MATLAB?

Postby jburel » Mon Oct 01, 2018 2:46 pm

Running locally
Code: Select all
which connect;

returns
Code: Select all
/Applications/MATLAB_R2017a.app/toolbox/control/ctrlobsolete/connect.m


jmarie
User avatar
jburel
Team Member
 
Posts: 348
Joined: Thu May 21, 2009 6:38 pm
Location: dundee

Next

Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 1 guest