%% IDAP: NMR LINE SHAPE ANALYSIS
% by Evgenii Kovrigin, 06/7/2010
% Medical College of Wisconsin
%
% This is a control file for 'fit_line_shape_data.m' with all parameters
% and settings.
%


% clean up
close all
clear classes
clear all

%%  USER INPUT


% TO DO HERE
% Introduce checking of histogram and parameter correlations request for
% matching the number of parameters in the model

%% ====  PARAMETERS OF A TITRATION SERIES ==== 
% DESIGN 
% Here we use two samples with equal concentration of the receptor but
% different concentration of a ligand: (sample 1) no ligand, (sample 2)
% maximum concentration of a ligand. Titration series is prepared by
% transfer of small aliquots of the sample 2 to the sample 1. Both samples
% are matched for salts, buffer and pH, therefore the mixing only leads to
% different concentration of a ligand thus different L/R ratio.
% This is the most robust titration design eliminating contribution of pH
% jumps, or other components such as DMSO added with the ligands (DMSO is
% added to sample 1 in equal quantity as to sample 2).
%
% The two initial solutions are prepared by splitting a receptor sample in
% half and addition of the ligand stock to the sample 2 and ligand buffer
% (buffer used for dissolution of the ligand stock) in the sample 1.
%
%  The series includes these two solutions as additional titration
%  points.

setup_parameters.titration_type='DIRECT_MIXING'; % SEQUENTIAL/DIRECT_MIXING/INDEPENDENT_SOLUTIONS


setup_parameters.titration_series_name='CaAc_directmix';


% Protein concentration, molar, prior to splitting the solution into two aliquots to
% make A and B  solutions
setup_parameters.titration_design.C_R_0=0.8e-3;
% Absolute uncertainty 
setup_parameters.titration_design.C_R_0_std=0.05*setup_parameters.titration_design.C_R_0;


% Volume of A and B samples, in Liters, after splitting and prior to addition of ligand
% to B and buffer to A
setup_parameters.titration_design.V_R_0=200e-6;
% Absolute uncertainty 
setup_parameters.titration_design.V_R_0_std=5e-6;


% An aliquot of the concentrated ligand stock, in Liters, added to the protein to make 
%  the sample B. The same volume of buffer is added
%  to another protein portion to make the sample A
%  (no ligand)
setup_parameters.titration_design.V_L_0=10e-6;  
% Absolute uncertainty 
setup_parameters.titration_design.V_L_0_std=0.1e-6;  
          

% Concentration of the concentrated ligand stock, molar
setup_parameters.titration_design.C_L_0=0.31;   
% Absolute uncertainty 
setup_parameters.titration_design.C_L_0_std=0.05*setup_parameters.titration_design.C_L_0;


% Aliquots of the sample B added to the sample A added one after another,
% in Liters:  NOTE: this must be a VECTOR!
setup_parameters.titration_design.V_B_aliquots= [
    5.38 
    5.67 
    12.28 
    13.73 
    32.94 
    140.00 
    ]' *1e-6;       
% Absolute uncertainty (assume 5% of each value)
setup_parameters.titration_design.V_B_aliquots_std=0.05*setup_parameters.titration_design.V_B_aliquots;




%% ====  SPECTRAL DATA SOURCE ==== 
%  folders created by IDAP 1D NMR Sparky extension
setup_parameters.data_path='Data_for_IDAP';


%% === BASELINE AND NORMALIZATION MODES ====
setup_parameters.method='integration';
% Baseline correction may be: (1) 'no_baseline' - no correction, (2)
% 'baseline_offset' - constant offset, or (3) 'linear_baseline' - linear
% baseline correction from first to last points of the dataset reset to 0
setup_parameters.option='no_baseline';
% setup_parameters.option='baseline_offset';
% setup_parameters.option='linear_baseline';

%% ==== FITTING MODEL ====
setup_parameters.model='U_model_1D_LR_LB_SF';
% U-model-LRcorrection parameters
% 'Rtotal', 'LRratio', 'log10(K_A)', 'k_2_A', 'w0_1', 'w0_2', 'FWHH_1', 'FWHH_2','ScaleFactor', 'LRcorrection', 'LB_a', 'LB_b'
setup_parameters.model_numeric_solver='analytical';
setup_parameters.model_numeric_options='none';


%  LINK PARAMETERS  
% Look up parameter serial numbers in the model using the model definition
% We link parameters if we want to have them identical values in the
% models/datasets. Being adjustable or constant is set up separately later.

% Link parameters between datasets constituting a series. Example: [ 3 ] -
% parameter 3 of dataset 1 will be linked to parameter 3 of dataset 2 and
% so on. This operation is done intependently for each series in a fitting
% session.
%  setup_parameters.link_within_series=[    3 4 5 6 7 8   10];
setup_parameters.link_within_series=[    3 4 5 6 7 8 9  10];

% Link thermodynamic and kinetic parameters BETWEEN all series for a global fit
% setup_parameters.parameters_to_link=[ 3 4 10]; 
setup_parameters.parameters_to_link=[ 3 4 9 10]; 

% Link parameters INSIDE each dataset (to keep them equal during fitting).
% Performed for all datasets in all series
setup_parameters.paired_model_parameters=[ 
        
    ];

%% ==== FITTING MODE AND A DATASET SELECTION  ====

% ---- Fitting modes
% setup_parameters.Fitting_mode='show-data-only';
setup_parameters.Fitting_mode='single';
%   setup_parameters.Fitting_mode='one-by-one';
% setup_parameters.Fitting_mode='global';
% setup_parameters.Fitting_mode='re-export-previous-results';


% Setting for 'one-by-one', "single" and "show-data-only" mode
 setup_parameters.all_dataset_names_fname='all_dataset_list.txt'; % contain all residues you will work on one by one
% setup_parameters.all_dataset_names_fname='dataset_list.txt'; % contain all residues you will work on one by one
% setup_parameters.all_dataset_names_fname='fit_with_errors.dataset_list.txt'; % all residues with decent fits


% Settings for the "single" mode
setup_parameters.selected_residue_number=1;

% Settings for "global" fitting mode
setup_parameters.global_project_name='Global';
setup_parameters.global_dataset_names_fname='all_dataset_list.txt'; % contain all residues you want to fit together

% A switch for fit-only or fit with error analysis
setup_parameters.fit_mode='fit_only';  
%  setup_parameters.fit_mode='fit_with_error_analysis';

% CHOOSE FITTING ALGORITHMS

%   Algorithm for one-time fitting (NOTE: this algorithm may be as
%   time-consuming as you like)
%setup_parameters.fit_only_algorithm='Newton-active-set';
%setup_parameters.fit_only_algorithm='Newton-interior-point';
% setup_parameters.fit_only_algorithm='simplex';
% setup_parameters.fit_only_algorithm='GlobalSearch';

% setup_parameters.fit_only_algorithm='MultiStart';
% setup_parameters.MultiStart_starting_points=200;

setup_parameters.fit_only_algorithm='DirectSearch';



% Algorithms for Monte-Carlo trials to estimate mutual correlation of
% parameters (NOTE: these better be simpler and faster algorithms. Since
% the data to be fit is synthetic, Newton's may be a good compromise but
% may also get stuck giving unrealistic small confidence intervals.
%setup_parameters.fit_MonteCarlo_algorithm='Newton-active-set';
%setup_parameters.fit_MonteCarlo_algorithm='Newton-interior-point';
setup_parameters.fit_MonteCarlo_algorithm='simplex';



% Settings for 're-export-previous-results' mode

% A unique suffix for the folder to create
setup_parameters.session_type='Monte_Carlo' ;
% A path to a session to export results from
setup_parameters.previous_session_path='Global/Global.U_model_1D_LR_LB_SF.global.Fit.MonteCarlo/session.mat';

%% ==== STATISTICAL ANALYSIS SETTINGS ====
% Cutoff on evidence ratio to make conclusions about one model being
% superior than another
setup_parameters.Significant_Evidence_Ratio=20;
setup_parameters.statistical_report_fname='AIC_report.txt';

%%  ==== INITIAL PARAMETERS ==== 

%  EQUILIBRIUM CONSTANT FOR BINDING
setup_parameters.log10_K_A=3;
% absolute limits
setup_parameters.log10_K_A_min=1;
setup_parameters.log10_K_A_max=7;
% Monte-Carlo search range
setup_parameters.log10_K_A_Monte_Carlo_min=2;
setup_parameters.log10_K_A_Monte_Carlo_max=5;



% RATE CONSTANT FOR DISSOCIATION
setup_parameters.k_2_A=3000;
% absolute limits
setup_parameters.k_2_A_min=10;
setup_parameters.k_2_A_max=5000;
% Monte-Carlo search range
setup_parameters.k_2_A_Monte_Carlo_min=100;
setup_parameters.k_2_A_Monte_Carlo_max=1000;


% CHEMICAL SHIFT ESTIMATES
% If you use 'manual', the following lines set the values. In 'auto' mode
% the values are estimated from fitting the lorentzians to initial and
% final traces.
% setup_parameters.W0_estimate_mode='manual';
setup_parameters.W0_estimate_mode='auto';

% manual settings (ignored in 'auto' mode)
% must be an array: separate value for each titration series !
setup_parameters.W0_1=[ 0 1 2 3 ] ;
setup_parameters.W0_2=[ 10 20 30 40 ];

% These are the fractions to obtain min and max limits as:
% min=value*(1-fraction) and max=value*(1+fraction)
setup_parameters.W0_1_limits_fraction=0.005;
setup_parameters.W0_2_limits_fraction=0.005;
setup_parameters.W0_1_Monte_Carlo_fraction=0.001;
setup_parameters.W0_2_Monte_Carlo_fraction=0.001;


% LINE WIDTH ESTIMATES
% If you use 'manual', the following lines set the values. In 'auto' mode
% the values are estimated from fitting the lorentzians to initial and
% final traces
% setup_parameters.FWHH_estimate_mode='manual';
setup_parameters.FWHH_estimate_mode='auto';

% manual settings (ignored in 'auto' mode)
% must be an array: separate value for each titration series !
setup_parameters.FWHH_1=[ 100 101 102 103];
setup_parameters.FWHH_2=[ 1000 2000 3000 4000];

% The values will be obtained from spectral slices so we
% only need to set up a range of variations for monte-carlo simulations.
setup_parameters.FWHH_1_limits_fraction=0.5;
setup_parameters.FWHH_2_limits_fraction=0.5;
setup_parameters.FWHH_1_Monte_Carlo_fraction=0.2;
setup_parameters.FWHH_2_Monte_Carlo_fraction=0.2;




% SCALING FACTOR 
% (adjustable but does not require determination of
% uncertainty range
setup_parameters.ScaleFactor=0.4e3;
% absolute limits
setup_parameters.ScaleFactor_min=0.1;
setup_parameters.ScaleFactor_max=1e4;


% L/R CORRECTION
% a factor taking into account systematic error due to lack of knowledge of
% concentrations of R and L
setup_parameters.LRcorrection=1;
% absolute limits
setup_parameters.LRcorrection_min=0.1;
setup_parameters.LRcorrection_max=10;
% Monte-Carlo search range
setup_parameters.LRcorrection_Monte_Carlo_min=0.7;
setup_parameters.LRcorrection_Monte_Carlo_max=1.4;


% LINEAR BASELINE CORRECTION A+Bx

% zeroth order
setup_parameters.LB_a=0;
% absolute limits
setup_parameters.LB_a_min=-1000;
setup_parameters.LB_a_max=1000;
% Monte-Carlo search range
setup_parameters.LB_a_Monte_Carlo_min=-1;
setup_parameters.LB_a_Monte_Carlo_max=1;

% first order
setup_parameters.LB_b=0;
% absolute limits
setup_parameters.LB_b_min=-1000;
setup_parameters.LB_b_max=1000;
% Monte-Carlo search range
setup_parameters.LB_b_Monte_Carlo_min=-1;
setup_parameters.LB_b_Monte_Carlo_max=1;

%% ===== PARAMETER MODES: CONSTANT/VARIABLE  0/1
% Define here modes for fitting parameters. The algorithm will perform
% consequtive fitting steps every time starting with the best-fit
% parameters from the previous run. Every next time it takes next line from
% the parameter_modes array to define variable parameters for fitting. This
% is a mechanism to allow gradual racheting complexity of the fitting
% problem so to most quickly approach the global minimium.
setup_parameters.parameter_modes=[ 

 %   0 0  1 1 0 1 0 0 1 0 0 0
   0 0  1 0 0 1 0 0 1 0 0 0
%   0 0  1 1 0 1 0 0 1 0 1 0
%   0 0  1 1 0 1 0 0 1 0 1 1
   
    ];

%% ===== MONTE-CARLO SETTINGS ======


% OPERATION MODE FOR MONTE-CARLO RUNS

setup_parameters.vary_starting_parameters='no';  % 'yes'/'no' - turns on variation of starting parameters in
                                                  % Monte-Carlo runs
                                                  % withing the MC
                                                  % intervals set above.

setup_parameters.cluster_mode= 'single_cluster'; setup_parameters.monte_carlo_monitoring='BRIEF';
% setup_parameters.cluster_mode= 'multiple_cores';  setup_parameters.monte_carlo_monitoring='BRIEF';

% For debugging purposes you can use SEE-ALL-FIGURES mode on a single core.
% You will see starting conditions and final results of every Monte-Carlo
% run. The program will be stopped with 'keyboard' so you can examine any
% variables you like. To continue type 'return' on MATLAB command line, to
% stop 'dbquit'.
%
% setup_parameters.cluster_mode= 'single_cluster'; setup_parameters.monte_carlo_monitoring='SEE-ALL-FIGURES';

setup_parameters.multi_cluster_run_folders={...
            '/Users/kovrigin/TotalFit/runs/ek_mac_1',...
            '/Users/kovrigin/TotalFit/runs/ek_mac_2'
            };

% setup_parameters.multi_cluster_run_folders={...
%             '/Users/kovrigin/TotalFit/runs/ek_mac_1',...
%             '/Users/kovrigin/TotalFit/runs/ek_mac_2'};
        
% Set criterion for convergence of confidence intervals
setup_parameters.fraction_conf=0.15;

%% ===== FILTERING BEST-FIT RESULTS FOR CONFIDENCE INTERVAL CALCULATION
setup_parameters.exclide_results_at_limits=1;  % 1 - is NOT to use the runs with values reaching
% absolute limits into determination of confidence intervals

setup_parameters.not_exceed_SS_times=3;  % This value controls whether to include the
       % a particular Monte-Carlo best-fit result into the pool of
       % all_filtered_final_parameters for confidence intervals evaluation.
       % Any result with SS < best_fit_SS*not_exceed_SS_times is used and
       % whatever exceeds is ignored.

%% ===== HISTOGRAMS  ==== 
setup_parameters.histogram_bins=20;
% Plot these histograms in global fits (as numbered in adjustable_parameters_only.txt)
% setup_parameters.specified_parameters=[ 1 2  3 4 5  ];
setup_parameters.specified_parameters=[ 1 2  3 4   ];

%% ===== PARAMETER CORRELATIONS ==== 
% To show only selected plots in multi-residue fitting session
% (otherwise all correlations are plotted automatically)
% NOTE: for parameters that are NOT LINKED BETWEEN SERIES
% (that is not common for all datasets) not all
% correlations will be shown for a multi-residue series.
% You can always plot desired parameter correlation using
% session.plot_correlation(parameter_number1,parameter_number2) command
% where parameter_number is the serial number from fit-vector
% (shown in 'adjustable_parameters_only.txt')

% setup_parameters.specified_parameter_pairs=[
%     1 2
%     1 3
%     1 4
%     1 5
%     2 3
%     2 4
%     2 5
%     3 5
%     4 5
%    ];
setup_parameters.specified_parameter_pairs=[
    1 2
    1 3
    1 4
    2 3
    2 4
   ];

%% ===== MISCELLANEOUS ==== 
% Visual offset in a series graph
setup_parameters.Y_visual_offset=33;


% ========  END OF USER INPUT  ======


%% Run fitting
line_shape_1D_fitting_modules.choose_fitting_mode(setup_parameters);


