%% FITFRAP WRAPPER Fit FRAP curve in batch mode % For Joao Firmino @ Knust Lab % Jean-Yves Tinevez - January 2010 -> November 2010 %% Parameters config % Root directory for xls files. They should be orgnized as follow: % root_dir / mutant_folders / position_folders / xls_files % Results will be group by mutant type, then by position. root_dir = '/Users/tinevez/Projects/Data/JFirmino'; % root_dir = '/Volumes/ipf/Dropbox/Joao Firmino/FRAP excel files mix'; % The xls file that should contain cutoffs and comments for each xls file % It should be in the root_dir master_file = 'master2.xls'; % Save directory. It will be created and the structure of the rood dir will % be copied, with results and figures in it. save_root = '/Users/tinevez/Projects/Data/JFirmino/Results'; % save_root = '/Volumes/ipf/Dropbox/Joao Firmino/Results_for_FRAP_mix'; % Wildcardfor file extension to look for file_wildcard = '*.xls'; % Draw and save fit figures? show_figure = false; x_label = 'Time (s)'; % label for x axis y_label = 'Normalized intensity'; % Draw and save boxplots? do_boxplots = false; % Do box plots on what? boxplot_fields = { 'A' 'tau' 'A1' 'A2' 'tau1' 'tau2' }; display_boxplot_opt.PlotStudentTest = false; display_rawdata_opt.AddMeanToPlot = true; % Fit by single exponential? fit_single_exp = true; % Starting value for tau tau_start = 30; % Fit by two exponentials? fit_two_exp = true; % Starting values for taus tau1_start = 3; tau2_start = 300; % Tolerance over beginning of recovery, for fitting tolerance = 0.1; % Column names to look for in xls file. The first columns with these names % will be taken and used for fitting. X_name = 'time'; Y_name = 'normalized'; % Curent version of this script version = 0.2; %% Setup warning('off','MATLAB:MKDIR:DirectoryExists') mkdir(save_root) %% Start clc disp( [ 'FITFRAPWRAPPER version ' num2str(version) ] ) disp(' ') %% Open and parse master file master_file_path = fullfile(root_dir, master_file); col1_name = 'filename'; col2_name = 'cutoff'; [ junk1 junk2 raw] = xlsread(master_file_path); headers = raw(1, :); % Read filenames col1 = strmatch(col1_name,headers,'exact'); col1 = col1(1); master_filenames = raw(2:end,col1); % Read cutoffs col2 = strmatch(col2_name,headers,'exact'); col2 = col2(1); master_cutoffs = raw(2:end,col2); %% Parse folders % Get all mutant folders mutant_folders = cleanFolderList(dir(root_dir)); mutant_folders_names= {mutant_folders.name}; n_mutant_folders = numel(mutant_folders); disp([ int2str(n_mutant_folders) ' types found.']) disp(' ') % Prepare result storage master_result = struct(); % Prepare x axis for boxplot x_boxplot = []; x_boxplot_index = 0; % Loop over mutant folders for mutant_index = 1 : n_mutant_folders current_mutant = mutant_folders_names{ mutant_index }; disp(current_mutant) % Get all position folders for this mutant current_mutant_folder = fullfile(root_dir,current_mutant); position_folders = cleanFolderList(dir(current_mutant_folder)); position_folders_names = {position_folders.name}; n_position_folders = numel(position_folders); % Loop over each position folder for position_index = 1 : n_position_folders current_position = position_folders_names{ position_index }; fprintf(' %s', current_position) % Get all xls files within this folder current_position_folder = fullfile(current_mutant_folder,current_position); xls_files = dir( fullfile(current_position_folder,file_wildcard)); xls_files = cleanFileList( xls_files ); xls_files_names = { xls_files.name }; n_xls_files = numel(xls_files); fprintf('\t%2i files found.\n',n_xls_files) % If empty, we skip if n_xls_files == 0 continue end % Prepare result structure result_template.Filename = ''; result_template = prepareSingleExpResult(result_template,[],[]); result_template = prepareDoubleExpResult(result_template,[],[]); position_results = repmat(result_template, [n_xls_files , 1] ); % Create save dir save_dir_position = fullfile(save_root,current_mutant,current_position); mkdir(save_dir_position) % Loop over each file for file_index = 1 : n_xls_files current_file = xls_files_names{ file_index }; current_file_path = fullfile(current_position_folder,current_file); fig_title = current_file( 1 : find(current_file == '.',1,'last')-1); fprintf(' %s\n',current_file) % Check if the file is listed in the master [pathstr, short_name, ext] = fileparts(current_file); master_index = find( strcmp(short_name, master_filenames) ); if numel(master_index) < 1 fprintf(' !! Not found in master file, skipping.\n') continue end if numel(master_index) > 1 fprintf(' !! Found %d times in master file, skipping.\n', numel(master_index)) continue end % Get cutoff time cutoff = master_cutoffs{master_index}; if ischar(cutoff) if strcmp(cutoff, 'ok') cutoff = inf; elseif strcmp(cutoff, '-') fprintf(' -- Flagged as bad data, skipping.\n'); continue else cutoff = str2double(cutoff); end end fprintf(' ++ Cutoff time: %.0f s\n', cutoff); % Extract data from xls file [X Y] = getColumnsFromXLS(current_file_path,X_name,Y_name); % Get rid of NaNs is_x_nan = isnan(X); is_y_nan = isnan(Y); X ( is_x_nan | is_y_nan ) = []; Y ( is_x_nan | is_y_nan ) = []; % Find end of interesting part, before the slow decay at the end of the % experiment. if cutoff == inf i_end = numel(Y); else i_end = find( X > cutoff, 1, 'first'); end % Check if we have the begining of recovery i_begin = find ( Y == 0, 1, 'first'); if isempty(i_begin) warning('MATLAB:fitfrapwrapper:BoundNotFound',... 'Could not find the beginning of recovery (In=0), skipped') continue end % Prepare massaged-data Xf = X(i_begin : i_end); Yf = Y(i_begin : i_end); Xb = X(1:i_begin-1); Yb = Y(1:i_begin-1); Xa = X(i_end+1:end); Ya = Y(i_end+1:end); % Prepare figure if needed if show_figure Xb = X(1:i_begin-1); Yb = Y(1:i_begin-1); Xa = X(i_end+1:end); Ya = Y(i_end+1:end); hf = figure; title(fig_title,'Interpreter','none') ha = cla; hold(ha,'on'); set(ha,'TickDir','out') plot(ha,Xb,Yb,'.','Color',[0.3 0.3 0.3]) plot(ha,Xa,Ya,'.','Color',[0.3 0.3 0.3]) plot(ha,Xf,Yf,'k.') xlabel(x_label) ylabel(y_label) end % Update data structure current_file_data.Filename = fig_title; % Fit single exponential exp1_fit = []; gof1 = []; if fit_single_exp % Do fit [exp1_fit gof1] = fitSingleExponential(Xf,Yf,tau_start,tolerance); % Draw figure if show_figure plot(Xf,feval(exp1_fit,Xf),'r','LineWidth',1.5) y_plot_range = get(ha,'YLim'); max_y_plot = y_plot_range(2); text_handle1 = text(0.3*X(end),max_y_plot,{ [ '\tau = ' sprintf('%.1f s',exp1_fit.tau) ] sprintf('A = %.2f',exp1_fit.A) sprintf('R^2 = %.2f',gof1.rsquare) }); set(text_handle1,'FontName','FixedWidth','VerticalAlignmen','top','Color','r') end end current_file_data = prepareSingleExpResult(current_file_data,exp1_fit,gof1); % Fit two exponentials exp2_fit = []; gof2 = []; if fit_two_exp % Do fit [exp2_fit gof2] = fitDoubleExponential(Xf,Yf,tau1_start,tau2_start,tolerance); % Draw figure if show_figure plot(Xf,feval(exp2_fit,Xf),'b','LineWidth',1.5) text_handle2 = text(0.6*X(end),max_y_plot,{ [ '\tau_1 = ' sprintf('%.1f s',exp2_fit.tau1) ] sprintf('A_1 = %.2f',exp2_fit.A1) [ '\tau_2 = ' sprintf('%.1f s',exp2_fit.tau2) ] sprintf('A_2 = %.2f',exp2_fit.A2) sprintf('R^2 = %.2f',gof2.rsquare) }); set(text_handle2,'FontName','FixedWidth','VerticalAlignmen','top','Color','b') end end current_file_data = prepareDoubleExpResult(current_file_data,exp2_fit,gof2); % Update result struct position_results(file_index) = current_file_data; % Save figure if needed if show_figure set(ha,'YLim',[0,max_y_plot]) fig_figure_path = fullfile(save_dir_position,[fig_title '.fig']); png_figure_path = fullfile(save_dir_position,[fig_title '.png']); saveas(hf,fig_figure_path) saveas(hf,png_figure_path) close(hf) end end % for file_index = 1 : n_xls_files % Save results % Put them in struct of array position_results = arrayst2starray(position_results); % Save them in 1 mat file per position save_dir_mutant = fullfile(save_root,current_mutant); mat_file_path = fullfile(save_dir_mutant,[current_position '.mat']); save(mat_file_path,'-struct','position_results') % Store them in the master sheet_name = [ current_mutant '_' current_position ]; master_result.(sheet_name) = position_results; % Update x boxplot x_boxplot_index = x_boxplot_index + 1; x_boxplot = [ x_boxplot ; x_boxplot_index]; %#ok disp(' ') end % for position_index = 1 : n_position_folders % Increment x boxplot x_boxplot_index = x_boxplot_index + 1; disp(' ') end % for mutant_index = 1 : n_mutant_folders %% After processing all files, save % Save master to mat file master_mat_file_path = fullfile(save_root,'fitresults.mat'); save(master_mat_file_path,'-struct','master_result'); % Generate master csv files fnames = fieldnames(master_result); for fn = fnames' f = fn{1}; % Create one csv file per position csv_file_path = fullfile(save_root,[ f '.csv']); sheet_name = f; ezwrite(csv_file_path,master_result.(f)) end % Generate box plots if do_boxplots % Get screen size set(0,'Units','pixels') scnsize = get(0,'ScreenSize'); scnsize(3) = scnsize(3)/2; % If you have dual monitor hf = figure('Position',0.8.*scnsize); x_boxplot = num2cell(x_boxplot); % A bit trickier: we have to loop on sub-sub-struct and categorize on % sub-struct for bf = boxplot_fields b = bf{1}; clf(hf) % Now loop over categories to collect data fnames = fieldnames(master_result); y_boxplot = cell(numel(fnames),1); p_labels = cell(numel(fnames),1); for i = 1 : numel(fnames) f = fnames{i}; % Create one csv file per position y_boxplot{i} = master_result.(f).(b); p_labels{i} = master_result.(f).Filename; end fit_stats = plotstats(x_boxplot,y_boxplot, ... 'SeriesLabels',fnames,... 'PointLabels',p_labels,... 'YLabel',b,... 'PlotBoxOptions',display_boxplot_opt,... 'PlotRawDataOptions',display_rawdata_opt,... 'FigureHandle',hf ); set(fit_stats.Plot.AxesRawData,'TickDir','out'); % ht = rotateandscalelabels(fit_stats.Plot.AxesRawData,90,12); % set(ht,'Interpreter','none') set(fit_stats.Plot.AxesBox ,'TickDir','out'); % ht = rotateandscalelabels(fit_stats.Plot.AxesBox,90,12); % set(ht,'Interpreter','none') htext = annotation('textarrow',[0.1 0.11],[0.5 0.5]); set(htext, ... 'String',['Statistics for ' b],... 'TextRotation',90,... 'HorizontalAlignment','center', ... 'LineStyle','none',... 'Position',[0 0.5 0.1 1],... 'FontSize',12); % Save figure fig_stat_figure_path = fullfile(save_root,['stats_' b '.fig']); png_stat_figure_path = fullfile(save_root,['stats_' b '.png']); saveas(hf,fig_stat_figure_path) saveas(hf,png_stat_figure_path) % Save stats stats_path = fullfile(save_root,['stats_' b '.mat']); save(stats_path,'-struct','fit_stats') end close(hf) end