0001 function imgs = model2img( models, param )
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
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111 if nargin > 2
0112 error( 'CellOrganizer: Wrong number of input arguments.' );
0113 end
0114
0115 if nargin == 1
0116 param = [];
0117 end
0118
0119 imgs = {};
0120
0121 try
0122 fileID = param.fileID;
0123 catch err
0124 fileID = [];
0125 end
0126
0127 try
0128 sampling = param.sampling.method;
0129 catch err
0130 param.sampling.method = 'disc';
0131 end
0132
0133
0134
0135
0136
0137
0138 N = [];
0139
0140 try
0141 logger = param.logger;
0142 catch err
0143 logger = false;
0144 end
0145
0146 try
0147 verbose = param.verbose;
0148 catch err
0149 verbose = true;
0150 end
0151
0152
0153 try
0154 debug = param.debug;
0155 if ~isa( debug, 'logical' )
0156 debug = false;
0157 end
0158 catch
0159 debug = false;
0160 end
0161
0162
0163 try
0164 synthesis = param.synthesis;
0165 if ~isa( synthesis, 'char' )
0166 synthesis = 'all';
0167 else
0168 if ~strcmpi( synthesis, 'nucleus' ) && ~strcmpi( synthesis, 'cell' ) && ...
0169 ~strcmpi( synthesis, 'framework' ) && ~strcmpi( synthesis, 'all' )
0170 synthesis = 'all';
0171 else
0172 synthesis = lower( synthesis );
0173 end
0174 end
0175 catch
0176
0177
0178
0179 if isfield(models{1}, 'proteinShape')
0180 param.synthesis = 'all';
0181 elseif isfield(models{1}, 'cellShapeModel')
0182 param.synthesis = 'framework';
0183 else
0184 param.synthesis = 'nucleus';
0185 end
0186 synthesis = param.synthesis;
0187 end
0188
0189
0190
0191
0192 if isempty( models )
0193 disp( 'Input argument models cannot be empty' );
0194 imgs = [];
0195 return;
0196 else
0197 if ~isempty( fileID )
0198 fprintf( fileID, '%s\n', ['Setting model dimensionality to ' models{1}.dimensionality] );
0199 end
0200
0201 if verbose
0202 fprintf( 1, '%s\n', ['Setting model dimensionality to ' models{1}.dimensionality] );
0203 end
0204
0205 try
0206 dimensionality = models{1}.dimensionality;
0207 catch err
0208 warning('Unable to set model dimensionality');
0209 return
0210 end
0211 end
0212
0213 if ~isempty( fileID )
0214 fprintf( fileID, '%s\n', 'Checking all models have the same dimensionality' );
0215 end
0216 if verbose
0217 fprintf( 1, '%s\n', 'Checking all models have the same dimensionality' );
0218 end
0219
0220 if ~exist( [ pwd filesep 'temp'], 'dir' )
0221 mkdir( [pwd filesep 'temp'] );
0222 end
0223
0224 for i=1:1:length(models)
0225
0226 if ~strcmpi( models{i}.dimensionality, dimensionality )
0227
0228 disp('Models have different dimensionality. Unable to synthesize image. Exiting method.');
0229
0230 if ~isempty( fileID )
0231 fprintf( fileID, '%s\n', 'Models have different dimensionality. Unable to synthesize image.' );
0232 end
0233
0234 return;
0235 end
0236 end
0237
0238
0239 if ~isfield(param,'randomwalk')
0240 param.randomwalk = 0;
0241 end
0242
0243 switch lower(dimensionality)
0244
0245 case '2d'
0246 if ~exist( 'param', 'var' )
0247 param = [];
0248 end
0249
0250 imgs = [];
0251
0252
0253
0254
0255
0256 if ~isfield( param, 'image_size' )
0257 param = ml_initparam(param,struct('imageSize',[1024 1024]));
0258 else
0259 param = ml_initparam(param,struct('imageSize', param.image_size));
0260 param = rmfield( param, 'image_size' );
0261 end
0262
0263 param = ml_initparam(param, ...
0264 struct('gentex',0,'loc','all', 'nimgs', 1));
0265
0266 imgs = {};
0267 for i = 1:param.nimgs
0268
0269 [nucEdge,cellEdge,outres] = model2framework( models{1}, param );
0270
0271 if isempty( nucEdge ) || isempty( cellEdge )
0272 return
0273 end
0274
0275 param.resolution.cell = outres;
0276
0277
0278 if strcmpi( param.synthesis, 'framework' ) || ...
0279 strcmpi( param.synthesis, 'all' )
0280
0281
0282
0283 if isempty( nucEdge ) || isempty( cellEdge )
0284 if debug
0285 warning('Unable to synthesize 2D framework.');
0286 return
0287 end
0288 end
0289 else
0290
0291
0292 if isempty( nucEdge )
0293 warning('Unable to synthesize 2D nuclear shape.');
0294 return
0295 end
0296 end
0297
0298
0299 dilate = false;
0300 if dilate
0301
0302 se=strel('disk',4,4);
0303 if ~exist('nuctex','var')
0304 nucimage = imdilate(nucEdge,se);
0305 else
0306 nucimage = nucEdge;
0307 end
0308
0309
0310 if strcmpi( param.synthesis, 'framework' ) || ...
0311 strcmpi( param.synthesis, 'all' )
0312 cellimage = imdilate(cellEdge,se);
0313 end
0314 else
0315
0316 nucimage = nucEdge;
0317 cellimage = cellEdge;
0318 end
0319
0320 img = [];
0321 img(:,:,1) = ml_bcimg(double(nucimage),[],[0 255]);
0322
0323 if exist('cellimage', 'var') && ~isempty(cellimage) && ( strcmpi(synthesis, 'framework') || strcmpi(synthesis, 'all') )
0324 img(:,:,2) = ml_bcimg(double(cellimage),[],[0 255]);
0325
0326
0327 c = 1;
0328 protimage = {};
0329 for j=1:1:length(models)
0330
0331
0332 if length(fieldnames(models{j}.proteinModel)) > 1
0333 protimage{c} = ...
0334 ml_genprotimg2D( models{j}.proteinModel, ...
0335 nucEdge, cellEdge, param );
0336 c = c+1;
0337 end
0338 end
0339
0340 for j=1:1:length(protimage)
0341 img(:,:,2+j) = ml_bcimg(double(protimage{j}),[],[0 255]);
0342 end
0343
0344 end
0345 imgs{i} = img;
0346 end
0347
0348 save( ['temp' filesep 'image.mat'], 'imgs' );
0349 case '3d'
0350
0351
0352 disp(['Checking synthesis flag: ' synthesis ] );
0353
0354
0355
0356 if strcmpi( synthesis, 'nucleus' )
0357 disp( 'Checking parameters for the nuclear model from the first model in list');
0358 end
0359
0360
0361
0362
0363
0364 if strcmpi( synthesis, 'framework' ) || strcmpi( synthesis, 'all' )
0365 disp( 'Checking parameters for the cell model from the first model in list');
0366
0367
0368 fprintf( 1, '%s\n', 'Generating cell framework' );
0369
0370
0371
0372
0373
0374
0375
0376 if ~isfield( models{1}, 'cellShapeModel' )
0377 warning( 'First model in list did not contain a cell model. Exiting method' );
0378 return
0379 end
0380
0381 if isfield(models{1}.cellShapeModel,'resolution')
0382
0383
0384
0385
0386 if isfield(param,'instance')
0387 if isfield(param.instance,'cell')
0388 if isfield(param,'resolution')
0389
0390 if isfield(param.resolution,'cell')
0391
0392 else
0393 warning('No resolution specified for the pre-segmented framework (param.resolution.cell)');
0394 error('Cannot synthesize images');
0395 end
0396 else
0397 warning('CellOrganizer error: no resolution specified for the pre-segmented framework (param.resolution.cell)');
0398 error('Cannot synthesize images');
0399 end
0400 else
0401 param.resolution.cell = models{1}.cellShapeModel.resolution;
0402 end
0403 else
0404 param.resolution.cell = models{1}.cellShapeModel.resolution;
0405 end
0406
0407 elseif isfield(param.resolution,'cell')
0408
0409 else
0410 error('No cell resolution specified. Unable to synthesize image without this information');
0411 end
0412 end
0413
0414
0415
0416
0417 if strcmpi( synthesis, 'all' )
0418 disp( 'Checking parameters for the protein model from the first model in list');
0419
0420
0421 if isfield(models{1}.proteinShape,'resolution')
0422 param.resolution.objects = models{1}.proteinShape.resolution;
0423 elseif isfield(param.resolution.objects)
0424
0425 else
0426 error('No object resolution specified. Unable to synthesize image without this information.');
0427 end
0428 end
0429
0430
0431
0432
0433 if strcmpi( synthesis, 'all' )
0434 disp( 'Checking all the protein models from the list');
0435
0436 disp( 'Calculating number of vesicle models' );
0437 vesicle = {};
0438 for j=1:1:length(models)
0439 if strcmpi( models{i}.proteinShape.type, 'vesicle' )
0440 vesicle{length(vesicle)+1} = j;
0441 end
0442 end
0443
0444 if ~isempty( vesicle )
0445 fprintf( 1, '%s\n', ['At least one vesicle model was found, looking for a cell shape ' ...
0446 ' and a nuclear shape model.']);
0447 if isfield( models{1}, 'nuclearShapeModel' ) && isfield( models{1}, 'cellShapeModel' )
0448 fprintf( 1, '%s\n', 'A nuclear shape and cell shape was found in the first model.');
0449 end
0450 end
0451
0452 microtubules = {};
0453 for j=1:1:length(models)
0454 if strcmpi( models{j}.proteinShape.type, 'network' ) && ...
0455 strcmpi( models{j}.proteinShape.class, 'microtubule' )
0456 microtubules{length(microtubules)+1} = j;
0457 end
0458 end
0459
0460 if ~isempty( microtubules )
0461 fprintf( 1, '%s\n', ['At least one microtubule model was found, ' ...
0462 'looking for a centrosome model.'] );
0463 centrosome = {};
0464 for j=1:1:length(models)
0465 if strcmpi( models{j}.proteinShape.type, 'centrosome' ) && ...
0466 strcmpi( models{j}.proteinShape.class, 'centrosome' )
0467 centrosome{length(centrosome)+1} = j;
0468 end
0469 end
0470 end
0471
0472 if ~isempty( microtubules ) && isempty( centrosome )
0473 if ~isempty( fileID )
0474 fprintf( fileID, '%s\n', ['At least one microtubule model was found ' ...
0475 'in your list but no centrosome models were found.'] );
0476 fprintf( fileID, '%s\n', 'Closing log file' );
0477 fclose( fileID );
0478 end
0479
0480 error( [num2str(length(microtubules)) ' microtubule models where found ' ...
0481 'in your list but no centrosome models found.'] )
0482 end
0483
0484 if exist( 'centrosome', 'var' )
0485 fprintf( 1, '%s\n', ['Centrosome models found:' num2str(length(centrosome))] );
0486 end
0487 end
0488
0489
0490 [param.nucleus, param.cell,outres] = model2framework( models{1}, param );
0491 param.resolution.cell = outres;
0492
0493
0494 if strcmpi( param.synthesis, 'nucleus' )
0495 if isempty( param.nucleus )
0496 if debug
0497 warning( 'Unable to synthesize 3D nucleus' );
0498 end
0499
0500 imgs = {};
0501 return
0502 end
0503 elseif strcmpi( param.synthesis, 'framework' )
0504 if isempty( param.nucleus ) || isempty( param.cell )
0505 if debug
0506 warning( 'Unable to synthesize 3D framework' );
0507 end
0508
0509 imgs = {};
0510 return
0511 end
0512 end
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 fprintf( 1, '%s\n', 'Filling cell and nuclear images' );
0531
0532
0533
0534
0535
0536 param.nucleusfilled=imfill(param.nucleus>0,'holes');
0537 param.cellfilled=imfill(param.cell>0,'holes');
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557 try
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573 if isfield(models{1}.proteinShape,'resolution')
0574 param.resolution.objects = zeros(length(models),3);
0575 for i = 1:length(models)
0576 param.resolution.objects(i,:) = models{i}.proteinShape.resolution;
0577 end
0578 elseif isfield(param.resolution,'objects')
0579
0580 else
0581 error('No object resolutions specified!');
0582 end
0583
0584
0585 if( size( param.resolution.objects, 1 ) == 1 )
0586
0587
0588
0589 param.nucleus = AdjustResolutions(param.nucleus,...
0590 param.resolution.cell,param.resolution.objects);
0591
0592 param.cell = AdjustResolutions(param.cell,...
0593 param.resolution.cell,param.resolution.objects);
0594
0595 if ~isfield(param, 'outputres')
0596 param.outputres = param.resolution.objects;
0597 end
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628 elseif(size(param.resolution.objects,1)>1)
0629
0630
0631
0632
0633 if ~isfield(param, 'outputres')
0634 param.outputres = param.resolution.objects;
0635 end
0636 end
0637 catch
0638
0639 if ~strcmpi( param.synthesis, 'nucleus' )
0640 warning(['CellOrganizer: No resolution specified for either cell or object class ',...
0641 'assuming no resizing necessary. If this is incorrect unexpected results will occur.'])
0642 end
0643 end
0644
0645
0646
0647
0648 if strcmpi( synthesis, 'nucleus' ) || ...
0649 strcmpi( synthesis, 'framework' ) || ...
0650 strcmpi( synthesis, 'all' )
0651 img = param.nucleus;
0652
0653 save( [pwd filesep 'temp/image1.mat'], 'img' );
0654 imgs{length(imgs)+1} = img;
0655 clear img;
0656 end
0657
0658
0659 if strcmpi( synthesis, 'nucleus' )
0660 return
0661 end
0662
0663
0664 if strcmpi( synthesis, 'cell' ) || ...
0665 strcmpi( synthesis, 'framework' ) || ...
0666 strcmpi( synthesis, 'all' )
0667 img = param.cell;
0668
0669 save( [pwd filesep 'temp/image2.mat'], 'img' );
0670 imgs{length(imgs)+1} = img;
0671 clear img;
0672 end
0673
0674
0675
0676 if strcmpi( synthesis, 'cell' ) || strcmpi( synthesis, 'framework' )
0677 return
0678 end
0679
0680 fprintf( 1, '%s\n', ['Computing Euclidean distance transform to cell and nuclear edges'] );
0681 param.celldist = bwdist(bwperim(param.cell));
0682 param.nucleardist = bwdist(bwperim(param.nucleus));
0683 param.nucleardist(param.nucleus==1) = -param.nucleardist(param.nucleus==1);
0684
0685 if exist( 'centrosome', 'var' ) && ~isempty( centrosome )
0686 fprintf( 1, '%s\n', 'Generating centrosome.' );
0687 [param.centrosome,outres] = model2instance( models{centrosome{1}}.proteinShape, param );
0688 end
0689
0690 if verbose
0691 fprintf( 1, '%s\n', ['Saving temporary cell shape model instance'] );
0692 end
0693
0694 clear blank;
0695 clear box;
0696 clear instance;
0697 clear nuclei;
0698
0699 for j=1:1:length(models)
0700 param.currentmodelnum = j;
0701 if ~strcmpi( models{j}.proteinShape.class, 'centrosome' )
0702 if verbose
0703 tic
0704 disp( ['Generating protein pattern model instance ' num2str(j) ' from ' models{j}.proteinShape.class ' pattern'] );
0705 end
0706
0707 [img,outres] = model2instance( models{j}.proteinShape, param );
0708
0709
0710 if verbose
0711 disp( ['Saving temporary protein model instance ' num2str(j+2) ] );
0712 end
0713
0714 save( [pwd filesep 'temp' filesep 'image' num2str(j+2) '.mat'], 'img' );
0715 imgs{length(imgs)+1} = img;
0716 clear img;
0717 end
0718
0719 if param.randomwalk == true
0720 objectMovementModel(models{j},param)
0721 end
0722 end
0723
0724
0725
0726 if( isfield( param, 'resolution' ) && isfield(param, 'outputres'))
0727
0728
0729 initres = param.resolution.cell;
0730
0731
0732
0733 finalres = param.outputres;
0734
0735 if length(finalres) == 1
0736 finalres = repmat(1/finalres, [1,3]).*initres;
0737 end
0738
0739
0740 finalsize_x = ceil(initres(1)./finalres(1).*size(param.nucleus,1));
0741 finalsize_y = ceil(initres(2)./finalres(2).*size(param.nucleus,2));
0742 finalsize_z = ceil(initres(3)./finalres(3).*size(param.nucleus,3));
0743 param.nucleus = imresize(param.nucleus,[finalsize_x finalsize_y],'bilinear');
0744
0745
0746
0747 param.nucleus = tp_stretch3d(param.nucleus,finalsize_z);
0748 param.nucleus = param.nucleus>0;
0749 imgs{1} = param.nucleus;
0750
0751 if strcmpi( synthesis, 'nucleus' ) || ...
0752 strcmpi( synthesis, 'framework' ) || ...
0753 strcmpi( synthesis, 'all' )
0754 img = param.nucleus;
0755
0756 save( [pwd filesep 'temp/image1.mat'], 'img' );
0757 clear img;
0758 end
0759
0760
0761
0762
0763
0764
0765
0766 finalsize_x = ceil(initres(1)./finalres(1).*size(param.cell,1));
0767 finalsize_y = ceil(initres(2)./finalres(2).*size(param.cell,2));
0768 finalsize_z = ceil(initres(3)./finalres(3).*size(param.cell,3));
0769 param.cell = imresize(param.cell,[finalsize_x finalsize_y],'bilinear');
0770
0771
0772 param.cell = tp_stretch3d(param.cell,finalsize_z);
0773 param.cell = param.cell;
0774 param.cell = param.cell>0;
0775 imgs{2} = param.cell;
0776
0777 if strcmpi( synthesis, 'cell' ) || ...
0778 strcmpi( synthesis, 'framework' ) || ...
0779 strcmpi( synthesis, 'all' )
0780 img = param.cell;
0781
0782 save( [pwd filesep 'temp/image2.mat'], 'img' );
0783 clear img;
0784 end
0785
0786
0787
0788 for j = 3:length(imgs)
0789 img = imgs{j};
0790 img = imresize(img,[finalsize_x finalsize_y],'bilinear');
0791 img = tp_stretch3d(img, finalsize_z);
0792 save( [pwd filesep 'temp/image' num2str(j) '.mat'], 'img' );
0793 imgs{j} = img;
0794
0795 end
0796
0797 end
0798
0799
0800 otherwise
0801 error( 'CellOrganizer: Unknown dimensionality or dimensionality argument missing.' );
0802 end
0803 end