%%%%%%%%%%%%%%%%%%%%    network      %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%      Signaling-gene        %%%%%%%%%%%%%%
clear;
[~,gene]=xlsread("C:\Users\27226\Desktop\sMFE\LUAD\ESCA_case.xlsx");
mcase=csvread("C:\Users\27226\Desktop\sMFE\LUAD\selectLUAD_case");
mcont=csvread("C:\Users\27226\Desktop\sMFE\LUAD\selectLUAD_control");
A=[0,106,230,269,328,390,400,421];
fid=fopen('C:\Users\27226\Desktop\sMFE\LUAD\LUAD_network_TA.txt');
[network,total]=signet(fid);
sMFE=csvread("C:\Users\27226\Desktop\sMFE\LUAD\LUAD_sMFE");
for i=1:total
    e=length(network{i});
    sMFE(i,:)=sMFE(i,:)/log2(e);
end
t=0;clear a g b;s=6;
for i=A(s)+1:A(s+1)
    tmpx=squeeze(sMFE(:,i));
    [stmpx,id]=sort(tmpx,'descend');
     t=t+1;
     a(:,t)=id(1:floor(total*0.05));
end
gsize=size(a);k=gsize(1);b(1:gsize(1),1)=a(:,1);b(1:gsize(1),2)=1;
for i=2:gsize(2)
    for j=1:gsize(1)
        c=a(j,i);
        for l=1:k
            if(b(l,1)==c)
                b(l,2)=b(l,2)+1;break;
            end
        end
        if l==k&&b(l,1)~=c
           k=k+1; b(k,1)=c;b(k,2)=1;
        end
    end
end
b=sortrows(b,-2);
bsize=size(b);
%%%%%%%  non-differential
k=0;
for i=1:bsize(1)
    if(ttest2(mcase(str2num(network{b(i,1)}{1}),:),mcont(str2num(network{b(i,1)}{1}),:))==0)
        k=k+1;
        B(k,1)=b(i,1);B(k,2)=b(i,2);
    end
end
%%%%%%%  optimistic//pessimistic
for i=A(1)+1:A(7+1)
    tmpx=squeeze(sMFE(:,i));
    [stmpx,id]=sort(tmpx,'descend');
     SLE(:,i)=id(1:floor(total*0.05));
end
surv=xlsread("C:\Users\27226\Desktop\sMFE\LUAD\biomarker\LUAD surv.csv");
st(1,:)=surv(1:end,1);
for i=1:length(st)
    if(isnan(st(i)))
        st(i)=0;
    end
end
clear surv
statu(st<median(st))=0;statu(st>=median(st))=1;
g2=B(:,1);
clear code m g1 g d
asize=size(SLE);m=0;
code(1:asize(2))=0;
for k=1:length(g2)
    for i=1:asize(2)
        for j=1:asize(1)
            if(SLE(j,i)==g2(k))
                code(1,i)=1;%%%识别的样本
                break
            else
                if(j==asize(1))
                    code(1,i)=2;
                end
            end
        end
    end
    if(sum(code(:)==1)>=asize(2)*0.02)
        p=((statu(:)'*code(:))/sum(code(:)));
        if(p>=0.5||p<=0.5)
            m=m+1;
            g1(1,m)=gene(str2num(network{g2(k)}{1}));%%%gene name
            d(1,m)=p;
            g(1:asize(2),m)=code(:);
        end
    end
    code(:)=0;
end
sheet=1;xlRange='B';
xlswrite('C:\Users\27226\Desktop\sMFE\LUAD\biomarker\LUAD identity.csv',g1');%%由于字符数组与数值不能同时存在一个矩阵或向量中，所以先输出基因。
xlswrite('C:\Users\27226\Desktop\sMFE\LUAD\biomarker\LUAD identity.csv',d',sheet,xlRange);
xlRange='C';
xlswrite('C:\Users\27226\Desktop\sMFE\LUAD\biomarker\LUAD surv.csv',[g1;num2cell(g)],sheet,xlRange);%%由于字符数组与数值不能同时存在一个矩阵或向量中，所以先输出基因。
