function [neg,zer,nd,pos]=findCompartmentInteractions(E,epCmpt,uComp,Cmpt1,Cmpt2)
%[neg,zer,nd,pos]=findCompartmentInteractions(E,epCmpt,Cmpt1,Cmpt2,uComp)
%
% This function was written to find out the type of epistatic interactions
% that either exist between two compartments or within same compartment
% INPUT:
% E: epistatic interaction matrix
% epCmpt:   Compartments belonging to the gene at that index (a second
%           order cell array)
% uCompt:   Unique Compartments in the epCmpt
% Cmpt1:    First compartment
% Cmpt2 (optional): Second Compartment, first compartment (default)
% OUTPUT:
% neg:  number of aggravating interactions
% zer:  number of no-epistatic interactions
% nd:   number of non-decisive interactions
% pos:  number of buffering interactions
%

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % Written By Chintan Joshi on 7/4/2014 % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

if (nargin<5)
    Cmpt2=Cmpt1;
end
if isempty(strcmp(Cmpt1,uComp)) || isempty(strcmp(Cmpt2,uComp))
    error('One of the compartmetns do not exist, check again!!!');
end

g1=0;
g2=0;
for i=1:length(epCmpt)
    cnt1=0;% counter for existence of 1st compt
    cnt2=0;% counter for existence of 2nd compt
    for j=1:length(epCmpt{i,1})
        if strcmp(Cmpt1,epCmpt{i,1}{j,1})==1
            cnt1=cnt1+1;
        end
        if strcmp(Cmpt2,epCmpt{i,1}{j,1})==1
            cnt2=cnt2+1;
        end
    end
    if cnt1~=0 % determines if loop was ever satisfied for 1st compartment existence of the gene
        g1=g1+1;
        ind1(g1,1)=i; % if yes, store the gene index
    end
    if cnt2~=0 % determines if loop was ever satisfied for 2nd compartment existence of the gene
        g2=g2+1;
        ind2(g2,1)=i; % if yes, store the gene index
    end
end

% E1=E(ind1,:); % reduces epistasis matrix to only genes involved in these compartments
% newE=E1(:,ind2); % reduces epistasis matrix to only genes involved in these compartments

% Now vectorize epistasis matrix
c=0;
newE=[];
for i=1:length(ind1)
    for j=1:length(ind2)
        if ind1(i,1)~=ind2(j,1)
            c=c+1;
            newE(c,1)=E(ind1(i,1),ind2(j,1));
        end
    end
end
% newE=reshape(newE,size(newE,1)*size(newE,2),1);
neg=0;
zer=0;
nd=0;
pos=0;
if ~isempty(newE)
    for k=1:length(newE)
        if newE(k,1)<-0.25
            neg=neg+1; % count number of aggravating interactions
        elseif newE(k,1)>=-0.25 && newE(k,1)<0.25
            zer=zer+1; % count number of non-epistatic interactions
        elseif newE(k,1)>=0.25 && newE(k,1)<0.85
            nd=nd+1; % count number of non-decisive interactions
        else
            pos=pos+1; % count number of buffering interactions
        end
    end
end