Home > cellorganizer > utilities > model2instance.m

model2instance

PURPOSE ^

MODEL2INSTANCE Generate a single instance from a valid SLML model.

SYNOPSIS ^

function [object,outres] = model2instance( model, param )

DESCRIPTION ^

 MODEL2INSTANCE Generate a single instance from a valid SLML model.
 Model is a valid protein pattern model. Depending on the model type,
 the structure of param changes.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [object,outres] = model2instance( model, param )
0002 % MODEL2INSTANCE Generate a single instance from a valid SLML model.
0003 % Model is a valid protein pattern model. Depending on the model type,
0004 % the structure of param changes.
0005 
0006 % Author: Ivan E. Cao-Berg (icaoberg@scs.cmu.edu)
0007 %
0008 % Copyright (C) 2011-2012 Murphy Lab
0009 % Lane Center for Computational Biology
0010 % School of Computer Science
0011 % Carnegie Mellon University
0012 %
0013 % March 8, 2012 Added location option to param structure and set a default value to 'all'
0014 % March 21, 2012 Changed protein.location parameter to protein.cytonuclearflag
0015 % October 17, 2012 D. Sullivan added param.cellresolution and param.objresolution
0016 % and resizing code to make everything the resolution of the object model
0017 % being synthesized.
0018 % November 14, 2012 D. Sullivan fixed bugs in resizing cell/nuclear images
0019 % to match object size. Needed to reset param.cell and param.nucleus and
0020 % resize the param.nucleardist and param.celldist images as well
0021 % November 15, 2012 I. Cao-Berg Changed warning to regular display
0022 % January 21, 2013 D. Sullivan updated resolution framework s.t. user may
0023 % now specify multiple object model resolutions and the output will be in
0024 % the form of the lowest resolution (highest numbers since resolutions =
0025 % microns/pixel)
0026 % January 21, 2013 D. Sullivan fixed bug where file was returning if no PSF
0027 % was used. This was necessary to have the file run to the end and resize
0028 % to the output resolution. Moved PSF application to after image resize.
0029 % January 21, 2013 D. Sullivan added resolution field to psf and resized
0030 % based on output resolution. Prints message to user informing of resize.
0031 % January 22, 2013 I. Cao-Berg modified if statement so that it checks
0032 % whether field exists before querying it
0033 % February 4, 2013 D. Sullivan added outres argument to arguments returned.
0034 % February 9, 2013 D. Sullivan added imXYZ which is a cell array of each MT
0035 % position in addition to the outputs already returned by
0036 % model2microtubules
0037 % February 20, 2013 D. Sullivan fixed bug which was forcing 'sampled'
0038 % version of the code to be binary.
0039 % February 27, 2013 D. Sullivan fixed up/downsampling for all patterns to
0040 % occur in this function so that it is done consistently and functions
0041 % downsampling things only upsample again after passing back a smaller data
0042 % matrix (should speed things up a bit)
0043 % September 25, 2013 D. Sullivan added corrections for rounding errors that
0044 % allowed objects that were resized to end up outside the cell.
0045 %
0046 % This program is free software; you can redistribute it and/or modify
0047 % it under the terms of the GNU General Public License as published
0048 % by the Free Software Foundation; either version 2 of the License,
0049 % or (at your option) any later version.
0050 %
0051 % This program is distributed in the hope that it will be useful, but
0052 % WITHOUT ANY WARRANTY; without even the implied warranty of
0053 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0054 % General Public License for more details.
0055 %
0056 % You should have received a copy of the GNU General Public License
0057 % along with this program; if not, write to the Free Software
0058 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0059 % 02110-1301, USA.
0060 %
0061 % For additional information visit http://murphylab.web.cmu.edu or
0062 % send email to murphy@cmu.edu
0063 
0064 object = [];
0065 outres=[];
0066 if nargin > 2
0067     error('CellOrganizer: Wrong number of input arguments.');
0068 end
0069 
0070 if length(model) > 1
0071     if isstruct(model)
0072         model=model(1);
0073     else
0074         model = model{1};
0075     end
0076 end
0077 
0078 
0079 try
0080     dimensionality = model.dimensionality;
0081 catch
0082     warning( 'CellOrganizer: Unable to set dimensionality' );
0083     return
0084 end
0085 
0086 try
0087     nucleus = param.nucleus;
0088 catch
0089     warning('CellOrganizer: Parameter nuclear shape instance not set.')
0090     return
0091 end
0092 
0093 try
0094     cellMembrane = param.cell;
0095 catch
0096     warning('CellOrganizer: Parameter cell shape instance not set.')
0097     return
0098 end
0099 
0100 try
0101     modelClass = model.class;
0102 catch
0103     warning('CellOrganizer: Parameter model class not set.')
0104     return
0105 end
0106 
0107 try
0108     modelType = model.type;
0109 catch
0110     warning('CellOrganizer: Parameter model type not set.');
0111     return
0112 end
0113 
0114 try
0115     microscope = param.microscope;
0116 catch
0117     microscope = 'none';
0118 end
0119 
0120 switch lower(dimensionality)
0121     case '2d'
0122         switch lower(modelType)
0123             case 'vesicle'
0124                 param = ml_initparam(param, ...
0125                     struct('imageSize',[1024 1024],'gentex',0,'loc','all'));
0126                 object = ml_genprotimg( model, nucleus, cellMembrane, param );
0127             otherwise
0128                 warning('CellOrganizer: Unknown or unsupported model type.');
0129                 object = [];
0130                 return
0131         end
0132     case '3d'
0133         %D. Sullivan 10/17/12
0134         %resample cell and nuc to proper resolution these values are expected
0135         %to be in um/pixel and have a value for x,y,and z
0136         %D. Sullivan 11/14/12
0137         %fixed bugs by rewriting param.cell and param.nucleus which are actually
0138         %what is passed to tp_genprotimage. For some reason need to continue to
0139         %actually resize the read in images from the param structure for the pass
0140         %to model2microtubules. Also needed to resize the param.nucleardist and
0141         %param.celldist images appropriately
0142         try
0143             cellMemOriginal = cellMembrane;
0144             nucleusOriginal = nucleus;
0145             if(size(param.resolution.objects,1)>1)
0146 %              if(size(param.resolution.objects,1)==1)
0147                 %since the cell is built conditionally on the nucleus, they should
0148                 %ALWAYS have the same resolution
0149                 initres = param.resolution.cell;%e.g. [0.05,0.05,0.35]
0150                 %D. Sullivan 2/27/13 if for getting the synthesis
0151                 %resolution of the network models.
0152                 if strcmpi(model.class,'centrosome')||strcmpi(model.type,'network')
0153                    finalres = model.resolution;
0154                 else  
0155                    finalres = param.resolution.objects(param.currentmodelnum,:);
0156                 end
0157                 
0158                 %nucleus
0159                 finalsize_xRaw = initres(1)./finalres(1).*size(nucleus,1);
0160                 finalsize_x = floor(finalsize_xRaw);
0161                 finalsize_yRaw = initres(2)./finalres(2).*size(nucleus,2);
0162                 finalsize_y = floor(finalsize_yRaw);
0163                 finalsize_zRaw = initres(3)./finalres(3).*size(nucleus,3);
0164                 finalsize_z = floor(finalsize_zRaw);
0165                 nucleus = imresize(nucleus,[finalsize_x finalsize_y]);
0166                 nucleardisttmp = imresize(param.nucleardist,[finalsize_x finalsize_y]);
0167                 
0168                 %need to resize the z
0169                 nucleus = tp_stretch3d(nucleus,finalsize_z);
0170                 param.nucleus = nucleus;
0171                 nucleardisttmp = tp_stretch3d(nucleardisttmp,finalsize_z);
0172                 param.nucleardist = nucleardisttmp;
0173                 
0174                 %cell
0175                 %Note: the recalculation of final size should be unnecessary since the
0176                 %cell and nucleus images should always be the same size, but since the
0177                 %arithmatic is trivially fast I recalculate to deal with potential
0178                 %weird situations in the future(e.g. if we need the nuclear image to be
0179                 %a smaller object that we add in to the cell image for space) DPS
0180                 finalsize_xRaw = initres(1)./finalres(1).*size(cellMembrane,1);
0181                 finalsize_x = floor(finalsize_xRaw);
0182                 finalsize_yRaw = initres(2)./finalres(2).*size(cellMembrane,2);
0183                 finalsize_y = floor(finalsize_yRaw);
0184                 finalsize_zRaw = initres(3)./finalres(3).*size(cellMembrane,3);
0185                 finalsize_z = floor(finalsize_zRaw);
0186                 cellMembrane = imresize(cellMembrane,[finalsize_x finalsize_y],'bilinear');
0187                 celldisttmp = imresize(param.celldist,[finalsize_x finalsize_y],'bilinear');
0188                 cellMembrane = cellMembrane>0;
0189                 %need to resize the z
0190                 cellMembrane = tp_stretch3d(cellMembrane,finalsize_z);
0191                 param.cell = cellMembrane;
0192                 celldisttmp = tp_stretch3d(celldisttmp,finalsize_z);
0193                 param.celldist = celldisttmp;
0194             end
0195         catch
0196             %icaoberg 11/15/2012
0197             %warning(['CellOrganizer: No resolution specified for either cell or object class',...
0198             %' assuming no resizing necessary. If this is incorrect unexpected results will occur'])
0199             disp(['No resolution specified for either cell or object class',...
0200                 ' assuming no resizing necessary. If this is incorrect unexpected results will occur'])
0201         end
0202         switch lower(modelType)
0203             case 'vesicle'
0204                 %tries to retrieve sampling parameters which if they exist, must already be valid
0205                 try
0206                     sampling = param.sampling.method;
0207                 catch
0208                     param.sampling.method = 0;
0209                 end
0210                 
0211                 try
0212                     N = param.sampling.N;
0213                 catch
0214                     param.sampling.N = [];
0215                 end
0216                 
0217                 try
0218                     location = param.sampling.location;
0219                 catch
0220                     param.sampling.location = 'all';
0221                 end
0222                 
0223                 object = tp_genprotimage( model, param );
0224                 %D. Sullivan 1/21/13 fixed if-return so that file
0225                 %executes until the end
0226                 %D. Sullivan 1/21/13 moved psf application to after final
0227                 %image is created
0228                 
0229                 
0230             case 'centrosome'
0231                 [object,resolution] = model2centrosome(nucleus, cellMembrane, model,param);
0232             case 'network'
0233                 try
0234                     centrosome = param.centrosome;
0235                 catch
0236                     warning(['CellOrganizer: A centrosome instance is needed to ' ...
0237                         'synthesize a cytoskeletal instance. Remember to initialize param.centrosome.'])
0238                     object = [];
0239                     return;
0240                 end
0241                 
0242                 %D. Sullivan 2/4/13 added resolution parameter in return
0243                 %D. Sullivan 2/9/13 added individual MT's in return
0244                 [object,imXYZ,resolution]  = model2microtubules( cellMembrane, nucleus, centrosome, model, param );
0245                 %D. Sullivan 1/21/13 fixed if-return so that file
0246                 %executes until the end
0247                 %D. Sullivan 1/21/13 moved psf application to after final
0248                 %image is created
0249                 
0250             otherwise
0251                 warning('CellOrganizer: Unknown or unsupported model type.');
0252                 object = [];
0253                 return
0254         end
0255         
0256         %dpsull 1/21/13
0257         %need to resize the image to uniform final output resolution given
0258         %by param.outputres
0259         
0260         %icaoberg 1/21/13
0261         %modified if statement so that it checks whether field exists
0262         %before querying it
0263         if( isfield( param, 'resolution' ) && ...
0264                 size(param.resolution.objects,1)>1) && ~strcmpi(modelType,'centrosome')
0265             %D. Sullivan 9/19/13 - changed to finalsizeRaw to avoid
0266             %rounding error
0267 %             outsize_x = ceil(finalres(1)./param.outputres(1).*size(object,1));
0268 %             outsize_y = ceil(finalres(2)./param.outputres(2).*size(object,2));
0269 %             outsize_z = ceil(finalres(3)./param.outputres(3).*size(object,3));
0270             outsize_x = ceil(finalres(1)./param.outputres(1).*finalsize_xRaw);
0271             outsize_y = ceil(finalres(2)./param.outputres(2).*finalsize_yRaw);
0272             outsize_z = ceil(finalres(3)./param.outputres(3).*finalsize_zRaw);
0273             object = imresize(object,[outsize_x outsize_y],'bilinear');
0274             
0275             %need to resize the z
0276             object = tp_stretch3d(object,outsize_z);
0277             
0278             %D. Sullivan 9/25/13 - correct for rounding errors
0279             %Force the object to lie within the cell using truncation.
0280             object = object.*cast((cellMemOriginal>0),class(object));
0281             
0282             
0283             %If it's in the cyto also mask out the nucleus
0284             if strcmpi(model.cytonuclearflag,'cyto');
0285                 object = object.*cast((~(nucleusOriginal)>0),class(object));
0286             end
0287             
0288             %D. Sullivan 2/20/13 need to output the object as binary if
0289             %'disc' mode
0290             if strcmpi(param.sampling.method,'disc')&& strcmpi(modelType,'vesicle')
0291                 object = object>0;
0292             end
0293             
0294         end
0295         %D. Sullivan 2/4/13 add outres returning the resolution of the
0296         %image generated
0297         outres = param.outputres;
0298         
0299         %D. Sullivan 1/21/13 moved psf application to after resize.
0300         [psf,psfres] = micro2psf( microscope );
0301         if ~isempty( psf )&&~strcmpi(modelType,'centrosome')
0302             %D. Sullivan 1/21/13
0303             %check if psf resolution is the same as param.outputres
0304             %if it doesn't resize psfres and print a message.
0305             if isfield(param,'outputres')&&sum(psfres~=param.outputres)~=0
0306                 disp(['Different final resolution specified than trained PSF, ',...
0307                     'resizing PSF to match final resolution'])
0308                 
0309                 outpsf_x = floor(psfres(1)./param.outputres(1).*size(psf,1));
0310                 outpsf_y = floor(psfres(2)./param.outputres(2).*size(psf,2));
0311                 outpsf_z = floor(psfres(3)./param.outputres(3).*size(psf,3));
0312                 psf = imresize(psf,[outpsf_x outpsf_y],'bilinear');
0313                 
0314                 %need to resize the z
0315                 psf = tp_stretch3d(psf,outpsf_z);
0316                 
0317             end
0318             %end 1/21/13 addition
0319             
0320             object = psf_blur_hela_mean_dps( object, psf );
0321         end
0322     otherwise
0323         warning('CellOrganizer: Unknown or unsupported dimensionality.')
0324         object = [];
0325         return
0326 end
0327

Generated on Sun 29-Sep-2013 18:44:06 by m2html © 2005