0001 function model = img2model( dimensionality, varargin )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 nuclearModelImages = [];
0072 cellModelImages = [];
0073 proteinModelImages = [];
0074
0075 model = [];
0076 switch lower(dimensionality)
0077 case '2d'
0078
0079
0080
0081 if nargin ~= 5
0082 warning('CellOrganizer: Wrong number of input arguments.');
0083 return;
0084 end
0085
0086
0087
0088 nuclearModelImagesDirectory = varargin{1};
0089
0090 cellModelImagesDirectory = varargin{2};
0091
0092 proteinModelImagesDirectory = varargin{3};
0093
0094 nuclearModelImages = ml_ls( ...
0095 nuclearModelImagesDirectory );
0096 cellModelImages = ml_ls( ...
0097 cellModelImagesDirectory );
0098 proteinModelImages = ml_ls( ...
0099 proteinModelImagesDirectory );
0100
0101
0102
0103 param = varargin{4};
0104 if ~isa( param, 'struct' )
0105 warning('CellOrganizer: Input argument parameter must be a struct.');
0106 return;
0107 end
0108
0109
0110 debug = setDebugParam(param);
0111 verbose = setVerboseParam(param);
0112
0113
0114 try
0115 masks = ml_ls( ...
0116 param.masks );
0117 catch
0118 masks = [];
0119 end
0120
0121
0122
0123 if ~exist([pwd filesep 'temp' filesep 'preprocessed'])
0124 mkdir([pwd filesep 'temp' filesep 'preprocessed']);
0125 end
0126
0127
0128 param.disp = 'false';
0129 model = ml_traingenmodel2D( ...
0130 proteinModelImages,...
0131 nuclearModelImages, ...
0132 cellModelImages, ...
0133 masks, param );
0134 case '3d'
0135
0136
0137 dnaImagesDirectoryPath = varargin{1};
0138 cellImagesDirectoryPath = varargin{2};
0139 proteinImagesDirectoryPath = varargin{3};
0140 param = varargin{4};
0141 if ~isa( param , 'struct' )
0142 warning('CellOrganizer: Input parameter list must be a structure.');
0143 return
0144 end
0145
0146
0147 debug = setDebugParam(param);
0148 verbose = setVerboseParam(param);
0149
0150
0151 try
0152 trainFlag = param.train.flag;
0153 if ~isa( trainFlag, 'char' );
0154 error('CellOrganizer: training flag must be a string');
0155 end
0156 catch
0157 param.train.flag = 'all';
0158 trainFlag = param.train.flag;
0159 end
0160
0161
0162 if ~strcmpi( trainFlag, 'nuclear' ) && ...
0163 ~strcmpi( trainFlag, 'framework' ) && ...
0164 ~strcmpi( trainFlag, 'all' )
0165 error('CellOrganizer: Unrecognized training flag');
0166 end
0167
0168
0169
0170 if strcmpi(param.nucleus.type, 'diffeomorphic') | ...
0171 strcmpi(param.cell.type, 'diffeomorphic')
0172 isdiffeomorphic = true;
0173 else
0174 isdiffeomorphic = false;
0175 end
0176
0177
0178
0179
0180
0181
0182 dna_image_files = ml_ls( dnaImagesDirectoryPath );
0183 cell_image_files = ml_ls( cellImagesDirectoryPath );
0184 prot_image_files = ml_ls( proteinImagesDirectoryPath );
0185
0186
0187
0188 if isempty( dna_image_files )
0189 warning('Could not find any images in the DNA images directory. Using DNA hole finding.' );
0190 dna_image_files = cell(size(cell_image_files));
0191
0192
0193 end
0194
0195 if isempty( cell_image_files )
0196 warning('Could not find any images in the cell images directory. Exiting method.' );
0197 model = [];
0198 return
0199 end
0200
0201
0202
0203 if isempty(prot_image_files)
0204 if strcmpi(param.train.flag,'framework')
0205 prot_image_files = cell(size(cell_image_files));
0206 else
0207 warning('Could not find any images in the protein images directory. Exiting method.' );
0208 model = [];
0209 return
0210 end
0211 end
0212
0213
0214 if isfield(param,'masks')
0215 mask_image_files = ml_ls(param.masks);
0216 else
0217 mask_image_files = cell(1,length(dna_image_files));
0218 end
0219
0220 numimgs = length(cell_image_files);
0221
0222
0223
0224
0225
0226 param = set_temp_result_folders(param);
0227 param = ml_initparam(param,struct('downsample',[1,1,1], 'preprocess', true, 'display', false));
0228
0229
0230
0231
0232
0233
0234 startmodel = true;
0235 if param.preprocess
0236 if ~isfield(param,'parallel') || param.parallel>=1
0237 try
0238 matlabpool('open', param.parallel)
0239
0240 parfor i = 1:numimgs
0241 percellparam(dna_image_files{i},cell_image_files{i},...
0242 prot_image_files{i},mask_image_files{i}, i, param)
0243 end
0244 parallelflag = true;
0245
0246 catch
0247 disp('Parallel code failed, trying linear method');
0248 parallelflag = false;
0249 end
0250 else
0251 parallelflag = false;
0252 end
0253
0254
0255 if ~parallelflag
0256 cellCounter = [];
0257 c = 1;
0258 for i = 1:numimgs
0259 tmpfile = [param.tempparent filesep 'image_lock_' num2str(i)];
0260 [startimage, ~, ~, tmpfile] = chunk_start(tmpfile);
0261
0262 if startimage
0263 try
0264 percellparam(dna_image_files{i},cell_image_files{i},...
0265 prot_image_files{i},mask_image_files{i}, i, param)
0266 catch
0267 disp(['Skipping image ' num2str(i) ' due to error'])
0268 end
0269 chunk_finish(tmpfile)
0270 else
0271 disp(['Image ' num2str(i) ' currently being operated on. Skipping.']);
0272 startmodel = false;
0273
0274 cellCounter(c) = i;
0275 tmpCounter{c} = tmpfile;
0276 c = c+1;
0277 end
0278
0279 end
0280 end
0281 else
0282 disp('Preprocessing flag set to false. Using only currently existing preprocessing results in the temp directory')
0283 end
0284
0285 if ~startmodel
0286
0287 if any(cellfun(@(x) exist(x, 'file'), tmpCounter))
0288 disp('The following images are still processing:')
0289 for i = 1:length(cellCounter)
0290 disp([num2str(cellCounter(i)) ': ' tmpCounter{i}])
0291 end
0292
0293
0294
0295 if ~isdiffeomorphic
0296 model = [];
0297 return;
0298 end
0299
0300
0301 end
0302 end
0303
0304
0305
0306
0307 if isfield(param,'percellreport') && ...
0308 (param.percellreport == true || param.percellreport == 1)
0309
0310 model2report_percell(param);
0311 end
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 try
0323
0324 model.dimensionality = '3D';
0325
0326
0327 if isdiffeomorphic
0328
0329 diff_model = train_diffeomorphic_model(param, numimgs);
0330 if strcmpi( trainFlag, 'all' ) | strcmpi(trainFlag, 'framework');
0331 model.nuclearShapeModel = diff_model;
0332 model.cellShapeModel = diff_model;
0333 elseif strcmpi(trainFlag, 'nuc')
0334 model.nuclearShapeModel = diff_model;
0335 elseif strcmpi(trainFlag, 'cell')
0336 model.cellShapeModel = diff_model;
0337 end
0338
0339 else
0340 if verbose
0341
0342 fprintf( 1, '%s\n', 'Training nuclear shape model' );
0343 end
0344
0345
0346
0347 if ~exist([param.tempparent filesep 'nuc_model.mat'],'file')
0348 model.nuclearShapeModel = train_nuc_shape_model( param.nuctemppath,...
0349 param.tempparent,param );
0350 else
0351 load([param.tempparent filesep 'nuc_model.mat']);
0352 model.nuclearShapeModel = nuclearShapeModel;
0353 end
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 if strcmpi(param.train.flag,'all')||strcmpi(param.train.flag,'framework')
0364 fprintf( 1, '%s\n', 'Training cell shape model' );
0365 if ~exist([param.tempparent filesep 'cell_shape_model.mat'],'file')
0366 model.cellShapeModel = train_cell_shape_model3(param.celltemppath,...
0367 param.tempparent);
0368 else
0369 load([param.tempparent filesep 'cell_shape_model.mat']);
0370 model.cellShapeModel = cellShapeModel;
0371 end
0372 end
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383 if strcmpi( trainFlag, 'all' )
0384 if verbose
0385
0386 fprintf( 1, '%s\n', 'Training protein model' );
0387 end
0388
0389
0390
0391
0392 if ~exist([param.tempparent filesep 'protmodel.mat'],'file')
0393 model.proteinShape = train_protein_model2( param );
0394 else
0395 load([param.tempparent filesep 'protmodel.mat']);
0396 model.proteinShape = proteinShape;
0397 end
0398
0399
0400
0401
0402
0403
0404 end
0405
0406 end
0407
0408
0409
0410 if isfield(param.model, 'original_resolution')
0411 model.info.original_resolution = param.model.original_resolution;
0412 else
0413 model.info.original_resolution = 'n/a';
0414 end
0415
0416 if isfield(param.model, 'downsampling')
0417 model.info.downsampling_vector = param.model.downsampling;
0418 else
0419 model.info.downsampling_vector = 1;
0420 end
0421
0422
0423 if isfield(param.model, 'resolution')
0424 model.cellShapeModel.resolution = param.model.resolution;
0425 else
0426 model.cellShapeModel.resolution = 'n/a';
0427 end
0428
0429
0430
0431
0432 catch err
0433
0434
0435
0436 model = [];
0437 warning('CellOrganizer: Unable to train 3D generative model.');
0438
0439
0440 if debug
0441 getReport( err, 'extended' )
0442 end
0443
0444 if ~debug
0445 rmdir( 'temp', 's' );
0446 end
0447
0448 return
0449 end
0450 otherwise
0451 warning(['Unknown dimensionality ' ...
0452 dimensionality '. Exiting method.' ]);
0453 model = [];
0454 return
0455 end
0456
0457 end
0458
0459 function [debug] = setDebugParam(param)
0460
0461 try
0462 debug = param.debug;
0463 if ischar(debug)
0464 if strcmpi(debug, 'false')
0465 debug = false;
0466 else
0467 debug = true;
0468 end
0469 else
0470 debug = logical(debug);
0471 end
0472 catch
0473 debug = true;
0474 end
0475 end
0476
0477 function [verbose] = setVerboseParam(param)
0478
0479 try
0480 verbose = param.verbose;
0481 if ischar(verbose)
0482 if strcmpi(verbose, 'false')
0483 verbose = false;
0484 else
0485 verbose = true;
0486 end
0487 else
0488 verbose = logical(verbose);
0489 end
0490 catch
0491 verbose = true;
0492 end
0493 end