Skip to content

Instantly share code, notes, and snippets.

@emilroz
Last active August 29, 2015 14:08
Show Gist options
  • Save emilroz/880213f603b3b144ee80 to your computer and use it in GitHub Desktop.
Save emilroz/880213f603b3b144ee80 to your computer and use it in GitHub Desktop.
Download Original Files belonging to the fileset (OMERO)
function downloadedFileList = ...
downloadOriginalFiles(session, imageId, folder, chunkSize)
% DOWNLOADORIGINALFILES downloaded original files assosiated with the Image.
%
% downloadedFileList = getFilePaths(session, image_id, folder, chunkSize)
% returns list of paths to downloaded files.
%
% author: Emil Rozbicki <emil@glencoesoftware.com
% Copyright (C) 2014 Glencoe Software, Inc.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice, this
% list of conditions and the following disclaimer.
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
% ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% Check input
ip = inputParser;
ip.addRequired('session');
ip.addRequired('imageId', @(x) isnumeric(x));
ip.addRequired('folder', @(x) ischar(x));
ip.addRequired('chunkSize', @(x) isnumeric(x));
ip.parse(session, imageId, folder, chunkSize);
queryService = session.getQueryService;
of_query = ['select f from Image i '...
'left outer join i.fileset as fs '...
'join fs.usedFiles as uf '...
'join uf.originalFile as f '...
'where i.id = ', num2str(imageId)];
originalFiles = queryService.findAllByQuery(of_query, []);
downloadedFileList = cell(originalFiles.size(), 1);
if originalFiles.size() > 0
for of = 0:originalFiles.size() - 1
oFile = originalFiles.get(of);
store = session.createRawFileStore();
store.setFileId(oFile.getId().getValue());
fid = fopen([folder, char(oFile.getName().getValue())], 'w');
downloadedFileList(of + 1) = {[folder, char(oFile.getName().getValue())]};
fprintf('Writing file %s\n', downloadedFileList{of + 1});
for i = 0:chunkSize:oFile.getSize().getValue()
if i + chunkSize > oFile.getSize().getValue()
fwrite(fid, store.read(i, oFile.getSize().getValue() - i), 'int8');
else
fwrite(fid, store.read(i, chunkSize), 'int8');
end
end
fclose(fid);
store.close();
end
else
fprintf('Image does not belong to fileset or Image does not exists');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment