Skip to content

Instantly share code, notes, and snippets.

@kouichi-c-nakamura
Last active November 17, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kouichi-c-nakamura/911ea9b3b9bdd20b1ffd628b26a99509 to your computer and use it in GitHub Desktop.
Save kouichi-c-nakamura/911ea9b3b9bdd20b1ffd628b26a99509 to your computer and use it in GitHub Desktop.
function [gateway,user,cred] = loadOmeroGateway(userName, password, host, varargin)
% 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)
% [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
% (Optional) 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
%
%
% NOTE
%
% Use gateway.close() to close the connection once you're done.
%
%
% 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.addOptional('port',[],@(x) isnumeric(x) && isscalar(x))
p.parse(userName, password, host, varargin{:});
port = p.Results.port;
if isempty(port)
cred = omero.gateway.LoginCredentials(userName, password, host);
else
cred = omero.gateway.LoginCredentials(userName, password, host, port);
end
simpleLogger = omero.log.SimpleLogger();
gateway = omero.gateway.Gateway(simpleLogger);
user = gateway.connect(cred);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment