function [DNA] = GenerateDNASequence(Length, Probabilities)
%[DNA] = GenerateDNASequence(Length, Probabilities)
%   Generates a random DNA sequence of length Length with
%   probabilities  P(DNA(i)=A) = Probabilities(1)
%                  P(DNA(i)=C) = Probabilities(2)
%                  P(DNA(i)=G) = Probabilities(3)
%                  P(DNA(i)=T) = Probabilities(4)
%   for all i=1,2,3,...,Length.
%   ($RCSfile: GenerateDNASequence.m,v $ $Revision: 1.1.1.1 $)

%%% Check arguments:
if (sum(Probabilities<0))
  error('GenerateDNASequence: Probabilities must be nonnegative.')
end
if (sum(Probabilities) ~= 1)
  error('GenerateDNASequence: Sum of probabilities must be equal to 1.')
end
if (Length ~= round(Length))
  error('GenerateDNASequence: Length must be integer.')
end
if (Length < 0)
  error('GenerateDNASequence: Length must be greater or equal than 0.')
end
%%% Do calculations:  
DNA='';                      % initialize DNA as empty string
rand('seed',1);              % restart the random number generator
rnd=rand(Length,1);          % load rnd with Length times 1 vector
                             % of uniform random numbers in [0,1)
for i=1:3                           % loop over i=1,2,3
  p(i)=sum(Probabilities(1:i));     % compute sum of probabilities
end
for i=1:Length                      % loop over all elements of the 
  if (rnd(i)< p(1)) DNA(i)='A';     % DNA and assign DNA(i) the 
  elseif (rnd(i)< p(2)) DNA(i)='C'; % corresponding letter.
  elseif (rnd(i)< p(3)) DNA(i)='G';
  else DNA(i)='T';
  end;
end;
DNA=DNA(:)';                 % make DNA a row vector 
return                       % return the variable DNA			     
