% send:
% --- d is the dna SR sequence
% --- m is the frame you want to use for the translation (ADD 1 to the frames 0,1,2,3,4,5 <=> #C to Matlab convention)

% p is the peptide to build
function p = Translate(d, m)

% see which ORF to use 
if m >=3
    % flip the DNA seq if the complement is the ORF
    d = fliplr(d);
    % convert 'A'->'T'; 'T'->'A';'G'->'C'; 'C'->'G';
    ind_A = find(d=='A'); 
    ind_T = find(d=='T');
    ind_G = find(d=='G');
    ind_C = find(d=='C');
    d(ind_A) = 'T'; 
    d(ind_T) = 'A';
    d(ind_G) = 'C';
    d(ind_C) = 'G'; 
    clear ind*
    % convert "m" to MatLab convention
    m = m - 2;
else
    m = m + 1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	
% SR length
N = length(d);
j = 0;
% go over the dna sequence and take 3 letters together starting at m
for i = m:3:N-2
    
    clear tmp loc
    
    % determine the location of the next 3 letters
    loc = i:i+2;
    % take the gene
    tmp = d(loc);
    
    % translate
    % T..
    if (strcmp(tmp,'TTT')); j = j+1; p(j) = 'F'; end
    if (strcmp(tmp,'TTC')); j = j+1; p(j) = 'F'; end
    if (strcmp(tmp,'TTA')); j = j+1; p(j) = 'L'; end
    if (strcmp(tmp,'TTG')); j = j+1; p(j) = 'L'; end

    if (strcmp(tmp,'TCT')); j = j+1; p(j) = 'S'; end
    if (strcmp(tmp,'TCC')); j = j+1; p(j) = 'S'; end
    if (strcmp(tmp,'TCA')); j = j+1; p(j) = 'S'; end
    if (strcmp(tmp,'TCG')); j = j+1; p(j) = 'S'; end

    if (strcmp(tmp,'TAT')); j = j+1; p(j) = 'Y'; end
    if (strcmp(tmp,'TAC')); j = j+1; p(j) = 'Y'; end
    if (strcmp(tmp,'TAA')); j = j+1; p(j) = '*'; end
    if (strcmp(tmp,'TAG')); j = j+1; p(j) = '*'; end

    if (strcmp(tmp,'TGT')); j = j+1; p(j) = 'C'; end
    if (strcmp(tmp,'TGC')); j = j+1; p(j) = 'C'; end
    if (strcmp(tmp,'TGA')); j = j+1; p(j) = '*'; end
    if (strcmp(tmp,'TGG')); j = j+1; p(j) = 'W'; end

    % C..
    if (strcmp(tmp,'CTT')); j = j+1; p(j) = 'L'; end
    if (strcmp(tmp,'CTC')); j = j+1; p(j) = 'L'; end
    if (strcmp(tmp,'CTA')); j = j+1; p(j) = 'L'; end
    if (strcmp(tmp,'CTG')); j = j+1; p(j) = 'L'; end

    if (strcmp(tmp,'CCT')); j = j+1; p(j) = 'P'; end
    if (strcmp(tmp,'CCC')); j = j+1; p(j) = 'P'; end
    if (strcmp(tmp,'CCA')); j = j+1; p(j) = 'P'; end
    if (strcmp(tmp,'CCG')); j = j+1; p(j) = 'P'; end

    if (strcmp(tmp,'CAT')); j = j+1; p(j) = 'H'; end
    if (strcmp(tmp,'CAC')); j = j+1; p(j) = 'H'; end
    if (strcmp(tmp,'CAA')); j = j+1; p(j) = 'Q'; end
    if (strcmp(tmp,'CAG')); j = j+1; p(j) = 'Q'; end

    if (strcmp(tmp,'CGT')); j = j+1; p(j) = 'R'; end
    if (strcmp(tmp,'CGC')); j = j+1; p(j) = 'R'; end
    if (strcmp(tmp,'CGA')); j = j+1; p(j) = 'R'; end
    if (strcmp(tmp,'CGG')); j = j+1; p(j) = 'R'; end

    % A..
    if (strcmp(tmp,'ATT')); j = j+1; p(j) = 'I'; end
    if (strcmp(tmp,'ATC')); j = j+1; p(j) = 'I'; end
    if (strcmp(tmp,'ATA')); j = j+1; p(j) = 'I'; end
    if (strcmp(tmp,'ATG')); j = j+1; p(j) = 'M'; end

    if (strcmp(tmp,'ACT')); j = j+1; p(j) = 'T'; end
    if (strcmp(tmp,'ACC')); j = j+1; p(j) = 'T'; end
    if (strcmp(tmp,'ACA')); j = j+1; p(j) = 'T'; end
    if (strcmp(tmp,'ACG')); j = j+1; p(j) = 'T'; end

    if (strcmp(tmp,'AAT')); j = j+1; p(j) = 'N'; end
    if (strcmp(tmp,'AAC')); j = j+1; p(j) = 'N'; end
    if (strcmp(tmp,'AAA')); j = j+1; p(j) = 'K'; end
    if (strcmp(tmp,'AAG')); j = j+1; p(j) = 'K'; end

    if (strcmp(tmp,'AGT')); j = j+1; p(j) = 'S'; end
    if (strcmp(tmp,'AGC')); j = j+1; p(j) = 'S'; end
    if (strcmp(tmp,'AGA')); j = j+1; p(j) = 'R'; end
    if (strcmp(tmp,'AGG')); j = j+1; p(j) = 'R'; end

    % G..
    if (strcmp(tmp,'GTT')); j = j+1; p(j) = 'V'; end
    if (strcmp(tmp,'GTC')); j = j+1; p(j) = 'V'; end
    if (strcmp(tmp,'GTA')); j = j+1; p(j) = 'V'; end
    if (strcmp(tmp,'GTG')); j = j+1; p(j) = 'V'; end

    if (strcmp(tmp,'GCT')); j = j+1; p(j) = 'A'; end
    if (strcmp(tmp,'GCC')); j = j+1; p(j) = 'A'; end
    if (strcmp(tmp,'GCA')); j = j+1; p(j) = 'A'; end
    if (strcmp(tmp,'GCG')); j = j+1; p(j) = 'A'; end

    if (strcmp(tmp,'GAT')); j = j+1; p(j) = 'D'; end
    if (strcmp(tmp,'GAC')); j = j+1; p(j) = 'D'; end
    if (strcmp(tmp,'GAA')); j = j+1; p(j) = 'E'; end
    if (strcmp(tmp,'GAG')); j = j+1; p(j) = 'E'; end

    if (strcmp(tmp,'GGT')); j = j+1; p(j) = 'G'; end
    if (strcmp(tmp,'GGC')); j = j+1; p(j) = 'G'; end
    if (strcmp(tmp,'GGA')); j = j+1; p(j) = 'G'; end
    if (strcmp(tmp,'GGG')); j = j+1; p(j) = 'G'; end

end

if j==0
    p = [];
end