% This code was developed using R2016a Matlab version (windows platform)
% Compare the samples in testing dataset with already obtained labels from iPGK-PseAAC webserver at http://app.aporc.org/iPGK-PseAAC/
% The result of this code is stored in the variable Results_Avg_iPGK_PseAAC 
% The code also generates data for calculating AUC. Please change directory where the AUC data is to be stored
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load iPGK_PseAAC_result % Contains the prediction by the iPGK-PseAAC predictor
load test

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

AUC_Data_iPGK_PseAAC{1,1} = 'inst#';
AUC_Data_iPGK_PseAAC{1,2} = 'actual';
AUC_Data_iPGK_PseAAC{1,3} = 'predicted';

all_instant = 1;

for k = 1:10

    st = num2str(k);
    test_name = strcat('NewFold', st);
    fold_file = [eval(test_name)];
    
    FN = 0;
    FP = 0;
    TN = 0;
    TP = 0;

    for i=1:size(fold_file,1)
        
        AUC_Data_iPGK_PseAAC{all_instant + 1, 1} = num2str(all_instant); % Store the instant number from 2nd row
        all_instant = all_instant + 1;

        for j=1:size(Result_File,1)

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

                       yes_no{i,1} = Result_File{j,3};

                       %break % As soon as ks from fold_file and result_file match, exit the for loop
            end
        end

        if yes_no{i,1} == 1 % Predicted is 1

            if fold_file{i,3} == '1' % Actual is 1

                TP = TP + 1;
                
                AUC_Data_iPGK_PseAAC{all_instant,2} = '2:1'; % Store the actual label of the instant (2:1 notation) 
                AUC_Data_iPGK_PseAAC{all_instant,3} = '2:1'; % Store the predicted (2:1 notation)

            elseif fold_file{i,3} == '0' % Actual is 0

                FP = FP + 1;
                
                AUC_Data_iPGK_PseAAC{all_instant,2} = '1:0'; % Store the actual label of the instant (1:0 notation) 
                AUC_Data_iPGK_PseAAC{all_instant,3} = '2:1'; % Store the predicted (2:1 notation)

            end

        elseif yes_no{i,1} == 0 % Predicted is 0

            if fold_file{i,3} == '1' % Actual is 1

                 FN = FN + 1;
                 
                 AUC_Data_iPGK_PseAAC{all_instant,2} = '2:1'; % Store the actual label of the instant (2:1 notation) 
                 AUC_Data_iPGK_PseAAC{all_instant,3} = '1:0'; % Store the predicted (1:0 notation)

            elseif fold_file{i,3} == '0' % Actual is 0

                 TN = TN + 1;
                 
                 AUC_Data_iPGK_PseAAC{all_instant,2} = '1:0'; % Store the actual label of the instant (1:0 notation) 
                 AUC_Data_iPGK_PseAAC{all_instant,3} = '1:0'; % Store the predicted (1:0 notation)

            end 
        end
    end
    sen2 = TP/(TP+FN);
    spe2 = TN/(TN+FP);
    G_Mean2 = sqrt(sen2*spe2);
    accuracy2 = (TN+TP)/(FN+FP+TN+TP);
    mcc2 = ((TN*TP)-(FN*FP))/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN));
    F_Measure2 = (2*TP)/((2*TP)+FP+FN);
    Results_iPGK_PseAAC(1,k) = sen2; 
    Results_iPGK_PseAAC(2,k) = spe2; 
    Results_iPGK_PseAAC(3,k) = G_Mean2; 
    Results_iPGK_PseAAC(4,k) = accuracy2; 
    Results_iPGK_PseAAC(5,k) = mcc2;
    Results_iPGK_PseAAC(6,k) = F_Measure2;
    
    confusion_matrix(1,k) = FN;
    confusion_matrix(2,k) = FP;
    confusion_matrix(3,k) = TN;
    confusion_matrix(4,k) = TP;
end
metrics = {'sensitivity'; 'specificity'; 'G-Mean'; 'accuracy'; 'mcc'; 'F-Measure'};
Results_Avg_iPGK_PseAAC = [metrics, num2cell(sum(Results_iPGK_PseAAC,2)/10)];

filename = fullfile('C:\Users\User\Desktop\AUC', 'AUC_Data_iPGK_PseAAC.txt'); % Change directory here
fid = fopen(filename,'w');
for l=1:size(AUC_Data_iPGK_PseAAC, 1)
    The_String = strcat(AUC_Data_iPGK_PseAAC{l,1}, ',', AUC_Data_iPGK_PseAAC{l,2}, ',', AUC_Data_iPGK_PseAAC{l,3});
    fprintf(fid, '%s\n', The_String);
end
fclose(fid)

