% This code was developed using R2016a Matlab version (windows platform)
% Compare the samples in testing dataset with already obtained labels from Phogly–PseAAC webserver at http://app.aporc.org/Phogly-PseAAC/
% The result of this code is stored in the variable Results_Avg_Phogly_PseAAC
% The code also writes actual label and predicted label to a text file for calculating AUC. Please change its directory before executing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

load Phogly_PseAAC_Result % Contains the prediction by the Phogly–PseAAC predictor
load test

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

AUC_Data_Phogly_PseAAC{1,1} = 'inst#';
AUC_Data_Phogly_PseAAC{1,2} = 'actual';
AUC_Data_Phogly_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)

        phosphoglycerylation_match = 0; % Put a value of zero and only put it to one when match has occured below in the code  
        AUC_Data_Phogly_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(fold_file{i,1}, result_file{j,2}) == true) && (result_file{j,3} == fold_file{i,4})

                phosphoglycerylation_match = 1; % Match has been found

           end

        end
        if (fold_file{i,3} == '1') && (phosphoglycerylation_match == 1) % Actual and predicted are 1 

            TP = TP + 1;

            AUC_Data_Phogly_PseAAC{all_instant,2} = '2:1'; % Store the actual label of the instant (2:1 notation) 
            AUC_Data_Phogly_PseAAC{all_instant,3} = '2:1'; % Store the predicted (2:1 notation)
            
        elseif (fold_file{i,3} == '1') && (phosphoglycerylation_match == 0) % Actual is 1 and predicted is 0

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

        elseif (fold_file{i,3} == '0') && (phosphoglycerylation_match == 1) % Actual is 0 and predicted is 1

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

        elseif (fold_file{i,3} == '0') && (phosphoglycerylation_match == 0) % Actual and predicted are 0

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

        end

    end
    sen = TP/(TP+FN);
    spe = TN/(TN+FP);
    G_Mean = sqrt(sen*spe);
    accuracy = (TN+TP)/(FN+FP+TN+TP);
    mcc = ((TN*TP)-(FN*FP))/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN));
    F_Measure = (2*TP)/((2*TP)+FP+FN);
    Results_Phogly_PseAAC(1,k) = sen; 
    Results_Phogly_PseAAC(2,k) = spe; 
    Results_Phogly_PseAAC(3,k) = G_Mean; 
    Results_Phogly_PseAAC(4,k) = accuracy; 
    Results_Phogly_PseAAC(5,k) = mcc;
    Results_Phogly_PseAAC(6,k) = F_Measure;
    
    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_Phogly_PseAAC = [metrics, num2cell(sum(Results_Phogly_PseAAC,2)/10)];

% Write to text file the actual label and predictions to calculate AUC

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