Binary to DNA sequence encoding - matlab

I am implementing DNA encryption algorithm.
For that, I have a vector containing binary values such as:
01100011 01110010 01111001 01110000 01110100 01101111.
Now, these values should be mapped to DNA sequence. How do I do that in MATLAB?

2 Comments

DNA has four bases (two complementary sets, A-T and C-G) and you have a binary sequence ...
yeah..i know To use ATGC concept,but i don't know how to code..Like how do i extract only 2 digits from the binary vector and assign it to either A,T,G,C..

Sign in to comment.

 Accepted Answer

Perhaps this:
% Assign sample data.
binaryArray = [0,1,1,0,0,0,1,1, 0,1,1,1,0,0,1,0, 0,1,1,1,1,0,0,1, 0,1,1,1,0,0,0,0, 0,1,1,1,0,1,0,0, 0,1,1,0,1,1,1,1];
% Define base letters to choose from.
bases = 'ATGC';
for k = 1 : 2 : length(binaryArray)
% Convert these two digits into a number 1 - 4.
index = 2 * binaryArray(k) + binaryArray(k+1) + 1;
% Use that index to assign a letter to our result.
result((k+1)/2) = bases(index);
end
% Display in command window:
result
It shows:
result =
TGACTCAGTCGTTCAATCTATGCC

12 Comments

Thank you so much :) it worked :)
I want to create a function in Matlab of above solution you have given. So that I don't need to describe different binary Everytime in .m blank file format. Please help Function []=binary()
function result = LabelBinaryArray(binaryArray)
% Define base letters to choose from.
bases = 'ATGC';
for k = 1 : 2 : length(binaryArray)
% Convert these two digits into a number 1 - 4.
index = 2 * binaryArray(k) + binaryArray(k+1) + 1;
% Use that index to assign a letter to our result.
result((k+1)/2) = bases(index);
end
Attempted to access binaryArray(2); index out of bounds because numel(binaryArray)=1.
Error in LabelBinaryArray (line 6) index = 2 * binaryArray(k) + binaryArray(k+1) + 1;
NOT WORKING
It works if you pass in something like you said, like '01100011'. You can't pass in a single value like '1' like you did because that can't be mapped to a letter. How do I know you passed in only a single element? Because the error message says "because numel(binaryArray)=1" Are you sure you're passing in a binary array like you said? I'm assuming a binary array, which is class logical, though a double will work. You aren't passing in a string are you? Show me how you defined the array you passed in to the function.
LabelBinaryArray('011000110111001001111001011100000111010001101111')
Attempted to access bases(146); index out of bounds because numel(bases)=4.
Error in LabelBinaryArray (line 8) result((k+1)/2) = bases(index);
OK, you have a string, not a binary/logical array. To convert to a binary array you need to do this:
str = '011000110111001001111001011100000111010001101111'
binaryArray = logical(str - '0')
You can do that either inside the function, if you want to pass a string, or in your main program before you call the function, if you want to pass a binary array.
it worked. THANKS
How to decrypt it back to binary sequence
Decrypt what back to a binary sequence. You have the binary sequence both as a character array, str, and a logical/boolean array, binaryArray. What else do you want? What's missing? Just keep both things and you're all set.
Pls Can I get the decryption code
Shiva, I'm not sure who you're asking, but personally I don't have any to give you.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!