% SEND:
% --- Kmer, interval: the Kmer found and their intervals
% GET:
% --- Lstat: "local" statistics of the Kmers based on repetative intervals

function Lstat = get_local_stat(Kmer, interval)

all_interval = [];

% total number of Kmers (DFTi)
N = length(Kmer);

% Most Frequent Interval (MFI)
for i = 1:N
    all_interval = [all_interval interval{i}];
end
MFI = mode(all_interval);

% Leading Kmer(s) at MFI - defined as the kmer with the largest number of repetitions at the most frequent interval
for i = 1:N
    nr(i) = length(find(interval{i} == MFI));
end
Lk_mfi = Kmer(find(nr == max(nr)));    % # the leading Kmer (the one with most rep at the most frequent interval)
Lk_Nrep_mfi = max(nr);                 % # of repetitions of the leading Kmer at the most frequent interval
Tot_Nkmer_mfi = length(find(nr > 0));  % TOTAL # Kmers (with min_rep) at the most frequent interval
Tot_Nrep_mfi = sum(nr);                % TOTAL # of repetitions of all Kmers (with min_rep) at the most frequent interval

% put in structure
%if Lk_Nrep_mfi >= 2 % for a specific FT to appear periodiclly
if Tot_Nrep_mfi >= 2 % for a specific interval to appear (considering all FTs)
    Lstat.MFI                  = MFI;
    Lstat.Lead_kmer_mfi        = Lk_mfi;
    Lstat.Lead_kmer_Nrep_mfi   = Lk_Nrep_mfi;
    Lstat.Tot_Nkmer_mfi        = Tot_Nkmer_mfi;
    Lstat.Tot_Nrep_mfi         = Tot_Nrep_mfi;
else
    Lstat.MFI                  = 0;
    Lstat.Lead_kmer_mfi        = 0;
    Lstat.Lead_kmer_Nrep_mfi   = 0;
    Lstat.Tot_Nkmer_mfi        = 0;
    Lstat.Tot_Nrep_mfi         = 0;
end
    
