0001 function answer = img2slml( 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 
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 
0112 
0113 
0114 
0115 
0116 
0117 
0118 
0119 
0120 
0121 
0122 
0123 
0124 
0125 
0126 
0127 
0128 
0129 
0130 
0131 
0132 
0133 
0134 
0135 
0136 answer = false;
0137 
0138 
0139 
0140 
0141 try
0142     param = varargin{5};
0143     if ~isa( param, 'struct' )
0144         warning('CellOrganizer: Input parameter list must be a structure.');
0145     end
0146     param.slml.level = '1';
0147     param.slml.version = '1.1';
0148 catch
0149     param = [];
0150     param.slml.level = '1';
0151     param.slml.version = '1.1';
0152     param.display = false;
0153     param.debug = false;
0154     param.verbose = true;
0155 end
0156 
0157 try
0158     verbose = param.verbose;
0159     if ~isa( verbose, 'logical' )
0160         verbose = true;
0161     end
0162 catch
0163     verbose = true;
0164 end
0165 
0166 
0167 
0168 try
0169     display = param.display;
0170     if ~isa( display, 'logical' )
0171         dislpay = false;
0172     end
0173 catch
0174     display = false;
0175     param.display = display;
0176 end
0177 
0178 try
0179     debug = param.debug;
0180     if ~isa( debug, 'logical' )
0181         param.debug = false;
0182         debug = false;
0183     end
0184 catch
0185     param.debug = false;
0186     debug = false;
0187 end
0188 
0189 
0190 if length(varargin) == 5
0191     
0192     dimensionality = varargin{1};
0193     if ~(strcmpi( dimensionality, '2d' ) || ...
0194             strcmpi( dimensionality, '3d' ))
0195         
0196         
0197         warning('Unrecognized or unsupported dimensionality. Exiting method.');
0198         return
0199     else
0200         
0201         param.dimensionality = dimensionality;
0202     end
0203     
0204     dnaImagesDirectoryPath = varargin{2};
0205     cellImagesDirectoryPath = varargin{3};
0206     proteinImagesDirectoryPath = varargin{4};
0207     
0208     
0209     try
0210         temp = param.model.name;
0211         
0212         if ~isa( temp, 'char' )
0213             warning(['CellOrganizer: Optional input argument param.model.name must be a string. ' ...
0214                 'Changing to default value']);
0215             param.model.name = '';
0216         end
0217     catch
0218         param.model.name = '';
0219     end
0220     
0221     try
0222         temp = param.model.id;
0223         
0224         if ~isa( temp, 'char' )
0225             warning(['CellOrganizer: Optional input argument param.model.id must be a string. ' ...
0226                 'Changing to default value']);
0227             param.model.id = '';
0228         end
0229     catch
0230         param.model.id = '';
0231     end
0232     
0233     
0234     try
0235         downsampling = param.model.downsampling;
0236     catch
0237         if strcmpi( dimensionality, '3D' )
0238             downsampling = [5 5 1];
0239         else
0240             downsampling = [1 1];
0241         end
0242     end
0243     
0244     
0245     try
0246         param.model.original_resolution = param.model.resolution;
0247         
0248         param.model.protein_resolution = param.model.original_resolution;
0249         
0250         param.model.downsampling = downsampling;
0251         param.model.resolution = param.model.resolution .* downsampling;
0252         clear downsampling
0253     catch
0254         disp('Model resolution not set. Exiting method.');
0255         
0256         answer = false;
0257         return
0258     end
0259     
0260     try
0261         temp = param.model.filename;
0262         if ~isa( temp, 'char' )
0263             error('CellOrganizer: Input parameter filename must be a string.');
0264         end
0265     catch err
0266         error( 'CellOrganizer: Unable to set output filename' );
0267     end
0268     
0269     
0270     compartments = { 'nucleus', 'cell', 'protein' };
0271     fields = { 'type', 'name', 'id' };
0272     for c=1:1:length(compartments)
0273         compartment = [ 'param.' compartments{c} ];
0274         for f=1:1:length(fields)
0275             field = [ compartment '.' fields{f} ];
0276             try
0277                 eval(['temp=' field ';']);
0278                 if ~isa( temp, 'char' )
0279                     eval([field '=''''' ';']);
0280                 end
0281             catch
0282                 eval([field '=''''' ';']);
0283             end
0284         end
0285     end
0286     
0287     try
0288         documentation = param.documentation;
0289         if ~isa( documentation, 'struct' )
0290             error('CellOrganizer: Documentation must be a structure.');
0291         end
0292     catch
0293         param.documentation = [];
0294     end
0295     
0296     
0297     
0298     try
0299         trainFlag = param.train.flag;
0300         if ~isa( trainFlag, 'char' );
0301             error('CellOrganizer: training flag must be a string');
0302         end
0303     catch
0304         param.train.flag = 'all';
0305         
0306         trainFlag = param.train.flag;
0307     end
0308     
0309     
0310     if ~strcmpi( trainFlag, 'nuclear') && ...
0311             ~strcmpi( trainFlag, 'framework') && ...
0312             ~strcmpi( trainFlag, 'all')
0313         if debug
0314             disp('CellOrganizer: unrecognized training flag. Using default value.');
0315         end
0316         trainFlag = 'all';
0317     end
0318     
0319 elseif length(varargin) == 10
0320     
0321     if verbose
0322         fprintf( 1, '%s', 'Entering backward compatibility mode' );
0323     end
0324     
0325     dimensionality = varargin{1};
0326     if ~strcmpi( dimensionality, '2D' ) || ...
0327             ~strcmpi( dimensionality, '3D' )
0328         error('CellOrganizer: Unknown or unsupported dimensionality');
0329     end
0330     
0331     dnaImagesDirectoryPath = varargin{2};
0332     if ~exist( dnaImagesDirectory, 'dir' )
0333         error('CellOrganizer: DNA images directory does not exist.')
0334     end
0335     
0336     cellImagesDirectoryPath = varargin{3};
0337     if ~exist( cellImagesDirectory, 'dir' )
0338         error('CellOrganizer: Cell images directory does not exist.')
0339     end
0340     
0341     proteinImagesDirectoryPath = varargin{4};
0342     if ~exist( proteinImagesDirectory, 'dir' )
0343         error('CellOrganizer: Protein images directory does not exist.')
0344     end
0345     
0346     modelName = varargin{5};
0347     if ~isa( modelName, 'char' )
0348         error('CellOrganizer: Model name must be a string.');
0349     end
0350     
0351     cellName = varargin{6};
0352     if ~isa( cellName, 'char' )
0353         error('CellOrganizer: Cell name must be a string.');
0354     end
0355     
0356     compartmentName = varargin{7};
0357     if ~isa( compartmentName, 'char' )
0358         error('CellOrganizer: Compartment name must be a string.');
0359     end
0360     
0361     proteinName = varargin{8};
0362     if ~isa( proteinName, 'char' )
0363         error('CellOrganizer: Protein name must be a string.');
0364     end
0365     
0366     documentation = varargin{9};
0367     if isempty( documentation )
0368         warning('CellOrganizer: Documentation is empty.');
0369     elseif ~isa( documentation, 'struct' )
0370         error('CellOrganizer: Documentation must be a structure');
0371     end
0372     
0373     filename = varargin{10};
0374     if isempty( filename )
0375         error( 'CellOrganizer: Filename for output SLML instance cannot be empty.');
0376     elseif ~isa( filename, 'char' );
0377         error( 'CellOrganizer: Filename for output SLML instance must be a string.');
0378     end
0379 else
0380     
0381     error('CellOrganizer: Wrong number of input arguments.');
0382 end
0383 
0384 if verbose
0385     fprintf( 1, '%s\n', 'Checking the existence of temporary folder' );
0386 end
0387 
0388 dimensionality = varargin{1};
0389 
0390 
0391 if strcmpi( dimensionality, '2D' )
0392     
0393     model = img2model( dimensionality, ...
0394         dnaImagesDirectoryPath, ...
0395         cellImagesDirectoryPath, ...
0396         proteinImagesDirectoryPath, param );
0397     
0398     
0399     if isempty( model )
0400         disp('CellOrganizer: img2model returned an empty model. Exiting program.')
0401         return
0402     end
0403 elseif strcmpi( dimensionality, '3D' )
0404     
0405     
0406     
0407     
0408     
0409     
0410     dnaImagesDirectoryPath = varargin{2};
0411     cellImagesDirectoryPath = varargin{3};
0412     proteinImagesDirectoryPath = varargin{4};
0413     
0414     model = img2model( dimensionality, ...
0415         dnaImagesDirectoryPath, ...
0416         cellImagesDirectoryPath, ...
0417         proteinImagesDirectoryPath, ...
0418         param );
0419     
0420     
0421     if isempty( model )
0422         warning( ['CellOrganizer: img2model returned an empty model. ' ...
0423             'Exiting program.']);
0424         return
0425     end
0426 else
0427     error(['CellOrganizer: Unknown dimensionality ' dimensionality ]);
0428 end
0429 
0430 
0431 filename = param.model.filename;
0432 if strcmpi( dimensionality, '2D' );
0433     
0434     
0435     
0436     
0437     
0438     
0439     if ~isfield(model,'dimensionality')
0440         model.dimensionality = '2D';
0441     end
0442     
0443     
0444     
0445     
0446     
0447     try
0448         if ~isempty( param.model.name )
0449             model.name = param.model.name;
0450         end
0451     catch
0452         if debug
0453             disp('Model name not set. Ignoring meta data field.')
0454         end
0455     end
0456     
0457     
0458     try
0459         if ~isempty( param.model.id )
0460             model.id = param.model.id;
0461         end
0462     catch
0463         
0464         if debug
0465             disp('Model ID not set. Ignoring meta data field.')
0466         end
0467     end
0468     
0469     
0470     try
0471         if ~isempty( param.model.filename )
0472             model.filename = param.model.filename;
0473         end
0474     catch
0475         
0476         if debug
0477             disp('Model filename not set. Ignoring meta data field.')
0478         end
0479     end
0480     
0481     
0482     
0483     try
0484         if ~isempty( param.nucleus.name )
0485             model.nuclearShapeModel.name = param.nucleus.name;
0486         end
0487     catch
0488         if debug
0489             disp('Nuclear shape model name not set. Ignoring meta data field.')
0490         end
0491     end
0492     
0493     
0494     try
0495         if ~isempty( param.nucleus.id )
0496             model.nuclearShapeModel.id = param.nucleus.id;
0497         end
0498     catch
0499         if debug
0500             disp('Nuclear shape model ID not set. Ignoring meta data field.')
0501         end
0502     end
0503     
0504     
0505     try
0506         if ~isempty( param.nucleus.type )
0507             model.nuclearShapeModel.type = param.nucleus.type;
0508         else
0509              model.nuclearShapeModel.type = 'medial axis';
0510         end
0511     catch
0512         if debug
0513             disp('Nuclear shape model type not set. Using default: medial axis.')
0514             model.nuclearShapeModel.type = 'medial axis';
0515         end
0516     end
0517     
0518     
0519     
0520     model.nuclearShapeModel.resolution = param.model.resolution;
0521     
0522     
0523     
0524     if strcmpi( param.train.flag, 'framework' ) || strcmpi( param.train.flag, 'all' )
0525         try
0526             if ~isempty( param.cell.name )
0527                 model.cellShapeModel.name = param.cell.name;
0528             end
0529         catch
0530             if debug
0531                 disp('Cell shape model name not set. Ignoring meta data field.')
0532             end
0533         end
0534         
0535         
0536         try
0537             if ~isempty( param.cell.id )
0538                 model.cellShapeModel.id = param.cell.id;
0539             end
0540         catch
0541             if debug
0542                 disp('Cell shape model ID not set. Ignoring meta data field.')
0543             end
0544         end
0545         
0546         
0547         try
0548             
0549             
0550             if ~isempty( para.cell.type )
0551                 model.cellShapeModel.type = param.cell.type;
0552             else
0553                 model.cellShapeModel.type = 'ratio';
0554             end
0555         catch
0556             if debug
0557                 disp('Cell shape model type not set. Using default: ratio.')
0558                 model.cellShapeModel.type = 'ratio';
0559             end
0560         end
0561         
0562         
0563         
0564         model.cellShapeModel.resolution = param.model.resolution;
0565     end
0566     
0567     
0568     
0569     if strcmpi( param.train.flag, 'all' )
0570         try
0571             if ~isempty( param.protein.name )
0572                 model.proteinModel.name = param.protein.name;
0573             end
0574         catch
0575             if debug
0576                 disp('Protein shape model name not set. Ignoring meta data field.')
0577             end
0578         end
0579         
0580         
0581         try
0582             if ~isempty( param.protein.id )
0583                 model.proteinModel.id = param.protein.id;
0584             end
0585         catch
0586             if debug
0587                 disp('Protein shape model ID not set. Ignoring meta data field.')
0588             end
0589         end
0590         
0591         
0592         try
0593             if ~isempty( param.protein.type )
0594                 model.proteinModel.type = param.protein.type;
0595             end
0596         catch
0597             if debug
0598                 disp('Protein shape model type not set. Using default: vesicle.')
0599                 model.proteinModel.type = 'vesicle';
0600             end
0601         end
0602         
0603         
0604         
0605         model.proteinModel.resolution = param.model.resolution;
0606      end
0607     
0608     
0609     
0610     
0611     disp( 'Building intermediate results' );
0612     make_intermediate_results_from_temp_folder( param );
0613     
0614     
0615     
0616     if ~debug
0617         disp( 'Removing temporary folder' );
0618         rmdir( [ pwd filesep 'temp' ], 's'  );
0619     end
0620     
0621     
0622     save( [filename(1:end-3) 'mat'], 'model' );
0623     answer = true;
0624 else
0625     
0626     
0627     model.dimensionality = dimensionality;
0628     
0629     
0630     try
0631         model.name = param.model.name;
0632     catch
0633         if debug
0634             disp('Model name not set. Ignoring meta data field.')
0635         end
0636     end
0637     
0638     
0639     try
0640         model.id = param.model.id;
0641     catch
0642         if debug
0643             disp('Model ID not set. Ignoring meta data field.')
0644         end
0645     end
0646     
0647     
0648     try
0649         model.filename = param.model.filename;
0650     catch
0651         if debug
0652             disp('Model filename not set. Ignoring meta data field.')
0653         end
0654     end
0655     
0656     
0657     
0658     try
0659         model.nuclearShapeModel.name = param.nucleus.name;
0660     catch
0661         if debug
0662             disp('Nuclear shape model name not set. Ignoring meta data field.')
0663         end
0664     end
0665     
0666     
0667     
0668     try
0669         model.nuclearShapeModel.id = param.nucleus.id;
0670     catch
0671         if debug
0672             disp('Nuclear shape model ID not set. Ignoring meta data field.')
0673         end
0674     end
0675     
0676     
0677     try
0678         model.nuclearShapeModel.type = param.nucleus.type;
0679     catch
0680         if debug
0681             disp('Nuclear shape model type not set. Using default: medial axis.')
0682             model.nuclearShapeModel.type = 'medial axis';
0683         end
0684     end
0685     
0686     if strcmpi( trainFlag, 'framework' ) || strcmpi( trainFlag, 'all' )
0687         
0688         
0689         try
0690             model.cellShapeModel.name = param.cell.name;
0691         catch
0692             if debug
0693                 disp('Cell shape model name not set. Ignoring meta data field.')
0694             end
0695         end
0696         
0697         
0698         try
0699             model.cellShapeModel.id = param.cell.id;
0700         catch
0701             if debug
0702                 disp('Cell shape model ID not set. Ignoring meta data field.')
0703             end
0704         end
0705         
0706         
0707         try
0708             
0709             
0710             model.cellShapeModel.type = param.cell.type;
0711         catch
0712             if debug
0713                 disp('Cell shape model type not set. Using default: ratio.')
0714                 model.cellShapeModel.type = 'ratio';
0715             end
0716         end
0717     end
0718     
0719     if strcmpi( trainFlag, 'all' )
0720         
0721         
0722         try
0723             if isfield( param.protein, 'name' ) && ~isempty( param.protein.name )
0724                 model.proteinShape.name = param.protein.name;
0725             end
0726         catch
0727             if debug
0728                 disp('Protein shape model name not set. Ignoring meta data field.')
0729             end
0730         end
0731         
0732         
0733         try
0734             model.proteinShape.id = param.protein.id;
0735         catch
0736             if debug
0737                 disp('Protein shape model ID not set. Ignoring meta data field.')
0738             end
0739         end
0740         
0741         
0742         try
0743             model.proteinShape.type = param.protein.type;
0744         catch
0745             if debug
0746                 disp('Protein shape model type not set. Using default: vesicle.')
0747                 model.proteinShapeModel.type = 'vesicle';
0748             end
0749         end
0750         
0751         
0752         try
0753             model.proteinShape.class = param.protein.class;
0754         catch
0755             if debug
0756                 disp('Protein shape model class not set. Ignoring meta data field.')
0757             end
0758         end
0759         
0760         
0761         
0762         
0763         try
0764             flag = param.protein.cytonuclearflag;
0765             if ~strcmpi( flag, 'cyto' ) && ~strcmpi( flag, 'nuc' ) ...
0766                     ...
0767                     if debug
0768                     warning('Protein shape model cytonuclear flag is unrecognized. Using default: cyto.');
0769                     end
0770                     flag = 'cyto';
0771             end
0772             
0773             model.proteinShape.cytonuclearflag = flag;
0774             clear flag;
0775         catch
0776             if debug
0777                 warning( 'Protein shape model cytonuclear flag not set. Using default: cyto.');
0778             end
0779             model.proteinShape.cytonuclearflag = 'cyto';
0780         end
0781         
0782         
0783         model.proteinShape.dimensionality = dimensionality;
0784         
0785         
0786         
0787         
0788         model.nuclearShapeModel.resolution = param.model.resolution;
0789         model.cellShapeModel.resolution = param.model.resolution;
0790     end
0791     
0792     try
0793         model.documentation = param.documentation;
0794         model.documentation.date = date;
0795     catch
0796         model.documentation.date = date;
0797     end
0798     
0799     
0800     
0801     
0802     make_intermediate_results_from_temp_folder( param );
0803     
0804     
0805     
0806     if ~debug
0807         disp( 'Removing temporary folder' );
0808         rmdir( [ pwd filesep 'temp' ], 's'  );
0809     end
0810     
0811     
0812     save( [filename(1:end-3) 'mat'], 'model' );
0813     answer = true;
0814 end
0815 end