%--------------------------------------------------------------------------
%Copyright 2016, Mengqiu Li

%The following code is licensed under a Creative Commons 
%Attribution-NonCommercial-ShareAlike 4.0 International Public License.

%Under the following terms:

%1) Attribution - You must give appropriate credit, provide a link to the 
%license, and indicate if changes were made. You may do so in any 
%reasonable manner, but not in any way that suggests the licensor 
%endorses you or your use. 

%Preferred method of attribution is citing or referring to the published 
%paper associated with this work: 
%Journal of the American Chemical Society, 2015, 137 (51), pp 16055–16063.

%2) NonCommercial - You may not use the material for commercial purposes.

%3) ShareAlike - If you remix, transform, or build upon the material, 
%you must distribute your contributions under the same license as the 
%original.

%4) No additional restrictions - You may not apply legal terms or 
%technological measures that legally restrict others from doing anything 
%the license permits.

%Under the above terms, you are free to:

%1) Share - copy and redistribute the material in any medium or format
%2) Adapt - remix, transform, and build upon the material

%The license in full: 
%   http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
%All the code are written for MATLAB and tested in the following
%environment:
%MATLAB Version:                            8.5.0.197613  (R2015a)
%Bioinformatics Toolbox                     Version 4.5.1 (R2015a)
%Curve Fitting Toolbox                      Version 3.5.1 (R2015a)
%Image Processing Toolbox                   Version 9.2   (R2015a)
%Optimization Toolbox                       Version 7.2   (R2015a)
%Parallel Computing Toolbox                 Version 6.6   (R2015a)
%Statistics and Machine Learning Toolbox    Version 10.0  (R2015a)
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
%Contact information:
%   Mengqiu Li (author): mengqiu.li@outlook.com
%   Lars Jeuken (group leader of the project): l.j.c.jeuken@leeds.ac.uk
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
%Data structure of 'Metadata':
%Column 1: Identifier for the experimental dataset
%Column 2: TL_number (Identifier for movie data)
%Column 3: When the reducing potential was switched on (s)
%Column 4: When the reducing potential was switched off (s)
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
%Data structure of 'BO3_Traces':

%Column 1: Identifier for the experimental dataset
%Column 2: TL_number (Identifier for movie data)
%Column 3: Vesicle_ID

%Column 4: Time (s)
%Column 5: HPTS fluorescenc ratio (raw data)

%Column 6: Intravesicular pH (converted from HPTS ratio)
%Column 7: Intravesicular pH after drift correction 
%          This column has never been used in the publication 
%          or further data analysis.

%Column 8: Proton pumping rate (proton/s)
%Column 9: Accumulated number of pumped proton from the first time point

%Column 10: Same as Column 8, with dirft correction, never used
%Column 11: Same as Column 9, with dirft correction, never used

%Note about Column 8-11: positive numbers corresponds to proton transport
%outwards, vice versa
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
%Notes about the data analysis: 

%1) Identifier for the experimental dataset:
%   This is only added for the open data project because data were usually
%   analysed individually after each dataset is accquired in the 
%   laboratory and there was no need to add an identifier. Here in the 
%   open data project, all the datasets have been combined, and the 
%   identifier becomes necessary. You will see that only part of the 
%   combined data is input into the functions because the functions were 
%   wrote without the identifier in mind.
    
%2) Potential window:
%   Most of the datasets were accquired using a potential window of
%   [100,200], meaning that between the 100th and 200th second, a reducing
%   potential was applied, providing cytochrome bo3 a reduced ubiquninol
%   pool. Some datasets used difference potential windows, but it should
%   have no effect on the data analysis because the noise level of
%   individual vesicle has been taken into account. You will see that 
%   different potential windows are used according to the experimental 
%   record/Metadata.

%3) Running this code is likely to take a (very) long time:
%   Most part of the code has been written for parallel execution, and
%   therefore the time it takes to run the code will largely depend on the
%   number of CPU cores on reasonably recent CPUs. Don't forget to 
%   start a parallel pool if MATLAB hasn't done so automatically. If you
%   don't have Parallel Computing Toolbox then you cannot benefit from the
%   parallel execution. On a moderately powered Intel Core i5-2320 3GHz 
%   quad-core CPU, analysing the whole dataset takes about 24h (never fully
%   tested). For demonstration purposes, you can modify the Metadata to
%   analyse a reduced dataset (by deleting rows in the Metadata as they 
%   are corresponding to individual movie data). The code is intended to 
%   run over-night on the data collected on that day, and a recently 
%   purchased quad-core normal desktop CPU should be enough.

%4) If you modify the code: 
%   Please pay attention to the data structure and which column of data you
%   are using. I updated the code multiple times and adapted them for the 
%   open data project, which means there are a lot of quick workarounds in 
%   the code. TL_No and Exp_ID are added on top of the old versions and pay
%   attention to those. Please also remember you must share the modified 
%   code under the same license to promote openness in the community.
%
%5) RAM usage:
%   8GB should be fine. Code was tested with 20GB.
%--------------------------------------------------------------------------

%fitting the post-potential and within-potential secionts
S=1;
E=1;
Post_Potential=zeros(1e6,10);
Within_Potential=zeros(1e6,54);
for i=1:size(Metadata,1)
    Temp_PP=open_data_fit_pp(Metadata(i,1),Metadata(i,2),BO3_Traces(BO3_Traces(:,1)==Metadata(i,1),2:end),8,Metadata(i,3:4));
    E=S-1+size(Temp_PP,1);
    Post_Potential(S:E,:)=Temp_PP;
    Temp_WP=open_data_fit_wp(Metadata(i,1),Metadata(i,2),BO3_Traces(BO3_Traces(:,1)==Metadata(i,1),2:end),8,Metadata(i,3:4),Temp_PP(:,2:end));
    Within_Potential(S:E,:)=Temp_WP;
    S=E+1;
end

Post_Potential(S:end,:)=[];
Within_Potential(S:end,:)=[];

clearvars i S E;
clearvars -regexp ^Temp_;