Skip to content
Snippets Groups Projects
Select Git revision
  • master
1 result

ConvertToKLB.m

Blame
  • ConvertToKLB.m 5.82 KiB
    function ConvertToKLB(dirIn,dirOut, subfolders, forceDatasetName)
        settingsList = dir(fullfile(dirIn,'*_Settings.txt'));
        firstMetaSettings = LLSM.ParseSettingsFile(fullfile(dirIn,settingsList(1).name));
        
        if ( ~exist('forceDatasetName','var') )
            forceDatasetName = '';
        end
    
        %% Cleanup function to try and avoid partial file-writes
        fileMap = containers.Map();
        if ( ~isempty(dirOut) )
            cleanupObj = onCleanup(@()(cleanupFunc(fileMap)));
        end
        
        for s = 1:length(subfolders)
            fullOutDir = fullfile(dirOut,subfolders{s});
            if (~exist(fullfile(dirIn, subfolders{s}),'dir'))
                warning('Cannot find %s.\nSkipping\n',fullfile(dirIn,subfolders{s}));
                continue
            end
            
            fprintf('Converting %s\n', fullfile(dirIn,subfolders{s}));
            if ( ~exist(fullOutDir, 'dir') )
                mkdir(fullOutDir);
            end
            
            flist = dir(fullfile(dirIn,subfolders{s},'*.tif'));
            parseStruct = LLSM.ParseFileNames(flist,'*.tif');
            
            imDT = MicroscopeData.ReadMetadataFile([fullOutDir filesep]);
            if ( ~isempty(imDT) )
                fprintf('Found metadata file for %s... Skipping\n', fullOutDir);
                continue;
            end
            
            %% TODO: Make this a little more generalized
            chanMap = unique(vertcat(parseStruct.chan));
            camMap = unique({parseStruct.cam}.');
            
            frames = unique(vertcat(parseStruct.stack));
            if ( length(frames) == 1 && ~isempty(vertcat(parseStruct.iter)) )
                frames = unique(vertcat(parseStruct.iter));
            end
            
            tiffName = LLSM.MatchFilename(parseStruct, camMap(1), frames(1), chanMap(1));
            chkIm = LLSM.loadtiff(fullfile(dirIn,subfolders{s},[tiffName{1} '.tif']));
            
            imSize = size(chkIm);
            
            datasetName = parseStruct(1).datasetName;
            if ( ~isempty(forceDatasetName) )
                datasetName = forceDatasetName;
            end
            
            %%
            imD = MicroscopeData.GetEmptyMetadata();
            imD.DatasetName = datasetName;
            
            imD.Dimensions = imSize([2,1,3]);
            imD.NumberOfChannels = length(camMap);
            imD.NumberOfFrames = length(frames);
            imD.PixelFormat = class(chkIm);
            
            imD.ChannelColors = [1,1,1; 0,1,0];
            imD.ChannelNames = {'FM 4-64','Calcein'};
            imD.PixelPhysicalSize = [0.104, 0.104, firstMetaSettings.zOffset];
            imD.StartCaptureDate = firstMetaSettings.startCaptureDate;
            
            %% Some KLB Helpers for getting block sizing
            bytes = MicroscopeData.Helper.GetBytesPerVoxel(chkIm);
            
            blockSize_xyzct = [64, 64, imSize(3), 1, 1];
            blockMem = prod(blockSize_xyzct)*bytes;
            blockSize_xyzct(5) = max(1,floor((1024^2) / blockMem)); % try to get close to 1MB block size
            
            myCluster = parcluster('local');
            threads = myCluster.NumWorkers;
            
            %%
    %         poolobj = gcp('nocreate');
    %         if isempty(poolobj)
    %             parpool(2);
    %         end
            
            prg = Utils.CmdlnProgress(length(frames), true, sprintf('Reading %s', imD.DatasetName));
            for t=1:size(frames,1)
                frameIm = zeros([imSize, length(camMap)]);
                
                % Try to skip completed output files
                fileList = getKLBFiles(imD.DatasetName,fullOutDir,1:length(camMap),[t,t],true,false);
                bFilesExist = cellfun(@(x)(exist(x,'file') > 0), fileList);
                if ( all(bFilesExist) )
                    continue;
                end
                
                bReadErr = false;
                for c=1:length(camMap)
                    tiffName = LLSM.MatchFilename(parseStruct, camMap(c), frames(t), chanMap(1));
                    try
                        frameIm(:,:,:,c) = LLSM.loadtiff(fullfile(dirIn,subfolders{s},[tiffName{1} '.tif']));
                    catch me
                        bReadErr = true;
                        warning(['ERROR reading ''' tiffName{1} '.tif' ''' - skipping: ' me.message]);
                        break;
                    end
                end
                if ( bReadErr )
                    % Skip frame-write if there was an error reading one of the tiffs
                    continue;
                end
                
                fileNameT = sprintf('%s_t%04d.klb', fullfile(fullOutDir,imD.DatasetName), t);
                
                wrtFunc = @()(MicroscopeData.KLB.writeKLBstack(frameIm, fileNameT, threads, [0.104 0.104, firstMetaSettings.zOffset, 1,1], blockSize_xyzct));
                safeWrite(fileMap, fileList, wrtFunc);
                
                prg.PrintProgress(t);
            end
            
            MicroscopeData.CreateMetadata(fullOutDir, imD);
        end
    end
    
    function fileList = getKLBFiles(datasetName,outDir,chanList,timeRange,filePerT,filePerC)
        fileList = {};
        
        fileName = fullfile(outDir,datasetName);
        if (filePerT)
            if (filePerC)
                for t=1:length(timeRange(1):timeRange(2))
                    for c=1:length(chanList)
                        fileList = [fileList; {sprintf('%s_c%d_t%04d.klb',fileName,chanList(c),(t-1)+timeRange(1))}];
                    end
                end
            else
                for t=1:length(timeRange(1):timeRange(2))
                    fileList = [fileList; {sprintf('%s_t%04d.klb',fileName,(t-1)+timeRange(1))}];
                end
            end
        elseif (filePerC)
            for c=1:length(chanList)
                fileList = [fileList; {sprintf('%s_c%d.klb',fileName,chanList(c))}];
            end
        else
            fileList = {[fileName,'.klb']};
        end
    end
    
    
    function safeWrite(fileMap, fileList, writeFunc)
        for i=1:length(fileList)
            fileMap(fileList{i}) = false;
        end
        
        writeFunc();
        
        for i=1:length(fileList)
            remove(fileMap,fileList{i});
        end
    end
    
    function cleanupFunc(fileMap)
        k = keys(fileMap);
        
        for i=1:length(k)
            if ( exist(k{i}, 'file') )
                delete(k{i});
            end
        end
    end