% SEND:
% --- P:       a cell array contaning protein sequences
% --- Info:    a cell array of the same size of P contaning information about each protein
% --- K:       the degree of K-mer
% --- min_rep: the minimal number of repetitions of (any) Kmer byond which the Kmer is defined as "Frequent"
% --- LIMIT:   the AA section with which the minimal number of repeats of a single FT must occur

% GET:
% --- Stat: all the statistics about the apperances of the Kmers at level K

function [OUT RES] = Main(P, Info, min_rep, LIMIT)

OUT{1,1}  = 'Index';
OUT{1,2}  = 'Info';
OUT{1,3}  = 'Length';
% global
OUT{1,4}  = 'nS1';
OUT{1,5}  = 'nS3';
OUT{1,6}  = 'Tot_Kmer (DFTi)';
OUT{1,7}  = 'Tot_Rep (all DFTi)';
OUT{1,8}  = 'Lead_Kmer';
OUT{1,9}  = 'Lead_Kmer_Rep';
% local (based on frequent Kmer)
OUT{1,10}  = 'MFI';
OUT{1,11} = 'Lead_Kmer_mfi';
OUT{1,12} = 'Lead_Kmer_Rep_mfi';
OUT{1,13} = 'Tot_Kmer_mfi';
OUT{1,14} = 'Tot_Rep_mfi';
OUT{1,15} = 'RP';
OUT{1,16} = 'RC';


% FIRST STEP
% --- FIND all Frequent Triplets (FTs) in each of the proteins
[DFTs DFTi Ndfti is_co] = get_FTs(P, min_rep, LIMIT);
% --- GET the Compositional Order (CO) set
co_id    = find(is_co == 1);
co_seq   = P(co_id);
co_info  = Info(co_id);
co_DFTi  = DFTi(co_id);
co_Ndfti = Ndfti(co_id);

% to save in RES
RES.Nco = length(co_id);
RES.Nno = length(find(is_co == 0));
RES.CO_id   = co_id;
RES.CO_info = co_info';
RES.CO_seq  = co_seq';
RES.CO_DFTi = co_DFTi;
RES.CO_DFTs = DFTs;


% SECOND STEP
% --- analysis/statistics of each CO protein
Nco = length(co_id);
all_intervals = [];

wb = waitbar(0,'Analyzing CO set, Please wait...');
for i = 1:Nco
    
     waitbar(i/Nco,wb);
     
    % Protein length
    Li = length(co_seq{i});

    % GET information for each DFTi: repetitions locations and intervals in the protein
    [repeats locations intervals] = get_DFTi_info(co_seq{i}, co_DFTi{i});
    % --- collect all inetrvals from all FTs from all proteins
    for p = 1:length(intervals)
        all_intervals = [all_intervals intervals{p}];
    end

    RES.FT_num_repeat{i,1} = repeats;
    RES.FT_intervals{i,1}  = intervals;
    RES.FT_locations{i,1}  = locations;
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%
    %%% GET "global" stat %%%
    %%%%%%%%%%%%%%%%%%%%%%%%%
    % --- the leading FT and its number of repetitions
    Gstat = get_global_stat(co_DFTi{i}, repeats);
    % --- Entropy measures
    [Kmer1 rep1 int1] = get_Kmer_info_in_protein(co_seq{i}, 1); % K=1
    S1 = get_entropy(rep1, Li, 1);
    [Kmer3 rep3 int3] = get_Kmer_info_in_protein(co_seq{i}, 3); % K=3
    S3 = get_entropy(rep3, Li, 3);
  
    
    %%%%%%%%%%%%%%%%%%%%%%%%%
    %%% GET "local" stat %%%
    %%%%%%%%%%%%%%%%%%%%%%%%%
    Lstat = get_local_stat(co_DFTi{i}, intervals);
    % --- get relative periodicity
    RP = Lstat.Tot_Nrep_mfi./sum(repeats);
    % --- get relative coverage of the FTs in a protein
    RC = get_RC(co_seq{i}, co_DFTi{i});

    
    % Organize the output cell array
    % Name/ID
    OUT{i+1,1}  = co_id(i);
    OUT{i+1,2}  = co_info{i};
    OUT{i+1,3}  = length(co_seq{i});
    % --- entropy statistics
    OUT{i+1,4}  = S1.Norm_Ent;
    OUT{i+1,5}  = S3.Norm_Ent;
    % --- global stat
    OUT{i+1,6}  = Gstat.Tot_Kmer;
    OUT{i+1,7}  = Gstat.Tot_rep;
    OUT{i+1,8}  = Gstat.Lead_Kmer;
    OUT{i+1,9}  = Gstat.Lead_Kmer_rep;
    % --- local statistics (based only on FTs)
    OUT{i+1,10}  = Lstat.MFI;
    OUT{i+1,11} = Lstat.Lead_kmer_mfi;
    OUT{i+1,12} = Lstat.Lead_kmer_Nrep_mfi;
    OUT{i+1,13} = Lstat.Tot_Nkmer_mfi;
    OUT{i+1,14} = Lstat.Tot_Nrep_mfi;
    % --- Relative Periodicity (RP)
    OUT{i+1,15} = RP;
    % --- Relative Coverage    (RC)
    OUT{i+1,16} = RC;

    clear Kmer* rep* int* interval* location* S* Gstat Lstat RC RP
    
end

% save
RES.all_intervals = all_intervals;

% -----
close(wb);
