% Transcribes DNA file to RNA file
%   C -> G
%   G -> C
%   T -> A
%   A -> U
% (leaves whitespace unchanged)


% open original file
dnaFile = 0;
while dnaFile < 1
   dnaName = input('DNA file: ', 's');
   [dnaFile,message] = fopen(dnaName, 'r');
   if dnaFile == -1
     disp(message)
   end
end


% open an output file using .rna as the suffix
if length(dnaName) > 4 && strcmp(dnaName(end-3:end),'.dna')
    rnaName = [dnaName(1:end-4) '.rna'];
else
    rnaName = [dnaName '.rna'];
end
[rnaFile,message] = fopen(rnaName, 'w');



[base, num] = fread(dnaFile, 1, 'char');
while num > 0
    if base == 'C'
	out = 'G';
    elseif base == 'G'
	out = 'C';
    elseif base == 'T'
	out = 'A';
    elseif base == 'A'
	out = 'U';
    elseif isspace(base)
	out = base;
    else
	out = '_';
	disp(['WARNING. unrecognized character: ', base]);
    disp(uint16(base));
    end

    fwrite(rnaFile, out);
    [base, num] = fread(dnaFile, 1, 'char');
end

fclose(dnaFile);
fclose(rnaFile);
