% this function takes ONE SP that was found on several PPs (in some Meta-genome). 
% PPs should be provided as a cell array of strings.

% DO:
% 1) Identify the location of the SP hit in each PP.
% 2) Aline PP pairs: such that the SP locations will concide at "Zero lag" 
%    - optional: limit the length on right/left to compare by "cut" (default = 100 AA).
%                if not specified compare the maximum possible length to the right/left of the SP location
% 3) Compares the PP pair on the righ and left:
%    - IF identical, assume the same Protein => count 1 (for species count). 
%    - IF there's a difference, assume different Proteins => count = 2.

% GET:
% pair vector of the PPs that were found identical

function Id = sp_pp_aline(SP, PPs)

Npp = length(PPs); % number of PPs
Nsp = length(SP);  % the length of the SP

% get SP hits locations & their distance(s), see below 
for i = 1:Npp
    loc(i) = strfind(PPs{i},SP);
    % distance from begining (Left)
    L(i) = loc(i) - 1;
    % distance from end (Right)
    R(i) = length(PPs{i}) - loc(i) - Nsp + 1;
end

% go over any 2 pais of the PPs (in which u have found that SP)
count = 0;
for i = 1:Npp
    
    for j = i+1:Npp

        % get the locations to compare
        Lind = min([L(i) L(j)]);
        Rind = min([R(i) R(j)]);
        
        % compare all letters to the left of the SP (that was found in each pair)
        if Lind > 0
            Xi = PPs{i}(loc(i)-Lind : L(i));
            Xj = PPs{j}(loc(j)-Lind : L(j));
            Lcmp = strcmp(Xi,Xj);
        else
            Lcmp = 1;
        end
        
        % compare all letters at the right of the SP
        if Rind > 0
            Yi = PPs{i}(loc(i)+Nsp : loc(i)+Nsp+Rind-1);
            Yj = PPs{j}(loc(j)+Nsp : loc(j)+Nsp+Rind-1);
            Rcmp = strcmp(Yi,Yj);
        else
            Rcmp = 1;
        end
        
        % if both sides are identical, this means it is the sames species
        if Lcmp == 1 && Rcmp == 1
            count = count + 1;
            Id(count,:) = [i j];
        end
        
    end
    
end

% if no match was found
if count == 0
    Id = [];
end

