Home > SuperSegger > viz > makeConsensusArray.m

makeConsensusArray

PURPOSE ^

makeConsensusArray : Computes consensus dataImArray array used to make

SYNOPSIS ^

function [dataImArray] = makeConsensusArray( cellDir, CONST, skip, mag, fnum, clist )

DESCRIPTION ^

 makeConsensusArray : Computes consensus dataImArray array used to make 
 consensus images and kymographs (average fluorescence localization).

 INPUT:
   cellDir : (string) directory to load cell files from
     CONST : (struct) Constants to use in calculation
      skip : (integer) Use every skip images to make cons image
       mag : (double) resize images by factor mag to generate cons image
 disp_flag : (flag) flag to show cells while they are processed

 OUPUT:
   dataImArray =
           towerCArray: cell array of tower of each single cell
            towerArray: cell array of tower of each single cell
        towerNormArray: cell array of tower of each single cell
     intWeightMinArray: weight of each cell
          cellArrayNum: number of each cell
                imCell: {1x8 cell}
            imCellNorm: {1x8 cell}
              maskCell: {1x8 cell}
           imCellScale: {1x8 cell}
       imCellNormScale: {1x8 cell}
         maskCellScale: {1x8 cell}
             sumWeight: [1x8 double]
          sumWeightMin: 368.8602
            sumWeightS: [1x8 double]
         sumWeightSMin: 0
              numCells: 5
                 ssTot: [40 1060]
             imCellSum: {1x8 cell}

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [dataImArray] = makeConsensusArray( cellDir, CONST, skip, mag, fnum, clist )
0002 % makeConsensusArray : Computes consensus dataImArray array used to make
0003 % consensus images and kymographs (average fluorescence localization).
0004 %
0005 % INPUT:
0006 %   cellDir : (string) directory to load cell files from
0007 %     CONST : (struct) Constants to use in calculation
0008 %      skip : (integer) Use every skip images to make cons image
0009 %       mag : (double) resize images by factor mag to generate cons image
0010 % disp_flag : (flag) flag to show cells while they are processed
0011 %
0012 % OUPUT:
0013 %   dataImArray =
0014 %           towerCArray: cell array of tower of each single cell
0015 %            towerArray: cell array of tower of each single cell
0016 %        towerNormArray: cell array of tower of each single cell
0017 %     intWeightMinArray: weight of each cell
0018 %          cellArrayNum: number of each cell
0019 %                imCell: {1x8 cell}
0020 %            imCellNorm: {1x8 cell}
0021 %              maskCell: {1x8 cell}
0022 %           imCellScale: {1x8 cell}
0023 %       imCellNormScale: {1x8 cell}
0024 %         maskCellScale: {1x8 cell}
0025 %             sumWeight: [1x8 double]
0026 %          sumWeightMin: 368.8602
0027 %            sumWeightS: [1x8 double]
0028 %         sumWeightSMin: 0
0029 %              numCells: 5
0030 %                 ssTot: [40 1060]
0031 %             imCellSum: {1x8 cell}
0032 
0033 %  imMosaic : Image mosaic of cells that make up the consensus image
0034 %   imColor : Color cons image (w/ black background)
0035 %      imBW : Grayscale cons image
0036 %     imInv : Color cons image (w/ white background)
0037 %      kymo : Consensus Kymograph
0038 %  kymoMask : Consensus Kymograph Cell mask
0039 %         I : Fit of intensities to a model for polar localization
0040 %
0041 % Copyright (C) 2016 Wiggins Lab
0042 % Written by Stella Stylianidou, Paul Wiggins.
0043 % University of Washington, 2016
0044 % This file is part of SuperSegger.
0045 %
0046 % SuperSegger is free software: you can redistribute it and/or modify
0047 % it under the terms of the GNU General Public License as published by
0048 % the Free Software Foundation, either version 3 of the License, or
0049 % (at your option) any later version.
0050 %
0051 % SuperSegger is distributed in the hope that it will be useful,
0052 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0053 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0054 % GNU General Public License for more details.
0055 %
0056 % You should have received a copy of the GNU General Public License
0057 % along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.
0058 
0059 
0060 
0061 
0062 dataImArray = [];
0063 
0064 if ~exist( 'skip', 'var' ) || isempty( skip )
0065     skip = 1;
0066 end
0067 
0068 % magnification : resizes the images of the cells to make them
0069 % larger the image will be mag X larger in each dimension
0070 
0071 if ~exist( 'mag', 'var' ) || isempty( mag )
0072     mag = 4;
0073 end
0074 
0075 if ~exist( 'fnum', 'var' ) || isempty( fnum )
0076     fnum = 1;
0077 end
0078 
0079 if exist( 'clist', 'var' ) && ~isempty( clist )
0080     clist = gate( clist );
0081 else
0082     clist = [];
0083 end
0084 
0085 
0086 
0087 % show images in false color
0088 if ~isfield(CONST.view, 'falseColorFlag' )
0089     CONST.view.falseColorFlag = false;
0090 end
0091 
0092 % Get the number of cells in the cell directory
0093 cellNames = getCellFiles (cellDir,CONST);
0094 
0095 numCells = numel(cellNames);
0096 
0097 if numCells == 0
0098     disp ('No cells found')
0099     return
0100 end
0101 
0102 % put in a max cell number to stop the code stalling
0103 if ~isfield(CONST.view, 'maxNumCell' ) || isempty(CONST.view.maxNumCell)
0104     CONST.view.maxNumCell = 100;
0105 end
0106 
0107 
0108 CONST.view.maxNumCell = min (CONST.view.maxNumCell ,numCells);
0109 
0110 
0111 if ~isempty( CONST.view.maxNumCell )
0112     numCells = min([numCells, CONST.view.maxNumCell] );
0113 end
0114 
0115 disp( ['Computing consensus array (max cell number ',...
0116     num2str(numCells),')'] );
0117 
0118 % ssTot : Keep track of the total size of the tower mosaic.
0119 
0120 
0121 % Manage the waitbar
0122 h = waitbar(0, 'Computation' );
0123 cleanup = onCleanup( @()( delete( h ) ) );
0124 
0125 % initialize the sum variables
0126 dataImArray = intInit( numCells );
0127 minimumLifetime = 4;
0128 imArray = cell(1,numCells);
0129 kk = 0;
0130 
0131 firstCell = load([cellDir,cellNames{1}]);
0132 if ~isfield(firstCell.CellA{1},'fluor1')
0133     disp ('no fluorescence found - exiting')
0134     return;
0135 end
0136 
0137 % loop through the cells to make the consensus image and the mosaic
0138 for ii = 1:numCells
0139     
0140     % update status bar
0141     waitbar(ii/numCells,h);
0142     
0143     % load data and get file number
0144     cell_name = cellNames{ii};
0145     [data, dataImArray.cellArrayNum{ii}] = intLoader(cellDir, cell_name);
0146     data.CellA = data.CellA(1:skip:end);
0147     cur_cell_num = str2double(cell_name(isnum(cell_name)));
0148     aboveMinLifetime = numel(data.CellA) > minimumLifetime;
0149     inClist = isempty( clist ) || ismember( cur_cell_num, row(clist.data(:,1)));
0150     
0151     
0152     % Compute consensus images for cell in data
0153     if aboveMinLifetime && inClist
0154         kk = kk + 1;
0155         dataIm = makeTowerCons(data,CONST,1,false, skip, mag, fnum );
0156         
0157         % Scale images down to the original size
0158         imArray{kk} = imresize( dataIm.tower, 1/mag);
0159         
0160         % update the sums
0161         dataImArray = intUpdate( dataImArray, dataIm, kk );
0162     end
0163 end
0164 
0165 numCells = kk;
0166 imArray = imArray(1:numCells);
0167 
0168 % close the status bar
0169 close(h);
0170 
0171 % normalize sums to convert sums to means.
0172 dataImArray = intNormalize(dataImArray, numCells );
0173 
0174 end
0175 
0176 function dataArray = intUpdate( dataArray, data, jj )
0177 % intUpdate : adds a new cell to the dataArray
0178 
0179 T0 = numel( data.imCell );
0180 
0181 if isempty( dataArray.sumWeight );
0182     dataArray.sumWeight    = zeros(1,T0);
0183     dataArray.sumWeightMin = 0;
0184 end
0185 
0186 
0187 % update im and mask sum
0188 if isempty(dataArray.tower)
0189     dataArray.tower        = double( data.towerRaw );
0190     dSumWeightMin          = min(data.intWeight);
0191     dataArray.sumWeightMin = dSumWeightMin;
0192     dataArray.towerNormW    = dataArray.sumWeightMin *...
0193         double( data.towerNormRaw );
0194     dataArray.towerNorm    = double( data.towerNormRaw );
0195     dataArray.towerMask    = double( data.towerMask );
0196 else
0197     dSumWeightMin = min(data.intWeight);
0198     dataArray.tower = dataArray.tower + double( data.towerRaw );
0199     dataArray.towerNormW = dataArray.towerNorm + ...
0200         dSumWeightMin * double( data.towerNormRaw );
0201     dataArray.towerNorm    = dataArray.towerNorm + ...
0202         double( data.towerNormRaw );
0203     dataArray.towerMask    = dataArray.towerMask + double( data.towerMask );
0204     dataArray.sumWeightMin = dataArray.sumWeightMin + dSumWeightMin;
0205 end
0206 
0207 ss = size(data.imCell{1});
0208 
0209 dataArray.ssTot = [ max([dataArray.ssTot(1),ss(1)]),...
0210     dataArray.ssTot(2)+ss(2) ];
0211 
0212 if isempty( dataArray.imCell )
0213     dataArray.imCell = data.imCell;
0214     dataArray.imCellNorm = data.imCellNorm;
0215     dataArray.maskCell = data.maskCell;
0216     dataArray.sumWeight = data.intWeight;
0217     for ii = 1:T0
0218         dataArray.imCellNorm{ii}  = data.imCellNorm{ii};
0219         dataArray.imCellNormW{ii} = data.imCellNorm{ii} ...
0220             * data.intWeight(ii);
0221     end
0222     
0223 else
0224     for ii = 1:T0
0225         dataArray.imCell{ii} = dataArray.imCell{ii} + data.imCell{ii};
0226         dataArray.maskCell{ii} = dataArray.maskCell{ii} + data.maskCell{ii};
0227         dataArray.imCellNormW{ii} = dataArray.imCellNormW{ii} ...
0228             + data.imCellNorm{ii} * data.intWeight(ii);
0229         dataArray.imCellNorm{ii}  = dataArray.imCellNorm{ii} ...
0230             + data.imCellNorm{ii};
0231     end
0232     
0233     dataArray.sumWeight = dataArray.sumWeight + data.intWeight;
0234 end
0235 
0236 dataArray.towerCArray{jj} = data.towerC;
0237 dataArray.towerArray{jj} = data.tower;
0238 dataArray.towerNormArray{jj} = data.towerNorm;
0239 dataArray.intWeightMinArray(jj) = min(data.intWeight);
0240 
0241 end
0242 
0243 function dataArray = intNormalize( dataArray, numCells )
0244 % intNormalize : normalizes the quantities in the dataArray by the number
0245 % of cells.
0246 
0247 T0 = numel( dataArray.imCell );
0248 dataArray.numCells = numCells;
0249 
0250 dataArray.towerCArray = dataArray.towerCArray(1:numCells);
0251 dataArray.towerArray = dataArray.towerArray(1:numCells);
0252 dataArray.towerNormArray = dataArray.towerNormArray(1:numCells);
0253 dataArray.intWeightMinArray = dataArray.intWeightMinArray(1:numCells);
0254 dataArray.cellArrayNum = dataArray.cellArrayNum(1:numCells);
0255 
0256 dataArray.tower = dataArray.tower/dataArray.numCells;
0257 dataArray.towerMask = dataArray.towerMask/dataArray.numCells;
0258 dataArray.towerNorm = dataArray.towerNorm/dataArray.numCells;
0259 dataArray.towerNormW = dataArray.towerNormW/dataArray.sumWeightMin;
0260 
0261 for ii = 1:T0
0262     dataArray.imCellSum{ii}  = dataArray.imCell{ii}/numCells;
0263     dataArray.maskCell{ii}  = dataArray.maskCell{ii}/numCells;
0264     weightNorm = sum( dataArray.imCellNorm{ii}(:).*dataArray.maskCell{ii}(:)/...
0265         sum(dataArray.maskCell{ii}(:)));
0266     weightNormW = sum(dataArray.imCellNormW{ii}(:).*dataArray.maskCell{ii}(:)/...
0267         sum(dataArray.maskCell{ii}(:)));
0268     dataArray.imCellNormW{ii} = dataArray.imCellNormW{ii}/weightNormW;
0269     dataArray.imCellNorm{ii}  = dataArray.imCellNorm{ii}/weightNorm;
0270 end
0271 end
0272 
0273 
0274 function data = intInit( numCells )
0275 % intInit : Initializes the arrays for the images.
0276 
0277 data = [];
0278 data.towerCArray = cell(1,numCells);
0279 data.towerArray  = cell(1,numCells);
0280 data.towerNormArray = cell(1,numCells);
0281 data.intWeightMinArray  = zeros( 1,numCells);
0282 data.cellArrayNum = cell(1,numCells);
0283 data.tower           = [];
0284 data.towerNorm       = [];
0285 data.towerNormW      = [];
0286 data.towerMask       = [];
0287 data.imCell          = [];
0288 data.imCellNorm      = [];
0289 data.imCellNormW      = [];
0290 data.maskCell        = [];
0291 data.sumWeight       = [];
0292 data.sumWeightMin    = [];
0293 data.numCells        = numCells;
0294 data.ssTot           = [0, 0];
0295 
0296 end
0297 
0298 
0299 function [ data, num_filename ] = intLoader( cellDir, filename )
0300 % intLoader :loads the cell data and returns the cell's number
0301 
0302 data = load( [cellDir, filename] );
0303 lpos =  find(filename == 'l', 1, 'last' ); % finds last 'l'
0304 ppos =  find(filename == '.', 1 ); % finds first '.'
0305 
0306 if isempty( lpos ) || isempty( ppos )
0307     disp('Error in makeConsIm - could not find cell number' );
0308     return;
0309 else
0310     num_filename = floor(str2num(filename(lpos+1:ppos-1)));
0311 end
0312 
0313 end

Generated on Thu 19-Jan-2017 13:55:21 by m2html © 2005