Home > SuperSegger > batch > convertImageNames.m

convertImageNames

PURPOSE ^

convertImageNames : Convert image names from to NIS Elements format

SYNOPSIS ^

function convertImageNames(dirname, basename, timeFilterBefore,timeFilterAfter, xyFilterBefore,xyFilterAfter, channelNames )

DESCRIPTION ^

 convertImageNames : Convert image names from to NIS Elements format
 The file naming convention for elements is basename_t1xy1c1.tif
 where c1 is brightfield and c2,c3 etc are the fluorescent channels.
 To convert using this program you need to specify what is before and
 after the time in your image filename, before and after the xy position
 and the channelNames used in your filenames starting with phase.
 If you leave both the before and after values as '', then it sets the
 value to 1 (can be used for snapshots, or a single xy position).
 For example, Micromanager has the convention img_00000000t_channel.tif,
 if we used BF for phase images and gfp for the 1st channel we would call
 this function as following :
 convertImageNames(dirname, 'img', '_', 't', '','' , {'BF','gfp'} )
 which would rename img_00000001t_BF.tif to img_t1xy1c1.tif

 INPUT : dirname : directory contains micromanager images
         timeFilterBefore : string found before frame number
         timeFilterAfter : string found after frame number
         xyFilterBefore : string found before xy position number
         xyFilterAfter : string found after xy position number
         channelFilters : cell with strings for each channel name found
         in the filenames. For example {'BF','gfp', 'mcherry'} will convert *BF*
         images to *c1*, gfp to c2 and mcherry to c3 .


 Copyright (C) 2016 Wiggins Lab
 Written by Stella Stylianidou.
 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 convertImageNames(dirname, basename, timeFilterBefore, ...
0002     timeFilterAfter, xyFilterBefore,xyFilterAfter, channelNames )
0003 % convertImageNames : Convert image names from to NIS Elements format
0004 % The file naming convention for elements is basename_t1xy1c1.tif
0005 % where c1 is brightfield and c2,c3 etc are the fluorescent channels.
0006 % To convert using this program you need to specify what is before and
0007 % after the time in your image filename, before and after the xy position
0008 % and the channelNames used in your filenames starting with phase.
0009 % If you leave both the before and after values as '', then it sets the
0010 % value to 1 (can be used for snapshots, or a single xy position).
0011 % For example, Micromanager has the convention img_00000000t_channel.tif,
0012 % if we used BF for phase images and gfp for the 1st channel we would call
0013 % this function as following :
0014 % convertImageNames(dirname, 'img', '_', 't', '','' , {'BF','gfp'} )
0015 % which would rename img_00000001t_BF.tif to img_t1xy1c1.tif
0016 %
0017 % INPUT : dirname : directory contains micromanager images
0018 %         timeFilterBefore : string found before frame number
0019 %         timeFilterAfter : string found after frame number
0020 %         xyFilterBefore : string found before xy position number
0021 %         xyFilterAfter : string found after xy position number
0022 %         channelFilters : cell with strings for each channel name found
0023 %         in the filenames. For example {'BF','gfp', 'mcherry'} will convert *BF*
0024 %         images to *c1*, gfp to c2 and mcherry to c3 .
0025 %
0026 %
0027 % Copyright (C) 2016 Wiggins Lab
0028 % Written by Stella Stylianidou.
0029 % University of Washington, 2016
0030 % This file is part of SuperSegger.
0031 %
0032 % SuperSegger is free software: you can redistribute it and/or modify
0033 % it under the terms of the GNU General Public License as published by
0034 % the Free Software Foundation, either version 3 of the License, or
0035 % (at your option) any later version.
0036 %
0037 % SuperSegger is distributed in the hope that it will be useful,
0038 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0039 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0040 % GNU General Public License for more details.
0041 %
0042 % You should have received a copy of the GNU General Public License
0043 % along with SuperSegger.  If not, see <http://www.gnu.org/licenses/>.
0044 
0045 
0046 images = dir([dirname,filesep,'*.tif']);
0047 
0048 % directory to move original images
0049 dirOriginal  = [dirname,filesep,'original',filesep] ;
0050 imagesInOrig = dir([dirOriginal,filesep,'*.tif']);
0051 
0052 elementsTime='t';
0053 elementsXY ='xy';
0054 elementsPhase = 'c';
0055 
0056 if exist([dirname,filesep,'raw_im'],'dir') && ~isempty(dir([dirname,filesep,'raw_im',filesep,'*tif*']))
0057     disp('Files already aligned');
0058     return;
0059 end
0060 
0061 if isRightNameFormat(dirname)
0062     disp('File names in NIS-Elements format')
0063     disp('Procede to segmentation')
0064     return;
0065 end
0066 
0067 if isempty(images) && isempty(imagesInOrig)
0068     error('No image files found, please check directory');
0069     return ;
0070 end
0071 
0072 
0073 disp('File names not in Elements format : Converting..')
0074 
0075 % if the basename does not exist ask the user to input all the variables
0076 if ~exist('basename','var')
0077     basename = input('Please type the basename:','s');
0078     timeFilterBefore = input('Please type the prefix for the number of the time frame, press enter if none:','s');
0079     timeFilterAfter = input('Please type the suffix for the number of the time frame, press enter if none:','s');
0080     xyFilterBefore = input('Please type the prefix for the number of the xy position, press enter if none:','s');
0081     xyFilterAfter = input('Please type the suffix for the number of the xy position, press enter if none:','s');
0082     channelNames = input('Please type the names of the channels as {''BF'',GFP''}:');
0083 end
0084 
0085 if ~exist(dirOriginal,'dir') % make original directory
0086     mkdir(dirOriginal) ;
0087 end
0088 
0089 if isempty(imagesInOrig) % move original images
0090     movefile([dirname,filesep,'*.tif'],dirOriginal); % move all images to dir original
0091 end
0092 
0093 images = dir([dirOriginal,filesep,'*.tif']);
0094 
0095 % go through every image
0096 for j = 1: numel (images)
0097     fileName = images(j).name;
0098     %disp(fileName);
0099     
0100     
0101     if isnumeric(timeFilterBefore ) && isnumeric(timeFilterAfter)
0102         currentTime = str2double(fileName(timeFilterBefore:timeFilterAfter));
0103         if ~isnumeric((currentTime))
0104             disp (['No frame numbers found in ', fileName, ' between ' , num2str(timeFilterBefore), ' ', num2str(timeFilterAfter), '- aborting']);
0105         end
0106     else
0107         currentTime = findNumbers (fileName, timeFilterBefore, timeFilterAfter); % find out time
0108         if isempty(currentTime)
0109             disp (['time expression incorrect for filename', fileName, '- aborting']);
0110             return;
0111         end
0112     end
0113     
0114     
0115     if isnumeric(xyFilterBefore ) && isnumeric(xyFilterAfter)
0116         currentXY = str2double(fileName(xyFilterBefore:xyFilterAfter));
0117         if ~isnumeric((currentXY))
0118             disp (['No xy numbers found in ', fileName, ' between ' , num2str(xyFilterBefore), ' ', num2str(xyFilterAfter), '- aborting']);
0119             return
0120         end
0121     else
0122         currentXY = findNumbers (fileName, xyFilterBefore, xyFilterAfter); % find out xy
0123         if isempty(currentXY)
0124             disp (['xy expression incorrect for filename', fileName, '- aborting']);
0125             return;
0126         end
0127     end
0128     
0129     channelPos = [];
0130     % find out channel
0131     if isempty(channelNames) || isempty(channelNames{1})
0132         c = 1;
0133         channelPos = 1;
0134     else
0135         for c = 1:numel(channelNames)
0136             channelPos = strfind(fileName, channelNames {c});
0137             if ~isempty(channelPos)
0138                 break;
0139             end
0140         end
0141     end
0142     
0143     if isempty(channelPos)
0144         disp (['channel expression incorrect for filename', fileName, '- aborting']);
0145         return;
0146     end
0147     
0148     newFileName = [basename,elementsTime,sprintf('%05d',currentTime),elementsXY,sprintf('%03d',currentXY),elementsPhase,num2str(c)];
0149     copyfile([dirOriginal,filesep,images(j).name],[dirname,filesep,newFileName,'.tif']);
0150     
0151 end
0152 
0153 end
0154 
0155 
0156 function numbers = findNumbers (fileName, patternBefore, patternAfter)
0157 % findNumbers : finds numbers in fileName between patternBefore and patternAfter
0158 
0159 is_num_mask = ismember( fileName,'01234567890'); % 1 where there are numbers
0160 timeStart = 0 ;
0161 timeEnd = 0 ;
0162 
0163 % both empty set numbers to 1
0164 if strcmp(patternBefore,'') && strcmp(patternAfter, '') ||...
0165         isempty(patternBefore) &&  isempty(patternAfter)
0166     numbers = 1;
0167     return
0168 end
0169 
0170 % return []  if not found at all
0171 if isempty(regexp(fileName,[patternBefore,'[0123456789]+',patternAfter], 'once'))
0172     numbers=[];
0173     return;
0174 end
0175 
0176 % find starting and ending number
0177 [timeStart,timeEnd] =  regexp(fileName,[patternBefore,'[0123456789]+',patternAfter]);
0178 
0179 if isempty(timeStart) ||isempty(timeEnd)
0180     disp (['pattern was not found',patternBefore,patternAfter,fileName,'setting to 1']);
0181     numbers = 1;
0182 else
0183     extraBefore = numel(patternBefore);
0184     extraAfter = numel(patternAfter);
0185     pattern = fileName(timeStart+extraBefore:timeEnd-extraAfter);
0186     numbers = str2double(pattern);
0187 end
0188 
0189 end

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