% SEND:
%    - PP list as cell array
%    - string of the SP that appears in each one of the PP
%    - N, the number of iteration (u think should be sufficient to converge)

% DO:
%    - Iterate through the species-count algorithm till all PPs are inconsistent with one another

% GET:
%    - PP: new set of PP that are all inconsistent with one another
%    - M:  Pair Distance Matrix of PP (as given by SP-alignment)


function [PP M] = Main(PP, SP, N)

% get Intial Consistency Match Matrix
Id = sp_pp_aline(SP, PP);

% number of iteration
for i = 1:N

    i
    
    % identify the first set of PP that are all different (those that had no-match)
    idx = union(Id(:,1), Id(:,2));
    v = 1:length(PP);
    set1 = setdiff(v,idx);
    new_PP1 = PP(set1);
    % analyse those that have match -> get the second set of new merged PPs
    [new_PP2 num_abs] = species_counting_byId_merge(SP, PP, Id);
    
    % combine the new lists (after fusion)
    PP = [new_PP1 new_PP2];
        
    % check if Consistency Matrix is empty , if so - finish, if not - re-iterate
    Id = sp_pp_aline(SP, PP);
    if isempty(Id)  
        break;
    end
    
end

% get Pair Distance Matrix (M)
[M S] = count_mutation(PP, SP);

