Transcribing: the “other” way
Section 1 January 23rd, 2007We have seen how to transcribe DNA using regular expression, even though the regex we used cannot be considered a real one. Now we are going to simplify our small script even more and take advantage of some string capabilities of Python. Instead of using two lines, we are going to use only one. And also we won’t need to import anything.
Let’s start again with the same DNA sequence
myDNA = 'ACGTTGCAACGTTGCAACGTTGCA'
This time we are going to use replace. This is one of the Python’s methods to manipulate strings. Basically, we are asking the interpreter to replace a certain string by another. The method returns a new copy of your string:
myRNA = myDNA.replace('T', 'U')
This tells Python: myRNA will receive a copy of myDNA where all Ts were changed by Us. the “dot” after myDNA means that the method replace will get that variable as input on that variable.
So our code from above would like this
#! /usr/bin/env python
myDNA = 'ACGTTGCAACGTTGCAACGTTGCA'
myRNA = myDNA.replace('T', 'U')
print myRNA
Simple and efficient. Next we will use the same approach on generating the reverse complement of a DNA sequence, with no regex pattern.
March 11th, 2008 at 4:23 pm
[...] long time ago when the blog was still based on the Perl book we have seen how to transcribe DNA to RNA. This entry serves only to remember the method and add a function to the fasta module in the [...]