clc
clear
close all
% fid=fopen('test.txt');
% test=[];
% temp=[];
% while ~feof(fid)
%     a=fgetl(fid);
%     if a(1,1)==' '
%         test=[test;temp];
%         temp=[];
%         a=deblank(a);
%         a=regexp(a,'\s+','split');
%         temp.id=a{4};
%         temp.sequence=[];
%         temp.pid=[];
%         temp.nid=[];
%         instance=0;
%     else
%         ind=find(a=='-');
%         if isempty(ind)
%             a=deblank(a);
%             a=regexp(a,'\s+','split');
%             temp.sequence=[temp.sequence a{1}];
%         else
%            a=deblank(a);
%            a=regexp(a,'\s+','split');
%            ind1=find(a{1}=='G');
%            ind2=find(a{1}=='K');
%            ind1=ind1+instance;
%            ind2=ind2+instance;
%            temp.pid=[temp.pid ind1];
%            temp.nid=[temp.nid ind2];
%            instance=instance+80;
%         end
%     end        
% end
% test=[test;temp];
% save('testdata.mat','test');
%%%%%%%%%提取片段%%%%%%%%%%%%
load testdata.mat;
Ptestflagment=[];
Ntestflagment=[];
testlabel=[];
window=15;
nnn=size(test,1);
ct=1;
nt=1;
SSptest=[];
SSntest=[];
for i=1:nnn
    SSdata=[];
    Pids=[];
    for j=1:window
       Pids =[Pids 'X'];
       SSdata=[SSdata; 0 0 0];
    end
    Pids=[Pids test(i,1).sequence];
    SSdata=[SSdata; test(i,1).SS];
    for j=1:window
        Pids=[Pids 'X'];
        SSdata=[SSdata; 0 0 0];
    end
    for j=1:size(test(i,1).pid,2)
        key=test(i,1).pid(j)+window;
        Ptestflagment=[Ptestflagment;Pids((key-window):(key+window))];
        SSptest(:,:,ct)=SSdata((key-window):(key+window),:);
        ct=ct+1;
    end
    for j=1:size(test(i,1).nid,2)
        key=test(i,1).nid(j)+window;
        Ntestflagment=[Ntestflagment;Pids((key-window):(key+window))];
        SSntest(:,:,nt)=SSdata((key-window):(key+window),:);
        nt=nt+1;
    end
end
testlabel=ones(size(Ptestflagment,1),1);
testlabel=[testlabel;-1*ones(size(Ntestflagment,1),1)];
%%%%%%%%%%%%%%%
label=[];
for i=1:size(Ptestflagment,1)
    label=[label;1];
end
for i=1:size(Ntestflagment,1)
    label=[label;-1];
end
%%%%%%%%%提取F特征%%%%%%%%%%%
[Fp Fn]=Fcalculate(Ptestflagment,Ntestflagment);
%%%%%%%%%提取SS特征%%%%%%%%%%
% [SSp SSn]=SScalculate(Ptestflagment,Ntestflagment);
%%%%%%%%%提取理化属性%%%%%%%%
testphychemical=phychecalculate(Ptestflagment,Ntestflagment,label);
%%%%%%%%%提取CKSAAP特征%%%%%
top=30;
%%%%%%%%%%%%%%%%%%%%选择窗口%%%%%%%%%%%%%%
[window,topfeature]=selectWindows(Fp,Fn,SSptest,SSntest,testphychemical,label,Ptestflagment,Ntestflagment,top);
%%%%%%%%%%%%%%%%%%%%生成测试集%%%%%%%%
center=16;
temp1=[Fp(:,(center-window(1,1)):(center+window(1,1)));Fn(:,(center-window(1,1)):(center+window(1,1)))];
temp2=[];
for i=1:size(SSptest,3)
    temp2=[temp2;SSptest((center-window(1,2)):(center+window(1,2)),1,i)' SSptest((center-window(1,2)):(center+window(1,2)),2,i)' SSptest((center-window(1,2)):(center+window(1,2)),3,i)'];
end
for i=1:size(SSntest,3)
    temp2=[temp2;SSntest((center-window(1,2)):(center+window(1,2)),1,i)' SSntest((center-window(1,2)):(center+window(1,2)),2,i)' SSntest((center-window(1,2)):(center+window(1,2)),3,i)'];
end
temp3=[];
m=size(testphychemical,3);
m=20;
for i=1:m
    temp3=[temp3 testphychemical(:,(center-window(1,3)):(center+window(1,3)),i)];
end
[pCK,nCK]=CKSAAPgenerate(Ptestflagment,Ntestflagment,topfeature,window(1,4));
temp4=[pCK;nCK];
aar=[temp1 temp2 temp3 temp4];
aar=zscore(aar);
[feature_ind,fit,prelabel]=fselection(aar,label);
save('feature_ind.mat','feature_ind');
save('aar.mat','aar');
save('label.mat','label');
