% E = FreeEnergyDist(RS,codebook) implements the Free Energy Ribosome
% Decoding Algorithm. It compares the subsequence s of length n with L
% codewords defined by the codebook "codebook" and finds the most complimentary
% codeword for the Received Sequence RS, i.e. the codeword that results in 
% a minimum free energy.

% E             :  is a vecotor of length L that represents the free energy 
%                  dissipated on binding with the nuceotide doublet codebook{k}(n) 
%                  and codebook{k}(n+1) (i.e. the (n)th and the (n+1)th  symbols of 
%                  the (k)th codeword. 

function E = FreeEnergyDist(RS,codebook)
RS = {RS};
L = length(codebook);
n = length(codebook{1});
E(1:L) = 0;  % Initialization
for i = 1:L
    for j = 1:n-1
        if isequal({codebook{i}(j:j+1)},{RS{1}(j:j+1)})
            FED = codebook{i}(j:j+1);
            E(i) = E(i) + energy(FED);
        else
            E(i) = E(i);
        end
    end
end

