% SEND:
% --- P:       cell-array of protein sequences
% --- min_rep: the minimal number of repetition to detect
% --- LIMIT:   the minimal range within "min_rep" occur

% GET:
% --- DFTs:     Unique Frequant Triplets in the proteome
% --- DFTi:     Unique Frequant Triplets in each protein
% --- bool_vec: Boolean vector with 1 if a protein contains FT , otherwise 0

function [DFTs DFTi Ndfti bool_vec] = get_FTs(P, min_rep, LIMIT)

% working with Triplets (K-mer of K = 3)
K = 3;

% go over proteins
N = length(P);
% initialize
DFTs     = [];
DFTi     = cell(N,1);
bool_vec = zeros(N,1);

wb = waitbar(0,'Processing ALL Proteins, Please wait...');
for i = 1:N

    waitbar(i/N,wb);
    
    % get info about the occurances of ALL Kmers (triplets)
    [Kmer rep interval] = get_Kmer_info_in_protein(P{i}, K);

    % --- get all potential FTs (defined by min_rep - mininal # of repetitions in the current protein):
    id_fts     = find(rep >= min_rep); %5
    
    % --- check if DFT is local 
    count = 0;
    for j = 1:length(id_fts)

        % locations
        ft_loc = strfind(P{i}, Kmer{id_fts(j)});

        % check if random <==>
        Nr = length(ft_loc);
        % ---- go over every 'min_rep' repeats and check if appeared in local section 
        for k = 1:Nr-min_rep+1
            dis(k) = -(ft_loc(k) - ft_loc(k+4));   
        end

        if min(dis) > LIMIT
            count = count + 1;
            ft_del(count) = j;
        end
        
        clear dis* ft_loc

    end
    % get the fts after correcting random effect
    if count > 0
        id_fts(ft_del) = [];
    end
    clear ft*
        
    FTs     = Kmer(id_fts);
    FTs     = unique(FTs);
    
    % see if protein contains DFTs
    if ~isempty(FTs)
        bool_vec(i) = 1;
        DFTi{i} = FTs;
    end
    
    % --- aggregate the unique FTs
    DFTs    = [DFTs FTs];

    clear Kmer rep interval FTs id*

end
DFTs = unique(DFTs);
Ndfti = get_num_of_DFTi(DFTi);
close(wb);





