%------------------- LineShapeKin MAIN -----------------------------
%-- version 3.0 
%
% 3/11/08
% Written by Evgenii Kovrigin, Medical College of Wisconsin
%
%  4/22/08 - check for existence of w0_B array added for compatibility of
%                   lower- and higher- level models

clear all;

global YES NO Model ;

YES=1;
NO=0;

%--- Get model- and sample-specific data and job description---
setup;

%-- find sizes of the residue list and titration series
number_residues=length(resnames);
total_titration_points=length(LPratio);

%-- check for errors in the above lists ---
if abs(length(Ptotal)-total_titration_points)>0
    error('Lengths of Ptotal[] and PLratio[] are different!!!');
end



%% Set conditions of the sample
 conditions.Ptotal=Ptotal;
 conditions.LPratio=LPratio;
 conditions.Temperature=T;
 conditions.LineWidth0=Line_Width_0;
 %-- list of editable titration points to use for specific residue
conditions. titration_points_list_fname= 'points_list.txt'; 
%-- standard file names to control estimates for A and B states R2 and w0
conditions.manual_R2_fname='manual_R2.txt';
conditions.manual_w0_fname='manual_w0.txt';
conditions.scale_factor_fname='scale_factor.txt';
conditions.results_path=results_path;
mkdir(conditions.results_path);

%% make paths for figures
conditions.figures_path=fullfile(conditions.results_path,'figures');  %-- path for sum of squares for statistical testing
mkdir(conditions.figures_path);

%% make paths for fitting results
conditions.SS_path=fullfile(conditions.results_path,'SS');  %-- path for sum of squares for statistical testing
mkdir(conditions.SS_path);

conditions.fit_results_path=fullfile(conditions.results_path,'fit_results');  
mkdir(conditions.fit_results_path);

%% Set modes for normalization, integration and baseline correction
modes.normalization=Normalization;
modes.baseline_correction=Baseline_Correction;
modes.integration_mode=Integration_mode;


%% Set a mode to plot raw data when data is read and normalized in
%   prepare_residu_data.m
modes.plot_raw_data=YES;

% default mode for final presentation of the data in display_fit_results.m
modes.plot_mode='raw+fitted';

modes.stacked_plot=Stacked_Plot;   %--- YES  to display the titration series stacked rather than overlayed
modes.percent_shift=Percent_Shift;   %-- percentage of the peak height for shifting next trace up





%--- Select fit run type ----
switch project_name
    
    case 'simulation'
        %-- inspect simulated line shapes
        modes.plot_raw_data=YES;
        res_data=prepare_residue_data(Sparky_data_path,resnames{residue_number}, ...
            conditions, modes);
     
        modes.plot_mode='simulation only'; %-- do not plot raw data 

        display_fit_results('simulation', res_data,  B_vector.starting_values,...
            B_vector.starting_values*0, 0, modes, conditions);


    case 'just display data'
        %-- inspect raw and normalized data
        res_data=prepare_residue_data(Sparky_data_path,resnames{residue_number}, ...
            conditions, modes);
        
        modes.plot_mode='raw only';   %-- do not plot simulation from initial estimates
         display_fit_results('raw', res_data,  B_vector.starting_values,...
            B_vector.starting_values*0, 0, modes, conditions);



    
    case 'single'
        %--- Fit just a single residue ----
        resname={resnames{residue_number}}; %-- a trick for same way of handling of one or many residues
        i=residue_number;
        [Raw_Data, beta, beta_error,norm] = fit_all_data(Sparky_data_path, resname, Model, conditions, modes);
        

        display_fit_results('single', Raw_Data{1},  beta, beta_error, norm, modes, conditions);
    
    case 'individual'
        %--- Fit one by one individually ----
		res_number=length(resnames);
		for i=1:res_number
            resname={resnames{i}}; %-- a trick for uniform handling of one and many residues
            
            %-- Set the  parameters individual for every residue 
            if exist('w0_B')  
                Model.w0_B = w0_B(i);
            end
            %-- refresh global model definition now
            model_setup;
            
            [Raw_Data, beta, beta_error,norm] = fit_all_data(Sparky_data_path, resname, Model, conditions, modes);
            
            display_fit_results('indiv', Raw_Data{1},  beta, beta_error, norm, modes, conditions);
        end



    %===== THIS IS NOT APPLICABLE FOR A MODEL WITH RESIDUE-SPECIFIC PARAMETERS
    case 'global'
                
		%-- Fit all together ----
		project_name='global'; %-- individual fittings ---
        [Raw_Data, beta, beta_error,norm] = fit_all_data(Sparky_data_path, resnames, Model, conditions, modes);
		res_number=length(resnames);
		for i=1:res_number
   
            display_fit_results(project_name, Raw_Data{i},  beta, beta_error, norm, modes, conditions);
        end

   
    
        
        
        
    case 'DPM'    
        
        
        %% make paths for fitting results
        conditions.DPM_path=fullfile(conditions.results_path,'DPM');  
        mkdir(conditions.DPM_path);

        
        
        switch DPM_session
            case 'single'
                %-- redefine arrays to have just one residue values in
                %     there
                resnames={resnames{residue_number}};
                w0_B=[w0_B(residue_number)];
        end
 
        %--- Fit one by one and submit to DPM individually ----
		res_number=length(resnames);
		for i=1:res_number
            resname={resnames{i}}; %-- a trick for uniform handling of one and many residues
            beta_fname=sprintf('%s_best_fit_pars.mat',resname{1});
            
            %-- set third parameter individual for every residue
            Model.w0_B = w0_B(i);
            %-- refresh global model definition now
            model_setup;
            
            
            switch DPM_mode
                
                    case 'computation'  %-- only compute SSD matrices

                        %-- fit spectral data and save fitting
                        %    parameters
                        [Raw_Data, beta, beta_error,norm] = fit_all_data(Sparky_data_path, resname, Model, conditions, modes);


                        display_fit_results('indiv', Raw_Data{1},  beta, beta_error, norm, modes, conditions);

                        save(fullfile(conditions.DPM_path,beta_fname),'beta','norm');


                    case 'analysis'   %-- analyze SSD matrices computed in the previous run
                        
                        %-- read the spectral data and fitting
                        %     results
                        modes.plot_raw_data=NO;
                        res_data=prepare_residue_data(Sparky_data_path,resnames{i}, ...
                            conditions, modes);
                        Raw_Data={res_data};
                        load(fullfile(conditions.DPM_path,beta_fname));
          
                        %-- reset third parameter individual for every residue
                        Model.w0_B = beta(B_vector.w0_B);
                        %-- refresh global model definition now
                        model_setup;

            end
            
            %--- run DPM module
            
            DPM_run(DPM_mode, Raw_Data{1}, beta, norm, DPMparams, conditions.DPM_path) ;


        end


    otherwise
        error('Wrong project_name!');
end


