Python command line tricks for reverse primer design
Oct 7th, 2008 by harijay
If I were to list one of the most attractive things about python , the interactive command line would rank among the top few.
Well today I was forced with a rather simple task. I had to order a primer for a sub-cloning experiment that was the reverse primer corresponding to the last twenty bases of a given protein coding gene sequence.
Here is how python made the task super easy
1) First I opened the pubmed page with my sequence and copied the forward strand to my interactive python session
2) Then I wrote a couple of lines to generate the reverse complement at the same time reversing the order of the string
>>> x="GGATGAAGTCCAGATA">>> c={"A":"T","T":"A","G":"C","C":"G"}
>>> p=""
>>> for i in x :
... p = c[i] + p
...
>>> p
'TATCTGGACTTCATCC'
So in the above example the
c = {"A":"T","T":"A","G":"C","C":"G"}Is the reverse complement dictionary. The for loop replaces each nucleotide with its complement and simultaneously reverses the order , because every nucleotide is added to the begining ( i.e prepended) . So the last nucleotide ends up first.
The other way to do the same thing would be to use python string reversal as demonstrated in the code below
>>> x="GGATGAAGTCCAGATA">>> c={"A":"T","T":"A","G":"C","C":"G"}
>>> for i in x :
... p = p + c[i]
...
>>> p
'CCTACTTCAGGTCTAT'
>>> reverse_5prime_3prime = p[::-1]
>>> reverse_5prime_3prime
'TATCTGGACTTCATCC'
The cool part here is the funky and admittedly unreadable string-reversal syntax,
-
reverse_5prime_3prime = p[::-1]
So In python string reversal is powerful. The p[::-1] basically says , give me the string from start to finish in backwards order (-1) . This elegant string reversal in python is explained well in this post and gives us the primer sequence in the correct 5-prime to 3-prime order as required by the oligo-synthesizing order forms.