function [epi]=epistaticInteractions(Wxy)
%[epi]=epistaticInteractions(Wxy)
% This function finds the epistatic interactions

% INPUT:
% Wxy:  fitness of double gene deletion knock outs, must be a square matrix
% OUTPUT:
% % epi: a structure
% %   E: non-scaled epistatic interactions
% %   sE:scaled epistatic interactions

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

Wx = diag(Wxy);
Wy = Wx;
Wx=roundn(Wx,-3);
Wxy=roundn(Wxy,-3);
Wy=roundn(Wy,-3);
ix=find(Wx==0);
iy=find(Wy==0);
nanx=find(isnan(Wx)==1);
Wx(nanx)=0; Wxy(nanx,:)=0;
nany=find(isnan(Wy)==1);
Wy(nany)=0; Wxy(:,nany)=0;

% Wy=Wx;
for i=1:size(Wxy,1)
    for j=1:size(Wxy,2)
        if (Wxy(i,j) - Wx(i,1)*Wy(j,1)) > 0
            nW(i,j)=min(Wx(i,1),Wy(j,1));
        else
            nW(i,j)=0;
        end
        if Wx(i,1)>0.1 || Wy(j,1)>0.1
            E(i,j)=Wxy(i,j)-Wx(i,1)*Wy(j,1);
        else
            E(i,j)=0;
        end
%         E(i,j)=Wxy(i,j)-Wx(i,1)*Wy(j,1);
        if E(i,j)==0
            sE(i,j)=0;
        else
            sE(i,j)=(Wxy(i,j)-Wx(i,1)*Wy(j,1))/abs(nW(i,j)-Wx(i,1)*Wy(j,1));
        end
    end
end
% for i=1:size(Wxy,1)
%     for j=1:size(Wxy,2)
%         E(i,j)=Wxy(i,j)-Wx(i,1)*Wy(j,1);
%         if E(i,j) < 0
%             sE(i,j)=Wxy(i,j)/(Wx(i,1)*Wy(j,1)) - 1;
%         else
%             sE(i,j)=(Wxy(i,j)-Wx(i,1)*Wy(j,1))/abs(min(Wx(i,1),Wy(j,1))-Wx(i,1)*Wy(j,1));
%         end
%     end
% end
if ~isempty(ix)
    E(ix,:)=0;
    sE(ix,:)=0;
end
if ~isempty(iy)
    E(:,iy)=0;
    sE(:,iy)=0;
end
epi.E=E;
epi.sE=sE;