Commit 0590becb authored by M. Julius Hossain's avatar M. Julius Hossain

First commit

parents
Pipeline #7745 failed with stages
in 42 seconds
This diff is collapsed.
function [] = bfCheckJavaMemory(varargin)
% bfCheckJavaMemory warn if too little memory is allocated to Java
%
% SYNOPSIS bfCheckJavaMemory()
%
% Input
%
% minMemory - (Optional) The minimum suggested memory setting in MB.
% Default: 512
%
% Output
%
% A warning message is printed if too little memory is allocated.
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2014 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
runtime = javaMethod('getRuntime', 'java.lang.Runtime');
maxMemory = runtime.maxMemory() / (1024 * 1024);
ip = inputParser;
ip.addOptional('minMemory', 512, @isscalar);
ip.parse(varargin{:});
minMemory = ip.Results.minMemory;
warningID = 'BF:lowJavaMemory';
if maxMemory < minMemory - 64
warning_msg = [...
'*** Insufficient memory detected. ***\n'...
'*** %dm found ***\n'...
'*** %dm or greater is recommended ***\n'...
'*** See http://www.mathworks.com/matlabcentral/answers/92813 ***\n'...
'*** for instructions on increasing memory allocation. ***\n'];
warning(warningID, warning_msg, round(maxMemory), minMemory);
end
function [status, version] = bfCheckJavaPath(varargin)
% bfCheckJavaPath check Bio-Formats is included in the Java class path
%
% SYNOPSIS bfCheckJavaPath()
% status = bfCheckJavaPath(autoloadBioFormats)
% [status, version] = bfCheckJavaPath()
%
% Input
%
% autoloadBioFormats - Optional. A boolean specifying the action
% to take if Bio-Formats is not in the javaclasspath. If true,
% tries to find a Bio-Formats jar file and adds it to the java
% class path.
% Default - true
%
% Output
%
% status - Boolean. True if the Bio-Formats classes are in the Java
% class path.
%
%
% version - String specifying the current version of Bio-Formats if
% Bio-Formats is in the Java class path. Empty string otherwise.
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2012 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Input check
ip = inputParser;
ip.addOptional('autoloadBioFormats', true, @isscalar);
ip.parse(varargin{:});
[status, version] = has_working_bioformats();
if ~status && ip.Results.autoloadBioFormats,
jarPath = fullfile(fileparts(mfilename('fullpath')), ...
'bioformats_package.jar');
if (exist(jarPath) == 2)
javaaddpath(jarPath);
[status, version] = has_working_bioformats();
if (~status)
javarmpath(jarPath);
end
end
end
% Return true if bioformats java interface is working, false otherwise.
% Not working will probably mean that the classes are not in
% the javaclasspath.
function [status, version] = has_working_bioformats()
status = true;
version = '';
try
% If the following fails for any reason, then bioformats is not working.
% Getting the version number and creating a reader is the bare minimum.
reader = javaObject('loci.formats.in.FakeReader');
if is_octave()
version = char(java_get('loci.formats.FormatTools', 'VERSION'));
else
version = char(loci.formats.FormatTools.VERSION);
end
catch
status = false;
end
function fileExt = bfGetFileExtensions
% bfGetFileExtensions list all extensions supported by Bio-Formats
%
% Synopsis: fileExt = bfGetExtensions()
%
% Input
% none
%
% Output
% fileExt: a cell array of dimensions n x2 where the first colum
% gives the extension and the second the name of the corresponding
% format.
% This cell array is formatted to be used with uigetfile funciton.
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2012 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Get all readers and create cell array with suffixes and names
readers = javaMethod('getReaders', javaObject('loci.formats.ImageReader'));
fileExt = cell(numel(readers), 2);
for i = 1:numel(readers)
suffixes = readers(i).getSuffixes();
if is_octave()
%% FIXME when https://savannah.gnu.org/bugs/?42700 gets fixed
ExtSuf = cell(numel(suffixes), 1);
for j = 1:numel(suffixes)
ExtSuf{j} = char(suffixes(j));
end
fileExt{i, 1} = ExtSuf;
else
fileExt{i, 1} = arrayfun(@char, suffixes, 'Unif', false);
end
fileExt{i, 2} = char(readers(i).getFormat());
end
% Concatenate all unique formats
allExt = unique(vertcat(fileExt{:, 1}));
allExt = allExt(~cellfun(@isempty, allExt));
fileExt = vertcat({allExt, 'All formats'}, fileExt);
% Format file extensions
for i = 1:size(fileExt, 1)
fileExt{i, 1} = sprintf('*.%s;', fileExt{i, 1}{:});
fileExt{i, 1}(end) = [];
end
function I = bfGetPlane(r, varargin)
% BFGETPLANE Retrieve the plane data from a reader using Bio-Formats
%
% I = bfGetPlane(r, iPlane) returns a specified plane from the input
% format reader. The index specifying the plane to retrieve should be
% contained between 1 and the number of planes for the series. Given a
% set of (z, c, t) plane coordinates, the plane index (0-based) can be
% retrieved using r.getIndex(z, c, t).
%
% I = bfGetPlane(r, iPlane, x, y, width, height) only returns the tile
% which origin is specified by (x, y) and dimensions are specified by
% (width, height).
%
% Examples
%
% I = bfGetPlane(r, 1) % First plane of the series
% I = bfGetPlane(r, r.getImageCount()) % Last plane of the series
% I = bfGetPlane(r, r.getIndex(0, 0, 0) + 1) % First plane of the series
% I = bfGetPlane(r, 1, 1, 1, 20, 20) % 20x20 tile originated at (0, 0)
%
% See also: BFGETREADER
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2012 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Input check
ip = inputParser;
isValidReader = @(x) isa(x, 'loci.formats.IFormatReader') && ...
~isempty(x.getCurrentFile());
ip.addRequired('r', isValidReader);
ip.parse(r);
% Plane check
isValidPlane = @(x) isscalar(x) && ismember(x, 1 : r.getImageCount());
% Optional tile arguments check
isValidX = @(x) isscalar(x) && ismember(x, 1 : r.getSizeX());
isValidY = @(x) isscalar(x) && ismember(x, 1 : r.getSizeY());
ip.addRequired('iPlane', isValidPlane);
ip.addOptional('x', 1, isValidX);
ip.addOptional('y', 1, isValidY);
ip.addOptional('width', r.getSizeX(), isValidX);
ip.addOptional('height', r.getSizeY(), isValidY);
ip.parse(r, varargin{:});
% Additional check for tile size
assert(ip.Results.x - 1 + ip.Results.width <= r.getSizeX(),...
'MATLAB:InputParser:ArgumentFailedValidation',...
'Invalid tile size');
assert(ip.Results.y - 1 + ip.Results.height <= r.getSizeY(),...
'MATLAB:InputParser:ArgumentFailedValidation',...
'Invalid tile size');
% Get pixel type
pixelType = r.getPixelType();
bpp = javaMethod('getBytesPerPixel', 'loci.formats.FormatTools', pixelType);
fp = javaMethod('isFloatingPoint', 'loci.formats.FormatTools', pixelType);
sgn = javaMethod('isSigned', 'loci.formats.FormatTools', pixelType);
little = r.isLittleEndian();
plane = r.openBytes(...
ip.Results.iPlane - 1, ip.Results.x - 1, ip.Results.y - 1, ...
ip.Results.width, ip.Results.height);
% convert byte array to MATLAB image
if sgn
% can get the data directly to a matrix
I = javaMethod('makeDataArray2D', 'loci.common.DataTools', plane, ...
bpp, fp, little, ip.Results.height);
else
% get the data as a vector, either because makeDataArray2D
% is not available, or we need a vector for typecast
I = javaMethod('makeDataArray', 'loci.common.DataTools', plane, ...
bpp, fp, little);
end
% Java does not have explicitly unsigned data types;
% hence, we must inform MATLAB when the data is unsigned
if ~sgn
% NB: arr will always be a vector here
switch class(I)
case 'int8'
I = typecast(I, 'uint8');
case 'int16'
I = typecast(I, 'uint16');
case 'int32'
I = typecast(I, 'uint32');
case 'int64'
I = typecast(I, 'uint64');
end
end
if isvector(I)
% convert results from vector to matrix
shape = [ip.Results.width ip.Results.height];
I = reshape(I, shape)';
end
function r = bfGetReader(varargin)
% BFGETREADER return a reader for a microscopy image using Bio-Formats
%
% r = bfGetReader() creates an empty Bio-Formats reader extending
% loci.formats.ReaderWrapper.
%
% r = bfGetReader(id) where id is a path to an existing file creates and
% initializes a reader for the input file.
%
% Examples
%
% r = bfGetReader()
% I = bfGetReader(path_to_file)
%
%
% See also: BFGETPLANE
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2012 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Input check
ip = inputParser;
ip.addOptional('id', '', @ischar);
ip.addOptional('stitchFiles', false, @isscalar);
ip.parse(varargin{:});
id = ip.Results.id;
% verify that enough memory is allocated
bfCheckJavaMemory();
% load the Bio-Formats library into the MATLAB environment
status = bfCheckJavaPath();
assert(status, ['Missing Bio-Formats library. Either add bioformats_package.jar '...
'to the static Java path or add it to the Matlab path.']);
% Check if input is a fake string
isFake = strcmp(id(max(1, end - 4):end), '.fake');
if ~isempty(id) && ~isFake
% Check file existence using fileattrib
[status, f] = fileattrib(id);
assert(status && f.directory == 0, 'bfGetReader:FileNotFound',...
'No such file: %s', id);
id = f.Name;
end
% set LuraWave license code, if available
if exist('lurawaveLicense', 'var')
path = fullfile(fileparts(mfilename('fullpath')), 'lwf_jsdk2.6.jar');
javaaddpath(path);
javaMethod('setProperty', 'java.lang.System', ...
'lurawave.license', lurawaveLicense);
end
% Create a loci.formats.ReaderWrapper object
r = javaObject('loci.formats.ChannelSeparator', ...
javaObject('loci.formats.ChannelFiller'));
if ip.Results.stitchFiles
r = javaObject('loci.formats.FileStitcher', r);
end
% Initialize the metadata store
OMEXMLService = javaObject('loci.formats.services.OMEXMLServiceImpl');
r.setMetadataStore(OMEXMLService.createOMEXMLMetadata());
% Initialize the reader
if ~isempty(id), r.setId(id); end
function bfInitLogging(varargin)
% BFINITLOGGING initializes the Bio-Formats logging system
%
% bfInitLogging() initializes the logging system at WARN level by default.
%
% bfInitLogging(level) sets the logging level to use when initializing
% the logging system
%
% Examples
%
% bfInitLogging();
% bfInitLogging('DEBUG');
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Check Bio-Formats is set in the Java class path
bfCheckJavaPath();
% Input check
levels = {'ALL', 'DEBUG', 'ERROR', 'FATAL', 'INFO', 'OFF', 'TRACE', 'WARN'};
ip = inputParser;
ip.addOptional('level', 'WARN', @(x) ismember(x, levels));
ip.parse(varargin{:});
% Set logging level
javaMethod('enableLogging', 'loci.common.DebugTools', ip.Results.level);
function volume = bfOpen3DVolume(filename)
% bfOpen3DVolume loads a stack of images using Bio-Formats and transforms them
% into a 3D volume
%
% SYNPOSIS bfOpen3DVolume
% V = bfOpen3DVolume(filename)
%
% Input
%
% filename - Optional. A path to the file to be opened. If not specified,
% then a file chooser window will appear.
%
% Output
%
% volume - 3D array containing all images in the file.
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2012 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% load the Bio-Formats library into the MATLAB environment
status = bfCheckJavaPath();
assert(status, ['Missing Bio-Formats library. Either add bioformats_package.jar '...
'to the static Java path or add it to the Matlab path.']);
% Prompt for a file if not input
if nargin == 0 || exist(filename, 'file') == 0
[file, path] = uigetfile(bfGetFileExtensions, 'Choose a file to open');
filename = [path file];
if isequal(path, 0) || isequal(file, 0), return; end
end
volume = bfopen(filename);
vaux{1} = cat(3, volume{1}{:, 1});
vaux{2} = filename;
volume{1} = vaux;
end
function bfUpgradeCheck(varargin)
% Check for new version of Bio-Formats and update it if applicable
%
% SYNOPSIS: bfUpgradeCheck(autoDownload, 'STABLE')
%
% Input
% autoDownload - Optional. A boolean specifying of the latest version
% should be downloaded
%
% versions - Optional: a string sepecifying the version to fetch.
% Should be either trunk, daily or stable (case insensitive)
%
% Output
% none
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2012 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as
% published by the Free Software Foundation, either version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
% Check input
ip = inputParser;
ip.addOptional('autoDownload', false, @isscalar);
versions = {'stable', 'daily', 'trunk'};
ip.addOptional('version', 'STABLE', @(x) any(strcmpi(x, versions)))
ip.parse(varargin{:})
% Create UpgradeChecker
upgrader = javaObject('loci.formats.UpgradeChecker');
if upgrader.alreadyChecked(), return; end
% Check for new version of Bio-Formats
if is_octave()
caller = 'Octave';
else
caller = 'MATLAB';
end
if ~ upgrader.newVersionAvailable(caller)
fprintf('*** bioformats_package.jar is up-to-date ***\n');
return;
end
fprintf('*** A new stable version of Bio-Formats is available ***\n');
% If appliable, download new version of Bioformats
if ip.Results.autoDownload
fprintf('*** Downloading... ***');
path = fullfile(fileparts(mfilename('fullpath')), 'bioformats_package.jar');
buildName = [upper(ip.Results.version) '_BUILD'];
upgrader.install(loci.formats.UpgradeChecker.(buildName), path);
fprintf('*** Upgrade will be finished when MATLAB is restarted ***\n');
end
function [result] = bfopen(id, varargin)
% Open microscopy images using Bio-Formats.
%
% SYNOPSIS r = bfopen(id)
% r = bfopen(id, x, y, w, h)
%
% Input
% r - the reader object (e.g. the output bfGetReader)
%
% x - (Optional) A scalar giving the x-origin of the tile.
% Default: 1
%
% y - (Optional) A scalar giving the y-origin of the tile.
% Default: 1
%
% w - (Optional) A scalar giving the width of the tile.
% Set to the width of the plane by default.
%
% h - (Optional) A scalar giving the height of the tile.
% Set to the height of the plane by default.
%
% Output
%
% result - a cell array of cell arrays of (matrix, label) pairs,
% with each matrix representing a single image plane, and each inner
% list of matrices representing an image series.
%
% Portions of this code were adapted from:
% http://www.mathworks.com/support/solutions/en/data/1-2WPAYR/
%
% This method is ~1.5x-2.5x slower than Bio-Formats's command line
% showinf tool (MATLAB 7.0.4.365 R14 SP2 vs. java 1.6.0_20),
% due to overhead from copying arrays.
%
% Thanks to all who offered suggestions and improvements:
% * Ville Rantanen
% * Brett Shoelson
% * Martin Offterdinger
% * Tony Collins
% * Cris Luengo
% * Arnon Lieber
% * Jimmy Fong
%
% NB: Internet Explorer sometimes erroneously renames the Bio-Formats library
% to bioformats_package.zip. If this happens, rename it back to
% bioformats_package.jar.
%
% For many examples of how to use the bfopen function, please see:
% http://www.openmicroscopy.org/site/support/bio-formats5.1/developers/matlab-dev.html
% OME Bio-Formats package for reading and converting biological file formats.
%
% Copyright (C) 2007 - 2016 Open Microscopy Environment:
% - Board of Regents of the University of Wisconsin-Madison
% - Glencoe Software, Inc.
% - University of Dundee