Transcription of a Template (Noncoding) Strand String with Python
I have always flirted with computer languages, ever since my years with BASIC and Pascal in high school (ah the hey day of the Apple II and TRS80). Recently I have been coming back to it as an interesting distraction and a way to deal with some problems in teaching things like population genetics and evolution. Obviously the days of BASIC and Pascal are long gone (though you can still get them), and it is the land of C++, PHP, Python, and many others. I don’t really mind learning a new language, as they all make a type of logical sense to me (more so than say French or Japanese), so I have dabbled in a number of them. My current language of choice is python (2.7, not 3.0) as it runs on pretty much anything and can be installed on a USB key to help move things around.
One thing I don’t get about the newest languages is the obsession with Object Oriented Programming (OOP). I grew up in the age of the Procedural Program (main program with subroutines) and still think in that paradigm. While I can see some uses for OOP in VERY large programs, I think it isn’t that big of an advantage for the types of programs I write, especially since I will probably eventually head towards the even more obscure Functional Programming paradigm when I get bored. The beauty of Python is that while it has OOP properties, it does not require it, so I can program it just fine without the need for going into OOP (the other non-OOP languages, PERL and C are also ones I dabble in).
Today I was working on a program to demonstrate basic genetic processes and then test the students with random sequences. I was writing a function to produce the reverse complement RNA sequence of a given DNA sequence and have a bugger of time getting it right. Admittedly there are “packages” for Python that have the function I am looking for baked in (i.e. biopython), but I view it as an intellectual challenge to figure out how to do it with just the basic tools (kind of like knowing how to solve a problem before using the calculator). It took me a while to figure it out (given my primitive knowledge of Python at this point), but I got it. When I decided to compare my solution to others I found that there were no easy examples on the web for producing the reverse complement of a given sequence. I am sure there are more elegant solutions, but here is mine.
#Function to transcribe template sequences
def ttranscription(sequence):
tsequence="" #initializing variables, not all needed, but habit
i=0
j=0
tempsequence=list(sequence) #casting the string into a mutable list
#iterative loop that goes through the list and replaces the bases
for i,j in enumerate(tempsequence):
if j=="A":
tempsequence[i]="U"
elif j=="T":
tempsequence[i]="A"
elif j=="G":
tempsequence[i]="C"
elif j=="C":
tempsequence[i]="G"
#iterative loop that produces a new string from the list data
for i,j in enumerate(tempsequence):
tsequence+=j
tsequence=tsequence[::-1] #reverses the sequence
return tsequence #returns the reverse complement sequence to the program
Obviously, this is just a quick and dirty logic exercise with no error checking or elaboration. I just thought I would post it.