Skip to content
Snippets Groups Projects
Commit 7de5fffc authored by Florian Vollrath's avatar Florian Vollrath
Browse files

cleaned mybfopenOnlyMetaData and added original authors

parent 3e05a8e5
Branches
No related tags found
No related merge requests found
function [metadatastruct] = mybfopenOnlyMetaData( fileName, varargin ) function [metadatastruct] = mybfopenOnlyMetaData( fileName)
% Extracts Meta Data from files
% 27.05.2013 (Version 1.0)
% Authors:
% Andre Zeug (zeug.andre@mh-hannover.de)
% Malte Butzlaff (Butzlaff.Malte@mh-hannover.de)
%
% This software is for non-commercial use only.
%% parsing imput parameters %% parsing imput parameters
p = inputParser; p = inputParser;
p.StructExpand = true; % if we allow parameter structure expanding p.StructExpand = true; % if we allow parameter structure expanding
%p.CaseSensitivity = false; % enables or disables case-sensitivity when matching entries in the argument list with argument names in the schema. Default, case-sensitive matching is disabled (false).
p.KeepUnmatched = true; % controls whether MATLAB throws an error (false) or not (true) when the function being called is passed an argument that has not been defined in the inputParser schema for this file. p.KeepUnmatched = true; % controls whether MATLAB throws an error (false) or not (true) when the function being called is passed an argument that has not been defined in the inputParser schema for this file.
% p.FunctionName = ['mybfopen v.' mybfopen_version ' (' mybfopen_date ')']; % stores a function name that is to be included in error messages that might be thrown in the process of validating input arguments to the function.
p.addParameter('verbose' , true , @(x)(islogical(x) || any(x==[0 1 2]))); p.addParameter('verbose' , true , @(x)(islogical(x) || any(x==[0 1 2])));
p.addParameter('gui' , true , @(x)(islogical(x) || any(x==[0 1 2 3]))); p.addParameter('gui' , true , @(x)(islogical(x) || any(x==[0 1 2 3])));
p.addParameter('timestamp' , false , @(x)(isnumeric(x))); p.addParameter('timestamp' , false , @(x)(isnumeric(x)));
%p.addParamValue('img_first' , false , @(x)(islogical(x) || (isscalar(x) && x>=0 && mod(x,1)==0)));
%p.addParamValue('img_last' , false , @(x)(islogical(x) || (isscalar(x) && x>=0 && mod(x,1)==0)));
p.parse(varargin{:});
par = p.Results; par = p.Results;
% setting clock
if par.verbose
if ~par.timestamp
par.timestamp = tic;
end
t1 = toc(par.timestamp); % starts timer for verbose
end
% fprintf('\n')
% if par.verbose; t1 = output(sprintf('mybfopen: Start mybfopen v. %s (%s)!',...
% mybfopen_version ,mybfopen_date), whos, toc(par.timestamp), t1); end
% add unmached structure elements % add unmached structure elements
s = fieldnames(p.Unmatched); s = fieldnames(p.Unmatched);
for n=1:length(s) for n=1:length(s)
...@@ -40,22 +30,20 @@ if (nargin == 0) ...@@ -40,22 +30,20 @@ if (nargin == 0)
else else
par.fname = fileName; %[pwd '\' fileName]; % require to replace since receive error when fileName contains full path par.fname = fileName; %[pwd '\' fileName]; % require to replace since receive error when fileName contains full path
end end
% if par.verbose; t1 = output(sprintf('mybfopen: read file ''%s''',fileName), whos, toc(par.timestamp), t1); end
jPath = javaclasspath('-all'); jPath = javaclasspath('-all');
isLociTools = cellfun(@(x) ~isempty(regexp(x, '.*bioformats_package.jar$', 'once')),jPath); isLociTools = cellfun(@(x) ~isempty(regexp(x, '.*bioformats_package.jar$', 'once')),jPath);
if ~any(isLociTools); if ~any(isLociTools)
locipath = which('bioformats_package.jar'); locipath = which('bioformats_package.jar');
if isempty(locipath) if isempty(locipath)
locipath = fullfile(fileparts(mfilename('fullpath')), 'bioformats_package.jar'); locipath = fullfile(fileparts(mfilename('fullpath')), 'bioformats_package.jar');
end end
assert(exist(locipath, 'file') == 2, 'Cannot automatically locate bioformats_package.jar'); assert(exist(locipath, 'file') == 2, 'Cannot automatically locate bioformats_package.jar');
% Add loci_tools to dynamic Java class path % Add loci_tools to dynamic Java class path
javaaddpath(locipath); javaaddpath(locipath);
% if par.verbose; t1 = output('mybfopen: added ''bioformats_package.jar'' to java path', whos, toc(par.timestamp), t1); end % if par.verbose; t1 = output('mybfopen: added ''bioformats_package.jar'' to java path', whos, toc(par.timestamp), t1); end
end end
% if par.verbose; t1 = output('mybfopen: generate loci class object', whos, toc(par.timestamp), t1); end
% Create a loci.formats.ReaderWrapper object % Create a loci.formats.ReaderWrapper object
r = javaObject('loci.formats.ChannelSeparator', ... r = javaObject('loci.formats.ChannelSeparator', ...
...@@ -64,7 +52,6 @@ r = javaObject('loci.formats.ChannelSeparator', ... ...@@ -64,7 +52,6 @@ r = javaObject('loci.formats.ChannelSeparator', ...
OMEXMLService = javaObject('loci.formats.services.OMEXMLServiceImpl'); OMEXMLService = javaObject('loci.formats.services.OMEXMLServiceImpl');
r.setMetadataStore(OMEXMLService.createOMEXMLMetadata()); r.setMetadataStore(OMEXMLService.createOMEXMLMetadata());
r.setId(par.fname); r.setId(par.fname);
% if par.verbose; t1 = output('mybfopen: file opened', whos, toc(par.timestamp), t1); end
% extract metadata table for this series % extract metadata table for this series
metadataOME = r.getMetadataStore(); metadataOME = r.getMetadataStore();
...@@ -75,147 +62,89 @@ iDZ = metadataOME.getPixelsSizeZ(0).getValue(); ...@@ -75,147 +62,89 @@ iDZ = metadataOME.getPixelsSizeZ(0).getValue();
iDT = metadataOME.getPixelsSizeT(0).getValue(); iDT = metadataOME.getPixelsSizeT(0).getValue();
imageDims = [iDY iDX iDZ iDC iDT]; imageDims = [iDY iDX iDZ iDC iDT];
numImages = r.getImageCount(); numImages = r.getImageCount();
% if par.verbose; t1 = output('mybfopen: main parameter read, start data reading', whos, toc(par.timestamp), t1); end
% Get pixel information (type, etc.)
pixelType = r.getPixelType();
bpp = javaMethod('getBytesPerPixel', 'loci.formats.FormatTools', pixelType);
fp = javaMethod('isFloatingPoint', 'loci.formats.FormatTools', pixelType);
sgn = javaMethod('isSigned', 'loci.formats.FormatTools', pixelType);
if sgn
stopHere
end
little = r.isLittleEndian();
% set data format according to bit depth
switch bpp
case 1; classstr = 'uint8';
case 2; classstr = 'uint16';
case 4; classstr = 'uint32';
case 8; classstr = 'uint64';
end
% allocating memory for data
% data = zeros(imageDims, classstr);
% calc bytes per image plane
% imb = bpp*iDX*iDY/1024/1024;
% generate progress bar
% if par.gui,
% h = waitbar(0,'Please wait...','Name','mybfopen: Wait for data reading!');
% iiw = 0; iio = 0; tw = toc(par.timestamp); tr1 = tw;
% end;
% data import loop
% for ii = 1:numImages
% % update waitbar (in steps of 1%)
% if par.gui && 100*ii/numImages>iiw,
% tr = toc(par.timestamp) - tw;
% tre = tr/ii*numImages;
% waitbar(ii/numImages,h,...
% {sprintf('Import frame: %d / %d (%0.0f%%)',ii,numImages,100*ii/numImages);...
% sprintf('data read: % 8.2f MB/s, Time: %02d:%02d / %02d:%02d min',imb*(ii-iio)/(tr-tr1),floor(tr/60),floor(60*(tr/60 - floor(tr/60))),floor(tre/60),floor(60*(tre/60 - floor(tre/60))))});
% iiw = iiw + 1; iio = ii; tr1 = tr;
% end;
% get image
% plane = r.openBytes(ii - 1, 0, 0, iDX, iDY);
% arr = javaMethod('makeDataArray', 'loci.common.DataTools', plane, bpp, fp, little);
% zct = r.getZCTCoords(ii - 1) + 1;
% % store image in matrix
% data(:,:,zct(1),zct(2),zct(3)) = reshape(typecast(arr, classstr),[iDX iDY])';
% end
% if par.gui, delete(h); drawnow; end;
% if par.verbose; t1 = output('mybfopen: data reading finished', whos, toc(par.timestamp), t1); end
%data = reshape(data, imageDims);
%% transfer output %% transfer output
nout = max(nargout,1)-1; tt = par.fname;
if 1>0, tte=regexpi(tt,'/|\');
tt = par.fname; ttp=regexpi(tt,'\.');
tte=regexpi(tt,'/|\'); if ~isempty(tte)
ttp=regexpi(tt,'\.'); if any(ttp>tte(end))
if ~isempty(tte) tt = tt(tte(end)+1:ttp(end)-1);
if any(ttp>tte(end))
tt = tt(tte(end)+1:ttp(end)-1);
else
tt = tt(tte(end)+1:end);
end
end
% if par.verbose; t1 = output('mybfopen: start list metadata reading', whos, toc(par.timestamp), t1); end
%% metadataList
metadataList = r.getGlobalMetadata();
metadataKeys = metadataList.keySet().iterator();
if metadataList.size()>0
% LA files contain no metadatalist
% but Andor iQ and Zeiss ZEN do
iic = 0;
for ii=1:metadataList.size()
iic = iic + 1;
try
key = metadataKeys.nextElement();
info.list{iic,1} = strrep(key,[tt ' '],'');
info.list{iic,2} = metadataList.get(key);
catch
info.list(iic,:)=[];
iic = iic - 1;
end
end
[~,ind] = sort(info.list(:,1));
info.list = info.list(ind,:);
% if par.verbose; t1 = output('mybfopen: metadata list sorted, generate structure', whos, toc(par.timestamp), t1); end
% generate structure out of list info
str = [];
for ii =1:size(info.list,1)
str = getStruckt(str, regexprep(regexprep(regexprep(info.list{ii,1},'\|',' '),'((?!\s)\W)',''),'\s*',' '), info.list{ii,2});
% (?!test)expr => Match expr and do not match test.
% \s: any whitespace character
% \W: Any character that is not alphabetic, numeric, or underscore. (equivalent to [^a-zA-Z_0-9])
end
info.liststr = str;
% if par.verbose; t1 = output('mybfopen: metadata list structure generate, read OME timestamp', whos, toc(par.timestamp), t1); end
else else
info.list{1,1} = 'no key'; tt = tt(tte(end)+1:end);
info.list{1,2} = 'no value';
info.liststr = 'Empty';
end end
%% metadataOME end
% read timestamp from OME data
Ts = zeros(numImages,1); %% metadataList
try metadataList = r.getGlobalMetadata();
for nn=1:numImages; metadataKeys = metadataList.keySet().iterator();
Ts(nn) = metadataOME.getPlaneDeltaT(0,nn-1).value(); %remove .value() when using older versions of bioformats if metadataList.size()>0
end % LA files contain no metadatalist
info.Ts = reshape(Ts,[imageDims(3:end)]); % but Andor iQ and Zeiss ZEN do
info.TimeStamp = squeeze(info.Ts(1,1,:));%-info.Ts(1,1,1)); % time in seconds iic = 0;
info.TStxt = 'TimeStamp for each frame'; for ii=1:metadataList.size()
catch iic = iic + 1;
if nn>1 try
switch nn-1 key = metadataKeys.nextElement();
case iDT info.list{iic,1} = strrep(key,[tt ' '],'');
info.Ts = Ts(1:nn-1); info.list{iic,2} = metadataList.get(key);
info.TimeStamp = squeeze(info.Ts); catch
info.TStxt = 'TimeStamp for each timepoint'; info.list(iic,:)=[];
case iDT*iDZ iic = iic - 1;
info.Ts = reshape(Ts(1:nn-1),[iDZ iDT]);
info.TimeStamp = squeeze(info.Ts(1,:));%-info.Ts(1,1,1)); % time in seconds
info.TStxt = 'TimeStamp for each z-section and timepoint';
case iDT*iDZ*iDC
info.Ts = reshape(Ts(1:nn-1),[iDC iDZ iDT]);
info.TimeStamp = squeeze(info.Ts(1,1,:));%-info.Ts(1,1,1)); % time in seconds
info.TStxt = 'TimeStamp for each channel, z-section and timepoint';
end
else
info.Ts = 0;
info.TimeStamp = 0;
info.TStxt = 'No TimeStamp found.';
warning('File contains no time information')
end end
end end
info.OME = metadataOME; [~,ind] = sort(info.list(:,1));
varargout(1) = {info}; info.list = info.list(ind,:);
metadatastruct=info % generate structure out of list info
% if par.verbose; t1 = output('mybfopen: 2nd output transfered', whos, toc(par.timestamp), t1); end str = [];
for ii =1:size(info.list,1)
str = getStruckt(str, regexprep(regexprep(regexprep(info.list{ii,1},'\|',' '),'((?!\s)\W)',''),'\s*',' '), info.list{ii,2});
% (?!test)expr => Match expr and do not match test.
% \s: any whitespace character
% \W: Any character that is not alphabetic, numeric, or underscore. (equivalent to [^a-zA-Z_0-9])
end
info.liststr = str;
else
info.list{1,1} = 'no key';
info.list{1,2} = 'no value';
info.liststr = 'Empty';
end end
if nout>1, %% metadataOME
% parameter list output % read timestamp from OME data
varargout(2) = {par}; Ts = zeros(numImages,1);
% if par.verbose; t1 = output('mybfopen: 3rd output transfered', whos, toc(par.timestamp), t1); end try
for nn=1:numImages
Ts(nn) = metadataOME.getPlaneDeltaT(0,nn-1).value(); %remove .value() when using older versions of bioformats
end
info.Ts = reshape(Ts,[imageDims(3:end)]);
info.TimeStamp = squeeze(info.Ts(1,1,:));%-info.Ts(1,1,1)); % time in seconds
info.TStxt = 'TimeStamp for each frame';
catch
if nn>1
switch nn-1
case iDT
info.Ts = Ts(1:nn-1);
info.TimeStamp = squeeze(info.Ts);
info.TStxt = 'TimeStamp for each timepoint';
case iDT*iDZ
info.Ts = reshape(Ts(1:nn-1),[iDZ iDT]);
info.TimeStamp = squeeze(info.Ts(1,:));%-info.Ts(1,1,1)); % time in seconds
info.TStxt = 'TimeStamp for each z-section and timepoint';
case iDT*iDZ*iDC
info.Ts = reshape(Ts(1:nn-1),[iDC iDZ iDT]);
info.TimeStamp = squeeze(info.Ts(1,1,:));%-info.Ts(1,1,1)); % time in seconds
info.TStxt = 'TimeStamp for each channel, z-section and timepoint';
end
else
info.Ts = 0;
info.TimeStamp = 0;
info.TStxt = 'No TimeStamp found.';
warning('File contains no time information')
end
end end
info.OME = metadataOME;
metadatastruct=info;
% close file % close file
r.close(); r.close();
% inline function for structure generation from metadatalist % inline function for structure generation from metadatalist
...@@ -236,7 +165,6 @@ r.close(); ...@@ -236,7 +165,6 @@ r.close();
end end
else else
if regexp(tt(1:tt_pos(1)-1),'^\d*$') % checks if tt is numerical if regexp(tt(1:tt_pos(1)-1),'^\d*$') % checks if tt is numerical
%str.(['no' tt(1:tt_pos(1)-1)]) = getStruckt(str, tt(tt_pos(1)+1:end), content);
ttn = ['no' tt(1:tt_pos(1)-1)]; ttn = ['no' tt(1:tt_pos(1)-1)];
else else
ttn = tt(1:tt_pos(1)-1); ttn = tt(1:tt_pos(1)-1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment