Binary to DNA sequence conversion

1 view (last 30 days)
lilly lord
lilly lord on 10 Jun 2020
Edited: James Tursa on 19 Jun 2020
Hi, i have a binary string which I want to convert into DNA sequence.
A='010110101010101011100110011111';
[m n]=size(A);
mn=m*n;
k1=[];k2=[];k3=[];k4=[];s=[];
for i=1:mn
x=A(i,i+1);
if x(1) == '0' && x(2) == '0';
k1 = 'A';
elseif x(1) == '0' && x(2) == '1';
k1 = 'C';
elseif x(1) == '1' && x(2) == '0';
k1 = 'G';
elseif x(1) == '1' && x(2) == '1';
k1 = 'T';
end
%___
if x(3) == '0' && x(4) == '0';
k2 = 'A';
elseif x(3) == '0' && x(4) == '1';
k2 = 'C';
elseif x(3) == '1' && x(4) == '0';
k2 = 'G';
elseif x(3) == '1' && x(4) == '1';
k2 = 'T';
end
%___
if x(5) == '0' && x(6) == '0';
k3 = 'A';
elseif x(5) == '0' && x(6) == '1';
k3 = 'C';
elseif x(5) == '1' && x(6) == '0';
k3 = 'G';
elseif x(5) == '1' && x(6) == '1';
k3 = 'T';
end
%___
if x(7) == '0' && x(8) == '0';
k4 = 'A';
elseif x(7) == '0' && x(8) == '1';
k4 = 'C';
elseif x(7) == '1' && x(8) == '0';
k4 = 'G';
elseif x(7) == '1' && x(8) == '1';
k4 = 'T';
end
s=[s k1 k2 k3 k4];
end
%%%Error
Index exceeds matrix dimensions.
Error in binay_rule1 (line 16)
elseif x(1) == '1' && x(2) == '0';
Can an anyone help me. Thanks in advance

Accepted Answer

James Tursa
James Tursa on 10 Jun 2020
Edited: James Tursa on 10 Jun 2020
x = A(i:i+1);
But if you are going to process pairs of characters in A, then maybe you need to step by 2 as well, e.g.
for i=1:2:mn
But, instead of all that handwritten logic, does this do what you want?
ACGT = 'ACGT';
s = ACGT(bin2dec(reshape(A,2,[])')+1)
  2 Comments
lilly lord
lilly lord on 11 Jun 2020
Thanks
ACGT = 'ACGT';
s = ACGT(bin2dec(reshape(A,2,[])')+1)
This work well. Can u plz tell how to get inverse of it
James Tursa
James Tursa on 19 Jun 2020
Edited: James Tursa on 19 Jun 2020
The inverse function would be:
reshape(dec2bin((0:3)*(s == ACGT'))',1,[])

Sign in to comment.

More Answers (0)

Categories

Find more on Downloads in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!