Home > SuperSegger > cell > trackOptiCellFiles.m

trackOptiCellFiles

PURPOSE ^

trackOptiCellFiles : organizes the data into the final cell files that

SYNOPSIS ^

function trackOptiCellFiles( dirname, dirname_cell, CONST, header, clist )

DESCRIPTION ^

 trackOptiCellFiles : organizes the data into the final cell files that
 contain all the time lapse data for a single cell.
 It allows for cell gating. If a clist is passed with an already made gate t
 code generates cell files for only cells that pass the gate.

 INPUT :
       dirname : xy directory
       dirname_cell : where cell files are placed, usually dirname/cell file
       CONST : segmentation constants
       header : string with information
       clist : array of cell files, can be used to generate gated cell files

 Copyright (C) 2016 Wiggins Lab 
 Written by Stella Stylianidou & Paul Wiggins.
 University of Washington, 2016
 This file is part of SuperSegger.
 
 SuperSegger 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 3 of the License, or
 (at your option) any later version.
 
 SuperSegger 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 SuperSegger.  If not, see <http://www.gnu.org/licenses/>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function trackOptiCellFiles( dirname, dirname_cell, CONST, header, clist )
0002 % trackOptiCellFiles : organizes the data into the final cell files that
0003 % contain all the time lapse data for a single cell.
0004 % It allows for cell gating. If a clist is passed with an already made gate t
0005 % code generates cell files for only cells that pass the gate.
0006 %
0007 % INPUT :
0008 %       dirname : xy directory
0009 %       dirname_cell : where cell files are placed, usually dirname/cell file
0010 %       CONST : segmentation constants
0011 %       header : string with information
0012 %       clist : array of cell files, can be used to generate gated cell files
0013 %
0014 % Copyright (C) 2016 Wiggins Lab
0015 % Written by Stella Stylianidou & Paul Wiggins.
0016 % University of Washington, 2016
0017 % This file is part of SuperSegger.
0018 %
0019 % SuperSegger is free software: you can redistribute it and/or modify
0020 % it under the terms of the GNU General Public License as published by
0021 % the Free Software Foundation, either version 3 of the License, or
0022 % (at your option) any later version.
0023 %
0024 % SuperSegger is distributed in the hope that it will be useful,
0025 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0026 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0027 % GNU General Public License for more details.
0028 %
0029 % You should have received a copy of the GNU General Public License
0030 % along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.
0031 
0032 if ~exist( 'clist', 'var') || isempty( clist) || isempty(clist.data)
0033     ID_LIST = [];
0034 else % gating clist
0035     clist = gate(clist);
0036     ID_LIST = clist.data(:,1);
0037 end
0038 
0039 if ~exist('header')
0040     header = [];
0041 end
0042 
0043 max_cell_num = 0;
0044 
0045 % Set up directory etc.
0046 if(nargin<1 || isempty(dirname))
0047     dirname='.';
0048 end
0049 
0050 dirname = fixDir(dirname);
0051 
0052 if(nargin<2 || isempty(dirname_cell))
0053     dirname_cell = dirname;
0054 end
0055 
0056 
0057 dirname_cell = fixDir (dirname_cell);
0058 contents=dir([dirname '/*_err.mat']);
0059 verbose = CONST.parallel.verbose;
0060 
0061 % check to make sure that there are err.mat files to work from. If not,
0062 % return.
0063 if isempty( contents )
0064     disp( [header, 'trackOptiCellFiles: No error files have been found. Possibly too many segments.'] );
0065 else
0066     
0067     % create a DA cell array for the number of cells in last frame
0068     num_im = length(contents);
0069     data_c = loaderInternal([dirname,contents(end).name]);
0070     MAX_NUM_CELLS = max(data_c.regs.ID)+100;
0071     DA  = cell(1,MAX_NUM_CELLS);
0072     
0073     if CONST.parallel.show_status
0074         h = waitbar( 0, 'Make Cell Files.');
0075         cleanup = onCleanup( @()( delete( h ) ) );
0076     else
0077         h = [];
0078     end
0079     
0080     for i = 1:num_im;
0081         
0082         if CONST.parallel.show_status
0083             waitbar(i/num_im,h,['Make Cell Files--Frame: ',num2str(i),'/',num2str(num_im)]);
0084         elseif verbose
0085             disp( [header, 'Cell Files--Frame: ',num2str(i),'/',num2str(num_im)] );
0086         end
0087         
0088         % load data
0089         data_c    = loaderInternal([dirname,contents(i).name]);
0090         num_regs = data_c.regs.num_regs;
0091         if verbose
0092             disp( [header, 'CellFiles: ','Frame: ', num2str(i), '. max cell num: ', num2str( max_cell_num ) ] );
0093         end
0094         
0095         for ii = 1:num_regs
0096             cellNum = data_c.regs.ID(ii);
0097             max_cell_num = max([max_cell_num, cellNum]);
0098           
0099             if cellNum && ( isempty( ID_LIST ) || ismember( cellNum, ID_LIST ))
0100                 if data_c.regs.birthF(ii) == 1 && data_c.regs.deathF(ii) == 1
0101                     % for snapshot images (birth and death are 1)
0102                     % initialize cell
0103                     DA{cellNum} = intInitCell( data_c, ii, cellNum );
0104                     % update cell
0105                     DA{cellNum} = intUpCell( DA{cellNum}, data_c, ii );
0106                     % delete and save cell
0107                     DA{cellNum} = intDelCell( DA{cellNum},...
0108                         dirname_cell, cellNum );
0109                     
0110                 elseif data_c.regs.birthF(ii)
0111                     % if a cell is born, initialize the structure
0112                     DA{cellNum} = intInitCell( data_c, ii, cellNum );
0113                     
0114                 elseif data_c.regs.deathF(ii)
0115                     % if a cell divides in the current frame, update
0116                     % the structure then delete the cell after saving
0117                     DA{cellNum} = intUpCell( DA{cellNum}, data_c, ii );
0118                     
0119                     % saves and deletes cell from datastructure
0120                     DA{cellNum} = intDelCell( DA{cellNum},...
0121                         dirname_cell, cellNum );
0122                     
0123                 else
0124                     % update the cell
0125                     DA{cellNum} = intUpCell( DA{cellNum}, data_c, ii );
0126                 end
0127             end
0128             
0129         end
0130     end
0131     
0132     % deletes and saves cells that were not saved yet
0133     for ii=1:MAX_NUM_CELLS
0134         if ~isempty(DA{ii})
0135             if verbose
0136             disp( ['Missing cell ', num2str(DA{ii}.ID)] );
0137             end
0138             DA{ii} = intDelCell( DA{ii},dirname_cell, ii );
0139         end
0140     end
0141     
0142     if CONST.parallel.show_status
0143         close(h);
0144     end
0145 end
0146 end
0147 
0148 
0149 function data = intInitCell( data_c, ii, cellNum )
0150 % intInitCell : creates an array of structures for a cell.
0151 % It's called the first frame that a cell appears.
0152 %
0153 % INPUT :
0154 %       data_c : cell data
0155 %       ii : frame number
0156 %       cellNum : cell id
0157 % OUTPUT :
0158 %       data : cell array/structure
0159 
0160 celld  = data_c.CellA{ii};
0161 celld.r  = data_c.regs.props(ii).Centroid;
0162 celld.error.label  = data_c.regs.error.label{ii};
0163 celld.ehist = data_c.regs.ehist(ii);
0164 celld.contactHist  = data_c.regs.contactHist(ii);
0165 celld.stat0 = data_c.regs.stat0(ii);
0166 data.CellA      = {celld};
0167 data.death      = data_c.regs.death(ii);
0168 data.birth      = data_c.regs.birth(ii);
0169 data.divide     = data_c.regs.divide(ii);
0170 data.sisterID   = data_c.regs.sisterID(ii);
0171 data.motherID   = data_c.regs.motherID(ii);
0172 data.daughterID = data_c.regs.daughterID{ii};
0173 data.ID         = cellNum;
0174 data.neighbors  = data_c.regs.neighbors{ii};
0175 
0176 end
0177 
0178 
0179 function data = intUpCell( data, data_c, ii )
0180 % inUpCell : is called to update a cell in a frame. The cell must already
0181 % exist and this must be called before destroying a cell in the frame that
0182 % the cell divides.
0183 
0184 celld = data_c.CellA{ii};
0185 celld.r = data_c.regs.props(ii).Centroid;
0186 celld.error.label = data_c.regs.error.label{ii};
0187 celld.ehist = data_c.regs.ehist(ii);
0188 celld.contactHist = data_c.regs.contactHist(ii);
0189 celld.stat0  = data_c.regs.stat0(ii);
0190 
0191 try
0192     data.CellA      = {data.CellA{:},celld};
0193 catch ME
0194     printError(ME);
0195 end
0196 
0197 data.death      = data_c.regs.death(ii);
0198 data.birth      = data_c.regs.birth(ii);
0199 data.divide     = data_c.regs.divide(ii);
0200 data.sisterID   = data_c.regs.sisterID(ii);
0201 data.motherID   = data_c.regs.motherID(ii);
0202 data.daughterID = data_c.regs.daughterID{ii};
0203 data.neighbors  = data_c.regs.neighbors{ii};
0204 
0205 end
0206 
0207 
0208 function data = intDelCell( data, dirToSave, cellNum )
0209 % inDelCell : destroys a cell when it divides and saves the cell file.
0210 %
0211 % INPUT :
0212 %       data : cell data
0213 %       dirToSave : directory that the cell file will be save in
0214 %       cellNum : cell id used to save the cell file
0215 % OUTPUT :
0216 %       data : cell file
0217 
0218 
0219 data.stat0 = data.CellA{end}.stat0;
0220 data.ehist = data.CellA{end}.ehist;
0221 data.contactHist = data.CellA{end}.contactHist;
0222 dirToSave = fixDir(dirToSave);
0223 
0224 if data.stat0 == 2; % full cell cycle cell.
0225     dataname=[dirToSave,'Cell',sprintf( '%07d', cellNum ),'.mat'];
0226 else
0227     dataname=[dirToSave,'cell',sprintf( '%07d', cellNum ),'.mat'];
0228 end
0229 
0230 save(dataname,'-STRUCT','data');
0231 data = [];
0232 
0233 end
0234 
0235 
0236 function data = loaderInternal( filename )
0237 % loaderInternal : standard internal loader function. It just loads the file
0238 data = load( filename );
0239 end

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