%mMutualCounter_IF_Alignment.m

if isfield(handles, 'pSEQNAME')
    handles.pSEQNAME=[];%clear previous values
end

% NSeq_all    =   zeros(1,length(ChosenDomains));                                       %Optimized for Speed%
% NSeq        =   zeros(1,length(ChosenDomains));
Count(length(BPs),length(BPs),length(ListInputData(:,1)),length(ChosenDomains))   =   0;  %Optimized for Speed%
CountOrgInd(length(BPs),length(BPs),length(ListInputData(:,1)),length(ChosenDomains))   =   0;  %Optimized for Speed%
Percent(length(BPs),length(BPs),length(ListInputData(:,1)),length(ChosenDomains)) =   0;  %Optimized for Speed%
SumTotal    =   zeros(length(ListInputData(:,1)),length(ChosenDomains));                  %Optimized for Speed%
% Amm  = zeros(length(BPs),length(BPs),length(ListInputData(:,1)),length(ChosenDomains));

HitCount=0;
TotalSeqs=0;
P=ChosenDomains;
% NSeq_all(P) = DomainLimits(P+1)-DomainLimits(P);
y = DomainLimits(P)+1;
z = DomainLimits(P+1);

prepSEQNAME{length(ListInputData(:,1)),100,length(BPs),length(BPs)}=[];
pSEQNAME{length(BPs),length(BPs)}=[];
for n=1:length(ListInputData(:,1))
    if ListTable(n)==ChosenTable
        HitCount=HitCount+1;
        for a=1:length(BPs)
            for b=1:length(BPs)
                CountOrgInd(a,b,n,P)=   sum(strcmp(BPs(a,b),ListPosArray(OrgInd,:,n)));
                Count(a,b,n,P)      =   sum(strcmp(BPs(a,b),ListPosArray(y:z,:,n)));            %Optimized for Speed%
                Percent(a,b,n,P)    =   100*Count(a,b,n,P);%%%%%%%NSeq(P)
                TotalSeqs = TotalSeqs + Count(a,b,n,P);

                Amm{a,b,n,P}        =   find(strcmp(BPs(a,b),ListPosArray(y:z,:,n))); %never initiated
                for x=1:Count(a,b,n,P) %n is most important here ----- Amm{a,b,n,P}
                    prepSEQNAME(n,x,a,b) = FastaOrganismNames(Amm{a,b,n,P}(x) + DomainLimits(P));%%%NEVER INITIATED
                    prepSEQNAME(n,x,a,b) = strcat(num2str(ListInputData(n,1)),'-',num2str(ListInputData(n,2)),'__',prepSEQNAME(n,x,a,b));
                end
                for i=1:length(prepSEQNAME(n,:,a,b))
                    if ~isempty(prepSEQNAME{n,i,a,b})
                        pSEQNAME{a,b}=[pSEQNAME{a,b}; prepSEQNAME(n,i,a,b)];
                    end
                end
            end
        end
    end
end

if HitCount~=0,
    if PercentsCounts == 2,
        VALUE=sum(Count,3);
    else VALUE=sum(Percent,3)/TotalSeqs;
        VALUE=round(Rounding*VALUE)/Rounding;
    end
else VALUE=zeros(length(BPs));
    SEQNAME=[];
end

%Change the two-dimensional pSEQNAME cell array into the three-dimensional SEQNAME
if sum(sum(VALUE(:,:,:,P)))>0
    SEQNAME{max(max(max(max(Count))))*length(ListInputData(:,1)),length(BPs),length(BPs)}=[]; %Initialized (?)
    for a=1:length(BPs)
        for b=1:length(BPs)
            for x=1:length(pSEQNAME{a,b})
                SEQNAME(x,a,b)=pSEQNAME{a,b}(x);
            end
        end
    end
end
