% Please use Octave version 4.0.0 (Code was executed on ubuntu platform)
% This code constructs the features for the occurrence of amino acid 2 upstream and 2 downstream of lysine including lysine in the middle
% The resulting feature for each lysine is a 20x5 matrix which is reshaped to produce a 100-dimensional feature vector
% In constructing the features, a 1 is given to one of the 20 amino acid which is present at that location in the peptide P while the others are given a 0
% The code also generates the arff files which is used to train MLP in WEKA. Before executing the code, please change the directory 

Amino_acids = {'A'; 'C'; 'D'; 'E'; 'F'; 'G'; 'H'; 'I'; 'K'; 'L'; 'M'; 'N'; 'P'; 'Q'; 'R'; 'S'; 'T'; 'V'; 'W'; 'Y'}; % 20 amino acid

load test
load train

for k = 1:10

    % Construct features for the 10 test files (10-fold cross-validation)
    st = num2str(k);
    test_name = strcat('NewFold', st);
    fold_file = [eval(test_name)];
    
    for i=1:size(fold_file,1)
        
        for m=1:5
            
            aa = fold_file{i,5}(m);
          
            for j=1:size(Amino_acids,1)
        
               
                if (strcmp(aa, Amino_acids{j}) == true) % Compare each of the amino acids at the five locations with the 20 amino acids
       
                    Feature_Mat(j,m) = 1; % Give a 1 for the matching amino acid
            
                else
                    
                    Feature_Mat(j,m) = 0; % Give a 0 for the non-matching amino acid
                    
                end
       
            end
        end   
        fold_file{i,2} = reshape(Feature_Mat,1,[]); % Store the features after reshaping 
    end
   
    Entire_Folds{k} = fold_file; % Save the test file which has been updated with this new set of features
    
    % Construct features for the 10 train files (10-fold cross-validation)
    train_name = strcat('NewTrain', st);
    train_file = [eval(train_name)];
 
    for i=1:size(train_file,1)
        
        for m=1:5
            
            aa = train_file{i,5}(m);
          
            for j=1:size(Amino_acids,1)
        
               
                if (strcmp(aa, Amino_acids{j}) == true)
       
                    Feature_Mat(j,m) = 1;
            
                else
                    
                    Feature_Mat(j,m) = 0;
                    
                end
       
            end
        end   
        train_file{i,2} = reshape(Feature_Mat,1,[]);
    end
    Entire_Train{k} = train_file;
end

% Construct 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 '/home/researcher/Desktop/arffs' % Change directory here
delete *.arff % Delete previously generated arff files

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

    filename = fullfile('/home/researcher/Desktop/arffs',test_names{1,k}); % Change directory here
    fid = fopen(filename,'w');

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

    for l=1:size(Data{1,2},2)
    fprintf(fid,'@%s%d %s\r\n', 'attribute F',l,'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,2});
    fprintf(fid,'%s%s\r\n', comma_sep, Data{j,3});
    end
    fclose(fid)
    
    clear Data;
    
    Data = Entire_Train{k}; 

    filename = fullfile('/home/researcher/Desktop/arffs',train_names{1,k}); % Change directory here
    fid = fopen(filename,'w');

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

    for m=1:size(Data{1,2},2)
    fprintf(fid,'@%s%d %s\r\n', 'attribute F',m,'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,2});
    fprintf(fid,'%s%s\r\n', comma_sep, Data{j,3});
    end
    fclose(fid)   
    
    clear Data;
end
    
    

    


