%%%%%%%¼ÆËãthe position scoring function %%%%%
clc
clear
close all
%%%%%
load Mpositive.mat;
load Mnegative.mat;
load Positive.mat;
load Negative.mat;
letter=['A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'];
p0=0.05;
num=size(PM,1);
l=size(PM,2);
%%%%%%%%%%%%%¼ÆËãMÖµ
Mp=[];
n=size(positive,1);
for i=1:num
    cout=0;
    for j=1:l
        temp=PM(i,j)/n-p0;
        temp=temp*temp;
        temp=temp/p0;
        cout=cout+temp;
    end
    Mp=[Mp;cout];
end
Mn=[];
n=size(negative,1);
for i=1:num
    cout=0;
    for j=1:l
        temp=NM(i,j)/n-p0;
        temp=temp*temp;
        temp=temp/p0;
        cout=cout+temp;
    end
    Mn=[Mn;cout];
end
Pp=[];
n=size(positive,1);
for i=1:num
    cout=[];
    for j=1:l
        temp=(PM(i,j)+p0*sqrt(n))/(n+sqrt(n));
        cout=[cout temp];
    end
    Pp=[Pp;cout];
end
Fp=[];
n=size(positive,1);
for i=1:n
    cout=[];
    for j=1:num
        index=find(letter(1,:)==positive(i,j));
        if ~isempty(index)
            temp=log((Pp(j,index)/p0))*(Mp(j)-Mn(j));
            cout=[cout temp];
        else
            cout=[cout 0];
        end
    end
    Fp=[Fp;cout];
end
Fn=[];
n=size(negative,1);
for i=1:n
    cout=[];
    for j=1:num
        index=find(letter(1,:)==negative(i,j));
        if ~isempty(index)
            temp=log((Pp(j,index)/p0))*(Mp(j)-Mn(j));
            cout=[cout temp];
        else
            cout=[cout 0];
        end
    end
    Fn=[Fn;cout];
end
save('Pp.mat','Pp');
save('Fp.mat','Fp');
save('Fn.mat','Fn');
save('Mp.mat','Mp');
save('Mn.mat','Mn');

%%%%%%%%%%%%%%%
set(gcf,'color','white');
window=15;
x=(-window):-1;
x2=1:window;
Mp=Mp';
Mn=Mn';
plot(x,Mp(1:window),'ro-','MarkerFaceColor','r','LineWidth',2);
hold on
plot(x,Mn(1:window),'ks-','MarkerFaceColor','k','LineWidth',2);
hold off
hold on
plot(x2,Mp((window+2):end),'ro-','MarkerFaceColor','r','LineWidth',2);
hold off
hold on
plot(x2,Mn((window+2):end),'ks-','MarkerFaceColor','k','LineWidth',2);
xlabel('Site');
ylabel('M(l)');
str={'Glycation' 'Non-glycation'};
legend(str);
hold off