I have already converted user input of DNA code (A,T,G,C)
into RNA code(A,U,G,C)
. This was fairly easy
RNA_Code=DNA_Code.replace('T','U')
Now the next thing I need to do is convert the RNA_Code into it's compliment strand. This means I need to replace A with U, U with A, G with C and C with G, but all simultaneously.
if I say
RNA_Code.replace('A','U')
RNA_Code.replace('U','A')
it converts all the As into Us then all the Us into As but I am left with all As for both.
I need it to take something like AUUUGCGGCAAA
and convert it to UAAACGCCGUUU
.
Any ideas on how to get this done?(3.3)
Use a translation table:
The
str.translate()
method takes a mapping from codepoint (a number) to replacement character. Theord()
function gives us a codepoint for a given character, making it easy to build your map.Demo:
You can use a mapping dictionary:
I have a simple solution:
If you're not already using it, I suggest trying out Biopython. It has all sorts of functions for dealing with biological data, including a pretty cool
Seq
object. There is areverse_complement()
function that does exactly what you're trying to do, and a bunch more that you might not even have thought of yet. Check it out, it's a real time-saver.