function aaseqInt = aa2int(aaseqChar);
% Converts amino acid sequence(s) from a character to an integer representation
% Program by Steinar Thorvaldsen, steinart@math.uit.no, april 2004.
% Ref.: http://www.math.uit.no/bi/deltaprot/

% aaseqChar: Amino acid character array. May be of size from one amino acid
%            to a three dimentional array.
% aaseqInt: Amino acids represented by an arrey of integers.
% The following mapping protocol is used:
% A R N D C Q E G H I  L  K  M  F  P  S  T  W  Y  V  - 
% 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
% where - are gaps, often at the beginning/end of the alognment.
% Unknown characters are treated as gaps (or should be ignored?) 

% Use:
% aaseqInt = aa2int(aaseqChar)

if isempty(aaseqChar)
    aaseqInt = uint8([]);
    return
end

[M,N,P] = size(aaseqChar);
aaseqInt = zeros(M, N, 'uint8');
aaseqChar=upper(aaseqChar);

%Fast code using direct lookup in the standard ASCII table,
%but a possible sources of error if this standard is altered...:

%    0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
%  0 NUL SOH STX ETX EOT ENQ ACK BEL BS  HT  LF  VT  FF  CR  SO  SI
% 16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM  SUB ESC FS  GS  RS  US
% 32 SP  !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /
% 48 0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?
% 64 @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O
% 80 P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _
% 96 `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o
%112 p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~ DEL

%Smart first to just represent gaps (-~ ) in ascii table as Z+1:
for p=1:P
    for m = 1 : M
        aaseqChar(m,:,p) = strrep(aaseqChar(m,:,p),'-',char('Z' + 1));
        aaseqChar(m,:,p) = strrep(aaseqChar(m,:,p),'~',char('Z' + 1));
        aaseqChar(m,:,p) = strrep(aaseqChar(m,:,p),' ',char('Z' + 1));
    end
end

aaseqInt = uint8(aaseqChar); %all characters in sequense converted to ascii numbers!
asciiA = uint8('A');
%Converts to standard aa number.
%Gap (no 21) is used for unknown characters(B,J,O,U,X,Z)???
map = uint8([1 21 5 4 7 14 8 9 10 21 12 11 13 3 21 15 6 2 16 17 21 20 18 21 19 21 21 21 21]);
for p=1:P
    for m = 1 : M
        aaseqInt(m,:,p) = map((aaseqInt(m,:,p))-asciiA + 1);
    end
end

%-----------------------------------------------------------------------
%Alternative safer (and slower) code not using direct lookup in the ASCII table:
% for m = 1:M
%  for n = 1:N
%    switch aaseqChar(m,n,p)
%    case 'A' %Ala
%        aaseqInt(m,n,p) = 1;
%    case 'R' %Arg
%        aaseqInt(m,n,p) = 2;
%    case 'N' %Asn
%        aaseqInt(m,n,p) = 3;
%    case 'D' %Asp
%        aaseqInt(m,n,p) = 4;
%    case 'C' %Cys
%         aaseqInt(m,n,p) = 5;
%    case 'Q' %Gln
%        aaseqInt(m,n,p) = 6;
%    case 'E' %Glu
%        aaseqInt(m,n,p) = 7;
%    case 'G' %Gly
%        aaseqInt(m,n,p) = 8;
%    case 'H' %His
%        aaseqInt(m,n,p) = 9;
%    case 'I' %Ile
%        aaseqInt(m,n,p) = 10;
%    case 'L' %Leu
%        aaseqInt(m,n,p) = 11;
%    case 'K' %Lys
%        aaseqInt(m,n,p) = 12;
%    case 'M' %Met
%        aaseqInt(m,n,p) = 13;
%    case 'F' %Phe
%        aaseqInt(m,n,p) = 14;
%    case 'P' %Pro
%        aaseqInt(m,n,p) = 15;
%    case 'S' %Ser
%        aaseqInt(m,n,p) = 16;
%    case 'T' %Thr
%        aaseqInt(m,n,p) = 17;
%    case 'W' %Trp
%        aaseqInt(m,n,p) = 18;
%    case 'Y' %Tyr
%        aaseqInt(m,n,p) = 19;
%    case 'V' %Val
%        aaseqInt(m,n,p) = 20;
%    case {'-','~'} %Gaps, often in alignments or at the end of the sequence
%        aaseqInt(m,n,p) = 21;
%    case {' '} %Space, sometimes at the end of the sequence
%        aaseqInt(m,n,p) = 21;    
%    otherwise % Not an amino acid
%        disp (['Warning: Sequence ',num2str(m),' contains an unknown character: ',aaseqChar(m,n,p),' in position ',num2str(n),'. Gap is used.']);
%        aaseqInt(m,n,p) = 21;
%    end
%  end
% end


