% This code was developed using R2016a Matlab version (windows platform)
% Code compares and extracts similar Testing data. Training data is different here as it is not optimized. PhoglyStruct is using target CV method hence training set is optimized. 
% It loads the already generated CKSAAP_Data which contains CKSAAP features and carries out extraction
% The code generates the arff files which is used to train MLP in WEKA
% Before executing the code, please create a folder by the name 'arffs' and provide its directory in the code below where the arff files are to be saved
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load CKSAAP_Data % This is the dataset containing CKSAAP features for each lysine K
load Feature_Label
load test
clear Train

Whole_Data = {'NewFold1' 'NewFold2' 'NewFold3' 'NewFold4' 'NewFold5' 'NewFold6' 'NewFold7' 'NewFold8' 'NewFold9' 'NewFold10'}; 

%Test Sets
for h = 1:10
     
    st = num2str(h);
    s = strcat('NewFold', st);
    fold_file = [eval(s)];
    
    for i=1:size(fold_file,1)
    
    for j=1:size(Final_Data,1)

        if (strcmp(Final_Data{j,1}, fold_file{i,1}) == true) && (Final_Data{j,4} == fold_file{i,4})

                   NewFold(i,:) = Final_Data(j,:);
                   
                   break % As soon as ks from fold_file and result_file match, exit the for loop
        end
    end
 
    end
    
    Fold{h} = NewFold; % Save each generated fold under one variable
    
end

%Train Sets
for k = 1:10
    z = 1;
    for l = 1:10
    
        if k == l
            % Do nothing
        else
            
            T(1,z) = l;
            z = z + 1;
        end
        
    end

    Train{k} = [Fold{T(1,1)}; Fold{T(1,2)}; Fold{T(1,3)}; Fold{T(1,4)}; Fold{T(1,5)}; Fold{T(1,6)}; Fold{T(1,7)}; Fold{T(1,8)}; Fold{T(1,9)}]; % use string name to save cell array of same name into Data
    
end


%F_Score calculation for each training set
for m = 1:10
    % Number of +ve and -ve samples and Separating +ve and -ve samples
    % The if conditions below will result in error if sample label is of type
    % double
    Data2 = Train{m};

    Num_of_Pos  = 0;
    Num_of_Neg  = 0;
    for i = 1:size(Data2,1)
        if Data2{i,3} == '1'
           Num_of_Pos = Num_of_Pos + 1;
           Pos_sample(Num_of_Pos,:) = Data2(i,:); 
        end
        if Data2{i,3} == '0'
           Num_of_Neg = Num_of_Neg + 1;
           Neg_sample(Num_of_Neg,:) = Data2(i,:);
        end    
    end
    % Sum the columns of the Final_Data
    for i = 5:size(Data2,2)

        Summ = 0;
        for j = 1:size(Data2,1)

          Summ = Summ + Data2{j,i};  

        end
        Feature_sum{1,i} = Summ;
    end

    % Mean j-th feature in whole
    for i = 5:size(Feature_sum, 2)

         Feature_mean_whole{1,i} = Feature_sum{1,i}/size(Data2,1);    

    end

    % Mean j-th feature in positive

    % Sum the columns of the Pos_sample
    for i = 5:size(Pos_sample,2)

        Summ = 0;
        for j = 1:size(Pos_sample,1)

          Summ = Summ + Pos_sample{j,i};  

        end
        Feature_sum_pos{1,i} = Summ;
    end
    % Mean j-th feature in Pos_sample
    for i = 5:size(Feature_sum_pos, 2)

         Feature_mean_pos{1,i} = Feature_sum_pos{1,i}/size(Pos_sample,1);    

    end

    % Mean j-th feature in negative

    % Sum the columns of the Neg_sample
    for i = 5:size(Neg_sample,2)

        Summ = 0;
        for j = 1:size(Neg_sample,1)

          Summ = Summ + Neg_sample{j,i};  

        end
        Feature_sum_neg{1,i} = Summ;
    end
    % Mean j-th feature in Neg_sample
    for i = 5:size(Feature_sum_neg, 2)

         Feature_mean_neg{1,i} = Feature_sum_neg{1,i}/size(Neg_sample,1);    

    end

    % Denominator part of F_Score formula

    % Pos part of deno
    for i = 5:size(Pos_sample, 2)
        Summ = 0;
        for j = 1:size(Pos_sample, 1)

            Summ = Summ + (Pos_sample{j,i} - Feature_mean_pos{1,i})^2;

        end
        Pos_deno{1,i} = (1/(size(Pos_sample, 1) - 1)) * Summ;
    end
    % Neg part of deno
    for i = 5:size(Neg_sample, 2)
        Summ = 0;
        for j = 1:size(Neg_sample, 1)

            Summ = Summ + (Neg_sample{j,i} - Feature_mean_neg{1,i})^2;

        end
        Neg_deno{1,i} = (1/(size(Neg_sample, 1) - 1)) * Summ;
    end

    % F-Score Calculation

    for i = 5:size(Data2, 2)

        if (Pos_deno{1,i} + Neg_deno{1,i}) == 0
            F_Score{1,i} = 0;

        else
            F_Score{1,i} = ((Feature_mean_pos{1,i}-Feature_mean_whole{1,i})^2 + (Feature_mean_neg{1,i}-Feature_mean_whole{1,i})^2)/(Pos_deno{1,i} + Neg_deno{1,i});
        end
    end

    Fscore_n_FeatureLabel = [F_Score; feature_label];

    A = Fscore_n_FeatureLabel';
    B = A(5:size(A,1),:);
    C = sortrows(B,1);
    Ju_Score{m} = flipud(C);
    
    clear Pos_sample;
    clear Neg_sample;
    clear Feature_sum;
    clear Feature_mean_whole;
    clear Feature_sum_pos;
    clear Feature_mean_pos;
    clear Feature_sum_neg;
    clear Feature_mean_neg;
    clear Pos_deno;
    clear Neg_deno;
    clear F_Score;
    clear A;
    clear B;
    clear C;
    
end

% Select first 300 features of both train and test sets using F_score (Ju's score)

for n = 1:10
    
    Data = Train{n};
    Data_300 = Data(:,1:4);

    for i=1:300

        for j=5:size(feature_label, 2)

            if strcmp(feature_label(1,j),Ju_Score{n}(i,2)) == true

                Data_300(:,i+4) = Data(:,j);

            end
        end

    end
    Final_Train{n} = Data_300;    
    
    clear Data_300; 
    clear Data;
    
    Data = Fold{n};
    Data_300 = Data(:,1:4);

    for i=1:300

        for j=5:size(feature_label, 2)

            if strcmp(feature_label(1,j),Ju_Score{n}(i,2)) == true

                Data_300(:,i+4) = Data(:,j);

            end
        end

    end
    Final_Fold{n} = Data_300;
    
    clear Data_300; 
    clear Data;    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Generating the arff files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


test_names = {'Test1.arff' 'Test2.arff' 'Test3.arff' 'Test4.arff' 'Test5.arff' 'Test6.arff' 'Test7.arff' 'Test8.arff' 'Test9.arff' 'Test10.arff'}; 
train_names = {'Train1.arff' 'Train2.arff' 'Train3.arff' 'Train4.arff' 'Train5.arff' 'Train6.arff' 'Train7.arff' 'Train8.arff' 'Train9.arff' 'Train10.arff'}; 

cd 'C:\Users\User\Desktop\arffs' % Change directory here
delete *.arff % Delete all the previously generated arff files

for k = 1:10
    
    Data = Final_Train{k}; 

    filename = fullfile('C:\Users\User\Desktop\arffs',train_names{1,k}); % Change directory here
    fid = fopen(filename,'w');
    
    fprintf(fid,'@%s\r\n\r\n', 'relation Phosphoglycerylation');

    for i=1:300
    fprintf(fid,'@%s%d %s\r\n', 'attribute F',i,'numeric');
    end

    fprintf(fid,'@%s\r\n', 'attribute label {0,1}');

    fprintf(fid,'\r\n@%s\r\n', 'data');

    for j=1:size(Data,1)
    comma_sep = sprintf('%f,' , Data{j,5:size(Data,2)});
    fprintf(fid,'%s%s\r\n', comma_sep, Data{j,3});
    end
    fclose(fid)
    
    clear Data;
    
    Data = Final_Fold{k}; 

    filename = fullfile('C:\Users\User\Desktop\arffs',test_names{1,k}); % Change directory here
    fid = fopen(filename,'w');

    fprintf(fid,'@%s\r\n\r\n', 'relation Phosphoglycerylation');

    for i=1:300
    fprintf(fid,'@%s%d %s\r\n', 'attribute F',i,'numeric');
    end

    fprintf(fid,'@%s\r\n', 'attribute label {0,1}');

    fprintf(fid,'\r\n@%s\r\n', 'data');

    for j=1:size(Data,1)
    comma_sep = sprintf('%f,' , Data{j,5:size(Data,2)});
    fprintf(fid,'%s%s\r\n', comma_sep, Data{j,3});
    end
    fclose(fid)    
    
    clear Data;
end


